Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert protobuf graph to binary wire format?

I have a method to convert binary wire format to human readable format but I cannot do the inverse of this

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

I just have to type the file name for this and it works. But on doing the opposite i get

  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.')
like image 534
ArafatK Avatar asked Jul 24 '16 05:07

ArafatK


2 Answers

You can perform the reverse translation using the google.protobuf.text_format module:

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
like image 178
mrry Avatar answered Sep 23 '22 19:09

mrry


You can use tf.Graph.as_graph_def() and then Protobuf's SerializeToString() like so:

proto_graph = # obtained by calling tf.Graph.as_graph_def()

with open("my_graph.bin", "wb") as f:
    f.write(proto_graph.SerializeToString())

If you just want to write the file and do not care about the encoding you can also use 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')

Note: Tested on TF 0.10, not sure about earlier versions.

like image 38
mirosval Avatar answered Sep 22 '22 19:09

mirosval