freezing an keras model

Hi experts

I have used vgg16 in transfer learning keras case:
conv_base=VGG16(weights=‘imagenet’,include_top=False, input_shape=(224,224,3))

And trained in on my own data.The model has been saved as a h5 format.

I want to run it on jetson nano with tensorrt. I then use:

def save(model, filename):
# First freeze the graph and remove training nodes.
output_names = model.output.op.name
sess = tf.keras.backend.get_session()
frozen_graph = tf.graph_util.convert_variables_to_constants(sess, sess.graph.as_graph_def(), [output_names])
frozen_graph = tf.graph_util.remove_training_nodes(frozen_graph)
# Save the model
with open(filename, “wb”) as ofile:
ofile.write(frozen_graph.SerializeToString())

save(min_model,“c:/projects/kort/fun.pb”)

But get the following error:

save(min_model,“c:/projects/kort/fun.pb”)
WARNING:tensorflow:From :5: convert_variables_to_constants (from tensorflow.python.framework.graph_util_impl) is deprecated and will be removed in a future version.
Instructions for updating:
Use tf.compat.v1.graph_util.convert_variables_to_constants
WARNING:tensorflow:From C:\Users\sojohans\AppData\Local\Continuum\miniconda3\pythonsdk\envs\tensor113\lib\site-packages\tensorflow\python\framework\graph_util_impl.py:245: extract_sub_graph (from tensorflow.python.framework.graph_util_impl) is deprecated and will be removed in a future version.
Instructions for updating:
Use tf.compat.v1.graph_util.extract_sub_graph
Traceback (most recent call last):
File “C:\Users\sojohans\AppData\Local\Continuum\miniconda3\pythonsdk\envs\tensor113\lib\site-packages\tensorflow\python\client\session.py”, line 1334, in _do_call
return fn(*args)
File “C:\Users\sojohans\AppData\Local\Continuum\miniconda3\pythonsdk\envs\tensor113\lib\site-packages\tensorflow\python\client\session.py”, line 1319, in _run_fn
options, feed_dict, fetch_list, target_list, run_metadata)
File “C:\Users\sojohans\AppData\Local\Continuum\miniconda3\pythonsdk\envs\tensor113\lib\site-packages\tensorflow\python\client\session.py”, line 1407, in _call_tf_sessionrun
run_metadata)
tensorflow.python.framework.errors_impl.FailedPreconditionError: Attempting to use uninitialized value dense_1/kernel
[[{{node _retval_dense_1/kernel_0_27}}]]

Thanks sojohans

Hi Sojohnas,

Have you figured out how to freeze your Keras model? I have the same task at hand and I am researching the options to do it. Any advice or direction you have to offer would be greatly appreciated.

Thank you,
Borislav

https://github.com/orcund/KerasFreezer just a small scrtipt to freeze your .h5 models.