尋夢新聞LINE@每日推播熱門推薦文章,趣聞不漏接❤️
LeNet是一個卷積神經網路,使用 Low-Level 交替卷積和最大池化操作,然後使用經典多層感知器(人工神經網路)和softmax層作為輸出。
LeNet = Combination of (Directed Convolution + Directed Max Pooling) as lower layers+ Artificial Neural Network + Softmax(Output Layer)
使用Keras創建LeNet
要使用Keras定義LeNet,我們需要導入以下Python庫
from keras.models import Sequential from keras.layers import Conv2D, MaxPooling2D, Flatten, Dense from keras.datasets import mnist from keras import backend as k from keras.utils import np_utils import numpy as np import matplotlib.pyplot as plt
簡要說明 –
- Sequential() – 我們將要使用的Keras模型,允許我們創建一個線性的層堆棧
- Conv2D() – Keras的卷積層
- MaxPooling2D() – Keras的最大池化層
- Flatten() – 用於將Pooled 3D層flattening為1D Column
- Dense() – 正常的感知器層
- np_utils() – 用於One Hot將分類變量編碼為數值
然後我們將在一個類中定義LeNet –
class LeNet: @staticmethod def build(input_shape, classes): # Defining the model to be used model = Sequential() # Adding the layers model.add(Conv2D(20, kernel_size=5, activation='relu', padding='same', input_shape=input_shape)) model.add(MaxPooling2D(pool_size=(2,2), strides=(2,2))) model.add(Conv2D(50, kernel_size=5, border_mode='same', activation='relu')) model.add(MaxPooling2D(pool_size=(2,2), strides=(2,2))) model.add(Flatten()) model.add(Dense(500, activation='relu')) model.add(Dense(classes, activation='softmax')) return model
在這里,我們定義一個build()方法,它創建一個Sequential模型並添加方法中定義的層 –
- 卷積層,使用ReLu激活函數,使用大小為(5,5)的kernel/filter將輸入轉換為20個Feature Maps 。
- 一個Max Pooling Layer,它的stride(jump)為2,從feature map中查找大小為(2,2)的塊中的最大值。
- 一個卷積層,使用大小為(5,5)的kernel/filter,使用ReLu激活函數將輸入轉換為50個Feature map。
- 一個Max Pooling Layer,它的stride(jump)為2,從feature map中查找大小為(2,2)的塊中的最大值。
- Flatten()層將Max Pooling Layer轉換為1D數字列,作為輸入饋送到ANN。
- 使用Dense()層創建500個節點的ANN,然後使用softmax層進行輸出。
訓練網路
現在,我們需要編寫一些額外的Python代碼來訓練網路並獲得預測 –
# Hyperparameter Definition BATCH_SIZE=128 EPOCHS = 10 IP_SHAPE = (1, 28, 28) k.set_image_dim_ordering('th') # Loading and preprocessing dataset (X_train, y_train), (X_test, y_test) = mnist.load_data() X_train = X_train.astype('float32') X_test = X_test.astype('float32') X_train /= 255 X_test /= 255 X_train = X_train[:, np.newaxis, :, :] X_test = X_test[:, np.newaxis, :, :] y_train = np_utils.to_categorical(y_train, 10) y_test = np_utils.to_categorical(y_test, 10) # Building the model model = LeNet.build(input_shape=IP_SHAPE, classes=10) model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy']) history=model.fit(X_train, y_train, batch_size=BATCH_SIZE, epochs=EPOCHS, verbose=True, validation_split=0.25) score=model.evaluate(X_test, y_test, verbose=True) print("Test Score : ", score[0]) print("Test Accuracy : ", score[1]) print(history.history.keys())
- 首先,我們定義一些將用於訓練過程的超參數(如epochs數,batch size 等)。
- 然後,我們加載機器學習數據集並創建訓練和測試集。
- 我們將值轉換為float32數據類型,並將值除以255以歸一化我們的機器學習數據集。
- 之後,我們使用ONE HOT ENCODER編碼將目標集轉換為分類數據。
- 之後,我們構建機器學習模型並將數據擬合到其中。
- 我們使用history變量通過擬合機器學習數據集來存儲返回的值,然後我們根據給定數據評估和測試機器學習模型。
可視化
在這一部分中,我們分別繪制訓練和測試集的模型準確度,以了解機器學習模型如何學習的。
# Accuracy Visualization plt.plot(history.history['acc']) plt.plot(history.history['val_acc']) plt.title("LeNet Model Accuracy") plt.ylabel('Accuracy') plt.xlabel('Number of Epochs') plt.legend(['train', 'test'], loc='upper left') plt.show()
最終完整做到
做到LeNet的完整Python代碼如下 –
from keras.models import Sequential from keras.layers import Conv2D, MaxPooling2D, Flatten, Dropout, Dense, Activation from keras.datasets import mnist from keras import backend as k from keras.utils import np_utils import numpy as np import matplotlib.pyplot as plt # Defining a LeNet Class class LeNet: @staticmethod def build(input_shape, classes): # Defining the model to be used model = Sequential() # Adding the layers model.add(Conv2D(20, kernel_size=5, activation='relu', padding='same', input_shape=input_shape)) model.add(MaxPooling2D(pool_size=(2,2), strides=(2,2))) model.add(Conv2D(50, kernel_size=5, border_mode='same', activation='relu')) model.add(MaxPooling2D(pool_size=(2,2), strides=(2,2))) model.add(Flatten()) model.add(Dense(500, activation='relu')) model.add(Dense(classes, activation='softmax')) return model # Hyperparameter Definition BATCH_SIZE=128 EPOCHS = 10 IP_SHAPE = (1, 28, 28) k.set_image_dim_ordering('th') # Loading and preprocessing dataset (X_train, y_train), (X_test, y_test) = mnist.load_data() X_train = X_train.astype('float32') X_test = X_test.astype('float32') X_train /= 255 X_test /= 255 X_train = X_train[:, np.newaxis, :, :] X_test = X_test[:, np.newaxis, :, :] y_train = np_utils.to_categorical(y_train, 10) y_test = np_utils.to_categorical(y_test, 10) # Building the model model = LeNet.build(input_shape=IP_SHAPE, classes=10) model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy']) history=model.fit(X_train, y_train, batch_size=BATCH_SIZE, epochs=EPOCHS, verbose=True, validation_split=0.25) score=model.evaluate(X_test, y_test, verbose=True) print("Test Score : ", score[0]) print("Test Accuracy : ", score[1]) print(history.history.keys()) # Accuracy Visualization plt.plot(history.history['acc']) plt.plot(history.history['val_acc']) plt.title("LeNet Model Accuracy") plt.ylabel('Accuracy') plt.xlabel('Number of Epochs') plt.legend(['train', 'test'], loc='upper left') plt.show()
在執行上面給出的代碼時,我們意識到訓練時間與正常MLP相比呈指數增長,但我們在10個epochs內做到了99.6%的準確度。