TF2.keras 實現基於卷積神經網路的影象分類模型

Galois發表於2020-03-11

Keras 實現卷積神經網路

# 匯入包
import matplotlib as mpl
import matplotlib.pyplot as plt
%matplotlib inline
import numpy as np
import sklearn
import pandas as pd
import os
import sys
import time
import tensorflow as tf
from tensorflow import keras
# tensorflow: V2.1.0

# 資料集、分集
fashion_mnist = keras.datasets.fashion_mnist
(x_train_all, y_train_all), (x_test, y_test) = fashion_mnist.load_data()
x_valid, x_train = x_train_all[:5000], x_train_all[5000:]
y_valid, y_train = y_train_all[:5000], y_train_all[5000:]

print(x_valid.shape, y_valid.shape)
print(x_train.shape, y_train.shape)
print(x_test.shape, y_test.shape)

# 歸一化
from sklearn.preprocessing import StandardScaler
scaler = StandardScaler()
x_train_scaled = scaler.fit_transform(
    x_train.astype(np.float32).reshape(-1, 1)).reshape(-1, 28, 28, 1)
x_valid_scaled = scaler.transform(
    x_valid.astype(np.float32).reshape(-1, 1)).reshape(-1, 28, 28, 1)
x_test_scaled = scaler.transform(
    x_test.astype(np.float32).reshape(-1, 1)).reshape(-1, 28, 28, 1)

# 神經網路模型
model = keras.models.Sequential()
# 卷積層
model.add(keras.layers.Conv2D(filters=32, kernel_size=3,
                             padding='same',
                             activation='relu',
                             input_shape=(28, 28, 1)))
# 卷積層
model.add(keras.layers.Conv2D(filters=32, kernel_size=3,
                             padding='same',
                             activation='relu'))
# 池化層
model.add(keras.layers.MaxPool2D(pool_size=2))
# 卷積層
model.add(keras.layers.Conv2D(filters=64, kernel_size=3,
                             padding='same',
                             activation='relu'))
# 卷積層
model.add(keras.layers.Conv2D(filters=64, kernel_size=3,
                             padding='same',
                             activation='relu'))
# 池化層
model.add(keras.layers.MaxPool2D(pool_size=2))
# 卷積層
model.add(keras.layers.Conv2D(filters=128, kernel_size=3,
                             padding='same',
                             activation='relu'))
# 卷積層
model.add(keras.layers.Conv2D(filters=128, kernel_size=3,
                             padding='same',
                             activation='relu'))
# 池化層
model.add(keras.layers.MaxPool2D(pool_size=2))
# 展平
model.add(keras.layers.Flatten())
# 全連線層
model.add(keras.layers.Dense(128, activation='relu'))
# 輸出層
model.add(keras.layers.Dense(10, activation="softmax"))

model.compile(loss="sparse_categorical_crossentropy",
             optimizer = "sgd",
             metrics = ["accuracy"])

model.summary()

slt3b0f3kr.png!large

# 訓練模型
logdir = './cnn-callbacks'
if not os.path.exists(logdir):
    os.mkdir(logdir)
output_model_file = os.path.join(logdir,
                                 "fashion_mnist_model.h5")

callbacks = [
    keras.callbacks.TensorBoard(logdir),
    keras.callbacks.ModelCheckpoint(output_model_file,
                                   save_best_only = True),
    keras.callbacks.EarlyStopping(patience=5, min_delta=1e-3)
]
history = model.fit(x_train_scaled, y_train, epochs=10,
                    validation_data=(x_valid_scaled, y_valid),
                    callbacks = callbacks)

經過了幾分鐘的訓練:

8FoyKulcSr.png!large

# 學習曲線圖
def plot_learning_curves(history):
    pd.DataFrame(history.history).plot(figsize=(8, 5))
    plt.grid(True)
    plt.gca().set_ylim(0, 1)
plot_learning_curves(history)

qJJQxNxN1k.png!large

# 測試集驗證
model.evaluate(x_test_scaled, y_test)

輸出:

10000/10000 [==============================] - 4s 393us/sample - loss: 0.2668 - accuracy: 0.9028
[0.2668113784730434, 0.9028]

$ tensorboard --logdir=cnn-callbacks/

PX5Bzplg8G.png!large7jkUs7pGSV.png!large

需要注意以上每池化後的層級的 filters 都翻了 2 倍,這是因為池化大小為 2。

以上啟用函式用的是ReLU,可以試試SeLU

model = keras.models.Sequential()
# 卷積層
model.add(keras.layers.Conv2D(filters=32, kernel_size=3,
                             padding='same',
                             activation='selu',
                             input_shape=(28, 28, 1)))
# 卷積層
model.add(keras.layers.Conv2D(filters=32, kernel_size=3,
                             padding='same',
                             activation='selu'))
# 池化層
model.add(keras.layers.MaxPool2D(pool_size=2))
# 卷積層
model.add(keras.layers.Conv2D(filters=64, kernel_size=3,
                             padding='same',
                             activation='selu'))
# 卷積層
model.add(keras.layers.Conv2D(filters=64, kernel_size=3,
                             padding='same',
                             activation='selu'))
# 池化層
model.add(keras.layers.MaxPool2D(pool_size=2))
# 卷積層
model.add(keras.layers.Conv2D(filters=128, kernel_size=3,
                             padding='same',
                             activation='selu'))
# 卷積層
model.add(keras.layers.Conv2D(filters=128, kernel_size=3,
                             padding='same',
                             activation='selu'))
# 池化層
model.add(keras.layers.MaxPool2D(pool_size=2))
# 展平
model.add(keras.layers.Flatten())
# 全連線層
model.add(keras.layers.Dense(128, activation='selu'))

model.add(keras.layers.Dense(10, activation="softmax"))

model.compile(loss="sparse_categorical_crossentropy",
             optimizer = "sgd",
             metrics = ["accuracy"])

SeLU訓練結果:

tHBP2q6kr4.png!largegRr1vVvrbu.png!large

model.evaluate(x_test_scaled, y_test)

10000/10000 [==============================] - 4s 401us/sample - loss: 0.3093 - accuracy: 0.9099
[0.30925562893152236, 0.9099]

本作品採用《CC 協議》,轉載必須註明作者和本文連結

不要試圖用百米衝刺的方法完成馬拉松比賽。

相關文章