三木社区

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 525|回复: 0
打印 上一主题 下一主题

基于MNIST数据的卷积神经网络CNN

[复制链接]

1562

主题

1564

帖子

4904

积分

博士

Rank: 8Rank: 8

积分
4904
跳转到指定楼层
楼主
发表于 2017-9-18 07:50:42 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
本文基于TensorFlow官网的Tutorial写成。输入数据是MNIST,全称是Modified National Institute of Standards and Technology,是一组由这个机构搜集的手写数字扫描文件和每个文件对应标签的数据集,经过一定的修改使其适合机器学习算法读取。这个数据集可以从牛的不行的Yann LeCun教授的网站获取。
本系列文章的这一篇对这份数据集使用了softmax regression,在测试集上取得了接近92%的准确率。本文将使用卷积神经网络以获得更高的准确率。关于CNN的理论知识,可以参考这篇文章
  1. #!/usr/bin/env python
  2. # -*- coding=utf-8 -*-
  3. # @author: 陈水平
  4. # @date: 2017-02-04
  5. # @description: implement a CNN model upon MNIST handwritten digits
  6. # @ref: http://yann.lecun.com/exdb/mnist/

  7. import gzip
  8. import struct
  9. import numpy as np
  10. from sklearn import preprocessing
  11. import tensorflow as tf

  12. # MNIST data is stored in binary format,
  13. # and we transform them into numpy ndarray objects by the following two utility functions
  14. def read_image(file_name):
  15.     with gzip.open(file_name, 'rb') as f:
  16.         buf = f.read()
  17.         index = 0
  18.         magic, images, rows, columns = struct.unpack_from('>IIII' , buf , index)
  19.         index += struct.calcsize('>IIII')

  20.         image_size = '>' + str(images*rows*columns) + 'B'
  21.         ims = struct.unpack_from(image_size, buf, index)
  22.         
  23.         im_array = np.array(ims).reshape(images, rows, columns)
  24.         return im_array

  25. def read_label(file_name):
  26.     with gzip.open(file_name, 'rb') as f:
  27.         buf = f.read()
  28.         index = 0
  29.         magic, labels = struct.unpack_from('>II', buf, index)
  30.         index += struct.calcsize('>II')
  31.         
  32.         label_size = '>' + str(labels) + 'B'
  33.         labels = struct.unpack_from(label_size, buf, index)

  34.         label_array = np.array(labels)
  35.         return label_array

  36. print "Start processing MNIST handwritten digits data..."
  37. train_x_data = read_image("MNIST_data/train-images-idx3-ubyte.gz")  # shape: 60000x28x28
  38. train_x_data = train_x_data.reshape(train_x_data.shape[0], train_x_data.shape[1], train_x_data.shape[2], 1).astype(np.float32)
  39. train_y_data = read_label("MNIST_data/train-labels-idx1-ubyte.gz")  
  40. test_x_data = read_image("MNIST_data/t10k-images-idx3-ubyte.gz")  # shape: 10000x28x28
  41. test_x_data = test_x_data.reshape(test_x_data.shape[0], test_x_data.shape[1], test_x_data.shape[2], 1).astype(np.float32)
  42. test_y_data = read_label("MNIST_data/t10k-labels-idx1-ubyte.gz")

  43. train_x_minmax = train_x_data / 255.0
  44. test_x_minmax = test_x_data / 255.0

  45. # Of course you can also use the utility function to read in MNIST provided by tensorflow
  46. # from tensorflow.examples.tutorials.mnist import input_data
  47. # mnist = input_data.read_data_sets("MNIST_data/", one_hot=False)
  48. # train_x_minmax = mnist.train.images
  49. # train_y_data = mnist.train.labels
  50. # test_x_minmax = mnist.test.images
  51. # test_y_data = mnist.test.labels

  52. # Reformat y into one-hot encoding style
  53. lb = preprocessing.LabelBinarizer()
  54. lb.fit(train_y_data)
  55. train_y_data_trans = lb.transform(train_y_data)
  56. test_y_data_trans = lb.transform(test_y_data)

  57. print "Start evaluating CNN model by tensorflow..."

  58. # Model input
  59. x = tf.placeholder(tf.float32, shape=[None, 28, 28, 1])
  60. y_ = tf.placeholder(tf.float32, [None, 10])

  61. # Weight initialization
  62. def weight_variable(shape):
  63.     initial = tf.truncated_normal(shape, stddev=0.1)
  64.     return tf.Variable(initial)

  65. def bias_variable(shape):
  66.     initial = tf.constant(0.1, shape=shape)
  67.     return tf.Variable(initial)

  68. # Convolution and Pooling
  69. def conv2d(x, W):
  70.     # `tf.nn.conv2d()` computes a 2-D convolution given 4-D `input` and `filter` tensors
  71.     # input tensor shape `[batch, in_height, in_width, in_channels]`, batch is number of observation
  72.     # filter tensor shape `[filter_height, filter_width, in_channels, out_channels]`
  73.     # strides: the stride of the sliding window for each dimension of input.
  74.     # padding: 'SAME' or 'VALID', determine the type of padding algorithm to use
  75.     return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='SAME')

  76. def max_pool_2x2(x):
  77.     # `tf.nn.max_pool` performs the max pooling on the input
  78.     #  ksize: the size of the window for each dimension of the input tensor.
  79.     return tf.nn.max_pool(x, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME')


  80. # First convolutional layer
  81. # Convolution: compute 32 features for each 5x5 patch
  82. # Max pooling: reduce image size to 14x14.
  83. W_conv1 = weight_variable([5, 5, 1, 32])
  84. b_conv1 = bias_variable([32])

  85. h_conv1 = tf.nn.relu(conv2d(x,  W_conv1) + b_conv1)
  86. h_pool1 = max_pool_2x2(h_conv1)

  87. # Second convolutional layer
  88. # Convolution: compute 64 features for each 5x5 patch
  89. # Max pooling: reduce image size to 7x7
  90. W_conv2 = weight_variable([5, 5, 32, 64])
  91. b_conv2 = bias_variable([64])

  92. h_conv2 = tf.nn.relu(conv2d(h_pool1, W_conv2) + b_conv2)
  93. h_pool2 = max_pool_2x2(h_conv2)

  94. # Densely connected layer
  95. # Fully-conected layer with 1024 neurons
  96. W_fc1 = weight_variable([7 * 7 * 64, 1024])
  97. b_fc1 = bias_variable([1024])

  98. h_pool2_flat = tf.reshape(h_pool2, [-1, 7*7*64])
  99. h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, W_fc1) + b_fc1)

  100. # Dropout
  101. # To reduce overfitting, we apply dropout before the readout layer.
  102. keep_prob = tf.placeholder(tf.float32)
  103. h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob)

  104. # Readout layer
  105. W_fc2 = weight_variable([1024, 10])
  106. b_fc2 = bias_variable([10])

  107. y_conv = tf.matmul(h_fc1_drop, W_fc2) + b_fc2

  108. # Train and evaluate
  109. loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(y_conv, y_))
  110. # y = tf.nn.softmax(y_conv)
  111. # loss = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(y), reduction_indices=[1]))
  112. optimizer = tf.train.AdamOptimizer(1e-4)
  113. # optimizer = tf.train.GradientDescentOptimizer(1e-4)
  114. train = optimizer.minimize(loss)

  115. correct_prediction = tf.equal(tf.argmax(y_conv, 1), tf.argmax(y_, 1))
  116. accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))

  117. init = tf.initialize_all_variables()
  118. sess = tf.Session()
  119. sess.run(init)

  120. for step in range(20000):
  121.     sample_index = np.random.choice(train_x_minmax.shape[0], 50)
  122.     batch_xs = train_x_minmax[sample_index, :]
  123.     batch_ys = train_y_data_trans[sample_index, :]
  124.     if step % 100 == 0:
  125.         train_accuracy = sess.run(accuracy, feed_dict={
  126.             x: batch_xs, y_: batch_ys, keep_prob: 1.0})
  127.         print "step %d, training accuracy %g" % (step, train_accuracy)
  128.     sess.run(train, feed_dict={x: batch_xs, y_: batch_ys, keep_prob: 0.5})

  129. print "test accuracy %g" % sess.run(accuracy, feed_dict={
  130.     x: test_x_minmax, y_: test_y_data_trans, keep_prob: 1.0})
