溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Python(TensorFlow框架)實(shí)現(xiàn)手寫數(shù)字識別系統(tǒng)的方法

發(fā)布時間:2020-09-04 16:03:24 來源:腳本之家 閱讀:371 作者:louishao 欄目:開發(fā)技術(shù)

手寫數(shù)字識別算法的設(shè)計與實(shí)現(xiàn)

本文使用python基于TensorFlow設(shè)計手寫數(shù)字識別算法,并編程實(shí)現(xiàn)GUI界面,構(gòu)建手寫數(shù)字識別系統(tǒng)。這是本人的本科畢業(yè)論文課題,當(dāng)然,這個也是機(jī)器學(xué)習(xí)的基本問題。本博文不會以論文的形式展現(xiàn),而是以編程實(shí)戰(zhàn)完成機(jī)器學(xué)習(xí)項目的角度去描述。

項目要求:本文主要解決的問題是手寫數(shù)字識別,最終要完成一個識別系統(tǒng)。

設(shè)計識別率高的算法,實(shí)現(xiàn)快速識別的系統(tǒng)。

1 LeNet-5模型的介紹

本文實(shí)現(xiàn)手寫數(shù)字識別,使用的是卷積神經(jīng)網(wǎng)絡(luò),建模思想來自LeNet-5,如下圖所示:

Python(TensorFlow框架)實(shí)現(xiàn)手寫數(shù)字識別系統(tǒng)的方法

這是原始的應(yīng)用于手寫數(shù)字識別的網(wǎng)絡(luò),我認(rèn)為這也是最簡單的深度網(wǎng)絡(luò)。

LeNet-5不包括輸入,一共7層,較低層由卷積層和最大池化層交替構(gòu)成,更高層則是全連接和高斯連接。

LeNet-5的輸入與BP神經(jīng)網(wǎng)路的不一樣。這里假設(shè)圖像是黑白的,那么LeNet-5的輸入是一個32*32的二維矩陣。同時,輸入與下一層并不是全連接的,而是進(jìn)行稀疏連接。本層每個神經(jīng)元的輸入來自于前一層神經(jīng)元的局部區(qū)域(5×5),卷積核對原始圖像卷積的結(jié)果加上相應(yīng)的閾值,得出的結(jié)果再經(jīng)過激活函數(shù)處理,輸出即形成卷積層(C層)。卷積層中的每個特征映射都各自共享權(quán)重和閾值,這樣能大大減少訓(xùn)練開銷。降采樣層(S層)為減少數(shù)據(jù)量同時保存有用信息,進(jìn)行亞抽樣。

第一個卷積層(C1層)由6個特征映射構(gòu)成,每個特征映射是一個28×28的神經(jīng)元陣列,其中每個神經(jīng)元負(fù)責(zé)從5×5的區(qū)域通過卷積濾波器提取局部特征。一般情況下,濾波器數(shù)量越多,就會得出越多的特征映射,反映越多的原始圖像的特征。本層訓(xùn)練參數(shù)共6×(5×5+1)=156個,每個像素點(diǎn)都是由上層5×5=25個像素點(diǎn)和1個閾值連接計算所得,共28×28×156=122304個連接。

S2層是對應(yīng)上述6個特征映射的降采樣層(pooling層)。pooling層的實(shí)現(xiàn)方法有兩種,分別是max-pooling和mean-pooling,LeNet-5采用的是mean-pooling,即取n×n區(qū)域內(nèi)像素的均值。C1通過2×2的窗口區(qū)域像素求均值再加上本層的閾值,然后經(jīng)過激活函數(shù)的處理,得到S2層。pooling的實(shí)現(xiàn),在保存圖片信息的基礎(chǔ)上,減少了權(quán)重參數(shù),降低了計算成本,還能控制過擬合。本層學(xué)習(xí)參數(shù)共有1*6+6=12個,S2中的每個像素都與C1層中的2×2個像素和1個閾值相連,共6×(2×2+1)×14×14=5880個連接。

S2層和C3層的連接比較復(fù)雜。C3卷積層是由16個大小為10×10的特征映射組成的,當(dāng)中的每個特征映射與S2層的若干個特征映射的局部感受野(大小為5×5)相連。其中,前6個特征映射與S2層連續(xù)3個特征映射相連,后面接著的6個映射與S2層的連續(xù)的4個特征映射相連,然后的3個特征映射與S2層不連續(xù)的4個特征映射相連,最后一個映射與S2層的所有特征映射相連。此處卷積核大小為5×5,所以學(xué)習(xí)參數(shù)共有6×(3×5×5+1)+9×(4×5×5+1)+1×(6×5×5+1)=1516個參數(shù)。而圖像大小為28×28,因此共有151600個連接。

