深度学习之手写字符识别简易代码

1、读取mnist数据集并可视化和保存

img_data_producer_chekpoint.py
import numpy as np
from matplotlib import pyplot as plt
#matplotlib inline
from tensorflow.examples.tutorials.mnist import  input_data

mnist = input_data.read_data_sets('../input_data/',one_hot=True,reshape=False)
test_image = mnist.test.images
print(test_image.shape)

test_image_selected = test_image[:10,]
print(test_image_selected.shape)
for i in range(test_image_selected.shape[0]):
    img = test_image_selected[i,]
    img = np.reshape(img,(28,28))
    #print(img)
    plt.imsave("../test_images/test_image"+str(10+i)+".png",(img),cmap="gray")
    plt.imshow(img,cmap="gray")
    plt.show()

 结果:

C:\Anaconda3\python.exe F:/yanjiushengfile/pyharm/demo3/111/mnist_test/.ipynb_checkpoints/img_data_producer_chekpoint.py
Extracting ../input_data/train-images-idx3-ubyte.gz
Extracting ../input_data/train-labels-idx1-ubyte.gz
Extracting ../input_data/t10k-images-idx3-ubyte.gz
Extracting ../input_data/t10k-labels-idx1-ubyte.gz
(10000, 28, 28, 1)
(10, 28, 28, 1)

2、训练数据

minst_train_checkpoint.py
import tensorflow as tf
import  numpy as np
from matplotlib import pyplot as plt
from tensorflow.examples.tutorials.mnist import  input_data
from PIL import Image
import cv2 as cv
##load data (input_data/)
mnist = input_data.read_data_sets('../input_data/',one_hot=True, reshape=False)
X =tf.placeholder(tf.float32, [None, 28, 28, 1], name = "X") # input of the model
Y_ =tf.placeholder(tf.float32, [None, 10], name = "Y_") # output of the model

##Parameters initialization
K = 16
L = 32
M = 32

W1 = tf.Variable(tf.truncated_normal([5,5,1,K],stddev=0.1))
B1 = tf.Variable(tf.constant(0.1,tf.float32,[K]))

W2 = tf.Variable(tf.truncated_normal([5,5,K,L],stddev=0.1))
B2 = tf.Variable(tf.constant(0.1,tf.float32,[L]))

W3= tf.Variable(tf.truncated_normal([7*7*L,M],stddev=0.1))
B3 = tf.Variable(tf.constant(0.1,tf.float32,[M]))

W4= tf.Variable(tf.truncated_normal([M,10],stddev=0.1))
B4 = tf.Variable(tf.constant(0.1,tf.float32,[10]))

##Model structure

keep_prob = tf.placeholder(tf.float32, name="keep_prob") #dropout: keep prob

conv1 = tf.nn.relu(tf.nn.conv2d(X,W1,strides=[1,1,1,1],padding='SAME')+B1)
pool1 = tf.nn.max_pool(conv1,ksize=[1,2,2,1],strides=[1,2,2,1],padding='SAME')

conv2 = tf.nn.relu(tf.nn.conv2d(pool1,W2,strides=[1,1,1,1],padding='SAME')+B2)
pool2 = tf.nn.max_pool(conv2,ksize=[1,2,2,1],strides=[1,2,2,1],padding='SAME')

pool2_flat = tf.reshape(pool2,[-1,7*7*L])
fc1 = tf.nn.relu(tf.matmul(pool2_flat,W3)+B3)

fc1_drop = tf.nn.dropout(fc1,keep_prob)

Ylogits = tf.matmul(fc1_drop,W4)+B4
Y = tf.nn.softmax(Ylogits, name="Y")

##loss_function: cross_entropy
cross_entropy = tf.nn.softmax_cross_entropy_with_logits(logits=Ylogits,labels=Y_)
cross_entropy = tf.reduce_mean(cross_entropy)

##Accuracy
is_accuracy = tf.equal(tf.argmax(Y_,1),tf.arg_max(Y,1))
accuracy = tf.reduce_mean(tf.cast(is_accuracy,tf.float32))

##Model training
epoch = 5000
batch = 50

#some lists used for saving results
train_acc = []
train_loss = []
test_acc = []
test_loss = []

lr = tf.placeholder(tf.float32)#learning rate (variable)
optimizer = tf.train.AdamOptimizer(lr) #optimal method

train_step = optimizer.minimize(cross_entropy)
saver = tf.train.Saver()
init = tf.global_variables_initializer() #initialize all variales

with tf.Session() as sess:
    sess.run(init) #run initialization process
    for i in range(epoch+1):
        #learning rate
        max_lr = 0.003
        min_lr = 0.0001
        decay_speed = 2000.0
        learning_rate = min_lr+(max_lr-min_lr)*np.math.exp(-i/decay_speed)

        #training step
        batch_X,batch_Y =mnist.train.next_batch(batch) #load one batch training data
        train_data = {X:batch_X,Y_:batch_Y,lr:learning_rate,keep_prob:0.5} #dictionary
        sess.run(train_step,feed_dict=train_data) #train one step
        aa=sess.run(fc1, feed_dict=train_data)  # train one step
        #image=Image.
        cv.imshow('1',np.array(aa))
        cv.waitKey(0)
        #save results
        acc,loss = sess.run([accuracy,cross_entropy],feed_dict=train_data)
        test_acc.append(acc)
        test_loss.append(loss)

        #print training process
        if i%10 == 0:
            print("epoch =%d," %i,"test accuracy= %.4f," %test_acc[i],\
                  "test_loss = %.6f" %test_loss[i], "learning rate =%.6f" %learning_rate)
        if i == epoch-1:
            print(1)
            saver.save(sess,'../test_model/test_model.ckpt')
    print("test accuracy = %.4f," %test_acc[-1],"test_loss = %.6f" %test_loss[-1])

