title: How to convert model from HDF5 format to pb?
body: <p>I am using TensorFlow v2.3.0. I would like to convert a model I saved as HDF5 with a .h5 extension to protocol buffers (.pb file).
  There are <a href="https://www.dlology.com/blog/how-to-convert-trained-keras-model-to-tensorflow-and-make-prediction/" rel="nofollow noreferrer">existing solutions</a> that I have tried but do not work directly because they were written for TensorFlow v1.x.x. So I tried editing the code to make them compatible with TensorFlow v2.3.0 and I ended up with this code:</p>
  <pre><code>import tensorflow as tf
  from tensorflow.keras.models import load_model
  from tensorflow.compat.v1.keras.backend import get_session
  from tensorflow.python.platform import gfile
  from tensorflow.compat.v1 import global_variables
  from tensorflow.compat.v1.graph_util import convert_variables_to_constants as c_to_c
  
  model = load_model('models/model-v2.h5')
  
  # print(model.summary())
  Model: &quot;functional_1&quot;
  _________________________________________________________________
  Layer (type)                 Output Shape              Param #   
  =================================================================
  input_1 (InputLayer)         [(None, 150, 150, 3)]     0         
  _________________________________________________________________
  conv2d (Conv2D)              (None, 148, 148, 16)      448       
  _________________________________________________________________
  max_pooling2d (MaxPooling2D) (None, 74, 74, 16)        0         
  _________________________________________________________________
  conv2d_1 (Conv2D)            (None, 72, 72, 32)        4640      
  _________________________________________________________________
  max_pooling2d_1 (MaxPooling2 (None, 36, 36, 32)        0         
  _________________________________________________________________
  conv2d_2 (Conv2D)            (None, 34, 34, 64)        18496     
  _________________________________________________________________
  max_pooling2d_2 (MaxPooling2 (None, 17, 17, 64)        0         
  _________________________________________________________________
  flatten (Flatten)            (None, 18496)             0         
  _________________________________________________________________
  dense (Dense)                (None, 512)               9470464   
  _________________________________________________________________
  dropout (Dropout)            (None, 512)               0         
  _________________________________________________________________
  dense_1 (Dense)              (None, 1)                 513       
  =================================================================
  Total params: 9,494,561
  Trainable params: 9,494,561
  Non-trainable params: 0
  
  # print(model.outputs)
  # [&lt;tf.Tensor 'dense_1/Sigmoid:0' shape=(None, 1) dtype=float32&gt;]
  # print(model.inputs)
  # [&lt;tf.Tensor 'input_1:0' shape=(None, 150, 150, 3) dtype=float32&gt;]
  
  
  def freeze_session(session, keep_var_names=None, output_names=None, clear_devices=True):
      graph = session.graph
      with graph.as_default():
          freeze_var_names = list(set(v.op.name for v in global_variables()).difference(keep_var_names or []))
          output_names = output_names or []
          output_names += [v.op.name for v in global_variables()]
          # Graph -&gt; GraphDef ProtoBuf
          input_graph_def = graph.as_graph_def()
          if clear_devices:
              print(input_graph_def.node)
              for node in input_graph_def.node:
                  print('Node',node)
                  node.device = &quot;&quot;
          frozen_graph = c_to_c(session, input_graph_def, output_names, freeze_var_names)
          return frozen_graph
  
  frozen_graph = freeze_session(tf.compat.v1.Session(),
                              output_names=[out.op.name for out in model.outputs])
  
  # Save to model/model.pb
  tf.io.write_graph(frozen_graph, &quot;models&quot;, &quot;model_v2.pb&quot;, as_text=False)
  </code></pre>
  <p>I am however encountering this error: <strong>AssertionError: dense_1/Sigmoid is not in graph</strong>. Apparently the graph_def is returning an empty list because nothing is being printed as I loop through the nodes. I would like to know how to correct this code or if there is a better alternative to converting a .h5 model to .pb</p>