InceptionV4
1.1 來源
1.2 架構
1.3 特性
訓練過程
2.1 預訓練模型
2.2 設置Callbacks
2.3 設置訓練集
2.4 開始訓練模型
2.5 儲存模型與紀錄學習曲線
模型訓練結果
3.1 學習曲線
3.2 Accuracy與Loss
驗證模型準確度
4.1 程式碼
4.2 驗證結果
InceptionV4
1.1 來源
1.2 架構
完整架構:
圖片來自於:http://yeephycho.github.io/2016/08/31/A-reminder-of-algorithms-in-Convolutional-Neural-Networks-and-their-influences-III/
Stem:因Inception module在低維度特徵提取時,降低特徵圖尺寸的速度過快,可能會損失大量特徵信息,進一步影響模型效能,故以Stem取代一般卷積層。
Inception Module:
Reduction:與Stem的設計概念相同,避免急遽降低特徵圖的尺寸。理論依據可參考論文5. Efficient Grid Size Reduction
Reduction-A:
Reduction-B:
Dropout:在Softmax與Average Pooling間,加入Dropout(0.2),降低模型訓練過擬合的可能性。
1.3 特性
訓練過程:
2.1 預訓練模型
# IMPORT MODULES
from keras.callbacks import EarlyStopping, ModelCheckpoint, ReduceLROnPlateau
from keras.layers import Input, Dense, GlobalAveragePooling2D
from keras.preprocessing.image import ImageDataGenerator
from keras.optimizers import Adam
import matplotlib.pyplot as plt
from keras.models import Model
from keras.applications import InceptionResNetV2
# -----------------------------1.客製化模型--------------------------------
# 載入keras模型(更換輸出圖片尺寸)
model = create_model(num_classes=800, dropout_prob=0.2,
weights=None, include_top=True)
# 編譯模型
model.compile(optimizer=Adam(lr=0.001),
loss='categorical_crossentropy',
metrics=['accuracy'])
2.2 設置Callbacks
# ---------------------------2.設置callbacks----------------------------
# 設定earlystop條件
estop = EarlyStopping(monitor='val_loss', patience=15, mode='min', verbose=1)
# 設定模型儲存條件
checkpoint = ModelCheckpoint('InceptionV4_checkpoint_v2.h5', verbose=1,
monitor='val_loss', save_best_only=True,
mode='min')
# 設定lr降低條件
reduce_lr = ReduceLROnPlateau(monitor='val_loss', factor=0.5,
patience=5, mode='min', verbose=1,
min_lr=1e-6)
2.3 設置訓練集
# -----------------------------3.設置資料集--------------------------------
# 設定ImageDataGenerator參數(路徑、批量、圖片尺寸)
train_dir = './workout/train/'
valid_dir = './workout/val/'
test_dir = './workout/test/'
batch_size = 64
target_size = (120, 120)
# 設定批量生成器
train_datagen = ImageDataGenerator(rescale=1./255,
rotation_range=20,
width_shift_range=0.2,
height_shift_range=0.2,
shear_range=0.2,
zoom_range=0.5,
fill_mode="nearest")
val_datagen = ImageDataGenerator(rescale=1./255)
test_datagen = ImageDataGenerator(rescale=1./255)
# 讀取資料集+批量生成器,產生每epoch訓練樣本
train_generator = train_datagen.flow_from_directory(train_dir,
target_size=target_size,
batch_size=batch_size)
valid_generator = val_datagen.flow_from_directory(valid_dir,
target_size=target_size,
batch_size=batch_size)
test_generator = test_datagen.flow_from_directory(test_dir,
target_size=target_size,
batch_size=batch_size,
shuffle=False)
2.4 重新訓練模型權重
# -----------------------------4.開始訓練模型------------------------------
# 重新訓練權重
history = model.fit_generator(train_generator,
epochs=50, verbose=1,
steps_per_epoch=train_generator.samples//batch_size,
validation_data=valid_generator,
validation_steps=valid_generator.samples//batch_size,
callbacks=[checkpoint, estop, reduce_lr])
2.5 儲存模型與紀錄學習曲線
# -----------------------5.儲存模型、紀錄學習曲線------------------------
# 儲存模型
model.save('./InceptionV4_retrained_v2.h5')
print('已儲存InceptionV2_retrained_v2.h5')
# 畫出acc學習曲線
acc = history.history['accuracy']
epochs = range(1, len(acc) + 1)
val_acc = history.history['val_accuracy']
plt.plot(epochs, acc, 'bo', label='Training acc')
plt.plot(epochs, val_acc, 'r', label='Validation acc')
plt.title('Training and validation accuracy')
plt.legend(loc='lower right')
plt.grid()
# 儲存acc學習曲線
plt.savefig('./acc.png')
plt.show()
# 畫出loss學習曲線
loss = history.history['loss']
val_loss = history.history['val_loss']
plt.plot(epochs, loss, 'bo', label='Training loss')
plt.plot(epochs, val_loss, 'r', label='Validation loss')
plt.title('Training and validation loss')
plt.legend(loc='upper right')
plt.grid()
# 儲存loss學習曲線
plt.savefig('loss.png')
plt.show()
模型訓練結果
3.1 訓練epochs:28 epochs。
3.2 耗費時間:3小時44分28秒(13468秒)。
3.3 學習曲線
3.4 Accuary與Loss
驗證準確度
4.1 程式碼
# -------------------------6.驗證模型準確度--------------------------
# 以vali資料夾驗證模型準確度
test_loss, test_acc = model.evaluate_generator(test_generator,
steps=test_generator.samples//batch_size,
verbose=1)
print('test acc:', test_acc)
print('test loss:', test_loss)
4.2 驗證結果
下一章目標是:介紹第五個預訓練模型InceptionResNetV2,與分享訓練成果」。
讓我們繼續看下去...