forked from frk2/ssd_keras
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrainbosch.py
247 lines (204 loc) · 12.2 KB
/
trainbosch.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
from keras import backend as K
from keras.optimizers import Adam
from keras_ssd7 import build_model
from keras_ssd_loss import SSDLoss
from ssd_batch_generator import BatchGenerator
from ssd_box_encode_decode_utils import SSDBoxEncoder
from keras.callbacks import ModelCheckpoint, EarlyStopping, ReduceLROnPlateau
from keras import backend as K
from keras.models import load_model
from math import ceil
from ssd_box_encode_decode_utils import SSDBoxEncoder, decode_y, decode_y2
from matplotlib import pyplot as plt
from pathlib import Path
### Set up the model
# 1: Set some necessary parameters
def train(epochs, batch_size, train_generator, n_train_samples, val_generator, n_val_samples ):
history = model.fit_generator(generator=train_generator,
steps_per_epoch=ceil(n_train_samples / batch_size),
epochs=epochs,
callbacks=[ModelCheckpoint('./ssd7_0_weights_epoch{epoch:02d}_loss{loss:.4f}.h5',
monitor='val_loss',
verbose=1,
save_best_only=True,
save_weights_only=True,
mode='auto',
period=1),
EarlyStopping(monitor='val_loss',
min_delta=0.001,
patience=2),
ReduceLROnPlateau(monitor='val_loss',
factor=0.5,
patience=0,
epsilon=0.001,
cooldown=0)],
validation_data=val_generator,
validation_steps=ceil(n_val_samples / batch_size))
model_name = 'ssd7_bosch'
model.save('./{}.h5'.format(model_name))
model.save_weights('./{}_weights.h5'.format(model_name))
print()
print("Model saved as {}.h5".format(model_name))
print("Weights also saved separately as {}_weights.h5".format(model_name))
print()
def predict(model, X):
y_pred = model.predict(X)
print(y_pred)
y_pred_decoded = decode_y2(y_pred,
confidence_thresh=0.2,
iou_threshold=0.4,
top_k='all',
input_coords='centroids',
normalize_coords=False,
img_height=None,
img_width=None)
print("Decoded predictions (output format is [class_id, confidence, xmin, xmax, ymin, ymax]):\n")
print(y_pred_decoded)
return y_pred_decoded
# 5: Draw the predicted boxes onto the image
def predictAndDraw(model, X, y_true):
i = 0
y_pred_decoded = predict(model, X)
plt.figure(figsize=(20,12))
plt.imshow(X[i])
current_axis = plt.gca()
classes = ['background', 'red', 'yellow', 'green' ]
# Draw the predicted boxes in blue
for box in y_pred_decoded[i]:
label = '{}: {:.2f}'.format(classes[int(box[0])], box[1])
current_axis.add_patch(plt.Rectangle((box[2], box[4]), box[3]-box[2], box[5]-box[4], color='blue', fill=False, linewidth=2))
current_axis.text(box[2], box[4], label, size='x-large', color='white', bbox={'facecolor':'blue', 'alpha':1.0})
# Draw the ground truth boxes in green (omit the label for more clarity)
for box in y_true[i]:
label = '{}'.format(classes[int(box[0])])
current_axis.add_patch(plt.Rectangle((box[1], box[3]), box[2]-box[1], box[4]-box[3], color='green', fill=False, linewidth=2))
plt.show()
if __name__ == '__main__':
img_height = 360 # Height of the input images
img_width = 640 # Width of the input images
img_channels = 3 # Number of color channels of the input images
n_classes = 4 # Number of classes including the background class
min_scale = 0.08 # The scaling factor for the smallest anchor boxes
max_scale = 0.96 # The scaling factor for the largest anchor boxes
scales = [0.08, 0.16, 0.32, 0.64,
0.96] # An explicit list of anchor box scaling factors. If this is passed, it will override `min_scale` and `max_scale`.
aspect_ratios = [0.5, 1.0, 2.0] # The list of aspect ratios for the anchor boxes
two_boxes_for_ar1 = True # Whether or not you want to generate two anchor boxes for aspect ratio 1
limit_boxes = False # Whether or not you want to limit the anchor boxes to lie entirely within the image boundaries
variances = [1.0, 1.0, 1.0, 1.0] # The list of variances by which the encoded target coordinates are scaled
coords = 'centroids' # Whether the box coordinates to be used should be in the 'centroids' or 'minmax' format, see documentation
normalize_coords = False # Whether or not the model is supposed to use relative coordinates that are within [0,1]
# 2: Build the Keras model (and possibly load some trained weights)
K.clear_session() # Clear previous models from memory.
# The output `predictor_sizes` is needed below to set up `SSDBoxEncoder`
model, predictor_sizes = build_model(image_size=(img_height, img_width, img_channels),
n_classes=n_classes,
min_scale=min_scale,
max_scale=max_scale,
scales=scales,
aspect_ratios_global=aspect_ratios,
aspect_ratios_per_layer=None,
two_boxes_for_ar1=two_boxes_for_ar1,
limit_boxes=limit_boxes,
variances=variances,
coords=coords,
normalize_coords=normalize_coords)
### Set up training
batch_size = 16
# 3: Instantiate an Adam optimizer and the SSD loss function and compile the model
adam = Adam(lr=0.001, beta_1=0.9, beta_2=0.999, epsilon=1e-08, decay=5e-05)
ssd_loss = SSDLoss(neg_pos_ratio=3, n_neg_min=0, alpha=1.0)
model.compile(optimizer=adam, loss=ssd_loss.compute_loss)
# 4: Instantiate an encoder that can encode ground truth labels into the format needed by the SSD loss function
ssd_box_encoder = SSDBoxEncoder(img_height=img_height,
img_width=img_width,
n_classes=n_classes,
predictor_sizes=predictor_sizes,
min_scale=min_scale,
max_scale=max_scale,
scales=scales,
aspect_ratios_global=aspect_ratios,
aspect_ratios_per_layer=None,
two_boxes_for_ar1=two_boxes_for_ar1,
limit_boxes=limit_boxes,
variances=variances,
pos_iou_threshold=0.5,
neg_iou_threshold=0.2,
coords=coords,
normalize_coords=normalize_coords)
# 5: Create the training set batch generator
train_dataset = BatchGenerator(images_path='./bosch/',
include_classes='all',
box_output_format=['class_id', 'xmin', 'xmax', 'ymin',
'ymax']) # This is the format in which the generator is supposed to output the labels. At the moment it **must** be the format set here.
i,l = train_dataset.parse_bosch_yaml(yaml_file='./bosch/combined_train.yaml', ret=True)
# XML parser will be helpful, check the documentation.
# Change the online data augmentation settings as you like
train_generator = train_dataset.generate(batch_size=batch_size,
train=True,
ssd_box_encoder=ssd_box_encoder,
equalize=False,
brightness=(0.5, 2, 0.5),
# Randomly change brightness between 0.5 and 2 with probability 0.5
flip=0.5, # Randomly flip horizontally with probability 0.5
translate=((5, 50), (3, 30), 0.5),
# Randomly translate by 5-50 pixels horizontally and 3-30 pixels vertically with probability 0.5
scale=(0.75, 1.3, 0.5),
# Randomly scale between 0.75 and 1.3 with probability 0.5
random_crop=False,
crop=False,
resize=(img_width, img_height),
gray=False,
limit_boxes=True,
include_thresh=0.4,
diagnostics=False)
n_train_samples = train_dataset.get_n_samples()
# 6: Create the validation set batch generator (if you want to use a validation dataset)
val_dataset = BatchGenerator(images_path='./bosch/',
include_classes='all',
box_output_format=['class_id', 'xmin', 'xmax', 'ymin', 'ymax'])
i,l = val_dataset.parse_bosch_yaml(yaml_file='./bosch/test.yaml', ret=True, force_dir='rgb/test')
# val_dataset.parse_csv(labels_path='./udacity_data/labels.csv',
# input_format=['image_name', 'xmin', 'xmax', 'ymin', 'ymax', 'class_id'])
val_generator = val_dataset.generate(batch_size=batch_size,
train=True,
ssd_box_encoder=ssd_box_encoder,
equalize=False,
brightness=False,
flip=False,
translate=False,
scale=False,
random_crop=False,
crop=False,
resize=(img_width, img_height),
gray=False,
limit_boxes=True,
include_thresh=0.4,
diagnostics=False)
n_val_samples = val_dataset.get_n_samples()
### Make predictions
# 1: Set the generator
predict_generator = train_dataset.generate(batch_size=1,
train=False,
equalize=False,
brightness=False,
flip=False,
translate=False,
scale=False,
random_crop=False,
crop=False,
resize=(img_width, img_height),
gray=False,
limit_boxes=True,
include_thresh=0.4,
diagnostics=False)
print(n_train_samples)
if Path('ssd7_bosch_weights.h5').is_file():
print('Using existing model!')
# model = load_model('ssd7_bosch.h5')
model.load_weights('ssd7_bosch_weights.h5')
while True:
X, y_true, filenames = next(predict_generator)
predictAndDraw(model, X, y_true)
else:
train(30, batch_size, train_generator, n_train_samples, val_generator, n_val_samples)