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?