How to load a keras model saved as .pb

13,732

Solution 1

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')

Solution 2

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.

Share:
13,732
Aditya Nikhil
Author by

Aditya Nikhil

Updated on June 11, 2022

Comments

  • Aditya Nikhil
    Aditya Nikhil almost 2 years

    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.

  • Aditya Nikhil
    Aditya Nikhil over 3 years
    Oh okay it works, now how to access the model, i mean, if i type model.summary(), it's not showing anything.
  • sakumoil
    sakumoil over 3 years
    What if you load the model with tf.keras.models.load_model('model') and run model.summary() after that?