Tensorflow: freeze_graph in python does not save variable values

I am trying to freeze a chart already prepared using the python API, load and use it in C ++. I know that I should use freeze_graph.py and looked at the example here freeze_graph_test.py to find out 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 works 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 values ​​for the variables: the resulting file size is only 2 MB (just like the graph_def file saved by write_graph, and the breakpoint weighs 57 MB) and tries to run it in C ++ (after downloading from using ReadBinaryProto ) ReadBinaryProto 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 suppose I should abuse some of the freeze_graph options, but none of my attempts got anything better ...

EDIT 3 :

Using freeze_graph as a script does create a valid .pb file of approximately 19 MB in size

  • how can it be much smaller than the control point? Is everything really in it?

  • I would prefer the python version to work, you guys do not know why this is not so?

0
source share

Source: https://habr.com/ru/post/1268801/


All Articles