-
Notifications
You must be signed in to change notification settings - Fork 0
/
finetune_vgg19.py
60 lines (47 loc) · 1.76 KB
/
finetune_vgg19.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import config
import models
import keras
import os
import numpy as np
import pickle
from keras.layers import *
from keras.models import Model, Sequential
from keras.utils import plot_model
from keras.preprocessing.image import ImageDataGenerator
from keras.callbacks import History, EarlyStopping, ModelCheckpoint
from keras.optimizers import Adam
input_shape = (224, 224)
batch_size = 32
train_gen = models.getTrainData(batch_size, data_aug=True, target_size=input_shape)
val_gen = models.getValData(batch_size, data_aug=True, target_size=input_shape)
test_gen = models.getTestData(target_size = input_shape)
model = models.getVGG19()
model.load_weights('trained/vgg19_best.hdf5')
# set the top 8 layers trainable, fine tune these with very little training rate
for layer in model.layers[-12:]:
layer.trainable = True
model.compile(
loss=keras.losses.categorical_crossentropy,
optimizer=Adam(lr=2e-6),
metrics=['accuracy'])
filename = model.name + "_fine_tune_top_8.hdf5"
checkpoint = ModelCheckpoint(config.trained_dir + filename, monitor='val_acc', verbose=1, save_best_only=True, mode='max')
history = History()
early_stopping = EarlyStopping(monitor='val_acc', min_delta=0.002, patience=30, verbose=0, mode='auto')
callbacks_list = [checkpoint, history, early_stopping]
model.fit_generator(
train_gen,
steps_per_epoch=train_gen.n // batch_size,
epochs=500,
validation_steps=val_gen.n // batch_size,
callbacks=callbacks_list,
validation_data=val_gen,
)
filename = model.name + "_fine_tune_top_8_history"
with open(config.trained_dir + filename, 'wb') as file_pi:
pickle.dump(history.history, file_pi)
print("Complete training.\n")
print("Metrics: ")
print(model.metrics_names)
met = model.evaluate_generator(generator=test_gen, use_multiprocessing=True, workers=6)
print(met)