本文作者:非文艺小燕儿
欢迎大家转载使用,请注明出处。
博主也在学习过程中,文章的主要目的是作为一个学习的记录和日后的参考。如果顺便能够帮到一两个同学,那也是一件令人开心的事儿。
Cifar数据库的下载地址:http://www.cs.toronto.edu/~kriz/cifar.html
最近在练习使用python,所以以下描述的是python版的数据。编辑器采用Spyder。
下载后是.gz格式的压缩包,解压缩后为:
参考地址中给出的处理方式,进行数据读取,以读取data_batch_1为例
def unpickle(file):
import cPickle
fo=open(file,'rb')
dict=cPickle.load(fo)
fo.close()
return dict
Train1=unpickle('data_batch_1')
读出来得到字典型数据
该字典型数据有以下元素:
- batch_label-- 是指你打开的是那个文件,上图说明是打开的5个batch里的第1个.
- data -- a 10000x3072 numpy array of uint8s. 每行存储一张32*32的彩色图像,前1024元素是R通道数据,中间1024元素是G通道数据,最后1024元素是B通道数据。每张图像的数据是以行扫描的形式存储的。
- labels -- a list of 10000 numbers in the range 0-9. The number at index i indicates the label of the ith image in the array data.
- filenames -- data中每行对应的图像的文件名.
解压的文件中还有batches.meta,该文件也是字典型数据,用上述同样的方式读取,得到其元素为:
- label_names -- 为数据库中10类图像的实际名字,如 label_names[0] == "airplane", label_names[1] == "automobile", etc。
- 剩下两个含义比较明了,不描述了。
训练数据分五个文件存放的,需要整合并提取出X_train,y_train,X_test,y_test.以下python代码实现该功能。
# -*- coding: utf-8 -*-
"""
Created on Fri Jan 27 18:15:45 2017
@author: VivienFu
"""
import numpy as np
def unpickle(file):
import cPickle
fo=open(file,'rb')
d=cPickle.load(fo)
fo.close()
data=d['data']
labels=d['labels']
data=data.reshape(data.shape[0],3,32,32)
return data,labels
nb_train_samples=50000
X_train=np.zeros((nb_train_samples,3,32,32),dtype='uint8')
y_train=np.zeros((nb_train_samples,),dtype='uint8')
for i in range(1,6):
filename='data_batch_'+str(i)
data,labels=unpickle(filename)
X_train[(i-1)*10000:i*10000,:,:,:]=data
y_train[(i-1)*10000:i*10000]=labels
X_test,y_test=unpickle('test_batch')
运行得到数据为: