宝塔服务器面板,一键全能部署及管理,送你10850元礼包,点我领取

Theano入门——CIFAR-10和CIFAR-100数据集1.CIFAR-10数据集介绍CIFAR-10数据集包含60000个32*32彩色图像,共有10种类型。 有50000个训练图像和10000个测试图像。

数据集分为五个训练块和一个测试块,每个块有10000个图像。 测试块包含为每个类随机选择的1000个图像。 训练块以随机顺序包含这些图像,但一些训练块可能包含的图像比其他类多。 训练块每个类别包含5000张图像。 类之间完全排他。 汽车和卡车类不重叠。 “Automobile”只包含sedans、SUVs等。 “Truck”只包括重型卡车。 两者都不包括皮卡车。

2.CIFAR-10数据集Python版本归档包含文件data_batch_1、data_batch_2、data_batch_5和test_batch 每个文件都是一个Python’pickled ‘对象。 按如下方式装入词典并返回一个。

defunpickle(file ) :importcpicklefo=open ) file,’ rb ‘ ) dict=c pickle.load (fo.close ) ) return dict每个块文件中

data——1个10000*3072大小的uint8s数组。 数组的每一行存储一张32*32的图像。 第一个1024包含红色通道值,下一个包含绿色,最后一个1024包含蓝色。 因为图像存储以行顺序为中心,所以数组的前32列是图像第一行的红色通道值。

labels——1个10000个范围为0~9的列表。 索引I中的数字表示数组data中第I个图像的标签。

数据集包含另一个名为batches.meta的文件。 它还包含一个Python词典对象。 这些要素包括: label_names——1个10个元素的列表。 为标签中的数字标签指定有意义的名称。 例如,label_names[0]==’airplane ‘,label_names[1]==’automobile ‘等。

3.CIFAR-100数据集包含100个小班,每个小班包含600个图像,其中有500个训练图像和100个测试图像。 第100类分为20类。 每个图像都有一个小类别的“fine”标签和一个大类别的“coarse”标签。

4.CIFAR-100数据集Python版本和CIFAR-10数据集Python版本。

5.CIFAR-10代码实现(1) CIFAR-10数据集存储在相对文件路径data_dir_cifar10下。

(2) _load_batch_cifar10函数

此函数加载CIFAR-10格式的块文件。 将块文件名filename和相对文件路径data_dir_cifar10连接起来得到块文件的位置。 用numpy中的load函数加载(也可以用cPickle中的load函数加载)并返回batch。 batch是一个词典,里面包含数据和标签。 从数据索引’ data ‘获得图像数据,从标签索引’ labels ‘获得图像分类标签,并将标签转换为one-hot编码格式。 请参阅上一篇文章中MNIST数据集的说明。 最后,将数据和标记中元素的数据类型统一为dtype类型。

(3)计数函数

该函数在axis=0时,将矩阵按行顺序从上到下排列,在axis=1时,将矩阵按列顺序从左到右排列。

(4) _grayscale函数

该函数首先将a变形为四维张量,维数为a.shape[0]、3、32、32]。 以前的a是矩阵形式,a的每一行代表一个图像样本。 a列是按照红、绿、蓝的顺序对图像中的所有像素进行排序的结果。 这意味着图像中所有像素的红色通道值、图像中所有像素的绿色通道值和图像中所有像素的蓝色通道值。 3表示颜色通道的数量,32表示图像的行数和列数。 这样,reshape函数首先通过将二维矩阵a的所有行排列成一行,首先将这一行剪切为a.shape[0]个行b,然后对行b进行3分割,分别对行c、行c进行32分割,分别为

mean(1)对应于第二个轴(颜色通道轴),对第二个轴的平均值(即三个通道值进行平均,最后第二个轴是单通道(灰色通道) )。 最后变形得到二维矩阵(a.shape[0],32*32 ) )。

)5) cifar10函数

