Pickle不能加载Pascal VOC pickle数据集

时间:2018-02-19 08:34:56

标签: python-3.x numpy pickle semantic-segmentation

我正在尝试从斯坦福网站here加载Pascal VOC数据集。还尝试从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)

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

1 个答案:

答案 0 :(得分:1)

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

但是如果你想加载Python3,你需要添加编码参数来pickle而不是open函数。以下是示例代码:

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