复制代码
输出如下:
  1. Start processing MNIST handwritten digits data...
  2. Start evaluating CNN model by tensorflow...
  3. step 0, training accuracy 0.1
  4. step 100, training accuracy 0.82
  5. step 200, training accuracy 0.92
  6. step 300, training accuracy 0.96
  7. step 400, training accuracy 0.92
  8. step 500, training accuracy 0.92
  9. step 600, training accuracy 1
  10. step 700, training accuracy 0.94
  11. step 800, training accuracy 0.96
  12. step 900, training accuracy 0.96
  13. step 1000, training accuracy 0.94
  14. step 1100, training accuracy 0.98
  15. step 1200, training accuracy 0.94
  16. step 1300, training accuracy 0.98
  17. step 1400, training accuracy 0.96
  18. step 1500, training accuracy 1
  19. ...
  20. step 15700, training accuracy 1
  21. step 15800, training accuracy 0.98
  22. step 15900, training accuracy 1
  23. step 16000, training accuracy 1
  24. step 16100, training accuracy 1
  25. step 16200, training accuracy 1
  26. step 16300, training accuracy 1
  27. step 16400, training accuracy 1
  28. step 16500, training accuracy 1
  29. step 16600, training accuracy 1
  30. step 16700, training accuracy 1
  31. step 16800, training accuracy 1
  32. step 16900, training accuracy 1
  33. step 17000, training accuracy 1
  34. step 17100, training accuracy 1
  35. step 17200, training accuracy 1
  36. step 17300, training accuracy 1
  37. step 17400, training accuracy 1
  38. step 17500, training accuracy 1
  39. step 17600, training accuracy 1
  40. step 17700, training accuracy 1
  41. step 17800, training accuracy 1
  42. step 17900, training accuracy 1
  43. step 18000, training accuracy 1
  44. step 18100, training accuracy 1
  45. step 18200, training accuracy 1
  46. step 18300, training accuracy 1
  47. step 18400, training accuracy 1
  48. step 18500, training accuracy 1
  49. step 18600, training accuracy 1
  50. step 18700, training accuracy 1
  51. step 18800, training accuracy 1
  52. step 18900, training accuracy 1
  53. step 19000, training accuracy 1
  54. step 19100, training accuracy 1
  55. step 19200, training accuracy 1
  56. step 19300, training accuracy 1
  57. step 19400, training accuracy 1
  58. step 19500, training accuracy 1
  59. step 19600, training accuracy 1
  60. step 19700, training accuracy 1
  61. step 19800, training accuracy 1
  62. step 19900, training accuracy 1
  63. test accuracy 0.9929
复制代码


回复

使用道具 举报

Archiver|手机版|小黑屋|三木电子社区 ( 辽ICP备11000133号-4 )

辽公网安备 21021702000620号

GMT+8, 2025-5-9 23:53 , Processed in 1.237570 second(s), 23 queries .

Powered by Discuz! X3.3

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表