我在张量流中非常新,并且想在C++环境中使用预先训练的模型(Python)进行推理。据我了解,为此,我需要使用“freeze_graph”工具冻结训练有素的模型。冻结模型时出错(freeze_graph)
下面的代码片段是如何寻找一个非常简单的模型MNIST:
with tf.Session(config=config) as s:
s.run(tf.global_variables_initializer())
for i in range(n):
batch = mnist.train.next_batch(50)
train_step.run(feed_dict={x: batch[0], y_: batch[1], keep_prob: 0.5})
print('test accuracy %g' % accuracy.eval(feed_dict={x: mnist.test.images, y_: mnist.test.labels, keep_prob: 1.0}))
saver.save(s, 'models/saved_checkpoint')
with tf.Session(config=config) as s:
# save the graph definition
tf.train.write_graph(s.graph_def, 'models', "graph_def.pbtxt")
freeze_graph.freeze_graph(input_graph = "models/graph_def.pbtxt", input_saver = "", input_binary = False, input_checkpoint = "models/saved_checkpoint", output_node_names = "output_node", restore_op_name = "save/restore_all", filename_tensor_name = "save/Const:0", output_graph = "frozen_graph.pb", clear_devices = True, initializer_nodes = "")
这样做,这样我得到了以下错误:
File "mnist.py", line 180, in main output_graph = "frozen_graph.pb", clear_devices = True, initializer_nodes = "")
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/tools/freeze_graph.py", line 184, in freeze_graph variable_names_blacklist)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/tools/freeze_graph.py", line 87, in freeze_graph_with_def_protos _ = importer.import_graph_def(input_graph_def, name="")
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/importer.py", line 313, in import_graph_def op_def=op_def) File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/ops.py", line 2633, in create_op self._add_op(ret)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/ops.py", line 2312, in _add_op "is already used" % op.name) ValueError: cannot add op with name conv1/Variable/Adam as that name is already used
没有任何人有任何想法这里可能有什么问题?我正在使用tensorflow 1.3和python 2.7。不幸的是,我找不到关于图冻结的很多信息,并且可用示例不适用于我...
在此先感谢您的任何建议!
最佳, 阿列克谢