3、测试数据

mnist_test_checkpoint.py

import tensorflow as tf
import  numpy as np
from matplotlib import pyplot as plt
import cv2 as cv
import cv2
def AutoAdpThreor(image):
    img1 = cv2.imread('./Image/letter.png', cv2.IMREAD_GRAYSCALE)
    img1 = cv2.resize(img1, (300, 300), interpolation=cv2.INTER_AREA)
    cv2.imshow('img1', img1)
    res1 = cv2.adaptiveThreshold(img1, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 25, 5)
    res2 = cv2.adaptiveThreshold(img1, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 25, 5)
    cv2.imshow('res1', res1)
    cv2.imshow('res2', res2)

# def threshold_demo(image):
#     gray = cv.cvtColor(image,cv.COLOR_RGB2GRAY)   #要二值化图像,要先进行灰度化处理
#     ret, binary = cv.threshold(gray,0,255,cv.THRESH_BINARY | cv.THRESH_OTSU)
#     #return binary
#     #print("threshold value: %s"%ret)#打印阈值,前面先进行了灰度处理0-255,我们使用该阈值进行处理,低于该阈值的图像部分全为黑,高于该阈值则为白色
#     cv.imshow("binary",binary)#显示二值化图像
test_image = cv.imread("../test_images/index.jpg",0)  # test_image0.png
# print(test_image,test_image.shape)
# cv.imshow("binary",test_image)#显示二值化图像
# cv.waitKey()
# #test_image=cv2.adaptiveThreshold(test_image, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 25, 5)
# gray = cv.cvtColor(test_image,cv.COLOR_RGB2GRAY)   #要二值化图像,要先进行灰度化处理
# #test_image = cv.threshold(gray,0,255,cv.THRESH_BINARY | cv.THRESH_OTSU)
# print(gray,gray.shape)
# cv.imshow("binary",gray)#显示二值化图像
# cv.waitKey()
# test_image = np.array(test_image,dtype='float32')
# #print(test_image.shape[0:2])
# cv.imshow("binary",test_image)#显示二值化图像
# #test_image=threshold_demo(test_image)
# #图像预处理
# cv.waitKey()
# size = (test_image.shape[0], test_image.shape[1])
# test_image = np.array(test_image,dtype='float32')
# #iTmp = cv.CreateImage(size,test_image.depth,test_image.nChannels)
# # for i in range(test_image.shape[0]):
# #   for j in range(test_image.shape[1]):
# #     test_image[i,j] = 255 - test_image[i,j]
# test_image = np.array(test_image,dtype='float32')
# print(test_image)
# #test_image = np.reshape(test_image,(28,28))
# print(test_image.shape)#(28,28)
# plt.imshow(test_image, cmap="gray")
# plt.show()
video_capture=cv.VideoCapture(0)

while True:
    with tf.Session() as sess:
        saver = tf.train.import_meta_graph("./../test_model/model.ckpt-9001.meta")
        saver.restore(sess,tf.train.latest_checkpoint("./../test_model/"))
        graph = tf.get_default_graph()

        X = graph.get_tensor_by_name("x:0")
       # keep_prob = graph.get_tensor_by_name("keep_prob:0")
        Y = graph.get_tensor_by_name("y_:0")
        ret,frame=video_capture.read()
        frame=cv.cvtColor(frame,cv2.COLOR_RGB2GRAY)
        #frame1=cv2.adaptiveThreshold(frame,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,cv2.THRESH_BINARY,5,10)
        for i in range(frame.shape[0]):
          for j in range(frame.shape[1]):
            frame[i,j] = 255 - frame[i,j]
        #frame = np.array(frame,dtype='float32')
        #cv2.bitwise_not(frame1,frame)
        print(frame)
        cv.imshow("binary1", frame)  # 显示二值化图像
        frame=cv.resize(frame,(28,28))
        cv.imshow("binary",frame)#显示二值化图像
        cv.waitKey(30)
        if ret:
            test_image = np.reshape(frame, [1, 28, 28, 1])
            #feed_dict = {X: test_image, keep_prob: 1}
            feed_dict = {X: test_image}
            print(sess.run(Y,feed_dict=feed_dict))
        #tf.argmax(vector, 1):返回的是vector中的最大值的索引号,
        # 如果vector是一个向量,那就返回一个值,
        # 如果是一个矩阵,那就返回一个向量,
        # 这个向量的每一个维度都是相对应矩阵行的最大值元素的索引号
            test_out = tf.argmax(Y, 1)
            print("test out ={}",sess.run(test_out,feed_dict=feed_dict))


猜你喜欢

转载自blog.csdn.net/l641208111/article/details/105853895