S4層是對C3層進(jìn)行的降采樣,與S2同理,學(xué)習(xí)參數(shù)有16×1+16=32個,同時共有16×(2×2+1)×5×5=2000個連接。

C5層是由120個大小為1×1的特征映射組成的卷積層,而且S4層與C5層是全連接的,因此學(xué)習(xí)參數(shù)總個數(shù)為120×(16×25+1)=48120個。

F6是與C5全連接的84個神經(jīng)元,所以共有84×(120+1)=10164個學(xué)習(xí)參數(shù)。

卷積神經(jīng)網(wǎng)絡(luò)通過通過稀疏連接和共享權(quán)重和閾值,大大減少了計算的開銷,同時,pooling的實(shí)現(xiàn),一定程度上減少了過擬合問題的出現(xiàn),非常適合用于圖像的處理和識別。

2 手寫數(shù)字識別算法模型的構(gòu)建

2.1 各層設(shè)計

有了第一節(jié)的基礎(chǔ)知識,在這基礎(chǔ)上,進(jìn)行完善和改進(jìn)。

輸入層設(shè)計

輸入為28×28的矩陣,而不是向量。

激活函數(shù)的選取

Sigmoid函數(shù)具有光滑性、魯棒性和其導(dǎo)數(shù)可用自身表示的優(yōu)點(diǎn),但其運(yùn)算涉及指數(shù)運(yùn)算,反向傳播求誤差梯度時,求導(dǎo)又涉及乘除運(yùn)算,計算量相對較大。同時,針對本文構(gòu)建的含有兩層卷積層和降采樣層,由于sgmoid函數(shù)自身的特性,在反向傳播時,很容易出現(xiàn)梯度消失的情況,從而難以完成網(wǎng)絡(luò)的訓(xùn)練。因此,本文設(shè)計的網(wǎng)絡(luò)使用ReLU函數(shù)作為激活函數(shù)。

ReLU的表達(dá)式:Python(TensorFlow框架)實(shí)現(xiàn)手寫數(shù)字識別系統(tǒng)的方法

卷積層設(shè)計

本文設(shè)計卷積神經(jīng)網(wǎng)絡(luò)采取的是離散卷積,卷積步長為1,即水平和垂直方向每次運(yùn)算完,移動一個像素。卷積核大小為5×5。

降采樣層

本文降采樣層的pooling方式是max-pooling,大小為2×2。

輸出層設(shè)計

輸出層設(shè)置為10個神經(jīng)網(wǎng)絡(luò)節(jié)點(diǎn)。數(shù)字0~9的目標(biāo)向量如下表所示:

Python(TensorFlow框架)實(shí)現(xiàn)手寫數(shù)字識別系統(tǒng)的方法

2.2 網(wǎng)絡(luò)模型的總體結(jié)構(gòu)

Python(TensorFlow框架)實(shí)現(xiàn)手寫數(shù)字識別系統(tǒng)的方法

其實(shí),本文網(wǎng)絡(luò)的構(gòu)建,參考自TensorFlow的手寫數(shù)字識別的官方教程的,讀者有興趣也可以詳細(xì)閱讀。

2.3 編程實(shí)現(xiàn)算法

本文使用Python,調(diào)用TensorFlow的api完成手寫數(shù)字識別的算法。

注:本文程序運(yùn)行環(huán)境是:Win10,python3.5.2。當(dāng)然,也可以在Linux下運(yùn)行,由于TensorFlow對py2和py3兼容得比較好,在Linux下可以在python2.7中運(yùn)行。

#!/usr/bin/env python2
# -*- coding: utf-8 -*-
"""
Created on Fri Feb 17 19:50:49 2017

@author: Yonghao Huang
"""

#import modules
import numpy as np
import matplotlib.pyplot as plt
#from sklearn.metrics import confusion_matrix
import tensorflow as tf
import time
from datetime import timedelta
import math
from tensorflow.examples.tutorials.mnist import input_data


def new_weights(shape):
  return tf.Variable(tf.truncated_normal(shape,stddev=0.05))
def new_biases(length):
  return tf.Variable(tf.constant(0.1,shape=length))
