Advertisement

【图像识别】CIFAR-10(CNN)

阅读量:

一、CIFAR-10简介

CIFAR-10官网

CIFAR-10 是由 Hinton的学生Alex Krizhevsky和 Ilya Sutskever 整理的一个用于识别普适物体的小型数据集。
它一共包含10个类别的RGB彩色图片,具体参考如下图:

Vici__CIFAR1

数据集中一共有5000张训练图片和1000张测试图片,图片的尺寸大小为 32 * 32。

官方提供文件介绍:

文件 用途
cifar10.py 建立 CIFAR-10 预测模型
cifar10_input.py 在 TensorFlow 中读入 CIFAR-10 文件
cifar10_input_test.py cifar10_input.py 的测试用例文件
cifar10_train.py 使用单个GPU或CPU训练模型
cifar10_train_multi_gpu.py 使用多个GPU训练模型
cifar10_eval.py 在测试集上测试模型的性能

- cifar10.py 文件中的 inference(images) 函数是官方提供的模型。

二、CIFAR-10 和 MNIST数据集的区别

CIFAR-10 MNIST
图像通道数 3 通道的 RGB 图像 灰度图像
尺寸大小 32 × 32 28 × 28
图片内容 现实世界的真实物体 0 ~ 9 数字
图片特点 噪声很大,可能有背景图片或其他物体干扰;
物体的比例、特征都不尽相同,识别难度大 噪声小,干扰物体少;
易识别

三、参考官方代码编写CNN模型

复制代码
>       1. import tensorflow as tf

