資料集預處理
1.1 ImageDataGenerator
1.2 flow_from_directory
Callbacks API
2.1 EarlyStopping
2.2 ModelCheckpoint
2.3 Callbacks
資料集預處理
1.1 簡介:
1.2 ImageDataGenerator
# 設定批量生成器
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)
參數說明
圖片來自於:https://medium.com/ai%E5%8F%8D%E6%96%97%E5%9F%8E/preprocessing-data-image-data-augmentation%E5%AF%A6%E4%BD%9C%E8%88%87%E5%8F%83%E6%95%B8%E8%AA%AA%E6%98%8E-d05f2ed24194
1.3 flow_from_directory
# 讀取資料集+批量生成器,產生每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.1 簡介:
2.2 EarlyStopping
# 設定earlystop條件
estop = EarlyStopping(monitor='val_loss', patience=10,
mode='min', verbose=1)
參數說明:
2.3 ModelCheckpoint
# 設定模型儲存條件(儲存最佳模型)
checkpoint = ModelCheckpoint('Densenet201_checkpoint_v2.h5', verbose=1,
monitor='val_loss', save_best_only=True,
mode='min')
參數說明:
2.4 Callbacks
# 重新訓練模型權重(以callbacks呼叫checkpoint、estop、reduce_lr)
history = model.fit_generator(train_generator,
epochs=500, 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])
參數說明:
steps_per_epoch = 資料集樣本數 除以 batch size
下一章,目標是:「和大家簡單介紹Learning rate,並比較3種Learning rate的策略與優劣」。
讓我們繼續看下去...