Tensorflow MINIST数据模型的训练,保存,恢复和手写字体识别

最近刚接触tensorflow,同样和广大网友一样采用MINIST数据来做手写识别,内容以注释的形式在代码里了

模型训练和保存

1.首先下载MINIST数据库(下载地址) ,四个文件下载后放到和你的python文件同一个目录下,不用解压,然后输入,其中e2.jpg在文末可下载

#coding=utf-8 
# 载入MINIST数据需要的库
from tensorflow.examples.tutorials.mnist import input_data
# 保存模型需要的库
from tensorflow.python.framework.graph_util import convert_variables_to_constants 
from tensorflow.python.framework import graph_util 
# 导入其他库
import tensorflow as tf
import cv2  
import numpy as np 
#获取MINIST数据
mnist = input_data.read_data_sets(".",one_hot = True)
# 创建会话 
sess = tf.InteractiveSession()
 
#占位符
x = tf.placeholder("float", shape=[None, 784], name="Mul")
y_ = tf.placeholder("float",shape=[None, 10],  name="y_")
#变量
W = tf.Variable(tf.zeros([784,10]),name='x')
b = tf.Variable(tf.zeros([10]),'y_')
 
#权重
def weight_variable(shape):
  initial = tf.truncated_normal(shape, stddev=0.1)
  return tf.Variable(initial)
#偏差
def bias_variable(shape):
  initial = tf.constant(0.1, shape=shape)
  return tf.Variable(initial)
#卷积
def conv2d(x, W):
  return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='SAME')
#最大池化
def max_pool_2x2(x):
  return tf.nn.max_pool(x, ksize=[1, 2, 2, 1],
                        strides=[1, 2, 2, 1], padding='SAME')
#相关变量的创建
W_conv1 = weight_variable([5, 5, 1, 32])
b_conv1 = bias_variable([32])
x_image = tf.reshape(x, [-1,28,28,1])
h_conv1 = tf.nn.relu(conv2d(x_image, W_conv1) + b_conv1)
h_pool1 = max_pool_2x2(h_conv1)
W_conv2 = weight_variable([5, 5, 32, 64])
b_conv2 = bias_variable([64])
#激活函数
h_conv2 = tf.nn.relu(conv2d(h_pool1, W_conv2) + b_conv2)
h_pool2 = max_pool_2x2(h_conv2)
W_fc1 = weight_variable([7 * 7 * 64, 1024])
b_fc1 = bias_variable([1024])
W_fc2 = weight_variable([1024, 10])
b_fc2 = bias_variable([10])
 
h_pool2_flat = tf.reshape(h_pool2, [-1, 7*7*64])
h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, W_fc1) + b_fc1)
keep_prob = tf.placeholder("float",name='rob')
h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob)
 
#用于训练用的softmax函数
y_conv=tf.nn.softmax(tf.matmul(h_fc1_drop, W_fc2) + b_fc2,name='res')
#用于训练作完后,作测试用的softmax函数
y_conv2=tf.nn.softmax(tf.matmul(h_fc1, W_fc2) + b_fc2,name="final_result")
 
#交叉熵的计算,返回包含了损失值的Tensor。
 
cross_entropy = -tf.reduce_sum(y_*tf.log(y_conv))
#优化器,负责最小化交叉熵
train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)
 
correct_prediction = tf.equal(tf.argmax(y_conv,1), tf.argmax(y_,1))
#计算准确率
accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))
#初始化所以变量
sess.run(tf.global_variables_initializer())
 
 # 保存输入输出,可以为之后用
tf.add_to_collection('res', y_conv)
tf.add_to_collection('output', y_conv2)
tf.add_to_collection('x', x)
 
#训练开始
for i in range(20000):
  batch = mnist.train.next_batch(50)
  if i%100 == 0:
    train_accuracy = accuracy.eval(feed_dict={
        x:batch[0], y_: batch[1], keep_prob: 1.0})
    print "step %d, training accuracy %g"%(i, train_accuracy)
