从字符串解析graph_def时出错 [英] Error when parsing graph_def from string

查看:67
本文介绍了从字符串解析graph_def时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to run a very simple saving of a Tensorflow graph as .pb file, but I have this error when parsing it back:

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 的代码:

This is the code that I use to write it to .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)

要查看我必须做的确切错误

To see the exact error I had to do

export PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION=python

在运行之前.此外,我已经在具有不同 tensorflow 版本的 python 2 和 3 上尝试过这个,我在 Ubuntu 16.04 上运行.在使用 tensorflow 0.9.0rc0 的 python 2.7 上,我设法得到一个稍微不同的错误:

before running it. Also I've tried this on python 2 and 3 with different tensorflow versions, I am running on Ubuntu 16.04. On python 2.7 with tensorflow 0.9.0rc0 I managed to get a slightly different error:

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

I am able to parse other .pb graph with this code for example this one https://github.com/taey16/tf/blob/master/imagenet/classify_image_graph_def.pb

提前致谢.

推荐答案

这里的问题是你试图解析一个 SavedModel 协议缓冲区,就好像它是一个 GraphDef.尽管 SavedModel 包含 GraphDef,但它们具有不同的二进制格式.以下代码,使用 tf.saved_model.loader.load() 应该可以工作:

The problem here is that you are trying to parse a SavedModel protocol buffer as if it were a GraphDef. Although a SavedModel contains GraphDef, they have different binary formats. The following code, using tf.saved_model.loader.load() should work:

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") 

这篇关于从字符串解析graph_def时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