Python 如何优化一个简单、保存的TensorFlow 1.0.1图的推理?
我无法在一个简单保存的TensorFlow图(Python 2.7;由Python 如何优化一个简单、保存的TensorFlow 1.0.1图的推理?,python,python-2.7,tensorflow,tensorflow-gpu,Python,Python 2.7,Tensorflow,Tensorflow Gpu,我无法在一个简单保存的TensorFlow图(Python 2.7;由pip安装TensorFlow gpu==1.0.1安装的包)上成功运行optimize\u for\u推断模块 背景 保存张量流图 下面是我的Python脚本,用于生成并保存一个简单的图形,以便将5添加到我的输入x占位符操作中 import tensorflow as tf # make and save a simple graph G = tf.Graph() with G.as_default(): x =
pip安装TensorFlow gpu==1.0.1安装的包)上成功运行optimize\u for\u推断
模块
背景
保存张量流图
下面是我的Python脚本,用于生成并保存一个简单的图形,以便将5添加到我的输入x
占位符
操作中
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")
恢复张量流图
我有一个简单的还原脚本,可以重新创建保存的图形并还原图形参数。两个保存/还原脚本都产生相同的输出
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
具体问题
为什么我在检查点中看不到x
和y
?是因为它们是运算而不是张量吗
由于我需要为optimize\u for\u inference
模块提供输入和输出名称,因此如何构建图形以便引用输入和输出节点
您做错了:input
是用于的graphdef文件,而不是检查点的数据部分。您需要将模型冻结到.pb
文件/或获取用于图形的prototxt并使用用于推理的优化脚本李>
此脚本获取冻结的二进制GraphDef文件(其中
变量已通过冻结(图形脚本)或
文本图形DEF原型文件(权重变量存储在单独的
检查点文件),并输出应用了优化的新GraphDef。
使用获取图形原型文件
得到冻结的模型
以下是关于如何优化推理的详细指南:
optimized_for_推断
模块将一个冻结的二进制图形Def
文件作为输入,并输出可用于推断的optimized Graph Def
文件。要获得冻结的二进制GraphDef文件
,您需要使用模块冻结图
,该模块采用GraphDef协议
、一个SaverDef协议
和一组存储在检查点文件中的变量。实现这一目标的步骤如下所示:
1.保存张量流图
2.冻结图
3.优化推理
4.使用优化图
5.用于多个输出名称
如果有多个输出节点,则指定:output\u node\u names='box,scores,classes'
并按导入图形
boxes,scores,classes, = tf.import_graph_def(graph_def_optimized, return_elements=['boxes:0', 'scores:0', 'classes:0'])
这是一个非常有帮助、建设性和详细的回答。非常感谢。回答得很好!您的评论非常宝贵,因为我不知道您必须单独保存图表和检查点。顺便说一句,我确实必须将--input\u checkpoint test\u model
更改为--input\u checkpoint./test\u model
,才能使冻结图
正常工作。@vijaym如何指定是否有多个输出--output\u names='cat',“shape”这不起作用。为什么需要调用全局变量\u初始值设定项。为冻结的图形运行
虽然?@user1050619指定多个输出:--output\u names='cat,shape'
。注意昏迷后没有空间
# 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})
# Save GraphDef
tf.train.write_graph(sess.graph_def,'.','graph.pb')
# Save checkpoint
saver.save(sess=sess, save_path="test_model")
python -m tensorflow.python.tools.freeze_graph --input_graph graph.pb --input_checkpoint test_model --output_graph graph_frozen.pb --output_node_names=y
python -m tensorflow.python.tools.optimize_for_inference --input graph_frozen.pb --output graph_optimized.pb --input_names=x --output_names=y
with tf.gfile.GFile('graph_optimized.pb', 'rb') as f:
graph_def_optimized = tf.GraphDef()
graph_def_optimized.ParseFromString(f.read())
G = tf.Graph()
with tf.Session(graph=G) as sess:
y, = tf.import_graph_def(graph_def_optimized, return_elements=['y:0'])
print('Operations in Optimized Graph:')
print([op.name for op in G.get_operations()])
x = G.get_tensor_by_name('import/x:0')
out = sess.run(y, feed_dict={x: 1.0})
print(out)
#Output
#Operations in Optimized Graph:
#['import/x', 'import/a', 'import/y']
#6.0
boxes,scores,classes, = tf.import_graph_def(graph_def_optimized, return_elements=['boxes:0', 'scores:0', 'classes:0'])