#run()可以看做输入相关值给到函数中的占位符,然后计算的出结果,这里将batch[0],给xbatch[1]给y_
  train_step.run(feed_dict={x: batch[0], y_: batch[1], keep_prob: 0.5})
 
#将当前图设置为默认图
graph_def = tf.get_default_graph().as_graph_def() 
#将上面的变量转化成常量,保存模型为pb模型时需要,注意这里的final_result和前面的y_con2是同名,只有这样才会保存它,否则会报错,
# 如果需要保存其他tensor只需要让tensor的名字和这里保持一直即可
output_graph_def = tf.graph_util.convert_variables_to_constants(sess,  
                graph_def, ['final_result'])  
#保存前面训练后的模型为pb文件
with tf.gfile.GFile("grf.pb", 'wb') as f:  
        f.write(output_graph_def.SerializeToString())
 
#用saver 保存模型
saver = tf.train.Saver()   
saver.save(sess, "model_data/model")  
 
#导入图片,同时灰度化
im = cv2.imread('pic/e2.jpg',cv2.IMREAD_GRAYSCALE)
#反转图像,因为e2.jpg为白底黑字   
im =reversePic(im)
cv2.namedWindow("camera", cv2.WINDOW_NORMAL); 
cv2.imshow('camera',im)  
cv2.waitKey(0) 

#调整大小
im = cv2.resize(im,(28,28),interpolation=cv2.INTER_CUBIC)   
x_img = np.reshape(im , [-1 , 784])  


#输出图像矩阵
# print x_img 
 
#用上面导入的图片对模型进行测试
output = sess.run(y_conv2 , feed_dict={x:x_img })  
# print 'the y_con :   ', '\n',output  
print 'the predict is : ', np.argmax(output) 
print "test accracy %g"%accuracy.eval(feed_dict={
    x: mnist.test.images, y_: mnist.test.labels, keep_prob: 1.0})

输出:


image.png

没有CUDA加速,训练的会比较慢,但都可以训练,只是速度区别
1)其中用Saver保存模型的代码:

saver = tf.train.Saver()     
saver.save(sess, "model_data/model") 

最终会产生model_data文件夹,其中包含了:


image.png

2)保存模型为pb格式的代码:


#将当前图设置为默认图  
graph_def = tf.get_default_graph().as_graph_def()   
#将上面的变量转化成常量,保存模型为pb模型时需要,注意这里的final_result和前面的y_con2是同名,只有这样才会保存它,否则会报错,  
# 如果需要保存其他tensor只需要让tensor的名字和这里保持一直即可  
output_graph_def = tf.graph_util.convert_variables_to_constants(sess,    
                graph_def, ['final_result'])    
#保存前面训练后的模型为pb文件  
with tf.gfile.GFile("grf.pb", 'wb') as f:    
        f.write(output_graph_def.SerializeToString()) 

最终在当前目录生成grf.pb文件

模型的恢复:

1.用Saver保存的模型的恢复:


# -*- coding:utf-8 -*-    
import cv2  
import tensorflow as tf  
import numpy as np  
from sys import path  
#用于将自定义输入图片反转
def reversePic(src):
        # 图像反转  
    for i in range(src.shape[0]):
        for j in range(src.shape[1]):
            src[i,j] = 255 - src[i,j]
    return src 
          
def main():  
    sess = tf.InteractiveSession()  
