1

我正在尝试从斯坦福网站加载 Pascal VOC 数据集还尝试在 Pascal VOC Pystruct 博客上实现语义图像分割的代码。但是当我尝试加载泡菜文件时,我得到了 UnicodeDecodeError。到目前为止,我尝试了以下代码:

import numpy as np
try:
    import cPickle as pickle
except ImportError:
    import pickle

from pystruct import learners
import pystruct.models as crfs
from pystruct.utils import SaveLogger

data_train = pickle.load(open("trainingData/data_train.pickle"))
C = 0.01

我得到了这个错误:

Traceback (most recent call last):
  File "/Users/mypath/PycharmProjects/semantic_segmentation_ex/ex1.py", line 11, in <module>
    data_train = pickle.load(open("trainingData/data_train.pickle"))
  File "/usr/local/Cellar/python3/3.6.2/Frameworks/Python.framework/Versions/3.6/lib/python3.6/encodings/ascii.py", line 26, in decode
    return codecs.ascii_decode(input, self.errors)[0]
UnicodeDecodeError: 'ascii' codec can't decode byte 0x80 in position 0: ordinal not in range(128)

我找不到任何相同的问题和解决方案。我怎样才能让它工作?

4

1 回答 1

1

我的一位朋友告诉我原因。序列化对象是一个python2对象,所以如果你用python2加载,它直接打开没有任何问题。

但是如果你想用 Python3 加载,你需要添加编码参数来泡菜而不是打开函数。这是示例代码:

import numpy as np

try:
    import cPickle as pickle
except ImportError:
    import pickle


with open('data_train.pickle', 'rb') as f:
    # If you use Python 3 needs a parameter as encoding='bytes'
    # Otherwise, you shouldn't add encoding parameters in Python 2
    data_train = pickle.load(f, encoding='bytes')


print("Finished loading data!")
print(data_train.keys())

特别感谢@ahmet-sezgin-duran

于 2018-02-20T06:38:00.243 回答