Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to load a keras model saved as .pb

I'd like to load a keras model that i've trained and saved it as .pb.
Here's the code,
enter image description here

Am using a jupyter notebook.
The model is successfully saved as saved_model.pb under the same directory. But the code is unable to access it.

Can anybody see to it, how can i access this keras model that's saved in .pb extension.
I checked at several other places for solution but no luck.

Model is saved at model/saved_model.pb.
I've taken out the .pb file and placed it in the same directory where my code file exists.

like image 892
Aditya Nikhil Avatar asked Jul 29 '20 05:07

Aditya Nikhil


People also ask

What is the use of saved model in keras?

SavedModel is the more comprehensive save format that saves the model architecture, weights, and the traced Tensorflow subgraphs of the call functions. This enables Keras to restore both built-in layers as well as custom objects. # Create a simple model. # Train the model. # Calling `save ('my_model')` creates a SavedModel folder `my_model`.

How are network weights written in keras?

The network weights are written to model.h5 in the local directory. The model and weight data is loaded from the saved files and a new model is created. It is important to compile the loaded model before it is used. This is so that predictions made using the model can use the appropriate efficient computation from the Keras backend.

What is Keras in Python?

Last Updated on August 27, 2020 Keras is a simple and powerful Python library for deep learning. Given that deep learning models can take hours, days and even weeks to train, it is important to know how to save and load them from disk.

How accurate is my LSTM keras model?

I am using an LSTM Keras model with 2 outputs (i.e predictions) for Slot and Intent filling. When I save, then load it (in json, yaml and single file format) , it provides random results. It seems like if the model does not fit well the loaded weights: The model has 84% of accuracy before saving, 20% after loading.


2 Answers

The function tf.keras.models.load_model load a SavedModel into a tf.keras -model. The argument of the function is path to a saved model.

So try model = tf.keras.models.load_model('model')

like image 50
sakumoil Avatar answered Oct 20 '22 00:10

sakumoil


You should load all model folder instead of loading .pb file. If you save model to './_models/vgg50_finetune' (I used this path in my project), you get folder vgg50_finetune with two .pb files (keras_metadata.pb and saved_model.pb) and two subfolders (assets and variables). The only thing you should do is use this code: model = tf.keras.models.load_model('./_models/vgg50_finetune') And you can both train model or use it for prediction.

like image 31
extnike Avatar answered Oct 19 '22 22:10

extnike