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
Asked
Active
Viewed 7,566 times
6

Sagar P. Ghagare
- 542
- 2
- 12
- 25

Avinash Kumar
- 298
- 3
- 13
-
Does https://github.com/keras-team/keras/issues/8026 help you? – keineahnung2345 Feb 19 '19 at 14:28
2 Answers
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')

nivpeled
- 1,810
- 5
- 17
-
1Although this code might solve the problem, a good answer should explain **what** the code does and **how** it helps. – BDL Jun 23 '20 at 13:53
-
@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 Jun 24 '20 at 06:22
-
@nivpeled I tried same code and got AttributeError: '_UserObject' object has no attribute '_is_graph_network' – H.H Aug 09 '20 at 14:55
-
I get OSError: SavedModel file does not exist at: saved_model_dir/{saved_model.pbtxt|saved_model.pb} error – AnupamChugh Aug 29 '20 at 08:50
-
AttributeError: 'AutoTrackable' object has no attribute '_is_graph_network' .I am also getting this error, have you solved? – maryam mehboob Jul 01 '21 at 14:29
-
model_path should be the path of directory not path of file so should use like "c:\temp" – Ali Aug 28 '21 at 21:25
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}

Nnamdi Affia
- 11
- 5