解决方法:创建张量流文本摘要时"google.protobuf.message.DecodeError: Error parsing message"



我正在尝试运行一个脚本来从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

我总体上正在尝试将 .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])

我运行了这个命令:

python inspect_pb.py /Users/nikhil.c/Desktop/tensorflowModel.pb   text_summary.txt

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

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

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

根据这个张量流/月光问题,有 3 个关键组件:

  • 张量板
  • 协议缓冲区又名Protobuf(XML和JSON文件传输的替代品,以获得更好的性能并保护数据损坏 - 通过某种哈希,因此解析解码错误)
  • 巴泽尔

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

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

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

您也可以尝试

pip3 install protobuf==3.6.0

这与张量流问题 #21719 有关

你可以试试:

with gfile.GFile(frozen_graph, 'rb') as f:
graph_def = tf.compat.v1.GraphDef()
graph_def.ParseFromString(f.read(-1)) 
# the -1 returns the contents of the file as a string

我在文档中看到了它: https://www.tensorflow.org/api_docs/python/tf/io/gfile/GFile#read

相关内容

最新更新