Tensorflow freeze_graph script failing on model defined with Keras

前端 未结 2 1389
小鲜肉
小鲜肉 2021-01-01 02:02

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

相关标签:
2条回答
  • 2021-01-01 02:32

    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)
    
    0 讨论(0)
  • 2021-01-01 02:33

    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.

    0 讨论(0)
提交回复
热议问题