Monday, May 03, 2021

Simple MNIST convnet Illustrated

Simple MNIST convnet Illustrated

2021/04/15

-----


https://pixabay.com/zh/photos/iceberg-ice-sol-antarctica-cold-2170383/

-----

https://keras.io/examples/vision/mnist_convnet/


◎ keras.datasets.mnist.load_data

https://www.tensorflow.org/api_docs/python/tf/keras/datasets/mnist/load_data


◎ .astype

https://numpy.org/doc/stable/reference/generated/numpy.ndarray.astype.html


◎ np.expand_dims

https://numpy.org/doc/stable/reference/generated/numpy.expand_dims.html


◎ keras.utils.to_categorical

https://www.tensorflow.org/api_docs/python/tf/keras/utils/to_categorical


◎ keras.Input

https://keras.io/api/layers/core_layers/input/

https://www.tensorflow.org/api_docs/python/tf/keras/Input


◎ layers.Conv2D

https://keras.io/zh/layers/convolutional/

https://www.tensorflow.org/api_docs/python/tf/keras/layers/Conv2D


◎ layers.MaxPooling2D

https://keras.io/zh/layers/pooling/

https://keras.io/api/layers/pooling_layers/max_pooling2d/

https://www.tensorflow.org/api_docs/python/tf/keras/layers/MaxPool2D


◎ layers.Flatten

https://keras.io/zh/layers/core/

https://keras.io/api/layers/reshaping_layers/flatten/

https://www.tensorflow.org/api_docs/python/tf/keras/layers/Flatten


◎ layers.Dropout

https://keras.io/zh/layers/core/

https://www.tensorflow.org/api_docs/python/tf/keras/layers/Dropout


◎ layers.Dense

https://keras.io/zh/layers/core/

https://www.tensorflow.org/api_docs/python/tf/keras/layers/Dense


◎ model.summary

◎ model.compile

◎ model.fit

◎ model.evaluate

https://keras.io/api/models/model/

https://www.tensorflow.org/api_docs/python/tf/keras/Model


-----

Setup

-----

import numpy as np

##### numpy

##### 「NumPy 是 Python 語言的一個擴充程式庫。支援高階大量的維度陣列與矩陣運算,此外也針對陣列運算提供大量的數學函數函式庫。」-- 中文維基百科。

from tensorflow import keras

##### tensorflow 

##### 「TensorFlow是一個開源軟體庫,用於各種感知和語言理解任務的機器學習。目前被 50 個團隊用於研究和生產許多 Google 商業產品,如語音辨識、Gmail、Google 相簿和搜尋,其中許多產品曾使用過其前任軟體 DistBelief。」-- 中文維基百科。

##### keras

##### 「Keras 是一個用 Python 編寫的開源神經網路庫,能夠在 TensorFlow 上執行。Keras 旨在快速實現深度神經網路,專注於使用者友好、模組化和可延伸性。Keras 被認為是一個介面,而非獨立的機器學習框架。它提供了更進階別、更直觀的抽象集,無論使用何種計算後端,使用者都可以輕鬆地開發深度學習模型。Keras 允許使用者在智慧型手機(iOS 和 Android)、網頁或 Java 虛擬機器上製作深度模型。」-- 由中文維基百科縮減。

from tensorflow.keras import layers

##### layers

##### 定義了深度學習模型的一些主要元件,如卷積層、池化層、全連接層、輸出層等等。

-----

Prepare the data

-----

# Model / data parameters

num_classes = 10

##### 阿拉伯數字 0 到 9 共 10 類。

input_shape = (28, 28, 1)

##### 圖片大小為 28x28,單色或彩色,此處 1 為單色。


# the data, split between train and test sets

(x_train, y_train), (x_test, y_test) = keras.datasets.mnist.load_data()

##### (x_train 訓練集影像, y_train 訓練集標籤), (x_test 測試集影像, y_test 測試集標籤)

##### keras.datasets.mnist.load_data()

##### https://waternotetw.blogspot.com/2018/03/keras-mnist.html


# Scale images to the [0, 1] range

x_train = x_train.astype("float32") / 255

x_test = x_test.astype("float32") / 255

##### astype("float32") / 255

##### 用 numpy 進行型別的強制轉換,然後將原來 0 到 255 的整數值變成 0 到 1 的浮點數。

##### https://numpy.org/doc/stable/reference/generated/numpy.ndarray.astype.html

# Make sure images have shape (28, 28, 1)

x_train = np.expand_dims(x_train, -1)

x_test = np.expand_dims(x_test, -1)

##### 參考下方範例。

##### >>> x.shape

##### (2, 2)

##### >>> np.expand_dims(x,axis=0).shape

##### (1, 2, 2)

##### >>> np.expand_dims(x,axis=-1).shape

##### (2, 2, 1)

##### https://www.zhihu.com/question/265545749

##### https://numpy.org/doc/stable/reference/generated/numpy.expand_dims.html

print("x_train shape:", x_train.shape)

print(x_train.shape[0], "train samples")

print(x_test.shape[0], "test samples")



# convert class vectors to binary class matrices

y_train = keras.utils.to_categorical(y_train, num_classes)

y_test = keras.utils.to_categorical(y_test, num_classes)

##### to_categorical。keras.utils.to_categorical(y, num_classes=None, dtype='float32') 將類向量(整數)轉換為二進制類矩陣。例如,用於categorical_crossentropy。參數。y:需要轉換成矩陣的類向量(從0到num_classes的整體)。num_classes:總類別數。D型:字符串,輸入所期望的數據類型(float32,float64,int32...)

