TF 保存/恢复图在 tf.GraphDef.ParseFromString() 失败 [英] TF save/restore graph fails at tf.GraphDef.ParseFromString()

查看:93
本文介绍了TF 保存/恢复图在 tf.GraphDef.ParseFromString() 失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基于此converting-trained-tensorflow-model-to-protobuf 我试图保存/恢复 TF 图但没有成功.

Based on this converting-trained-tensorflow-model-to-protobuf I am trying to save/restore TF graph without success.

这是保护程序:

with tf.Graph().as_default():
    variable_node = tf.Variable(1.0, name="variable_node")
    output_node = tf.mul(variable_node, 2.0, name="output_node")
    sess = tf.Session()
    init = tf.initialize_all_variables()
    sess.run(init)
    output = sess.run(output_node)
    tf.train.write_graph(sess.graph.as_graph_def(), summ_dir, 'model_00_g.pbtxt', as_text=True)
    #self.assertNear(2.0, output, 0.00001)
    saver = tf.train.Saver()
    saver.save(sess, saver_path)

产生带有文本图描述的model_00_g.pbtxt.几乎从 .pya_graph 复制粘贴>.

which produces model_00_g.pbtxt with text graph description. Pretty much copy paste from freeze_graph_test.py.

这里是读者:

with tf.Session() as sess:

    with tf.Graph().as_default():
        graph_def = tf.GraphDef()
        graph_path = '/mnt/code/test_00/log/2016-02-11.22-37-46/model_00_g.pbtxt'
        with open(graph_path, "rb") as f:
            proto_b = f.read()
            #print proto_b   # -> I can see it
            graph_def.ParseFromString(proto_b) # no luck..
            _ = tf.import_graph_def(graph_def, name="")

    print sess.graph_def

graph_def.ParseFromString() 处失败,带有 DecodeError: Tag has invalid wire type.

我在 docker 容器 b.gcr.io/tensorflow/tensorflow:latest-devel 以防万一.

I am on docker container b.gcr.io/tensorflow/tensorflow:latest-devel in case it makes any difference.

推荐答案

GraphDef.ParseFromString() 方法(一般来说,ParseFromString() 方法在任何 Python protobuf 包装器)都需要二进制协议缓冲区格式的字符串.如果您将 as_text=False 传递给 tf.train.write_graph(),则文件将采用适当的格式.

The GraphDef.ParseFromString() method (and, in general, the ParseFromString() method on any Python protobuf wrapper) expects a string in the binary protocol buffer format. If you pass as_text=False to tf.train.write_graph(), then the file will be in the appropriate format.

否则,您可以执行以下操作来阅读基于文本的格式:

Otherwise you can do the following to read the text-based format:

from google.protobuf import text_format
# ...
graph_def = tf.GraphDef()
text_format.Merge(proto_b, graph_def) 

这篇关于TF 保存/恢复图在 tf.GraphDef.ParseFromString() 失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