Created
August 9, 2016 16:09
-
-
Save arafatkatze/c063bddb9b8d17a037695d748db4f592 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This file is useful for reading the contents of the ops generated by ruby. | |
# You can read any graph defination in pb/pbtxt format generated by ruby | |
# or by python and then convert it back and forth from human readable to binary format. | |
import tensorflow as tf | |
from google.protobuf import text_format | |
from tensorflow.python.platform import gfile | |
def pbtxt_to_graphdef(filename): | |
with open(filename, 'r') as f: | |
graph_def = tf.GraphDef() | |
file_content = f.read() | |
text_format.Merge(file_content, graph_def) | |
tf.import_graph_def(graph_def, name='') | |
tf.train.write_graph(graph_def, 'pbtxt/', 'protobuf.pb', as_text=False) | |
def graphdef_to_pbtxt(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.pbtxt', as_text=True) | |
return | |
graphdef_to_pbtxt('graph.pb') # here you can write the name of the file to be converted | |
# and then a new file will be made in pbtxt directory. |
This function obtained from here will do the trick:
import tensorflow as tf
import sys
from tensorflow.python.platform import gfile
from tensorflow.core.protobuf import saved_model_pb2
from tensorflow.python.util import compat
model_filename ='saved_model.pb'
with gfile.FastGFile(model_filename, 'rb') as f:
data = compat.as_bytes(f.read())
sm = saved_model_pb2.SavedModel()
sm.ParseFromString(data)
g_in = tf.import_graph_def(sm.meta_graphs[0].graph_def)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I tried to implement the same but I am getting the following error
in graphdef_to_pbtxt(filename)
7 with open(filename,'rb') as f:
8 graph_def = tf.compat.v1.GraphDef()
----> 9 graph_def.ParseFromString(f.read())
10 with open('protobuf.txt', 'w') as fp:
11 fp.write(str(graph_def))
DecodeError: Error parsing message
The code I used is as follows
Can anybody help me on this?