import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Flatten
from tensorflow.keras.layers import Conv2D, MaxPool2D
from tensorflow.keras.optimizers import SGD, Adam
from tensorflow.keras.datasets import cifar10
from tensorflow.keras.utils import to_categorical
from tensorflow.keras.preprocessing.image import load_img
CIFAR 10 是包含 10 種類的彩色小圖資料集,每張圖的尺寸為 $32\times32$
10 個類別分別是:飛機、交通工具、鳥、貓、鹿、狗、青蛙、馬、船、卡車
讀取 CIFAR 10 資料集
class_name = ['飛機', '汽車', '鳥', '貓', '鹿', '狗', '青蛙', '馬', '船', '卡車']
(x_train, y_train), (x_test, y_test) = cifar10.load_data()
x_train = x_train/255
x_test = x_test/255
y_train = to_categorical(y_train, 10)
y_test = to_categorical(y_test, 10)
plt.imshow(x_test[5])
print(f"這是{class_name[y_test[5].argmax()]}");
這是青蛙
y_test[5].flatten()
(10000, 1)
x_train = x_train/255
x_test = x_test/255
y_train = to_categorical(y_train, 10)
y_test = to_categorical(y_test, 10)
在這個部分,我們將逐步帶領大家建立經典的 CNN 模型 LeNet-5 的變形。
LeNet-5 分成兩個部分,分別為卷積層與全連接層,兩部份之間是透過扁平層 (Flatten) ,將卷積層最後輸出的 2 維向量壓扁成 1 維向量。
model = Sequential()
model.add(Conv2D(32, (3, 3), input_shape=(32, 32, 3), padding='same', activation='relu'))
model.add(MaxPool2D())
model.add(Conv2D(128, (3, 3), padding='same', activation='relu'))
model.add(MaxPool2D())
model.add(Conv2D(512, (3, 3), padding='same', activation='relu'))
model.add(MaxPool2D())
model.add(Flatten())
model.add(Dense(256, activation='relu'))
model.add(Dense(10, activation='softmax'))
model.summary()
Model: "sequential" ___________________________________________________________________________ Layer (type) Output Shape Param # =========================================================================== conv2d (Conv2D) (None, 32, 32, 32) 896 max_pooling2d (MaxPooling2D) (None, 16, 16, 32) 0 conv2d_1 (Conv2D) (None, 16, 16, 128) 36992 max_pooling2d_1 (MaxPooling2D) (None, 8, 8, 128) 0 conv2d_2 (Conv2D) (None, 8, 8, 512) 590336 max_pooling2d_2 (MaxPooling2D) (None, 4, 4, 512) 0 flatten (Flatten) (None, 8192) 0 dense (Dense) (None, 256) 2097408 dense_1 (Dense) (None, 10) 2570 =========================================================================== Total params: 2,728,202 Trainable params: 2,728,202 Non-trainable params: 0 ___________________________________________________________________________
model.compile(loss='categorical_crossentropy',
optimizer=Adam(),
metrics=['accuracy'])
model.fit(x_train, y_train,
batch_size=128,
epochs=3,
validation_data=(x_test, y_test)
)
Epoch 1/3 391/391 [==============================] - 26s 38ms/step - loss: 1.4611 - accuracy: 0.4717 - val_loss: 1.1093 - val_accuracy: 0.6097 Epoch 2/3 391/391 [==============================] - 14s 36ms/step - loss: 0.9935 - accuracy: 0.6487 - val_loss: 1.0079 - val_accuracy: 0.6478 Epoch 3/3 391/391 [==============================] - 14s 36ms/step - loss: 0.8006 - accuracy: 0.7194 - val_loss: 0.7835 - val_accuracy: 0.7292
<keras.callbacks.History at 0x7f5e2c2fca10>
score_train = model.evaluate(x_train, y_train)
score_test = model.evaluate(x_test, y_test)
1563/1563 [==============================] - 11s 7ms/step - loss: 0.6452 - accuracy: 0.7798 313/313 [==============================] - 2s 7ms/step - loss: 0.7835 - accuracy: 0.7292
print(f'Train Accuracy: {score_train[1]*100}')
print(f'Test Accuracy: {score_test[1]*100}')
Train Accuracy: 77.97799706459045 Test Accuracy: 72.9200005531311
many_layers = [Conv2D(32, (3, 3), input_shape=(32, 32, 3), padding='same', activation='relu'),
MaxPool2D(),
Conv2D(128, (3, 3), padding='same', activation='relu'),
MaxPool2D(),
Conv2D(512, (3, 3), padding='same', activation='relu'),
MaxPool2D(),
Flatten(),
Dense(256, activation='relu'),
Dense(10, activation='softmax'),
]
second_model = Sequential(many_layers)
second_model.summary()
Model: "sequential_1" ___________________________________________________________________________ Layer (type) Output Shape Param # =========================================================================== conv2d_3 (Conv2D) (None, 32, 32, 32) 896 max_pooling2d_3 (MaxPooling2D) (None, 16, 16, 32) 0 conv2d_4 (Conv2D) (None, 16, 16, 128) 36992 max_pooling2d_4 (MaxPooling2D) (None, 8, 8, 128) 0 conv2d_5 (Conv2D) (None, 8, 8, 512) 590336 max_pooling2d_5 (MaxPooling2D) (None, 4, 4, 512) 0 flatten_1 (Flatten) (None, 8192) 0 dense_2 (Dense) (None, 256) 2097408 dense_3 (Dense) (None, 10) 2570 =========================================================================== Total params: 2,728,202 Trainable params: 2,728,202 Non-trainable params: 0 ___________________________________________________________________________
CNN_layers = [Conv2D(32, (3, 3), input_shape=(32, 32, 3), padding='same',activation='relu'),
MaxPool2D(),
Conv2D(128, (3, 3), padding='same', activation='relu'),
MaxPool2D(),
Conv2D(512, (3, 3), padding='same', activation='relu'),
MaxPool2D(),
Flatten(),
]
FC_layers = [Dense(units=256, activation='relu'),
Dense(units=10, activation='softmax'),
]
third_model = Sequential(CNN_layers+FC_layers)
third_model.summary()
Model: "sequential_2" ___________________________________________________________________________ Layer (type) Output Shape Param # =========================================================================== conv2d_6 (Conv2D) (None, 32, 32, 32) 896 max_pooling2d_6 (MaxPooling2D) (None, 16, 16, 32) 0 conv2d_7 (Conv2D) (None, 16, 16, 128) 36992 max_pooling2d_7 (MaxPooling2D) (None, 8, 8, 128) 0 conv2d_8 (Conv2D) (None, 8, 8, 512) 590336 max_pooling2d_8 (MaxPooling2D) (None, 4, 4, 512) 0 flatten_2 (Flatten) (None, 8192) 0 dense_4 (Dense) (None, 256) 2097408 dense_5 (Dense) (None, 10) 2570 =========================================================================== Total params: 2,728,202 Trainable params: 2,728,202 Non-trainable params: 0 ___________________________________________________________________________
new_FC_layers = [Dense(1024, activation='relu'),
Dense(128, activation='relu'),
Dense(10, activation='softmax'),
]
fourth_model = Sequential(CNN_layers+new_FC_layers)
fourth_model.summary()
Model: "sequential_3" ___________________________________________________________________________ Layer (type) Output Shape Param # =========================================================================== conv2d_6 (Conv2D) (None, 32, 32, 32) 896 max_pooling2d_6 (MaxPooling2D) (None, 16, 16, 32) 0 conv2d_7 (Conv2D) (None, 16, 16, 128) 36992 max_pooling2d_7 (MaxPooling2D) (None, 8, 8, 128) 0 conv2d_8 (Conv2D) (None, 8, 8, 512) 590336 max_pooling2d_8 (MaxPooling2D) (None, 4, 4, 512) 0 flatten_2 (Flatten) (None, 8192) 0 dense_6 (Dense) (None, 1024) 8389632 dense_7 (Dense) (None, 128) 131200 dense_8 (Dense) (None, 10) 1290 =========================================================================== Total params: 9,150,346 Trainable params: 9,150,346 Non-trainable params: 0 ___________________________________________________________________________