cifar函数首先调用_load_batch_cifar10函数读取块文件,返回的x和t都是列表格式。 所以可以用append连接。 连接后,每个块文件的内容都放在大括号中,因此concatenate函数会移除用于区分外部块文件类型的括号。 此时获得的x_train行为图像样本被列举为像素的红色、绿色和蓝色通道值; t_train的动作图像标签。 列为标签的one-hot编码值。 x_test和t_test的结构相同。 变换为灰度图像后x_train和x_test是矩阵,行动图像样本被列举为像素被归一化后的灰度值; t_train和t_test是矩阵,在行为图像标记中列为每个one-hot编码值。

import numpy as

npimport osimport cPickle as pickleimport globimport matplotlib.pyplot as pltdata_dir = “data”data_dir_cifar10 = os.path.join(data_dir, “cifar-10-batches-py”)data_dir_cifar100 = os.path.join(data_dir, “cifar-100-python”)class_names_cifar10 = np.load(os.path.join(data_dir_cifar10, “batches.meta”))class_names_cifar100 = np.load(os.path.join(data_dir_cifar100, “meta”))def one_hot(x, n): “”” convert index representation to one-hot representation “”” x = np.array(x) assert x.ndim == 1 return np.eye(n)[x]def _load_batch_cifar10(filename, dtype=’float64′): “”” load a batch in the CIFAR-10 format “”” path = os.path.join(data_dir_cifar10, filename) batch = np.load(path) data = batch[‘data’] / 255.0 # scale between [0, 1] labels = one_hot(batch[‘labels’], n=10) # convert labels to one-hot representation return data.astype(dtype), labels.astype(dtype)def _grayscale(a): print a.reshape(a.shape[0], 3, 32, 32).mean(1).reshape(a.shape[0], -1) return a.reshape(a.shape[0], 3, 32, 32).mean(1).reshape(a.shape[0], -1)def cifar10(dtype=’float64′, grayscale=True): # train x_train = [] t_train = [] for k in xrange(5): x, t = _load_batch_cifar10(“data_batch_%d” % (k + 1), dtype=dtype) x_train.append(x) t_train.append(t) x_train = np.concatenate(x_train, axis=0) t_train = np.concatenate(t_train, axis=0) # test x_test, t_test = _load_batch_cifar10(“test_batch”, dtype=dtype) if grayscale: x_train = _grayscale(x_train) x_test = _grayscale(x_test) return x_train, t_train, x_test, t_testdef _load_batch_cifar100(filename, dtype=’float64′): “”” load a batch in the CIFAR-100 format “”” path = os.path.join(data_dir_cifar100, filename) batch = np.load(path) data = batch[‘data’] / 255.0 labels = one_hot(batch[‘fine_labels’], n=100) return data.astype(dtype), labels.astype(dtype)def cifar100(dtype=’float64′, grayscale=True): x_train, t_train = _load_batch_cifar100(“train”, dtype=dtype) x_test, t_test = _load_batch_cifar100(“test”, dtype=dtype) if grayscale: x_train = _grayscale(x_train) x_test = _grayscale(x_test) return x_train, t_train, x_test, t_testXtrain, Ytrain, Xtest, Ytest = cifar10()################################################# 图像样本显示image = Xtrain[0].reshape(32, 32)image1 = Xtrain[255].reshape(32, 32)fig = plt.figure()ax = fig.add_subplot(121)plt.axis(‘off’)plt.title(class_names_cifar10[‘label_names’][list(Ytrain[0]).index(1)])plt.imshow(image, cmap=’gray’)ax = fig.add_subplot(122)plt.title(class_names_cifar10[‘label_names’][list(Ytrain[255]).index(1)])plt.imshow(image1, cmap=’gray’)plt.axis(‘off’)plt.show()

6.实验结果

7.参考链接

(1)CIFAR数据集:http://www.cs.toronto.edu/~kriz/cifar.html
(2)数据集加载:https://github.com/benanne/theano-tutorial/blob/master/load.py