-
Notifications
You must be signed in to change notification settings - Fork 4
/
models.py
385 lines (359 loc) · 14 KB
/
models.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
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
import os
import numpy as np
import config
import utils
import data
from keras.preprocessing.image import ImageDataGenerator
from keras.models import Model, Sequential
from keras.layers import Dense, Flatten, Dropout
from keras.optimizers import SGD
from keras.callbacks import EarlyStopping, ModelCheckpoint, ReduceLROnPlateau
class TransferModel(object):
def __init__(self,
base_model=None,
fc_layer_size=2048,
classes=None,
freeze_layers_num=None):
if not base_model:
base_model = config.model
assert utils.is_keras_pretrained_model(base_model)
self.base_model = base_model
self.input_shape = utils.get_input_shape(self.base_model)
self.fc_layer_size = fc_layer_size
if classes is None:
classes = config.classes
self.classes = classes
self.output_dim = len(classes)
self.image_size = config.target_size_dict[base_model]
if freeze_layers_num is None:
freeze_layers_num = 80
self.freeze_layers_num = freeze_layers_num
self.model_weights_path = config.get_transfer_model_weights_path(
base_model)
self.model_path = config.get_transfer_model_path(base_model)
self.preprocess_fun = data.preprocess_input_wrapper(self.base_model)
self._create()
def _create(self):
model = utils.get_pretrained_model(
self.base_model,
include_top=False,
input_shape=self.input_shape)
interm = model.output
interm = Flatten()(interm)
interm = Dropout(0.5)(interm)
interm = Dense(self.fc_layer_size, activation='relu')(interm)
interm = Dropout(0.5)(interm)
output = Dense(len(self.classes), activation='softmax')(interm)
self.model = Model(inputs=model.input, outputs=output)
def fit(self,
x=None,
y=None,
batch_size=32,
epochs=250,
verbose=1,
callbacks=None,
validation_data=None,
lr=1e-4,
**kwargs):
self.freeze_top_layers(self.model, self.freeze_layers_num)
if x is None:
x_train_path = config.get_x_train_path(self.base_model)
x = utils.load_h5file(x_train_path)
if y is None:
y_train_path = config.y_train_path
y = utils.load_h5file(y_train_path)
if callbacks is None:
callbacks = self.get_callbacks(
self.model_weights_path,
patience=30)
if validation_data is None:
x_valid_path = config.get_x_valid_path(self.base_model)
x_valid = utils.load_h5file(x_valid_path)
y_valid_path = config.y_valid_path
y_valid = utils.load_h5file(y_valid_path)
validation_data = (x_valid, y_valid)
self.model.compile(
loss='categorical_crossentropy',
optimizer=SGD(lr=lr, momentum=0.9),
metrics=['accuracy'])
self.model.fit(
x, y,
batch_size=batch_size,
epochs=epochs,
verbose=verbose,
callbacks=callbacks,
validation_data=validation_data,
**kwargs)
def fit_generator(self,
generator=None,
steps_per_epoch=None,
epochs=250,
verbose=1,
callbacks=None,
validation_data=None,
validation_steps=None,
lr=1e-4,
batch_size=32,
source='path',
**kwargs):
self.freeze_top_layers(self.model, self.freeze_layers_num)
assert source in {'path', 'tensor'}
if generator is None:
datagen_train = ImageDataGenerator(
preprocessing_function=self.preprocess_fun,
rotation_range=30.,
width_shift_range=0.2,
height_shift_range=0.2,
shear_range=0.2,
zoom_range=0.2,
horizontal_flip=True)
x_train_path = config.get_x_train_path(self.base_model)
y_train_path = config.y_train_path
if (source == 'tensor' and
os.path.exists(x_train_path) and
os.path.exists(y_train_path)):
x_train = utils.load_h5file(x_train_path)
y_train = utils.load_h5file(y_train_path)
generator = datagen_train.flow(x_train, y_train, batch_size)
n_train = len(x_train)
else:
generator = datagen_train.flow_from_directory(
config.train_dir,
target_size=self.image_size,
batch_size=batch_size)
n_train = len(utils.images_under_subdirs(
config.train_dir,
subdirs=self.classes))
if not steps_per_epoch:
steps_per_epoch = utils.ceildiv(n_train, batch_size)
if steps_per_epoch is None:
steps_per_epoch = 500
if not callbacks:
callbacks = self.get_callbacks(
self.model_weights_path,
patience=50)
if validation_data is None:
datagen_valid = ImageDataGenerator(
preprocessing_function=self.preprocess_fun)
x_valid_path = config.get_x_valid_path(self.base_model)
y_valid_path = config.y_valid_path
if (source == 'tensor' and
os.path.exists(x_valid_path) and
os.path.exists(y_valid_path)):
x_valid = utils.load_h5file(x_valid_path)
y_valid = utils.load_h5file(y_valid_path)
validation_data = datagen_valid.flow(
x_valid, y_valid, batch_size)
n_valid = len(x_valid)
else:
validation_data = datagen_valid.flow_from_directory(
config.valid_dir,
target_size=self.image_size,
batch_size=batch_size)
n_valid = len(utils.images_under_subdirs(
config.valid_dir,
subdirs=self.classes))
if not validation_steps:
validation_steps = utils.ceildiv(n_valid, batch_size)
if validation_steps is None:
validation_steps = 100
self.model.compile(
loss='categorical_crossentropy',
optimizer=SGD(lr=lr, momentum=0.9),
metrics=['accuracy'])
self.model.fit_generator(
generator,
steps_per_epoch=steps_per_epoch,
epochs=epochs,
validation_data=validation_data,
validation_steps=validation_steps,
callbacks=callbacks,
verbose=verbose,
**kwargs)
def evaluate(self, *args, **kwargs):
return self.model.evaluate(*args, **kwargs)
def predict(self, *args, **kwargs):
return self.model.predict(*args, **kwargs)
def predict_from_path(self, image_path):
tensor = data.path_to_tensor(image_path, target_size=self.target_size)
tensor = self.preprocess_fun(tensor)
pred = self.predict(tensor)
return pred
def predict_from_url(self, url):
image_path = utils.url2file(url)
pred = self.predict_from_path(image_path)
return pred
def save_model(self):
self.model.save(self.model_path)
def load_weights(self, weights_path=None):
if not weights_path:
weights_path = self.weights_top_model_path
self.model.load_weights(weights_path)
def load_weights_from_top_model(self, top_model_weights_path=None):
if top_model_weights_path is None:
top_model_weights_path = config.get_top_model_weights_path(
self.base_model)
pretrained_model = utils.get_pretrained_model(
self.base_model,
include_top=False,
input_shape=self.input_shape)
top_model = TopModel(
base_model=self.base_model,
fc_layer_size=self.fc_layer_size,
classes=self.classes)
top_model.load_weights(top_model_weights_path)
model = Model(
inputs=pretrained_model.input,
outputs=top_model.model(pretrained_model.output))
self.model = model
@staticmethod
def make_model_layers_nontrainable(model):
for layer in model.layers:
layer.trainable = False
@staticmethod
def freeze_top_layers(model, freeze_layers_num):
num_layers = len(model.layers)
if freeze_layers_num >= 0 and freeze_layers_num <= num_layers:
for layer in model.layers[:freeze_layers_num]:
layer.trainable = False
for layer in model.layers[freeze_layers_num:]:
layer.trainable = True
@staticmethod
def get_callbacks(weights_path,
monitor='val_loss',
patience=40,
patience_lr=20):
early_stopping = EarlyStopping(
verbose=1,
patience=patience,
monitor=monitor)
model_checkpoint = ModelCheckpoint(
weights_path,
verbose=1,
save_best_only=True,
save_weights_only=True,
monitor=monitor)
reduce_lr = ReduceLROnPlateau(
monitor=monitor, factor=0.2, patience=patience_lr, min_lr=1e-8)
return [early_stopping, model_checkpoint, reduce_lr]
class TopModel(object):
"""
An image classification model that is built on bottleneck features of a
pre-trained model chosen from
- 'inception_v3',
- 'mobilenet',
- 'resnet50',
- 'resnet101',
- 'resnet152',
- 'vgg16',
- 'vgg19',
- 'xception'
"""
def __init__(self,
base_model=None,
fc_layer_size=2048,
classes=None):
self.fc_layer_size = fc_layer_size
if not base_model:
base_model = config.model
assert utils.is_keras_pretrained_model(base_model)
self.base_model = base_model
if classes is None:
classes = config.classes
self.classes = np.array(classes)
self.output_dim = len(classes)
self.image_size = config.target_size_dict[base_model]
self.model_weights_path = config.get_top_model_weights_path(base_model)
self.model_path = config.get_top_model_path(base_model)
self.preprocess_fun = data.preprocess_input_wrapper(self.base_model)
self._create()
def _create(self):
pretrained_model = utils.get_pretrained_model(
self.base_model,
include_top=False,
input_shape=utils.get_input_shape(self.base_model))
self.pretrained_model = pretrained_model
input_shape = [int(ele) for ele in pretrained_model.output.shape[1:]]
model = Sequential()
model.add(Flatten(input_shape=input_shape))
model.add(Dropout(0.5))
model.add(Dense(self.fc_layer_size, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(self.output_dim, activation='softmax'))
self.model = model
def fit(self,
x=None,
y=None,
batch_size=32,
epochs=250,
verbose=1,
callbacks=None,
validation_data=None,
lr=1e-3,
**kwargs):
if x is None:
x_train_path = config.get_bf_train_path(self.base_model)
x = utils.load_h5file(x_train_path)
if y is None:
y_train_path = config.y_train_path
y = utils.load_h5file(y_train_path)
if callbacks is None:
callbacks = self.get_callbacks(
self.model_weights_path,
patience=30)
if validation_data is None:
x_valid_path = config.get_bf_valid_path(self.base_model)
x_valid = utils.load_h5file(x_valid_path)
y_valid_path = config.y_valid_path
y_valid = utils.load_h5file(y_valid_path)
validation_data = (x_valid, y_valid)
self.model.compile(
loss='categorical_crossentropy',
optimizer=SGD(lr=lr, momentum=0.9),
metrics=['accuracy'])
self.model.fit(
x, y,
batch_size=batch_size,
epochs=epochs,
verbose=verbose,
callbacks=callbacks,
validation_data=validation_data,
**kwargs)
def evaluate(self, *args, **kwargs):
return self.model.evaluate(*args, **kwargs)
def predict(self, *args, **kwargs):
return self.model.predict(*args, **kwargs)
def save_model(self):
self.model.save(self.model_path)
def load_weights(self, weights_path=None):
if not weights_path:
weights_path = self.model_weights_path
self.model.load_weights(weights_path)
def predict_from_path(self, image_path):
tensor = data.path_to_tensor(image_path, target_size=self.target_size)
tensor = self.preprocess_fun(tensor)
bf = self.pretrained_model.predict(tensor)
pred = self.predict(bf)
return pred
def predict_from_url(self, url):
image_path = utils.url2file(url)
pred = self.predict_from_path(image_path)
return pred
@staticmethod
def get_callbacks(weights_path,
monitor='val_loss',
patience=30,
patience_lr=10):
early_stopping = EarlyStopping(
verbose=1,
patience=patience,
monitor=monitor)
model_checkpoint = ModelCheckpoint(
weights_path,
verbose=1,
save_best_only=True,
save_weights_only=True,
monitor=monitor)
reduce_lr = ReduceLROnPlateau(
monitor=monitor, factor=0.2, patience=patience_lr, min_lr=1e-8)
return [early_stopping, model_checkpoint, reduce_lr]