#模型恢复
    saver=tf.train.import_meta_graph('model_data/model.meta')
 
    saver.restore(sess, 'model_data/model')
    graph = tf.get_default_graph()
    
    # 获取输入tensor,,获取输出tensor
    input_x = sess.graph.get_tensor_by_name("Mul:0")
    y_conv2 = sess.graph.get_tensor_by_name("final_result:0")

    # 也可以上面注释,通过下面获取输出输入tensor,
    # y_conv2 = tf.get_collection('output')[0]
    # # x= tf.get_collection('x')[0]
    # input_x = graph.get_operation_by_name('Mul').outputs[0]
    # keep_prob = graph.get_operation_by_name('rob').outputs[0]
    
    path="pic/e2.jpg"  
    im = cv2.imread(path,cv2.IMREAD_GRAYSCALE)
    #反转图像,因为e2.jpg为白底黑字   
    im =reversePic(im)
    cv2.namedWindow("camera", cv2.WINDOW_NORMAL); 
    cv2.imshow('camera',im)  
    cv2.waitKey(0)  
    # im=cv2.threshold(im, , 255, cv2.THRESH_BINARY_INV)[1];

    im = cv2.resize(im,(28,28),interpolation=cv2.INTER_CUBIC)  

    # im=cv2.threshold(im,200,255,cv2.THRESH_TRUNC)[1]
    # im=cv2.threshold(im,60,255,cv2.THRESH_TOZERO)[1]
 
    #数据从0~255转为-0.5~0.5  
    # img_gray = (im - (255 / 2.0)) / 255  
    x_img = np.reshape(im , [-1 , 784])  
    output = sess.run(y_conv2 , feed_dict={input_x:x_img})  
    print 'the predict is %d' % (np.argmax(output)) 
    #关闭会话  
    sess.close()  
  
if __name__ == '__main__':  
    main()  

2.pb模型的恢复:

#coding=utf-8 
from __future__ import absolute_import, unicode_literals
from tensorflow.examples.tutorials.mnist import input_data
from tensorflow.python.framework.graph_util import convert_variables_to_constants 
from tensorflow.python.framework import graph_util 
import cv2 
import numpy as np  
mnist = input_data.read_data_sets(".",one_hot = True)
import tensorflow as tf

#用于将自定义输入图片反转
def reversePic(src):
        # 图像反转  
    for i in range(src.shape[0]):
        for j in range(src.shape[1]):
            src[i,j] = 255 - src[i,j]
    return src 

with tf.Session() as persisted_sess:
  print("load graph")
  with tf.gfile.FastGFile("grf.pb",'rb') as f:
    graph_def = tf.GraphDef()
    graph_def.ParseFromString(f.read())
    persisted_sess.graph.as_default()
    tf.import_graph_def(graph_def, name='')
  # print("map variables")
  with tf.Session() as sess:

        # tf.initialize_all_variables().run()
        input_x = sess.graph.get_tensor_by_name("Mul:0")
        y_conv_2 = sess.graph.get_tensor_by_name("final_result:0")


        path="pic/e2.jpg"  
        im = cv2.imread(path,cv2.IMREAD_GRAYSCALE) 
        #反转图像,因为e2.jpg为白底黑字   
        im =reversePic(im)
        cv2.namedWindow("camera", cv2.WINDOW_NORMAL); 
        cv2.imshow('camera',im)  
        cv2.waitKey(0) 
        # im=cv2.threshold(im, , 255, cv2.THRESH_BINARY_INV)[1];
 
        im = cv2.resize(im,(28,28),interpolation=cv2.INTER_CUBIC)  
        # im =reversePic(im)
        # im=cv2.threshold(im,200,255,cv2.THRESH_TRUNC)[1]
        # im=cv2.threshold(im,60,255,cv2.THRESH_TOZERO)[1]

        # img_gray = (im - (255 / 2.0)) / 255  
        x_img = np.reshape(im , [-1 , 784])  
        output = sess.run(y_conv_2 , feed_dict={input_x:x_img})  
        print 'the predict is %d' % (np.argmax(output)) 
        #关闭会话  
        sess.close() 

其中e2.jpg:


image.png

两个模型恢复的输出结果都是:


image.png

注意:
用MINIST训练出来的模型。主要用来识别手写数字的,而且对输入的图片要求是近似黑底白字的,所以如果图片预处理不合适会导致识别率不高。
如果直接用官方的图片输 入,则识别完全没问题
附官方图片和e2.jpg的下载地址

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 158,736评论 4 362
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,167评论 1 291
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,442评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,902评论 0 204
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,302评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,573评论 1 216
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,847评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,562评论 0 197
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,260评论 1 241
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,531评论 2 245
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,021评论 1 258
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,367评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,016评论 3 235
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,068评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,827评论 0 194
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,610评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,514评论 2 269

推荐阅读更多精彩内容