识别手写数字实战

本次实战使用的数据集是mnist。tensorflow提供了一个库,可以直接用在下载MNIST,见下面代码。

1
2
from tensorflow.examples.tutorials.mnist import input_data
mnist=input_data.read_data_sets("./mnist_dataset/",one_hot=True)

运行上面的代码,会自动下载数据集并将文件解压到当前代码所在的同级目录下。one_hot=True表示将样本标签转化为one-hot编码

1
2
3
4
5
6
7
#返回各子集样本数
print("train data size",mnist.train.num_examples)
#train data size 55000
print("validation data size",mnist.validation.num_examples)
#validation data size 5000
print("test data size",mnist.test.num_example)
#test data size 10000
1
2
3
4
#返回标签,第1张图片的one-hot编码
mnist.train.labels[0]
#返回数据,第1张图片的784个像素点
mnist.train.images[0]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
def weight_variables(shape):
w=tf.Variable(tf.random_normal(shape=shape,mean=1.0,stddev=1.0))
return w
def bias_variables(shape):
b=tf.Variable(tf.constant(0.0,shape=shape))
return b
def model():
x=tf.placeholder(tf.float32,[None,784])
y_true=tf.placeholder(tf.int32,[None,10])
#随机初始化权重,第一层卷积:5*5*1,32个,strides=1
w_conv1=weight_variables([5,5,1,32])
b_conv1=bias_variables([32])
#将图片大小转为对应成4D输入 [None,784]----->[None,28,28,1]
x_reshape=tf.reshape(x,[-1,28,28,1])
#先卷积后激活函数,卷积:[None,28,28,1]----->[None,28,28,32]
x_relu1=tf.nn.relu(tf.nn.conv2d(x_reshape,w_conv1,strides=[1,1,1,1],padding="SAME")+b_conv1)
#池化 2*2 strides=2 [None,28,28,32]----->[None,14,14,32]
x_pool1=tf.nn.max_pool(x_relu1,ksize=[1,2,2,1],strides=[1,2,2,1],padding="SAME")
#第二个卷积层:[5,5,32,64] 偏置 64
w_conv2=weight_variables([5,5,32,64])
b_conv2=bias_variables([64])
#卷积 [None,14,14,32]----->[None,14,14,64]
x_relu2 = tf.nn.relu(tf.nn.conv2d(x_pool1, w_conv2, strides=[1, 1, 1, 1], padding="SAME") + b_conv2)
#池化 2*2 stride2 [None,14,14,64]------>[None,7,7,64]
x_pool2=tf.nn.max_pool(x_relu2,ksize=[1,2,2,1],strides=[1,2,2,1],padding="SAME")
#全连接层
w_fc=weight_variables([7*7*64,10])
b_fc=bias_variables([10])
x_fc_reshape=tf.reshape(x_pool2,[-1,7*7*64])
#矩阵运算
y_predict=tf.matmul(x_fc_reshape,w_fc)+b_fc
return x,y_true,y_predict
def conv_fc():
mnist=input_data.read_data_sets("./mnist_dataset/",one_hot=True)
x,y_true,y_predict=model()
# 求平均交叉熵损失
loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y_true, logits=y_predict))
# 梯度下降求出损失
train_op = tf.train.GradientDescentOptimizer(0.0001).minimize(loss)
# 计算准确率
equal_list = tf.equal(tf.argmax(y_true, 1), tf.argmax(y_predict, 1))
#cast转换数据类型
accuracy = tf.reduce_mean(tf.cast(equal_list, tf.float32))
init_op = tf.global_variables_initializer()
with tf.Session() as sess:
sess.run(init_op)
for i in range(1000):
mnist_x,mnist_y=mnist.train.next_batch(50)
sess.run(train_op,feed_dict={x:mnist_x,y_true:mnist_y})
print("训练第%d步,准确率为:%f" % (i,sess.run(accuracy,feed_dict={x:mnist_x,y_true:mnist_y})))
if __name__=="__main__":
conv_fc()

上面的代码训练效果实在惨不忍睹,训练了半天还是连20%都没过。所以,又去抄了大佬的代码学习,他分成三个文件:mnist_forward、mnist_backward、mnist_test。

mnist_forward

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import tensorflow as tf
#输入节点
INPUT_NODE = 784
#输出十个数,每个数代表每个数字的概率
OUTPUT_NODE = 10
#隐藏节点个数
LAYER1_NODE = 500

def get_weight(shape, regularizer):
#随机生成w
w = tf.Variable(tf.truncated_normal(shape,stddev=0.1))
#正则化
if regularizer != None: tf.add_to_collection('losses', tf.contrib.layers.l2_regularizer(regularizer)(w))
return w

