Reading a Google Protocol buffer .pb file using protoc

Viewed 9314

I have compiled Google Protobuf from the source and generated the protoc binary. Now, given a .pb file, i.e., tensorflow_inception_v3_stripped_optimized_quantized.pb how am I gonna be able to read its content without using the Tensorflow library ?

Currently, I can write a sample reader to dump the event of my .pb file later be read by tensorboardas following:

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

INCEPTION_LOG_DIR = '/tmp/inception_v3_log'

if not os.path.exists(INCEPTION_LOG_DIR):
    os.makedirs(INCEPTION_LOG_DIR)
with tf.Session() as sess:
    model_filename = './model/tensorflow_inception_v3_stripped_optimized_quantized.pb'
    with gfile.FastGFile(model_filename, 'rb') as f:
        graph_def = tf.GraphDef()
        graph_def.ParseFromString(f.read())
        _ = tf.import_graph_def(graph_def, name='')
    #writer = tf.train.SummaryWriter(INCEPTION_LOG_DIR, graph_def)
    writer=tf.summary.FileWriter(INCEPTION_LOG_DIR, graph_def)                                                
    writer.close()

However, I don't quite get the reason why I compiled protoc for? Can't it be used as a standalone reader? Or, the mentioned inception.pb file is already using Protocol buffer in the backend of the Tensorflow without the need of using protoc?

commands like these do generate error:

protoc --python_out=. tensorflow_inception_v3_stripped_optimized_quantized.pb
protoc --cpp_out=. tensorflow_inception_v3_stripped_optimized_quantized.pb

As I checked, .pb files are semi-readable, however, I could not find a solid answer to my question anywhere to directly parse this file's content. Am I missing something here? Thanks.

1 Answers

Yes, protoc can also be used to decode .pb files.

protoc --decode_raw < my_input.pb

will output the raw structure of the file. This is not very useful, because (contrary to, e.g., XML or JSON) protobuf files do not contain as much structural information (element names), but that is "outsourced" into the .proto files.

If you have the correct .proto files, in this case from the tensorflow repository, you can use -I path_to_tensorflow_checkout and specify the correct message type name. Note that in the tensorflow .proto files, all types are in the tensorflow package, so you have to prefix the type name. Working example:

protoc --decode tensorflow.SavedModel tensorflow/core/protobuf/saved_model.proto < path_to_saved_model.pb

(In this case, I ran the command from the tensorflow repository directory, omitting the -I / --proto_path.)

Depending on your model file format (SavedModel or GraphDef), you might need to use tensorflow.GraphDef (e.g., for "freezed graphs") instead of tensorflow.SavedModel.

Related