Burton2000
Burton2000

Reputation: 2072

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?

Upvotes: 7

Views: 4786

Answers (2)

EphC
EphC

Reputation: 11

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.

Upvotes: 1

Jiten Patel
Jiten Patel

Reputation: 180

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

Upvotes: 3

Related Questions