0

How do I convert a .h5 weight file to .pb file ? I trained keras pre-trained model and saved the file as something.h5 . How do i convert it to .pb file for using it for tensorflow serving ?

P.S: Don't degrade the question, I couldn't find any solution online. Do mention the reason why you degraded the question. Otherwise, help with solving the question.

Sai Krishnadas
  • 2,863
  • 9
  • 36
  • 69
  • 1
    did you look here [how-to-export-keras-h5-to-tensorflow-pb](https://stackoverflow.com/questions/45466020/how-to-export-keras-h5-to-tensorflow-pb) – sxeros Feb 11 '20 at 10:49
  • does this answer your question? https://datascience.stackexchange.com/questions/57310/how-to-convert-model-h5-to-model-pb or may be this: https://www.dlology.com/blog/how-to-convert-trained-keras-model-to-tensorflow-and-make-prediction/ – Drako Feb 11 '20 at 10:49
  • I don't believe You searched on the net - those answers in comments are right from the first page of results from google – Drako Feb 11 '20 at 10:51
  • Tensorflow also has a tutorial on saving .h5 and .pb – jkr Feb 11 '20 at 14:09
  • @jakub ofc they have a tutorial . I couldn't underatand much over there thats why posted it on stackoverflow ! – Sai Krishnadas Feb 12 '20 at 08:10
  • -1 you do not show what you have already tried, and tensorflow has gone through lots of effort to publish excellent tutorials such as [Save and load models](https://www.tensorflow.org/tutorials/keras/save_and_load#save_the_entire_model) and [Using the SavedModel format](https://www.tensorflow.org/guide/saved_model#creating_a_savedmodel_from_keras). – jkr Feb 12 '20 at 15:03

1 Answers1

3
import tensorflow as tf
model = tf.keras.models.load_model(keras_model_path)
tf.saved_model.save(model, saved_model_path)

Bingo.

Eric McLachlan
  • 3,132
  • 2
  • 25
  • 37
  • ImportError: DLL load failed: The specified module could not be found. – Sai Krishnadas Feb 12 '20 at 12:18
  • That is too little information for me to be able to help you. It sounds like you don't have something installed. but I don't know what. I suggest you refer to the official documentation for the basics of loading and saving: https://www.tensorflow.org/tutorials/keras/save_and_load. This document covers both h5 and pb file formats. – Eric McLachlan Feb 12 '20 at 12:28
  • 1
    instead of `tf.saved_model.save(model, saved_model_path)`, you can also use `model.save(saved_model_path)` – jkr Feb 12 '20 at 15:04
  • 1
    if you do not include an extension, it will default to savedmodel format – jkr Feb 12 '20 at 19:25