2016-11-02 109 views
0

我试图将多层感知器示例导出为.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)]] 

我试图初始化所有变量,但它不起作用。

回答

1

TensorFlow拆分将图形定义和变量值保存在不同的文件(分别为图形和检查点)中。

您想使用TF Saver。

看到这个答案的详细信息: https://stackoverflow.com/a/33762168/4120005

还是这里的文档: https://www.tensorflow.org/versions/r0.11/how_tos/variables/index.html#saving-variables

如果你真的需要从graphdef文件刚恢复(* .pb),从C加载它++为例如,你需要从这里使用freeze_graph.py脚本: https://github.com/tensorflow/tensorflow/blob/master/tensorflow/python/tools/freeze_graph.py

这个脚本需要graphdef(.pb)和检查点( .ckp t)文件作为输入并输出包含常量形式权重的graphdef文件(您可以阅读脚本中的文档以获取更多详细信息)。

+0

我按照以下代码在Android上使用它:https://github.com/tensorflow/tensorflow/tree/master/tensorflow/examples/android 似乎他们得到了带有权重的.pb文件 – BernardoGO

+0

老实说,我不知道具体的例子,但我认为这很可能是因为它遵循C++部署流程:在哪里运行特定的官方脚本来将检查点文件合并到graphdef文件中(通过将变量赋值转换为常量图形定义)。这可以从C++加载 –

+0

“this”我的意思是生成的graphdef。 –