5

In Tensorflow, after training the model, I saved it use:

with tf.Session() as session: /** ------- Model training code goes here ------ **/ tf.train.write_graph(session.graph_def, '.', '../har.pbtxt') saver.save(session,save_path = "../har.ckpt") 

And to freeze and save the optimized model:

from tensorflow.python.tools import freeze_graph from tensorflow.python.tools import optimize_for_inference_lib freeze_graph.freeze_graph(input_graph = "../har.pbtxt", input_saver = "", input_binary = False, input_checkpoint = "../har.ckpt", output_node_names = "y_", restore_op_name = "save/restore_all", filename_tensor_name = "save/Const:0", output_graph = "frozen_har.pb", clear_devices = True, initializer_nodes = "") input_graph_def = tf.GraphDef() with tf.gfile.Open(output_frozen_graph_name, "r") as f: data = f.read() input_graph_def.ParseFromString(data) output_graph_def = optimize_for_inference_lib.optimize_for_inference( input_graph_def, ["input"], ["y_"], tf.float32.as_datatype_enum) f = tf.gfile.FastGFile("optimized_frozen_har.pb", "w") f.write(output_graph_def.SerializeToString()) 

However, I get the error:

Traceback (most recent call last):
File "C:\Users\asus\Desktop\cnn.py", line 176, in output_graph = "frozen_har.pb", clear_devices = True, initializer_nodes = "")
File "C:\Users\asus\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\tools\freeze_graph.py", line 122, in freeze_graph variable_names_blacklist=variable_names_blacklist) File "C:\Users\asus\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\framework\graph_util_impl.py", line 202, in convert_variables_to_constants inference_graph = extract_sub_graph(input_graph_def, output_node_names)
File "C:\Users\asus\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\framework\graph_util_impl.py", line 141, in extract_sub_graph assert d in name_to_node_map, "%s is not in graph" % d AssertionError: y_ is not in graph

I defined y_in my code as the output:

y_ = tf.nn.softmax(tf.matmul(f, out_weights) + out_biases) 

What seens to be the problem?

1 Answer 1

4

When you use,

y_ = tf.nn.softmax(tf.matmul(f, out_weights) + out_biases) 

y_ is not the name of the tensor. Please use the following, to explicitly name the tensor as y_.

y_ = tf.nn.softmax(tf.matmul(f, out_weights) + out_biases, name="y_") 
Sign up to request clarification or add additional context in comments.

1 Comment

Yeah! I figured it out too! what about for the weights? I have several layers of CNN and I define a function weight_variable(). How can I create a unique name of the weight for each weight variable?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.