forked from ThalesGroup/DRBox_keras
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathTraining_DRBox.py
331 lines (264 loc) · 12.4 KB
/
Training_DRBox.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
'''
DRBox Training
This script can be used to train a new model or continue the training of an old model
You must specify the training parameters you want to use, either airplanes, ships or vehicles
you must also specify the csv file containing the labels that you wish to use during training and validation
Copyright © 2019 THALES ALENIA SPACE FRANCE. All rights reserved
Author : Paul Pontisso
'''
from keras.optimizers import Adam, SGD
from keras.callbacks import ModelCheckpoint, ReduceLROnPlateau, TerminateOnNaN, CSVLogger
from keras import backend as K
from math import ceil
import numpy as np
import os
import warnings
from models.keras_DRBox import drbox
from keras_loss_function.keras_drbox_loss import DRBoxLoss
from keras.models import load_model
from keras_layers.keras_layer_AnchorBoxes import AnchorBoxes
from keras_layers.keras_layer_L2Normalization import L2Normalization
from drbox_encoder_decoder.drbox_input_encoder import DRBoxInputEncoder
from data_generator.object_detection_2d_data_generator import DataGenerator
from data_generator.data_augmentation_chain_drbox import DataAugmentation
# Deactivate the irrelevant warnings likely to occur
warnings.simplefilter(action='ignore', category=UserWarning)
warnings.simplefilter(action='ignore', category=np.RankWarning)
warnings.simplefilter(action='ignore', category=FutureWarning)
warnings.simplefilter(action='ignore', category=RuntimeWarning)
# Lower the verbosity of TensorFlow
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
# 1. Set the model configuration parameters
img_height = 300 # Height of the model input images
img_width = 300 # Width of the model input images
img_channels = 3 # Number of color channels of the model input images
# The per-channel mean of the images in the dataset. Do not change this value if you're using any of the
# pre-trained weights.
mean_color = [123, 117, 104]
n_classes = 1 # Number of positive classes
##################################################################################
# Aiplanes Parameters
##################################################################################
#
# scales = [0.1, 0.14, 0.17, 0.22, 0.27, 0.34] # The anchor boxes scaling factors
# aspect_ratios = [[1, 1.4],
# [1, 1.4],
# [1, 1.4],
# [1, 1.4],
# [1, 1.4],
# [1, 1.4]] # The anchor boxes aspect ratios
#
# angles = [i * np.pi / 6 for i in range(6)] # the anchor boxes angles
#
# pos_ariou_threshold = 0.4
# neg_ariou_limit = 0.2
#
# # The offsets of the first anchor box center points from the top and left borders of the image as a fraction of the
# # step size for each predictor layer.
# offsets = [0.5, 0.5, 0.5, 0.5, 0.5, 0.5]
# # The variances by which the encoded target coordinates are divided as in the original implementation os SSD
# variances = [0.1, 0.1, 0.2, 0.2, 1]
# # learning rate
# lr = 0.0001
# # l2 regularization parameter
# l2_reg = 0
# alpha = 1
#
# # Set the paths to the datasets here.
# images_dir = 'data/Airplane/train_data/'
#
# train_labels_filename = "data/Airplane/labelstrain.csv"
# val_labels_filename = "data/Airplane/labelsvalidate.csv"
#
# proba_no_aug = 1/3
##################################################################################
# Ships Parameters
##################################################################################
scales = [0.04, 0.08, 0.1, 0.13, 0.16, 0.2] # The anchor boxes scaling factors
aspect_ratios = [[ 2, 3, 4],
[ 2, 3, 4],
[ 2.5, 3.5],
[ 2.5, 3.5],
[ 2.5, 3.5],
[ 2.5, 3.5]] # The anchor boxes aspect ratios
angles = [i * np.pi / 9 for i in range(9)] # the anchor boxes angles
pos_ariou_threshold = 0.4
neg_ariou_limit = 0.3
# The offsets of the first anchor box center points from the top and left borders of the image as a fraction of the
# step size for each predictor layer.
offsets = [0.5, 0.5, 0.5, 0.5, 0.5, 0.5]
# The variances by which the encoded target coordinates are divided as in the original implementation os SSD
variances = [0.1, 0.1, 0.2, 0.2, 1]
# learning rate
lr = 0.0001
# l2 regularization parameter
l2_reg = 0
alpha = 3.0
# Set the paths to the datasets here.
images_dir = 'data/Ship/train_data/'
train_labels_filename = "data/Ship/labelstrain.csv"
val_labels_filename = "data/Ship/labelsvalidate.csv"
proba_no_aug = 0.5
##################################################################################
# Vehicles Parameters
##################################################################################
#
# scales = [0.018, 0.018, 0.018, 0.018, 0.018, 0.018] # The anchor boxes scaling factors
# aspect_ratios = [[ 2.7],
# [ 2.7],
# [ 2.7],
# [ 2.7],
# [ 2.7],
# [ 2.7]] # The anchor boxes aspect ratios
#
# angles = [i * np.pi / 6 for i in range(6)] # the anchor boxes angles
# pos_ariou_threshold = 0.3
# neg_ariou_limit = 0.01
#
# # The offsets of the first anchor box center points from the top and left borders of the image as a fraction of the
# # step size for each predictor layer.
# offsets = [0.5, 0.5, 0.5, 0.5, 0.5, 0.5]
# # The variances by which the encoded target coordinates are divided as in the original implementation os SSD
# variances = [0.1, 0.1, 0.2, 0.2, 1]
# # learning rate
# lr = 0.0001
# # l2 regularization parameter
# l2_reg = 0.005
# alpha = 1
#
# # Set the paths to the datasets here.
# images_dir = 'data/Car/train_data/'
#
# train_labels_filename = "data/Car/labelstrain.csv"
# val_labels_filename = "data/Car/labelsvalidate.csv"
#
# proba_no_aug = 1/3
normalize_coords = True
# 2. Create DRBox model
# You can use either part 2.1 if you want to create model from scratch or 2.2 if you want to load a trained model
# but you should not use both at the same time
#
# 2.1 Build the model from scratch
# Build the Keras model.
K.clear_session() # Clear previous models from memory.
model = drbox(image_size=(img_height, img_width, img_channels),
n_classes=n_classes,
l2_regularization=l2_reg,
scales=scales,
aspect_ratios_per_layer=aspect_ratios,
angles_global=angles,
offsets=offsets,
variances=variances,
normalize_coords=normalize_coords,
subtract_mean=mean_color)
# Load some weights into the model.
# uncomment if you want to load pretrained weights
# Set the path to the weights you want to load.
weights_path = 'VGG_weights/VGG_ILSVRC_16_layers_fc_reduced.h5'
if os.path.exists(weights_path):
model.load_weights(weights_path, by_name=True)
# Instantiate an Adam optimizer and the DRbox loss function and compile the model.
adam = Adam(lr=lr, beta_1=0.9, beta_2=0.999, epsilon=1e-08, decay=0.0)
drbox_loss = DRBoxLoss(neg_pos_ratio=3, alpha=alpha)
model.compile(optimizer=adam, loss=drbox_loss.compute_loss)
# 2.2 Build the model from a previously trained model
# uncomment if you want to use a pre trained model
# drbox_loss = DRBoxLoss(neg_pos_ratio=3, alpha=1.0)
# model_name = 'trained_models/1model.h5'
# model = load_model(model_name, custom_objects={'L2Normalization': L2Normalization, 'AnchorBoxes': AnchorBoxes,
# 'compute_loss': drbox_loss.compute_loss})
# 3. Set up the data generators for the training
# Instantiate two `DataGenerator` objects: One for training, one for validation.
train_dataset = DataGenerator(load_images_into_memory=False, show_images=False)
val_dataset = DataGenerator(load_images_into_memory=False)
# Parse the image and label lists for the training and validation datasets.
train_dataset.parse_csv(images_dir=images_dir,
labels_filename=train_labels_filename,
include_classes='all')
val_dataset.parse_csv(images_dir=images_dir,
labels_filename=val_labels_filename,
include_classes='all')
# Set the batch size.
batch_size = 6
# Set the image transformations for pre-processing and data augmentation options.
# For the training generator:
data_augmentation = DataAugmentation(img_height, img_width, proba_no_aug=proba_no_aug)
# Instantiate an encoder that can encode ground truth labels into the format needed by the DRBox loss function.
# The encoder constructor needs the spatial dimensions of the model's predictor layers to create the anchor boxes.
predictor_sizes = [model.get_layer('conv4_3_norm_mbox_conf').output_shape[1:3],
model.get_layer('fc7_mbox_conf').output_shape[1:3],
model.get_layer('conv6_2_mbox_conf').output_shape[1:3],
model.get_layer('conv7_2_mbox_conf').output_shape[1:3],
model.get_layer('conv8_2_mbox_conf').output_shape[1:3],
model.get_layer('conv9_2_mbox_conf').output_shape[1:3]]
drbox_input_encoder = DRBoxInputEncoder(img_height=img_height,
img_width=img_width,
n_classes=n_classes,
predictor_sizes=predictor_sizes,
scales=scales,
aspect_ratios_per_layer=aspect_ratios,
angles_global=angles,
offsets=offsets,
variances=variances,
matching_type='multi',
pos_ariou_threshold=pos_ariou_threshold,
neg_ariou_limit=neg_ariou_limit,
normalize_coords=normalize_coords)
# Create the generator handles that will be passed to Keras' `fit_generator()` function.
train_generator = train_dataset.generate(batch_size=batch_size,
shuffle=True,
transformations=[data_augmentation],
label_encoder=drbox_input_encoder,
returns={'processed_images',
'encoded_labels'},
keep_images_without_gt=False)
val_generator = val_dataset.generate(batch_size=batch_size,
shuffle=True,
transformations=[],
label_encoder=drbox_input_encoder,
returns={'processed_images',
'encoded_labels'},
keep_images_without_gt=False)
# Get the number of samples in the training and validations datasets.
train_dataset_size = train_dataset.get_dataset_size()
val_dataset_size = val_dataset.get_dataset_size()
print("Number of images in the training dataset:\t{:>6}".format(train_dataset_size))
print("Number of images in the validation dataset:\t{:>6}".format(val_dataset_size))
# visualize the input if needed
if train_dataset.show_images:
for x, y in train_generator:
pass
# 4. Set the remaining training parameters
# Define a learning rate schedule.
reduce_lr = ReduceLROnPlateau(patience=5, factor=0.666, verbose=1, epsilon=1e-1)
# Set the filepath under which you want to save the weights.
model_checkpoint = ModelCheckpoint(
filepath='trained_models/drbox_epoch-{epoch:02d}_loss-{loss:.4f}_val_loss-{val_loss:.4f}.h5',
monitor='val_loss',
verbose=1,
save_best_only=True,
save_weights_only=False,
mode='auto',
period=1)
# model_checkpoint.best =
csv_logger = CSVLogger(filename='drbox_training_log.csv',
separator=',',
append=True)
terminate_on_nan = TerminateOnNaN()
callbacks = [model_checkpoint,
csv_logger,
reduce_lr,
terminate_on_nan]
# 5. Train
# If you're resuming a previous training, set `initial_epoch` and `final_epoch` accordingly.
initial_epoch = 0
final_epoch = 100
steps_per_epoch = 1000
history = model.fit_generator(generator=train_generator,
steps_per_epoch=steps_per_epoch,
epochs=final_epoch,
callbacks=callbacks,
verbose=1,
validation_data=val_generator,
validation_steps=ceil(val_dataset_size / batch_size),
initial_epoch=initial_epoch)