2019年8月9日

Python AI-手寫辨識

Python AI-手寫辨識

類神經網路-手寫辨識

手寫辨識

(1) 問題定義
enter image description here

將輸入手寫數字圖片,經由類神經網路訓練後,可以辨識手寫圖片得到一個正確的答案,例如讓電腦辨識上面圖片手寫數字0-9,都可以認得.在了解問題後,需要先知道輸入的資料格式,例如圖片為NxN的矩陣向量.

enter image description here

  • 輸入:輸入的資料格式有很多種,例如數字圖片為矩陣向量
  • 模型:NN
  • 輸出:輸出的方式,神經網路輸出不一定跟輸入同值,手寫數字輸入為1,輸出有可能是1.1或是1.5等等,所以輸出必須經過轉換成真實世界的數字.

(2)定義函式
輸出會有兩個問題:
A.輸出利用one-hot encoding來表示,就是N個狀態會對應N的結果,例如:輸出結果為1,表示[0,1,0,0,0,0,0,0,0,0]
B.輸出結果不能超過1,我們通常會利用Softmax函数來進行輸出的處理.

(3) 準備訓練/測試資料
在這邊需要從輸入去定義那些要當作訓練與測試資料,我們手寫資料使用MNIST 資料庫來訓練使用,MNIST共有70,000筆手寫資料,60,000筆為訓練資料,10,000為測試資料.

(4)建構類神經網路模型
開始建構我們的神經網路模型,首先決定好28x28的像素(這邊不用擔心如何將圖片轉成矩陣),模型使用SGD的方式進行學習,輸出是一個10為的陣列來表示.
enter image description here

  • 輸入:手寫數字圖片(28x28=784)
  • 模型:SGD
  • 輸出:數字(one hard encoding)

(5)學習
首先介紹SGD(Stochastic Gradient Descent) 的學習方式,因為蕾神經網路需要訓練很多次才會提高準確度,SGD最大的好處就是當每次重新學習的會將訓練資料打散,來防止機器學習將答案死背下來.

(6)實作開發
下面程式碼有完整的說明,這邊就不多說明了,當開始執行程式時就會進行資料訓練.

enter image description here

由訓練結果最後acc=0.9447,表示準確率可以到達94%,我們再由實際測試可以看出該圖為7的圖示,由神經網路判斷為7,跟我們人類判斷相同,我們可以知道由訓練的結果可看得到不錯的準確度.

enter image description here

執行神經網路遇到不少問題,請參考下面連結,是筆者所整理的問題集,請多多指教https://programdoubledragon.blogspot.com/2019/08/python-ai_5.html

#確定使用深度學習套件tensorflow
%env KERAS_BACKEND=tensorflow

%matplotlib inline

#載入相關函式庫
import numpy as np
import matplotlib.pyplot as plt
import os
os.environ["KMP_DUPLICATE_LIB_OK"]="TRUE"

#讀入MNIST的數據庫
from keras.datasets import mnist 
(x_train, y_train),(x_test, y_test) = mnist.load_data()
#驗證len(x_train),len(y_train)是不是六萬筆資料
len(x_train)
len(y_train)


#輸出一筆看看
#x_train[9487].shape
#(28,28)
#輸出矩陣
#x_train[9487]
#輸出圖片
#plt.imshow(x_train[9486], cmap='Greys')
#y_train[9487]

#輸入格式整理
x_train.shape

#輸入格式整理:因為類神經網路,只能平平的資料,所以需要用reshape重新將
x_train = x_train.reshape(60000, 784)
x_test  = x_test.reshape(10000, 784)

#輸出 one-hot encoding
from keras.utils import np_utils
y_train = np_utils.to_categorical(y_train, 10)
y_test = np_utils.to_categorical(y_test, 10)
#9487可以看出[0,1,0,0,0,0,0,0,0,0]
y_train[9487]

#===========================神經網路模型============================
# 隱藏層: 兩個隱藏層,每個有500神經元
# 學習模型:SGD
#
#=================================================================
from keras.models import Sequential
from keras.layers import Dense, Activation
from keras.optimizers import SGD

#建立空的神經網路
model = Sequential()

#第一層隱藏層神經網路:建立500個神經元,輸入矩陣資料784個元素
model.add(Dense(500, input_dim=784))
#激發函式為sigmoid
model.add(Activation('sigmoid'))
#第二層隱藏層神經網路:建立500個神經元,輸入矩陣資料784個元素
model.add(Dense(500))
model.add(Activation('sigmoid'))
#輸出10維
model.add(Dense(10))
#激發函式為softmax,表示輸出只會是1,因為輸出陣列的位置表示為數字,例如:y[3]=1表示輸出為3;
model.add(Activation('softmax'))

#compile你的神經網路
#loss='mse'將誤差平方起來且平均
#optimizer=SGD(lr=0.1) lr為學習率
#metrics=['accuracy'] 可以查看正確率
model.compile(loss='mse', optimizer=SGD(lr=0.1) ,  metrics=['accuracy'])
#查看神經網路的模型
#model.summary()

# 驗證第一層:784X500+500(biase)=392500
# 驗證第二層:500X500+500(biase)=250500
# 輸出:500*10+10=5010

#終於來到最後輸出
#每100筆重新調整一次參數,並訓練次
model.fit(x_train, y_train, batch_size=100, epochs=20)


from ipywidgets import interact_manual
predict = model.predict_classes(x_test)

#建立測試函式
def test(測試編號):
    plt.imshow(x_test[測試編號].reshape(28,28), cmap='Greys'  )
    print('神經網路判斷為' , predict[測試編號])

#執行
interact_manual(test, 測試編號=(0,9999))

#儲存神經網路模型
#因為模型是以json格式,所以我們先用model_json來儲存
model_json = model.to_json()
#利用檔案的方式儲存
open('handwriting_model_arch.json','w').write(model_json)
model.save_weights("handwriting_model_arch.h5")

參考

1.http://moocs.nccu.edu.tw/media/23062
2. MNIST_database 維基百科
3. keras - 优化器 Optimizers
4. 資料:手寫六萬筆資料

2 則留言:

  1. 您好 請問上面手寫的是人寫的,然後輸入程式中嗎 ?

    回覆刪除
  2. Do you realize there is a 12 word sentence you can say to your partner... that will trigger intense emotions of love and impulsive attractiveness for you buried within his chest?

    That's because hidden in these 12 words is a "secret signal" that fuels a man's impulse to love, please and care for you with his entire heart...

    =====> 12 Words Will Trigger A Man's Love Response

    This impulse is so hardwired into a man's genetics that it will make him try harder than before to love and admire you.

    In fact, fueling this powerful impulse is absolutely binding to getting the best possible relationship with your man that once you send your man one of these "Secret Signals"...

    ...You'll instantly notice him expose his heart and soul to you in such a way he never experienced before and he will perceive you as the one and only woman in the world who has ever truly interested him.

    回覆刪除