关于 运行 CIFAR-10 数据集上的 cPickle 库的问题

Problems about cPickle library on running CIFAR-10 dataset

我目前正在关注 CIFAR-10 示例。在 http://www.cs.toronto.edu/~kriz/cifar.html 上下载数据集(CIFAR-10 python 版本)。提取文件后,"cifar-10-batches-py"文件夹出来了。

有了这个文件夹,我试图查看它的数据集。我使用了下面显示的代码。

import cPickle
import os
import numpy as np

def unpickle(file):
    fo = open(file, 'rb')
    dict = cPickle.load(fo)
    fo.close()
    return dict

def conv_data2image(data):
    return np.rollaxis(data.reshape((3,32,32)),0,3)

def get_cifar10(folder):
    tr_data = np.empty((0,32*32*3))
    tr_labels = np.empty(1)
    '''
    32x32x3
    '''
    for i in range(1,6):
        fname = os.path.join(folder, "%s%d" % ("data_batch_", i))
        data_dict = unpickle(fname)
        if i == 1:
            tr_data = data_dict['data']
            tr_labels = data_dict['labels']
        else:
            tr_data = np.vstack((tr_data, data_dict['data']))
            tr_labels = np.hstack((tr_labels, data_dict['labels']))

    data_dict = unpickle(os.path.join(folder, 'test_batch'))
    te_data = data_dict['data']
    te_labels = np.array(data_dict['labels'])

    bm = unpickle(os.path.join(folder, 'batches.meta'))
    label_names = bm['label_names']

    return tr_data, tr_labels, te_data, te_labels, label_names


if __name__ == '__main__':
    datapath = '/Users/sungtegg/Documents/cifar-10-batch.py'

    tr_data10, tr_labels10, te_data10, te_labels10, label_names10 = get_cifar10(datapath)

编译这段代码后,

"IOError: [Errno 2] No such file or directory: '/Users/sungtegg/Documents/cifar-10-batch.py/data_batch_1'

这个错误码出来了。

代码的哪一部分出错了?

您的数据路径不正确,您可以从CIFAR10下载并解压到任意文件夹。确保指向正确的数据集目录。

数据路径='/Users/sungtegg/Documents/cifar10-dataset'