' UTF-8'在tensorflow教程中解码错误

时间:2016-02-19 19:25:01

标签: python tensorflow python-unicode

我遇到了这个奇怪的问题,当我跑步时

  from tensorflow.examples.tutorials.mnist import input_data

  mnist = input_data.read_data_sets('/home/fqiao/development/MNIST_data/', one_hot=True)

我明白了:

  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.5/dist-packages/tensorflow/examples/tutorials/mnist/input_data.py", line 199, in read_data_sets
    train_images = extract_images(local_file)
  File "/usr/local/lib/python3.5/dist-packages/tensorflow/examples/tutorials/mnist/input_data.py", line 58, in extract_images
    magic = _read32(bytestream)
  File "/usr/local/lib/python3.5/dist-packages/tensorflow/examples/tutorials/mnist/input_data.py", line 51, in _read32
    return numpy.frombuffer(bytestream.read(4), dtype=dt)[0]
  File "/usr/lib/python3.5/gzip.py", line 274, in read
    return self._buffer.read(size)
  File "/usr/lib/python3.5/_compression.py", line 68, in readinto
    data = self.read(len(byte_view))
  File "/usr/lib/python3.5/gzip.py", line 461, in read
    if not self._read_gzip_header():
  File "/usr/lib/python3.5/gzip.py", line 404, in _read_gzip_header
    magic = self._fp.read(2)
  File "/usr/lib/python3.5/gzip.py", line 91, in read
    self.file.read(size-self._length+read)
  File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/platform/default/_gfile.py", line 45, in sync
    return fn(self, *args, **kwargs)
  File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/platform/default/_gfile.py", line 199, in read
    return self._fp.read(n)
  File "/usr/lib/python3.5/codecs.py", line 321, in decode
    (result, consumed) = self._buffer_decode(data, self.errors, final)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0x8b in position 1: invalid start byte

但是,如果我只是直接在input_data.py中运行代码,那么一切似乎都没问题:

>>> dt = numpy.dtype(numpy.uint32).newbyteorder('>')
>>> f = tf.gfile.Open('/home/fqiao/development/MNIST_data/train-images-idx3-ubyte.gz', 'rb')
>>> bytestream = gzip.GzipFile(fileobj=f)
>>> testbytes = numpy.frombuffer(bytestream.read(4), dtype=dt)[0]
>>> testbytes
2051

任何人都知道发生了什么?

我的系统:Ubuntu 15.10 x64 python 3.5.0。

2 个答案:

答案 0 :(得分:4)

最近的更改555e73d解决了该错误。 MNIST文件需要以二进制'rb'模式打开,而不仅仅是文本'r'。

答案 1 :(得分:1)

就我而言,问题在于数据文件的编码。

使用vim打开文件并执行:

:set fileencoding=utf-8

这解决了我的问题。