# 基于tensorflow+CNN的MNIST数据集手写数字分类预测

2018年9月18日笔记

tensorflow是谷歌google的深度学习框架，tensor中文叫做张量，flow叫做流。 CNN是convolutional neural network的简称，中文叫做卷积神经网络。 MNIST是Mixed National Institue of Standards and Technology database的简称，中文叫做美国国家标准与技术研究所数据库。 此文在上一篇文章《基于tensorflow+DNN的MNIST数据集手写数字分类预测》的基础上修改模型为卷积神经网络模型，模型准确率从98%提升到99.2% 《基于tensorflow+DNN的MNIST数据集手写数字分类预测》文章链接：https://www.jianshu.com/p/9a4ae5655ca6

## 2.下载并解压数据集

MNIST数据集下载链接: https://pan.baidu.com/s/1fPbgMqsEvk2WyM9hy5Em6w 密码: wa9p 下载压缩文件MNIST_data.rar完成后，选择解压到当前文件夹不要选择解压到MNIST_data。 文件夹结构如下图所示：

image.png

## 4.完整代码

```import warnings
warnings.filterwarnings('ignore')
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data

batch_size = 100
X_holder = tf.placeholder(tf.float32)
y_holder = tf.placeholder(tf.float32)

X_images = tf.reshape(X_holder, [-1, 28, 28, 1])
#convolutional layer 1
conv1_Weights = tf.Variable(tf.truncated_normal([5, 5, 1, 32], stddev=0.1))
conv1_biases = tf.Variable(tf.constant(0.1, shape=[32]))
conv1_conv2d = tf.nn.conv2d(X_images, conv1_Weights, strides=[1, 1, 1, 1], padding='SAME') + conv1_biases
conv1_activated = tf.nn.relu(conv1_conv2d)
conv1_pooled = tf.nn.max_pool(conv1_activated, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME')
#convolutional layer 2
conv2_Weights = tf.Variable(tf.truncated_normal([5, 5, 32, 64], stddev=0.1))
conv2_biases = tf.Variable(tf.constant(0.1, shape=[64]))
conv2_conv2d = tf.nn.conv2d(conv1_pooled, conv2_Weights, strides=[1, 1, 1, 1], padding='SAME') + conv2_biases
conv2_activated = tf.nn.relu(conv2_conv2d)
conv2_pooled = tf.nn.max_pool(conv2_activated, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME')
#full connected layer 1
connect1_flat = tf.reshape(conv2_pooled, [-1, 7 * 7 * 64])
connect1_Weights = tf.Variable(tf.truncated_normal([7 * 7 * 64, 1024], stddev=0.1))
connect1_biases = tf.Variable(tf.constant(0.1, shape=[1024]))
connect1_activated = tf.nn.relu(connect1_Wx_plus_b)
#full connected layer 2
connect2_Weights = tf.Variable(tf.truncated_normal([1024, 10], stddev=0.1))
connect2_biases = tf.Variable(tf.constant(0.1, shape=[10]))
predict_y = tf.nn.softmax(connect2_Wx_plus_b)
#loss and train
loss = tf.reduce_mean(-tf.reduce_sum(y_holder * tf.log(predict_y), 1))
train = optimizer.minimize(loss)

init = tf.global_variables_initializer()
session = tf.Session()
session.run(init)

for i in range(1001):
train_images, train_labels = mnist.train.next_batch(200)
session.run(train, feed_dict={X_holder:train_images, y_holder:train_labels})
if i % 100 == 0:
correct_prediction = tf.equal(tf.argmax(predict_y, 1), tf.argmax(y_holder, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
test_images, test_labels = mnist.test.next_batch(2000)
train_accuracy = session.run(accuracy, feed_dict={X_holder:train_images, y_holder:train_labels})
test_accuracy = session.run(accuracy, feed_dict={X_holder:test_images, y_holder:test_labels})
print('step:%d train accuracy:%.4f test accuracy:%.4f' %(i, train_accuracy, test_accuracy))```

Extracting MNIST_data\train-images-idx3-ubyte.gz Extracting MNIST_data\train-labels-idx1-ubyte.gz Extracting MNIST_data\t10k-images-idx3-ubyte.gz Extracting MNIST_data\t10k-labels-idx1-ubyte.gz step:0 train accuracy:0.1750 test accuracy:0.1475 step:100 train accuracy:0.8900 test accuracy:0.9080 step:200 train accuracy:0.9150 test accuracy:0.9375 step:300 train accuracy:0.9600 test accuracy:0.9525 step:400 train accuracy:0.9600 test accuracy:0.9605 step:500 train accuracy:0.9400 test accuracy:0.9670 step:600 train accuracy:0.9700 test accuracy:0.9680 step:700 train accuracy:0.9750 test accuracy:0.9630 step:800 train accuracy:0.9850 test accuracy:0.9745 step:900 train accuracy:1.0000 test accuracy:0.9760 step:1000 train accuracy:0.9750 test accuracy:0.9795

