-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtransfer_inception.py
More file actions
228 lines (186 loc) · 7.43 KB
/
transfer_inception.py
File metadata and controls
228 lines (186 loc) · 7.43 KB
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
import os
import sys
import glob
import argparse
import matplotlib.pyplot as plt
import shutil
import numpy as np
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
IMAGE_WIDTH, IMAGE_HEIGHT = 299, 299 #fixed size for InceptionV3
NB_EPOCHS = 20
BAT_SIZE = 50
FC_SIZE = 1024
NB_IV3_LAYERS_TO_FREEZE = 172
ALL_DATA_FILEPATH = './data/dataset-original/'
TRAINING_DATA_FILEPATH = './data/training/'
VALIDATION_DATA_FILEPATH = './data/validation/'
VALIDATION_SPLIT = 0.25
def split_dataset_into_test_and_train_sets(all_data_dir, training_data_dir, testing_data_dir, testing_data_pct):
# Recreate testing and training directories
if testing_data_dir.count('/') > 1:
shutil.rmtree(testing_data_dir, ignore_errors=False)
os.makedirs(testing_data_dir)
print("Successfully cleaned directory " + testing_data_dir)
else:
print("Refusing to delete testing data directory " + testing_data_dir + " as we prevent you from doing stupid things!")
if training_data_dir.count('/') > 1:
shutil.rmtree(training_data_dir, ignore_errors=False)
os.makedirs(training_data_dir)
print("Successfully cleaned directory " + training_data_dir)
else:
print("Refusing to delete testing data directory " + training_data_dir + " as we prevent you from doing stupid things!")
num_training_files = 0
num_testing_files = 0
for subdir, dirs, files in os.walk(all_data_dir):
category_name = os.path.basename(subdir)
# Don't create a subdirectory for the root directory
print(category_name + " vs " + os.path.basename(all_data_dir))
if category_name == os.path.basename(all_data_dir):
continue
training_data_category_dir = training_data_dir + '/' + category_name
testing_data_category_dir = testing_data_dir + '/' + category_name
if not os.path.exists(training_data_category_dir):
os.mkdir(training_data_category_dir)
if not os.path.exists(testing_data_category_dir):
os.mkdir(testing_data_category_dir)
for file in files:
input_file = os.path.join(subdir, file)
if np.random.rand(1) < testing_data_pct:
shutil.copy(input_file, testing_data_dir + '/' + category_name + '/' + file)
num_testing_files += 1
else:
shutil.copy(input_file, training_data_dir + '/' + category_name + '/' + file)
num_training_files += 1
print("Processed " + str(num_training_files) + " training files.")
print("Processed " + str(num_testing_files) + " testing files.")
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 + "/*")))
return cnt
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'])
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(input=base_model.input, output=predictions)
return model
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'])
def train(args):
"""Use transfer learning and fine-tuning to train a network on a new dataset"""
nb_train_samples = get_nb_files(TRAINING_DATA_FILEPATH)
nb_classes = len(glob.glob(TRAINING_DATA_FILEPATH + "/*"))
nb_val_samples = get_nb_files(VALIDATION_DATA_FILEPATH)
nb_epoch = int(args.nb_epoch)
batch_size = int(args.batch_size)
# split_dataset_into_test_and_train_sets(ALL_DATA_FILEPATH, TRAINING_DATA_FILEPATH, VALIDATION_DATA_FILEPATH, VALIDATION_SPLIT)
# 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(
TRAINING_DATA_FILEPATH,
target_size=(IMAGE_WIDTH, IMAGE_HEIGHT),
batch_size=batch_size,
)
validation_generator = test_datagen.flow_from_directory(
VALIDATION_DATA_FILEPATH,
target_size=(IMAGE_WIDTH, IMAGE_HEIGHT),
batch_size=batch_size,
)
# setup model
base_model = InceptionV3(weights='imagenet', include_top=False) #include_top=False excludes final FC layer
model = add_new_last_layer(base_model, nb_classes)
# transfer learning
setup_to_transfer_learn(model, base_model)
history_tl = model.fit_generator(
train_generator,
nb_epoch=nb_epoch,
samples_per_epoch=nb_train_samples,
validation_data=validation_generator,
validation_steps=nb_val_samples/BAT_SIZE,
class_weight='auto',
verbose=1)
# fine-tuning
# setup_to_finetune(model)
#
# history_ft = model.fit_generator(
# train_generator,
# samples_per_epoch=nb_train_samples,
# nb_epoch=nb_epoch,
# validation_data=validation_generator,
# nb_val_samples=nb_val_samples,
# class_weight='auto')
model.save(args.output_model_file)
#if args.plot:
# plot_training(history_tl)
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()
if __name__=="__main__":
a = argparse.ArgumentParser()
# a.add_argument("--train_dir")
# a.add_argument("--val_dir")
a.add_argument("--nb_epoch", default=NB_EPOCHS)
a.add_argument("--batch_size", default=BAT_SIZE)
a.add_argument("--output_model_file", default="inceptionv3-ft.model")
a.add_argument("--plot", action="store_true")
args = a.parse_args()
train(args)