CS231n 第一次作业KNN中本地CIFAR10数据集的载入

一、问题描述

网上绝大多数作业参考都是在jupyter下运行的,数据集载入过程一般如下:

from cs231n.data_utils import load_CIFAR10

#导入数据集,并打印出数据集相关参数以确定是否加载成功

cifar10_dir =  'cs231n/datasets/cifar-10-batches-py' #数据集地址(获取数据集的脚本)

#删除以前可能导入的数据,若之前未导入数据,则直接pass
#try...except...为解决异常的语句,参见https://www.cnblogs.com/Lival/p/6203111.html
try:
    del X_train, y_train
    del X_test, y_test
    print('Clear previously loaded data')
except:
    pass

X_train, y_train, X_test, y_test = load_CIFAR10(cifar10_dir) #加载数据集

#打印出数据集相关参数以确定是否加载成功
print('Training data shapa:', X_train.shape)
print('Training labels shape:', y_train.shape)
print('Test data shape:', X_test.shape)
print('Test labels shape:', y_test.shape)

1)给出数据集的地址(获取数据集的脚本)

cifar10_dir = 'cs231n/datasets/cifar-10-batches-py'

2)使用斯坦福布置作业时已提供的脚本读取数据集

from cs231n.data_utils import load_CIFAR10

X_train, y_train, X_test, y_test = load_CIFAR10(cifar10_dir)

需要注意的是,获取数据集的脚本和读取数据集的脚本都在名为”cs231n“的文件夹里面,必须保证该文件夹与当前的.ipynb文件在同一文件夹下。

在jupyter下运行,报错:

FileNotFoundError: [Errno 2] No such file or directory: 'cs231n/datasets/cifar-10-batches-py\data_batch_1'

二、解决方法

将数据集下到本地,直接给出数据集在本地的绝对地址。

cifar10_dir = 'F:JupyterCIFAR10'

其他照旧,可正确读取出数据集:

Training data shapa: (50000, 32, 32, 3)
Training labels shape: (50000,)
Test data shape: (10000, 32, 32, 3)
Test labels shape: (10000,)

附:CIFAR10百度网盘下载链接:https://pan.baidu.com/s/1wfVcvZAvPgsvKyrdaEqGGg      提取码:qd4v 

原文地址:https://www.cnblogs.com/HL-space/p/10546588.html