如何修复:创建张量流文本摘要时出现“google.protobuf.message.DecodeError:解析消息时出错”

Nik*_*dra 5 python parsing tensorflow coreml firebase-mlkit

我正在尝试运行一个脚本以从 tensorflow .pb 模型中获取文本摘要,如下所示:

    OPS counts:
    Squeeze : 1
    Softmax : 1
    BiasAdd : 1
    Placeholder : 1
    AvgPool : 1
    Reshape : 2
    ConcatV2 : 9
    MaxPool : 13
    Sub : 57
    Rsqrt : 57
    Relu : 57
    Conv2D : 58
    Add : 114
    Mul : 114
    Identity : 231
    Const : 298
Run Code Online (Sandbox Code Playgroud)

我总体上尝试将 .pb 模型转换为 .coremlmodel 并关注这篇文章:

https://hackernoon.com/integrating-tensorflow-model-in-an-ios-app-cecf30b9068d

从 .pb 模型中获取文本摘要是朝着这个目标迈出的一步。我尝试运行来创建文本摘要的代码如下:

import tensorflow as tf
from tensorflow.core.framework import graph_pb2
import time
import operator
import sys

def inspect(model_pb, output_txt_file):
    graph_def = graph_pb2.GraphDef()
    with open(model_pb, "rb") as f:
    graph_def.ParseFromString(f.read())

    tf.import_graph_def(graph_def)

    sess = tf.Session()
    OPS = sess.graph.get_operations()

    ops_dict = {}

    sys.stdout = open(output_txt_file, 'w')
    for i, op in enumerate(OPS):
            print('---------------------------------------------------------------------------------------------------------------------------------------------')
        print("{}: op name = {}, op type = ( {} ), inputs = {}, outputs = {}".format(i, op.name, op.type, ", ".join([x.name for x in op.inputs]), ", ".join([x.name for x in op.outputs])))
        print('@input shapes:')
        for x in op.inputs:
            print("name = {} : {}".format(x.name, x.get_shape()))
        print('@output shapes:')
        for x in op.outputs:
            print("name = {} : {}".format(x.name, x.get_shape()))
        if op.type in ops_dict:
            ops_dict[op.type] += 1
        else:
            ops_dict[op.type] = 1

    print('---------------------------------------------------------------------------------------------------------------------------------------------')
sorted_ops_count = sorted(ops_dict.items(),     key=operator.itemgetter(1))
    print('OPS counts:')
    for i in sorted_ops_count:
        print("{} : {}".format(i[0], i[1]))

if __name__ == "__main__":
"""
Write a summary of the frozen TF graph to a text file.
Summary includes op name, type, input and output names and shapes. 

Arguments
----------
- path to the frozen .pb graph
- path to the output .txt file where the summary is written

Usage
----------
python inspect_pb.py frozen.pb text_file.txt

"""
if len(sys.argv) != 3:
    raise ValueError("Script expects two arguments. " +
          "Usage: python inspect_pb.py /path/to/the/frozen.pb /path/to/the/output/text/file.txt")
inspect(sys.argv[1], sys.argv[2])
Run Code Online (Sandbox Code Playgroud)

我运行了这个命令:

 python inspect_pb.py /Users/nikhil.c/Desktop/tensorflowModel.pb   text_summary.txt
Run Code Online (Sandbox Code Playgroud)

但我没有收到预期的输出,而是收到以下错误消息:

    Traceback (most recent call last):
      File "inspect_pb.py", line 58, in <module>
        inspect(sys.argv[1], sys.argv[2])
      File "inspect_pb.py", line 10, in inspect
        graph_def.ParseFromString(f.read())
    google.protobuf.message.DecodeError: Error parsing message
Run Code Online (Sandbox Code Playgroud)

并且真的不知道从哪里开始。其他似乎收到相同错误消息的类似问题没有太大意义。我该怎么办?

ETd*_*ode 5

根据这个tensorflow/moonlight问题,有3个关键组件:

  • 张量板
  • Protocol Buffer 又名 Protobuf(替代 XML 和 JSON 文件传输,以提高性能并保护数据损坏 - 通过某种哈希,从而避免解析解码错误)
  • 巴泽尔

我可以将问题从 GCP(云)v.1.14 平台重现到本地 TensorFlow(JitTeam Docker)v.1.13 - 如果我重新训练模型,它可以工作,如果我导入,所有脚本都会因此错误而崩溃。

假设您仍然可以访问原始文件和系统,您还有其他导出选项

您可以尝试使用本指南安装其他组件版本 - 作者建议使用他的脚本而不是 pip3 install。(您可以阅读评论以审查脚本)。它基于官方 Tensorflow 构建。

你也可以尝试

pip3 install protobuf==3.6.0
Run Code Online (Sandbox Code Playgroud)

这与张量流问题 #21719 有关