无法为 Pascal VOC pickle 数据集加载 Pickle

Pickle can't be load for Pascal VOC pickle dataset

我正在尝试从斯坦福网站加载 Pascal VOC 数据集 here. Also trying to implement a code from Semantic Image Segmentation on Pascal VOC Pystruct blog。但是当我尝试加载 pickle 文件时出现 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)

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

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

但是如果你想加载Python3,你需要添加编码参数来pickle not into open function。这是示例代码:

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