6

I've retrained my model using tensorflow and now want to use keras to avoid session stuffs. How can I convert .pb file to .h5

1

2 Answers 2

4
import tensorflow as tf from tensorflow.keras.models import save_model, Sequential model_path = r"c:\temp\model.pb" model = tf.keras.models.load_model(model_path) save_model(model,model_path + r"\new_model.h5", save_format='h5') 
Sign up to request clarification or add additional context in comments.

6 Comments

Although this code might solve the problem, a good answer should explain what the code does and how it helps.
@BDL generally I tend to agree, but in this specific case I am pretty sure the code is self explanatory. i.e. "tf.keras.models.load_model" will load a tensor flow model, while save_model with argument save_format='h5' will, hmmm, save it as an h5 keras file.
@nivpeled I tried same code and got AttributeError: '_UserObject' object has no attribute '_is_graph_network'
I get OSError: SavedModel file does not exist at: saved_model_dir/{saved_model.pbtxt|saved_model.pb} error
AttributeError: 'AutoTrackable' object has no attribute '_is_graph_network' .I am also getting this error, have you solved?
|
1

I found this link useful: https://backstreetcoder.com/convert-tensorflow-pb-model-to-keras-h5-model/

Apparently, you need to use the enclosing directory name and not the .pb filename when loading the model initially. This solves the error:

SavedModel file does not exist at: saved_model_dir/{saved_model.pbtxt|saved_model.pb} 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.