——Tesla M40 + singularity + keras2 + jupyter notebook
- 硬件:Nvidia Tesla M40 顯存24GB
- 軟件環(huán)境:CentOS 7 + singularity + jupyter notebook
- 深度學(xué)習(xí)框架:keras2 ( 底層 tensorflow-gpu 1.2.0 )
- 語言環(huán)境:python 3.5
本案例也可以用 Ubuntu + keras2 或 Ubuntu + docker + keras2 實(shí)現(xiàn)索赏,當(dāng)然最好使用 GPU 跑, CPU 太慢。因此對(duì)環(huán)境的要求就是只要能跑起來 keras 調(diào)用 GPU 就 OK 了茂卦。
遷移學(xué)習(xí)的定義:在 ImageNet 已經(jīng)得到一個(gè)預(yù)訓(xùn)練好的 ConvNet 網(wǎng)絡(luò)痘绎,刪除網(wǎng)絡(luò)的最后一個(gè)全連接層弧腥,然后將 ConvNet 網(wǎng)絡(luò)的剩余部分作為新數(shù)據(jù)集的特征提取層本辐。一旦你提取了所有圖像的特征,就可以開始訓(xùn)練新數(shù)據(jù)集分類器赖淤。
微調(diào):更換并重新訓(xùn)練 ConvNet 的網(wǎng)絡(luò)層蜀漆,還可以通過反向傳播算法對(duì)預(yù)訓(xùn)練網(wǎng)絡(luò)的權(quán)重進(jìn)行微調(diào)。
在 jupyter notebook 命令行下咱旱,首先引入
%matplotlib inline
import os
import sys
import glob
# import argparse #這個(gè)模塊是命令行參數(shù)傳入确丢,在nb中不需要
import matplotlib.pyplot as plt
可以使用 jupyter notebook绷耍,也可以直接在編輯器(sublime text , Atom , pycharm 等)中編輯然后在終端用命令行運(yùn)行。
本文用 notebook蠕嫁,如果想在終端來運(yùn)行,就調(diào)用 argparse 模塊毯盈,運(yùn)行時(shí)給主函數(shù)傳入?yún)?shù)剃毒。
引入一些必要的模塊
from keras import __version__
from keras.applications.inception_v3 import InceptionV3, preprocess_input
from keras.models import Model
from keras.layers import Dense, GlobalAveragePooling2D
from keras.preprocessing.image import ImageDataGenerator
from keras.optimizers import SGD
引入 Inception V3 的 模型,第一次使用時(shí)搂赋,首先下載赘阀,大約88MB,會(huì)保存在 ~/.keras/models 下脑奠,以后再用就不用下載基公。
定義一些全局變量,這些全局變量是可以通過 argparse 模塊從命令行獲取宋欺,傳遞給主函數(shù)轰豆。在notebook中,調(diào)用主函數(shù)的時(shí)候直接傳遞給主函數(shù)齿诞。注意 keras2 中已經(jīng)將 nb_epochs 修改為 epochs 了酸休。
定義全連接層數(shù)為 FC_SIZE 變量(遷移學(xué)習(xí)需要傳遞的參數(shù)),定義凍結(jié)層數(shù)為 NB_IV3_LAYERS_TO_FREEZE 變量(微調(diào)需要傳遞的參數(shù))祷杈。
IM_WIDTH, IM_HEIGHT = 299, 299 #修正 InceptionV3 的尺寸參數(shù)
EPOCHS = 10
BAT_SIZE = 40
FC_SIZE = 1024
NB_IV3_LAYERS_TO_FREEZE = 172
# 定義一個(gè)方法——獲取訓(xùn)練集和驗(yàn)證集中的樣本數(shù)量斑司,即nb_train_samples,nb_val_samples
def get_nb_files(directory):
"""Get number of files by searching directory recursively"""
if not os.path.exists(directory):
return 0
cnt = 0
for r, dirs, files in os.walk(directory):
for dr in dirs:
cnt += len(glob.glob(os.path.join(r, dr + "/*"))) # glob模塊是用來查找匹配文件的,后面接匹配規(guī)則但汞。
return cnt
定義遷移學(xué)習(xí)函數(shù)宿刮,凍結(jié)所有的 base_model 層,不訓(xùn)練私蕾。
# 定義遷移學(xué)習(xí)的函數(shù)僵缺,不需要訓(xùn)練的部分。
def setup_to_transfer_learn(model, base_model):
"""Freeze all layers and compile the model"""
for layer in base_model.layers:
layer.trainable = False
model.compile(optimizer='rmsprop', loss='categorical_crossentropy', metrics=['accuracy'])
定義增加最后一個(gè)全連接層的函數(shù)踩叭,1024層
# 定義增加最后一個(gè)全連接層的函數(shù)
def add_new_last_layer(base_model, nb_classes):
"""Add last layer to the convnet
Args:
base_model: keras model excluding top
nb_classes: # of classes
Returns:
new keras model with last layer
"""
x = base_model.output
x = GlobalAveragePooling2D()(x)
x = Dense(FC_SIZE, activation='relu')(x) #new FC layer, random init
predictions = Dense(nb_classes, activation='softmax')(x) #new softmax layer
model = Model(inputs=base_model.input, outputs=predictions)
return model
定義微調(diào)函數(shù)谤饭,凍結(jié)172層之前的層
# 定義微調(diào)函數(shù)
def setup_to_finetune(model):
"""Freeze the bottom NB_IV3_LAYERS and retrain the remaining top layers.
note: NB_IV3_LAYERS corresponds to the top 2 inception blocks in the inceptionv3 arch
Args:
model: keras model
"""
for layer in model.layers[:NB_IV3_LAYERS_TO_FREEZE]:
layer.trainable = False
for layer in model.layers[NB_IV3_LAYERS_TO_FREEZE:]:
layer.trainable = True
model.compile(optimizer=SGD(lr=0.0001, momentum=0.9), loss='categorical_crossentropy', metrics=['accuracy'])
訓(xùn)練結(jié)束后畫 acc-loss 圖,查看訓(xùn)練效果懊纳。
def plot_training(history):
acc = history.history['acc']
val_acc = history.history['val_acc']
loss = history.history['loss']
val_loss = history.history['val_loss']
epochs = range(len(acc))
plt.plot(epochs, acc, 'r.')
plt.plot(epochs, val_acc, 'r')
plt.title('Training and validation accuracy')
plt.figure()
plt.plot(epochs, loss, 'r.')
plt.plot(epochs, val_loss, 'r-')
plt.title('Training and validation loss')
plt.show()
主函數(shù)揉抵,傳入一些參數(shù)。將圖片處理的代碼也放在主函數(shù)中嗤疯。
def train(train_dir, val_dir, epochs=EPOCHS, batch_size=BAT_SIZE, output_model_file="inceptionv3_25000.model"):
"""Use transfer learning and fine-tuning to train a network on a new dataset"""
nb_train_samples = get_nb_files(train_dir)
nb_classes = len(glob.glob(train_dir + "/*"))
nb_val_samples = get_nb_files(val_dir)
epochs = int(epochs)
batch_size = int(batch_size)
# data prep
train_datagen = ImageDataGenerator(
preprocessing_function=preprocess_input,
rotation_range=30,
width_shift_range=0.2,
height_shift_range=0.2,
shear_range=0.2,
zoom_range=0.2,
horizontal_flip=True
)
test_datagen = ImageDataGenerator(
preprocessing_function=preprocess_input,
rotation_range=30,
width_shift_range=0.2,
height_shift_range=0.2,
shear_range=0.2,
zoom_range=0.2,
horizontal_flip=True
)
train_generator = train_datagen.flow_from_directory(
train_dir,
target_size=(IM_WIDTH, IM_HEIGHT),
batch_size=batch_size,
)
validation_generator = test_datagen.flow_from_directory(
val_dir,
target_size=(IM_WIDTH, IM_HEIGHT),
batch_size=batch_size,
)
# 準(zhǔn)備跑起來冤今,首先給 base_model 和 model 賦值,遷移學(xué)習(xí)和微調(diào)都是使用 InceptionV3 的 notop 模型(看 inception_v3.py 源碼茂缚,此模型是打開了最后一個(gè)全連接層)戏罢,利用 add_new_last_layer 函數(shù)增加最后一個(gè)全連接層屋谭。
base_model = InceptionV3(weights='imagenet', include_top=False) #include_top=False excludes final FC layer
model = add_new_last_layer(base_model, nb_classes)
print "開始遷移學(xué)習(xí):\n"
# transfer learning
setup_to_transfer_learn(model, base_model)
history_tl = model.fit_generator(
train_generator,
steps_per_epoch=nb_train_samples // batch_size,
epochs=epochs,
validation_data=validation_generator,
validation_steps=nb_val_samples // batch_size,
class_weight='auto')
print "開始微調(diào):\n"
# fine-tuning
setup_to_finetune(model)
history_ft = model.fit_generator(
train_generator,
steps_per_epoch=nb_train_samples // batch_size,
epochs=epochs,
validation_data=validation_generator,
validation_steps=nb_val_samples // batch_size,
class_weight='auto')
model.save(output_model_file)
plot_training(history_ft)
# train(train_dir, val_dir, epochs=EPOCHS, batch_size=BAT_SIZE, output_model_file="inceptionv3_nbs.model")
train("./data/train", "./data/validation")
Found 20000 images belonging to 2 classes.Found 5000 images belonging to 2 classes.Epoch 1/10
500/500 [==============================] - 364s - loss: 0.8835 - acc: 0.8754 - val_loss: 0.0983 - val_acc: 0.9596
Epoch 2/10
500/500 [==============================] - 350s - loss: 0.1555 - acc: 0.9423 - val_loss: 0.1182 - val_acc: 0.9570
Epoch 3/10
500/500 [==============================] - 348s - loss: 0.1257 - acc: 0.9497 - val_loss: 0.0827 - val_acc: 0.9686
Epoch 4/10
500/500 [==============================] - 349s - loss: 0.1244 - acc: 0.9531 - val_loss: 0.0774 - val_acc: 0.9656
Epoch 5/10
500/500 [==============================] - 348s - loss: 0.1112 - acc: 0.9589 - val_loss: 0.1371 - val_acc: 0.9506
Epoch 6/10
500/500 [==============================] - 347s - loss: 0.1082 - acc: 0.9590 - val_loss: 0.0708 - val_acc: 0.9732
Epoch 7/10
500/500 [==============================] - 350s - loss: 0.1078 - acc: 0.9601 - val_loss: 0.0730 - val_acc: 0.9712
Epoch 8/10
500/500 [==============================] - 351s - loss: 0.1055 - acc: 0.9617 - val_loss: 0.1071 - val_acc: 0.9650
Epoch 9/10
500/500 [==============================] - 351s - loss: 0.1028 - acc: 0.9638 - val_loss: 0.1173 - val_acc: 0.9580
Epoch 10/10
500/500 [==============================] - 353s - loss: 0.1036 - acc: 0.9611 - val_loss: 0.0654 - val_acc: 0.9748
Epoch 1/10
500/500 [==============================] - 363s - loss: 0.0712 - acc: 0.9741 - val_loss: 0.0720 - val_acc: 0.9770
Epoch 2/10
500/500 [==============================] - 357s - loss: 0.0587 - acc: 0.9779 - val_loss: 0.0566 - val_acc: 0.9756
Epoch 3/10
500/500 [==============================] - 360s - loss: 0.0555 - acc: 0.9781 - val_loss: 0.0561 - val_acc: 0.9798
Epoch 4/10
500/500 [==============================] - 360s - loss: 0.0518 - acc: 0.9795 - val_loss: 0.0580 - val_acc: 0.9796
Epoch 5/10
500/500 [==============================] - 360s - loss: 0.0458 - acc: 0.9815 - val_loss: 0.0509 - val_acc: 0.9826
Epoch 6/10
500/500 [==============================] - 361s - loss: 0.0458 - acc: 0.9827 - val_loss: 0.0491 - val_acc: 0.9792
Epoch 7/10
500/500 [==============================] - 363s - loss: 0.0457 - acc: 0.9810 - val_loss: 0.0538 - val_acc: 0.9816
Epoch 8/10
500/500 [==============================] - 362s - loss: 0.0490 - acc: 0.9809 - val_loss: 0.0489 - val_acc: 0.9820
Epoch 9/10
500/500 [==============================] - 363s - loss: 0.0388 - acc: 0.9847 - val_loss: 0.0530 - val_acc: 0.9830
Epoch 10/10
500/500 [==============================] - 365s - loss: 0.0390 - acc: 0.9849 - val_loss: 0.0419 - val_acc: 0.9842