【问题标题】:TF save/restore graph fails at tf.GraphDef.ParseFromString()TF 保存/恢复图在 tf.GraphDef.ParseFromString() 失败
【发布时间】:2016-05-22 23:06:24
【问题描述】:

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

这是保护程序:

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。几乎从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 had invalid wire type. 失败

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

【问题讨论】:

    标签: tensorflow


    【解决方案1】:

    我尝试通过仅接受二进制文件的 java API 加载模型。但是在我们使用 contrib.Estimator 的 python 中会生成文本模型文件格式。 我在网上找到了model file converter,看来它工作正常。 如果您有现有的文本格式模型文件,这也可能解决原始问题(使用二进制模型加载器)。

    【讨论】:

      【解决方案2】:

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

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

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

      【讨论】:

      • 谢谢 - 这两种方式都可以解决眼前的问题。但是主要任务不适合我-我想存储图形+变量然后加载,然后在其他地方进行评估。这个tensorflow/tensorflow/python/tools/freeze_graph.py 看起来非常符合我的需要,但它还没有在我的 docker 映像中,并且一半参数的用法不是很清楚 - 我想我会为此等待 0.7 并暂时切换到其他任务。
      【解决方案3】:

      ParseFromString 需要二进制序列化协议缓冲区,对于人类可读的表示,您需要使用 text_format.Mergehere 一样使用

      【讨论】:

        猜你喜欢
        • 2016-08-29
        • 2013-05-13
        • 2015-06-05
        • 2014-05-22
        • 1970-01-01
        • 2016-05-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多