Tensorflow: freeze_graph in python does not store the variable values

一曲冷凌霜 提交于 2019-12-11 04:17:10

问题


I'm trying to freeze a graph already trained with the python API, to load and use it in C++. I know I should use freeze_graph.py, and looked at the example here freeze_graph_test.py to learn how to use it. I have the following code in python:

def load_ckpt_and_save_graph(path_to_ckpt=None, output_dir=None, output_name="subsign_classifier_frozen.pb",
                             meta_graph=None):
    if output_dir is None:
        output_dir = OUTPUT_DIR
    if path_to_ckpt is None:
        path_to_ckpt = tf.train.latest_checkpoint(CKPT_DIR, LATEST_CKPT_FILENAME)
    if meta_graph is None:
        meta_graph = MODEL_FILENAME_PATH

    tf.reset_default_graph()
    with tf.Session() as sess:
        saver = tf.train.import_meta_graph(meta_graph)
        saver.restore(sess, path_to_ckpt)
        graph_path = tf.train.write_graph(sess.graph_def, logdir=output_dir, name=output_name, as_text=False)
        output_nodes = ["full_prediction/output_length_prediction", "full_prediction/output_class_prediction_1"]
        freeze_graph.freeze_graph(input_graph=graph_path, input_saver=saver._name, input_binary=True,
                                  input_checkpoint=path_to_ckpt, output_node_names=",".join(output_nodes),
                                  restore_op_name="save/restore_all", filename_tensor_name= "save/Const:0",  
                                  output_graph=output_name, clear_devices=True, initializer_nodes="",
                                  variable_names_blacklist="")
    log_and_print('load_ckpt_and_save_graph/ : Graph saved to %s' % output_name)

This code runs fine, and prints:

INFO:tensorflow:Froze 39 variables.
Converted 39 variables to const ops.
632 ops in the final graph.
load_ckpt_and_save_graph/ : Graph saved to subsign_classifier_frozen.pb

However, the result does not seem to contain the values for the variables: the resulting file size is only 2MB (just like the graph_def saved by write_graph, while the checkpoint weighs 57MB), and trying to run it in c++ (after loading with ReadBinaryProto) throws the following status:

Failed precondition: Attempting to use uninitialized value Classes_readout/Classes_logits3/bias
     [[Node: Classes_readout/Classes_logits3/bias/read = Identity[T=DT_FLOAT, _class=["loc:@Classes_readout/Classes_logits3/bias"], _device="/job:localhost/replica:0/task:0/cpu:0"](Classes_readout/Classes_logits3/bias)]]

I guess I must misuse some of the parameters of freeze_graph, but none of my tries got anything better...

EDIT 3:

Using freeze_graph as a script does generate a valid .pb file, of size roughly 19MB

  • how can it be that much smaller than the checkpoint ? Is there really everything in it ?

  • I would really prefer to get the python version working, do you guys have any idea why it is not ?

来源:https://stackoverflow.com/questions/43515671/tensorflow-freeze-graph-in-python-does-not-store-the-variable-values

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!