def conv2d(x,W):
  return tf.nn.conv2d(x,W,strides=[1,1,1,1],padding='SAME')
def max_pool_2x2(inputx):
  return tf.nn.max_pool(inputx,ksize=[1,2,2,1],strides=[1,2,2,1],padding='SAME')

#import data
data = input_data.read_data_sets("./data", one_hot=True) # one_hot means [0 0 1 0 0 0 0 0 0 0] stands for 2

print("Size of:")
print("--Training-set:\t\t{}".format(len(data.train.labels)))
print("--Testing-set:\t\t{}".format(len(data.test.labels)))
print("--Validation-set:\t\t{}".format(len(data.validation.labels)))
data.test.cls = np.argmax(data.test.labels,axis=1)  # show the real test labels: [7 2 1 ..., 4 5 6], 10000values

x = tf.placeholder("float",shape=[None,784],name='x')
x_image = tf.reshape(x,[-1,28,28,1])

y_true = tf.placeholder("float",shape=[None,10],name='y_true')
y_true_cls = tf.argmax(y_true,dimension=1)
# Conv 1
layer_conv1 = {"weights":new_weights([5,5,1,32]),
        "biases":new_biases([32])}
h_conv1 = tf.nn.relu(conv2d(x_image,layer_conv1["weights"])+layer_conv1["biases"])
h_pool1 = max_pool_2x2(h_conv1)
# Conv 2
layer_conv2 = {"weights":new_weights([5,5,32,64]),
        "biases":new_biases([64])}
h_conv2 = tf.nn.relu(conv2d(h_pool1,layer_conv2["weights"])+layer_conv2["biases"])
h_pool2 = max_pool_2x2(h_conv2)
# Full-connected layer 1
fc1_layer = {"weights":new_weights([7*7*64,1024]),
      "biases":new_biases([1024])}
h_pool2_flat = tf.reshape(h_pool2,[-1,7*7*64])
h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat,fc1_layer["weights"])+fc1_layer["biases"])
# Droupout Layer
keep_prob = tf.placeholder("float")
h_fc1_drop = tf.nn.dropout(h_fc1,keep_prob)
# Full-connected layer 2
fc2_layer = {"weights":new_weights([1024,10]),
       "biases":new_weights([10])}
# Predicted class
y_pred = tf.nn.softmax(tf.matmul(h_fc1_drop,fc2_layer["weights"])+fc2_layer["biases"]) # The output is like [0 0 1 0 0 0 0 0 0 0]
y_pred_cls = tf.argmax(y_pred,dimension=1) # Show the real predict number like '2'
# cost function to be optimized
cross_entropy = -tf.reduce_mean(y_true*tf.log(y_pred))
optimizer = tf.train.AdamOptimizer(learning_rate=1e-4).minimize(cross_entropy)
# Performance Measures
correct_prediction = tf.equal(y_pred_cls,y_true_cls)
accuracy = tf.reduce_mean(tf.cast(correct_prediction,"float"))
with tf.Session() as sess:
  init = tf.global_variables_initializer()
  sess.run(init)
  train_batch_size = 50
  def optimize(num_iterations):
    total_iterations=0
    start_time = time.time()
    for i in range(total_iterations,total_iterations+num_iterations):
      x_batch,y_true_batch = data.train.next_batch(train_batch_size)
      feed_dict_train_op = {x:x_batch,y_true:y_true_batch,keep_prob:0.5}
      feed_dict_train = {x:x_batch,y_true:y_true_batch,keep_prob:1.0}
      sess.run(optimizer,feed_dict=feed_dict_train_op)
      # Print status every 100 iterations.
      if i%100==0:
        # Calculate the accuracy on the training-set.
        acc = sess.run(accuracy,feed_dict=feed_dict_train)
        # Message for printing.
        msg = "Optimization Iteration:{0:>6}, Training Accuracy: {1:>6.1%}"
        # Print it.
        print(msg.format(i+1,acc))
    # Update the total number of iterations performed
    total_iterations += num_iterations
    # Ending time
    end_time = time.time()
    # Difference between start and end_times.
    time_dif = end_time-start_time
    # Print the time-usage
    print("Time usage:"+str(timedelta(seconds=int(round(time_dif)))))
  test_batch_size = 256
  def print_test_accuracy():
    # Number of images in the test-set.
    num_test = len(data.test.images)
    cls_pred = np.zeros(shape=num_test,dtype=np.int)
    i = 0
    while i < num_test:
      # The ending index for the next batch is denoted j.
      j = min(i+test_batch_size,num_test)
      # Get the images from the test-set between index i and j
      images = data.test.images[i:j, :]
      # Get the associated labels
      labels = data.test.labels[i:j, :]
      # Create a feed-dict with these images and labels.
      feed_dict={x:images,y_true:labels,keep_prob:1.0}
      # Calculate the predicted class using Tensorflow.
      cls_pred[i:j] = sess.run(y_pred_cls,feed_dict=feed_dict)
      # Set the start-index for the next batch to the
      # end-index of the current batch
      i = j
    cls_true = data.test.cls
    correct = (cls_true==cls_pred)
    correct_sum = correct.sum()
    acc = float(correct_sum) / num_test
    # Print the accuracy
    msg = "Accuracy on Test-Set: {0:.1%} ({1}/{2})"
    print(msg.format(acc,correct_sum,num_test))
  # Performance after 10000 optimization iterations
  optimize(num_iterations=10000)
  print_test_accuracy()
  savew_hl1 = layer_conv1["weights"].eval()
  saveb_hl1 = layer_conv1["biases"].eval()
  savew_hl2 = layer_conv2["weights"].eval()
  saveb_hl2 = layer_conv2["biases"].eval()
  savew_fc1 = fc1_layer["weights"].eval()
  saveb_fc1 = fc1_layer["biases"].eval()
  savew_op = fc2_layer["weights"].eval()
  saveb_op = fc2_layer["biases"].eval()

  np.save("savew_hl1.npy", savew_hl1)
  np.save("saveb_hl1.npy", saveb_hl1)
  np.save("savew_hl2.npy", savew_hl2)
  np.save("saveb_hl2.npy", saveb_hl2)
  np.save("savew_hl3.npy", savew_fc1)
  np.save("saveb_hl3.npy", saveb_fc1)
  np.save("savew_op.npy", savew_op)
  np.save("saveb_op.npy", saveb_op)

