TensorFlow 2.0 mnist手写数字识别(CNN卷积神经网络)
TensorFlow 2.0 (五) - mnist手寫數(shù)字識別(CNN卷積神經(jīng)網(wǎng)絡)
源代碼/數(shù)據(jù)集已上傳到?Github - tensorflow-tutorial-samples
大白話講解卷積神經(jīng)網(wǎng)絡工作原理,推薦一個bilibili的講卷積神經(jīng)網(wǎng)絡的視頻,up主從youtube搬運過來,用中文講了一遍。
這篇文章是?TensorFlow 2.0 Tutorial?入門教程的第五篇文章,介紹如何使用卷積神經(jīng)網(wǎng)絡(Convolutional Neural Network,?CNN)來提高mnist手寫數(shù)字識別的準確性。之前使用了最簡單的784x10的神經(jīng)網(wǎng)絡,達到了?0.91?的正確性,而這篇文章在使用了卷積神經(jīng)網(wǎng)絡后,正確性達到了0.99
卷積神經(jīng)網(wǎng)絡(Convolutional Neural Network,?CNN)是一種前饋神經(jīng)網(wǎng)絡,它的人工神經(jīng)元可以響應一部分覆蓋范圍內(nèi)的周圍單元,對于大型圖像處理有出色表現(xiàn)。
卷積神經(jīng)網(wǎng)絡由一個或多個卷積層和頂端的全連通層(對應經(jīng)典的神經(jīng)網(wǎng)絡)組成,同時也包括關聯(lián)權重和池化層(pooling layer)。這一結構使得卷積神經(jīng)網(wǎng)絡能夠利用輸入數(shù)據(jù)的二維結構。與其他深度學習結構相比,卷積神經(jīng)網(wǎng)絡在圖像和語音識別方面能夠給出更好的結果。這一模型也可以使用反向傳播算法進行訓練。相比較其他深度、前饋神經(jīng)網(wǎng)絡,卷積神經(jīng)網(wǎng)絡需要考量的參數(shù)更少,使之成為一種頗具吸引力的深度學習結構。
——維基百科
1. 安裝TensorFlow 2.0
Google與2019年3月發(fā)布了TensorFlow 2.0,TensorFlow 2.0 清理了廢棄的API,通過減少重復來簡化API,并且通過Keras能夠輕松地構建模型,從這篇文章開始,教程示例采用TensorFlow 2.0版本。
| 1 | pip install tensorflow==2.0.0-beta0 |
或者在這里下載whl包安裝:https://pypi.tuna.tsinghua.edu.cn/simple/tensorflow/
2. 代碼目錄結構
| 1 2 3 4 5 6 7 8 9 10 11 12 13 | data_set_tf2/ # TensorFlow 2.0的mnist數(shù)據(jù)集|--mnist.npz test_images/ # 預測所用的圖片|--0.png|--1.png|--4.png v4_cnn/|--ckpt/ # 模型保存的位置|--checkpoint|--cp-0005.ckpt.data-00000-of-00001|--cp-0005.ckpt.index|--predict.py # 預測代碼|--train.py # 訓練代碼 |
3. CNN模型代碼(train.py)
模型定義的前半部分主要使用Keras.layers提供的Conv2D(卷積)與MaxPooling2D(池化)函數(shù)。
CNN的輸入是維度為 (image_height, image_width, color_channels)的張量,mnist數(shù)據(jù)集是黑白的,因此只有一個color_channel(顏色通道),一般的彩色圖片有3個(R,G,B),熟悉Web前端的同學可能知道,有些圖片有4個通道(R,G,B,A),A代表透明度。對于mnist數(shù)據(jù)集,輸入的張量維度就是(28,28,1),通過參數(shù)input_shape傳給網(wǎng)絡的第一層。
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | import os import tensorflow as tf from tensorflow.keras import datasets, layers, modelsclass CNN(object):def __init__(self):model = models.Sequential()# 第1層卷積,卷積核大小為3*3,32個,28*28為待訓練圖片的大小model.add(layers.Conv2D(32, (3, 3), activation='relu', input_shape=(28, 28, 1)))model.add(layers.MaxPooling2D((2, 2)))# 第2層卷積,卷積核大小為3*3,64個model.add(layers.Conv2D(64, (3, 3), activation='relu'))model.add(layers.MaxPooling2D((2, 2)))# 第3層卷積,卷積核大小為3*3,64個model.add(layers.Conv2D(64, (3, 3), activation='relu'))model.add(layers.Flatten())model.add(layers.Dense(64, activation='relu'))model.add(layers.Dense(10, activation='softmax'))model.summary()self.model = model |
model.summary()用來打印我們定義的模型的結構。
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | Model: "sequential" _________________________________________________________________ Layer (type) Output Shape Param # ================================================================= conv2d (Conv2D) (None, 26, 26, 32) 320 _________________________________________________________________ max_pooling2d (MaxPooling2D) (None, 13, 13, 32) 0 _________________________________________________________________ conv2d_1 (Conv2D) (None, 11, 11, 64) 18496 _________________________________________________________________ max_pooling2d_1 (MaxPooling2 (None, 5, 5, 64) 0 _________________________________________________________________ conv2d_2 (Conv2D) (None, 3, 3, 64) 36928 _________________________________________________________________ flatten (Flatten) (None, 576) 0 _________________________________________________________________ dense (Dense) (None, 64) 36928 _________________________________________________________________ dense_1 (Dense) (None, 10) 650 ================================================================= Total params: 93,322 Trainable params: 93,322 Non-trainable params: 0 _________________________________________________________________ |
我們可以看到,每一個Conv2D和MaxPooling2D層的輸出都是一個三維的張量(height, width, channels)。height和width會逐漸地變小。輸出的channel的個數(shù),是由第一個參數(shù)(例如,32或64)控制的,隨著height和width的變小,channel可以變大(從算力的角度)。
模型的后半部分,是定義輸出張量的。layers.Flatten會將三維的張量轉(zhuǎn)為一維的向量。展開前張量的維度是(3, 3, 64) ,轉(zhuǎn)為一維(576)的向量后,緊接著使用layers.Dense層,構造了2層全連接層,逐步地將一維向量的位數(shù)從576變?yōu)?4,再變?yōu)?0。
后半部分相當于是構建了一個隱藏層為64,輸入層為576,輸出層為10的普通的神經(jīng)網(wǎng)絡。最后一層的激活函數(shù)是softmax,10位恰好可以表達0-9十個數(shù)字。
最大值的下標即可代表對應的數(shù)字,使用numpy很容易計算出來:
| 1 2 3 4 5 6 | import numpy as npy1 = [0, 0.8, 0.1, 0.1, 0, 0, 0, 0, 0, 0] y2 = [0, 0.1, 0.1, 0.1, 0.5, 0, 0.2, 0, 0, 0] np.argmax(y1) # 1 np.argmax(y2) # 4 |
4. mnist數(shù)據(jù)集預處理(train.py)
| 1 2 3 4 5 6 7 8 9 10 11 12 13 | class DataSource(object):def __init__(self):# mnist數(shù)據(jù)集存儲的位置,如何不存在將自動下載data_path = os.path.abspath(os.path.dirname(__file__)) + '/../data_set_tf2/mnist.npz'(train_images, train_labels), (test_images, test_labels) = datasets.mnist.load_data(path=data_path)# 6萬張訓練圖片,1萬張測試圖片train_images = train_images.reshape((60000, 28, 28, 1))test_images = test_images.reshape((10000, 28, 28, 1))# 像素值映射到 0 - 1 之間train_images, test_images = train_images / 255.0, test_images / 255.0self.train_images, self.train_labels = train_images, train_labelsself.test_images, self.test_labels = test_images, test_labels |
因為mnist數(shù)據(jù)集國內(nèi)下載不穩(wěn)定,因此數(shù)據(jù)集也同步到了Github倉庫。
對mnist數(shù)據(jù)集的介紹,大家可以參考這個系列的第一篇文章TensorFlow入門(一) - mnist手寫數(shù)字識別(網(wǎng)絡搭建)。
5. 開始訓練并保存訓練結果(train.py)
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | class Train:def __init__(self):self.cnn = CNN()self.data = DataSource()def train(self):check_path = './ckpt/cp-{epoch:04d}.ckpt'# period 每隔5epoch保存一次save_model_cb = tf.keras.callbacks.ModelCheckpoint(check_path, save_weights_only=True, verbose=1, period=5)self.cnn.model.compile(optimizer='adam',loss='sparse_categorical_crossentropy',metrics=['accuracy'])self.cnn.model.fit(self.data.train_images, self.data.train_labels, epochs=5, callbacks=[save_model_cb])test_loss, test_acc = self.cnn.model.evaluate(self.data.test_images, self.data.test_labels)print("準確率: %.4f,共測試了%d張圖片 " % (test_acc, len(self.data.test_labels)))if __name__ == "__main__":app = Train()app.train() |
在執(zhí)行python train.py后,會得到以下的結果:
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | Train on 60000 samples Epoch 1/5 60000/60000 [==============================] - 45s 749us/sample - loss: 0.1477 - accuracy: 0.9536 Epoch 2/5 60000/60000 [==============================] - 45s 746us/sample - loss: 0.0461 - accuracy: 0.9860 Epoch 3/5 60000/60000 [==============================] - 50s 828us/sample - loss: 0.0336 - accuracy: 0.9893 Epoch 4/5 60000/60000 [==============================] - 50s 828us/sample - loss: 0.0257 - accuracy: 0.9919 Epoch 5/5 59968/60000 [============================>.] - ETA: 0s - loss: 0.0210 - accuracy: 0.9930 Epoch 00005: saving model to ./ckpt/cp-0005.ckpt 60000/60000 [==============================] - 51s 848us/sample - loss: 0.0210 - accuracy: 0.9930 10000/10000 [==============================] - 3s 290us/sample - loss: 0.0331 - accuracy: 0.9901 準確率: 0.9901,共測試了10000張圖片 |
可以看到,在第一輪訓練后,識別準確率達到了0.9536,5輪之后,使用測試集驗證,準確率達到了0.9901
在第五輪時,模型參數(shù)成功保存在了./ckpt/cp-0005.ckpt。接下來我們就可以加載保存的模型參數(shù),恢復整個卷積神經(jīng)網(wǎng)絡,進行真實圖片的預測了。
6. 圖片預測(predict.py)
為了將模型的訓練和加載分開,預測的代碼寫在了predict.py中。
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | import tensorflow as tf from PIL import Image import numpy as npfrom train import CNN''' python 3.7 tensorflow 2.0.0b0 pillow(PIL) 4.3.0 '''class Predict(object):def __init__(self):latest = tf.train.latest_checkpoint('./ckpt')self.cnn = CNN()# 恢復網(wǎng)絡權重self.cnn.model.load_weights(latest)def predict(self, image_path):# 以黑白方式讀取圖片img = Image.open(image_path).convert('L')img = np.reshape(img, (28, 28, 1)) / 255.x = np.array([1 - img])# API refer: https://keras.io/models/model/y = self.cnn.model.predict(x)# 因為x只傳入了一張圖片,取y[0]即可# np.argmax()取得最大值的下標,即代表的數(shù)字print(image_path)print(y[0])print(' -> Predict digit', np.argmax(y[0]))if __name__ == "__main__":app = Predict()app.predict('../test_images/0.png')app.predict('../test_images/1.png')app.predict('../test_images/4.png') |
最終,執(zhí)行predict.py,可以看到:
| 1 2 3 4 5 6 7 8 9 10 | $ python predict.py ../test_images/0.png [1. 0. 0. 0. 0. 0. 0. 0. 0. 0.]-> Predict digit 0 ../test_images/1.png [0. 1. 0. 0. 0. 0. 0. 0. 0. 0.]-> Predict digit 1 ../test_images/4.png [0. 0. 0. 0. 1. 0. 0. 0. 0. 0.]-> Predict digit 4 |
與TensorFlow1.0的區(qū)別總結
附: 推薦
- 一篇文章入門 Python
總結
以上是生活随笔為你收集整理的TensorFlow 2.0 mnist手写数字识别(CNN卷积神经网络)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 在pandas中遍历DataFrame行
- 下一篇: Gtk-WARNING : cannot