使用 protoc 读取 Google Protocol buffer .pb 文件

Ami*_*mir 4 protocol-buffers tensorflow

我已经从源代码编译了 Google Protobuf 并生成了protoc二进制文件。现在,给定一个.pb文件,即,tensorflow_inception_v3_stripped_optimized_quantized.pb我如何在使用Tensorflow库的情况下读取其内容?

目前,我可以编写一个示例阅读器来转储我的.pb文件事件,然后tensorboard按如下方式读取:

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()
Run Code Online (Sandbox Code Playgroud)

但是,我不太明白我编译的原因protoc?它不能用作独立阅读器吗?或者,上面提到的 inception.pb 文件已经在 Tensorflow 的后端使用了 Protocol buffer 而不需要使用protoc?

像这样的命令确实会产生错误:

protoc --python_out=. tensorflow_inception_v3_stripped_optimized_quantized.pb
protoc --cpp_out=. tensorflow_inception_v3_stripped_optimized_quantized.pb
Run Code Online (Sandbox Code Playgroud)

当我检查时,.pb文件是半可读的,但是,我无法在任何地方找到我的问题的可靠答案来直接解析此文件的内容。我在这里错过了什么吗?谢谢。

han*_*ine 5

是的,protoc也可用于解码 .pb 文件。

protoc --decode_raw < my_input.pb
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

(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.