CIFAR-10数据集读取

参考:https://jingyan.baidu.com/article/656db9183296c7e381249cf4.html

1、使用读取方式pickle

def unpickle(file):
    import pickle
    with open(file, 'rb') as fo:
        dict = pickle.load(fo, encoding='bytes')
    return dict

返回的是一个python字典

2、通过字典的内置函数,获取键值

>>> dict.keys()
dict_keys([b'labels', b'batch_label', b'data', b'filenames'])

3、打印所有键值对应的值

>>> dict[b'labels']------------------对应的是每个图片的真实结果Y,通过batches.meta可以找出对应的字符结果,比如:0表示‘airplane’
[6, 9, 9, 4, 1, 1, 2, 7, 8, 3, 4, 7, 7, 2, 9, 9, 9, 3, ............. 9, 1, 1, 5](结果省略了一些)

>>> len(dict[b'labels'])-----------代表图片的结果数量确实为10000
10000

>>> dict[b'batch_label']------------对应当前数据集是训练集中的那一份
b'training batch 1 of 5'

>>> dict[b'filenames']---------------对应数据集中每张图片的文件名
[b'leptodactylus_pentadactylus_s_000004.png', b'camion_s_000148.png', b'tipper_truck_s_001250.png', b'american_elk_s_001521.png',......... b'estate_car_s_001433.png', b'cur_s_000170.png'](结果同样省略了一些)

>>> dict[b'data']----------------每张图片的数据,每一位类型为uint8
array([[ 59,  43,  50, ..., 140,  84,  72],
       [154, 126, 105, ..., 139, 142, 144],
       [255, 253, 253, ...,  83,  83,  84],
       ...,
       [ 71,  60,  74, ...,  68,  69,  68],
       [250, 254, 211, ..., 215, 255, 254],
       [ 62,  61,  60, ..., 130, 130, 131]], dtype=uint8)

>>> dict[b'data'].shape----------由于我们需要用图像数据来进行卷积,所以需要知道shape
(10000, 3072)(代表有10000张图片,每张图片3072b大小(32 x 32 x 3),前1024是Red通道的图片data,接着是Green通道的1024图片,之后是Blue通道的1024图片)

原文地址:https://www.cnblogs.com/lingjiajun/p/9032380.html