当前位置:   article > 正文

TensorFlow实战 4 进阶CNN -- CIFAR-10数据集训练与测试_基于cifar-10数据集,完成cnn分类模型的训练、测试与评估。 1. 学习jupyter not

基于cifar-10数据集,完成cnn分类模型的训练、测试与评估。 1. 学习jupyter notebo

    背景:CIFAR-10数据集包含10个类别的RGB彩色图片。图片尺寸为32×32,这十个类别包括:飞机、汽车、鸟、猫、鹿、狗、蛙、马、船、卡车。一共有50000张训练图片和10000张测试图片。

    与之前简单的LeNet相比,新的神经网络具有以下特点:

    1)对weights采用了L2正则

    2)对图像数据做了翻转、随机剪切等数据增强处理,制造新样本

    3)在每个卷积层、池化层输出后连接LRN(local response neurial)层, 提升模型泛化能力

实现步骤:

 1、下载CIFAR-10数据集 (cifar-10-binary)

git clone https://github.com/tensorflow/models.git

 2、下载cifar10.py和cifar10_input.py文件至cifar-10-binary同一目录下

https://github.com/tensorflow/models/tree/master/tutorials/image/cifar10

3、实现代码如下:

  1. import tensorflow as tf
  2. import matplotlib.pyplot as plt
  3. import time
  4. import numpy as np
  5. import cifar10, cifar10_input
  6. #define patameters
  7. max_steps = 8000
  8. batch_size = 512
  9. n_fc_1 = 128
  10. n_fc_2 = 64
  11. display_step = 200
  12. data_dir = 'cifar-10-binary/cifar-10-batches-bin'
  13. #define L2 regularization for weight loss
  14. def l2_weight_loss(shape, stddev, w_1):
  15. '''
  16. description: to adopt L2 regularization for weight to prevent over-fitting
  17. Args: shape:
  18. stddev: standard deviation
  19. w_1: weight coeffient
  20. Returns:
  21. weight: the regularized weight coeffient
  22. '''
  23. weight = tf.Variable(tf.truncated_normal(shape, stddev))
  24. if w_1 is not None:
  25. weight_loss = tf.multiply(tf.nn.l2_loss(weight), w_1, name='weight_loss')
  26. tf.add_to_collection('losses', weight_loss)
  27. return weight
  28. #define weight initializer
  29. def weight_init(shape, stddev):
  30. '''
  31. description: to initialize weight values
  32. Args: shape:
  33. stddev: standard deviation
  34. Returns:
  35. weight: weight values
  36. '''
  37. return tf.Variable(tf.truncated_normal(shape, stddev))
  38. #define biases initializer
  39. def biases_init(shape):
  40. '''
  41. description: to initialize biases values
  42. Args: shape:
  43. Returns: biases values
  44. '''
  45. return tf.Variable(tf.random_normal(shape))
  46. #define conv layer
  47. def conv2d(x_image, weight):
  48. '''
  49. description: to excute conv operation
  50. Args: x_image: input data
  51. weight: weight values
  52. Returns: conv result
  53. '''
  54. return tf.nn.conv2d(x_image, weight, strides = [1, 1, 1, 1], padding = 'SAME')
  55. #define max pooling layer
  56. def max_pool(x_image):
  57. '''
  58. description: to adopt max pooling method
  59. Args: x_image: input data
  60. Returns: max pooling result
  61. '''
  62. return tf.nn.max_pool(x_image, ksize = [1, 3, 3, 1], strides = [1, 2, 2, 1], padding = 'SAME')
  63. def lrn_norm(x_image):
  64. '''
  65. description : to adopt LRN(local response neurial) normalization
  66. Args: x_image: input data
  67. Returns: the lrn normalized result
  68. '''
  69. return tf.nn.lrn(x_image, 4, bias = 1.0, alpha = 0.001 / 9.0, beta = 0.75)
  70. #image enhancement processing for trainning data sets
  71. train_images, train_labels = cifar10_input.distorted_inputs(batch_size = batch_size, data_dir = data_dir)
  72. #define input placeholder
  73. x_images = tf.placeholder(tf.float32, [batch_size, 24, 24, 3])
  74. x_labels = tf.placeholder(tf.float32, [batch_size])
  75. #define layer 1
  76. w_1 = weight_init([5, 5, 3, 32], 0.05)
  77. b_1 = biases_init([32])
  78. conv_1 = tf.nn.relu(conv2d(x_images, w_1) + b_1)
  79. pool_1 = max_pool(conv_1)
  80. lrn_1 = lrn_norm(pool_1)
  81. #define layer 2
  82. w_2 = weight_init([5, 5, 32, 32], 0.05)
  83. b_2 = biases_init([32])
  84. conv_2 = tf.nn.relu(conv2d(lrn_1, w_2) + b_2)
  85. pool_2 = max_pool(conv_2)
  86. #define flatten layer
  87. re_shape = tf.reshape(pool_2, [batch_size, -1])
  88. n_input = re_shape.get_shape()[1].value
  89. #define full connection 1
  90. w_3 = l2_weight_loss([n_input, n_fc_1], 0.05, w_1 = 0.001)
  91. b_3 = biases_init([n_fc_1])
  92. fc_1 = tf.nn.relu(tf.matmul(re_shape, w_3) + b_3)
  93. #define full connection 2
  94. w_4 = l2_weight_loss([n_fc_1, n_fc_2], 0.05, w_1 = 0.003)
  95. b_4 = biases_init([n_fc_2])
  96. fc_2 = tf.nn.relu(tf.matmul(fc_1, w_4) + b_4)
  97. #define output layer
  98. w_5 = weight_init([n_fc_2, 10], 1.0 / 96.0)
  99. b_5 = biases_init([10])
  100. logits = tf.add(tf.matmul(fc_2, w_5), b_5)
  101. y_pred = tf.nn.softmax(logits)
  102. #define loss function
  103. x_labels = tf.cast(x_labels, tf.int32)
  104. cross_enerty = tf.nn.sparse_softmax_cross_entropy_with_logits(logits = logits, labels = x_labels, name = 'cross_enerty_pre_example')
  105. losses = tf.reduce_mean(cross_enerty, name = 'cross_enerty')
  106. tf.add_to_collection('losses', losses)
  107. loss = tf.add_n(tf.get_collection('losses'), name = 'total_loss')
  108. #define optimizer
  109. optimizer = tf.train.AdamOptimizer(learning_rate = 0.0001).minimize(loss)
  110. #display test result
  111. test_images, test_labels = cifar10_input.inputs(batch_size = batch_size, data_dir = data_dir, eval_data = True)
  112. #claculate trainning accuracy
  113. def accuracy(test_labels, y_pred):
  114. '''
  115. description: to claculate the trainning accuracy
  116. Args: test_labels: test labels
  117. y_pred: conv output value
  118. Returns:
  119. accuracy of trainning result
  120. '''
  121. test_labels = tf.to_int64(test_labels)
  122. correction_pred = tf.equal(test_labels, tf.argmax(y_pred, 1))
  123. acc = tf.reduce_mean(tf.cast(correction_pred, tf.float32))
  124. return acc
  125. #create session
  126. sess = tf.Session()
  127. init = tf.global_variables_initializer()
  128. sess.run(init)
  129. #create thread to accelaeate processing efficience
  130. tf.train.start_queue_runners(sess = sess)
  131. #start trainning and display
  132. cross_loss = []
  133. for i in range(max_steps):
  134. start_time = time.time()
  135. batch_xs, batch_ys = sess.run([train_images, train_labels])
  136. _, c = sess.run([optimizer, loss], feed_dict = {x_images:batch_xs, x_labels:batch_ys})
  137. cross_loss.append(c)
  138. every_epoch_time = time.time() - start_time
  139. if i % display_step == 0:
  140. examples_per_sec = batch_size / every_epoch_time
  141. every_batch_time = float(every_epoch_time)
  142. print('Epoch : ', '%d'%(i+100), 'loss : ', '{:.5f}'.format(c))
  143. print("optimization finished ! ")
  144. #display loss processing
  145. fig, ax = plt.subplots(figsize(13, 6))
  146. ax.plot(cross_loss)
  147. plt.grid()
  148. plt.title("trian loss")
  149. plt.show()
  150. #claculate test accuracy
  151. for i in range(10):
  152. test_acc = []
  153. batch_xs, batch_ys = sess.run([test_images, test_labels])
  154. batch_y_pred = sess.run(y_pred, feed_dict = {x_images:batch_xs})
  155. test_accuracy = accuracy(batch_ys, batch_y_pred)
  156. acc = sess.run(test_accuracy, feed_dict = {x_images:batch_xs})
  157. test_acc.append(acc)
  158. print("test accuracy : ", acc)
  159. print("mean accuracy : ", np.mean(test_acc))

最终验证准确率为86%【CPU版本TensorFlow,整整训练了1个小时】

practice makes perfect!

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Monodyee/article/detail/598256
推荐阅读
相关标签
  

闽ICP备14008679号