我试图将多层感知器示例导出为.pb图。 为了做到这一点,我有一个名为输入变量和输出操作,并添加以下行:对未初始化的变量导入张量流图失败
tf.train.write_graph(sess.graph_def, "./", "graph.pb", False)
导入,我做了以下内容:
with gfile.FastGFile("graph.pb",'rb') as f:
print("load graph")
graph_def = tf.GraphDef()
graph_def.ParseFromString(f.read())
graph_def = tf.GraphDef()
graph_def.ParseFromString(f.read())
_ = tf.import_graph_def(graph_def, name='')
with tf.Session() as persisted_sess:
persisted_result = persisted_sess.graph.get_tensor_by_name("output:0")
avd = persisted_sess.run(persisted_result, feed_dict={"input_x:0": features_t})
print ("Result:", str(avd))
它做进口罚款,但抛出“运行”行错误。
Traceback (most recent call last):
File "/usr/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 972, in _do_call
return fn(*args)
File "/usr/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 954, in _run_fn
status, run_metadata)
File "/usr/lib/python3.5/contextlib.py", line 66, in __exit__
next(self.gen)
File "/usr/lib/python3.5/site-packages/tensorflow/python/framework/errors.py", line 463, in raise_exception_on_not_ok_status
pywrap_tensorflow.TF_GetCode(status))
tensorflow.python.framework.errors.FailedPreconditionError: Attempting to use uninitialized value Variable_3
[[Node: Variable_3/read = Identity[T=DT_FLOAT, _class=["loc:@Variable_3"], _device="/job:localhost/replica:0/task:0/cpu:0"](Variable_3)]]
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "teste.py", line 56, in <module>
avd = persisted_sess.run(persisted_result, feed_dict={"input_x:0": features_t})
File "/usr/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 717, in run
run_metadata_ptr)
File "/usr/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 915, in _run
feed_dict_string, options, run_metadata)
File "/usr/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 965, in _do_run
target_list, options, run_metadata)
File "/usr/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 985, in _do_call
raise type(e)(node_def, op, message)
tensorflow.python.framework.errors.FailedPreconditionError: Attempting to use uninitialized value Variable_3
[[Node: Variable_3/read = Identity[T=DT_FLOAT, _class=["loc:@Variable_3"], _device="/job:localhost/replica:0/task:0/cpu:0"](Variable_3)]]
Caused by op 'Variable_3/read', defined at:
File "teste.py", line 37, in <module>
_ = tf.import_graph_def(graph_def, name='')
File "/usr/lib/python3.5/site-packages/tensorflow/python/framework/importer.py", line 285, in import_graph_def
op_def=op_def)
File "/usr/lib/python3.5/site-packages/tensorflow/python/framework/ops.py", line 2380, in create_op
original_op=self._default_original_op, op_def=op_def)
File "/usr/lib/python3.5/site-packages/tensorflow/python/framework/ops.py", line 1298, in __init__
self._traceback = _extract_stack()
FailedPreconditionError (see above for traceback): Attempting to use uninitialized value Variable_3
[[Node: Variable_3/read = Identity[T=DT_FLOAT, _class=["loc:@Variable_3"], _device="/job:localhost/replica:0/task:0/cpu:0"](Variable_3)]]
我试图初始化所有变量,但它不起作用。
我按照以下代码在Android上使用它:https://github.com/tensorflow/tensorflow/tree/master/tensorflow/examples/android 似乎他们得到了带有权重的.pb文件 – BernardoGO
老实说,我不知道具体的例子,但我认为这很可能是因为它遵循C++部署流程:在哪里运行特定的官方脚本来将检查点文件合并到graphdef文件中(通过将变量赋值转换为常量图形定义)。这可以从C++加载 –
“this”我的意思是生成的graphdef。 –