来自tf.train.import_meta_graph的UnicodeDecodeError

时间:2018-05-24 03:24:48

标签: tensorflow deserialization

我使用以下代码序列化了Tensorflow模型......

save_path = self.saver.save(self.session, os.path.join(self.logdir, "model.ckpt"), global_step)
logging.info("Model saved in file: %s" % save_path)

...我现在正尝试使用以下代码在单独的文件中从头开始恢复它:

saver = tf.train.import_meta_graph(PROJ_DIR + '/logs/default/model.ckpt-54.meta')
session = tf.Session()
saver.restore(session, PROJ_DIR + '/logs/default/model.ckpt-54')
print('Model restored')

调用tf.train.import_meta_graph时,抛出以下异常:

[libprotobuf ERROR google/protobuf/io/coded_stream.cc:207] A protocol message was rejected because it was too big (more than 67108864 bytes).  To increase the limit (or to disable these warnings), see CodedInputStream::SetTotalBytesLimit() in google/protobuf/io/coded_stream.h.
Traceback (most recent call last):
  File "/home/reid/projects/research/ccg/taggerflow_modified/test/tf_restore.py", line 4, in <module>
    saver = tf.train.import_meta_graph(PROJ_DIR + '/logs/default/model.ckpt-54.meta')
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/training/saver.py", line 1711, in import_meta_graph
    read_meta_graph_file(meta_graph_or_file), clear_devices)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/training/saver.py", line 1563, in read_meta_graph_file
    text_format.Merge(file_content.decode("utf-8"), meta_graph_def)
  File "/usr/lib/python2.7/encodings/utf_8.py", line 16, in decode
    return codecs.utf_8_decode(input, errors, True)
UnicodeDecodeError: 'utf8' codec can't decode byte 0xa7 in position 1: invalid start byte

作为参考,这是<PROJ_DIR>/logs/default/model.ckpt-54.meta的前几行:

<A7>:^R<A4>:
9
^CAdd^R^F
^Ax"^AT^R^F
^Ay"^AT^Z^F
^Az"^AT"^Z
^AT^R^Dtype:^O
^M2^K^S^A^B^D^F^E^C    ^R^G

我认为Tensorflow在序列化时和反序列化时使用不同的编码。我们如何指定Tensorflow在序列化/反序列化时使用的编码?或解决方案有所不同吗?

1 个答案:

答案 0 :(得分:-1)

我遇到了同样的问题。你有没有确保

.meta, .data-00000-of-00001 and the .index files

名称为'checkpoint'的文件是否也在您加载模型的目录中?

我确定了这个问题后,我的问题得到了解决。希望这有帮助!