運(yùn)行結(jié)果顯示:測試集中準(zhǔn)確率大概為99.2%。

我還寫了一些輔助函數(shù),可以查看部分識別錯誤的圖片,

Python(TensorFlow框架)實(shí)現(xiàn)手寫數(shù)字識別系統(tǒng)的方法

還可以查看混淆矩陣,

Python(TensorFlow框架)實(shí)現(xiàn)手寫數(shù)字識別系統(tǒng)的方法

2.3 實(shí)現(xiàn)手寫識別系統(tǒng)

最后,將訓(xùn)練好的參數(shù)保存,封裝進(jìn)一個GUI界面中,形成一個手寫識別系統(tǒng)。

Python(TensorFlow框架)實(shí)現(xiàn)手寫數(shù)字識別系統(tǒng)的方法

系統(tǒng)中還添加了一點(diǎn)圖像預(yù)處理的操作,比如灰度化,圖像信息的歸一化等,更貼近實(shí)際應(yīng)用。
系統(tǒng)可進(jìn)行快速識別,如下圖

Python(TensorFlow框架)實(shí)現(xiàn)手寫數(shù)字識別系統(tǒng)的方法

3 總結(jié)

本文實(shí)現(xiàn)的系統(tǒng)其實(shí)是基于卷積神經(jīng)網(wǎng)絡(luò)的手寫數(shù)字識別系統(tǒng)。該系統(tǒng)能快速實(shí)現(xiàn)手寫數(shù)字識別,成功識別率高。缺點(diǎn):只能正確識別單個數(shù)字,圖像預(yù)處理還不夠,沒有進(jìn)行圖像分割,讀者也可以自行添加,進(jìn)行完善。

4 收獲

本人之前的本科期間,雖然努力學(xué)習(xí)高數(shù)、線性代數(shù)和概率論,但是沒有認(rèn)真學(xué)習(xí)過機(jī)器學(xué)習(xí),本人是2017年才開始系統(tǒng)學(xué)習(xí)機(jī)器學(xué)習(xí)相關(guān)知識,而且本科畢業(yè)論文也選擇了相關(guān)的課題,雖然比較基礎(chǔ),但是認(rèn)真完成后,有一種學(xué)以致用的滿足感,同時也激勵著我進(jìn)行更深入的理論學(xué)習(xí)和實(shí)踐探討,與所有讀者共勉。

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持億速云。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI