defbackward(mnist): #x,y_占位 x = tf.placeholder(tf.float32,[ BATCH_SIZE, mnist_lenet5_forward.IMAGE_SIZE, mnist_lenet5_forward.IMAGE_SIZE, mnist_lenet5_forward.NUM_CHANNELS]) y_ = tf.placeholder(tf.float32, [None, mnist_lenet5_forward.OUTPUT_NODE]) #前向传播 y = mnist_lenet5_forward.forward(x,True, REGULARIZER) #声明一个全局计数器,并输出化为0 global_step = tf.Variable(0, trainable=False) #先是对网络最后一层的输出y做softmax,再将此向量和实际标签值做交叉熵 ce = tf.nn.sparse_softmax_cross_entropy_with_logits(logits=y, labels=tf.argmax(y_, 1)) #再对得到的向量求均值就得到 loss cem = tf.reduce_mean(ce) #添加正则化中的 losses 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) #传入学习率,构造一个实现梯度下降算法的优化器,再通过使用minimize更新存储要训练的变量的列表来减小loss train_step = tf.train.GradientDescentOptimizer(learning_rate).minimize(loss, global_step=global_step) #实现滑动平均模型,参数MOVING_AVERAGE_DECAY用于控制模型更新的速度 ema = tf.train.ExponentialMovingAverage(MOVING_AVERAGE_DECAY, global_step) ema_op = ema.apply(tf.trainable_variables()) #将train_step和ema_op两个训练操作绑定到train_op with tf.control_dependencies([train_step, ema_op]): train_op = tf.no_op(name='train') #实例化一个保存和恢复变量的saver saver = tf.train.Saver()
#创建一个会话,并通过python中的上下文管理器来管理这个会话 with tf.Session() as sess: init_op = tf.global_variables_initializer() sess.run(init_op)
# 通过checkpoint文件定位到最新保存的模型 ckpt = tf.train.get_checkpoint_state(MODEL_SAVE_PATH) if ckpt and ckpt.model_checkpoint_path: saver.restore(sess, ckpt.model_checkpoint_path)
for i inrange(STEPS): #读取一个batch的数据 xs, ys = mnist.train.next_batch(BATCH_SIZE) #将输入数据xs转换成与网络输入相同形状的矩阵 reshaped_xs = np.reshape(xs,( BATCH_SIZE, mnist_lenet5_forward.IMAGE_SIZE, mnist_lenet5_forward.IMAGE_SIZE, mnist_lenet5_forward.NUM_CHANNELS)) #喂入训练图像和标签,开始训练 _, loss_value, step = sess.run([train_op, loss, global_step], feed_dict={x: reshaped_xs, y_: ys}) if i % 100 == 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)
运行结果
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
RESTART: G:\...\lenet5\mnist_lenet5_backward.py Extracting ./data/train-images-idx3-ubyte.gz Extracting ./data/train-labels-idx1-ubyte.gz Extracting ./data/t10k-images-idx3-ubyte.gz Extracting ./data/t10k-labels-idx1-ubyte.gz After 19312 training step(s), loss on training batch is 0.650531. After 19412 training step(s), loss on training batch is 0.699633. After 19512 training step(s), loss on training batch is 0.686086. After 19612 training step(s), loss on training batch is 0.725393. After 19712 training step(s), loss on training batch is 0.788735. After 19812 training step(s), loss on training batch is 0.697031. After 19912 training step(s), loss on training batch is 0.712534. After 20012 training step(s), loss on training batch is 0.746723. After 20112 training step(s), loss on training batch is 0.776782. After 20212 training step(s), loss on training batch is 0.791459. After 20312 training step(s), loss on training batch is 0.731853. After 20412 training step(s), loss on training batch is 0.666092.
import time import tensorflow as tf from tensorflow.examples.tutorials.mnist import input_data import mnist_lenet5_forward import mnist_lenet5_backward import numpy as np
deftest(mnist): with tf.Graph().as_default() as g: x = tf.placeholder(tf.float32,[ BATCH_SIZE,#mnist.test.num_examples, mnist_lenet5_forward.IMAGE_SIZE, mnist_lenet5_forward.IMAGE_SIZE, mnist_lenet5_forward.NUM_CHANNELS]) y_ = tf.placeholder(tf.float32, [None, mnist_lenet5_forward.OUTPUT_NODE]) y = mnist_lenet5_forward.forward(x,False,None)
ema = tf.train.ExponentialMovingAverage(mnist_lenet5_backward.MOVING_AVERAGE_DECAY) ema_restore = ema.variables_to_restore() saver = tf.train.Saver(ema_restore)
whileTrue: with tf.Session() as sess: ckpt = tf.train.get_checkpoint_state(mnist_lenet5_backward.MODEL_SAVE_PATH) if ckpt and ckpt.model_checkpoint_path: saver.restore(sess, ckpt.model_checkpoint_path)
RESTART: G:\TestProject\python\tensorflow\peking_caojian\7CNNbase\lenet5\mnist_lenet5_test2.py Extracting ./data/train-images-idx3-ubyte.gz Extracting ./data/train-labels-idx1-ubyte.gz Extracting ./data/t10k-images-idx3-ubyte.gz Extracting ./data/t10k-labels-idx1-ubyte.gz After 21512 training step(s), test accuracy = 0.9842 After 21512 training step(s), test accuracy = 0.9802
import tensorflow as tf import numpy as np from PIL import Image import mnist_lenet5_backward as mnist_backward import mnist_lenet5_forward as mnist_forward
import os filedir = os.getcwd()+ '\\pic' m = 0 n = 0
defrestore_model(testPicArr): with tf.Graph().as_default() as tg: x = tf.placeholder(tf.float32,[ 1, mnist_forward.IMAGE_SIZE, mnist_forward.IMAGE_SIZE, mnist_forward.NUM_CHANNELS]) y = mnist_forward.forward(x,False,None) preValue = tf.argmax(y, 1)
''' def application(): testNum = input("input the number of test pictures:") for i in range(int(testNum)): testPic = input("the path of test picture:") testPicArr = pre_pic(testPic) preValue = restore_model(testPicArr) print ("The prediction number is:", preValue) '''
defapplication(): global m,n for root, dirs, files in os.walk(filedir): for file in files: if os.path.splitext(file)[1] == '.png': n = n+1 imagename = os.path.splitext(file)[0]+'.png' testPic = os.path.join(root, file) testPicArr = pre_pic(testPic) preValue = restore_model(testPicArr) print ("The %d image name is %s:" % (n,imagename)) print ("The prediction number is:", preValue) ifint(imagename[0])== preValue: m = m+1 print("TRUE") else: print("FALSE!!!!!!!!!!!!!!!!!!!!") print("m = %d,n = %d" % (m,n)) print("test accuracy = %d%%" % (m/n*100)) defmain(): application()
RESTART: G:\...\lenet5\mnist_app.py The 1 image name is 0.png: The prediction number is: [0] TRUE The 2 image name is 1.png: The prediction number is: [1] TRUE The 3 image name is 2.png: The prediction number is: [2] TRUE The 4 image name is 3.png: The prediction number is: [3] TRUE The 5 image name is 4.png: The prediction number is: [4] TRUE The 6 image name is 5.png: The prediction number is: [5] TRUE The 7 image name is 6.png: The prediction number is: [6] TRUE The 8 image name is 7.png: The prediction number is: [7] TRUE The 9 image name is 8.png: The prediction number is: [8] TRUE The 10 image name is 9.png: The prediction number is: [9] TRUE m = 10,n = 10 test accuracy = 100%