## 5.数据准备

```import warnings
warnings.filterwarnings('ignore')
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data

batch_size = 100
X_holder = tf.placeholder(tf.float32)
y_holder = tf.placeholder(tf.float32)```

image.png

## 6.搭建神经网络

```X_images = tf.reshape(X_holder, [-1, 28, 28, 1])
#convolutional layer 1
conv1_Weights = tf.Variable(tf.truncated_normal([5, 5, 1, 32], stddev=0.1), name='conv1_Weights')
conv1_biases = tf.Variable(tf.constant(0.1, shape=[32]), name='conv1_biases')
conv1_conv2d = tf.nn.conv2d(X_images, conv1_Weights, strides=[1, 1, 1, 1], padding='SAME') + conv1_biases
conv1_activated = tf.nn.relu(conv1_conv2d)
conv1_pooled = tf.nn.max_pool(conv1_activated, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME')
#convolutional layer 2
conv2_Weights = tf.Variable(tf.truncated_normal([5, 5, 32, 64], stddev=0.1), name='conv2_Weights')
conv2_biases = tf.Variable(tf.constant(0.1, shape=[64]), name='conv2_biases')
conv2_conv2d = tf.nn.conv2d(conv1_pooled, conv2_Weights, strides=[1, 1, 1, 1], padding='SAME') + conv2_biases
conv2_activated = tf.nn.relu(conv2_conv2d)
conv2_pooled = tf.nn.max_pool(conv2_activated, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME')
#full connected layer 1
connect1_flat = tf.reshape(conv2_pooled, [-1, 7 * 7 * 64])
connect1_Weights = tf.Variable(tf.truncated_normal([7 * 7 * 64, 1024], stddev=0.1), name='connect1_Weights')
connect1_biases = tf.Variable(tf.constant(0.1, shape=[1024]), name='connect1_biases')
connect1_activated = tf.nn.relu(connect1_Wx_plus_b)
#full connected layer 2
connect2_Weights = tf.Variable(tf.truncated_normal([1024, 10], stddev=0.1), name='connect2_Weights')
connect2_biases = tf.Variable(tf.constant(0.1, shape=[10]), name='connect2_biases')
predict_y = tf.nn.softmax(connect2_Wx_plus_b)
#loss and train
loss = tf.reduce_mean(-tf.reduce_sum(y_holder * tf.log(predict_y), 1))
train = optimizer.minimize(loss)```

## 7.变量初始化

```init = tf.global_variables_initializer()
session = tf.Session()
session.run(init)```

image.png

## 8.模型训练

```for i in range(1001):
train_images, train_labels = mnist.train.next_batch(200)
session.run(train, feed_dict={X_holder:train_images, y_holder:train_labels})
if i % 100 == 0:
correct_prediction = tf.equal(tf.argmax(predict_y, 1), tf.argmax(y_holder, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
test_images, test_labels = mnist.test.next_batch(2000)
train_accuracy = session.run(accuracy, feed_dict={X_holder:train_images, y_holder:train_labels})
test_accuracy = session.run(accuracy, feed_dict={X_holder:test_images, y_holder:test_labels})
print('step:%d train accuracy:%.4f test accuracy:%.4f' %(i, train_accuracy, test_accuracy))```

step:0 train accuracy:0.0850 test accuracy:0.1200 step:100 train accuracy:0.9200 test accuracy:0.8980 step:200 train accuracy:0.9400 test accuracy:0.9445 step:300 train accuracy:0.9400 test accuracy:0.9595 step:400 train accuracy:0.9450 test accuracy:0.9595 step:500 train accuracy:0.9750 test accuracy:0.9640 step:600 train accuracy:0.9800 test accuracy:0.9675 step:700 train accuracy:0.9800 test accuracy:0.9775 step:800 train accuracy:0.9900 test accuracy:0.9700 step:900 train accuracy:0.9850 test accuracy:0.9825 step:1000 train accuracy:0.9750 test accuracy:0.9765

## 9.保存模型

```saver = tf.train.Saver()
save_path = saver.save(session, 'save_model/mnist_cnn.ckpt')
print('Save to path:', save_path)```

## 10.加载模型

image.png

save_model文件夹与代码文件在同级目录下，即可成功运行下面的代码。 请读者对照下图，确保自己的代码文件数据、模型放置在正确的路径下。

image.png

