当我尝试释放cifar-10数据集时出现以下错误。我需要训练一个模型,但我什至无法获得操作数据。我该如何解决这个问题
dict = cPickle.load(fo)
UnpicklingError:无效的加载密钥'\ x06'。
import tensorflow as tf
import os
import numpy as np
import dataset_class
import matplotlib.pyplot as plt
import matplotlib.cm as cm
import glob
from PIL import Image
from scipy.spatial.distance import pdist
def cifar_10_reshape(batch_arg):
output=np.reshape(batch_arg,(10000,3,32,32)).transpose(0,2,3,1)
return output
def unpickle(file):
import _pickle as cPickle
fo=open(file,'rb')
dict=cPickle.load(fo)
fo.close()
return dict
#Loading cifar-10 data and reshaping it to be batch_sizex32x32x3
batch1=unpickle('cifar-10-batches-py/data_batch_1.bin')
batch2=unpickle('cifar-10-batches-py/data_batch_2.bin')
batch3=unpickle('cifar-10-batches-py/data_batch_3.bin')
batch4=unpickle('cifar-10-batches-py/data_batch_4.bin')
batch5=unpickle('cifar-10-batches-py/data_batch_5.bin')
batch1_data=cifar_10_reshape(batch1['data'])
batch2_data=cifar_10_reshape(batch2['data'])
batch3_data=cifar_10_reshape(batch3['data'])
batch4_data=cifar_10_reshape(batch4['data'])
batch5_data=cifar_10_reshape(batch5['data'])
batch1_labels=batch1['labels']
batch2_labels=batch2['labels']
batch3_labels=batch3['labels']
batch4_labels=batch4['labels']
batch5_labels=batch5['labels']
test_batch=unpickle('cifar-10-batches-py/test_batch')
test_images=cifar_10_reshape(test_batch['data'])
test_labels_data=test_batch['labels']
train_images=np.concatenate((batch1_data,batch2_data,batch3_data,batch4_data,batch5_data),axis=0)
train_labels_data=np.concatenate((batch1_labels,batch2_labels,batch3_labels,batch4_labels,batch5_labels),axis=0)
最佳答案
根据我对CIFAR-10数据集的了解,您尝试发布的版本是二进制格式,而您没有向“拆箱者”提供有关编码的任何信息。您可能会更幸运地尝试使用CIFAR-10网站(https://www.cs.toronto.edu/~kriz/cifar.html)为python 3.x提供的加载功能:
def unpickle(file):
import pickle
with open(file, 'rb') as fo:
dict = pickle.load(fo, encoding='bytes')
return dict
关于python - Cifar-10/解开,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/49782292/