【问题标题】:How to convert protobuf graph to binary wire format?如何将 protobuf 图转换为二进制线格式?
【发布时间】:2016-07-24 05:51:35
【问题描述】:

我有一种将二进制有线格式转换为人类可读格式的方法,但我不能做相反的操作

import tensorflow as tf
from tensorflow.python.platform import gfile

def converter(filename): 
  with gfile.FastGFile(filename,'rb') as f:
    graph_def = tf.GraphDef()
    graph_def.ParseFromString(f.read())
    tf.import_graph_def(graph_def, name='')
    tf.train.write_graph(graph_def, 'pbtxt/', 'protobuf.pb', as_text=True)
  return

我只需为此输入文件名即可。但是在做相反的事情时,我得到了

  File "pb_to_pbtxt.py", line 16, in <module>
    converter('protobuf.pb')  # here you can write the name of the file to be converted
  File "pb_to_pbtxt.py", line 11, in converter
    graph_def.ParseFromString(f.read())
  File "/usr/local/lib/python2.7/dist-packages/google/protobuf/message.py", line 185, in ParseFromString
    self.MergeFromString(serialized)
  File "/usr/local/lib/python2.7/dist-packages/google/protobuf/internal/python_message.py", line 1008, in MergeFromString
    if self._InternalParse(serialized, 0, length) != length:
  File "/usr/local/lib/python2.7/dist-packages/google/protobuf/internal/python_message.py", line 1034, in InternalParse
    new_pos = local_SkipField(buffer, new_pos, end, tag_bytes)
  File "/usr/local/lib/python2.7/dist-packages/google/protobuf/internal/decoder.py", line 868, in SkipField
    return WIRETYPE_TO_SKIPPER[wire_type](buffer, pos, end)
  File "/usr/local/lib/python2.7/dist-packages/google/protobuf/internal/decoder.py", line 838, in _RaiseInvalidWireType
    raise _DecodeError('Tag had invalid wire type.')

【问题讨论】:

  • pb到txt格式的转换是怎么做的?

标签: python python-2.7 tensorflow protocol-buffers


【解决方案1】:

您可以使用google.protobuf.text_format 模块执行反向翻译:

import tensorflow as tf
from google.protobuf import text_format

def convert_pbtxt_to_graphdef(filename):
  """Returns a `tf.GraphDef` proto representing the data in the given pbtxt file.

  Args:
    filename: The name of a file containing a GraphDef pbtxt (text-formatted
      `tf.GraphDef` protocol buffer data).

  Returns:
    A `tf.GraphDef` protocol buffer.
  """
  with tf.gfile.FastGFile(filename, 'r') as f:
    graph_def = tf.GraphDef()

    file_content = f.read()

    # Merges the human-readable string in `file_content` into `graph_def`.
    text_format.Merge(file_content, graph_def)
  return graph_def

【讨论】:

    【解决方案2】:

    你可以像这样使用tf.Graph.as_graph_def(),然后是Protobuf的SerializeToString()

    proto_graph = # obtained by calling tf.Graph.as_graph_def()
    
    with open("my_graph.bin", "wb") as f:
        f.write(proto_graph.SerializeToString())
    

    如果你只是想写文件而不关心编码你也可以使用tf.train.write_graph()

    v = tf.Variable(0, name='my_variable')
    sess = tf.Session()
    tf.train.write_graph(sess.graph_def, '/tmp/my-model', 'train.pbtxt')
    

    注意:在 TF 0.10 上测试,不确定早期版本。

    【讨论】:

      猜你喜欢
      • 2013-04-30
      • 1970-01-01
      • 1970-01-01
      • 2011-08-12
      • 2015-11-13
      • 2021-01-20
      • 1970-01-01
      • 2013-09-09
      • 1970-01-01
      相关资源
      最近更新 更多