Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exporting a frozen graph .pb file in Tensorflow 2

I've beeen trying out the Tensorflow 2 alpha and I have been trying to freeze and export a model to a .pb graphdef file.

In Tensorflow 1 I could do something like this:

# Freeze the graph.
frozen_graph_def = tf.graph_util.convert_variables_to_constants(
    sess,
    sess.graph_def,
    output_node_names)

# Save the frozen graph to .pb file.
with open('model.pb', 'wb') as f:
    f.write(frozen_graph_def.SerializeToString())

However this doesn't seem possible anymore as convert_variables_to_constants is removed and use of sessions is discouraged.

I looked and found there is the freeze graph util https://github.com/tensorflow/tensorflow/blob/master/tensorflow/python/tools/freeze_graph.py that works with SavedModel exports.

Is there some way to do it within Python still or I am meant to switch and use this tool now?

like image 829
Burton2000 Avatar asked Mar 22 '19 12:03

Burton2000


People also ask

What is frozen inference graph in TensorFlow?

Frozen graphs are commonly used for inference in TensorFlow and are stepping stones for inference for other frameworks. TensorFlow 1. x provided an interface to freeze models via tf. Session , and I previously had a blog on how to use frozen models for inference in TensorFlow 1.

What is Model PB file?

The saved_model. pb file stores the actual TensorFlow program, or model, and a set of named signatures, each identifying a function that accepts tensor inputs and produces tensor outputs.


2 Answers

I have also faced this same problem while migrating from tensorflow1.x to tensoflow2.0 beta. This problem can be solved by 2 methods:

  1. 1st is to go to the tensflow2.0 docs search for the methods you have used and change the syntax for each line &
  2. To use google's tf_ugrade_v2 script

tf_upgrade_v2 --infile your_tf1_script_file --outfile converted_tf2_file

You try above command to change your tensorflow1.x script to tensorflow2.0, it will solve all your problem.

Also, you can rename the method (Manual step by refering documentation) Rename 'tf.graph_util.convert_variables_to_constants' to 'tf.compat.v1.graph_util.convert_variables_to_constants'

The measure problem is that in tensorflow2.0 is that many syntax and function has changed try referring the tensoflow2.0 docs or use the google's tf_upgrade_v2 script

like image 137
Jiten Patel Avatar answered Oct 19 '22 18:10

Jiten Patel


Not sure if you've seen this Tensorflow 2.0 issue, but this response seems to be a work-around:

https://github.com/tensorflow/tensorflow/issues/29253#issuecomment-530782763

Note: this hasn't worked for my nlp model but maybe it will work for you. The suggested work-around is to use model.save_weights('weights.h5') while in TF 2.0 environment. Then create new environment with TF 1.14 and do all following steps in TF 1.14 env. Build your model model = create_model() and use model.load_weights('weights.h5') to load weights back into your model. Then save entire model with model.save('final_model.h5'). If you manage to have success with the above steps, then follow the rest of the steps in the link to use freeze_graph.

like image 1
EphC Avatar answered Oct 19 '22 17:10

EphC