question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to directly rebuild the model from saved zip file?

See original GitHub issue

Currently I want to rebuild the stable baseline model in keras and pytorch. It seems that the model cannot directly save to a h5 file. Thus I want to rebuild from the zip file save in this code.

log_dir = "test_save/"
model_dqn.save(log_dir + "pong")

This file contains the model structure in parameter_list, and weights in parameters. However, I cannot rebuild the model from these two files with the code below:

# load json and create model
json_file = open('parameter_list', 'r')
loaded_model_json = json_file.read()
json_file.close()
loaded_model = model_from_json(loaded_model_json)

loaded_model.load_weights("parameters")

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:11

github_iconTop GitHub Comments

1reaction
xinghua-qucommented, Mar 12, 2020

Please take a look at the documentation on exporting models. You have to get the parameters as numpy arrays with get_parameters, and map these to correct Keras layers. I had a mapping like this for Keras model to stable-baselines. The key tells stable-baselines parameter name, and value is a lambda function that retrieves corresponding layer from a Keras model.

# Mapping from stable-baselines agent variable
# name to a function that takes in keras model
# and assigns right variables
CNN_PARAMS = {
    "model/cnn1/w:0": lambda model: model.get_layer("conv2d_1").get_weights()[0],
    "model/cnn1/b:0": lambda model: model.get_layer("conv2d_1").get_weights()[1][None, :, None, None],
    "model/cnn2/w:0": lambda model: model.get_layer("conv2d_2").get_weights()[0],
    "model/cnn2/b:0": lambda model: model.get_layer("conv2d_2").get_weights()[1][None, :, None, None],
    "model/cnn3/w:0": lambda model: model.get_layer("conv2d_3").get_weights()[0],
    "model/cnn3/b:0": lambda model: model.get_layer("conv2d_3").get_weights()[1][None, :, None, None],
    "model/cnn_fc1/w:0": lambda model: model.get_layer("dense_1").get_weights()[0],
    "model/cnn_fc1/b:0": lambda model: model.get_layer("dense_1").get_weights()[1]
}

Another way would be: extracting the dqn model from the build TF graph and save it independently.

This is planned for v3 with PyTorch backend.

Thanks for your reply. This mapping looks like ignore the last fully connected layer (i.e., from 512 hidden nodes to the action outputs). It would be soooo nice if you can share one example for loading from pkl file and then converting weights to a keras model. In that way, I think people can also save models to other formats (e.g., .h5 that is mentioned by ChunJyeBehBeh). However, this is based on your team has such an extension plan. Thanks so much for your help anyway:)

1reaction
araffincommented, Mar 10, 2020

Note to self: For v3, I should look into a possible common format for saving the model architecture as well, if in any way possible (in a convenient way).

ONNX export ? or tracing using pytorch jit?

Read more comments on GitHub >

github_iconTop Results From Across the Web

structure and functioning of a ZIP file - Computer forensics
In this computer forensics tutorial you can learn how to restore a ZIP file manually. We go through the whole process step by...
Read more >
Save and load Keras models | TensorFlow Core
# Train the model. # Calling `save('my_model')` creates a SavedModel folder `my_model`. # It can be used to reconstruct the model identically.
Read more >
How do I reassemble a zip file that has been emailed in ...
Open the folder where the split folders are stored. · Right-click on the first of the folders and left click to select the...
Read more >
How to save/restore a model after training? - Stack Overflow
If you restore to continue to train, just use the Saver checkpoints. If you save the model to do reference, just the tensorflow...
Read more >
A quick complete tutorial to save and restore Tensorflow models
Hence, Tensorflow model has two main files: a) Meta graph: This is a protocol buffer which saves the complete Tensorflow graph; i.e. all...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found