How to convert a pretrained tensorflow pb frozen graph into a modifiable h5 keras model? - python

I have been searching for a method to do this for so long, and I can not find an answer. Most threads I found are of people wanting to do the opposite.
Backstory:
I am experimenting with some pre-trained models provided by the tensorflow/models repository. The models are saved as .pb frozen graphs. I want to fine-tune some of these models by changing the final layers to suit my application.
Hence, I want to load the models inside a jupyter notebook as a normal keras h5 model.
How can I do that?
do you have a better way to do so?
Thanks.

seems like all you would have to do is download the model files and store them in a directory. Call the directory for example c:\models. Then load the model.
model = tf.keras.models.load_model(r'c:\models')
model.summary() # prints out the model layers
# generate code to modify the model as you typically do for transfer learning
# compile the changed model
# train the model
# save the trained model as a .h5 file
dir=r'path to the directory you want to save the model to'
model_identifier= 'abcd.h5' # for abcd use whatever identification you want
save_path=os.path.join(dir, model_identifier)
model.save(save_path)

Related

How to save model which is based on tensorlayer as 'savedmodel' format?

I trained an NN model by Tensorlayer (a simple ABR algorthim based on RL Policy Gradient). Now I want to save it as 'savedmodel' format in tensorflow because I want to use tensorflow.js. In Tensorlayer I use tl.files.save_weights_to_hdf5('model/pg_policy.hdf5', self.model) to save my model. After that, I got a .hdf5 model file. But when I want to load my model, I must do it in my source code class.
So I don't know how to save my Tensorlayer model to 'savedmodel' format (https://tensorflow.google.cn/guide/saved_model). Or is there any way I can turn my Tensorlayer model to tensorflow model?
Here is my simple NN build by Tensorlayer, and sorry I am new to this area.
enter image description here

Save entire model but load weights only

I have defined a deep learning model my_unet() in tensorflow. During training I set save_weigths=False since I wanted to save the entire model (not only the wieghts bu the whole configuration). The generated file is path_to_model.hdf5.
However, when loading back the model I used the earlier version (I forgot to update it) in which I first called the model and then load the model using:
model = my_unet()
model.load_weights(path_to_model.hdf5)
Instead of simply using: model = tf.keras.models.load_model(path_to_model.hdf5) to load the entire model.
Both ways of loading the model and the weights provided the same predictions when run in some dummy data and there were no errors.
My question is: Why loading the entire model using model.load_weights() does not generate any problem? What is the structure of the hdf5 file and how theese two ways of loading exactly work? Where can I find this information?
You can please see the documentation here for any future reference: http://davis.lbl.gov/Manuals/HDF5-1.8.7/UG/03_DataModel.html

Loading a pre-trained model in Chainer Deep Learning Framework

I need to load a pre-trained model in Chainer framework, but as I understood, the saved (.npz) file only contains the weights and I have to reconstruct the model then load the weights into it, and there is no way to load the full model in one command like Tensorflow.
Is this true? I so, anyone with Chainer framework experience can provide some guidance? If not, what is the proper way to load a pre-trained model in the mentioned framework?
Yes, only npz files only contain weights. You need to first construct an instance of the model (a subclass of chainer.Chain), then load weights on it using load_npz. https://docs.chainer.org/en/stable/guides/serializers.html

Saving Model Checkpoint vs Saving Entire model in Keras

Which method is best, whether saving model checkpoints or saving entire model to disk for each epochs. Why nobody saves the entire model?
A keras model has two things, an architecture and weights. If you save the whole model in each checkpoint, you’re saving the architecture every time. For this reason the best on training is to save only weight and use the wireframe in memory.
On tensorflow.keras documentation have more about other methods.
Checkpoints are used to save your model if in case your system crashes or code interrupted while training so when you start training your model again after crashes you don't have to start from scratch.Checkpoints capture the exact value of all parameters (tf.Variable objects) used by a model. Checkpoints do not contain any description of the computation defined by the model.
The SavedModel format on the other hand includes a serialized description of the computation defined by the model in addition to the parameter values (checkpoint). Models in this format are independent of the source code that created the model.
you can see the above info in the official doc of tensorflow. #R Nanthak

Saving model in tensorflow

Tensorflow allows us to save/load model's structure, using method tf.train.write_graph, so that we can restore it in the future to continue our training session. However, I'm wondering that if this is necessary because I can create a module, e.g GraphDefinition.py, and use this module to re-create the model.
So, which is the better way to save the model structure or are there any rule of thumb that suggest which way should I use when saving a model?
First of all you have to understand, that tensorflow graph does not have current weights in it (until you save them manually there) and if you load model structure from graph.pb, you will start you train from the very beginning. But if you want to continue train or use your trained model, you have to save checkpoint (using tf Saver) with the values of the variables in it, not only the structure.
Check out this tread: Tensorflow: How to restore a previously saved model (python)

Categories

Resources