gpt4 book ai didi

python - 从字符串解析 graph_def 时出错

转载 作者:太空狗 更新时间:2023-10-29 21:25:29 26 4
gpt4 key购买 nike

我正在尝试将 Tensorflow 图运行一个非常简单的保存为 .pb 文件,但在解析它时出现此错误:

Traceback (most recent call last):
File "test_import_stripped_bm.py", line 28, in <module>
graph_def.ParseFromString(fileContent)
File "/usr/local/lib/python3.5/dist-packages/google/protobuf/message.py", line 185, in ParseFromString
self.MergeFromString(serialized)
File "/usr/local/lib/python3.5/dist-packages/google/protobuf/internal/python_message.py", line 1069, in MergeFromString
if self._InternalParse(serialized, 0, length) != length:
File "/usr/local/lib/python3.5/dist-packages/google/protobuf/internal/python_message.py", line 1105, in InternalParse
pos = field_decoder(buffer, new_pos, end, self, field_dict)
File "/usr/local/lib/python3.5/dist-packages/google/protobuf/internal/decoder.py", line 633, in DecodeField
if value._InternalParse(buffer, pos, new_pos) != new_pos:
File "/usr/local/lib/python3.5/dist-packages/google/protobuf/internal/python_message.py", line 1105, in InternalParse
pos = field_decoder(buffer, new_pos, end, self, field_dict)
File "/usr/local/lib/python3.5/dist-packages/google/protobuf/internal/decoder.py", line 612, in DecodeRepeatedField
if value.add()._InternalParse(buffer, pos, new_pos) != new_pos:
File "/usr/local/lib/python3.5/dist-packages/google/protobuf/internal/python_message.py", line 1105, in InternalParse
pos = field_decoder(buffer, new_pos, end, self, field_dict)
File "/usr/local/lib/python3.5/dist-packages/google/protobuf/internal/decoder.py", line 743, in DecodeMap
if submsg._InternalParse(buffer, pos, new_pos) != new_pos:
File "/usr/local/lib/python3.5/dist-packages/google/protobuf/internal/python_message.py", line 1095, in InternalParse
new_pos = local_SkipField(buffer, new_pos, end, tag_bytes)
File "/usr/local/lib/python3.5/dist-packages/google/protobuf/internal/decoder.py", line 850, in SkipField
return WIRETYPE_TO_SKIPPER[wire_type](buffer, pos, end)
File "/usr/local/lib/python3.5/dist-packages/google/protobuf/internal/decoder.py", line 799, in _SkipGroup
new_pos = SkipField(buffer, pos, end, tag_bytes)
File "/usr/local/lib/python3.5/dist-packages/google/protobuf/internal/decoder.py", line 850, in SkipField
return WIRETYPE_TO_SKIPPER[wire_type](buffer, pos, end)
File "/usr/local/lib/python3.5/dist-packages/google/protobuf/internal/decoder.py", line 814, in _SkipFixed32
raise _DecodeError('Truncated message.')
google.protobuf.message.DecodeError: Truncated message.

这是我用来将其写入 .pb 的代码:

import tensorflow as tf

builder = tf.saved_model.builder.SavedModelBuilder('models/TEST-3')

w1 = tf.Variable(tf.random_normal((2,2)), name="w1")
w2 = tf.Variable(tf.random_normal((2,2)), name="w2")

sess = tf.Session()
sess.run(tf.global_variables_initializer())

builder.add_meta_graph_and_variables(sess, tags=[tf.saved_model.tag_constants.SERVING], clear_devices = True)

builder.save()
sess.close()

这是解析它的代码:

import tensorflow as tf
import os

model_path = os.path.join('models/TEST-3', 'saved_model.pb')
with open(model_path, mode='rb') as f:
fileContent = f.read()
graph_def = tf.GraphDef()
graph_def.ParseFromString(fileContent)

要查看我必须执行的确切错误

export PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION=python

在运行它之前。我也在 python 2 和 3 上用不同的 tensorflow 版本试过这个,我在 Ubuntu 16.04 上运行。在带有 tensorflow 0.9.0rc0 的 python 2.7 上,我设法得到了一个稍微不同的错误:

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python2.7/dist-packages/google/protobuf/message.py", line 185, in ParseFromString
self.MergeFromString(serialized)
File "/usr/local/lib/python2.7/dist-packages/google/protobuf/internal/python_message.py", line 1091, in MergeFromString
if self._InternalParse(serialized, 0, length) != length:
File "/usr/local/lib/python2.7/dist-packages/google/protobuf/internal/python_message.py", line 1127, in InternalParse
pos = field_decoder(buffer, new_pos, end, self, field_dict)
File "/usr/local/lib/python2.7/dist-packages/google/protobuf/internal/decoder.py", line 633, in DecodeField
if value._InternalParse(buffer, pos, new_pos) != new_pos:
File "/usr/local/lib/python2.7/dist-packages/google/protobuf/internal/python_message.py", line 1127, in InternalParse
pos = field_decoder(buffer, new_pos, end, self, field_dict)
File "/usr/local/lib/python2.7/dist-packages/google/protobuf/internal/decoder.py", line 612, in DecodeRepeatedField
if value.add()._InternalParse(buffer, pos, new_pos) != new_pos:
File "/usr/local/lib/python2.7/dist-packages/google/protobuf/internal/python_message.py", line 1127, in InternalParse
pos = field_decoder(buffer, new_pos, end, self, field_dict)
File "/usr/local/lib/python2.7/dist-packages/google/protobuf/internal/decoder.py", line 612, in DecodeRepeatedField
if value.add()._InternalParse(buffer, pos, new_pos) != new_pos:
File "/usr/local/lib/python2.7/dist-packages/google/protobuf/internal/python_message.py", line 1127, in InternalParse
pos = field_decoder(buffer, new_pos, end, self, field_dict)
File "/usr/local/lib/python2.7/dist-packages/google/protobuf/internal/decoder.py", line 489, in DecodeRepeatedField
value.append(_ConvertToUnicode(buffer[pos:new_pos]))
File "/usr/local/lib/python2.7/dist-packages/google/protobuf/internal/decoder.py", line 469, in _ConvertToUnicode
return local_unicode(byte_str, 'utf-8')
UnicodeDecodeError: 'utf8' codec can't decode byte 0x80 in position 18: 'utf8' codec can't decode byte 0x80 in position 18: invalid start byte in field: tensorflow.FunctionDef.Node.ret

我能够使用此代码解析其他 .pb 图,例如这个 https://github.com/taey16/tf/blob/master/imagenet/classify_image_graph_def.pb

提前致谢。

最佳答案

这里的问题是您正在尝试解析 SavedModel Protocol Buffer 就好像它是一个 GraphDef .尽管 SavedModel 包含 GraphDef,但它们具有不同的二进制格式。以下代码,使用tf.saved_model.loader.load()应该工作:

import tensorflow as tf

with tf.Session(graph=tf.Graph()) as sess:
tf.saved_model.loader.load(
sess, [tf.saved_model.tag_constants.SERVING], "models/TEST-3")

关于python - 从字符串解析 graph_def 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46547319/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com