```import warnings
warnings.filterwarnings('ignore')
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data

batch_size = 100
X_holder = tf.placeholder(tf.float32)
y_holder = tf.placeholder(tf.float32)

X_images = tf.reshape(X_holder, [-1, 28, 28, 1])
#convolutional layer 1
conv1_Weights = tf.Variable(tf.truncated_normal([5, 5, 1, 32], stddev=0.1), name='conv1_Weights')
conv1_biases = tf.Variable(tf.constant(0.1, shape=[32]), name='conv1_biases')
conv1_conv2d = tf.nn.conv2d(X_images, conv1_Weights, strides=[1, 1, 1, 1], padding='SAME') + conv1_biases
conv1_activated = tf.nn.relu(conv1_conv2d)
conv1_pooled = tf.nn.max_pool(conv1_activated, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME')
#convolutional layer 2
conv2_Weights = tf.Variable(tf.truncated_normal([5, 5, 32, 64], stddev=0.1), name='conv2_Weights')
conv2_biases = tf.Variable(tf.constant(0.1, shape=[64]), name='conv2_biases')
conv2_conv2d = tf.nn.conv2d(conv1_pooled, conv2_Weights, strides=[1, 1, 1, 1], padding='SAME') + conv2_biases
conv2_activated = tf.nn.relu(conv2_conv2d)
conv2_pooled = tf.nn.max_pool(conv2_activated, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME')
#full connected layer 1
connect1_flat = tf.reshape(conv2_pooled, [-1, 7 * 7 * 64])
connect1_Weights = tf.Variable(tf.truncated_normal([7 * 7 * 64, 1024], stddev=0.1), name='connect1_Weights')
connect1_biases = tf.Variable(tf.constant(0.1, shape=[1024]), name='connect1_biases')
connect1_activated = tf.nn.relu(connect1_Wx_plus_b)
#full connected layer 2
connect2_Weights = tf.Variable(tf.truncated_normal([1024, 10], stddev=0.1), name='connect2_Weights')
connect2_biases = tf.Variable(tf.constant(0.1, shape=[10]), name='connect2_biases')
predict_y = tf.nn.softmax(connect2_Wx_plus_b)
#loss and train
loss = tf.reduce_mean(-tf.reduce_sum(y_holder * tf.log(predict_y), 1))
train = optimizer.minimize(loss)

session = tf.Session()
saver = tf.train.Saver()
saver.restore(session, 'save_model/mnist_cnn.ckpt')
correct_prediction = tf.equal(tf.argmax(predict_y, 1), tf.argmax(y_holder, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
train_images, train_labels = mnist.train.next_batch(5000)
test_images, test_labels = mnist.test.next_batch(5000)
train_accuracy = session.run(accuracy, feed_dict={X_holder:train_images, y_holder:train_labels})
test_accuracy = session.run(accuracy, feed_dict={X_holder:test_images, y_holder:test_labels})
print('train accuracy:%.4f test accuracy:%.4f' %(train_accuracy, test_accuracy))```

Extracting MNIST_data\train-images-idx3-ubyte.gz Extracting MNIST_data\train-labels-idx1-ubyte.gz Extracting MNIST_data\t10k-images-idx3-ubyte.gz Extracting MNIST_data\t10k-labels-idx1-ubyte.gz INFO:tensorflow:Restoring parameters from save_model/mnist_cnn.ckpt load model successful train accuracy:1.0000 test accuracy:0.9903

## 11.模型测试

```import math
import matplotlib.pyplot as plt
import numpy as np

def drawDigit2(position, image, title, isTrue):
plt.subplot(*position)
plt.imshow(image.reshape(-1, 28), cmap='gray_r')
plt.axis('off')
if not isTrue:
plt.title(title, color='red')
else:
plt.title(title)

def batchDraw2(batch_size):
images,labels = mnist.test.next_batch(batch_size)
predict_labels = session.run(predict_y, feed_dict={X_holder:images, y_holder:labels})
image_number = images.shape[0]
row_number = math.ceil(image_number ** 0.5)
column_number = row_number
plt.figure(figsize=(row_number+8, column_number+8))
for i in range(row_number):
for j in range(column_number):
index = i * column_number + j
if index < image_number:
position = (row_number, column_number, index+1)
image = images[index]
actual = np.argmax(labels[index])
predict = np.argmax(predict_labels[index])
isTrue = actual==predict
title = 'actual:%d\npredict:%d' %(actual,predict)
drawDigit2(position, image, title, isTrue)

batchDraw2(100)
plt.show()```

image.png

## 12.总结

1.这是本文作者写的第6篇关于tensorflow的文章，加深了对tensorflow框架的理解； 2.通过代码实践，本文作者掌握了卷积神经网络的构建，权重初始化，优化器选择等技巧； 3.tensorflow加载模型比sklearn加载模型稍有难度，保存模型时必须对变量命名，否则无法成功加载模型。

120 篇文章26 人订阅

0 条评论

## 相关文章

### Python学习，这有可能是最详细的PIL库基本概念文章了

PIL有如下几个模块：Image模块、ImageChops模块、ImageCrackCode模块、ImageDraw模块、ImageEnhance模块、Imag...

12730

850110

10210

27140

54560

66080

10810

17020

### MXNET学习笔记（一）：Module类（1）

Module 是 mxnet 提供给用户的一个高级封装的类。有了它，我们可以很容易的来训练模型。 Module 包含以下单元的一个 wraper symbol ...

31450

6.4K90