##### https://keras.io/zh/utils/

##### https://blog.csdn.net/nima1994/article/details/82468965

-----

說明:


-----

x_train shape: (60000, 28, 28, 1)

60000 train samples

10000 test samples

-----

說明:


-----

Build the model

-----

model = keras.Sequential(

    [

        keras.Input(shape=input_shape),

        layers.Conv2D(32, kernel_size=(3, 3), activation="relu"),

##### Conv2D

##### 32 個卷積核。

##### kernel_size

##### (3, 3)

##### activation

##### relu

        layers.MaxPooling2D(pool_size=(2, 2)),

##### MaxPooling2D

        layers.Conv2D(64, kernel_size=(3, 3), activation="relu"),

        layers.MaxPooling2D(pool_size=(2, 2)),

        layers.Flatten(),

##### layers.Flatten() 將輸入展平。不影響批量大小。

        layers.Dropout(0.5),

##### layers.Dropout

        layers.Dense(num_classes, activation="softmax"),

##### layers.Dense

##### softmax

    ]

)


model.summary()

-----

說明:


-----

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         

_________________________________________________________________

flatten (Flatten)            (None, 1600)              0         

_________________________________________________________________

dropout (Dropout)            (None, 1600)              0         

_________________________________________________________________

dense (Dense)                (None, 10)                16010     

=================================================================

Total params: 34,826

Trainable params: 34,826

Non-trainable params: 0

_________________________________________________________________

-----

說明:


-----

Train the model

-----

batch_size = 128

##### batch_size:批次大小。每次反向傳播所處理的樣本數。

epochs = 15

##### epochs 

##### iteration 迭代次數。

##### 我們可以將 2000 個樣本的資料集分為 500 個批次,然後將需要 4 次迭代才能完成 1 個 epoch。

##### https://towardsdatascience.com/epoch-vs-iterations-vs-batch-size-4dfb9c7ce9c9


model.compile(loss="categorical_crossentropy", optimizer="adam", metrics=["accuracy"])

##### loss

##### categorical_crossentropy

##### optimizer

##### adam

##### metrics

##### accuracy


model.fit(x_train, y_train, batch_size=batch_size, epochs=epochs, validation_split=0.1)

##### validation_split

-----

Epoch 1/15

422/422 [==============================] - 13s 29ms/step - loss: 0.7840 - accuracy: 0.7643 - val_loss: 0.0780 - val_accuracy: 0.9780

Epoch 2/15

422/422 [==============================] - 13s 31ms/step - loss: 0.1199 - accuracy: 0.9639 - val_loss: 0.0559 - val_accuracy: 0.9843

Epoch 3/15

422/422 [==============================] - 14s 33ms/step - loss: 0.0845 - accuracy: 0.9737 - val_loss: 0.0469 - val_accuracy: 0.9877

Epoch 4/15

422/422 [==============================] - 14s 33ms/step - loss: 0.0762 - accuracy: 0.9756 - val_loss: 0.0398 - val_accuracy: 0.9895

Epoch 5/15

422/422 [==============================] - 15s 35ms/step - loss: 0.0621 - accuracy: 0.9812 - val_loss: 0.0378 - val_accuracy: 0.9890

Epoch 6/15

422/422 [==============================] - 17s 40ms/step - loss: 0.0547 - accuracy: 0.9825 - val_loss: 0.0360 - val_accuracy: 0.9910

Epoch 7/15

422/422 [==============================] - 17s 41ms/step - loss: 0.0497 - accuracy: 0.9840 - val_loss: 0.0311 - val_accuracy: 0.9920

Epoch 8/15

422/422 [==============================] - 16s 39ms/step - loss: 0.0443 - accuracy: 0.9862 - val_loss: 0.0346 - val_accuracy: 0.9910

Epoch 9/15

422/422 [==============================] - 17s 39ms/step - loss: 0.0436 - accuracy: 0.9860 - val_loss: 0.0325 - val_accuracy: 0.9915

Epoch 10/15

422/422 [==============================] - 16s 38ms/step - loss: 0.0407 - accuracy: 0.9865 - val_loss: 0.0301 - val_accuracy: 0.9920

Epoch 11/15

422/422 [==============================] - 16s 37ms/step - loss: 0.0406 - accuracy: 0.9874 - val_loss: 0.0303 - val_accuracy: 0.9920

Epoch 12/15

237/422 [===============>..............] - ETA: 7s - loss: 0.0398 - accuracy: 0.9877

-----

說明:


-----

Evaluate the trained model

-----

score = model.evaluate(x_test, y_test, verbose=0)

##### verbose

print("Test loss:", score[0])

print("Test accuracy:", score[1])

-----

說明:


-----

Test loss: 0.023950600996613503

Test accuracy: 0.9922000169754028

-----

說明:


-----

References

[1] Simple MNIST convnet

https://keras.io/examples/vision/mnist_convnet/

[2] DAY27 基於Keras使用CNN進行數字辨識(1) - iT 邦幫忙::一起幫忙解決難題,拯救 IT 人的一天

https://ithelp.ithome.com.tw/articles/10197257

[3] 深度學習基礎理論

https://hemingwang.blogspot.com/2021/02/lee.html

[4] LeNet

https://hemingwang.blogspot.com/2020/12/illustrated-lenet.html

[5] AlexNet

https://hemingwang.blogspot.com/2020/12/illustrated-alexnet.html

-----

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.