question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Conversion from h5 to Tensorflow pb file

See original GitHub issue

I trained a yolov3 model (not the tiny version), and now I am trying to convert the saved yolo h5 file to Tensorflow pb, and so far I haven’t had luck. This is how I am trying to do the conversion:

import tensorflow as tf
from tensorflow.python.framework import graph_io
from tensorflow.keras.models import load_model


# Clear any previous session.
tf.keras.backend.clear_session()

def freeze_graph(graph, session, output, save_pb_dir='.', save_pb_name='yoloModel.pb', save_pb_as_text=False):
    with graph.as_default():
        graphdef_inf = tf.graph_util.remove_training_nodes(graph.as_graph_def())
        graphdef_frozen = tf.graph_util.convert_variables_to_constants(session, graphdef_inf, output)
        graph_io.write_graph(graphdef_frozen, save_pb_dir, save_pb_name, as_text=save_pb_as_text)
        return graphdef_frozen

tf.keras.backend.set_learning_phase(0)

model = load_model('yoloModel.h5')

session = tf.keras.backend.get_session()

INPUT_NODE = [t.op.name for t in model.inputs]
OUTPUT_NODE = [t.op.name for t in model.outputs]
print(INPUT_NODE, OUTPUT_NODE)
frozen_graph = freeze_graph(session.graph, session, [out.op.name for out in model.outputs], save_pb_dir=save_pb_dir)

I have seen other issues where people asked the same question and seems like some of them solved their problem. But I could not really understand some of the comments since they were in Chinese.

Could anyone who succeeded in converting the file, share his/her script?

Best, Frank

Issue Analytics

  • State:open
  • Created 4 years ago
  • Comments:10

github_iconTop GitHub Comments

11reactions
tiancailincommented, Jan 9, 2020
1reaction
frankbrewercommented, Jan 9, 2020

@tiancailin I tried your suggestion, but I guess according to output h5 file does not have the model information?

(conda-tensorflow) C:\Users\Usr2192\Desktop\conversionTry\amir-abdi>python keras_to_tensorflow.py --input_model="trainedWeights.h5" --output_model="out.pb"
Using TensorFlow backend.
E0109 13:46:07.891747 12844 keras_to_tensorflow.py:95] Input file specified only holds the weights, and not the model definition. Save the model using model.save(filename.h5) which will contain the network architecture as well as its weights. If the model is saved using the model.save_weights(filename) function, either input_model_json or input_model_yaml flags should be set to to import the network architecture prior to loading the weights.
Check the keras documentation for more details (https://keras.io/getting-started/faq/)
Traceback (most recent call last):
  File "keras_to_tensorflow.py", line 182, in <module>
    app.run(main)
  File "C:\Users\Usr2192\Anaconda3\envs\conda-tensorflow\lib\site-packages\absl\app.py", line 299, in run
    _run_main(main, args)
  File "C:\Users\Usr2192\Anaconda3\envs\conda-tensorflow\lib\site-packages\absl\app.py", line 250, in _run_main
    sys.exit(main(argv))
  File "keras_to_tensorflow.py", line 128, in main
    model = load_model(FLAGS.input_model, FLAGS.input_model_json, FLAGS.input_model_yaml)
  File "keras_to_tensorflow.py", line 106, in load_model
    raise wrong_file_err
  File "keras_to_tensorflow.py", line 63, in load_model
    model = keras.models.load_model(input_model_path)
  File "C:\Users\Usr2192\Anaconda3\envs\conda-tensorflow\lib\site-packages\keras\engine\saving.py", line 492, in load_wrapper
    return load_function(*args, **kwargs)
  File "C:\Users\Usr2192\Anaconda3\envs\conda-tensorflow\lib\site-packages\keras\engine\saving.py", line 584, in load_model
    model = _deserialize_model(h5dict, custom_objects, compile)
  File "C:\Users\Usr2192\Anaconda3\envs\conda-tensorflow\lib\site-packages\keras\engine\saving.py", line 270, in _deserialize_model
    model_config = h5dict['model_config']
  File "C:\Users\Usr2192\Anaconda3\envs\conda-tensorflow\lib\site-packages\keras\utils\io_utils.py", line 318, in __getitem__
    raise ValueError('Cannot create group in read-only mode.')
ValueError: Cannot create group in read-only mode.
Read more comments on GitHub >

github_iconTop Results From Across the Web

Tensorflow (.pb) format to Keras (.h5) - Stack Overflow
Complete working Code to convert a Model from Tensorflow Saved Model Format (pb) to Keras Saved Model Format (h5) is shown below:
Read more >
Convert Keras '.h5' Model to TensorFlow SavedModel ...
Hello,. Is the conversion from a trained Keras model (.h5) to a TensorFlow model (either TF1 or TF2) possible/supported by TensorFlow?
Read more >
How to convert model.h5 to model.pb?
There are different tools for converting hdf5 to .bp file as: 1 - convert trained Keras model to a single TensorFlow .pb file...
Read more >
Can we convert a .h5 (keras) file to a .pb (TensorFlow) model ...
It is possible to convert a . h5 file generated from training in Keras to a frozen . pb model. I would like...
Read more >
How to convert trained Keras model to a single TensorFlow ...
Keras to TensorFlow .pb file ... When you have trained a Keras model, it is a good practice to save it as a...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found