深度学习:CIFAR-10数据集读取实操

CIFAR-10和CIFAR-100是来自于80 million张小型图片的数据集,图片收集者是Alex Krizhevsky, Vinod Nair, and Geoffrey Hinton。

官网 http://www.cs.toronto.edu/~kriz/cifar.html

cifar-10 数据集说明及下载
数据集组成
本数据及包含了6万张分辨率为32x32的图片,一共分为了10类,分别为:

飞机、汽车、鸟、猫、鹿、狗、青蛙、马、船、货车


其中,5万张作为训练集,1万张作为测试机。
训练集被分为了5批训练和1批测试。每一批都是1万张。

测试集是从每一种分类中随机抽取出来1000张组成。
训练集从10个分类中各自随机抽取5000张,一共5万张。

----------------------------------------------------------------------------

数据的结构
首先看一下数据集解压之后的样子:

æ°æ®é解åç»æ

那些没有后缀名的文件,其实都是用python的cpickle库打包好的,这个库就是用来将python中的变量原封不动地存到本地使用的,当再次使用的时候,可以读取出来。
有经验的朋友会自动和json.dumps联系起来,其实json模块只能保存一个字典到本地的json文件,而pickle模块(或者cpickle却可以打包任何对象).
在python3环境下读取数据:

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

data = unpickle('test_batch')
data.keys() # dict_keys([b'batch_label', b'labels', b'data', b'filenames'])
data[b'data'][0] # array([158, 159, 165, ..., 124, 129, 110], dtype=uint8)


每个字典如下表所示,每个batch文件转换为dictonary,其中的内容是:

data a 10000×3072 array(uint85),array的每行是一张32×32的彩色图片,前1024是red channel的值,后面1024是green channel的值,最后1024是blue channel的值。图片是以行主顺序存储,所以,前数组中前32个数表示的是一张图片第一行的red channel values。
labels 标签,长度是10000,每个都是0-9的数字,是一个列表。其索引和data里面的索引相互对应。
batch_label batch的名称
filenames 数据集中data对应的图片名称数组


除此之外还有一个文件是batches.meta,这个文件保存的就是索引与名字之间的对应。如:label_names[0] == “airplane”, label_names[1] == “automobile”

batches.meta文件包含了[b’num_cases_per_batch’, b’label_names’, b’num_vis’]
label_names – 十个类别对应的英文名
cifar-10划分为5个training batches。图像也是彩色,所以要给图像的三个通道进行管理。
 

cifar10和cifar100,还可以参考:https://www.cnblogs.com/cloud-ken/p/8456878.html

读取代码参考:https://blog.csdn.net/DarrenXf/article/details/85471718,cifar10数据格式以及读取方式

https://blog.csdn.net/u014281392/article/details/74881967,TensorFlow进阶:CNN对CIFAR10图像分类

CIFAR-10和python读取https://www.cnblogs.com/jimobuwu/p/9161531.html

将cifar的3个单通道数据,转化为三通道数据,并保存

p = './data/cifar-10-batches/data_batch_1'
d = unpickle(p)
#print(d)
#print(d.keys())
#print(d[b'batch_label'])
#print(d[b'labels'])
#print(d[b'filenames'])

e = d[b'data']
for i in range(100):
    image = e[i]
    red_image = image[:1024].reshape(32,32)
    green_image = image[1024:2048].reshape(32,32)
    blue_image = image[2048:].reshape(32,32)
    result_img = np.ones((32, 32, 3), dtype=np.uint8)
    result_img[:,:,0] = red_image
    result_img[:,:,1] = green_image
    result_img[:,:,2] = blue_image
    cv2.imwrite('a\\'+str(i)+'.jpg',result_img)

发布了3 篇原创文章 · 获赞 0 · 访问量 557

猜你喜欢

转载自blog.csdn.net/yiyayiya557/article/details/104676340