>  
>       2. import numpy as np
>  
>       3. import time
>  
>       4. import math
>  
>       5. # 官方提供的两个文件,用于对数据集的输入等操作
>  
>       6. import cifar10
>  
>       7. import cifar10_input
>  
>       8.  
>  
>       9. max_steps = 3000 # 训练次数
>  
>       10. batch_size = 128 # 批处理参数
>  
>       11.  
>  
>       12. # ---1.加载数据 ---
>  
>       13.  
>  
>       14. # 下载cifar10数据集的默认路径,需要把cifar10.py/line 53/对应代码改一下
>  
>       15. data_dir = 'D:/Python_code/Data/cifar-10-python/cifar-10-batches-bin'
>  
>       16.  
>  
>       17. # 权值初始化函数(shape,标准差,L2正则化比例系数)
>  
>       18. def variable_with_weight_losses(shape, stddev, wl):
>  
>       19.   # 使用tf.truncated_normal截断的正态分布来初始化
>  
>       20.   var = tf.Variable(tf.truncated_normal(shape, stddev=stddev))
>  
>       21.   if wl is not None:
>  
>       22.     # 做一个L2的正则化处理,用wl控制L2的大小比例
>  
>       23.     weight_loss = tf.multiply(tf.nn.l2_loss(var), wl, name='weight_loss')
>  
>       24.     # 将weight_loss统一存放起来
>  
>       25.     tf.add_to_collection("losses", weight_loss)
>  
>       26.   return var
>  
>       27.  
>  
>       28. # 调用cifar10.py中的一个函数,下载数据集,并解压
>  
>       29. cifar10.maybe_download_and_extract()
>  
>       30.  
>  
>       31. # 生成训练数据,使用distorted_inputs函数,做数据增强处理
>  
>       32. images_train, labels_train = cifar10_input.distorted_inputs(data_dir=data_dir, batch_size=batch_size)
>  
>       33. # 生成测试数据,不必做数据增强
>  
>       34. images_test, labels_test = cifar10_input.inputs(eval_data=True, data_dir=data_dir, batch_size=batch_size)
>  
>       35.  
>  
>       36. # 占位符
>  
>       37. image_holder = tf.placeholder(tf.float32, [batch_size, 24, 24, 3])
>  
>       38. label_holder = tf.placeholder(tf.int32, [batch_size])
>  
>       39.  
>  
>       40.  
>  
>       41. # ---2. 构建模型 ---
>  
>       42.  
>  
>       43. # 第一层卷积层,64个卷积核,大小为5*5,3通道(RGB三种颜色通道)
>  
>       44.   # 1).定义权重
>  
>       45. weight1 = variable_with_weight_losses(shape=[5,5,3,64], stddev=0.05, wl=0.0)
>  
>       46.   # 2).卷积操作
>  
>       47. kernel1 = tf.nn.conv2d(image_holder, filter=weight1, strides=[1, 1, 1, 1], padding='SAME')
>  
>       48.   # 3).定义偏差
>  
>       49. bias1 = tf.Variable(tf.constant(0.0, shape=[64]))
>  
>       50.   # 4).relu激活函数
>  
>       51. conv1 = tf.nn.relu(tf.nn.bias_add(kernel1, bias1))
>  
>       52.   # 5).最大池化
>  
>       53. pool1 = tf.nn.max_pool(conv1, ksize=[1, 3, 3, 1], strides=[1, 2, 2, 1], padding='SAME')
>  
>       54.   # 6.lrn: 局部响应归一化,可防止过拟合,原理是生物学上的‘侧抑制’,(通俗来讲就是增强强的地方,削弱弱的地方)
>  
>       55.   # pool1表示输入数据,4表示使用前后几层进行归一化操作,bias表示偏移量,alpha和beta表示系数
>  
>       56. norm1 = tf.nn.lrn(pool1, 4, bias=1.0, alpha=0.001/9.0, beta=0.75)
>  
>       57.  
>  
>       58. # 第二层卷积层
>  
>       59. weight2 = variable_with_weight_losses(shape=[5,5,64,64], stddev=5e-2, wl=0.0)
>  
>       60. kernel2 = tf.nn.conv2d(norm1, filter=weight2, strides=[1, 1, 1, 1], padding='SAME')
>  
>       61. bias2 = tf.Variable(tf.constant(0.1, shape=[64]))
>  
>       62. conv2 = tf.nn.relu(tf.nn.bias_add(kernel2, bias2))
>  
>       63. norm2 = tf.nn.lrn(conv2, 4, bias=1.0, alpha=0.001/9.0, beta=0.75)
>  
>       64. pool2 = tf.nn.max_pool(norm2, ksize=[1, 3, 3, 1], strides=[1, 2, 2, 1], padding='SAME')
>  
>       65.  
>  
>       66. # 第三层全连接层
>  
>       67. # 将第二层卷积层输出结果变成一维向量
>  
>       68. reshape = tf.reshape(pool2, [batch_size, -1])
>  
>       69. dim = reshape.get_shape()[1].value
>  
>       70. # 初始化权值,隐含节点384个,正态分布标准差为0.04,偏差bias为0.1
>  
>       71. weight3 = variable_with_weight_losses(shape=[dim,384], stddev=0.04, wl=0.004)
>  
>       72. bias3 = tf.Variable(tf.constant(0.1, shape=[384]))
>  
>       73. local3 = tf.nn.relu(tf.matmul(reshape, weight3) + bias3)
>  
>       74.  
>  
>       75. # 第四层全连接层
>  
>       76. weight4 = variable_with_weight_losses(shape=[384,192], stddev=0.04, wl=0.004)
>  
>       77. bias4 = tf.Variable(tf.constant(0.1, shape=[192]))
>  
>       78. local4 = tf.nn.relu(tf.matmul(local3, weight4) + bias4)
>  
>       79.  
>  
>       80. # 第五层输出层
>  
>       81. weight5 = variable_with_weight_losses(shape=[192,10], stddev=1/192.0, wl=0.0)
>  
>       82. bias5 = tf.Variable(tf.constant(0.0, shape=[10]))
>  
>       83. logits = tf.add(tf.matmul(local4, weight5), bias5)
>  
>       84.  
>  
>       85. # 定义损失函数
>  
>       86. def loss(logits, labels):
>  
>       87.   labels = tf.cast(labels, tf.int64)
>  
>       88.   # 交叉熵损失函数
>  
>       89.   cross_entropy = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(
>  
>       90.     logits=logits, labels=labels
>  
>       91.   ))
>  
>       92.   tf.add_to_collection('losses', cross_entropy)
>  
>       93.   return tf.add_n(tf.get_collection('losses'), name='total_loss')
>  
>       94.  
>  
>       95. loss = loss(logits=logits,labels=label_holder)
>  
>       96. train_op = tf.train.AdamOptimizer(0.001).minimize(loss)
>  
>       97. top_k_op = tf.nn.in_top_k(logits, label_holder, 1)
>  
>       98.  
>  
>       99. # ---3. 训练模型---
>  
>       100. sess = tf.InteractiveSession()
>  
>       101. tf.global_variables_initializer().run()
>  
>       102. # 引入多线程
>  
>       103. tf.train.start_queue_runners()
>  
>       104.  
>  
>       105. for step in range(max_steps):
>  
>       106.   start_time = time.time()
>  
>       107.   image_batch, label_batch = sess.run([images_train, labels_train])
>  
>       108.   _, loss_value = sess.run([train_op, loss], feed_dict={image_holder: image_batch, label_holder: label_batch})
>  
>       109.   duration = time.time() - start_time
>  
>       110.   if step % 10 == 0:
>  
>       111.     examples_per_sec = batch_size / duration
>  
>       112.     sec_per_batch = float(duration)
>  
>       113.     str = 'step %d, loss = %.2f (%.1f examples/sec; %.3f sec/batch)'
>  
>       114.     #print(step, loss_value)
>  
>       115.     print(str % (step, loss_value, examples_per_sec, sec_per_batch))
>  
>       116.  
>  
>       117. num_examples = 10000
>  
>       118. num_iter = int(math.ceil(num_examples / batch_size))
>  
>       119. true_count = 0
>  
>       120. total_sample_count = num_iter * batch_size
>  
>       121. step = 0
>  
>       122. while step < num_iter:
>  
>       123.   image_batch, label_batch = sess.run([images_test, labels_test])
>  
>       124.   predictions = sess.run([top_k_op], feed_dict={image_holder: image_batch, label_holder: label_batch})
>  
>       125.   true_count += np.sum(predictions)
>  
>       126.   step += 1
>  
>       127.  
>  
>       128. precision = true_count / total_sample_count
>  
>       129. print('precision = %.3f' % precision)
>  
>  
>

四、训练结果

左图为基于 MNIST 应用 RMSProp 算法的优化器;
右图为参考书籍中使用 Adam 优化算法的优化器。
经过3000次测试。

参考文献:

《21个项目掌握深度学习》何之源著
bilibili: CNN识别图片
CIFAR 官网

全部评论 (0)

还没有任何评论哟~