我无法在一个简单的,保存的TensorFlow图上成功运行 optimize_for_inference
模块(Python 2.7;由 pip install tensorflow-gpu==1.0.1
安装的软件包) .
背景
保存TensorFlow图
这是我的Python脚本,用于生成并保存一个简单的图形,以便为我的输入 x
placeholder
操作添加5 .
import tensorflow as tf
# make and save a simple graph
G = tf.Graph()
with G.as_default():
x = tf.placeholder(dtype=tf.float32, shape=(), name="x")
a = tf.Variable(5.0, name="a")
y = tf.add(a, x, name="y")
saver = tf.train.Saver()
with tf.Session(graph=G) as sess:
sess.run(tf.global_variables_initializer())
out = sess.run(fetches=[y], feed_dict={x: 1.0})
print(out)
saver.save(sess=sess, save_path="test_model")
恢复TensorFlow图
我有一个简单的恢复脚本,可以重新创建已保存的图形并恢复图形参数 . 保存/恢复脚本都生成相同的输出 .
import tensorflow as tf
# Restore simple graph and test model output
G = tf.Graph()
with tf.Session(graph=G) as sess:
# recreate saved graph (structure)
saver = tf.train.import_meta_graph('./test_model.meta')
# restore net params
saver.restore(sess, tf.train.latest_checkpoint('./'))
x = G.get_operation_by_name("x").outputs[0]
y = G.get_operation_by_name("y").outputs
out = sess.run(fetches=[y], feed_dict={x: 1.0})
print(out[0])
优化尝试
但是,虽然我对优化没有太多期待,但当我尝试优化图形进行推理时,我收到以下错误消息 . 预期的输出节点似乎不在保存的图形中 .
$ python -m tensorflow.python.tools.optimize_for_inference --input test_model.data-00000-of-00001 --output opt_model --input_names=x --output_names=y
Traceback (most recent call last):
File "/usr/lib/python2.7/runpy.py", line 174, in _run_module_as_main
"__main__", fname, loader, pkg_name)
File "/usr/lib/python2.7/runpy.py", line 72, in _run_code
exec code in run_globals
File "/{path}/lib/python2.7/site-packages/tensorflow/python/tools/optimize_for_inference.py", line 141, in <module>
app.run(main=main, argv=[sys.argv[0]] + unparsed)
File "/{path}/local/lib/python2.7/site-packages/tensorflow/python/platform/app.py", line 44, in run
_sys.exit(main(_sys.argv[:1] + flags_passthrough))
File "/{path}/lib/python2.7/site-packages/tensorflow/python/tools/optimize_for_inference.py", line 90, in main
FLAGS.output_names.split(","), FLAGS.placeholder_type_enum)
File "/{path}/local/lib/python2.7/site-packages/tensorflow/python/tools/optimize_for_inference_lib.py", line 91, in optimize_for_inference
placeholder_type_enum)
File "/{path}/local/lib/python2.7/site-packages/tensorflow/python/tools/strip_unused_lib.py", line 71, in strip_unused
output_node_names)
File "/{path}/local/lib/python2.7/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
进一步的调查使我检查了保存图表的检查点,该图表仅显示1个张量( a
,没有 x
且没有 y
) .
(tf-1.0.1) $ python -m tensorflow.python.tools.inspect_checkpoint --file_name ./test_model --all_tensors
tensor_name: a
5.0
具体问题
-
为什么我在检查点看不到
x
和y
?是因为它们是操作而不是张量? -
由于我需要为
optimize_for_inference
模块提供输入和输出名称,如何构建图形以便我可以引用输入和输出节点?
2 回答
Here is the detailed guide on how to optimize for inference:
optimize_for_inference
模块将frozen binary GraphDef
文件作为输入并输出optimized Graph Def
文件,您可以将其用于推理 . 要获得frozen binary GraphDef file
,您需要使用模块freeze_graph
,它将GraphDef proto
,SaverDef proto
和一组存储在检查点文件中的变量 . 实现这一目标的步骤如下:1.保存张量流图
2.冻结图
3.优化推理
4.使用优化图
5.对于多个输出名称
如果有多个输出节点,则指定:
output_node_names = 'boxes, scores, classes'
并导入图形,input
是script的graphdef文件,而不是检查点的数据部分 . 您需要将模型冻结到.pb
文件/或获取图形的原型文本并使用optimize for推理脚本 .This script takes either a frozen binary GraphDef file (where the weight variables have been converted into constants by the freeze_graph script), or a text GraphDef proto file (the weight variables are stored in a separate checkpoint file), and outputs a new GraphDef with the optimizations applied.
使用write_graph获取图形原型文件
获取冻结模型freeze graph