def get_bias(shape):
b = tf.Variable(tf.zeros(shape))
return b

def forward(x, regularizer):
w1 = get_weight([INPUT_NODE, LAYER1_NODE], regularizer)
b1 = get_bias([LAYER1_NODE])
y1 = tf.nn.relu(tf.matmul(x, w1) + b1)

w2 = get_weight([LAYER1_NODE, OUTPUT_NODE], regularizer)
b2 = get_bias([OUTPUT_NODE])
y = tf.matmul(y1, w2) + b2
return y

mnist_backward

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
import mnist_forward
import os
#每轮训练200张图片
BATCH_SIZE = 200
#初始学习率
LEARNING_RATE_BASE = 0.1
#学习率衰减率
LEARNING_RATE_DECAY = 0.99
#正则化系数
REGULARIZER = 0.0001
#共训练50000
STEPS = 50000
#滑动平均衰减率
MOVING_AVERAGE_DECAY = 0.99
#模型存放的路径
MODEL_SAVE_PATH="./model/"
#模型保存文件名
MODEL_NAME="mnist_model"

def backward(mnist):

x = tf.placeholder(tf.float32, [None, mnist_forward.INPUT_NODE])
y_ = tf.placeholder(tf.float32, [None, mnist_forward.OUTPUT_NODE])
y = mnist_forward.forward(x, REGULARIZER)
#设定global_step初值为0,设定为不可训练
global_step = tf.Variable(0, trainable=False)
ce = tf.nn.sparse_softmax_cross_entropy_with_logits(logits=y, labels=tf.argmax(y_, 1))
cem = tf.reduce_mean(ce)
#加上正则化
loss = cem + tf.add_n(tf.get_collection('losses'))
#学习率衰减
learning_rate = tf.train.exponential_decay(
LEARNING_RATE_BASE,
global_step,
mnist.train.num_examples / BATCH_SIZE,
LEARNING_RATE_DECAY,
staircase=True)
#梯度下降
train_step = tf.train.GradientDescentOptimizer(learning_rate).minimize(loss, global_step=global_step)
#滑动平均
ema = tf.train.ExponentialMovingAverage(MOVING_AVERAGE_DECAY, global_step)
ema_op = ema.apply(tf.trainable_variables())
with tf.control_dependencies([train_step, ema_op]):
train_op = tf.no_op(name='train')

saver = tf.train.Saver()

with tf.Session() as sess:
init_op = tf.global_variables_initializer()
sess.run(init_op)

for i in range(STEPS):
xs, ys = mnist.train.next_batch(BATCH_SIZE)
_, loss_value, step = sess.run([train_op, loss, global_step], feed_dict={x: xs, y_: ys})
if i % 1000 == 0:
print("After %d training step(s), loss on training batch is %g." % (step, loss_value))
saver.save(sess, os.path.join(MODEL_SAVE_PATH, MODEL_NAME), global_step=global_step)


def main():
mnist = input_data.read_data_sets("./data/", one_hot=True)
backward(mnist)

if __name__ == '__main__':
main()

mnist_test

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#coding:utf-8
import time
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
import mnist_forward
import mnist_backward
TEST_INTERVAL_SECS = 5

def test(mnist):
with tf.Graph().as_default() as g:
x = tf.placeholder(tf.float32, [None, mnist_forward.INPUT_NODE])
y_ = tf.placeholder(tf.float32, [None, mnist_forward.OUTPUT_NODE])
y = mnist_forward.forward(x, None)

ema = tf.train.ExponentialMovingAverage(mnist_backward.MOVING_AVERAGE_DECAY)
ema_restore = ema.variables_to_restore()
saver = tf.train.Saver(ema_restore)

correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))

while True:
with tf.Session() as sess:
#保存节点状态
#26、27行代码用于访问到最新保存的节点文件
ckpt = tf.train.get_checkpoint_state(mnist_backward.MODEL_SAVE_PATH)
if ckpt and ckpt.model_checkpoint_path:
saver.restore(sess, ckpt.model_checkpoint_path)
global_step = ckpt.model_checkpoint_path.split('/')[-1].split('-')[-1]
accuracy_score = sess.run(accuracy, feed_dict={x: mnist.test.images, y_: mnist.test.labels})
print("After %s training step(s), test accuracy = %g" % (global_step, accuracy_score))
else:
print('No checkpoint file found')
return
time.sleep(TEST_INTERVAL_SECS)

def main():
mnist = input_data.read_data_sets("./data/", one_hot=True)
test(mnist)

if __name__ == '__main__':
main()
----本文结束,感谢您的阅读。如有错,请指正。----
大哥大嫂过年好!支持我一下呗
0%