mnist_test.py
#encoding:utf-8
#####lesson25 输入手写数字图片输出识别结果
import tensorflow as tf
#MNIST数据集输出识别准确率
#MNIST数据集:
#提供6w张28*28像素点的0-9手写数字图片和标签,用于训练
#提供1w张28*28像素点的0-9手写数字图片和标签,用于测试
#每张图片的784个像素点(28*28=784)组成长度为784的一维数组。作为输入特征
#图片的标签以一维数组形式给出,每个元素表示对应分类出现的 概率。
#tf.get_clooection("") #从集合中取全部变量,生成一个列表
#tf.add_n([]) #列表内对应元素相加
#tf.cast(x,dtype) #把x转为dtype类型
#tf.argmax(x,axis) #返回最大值所在索引号 如:tf.argmax([1,0,0],1)返回0
#os.path,join("home","name")
#字符串.split()#按指定拆分符对字符串切片,返回分割后的列表
#如:‘./model/mnist_model-10001’.split('/')[-1].split('-')[-1] 返回1001
#with tf.Graph().as_default() as g:#其内定义的节点在计算图g中
#mnist_app.py
def pre_pic(picName):
img = Image.open
#前向传播:mnist_forward.py
INPUT_MODE = 784
OUTPUT_MODE = 10
LAYER1_MODE = 500
def get_weight(shape, regularizer):
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_MODE, LAYER1_MODE], regularizer)
b1 = get_bias([LAYER1_MODE])
y1 = tf.nn.relu(tf.matmul(x, w1) + b1)
w2 = get_weight([LAYER1_MODE,OUTPUT_MODE], regularizer)
b2 = get_bias([OUTPUT_MODE])
y = tf.matmul(y1, w2) + b2
return y
#反向传播: mnist_backward.py
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
import os
BATCH_SIZE = 200
LEARNING_RATE_BASE = 0.1
LEARNING_RATE_DECAY = 0.99
REGULARIZER = 0.0001
MOVING_AVERAGE_DECAY = 0.99
MODEL_SAVE_PATH = "./model/"
MODEL_NAME="mnist_model"
STEPS = 50000
def backward(mnist):
x = tf.placeholder(tf.float32, [None, INPUT_MODE])
y_ = tf.placeholder(tf.float32, [None,OUTPUT_MODE])
y = forward(x, REGULARIZER)
global_step = tf.Variable(0,trainable=False)
#损失函数loss含正则化regularization
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):
with tf.control_dependencies([train_step, ema_op]):
train_op = tf.no_op(name='train')
#实例化saver对象 保存模型
saver = tf.train.Saver()
with tf.Session() as sess:
init_op = tf.global_variables_initializer()
sess.run(init_op)
#价值ckpt模型
ckpt = tf.train.get_checkpoint_state(MODEL_SAVE_PATH)
#如果已有ckpt模型则恢复
if ckpt and ckpt.model_checkpoint_path:
#恢复会话
saver.restore(sess, ckpt.model_checkpoint_path)
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(step,loss_value,saver.save(sess,os.path.join(MODEL_SAVE_PATH,MODEL_NAME),global_step=global_step))
#测试输出准确率 : mnist_test.py
#def main():
# mnist = input_data.read_data_sets("./date", one_hot=True)
# backward(mnist)
#if __name__ == '__main__':
# main()
#mnist_test.py
import time
TEST_INTERVAL_SECS=5
def test(mnist):
with tf.Graph().as_default() as g:
x = tf.placeholder(tf.float32,[None,INPUT_MODE])
y_=tf.placeholder(tf.float32,[None,OUTPUT_MODE])
y = forward(x, None)
#实列化可还原滑动平均值的saver
ema = tf.train.ExponentialMovingAverage(MOVING_AVERAGE_DECAY)
ema_restore = ema.variables_to_restore()
saver = tf.train.Saver(ema_restore)
#准确率计算方法
correct_prediction = tf.equal(tf.argmax(y,1), tf.arg_max(y_,1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
while True:
with tf.Session() as sess:
#价值ckpt模型
ckpt = tf.train.get_checkpoint_state(MODEL_SAVE_PATH)
#如果已有ckpt模型则恢复
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(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()
mnist_app.py
import tensorflow as tf
#mnist_app.py
import numpy as np
#import image as Image
from PIL import Image
import mnist_test
BATCH_SIZE = 200
LEARNING_RATE_BASE = 0.1
LEARNING_RATE_DECAY = 0.99
REGULARIZER = 0.0001
MOVING_AVERAGE_DECAY = 0.99
MODEL_SAVE_PATH = "./model/"
MODEL_NAME="mnist_model"
STEPS = 50000
INPUT_MODE = 784
OUTPUT_MODE = 10
LAYER1_MODE = 500
def restore_model(testPicArr):
with tf.Graph().as_default() as tg:
x = tf.placeholder(tf.float32,[None,INPUT_MODE])
y = mnist_test.forward(x,None)
preValue = tf.argmax(y,1)
variable_averages = tf.train.ExponentialMovingAverage(MOVING_AVERAGE_DECAY)
variables_to_restore = variable_averages.variables_to_restore()
saver = tf.train.Saver(variables_to_restore)
with tf.Session() as sess:
ckpt = tf.train.get_checkpoint_state(MODEL_SAVE_PATH)
if ckpt and ckpt.model_checkpoint_path:
saver.restore(sess, ckpt.model_checkpoint_path)
preValue = sess.run(preValue, feed_dict={x:testPicArr})
return preValue
else:
print("No checkpoint file found")
return -10
def pre_pic(picName):
img = Image.open(picName)
reIm = img.resize((28,28),Image.ANTIALIAS)
im_arr = np.array(reIm.convert('L'))
threshold = 50
#for i in range(28):
# for j in range(28):
# im_arr[i][j] = 255 - im_arr[i][j]
# if(im_arr[i][j] < threshold):
# im_arr[i][j] = 0
# else:
# im_arr[i][j] = 255
nm_arr = im_arr.reshape([1, 784])
nm_arr = nm_arr.astype(np.float32)
img_ready = np.multiply(nm_arr, 1.0/255.0)
return img_ready
def application():
testNum = input("input the number of test pictures:")
for i in range(testNum):
testPic = raw_input("the path of test picture:")
testPicArr = pre_pic(testPic)
preValue = restore_model(testPicArr)
print( preValue)
def main():
application()
if __name__=='__main__':
main()