I\'m attempting to export a model built and trained with Keras to a protobuffer that I can load in a C++ script (as in this example). I\'ve generated a .pb file containing t
The problem is the order of these two lines in your original program:
tf.train.write_graph(graph_def=sess.graph.as_graph_def(), logdir='.', name='simple_as_binary.pb', as_text=False)
tf.train.Saver().save(sess, 'simple.ckpt')
Calling tf.train.Saver()
adds a set of nodes to the graph, including one called "save/restore_all"
. However, this program calls it after writing out the graph, so the file you pass to freeze_graph.py
doesn't contain those nodes, which are necessary for doing the rewriting.
Reversing the two lines should make the script work as intended:
tf.train.Saver().save(sess, 'simple.ckpt')
tf.train.write_graph(graph_def=sess.graph.as_graph_def(), logdir='.', name='simple_as_binary.pb', as_text=False)
So, I got it working. Sort of.
By using tensorflow.python.client.graph_util.convert_variables_to_constants
directly instead of first saving GraphDef
and a checkpoint to disk and then using the freeze_graph
tool/script, I have been able to save a GraphDef
containing both graph definition and variables converted to constants.
EDIT
mrry updated his answer, which solved my issue of freeze_graph not working, but I'll leave this answer as well, in case anyone else could find it useful.