我想加载cifar10数据集,但它有UnpicklingError:无效的加载密钥,''

时间:2017-10-27 04:25:42

标签: python-3.x

import import pickle
import numpy as np
import os
import gzip
from sklearn.externals import joblib

datadir='E:/python/waa/cifar10/cifar-10-batches-bin'

def load_cifar_batch(filename):
with open(filename,'rb') as f :
    datadict=pickle.load(f,encoding='bytes')
    x=datadict[b'data']
    y=datadict[b'labels']
    x=x.reshape(10000,3,32,32).transpose(0,2,3,1).astype('float')
    y=np.array(y)
    return x,y

def load_cifar10(root):
    xs=[]
    ys=[]
    for b in range(1,6):
        f=os.path.join(datadir,'data_batch_%d.bin' % (b,))
        x,y=load_cifar_batch(f)
        xs.append(x)
        ys.append(y)
    Xtrain=np.concatenate(xs) #1
    Ytrain=np.concatenate(ys)
    del x ,y
    Xtest,Ytest=load_cifar_batch(os.path.join(root,'test_batch')) #2
    return Xtrain,Ytrain,Xtest,Ytest

x_train,y_train,x_test,y_test=load_cifar10(datadir)

print('training data shape:',x_train.shape)
print('training labels shape:',y_train.shape)
print('test data shape:',x_test.shape)
print('test labels shape:',y_test.shape)

如果我从f = os.path.join(datadir,' data_batch_%d.bin'%(b,))更改为f = os.path.join(datadir,' data_batch_%d'%(b,)), 它没有.bin,错误是FileNotFoundError:[Errno 2]没有这样的文件或目录:' E:/ python / waa / cifar10 / cifar-10-batches-bin \ data_batch_1',我该如何解决这个问题,因为我无法找到办法。请帮助我。

1 个答案:

答案 0 :(得分:1)

您的问题可能与您下载的数据集有关。

你的程序对我来说工作得很好,Bin从文件路径中删除了

f=os.path.join(datadir,'data_batch_%d' % (b,)) 

我选择的数据集是CIFAR-10 python版本

下载

Version     Size    md5sum

CIFAR-10 python version     163 MB  

CIFAR-10 Matlab version     175 MB  

CIFAR-10 binary version (suitable for C programs)   162 MB