-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathutils.py
302 lines (239 loc) · 11.1 KB
/
utils.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
import tensorflow as tf
from tensorflow import keras
import keras.backend as K
import cv2 as cv
import numpy as np
# We are going to define a custom layer , that will serve as the reshaping layer.
class CustomReshapeLayer(tf.keras.layers.Layer):
def __init__(self, target_shape):
super(CustomReshapeLayer, self).__init__()
self.target_shape = tuple(target_shape)
def get_config(self):
config = super().get_config().copy()
config.update({"target_shape": self.target_shape})
return config
def call(self, input):
# grids 7x7
S = [self.target_shape[0], self.target_shape[1]]
# classes
C = 20
# no of bounding boxes per grid
B = 2
# n_class_prob_elements is 7*7*20 = 980 | Represents the class probabilities of every grid cell .
n_class_prob_elements = S[0] * S[1] * C
#
n_class_prob_and_prob_object = n_class_prob_elements + S[0] * S[1] * B
# class probabilities
# taking the FIRST 'n_class_prob_elements' values and reshaping them into 7*7*20 tensor
class_probs = K.reshape(
input[:, :n_class_prob_elements],
(K.shape(input)[0],) + tuple([S[0], S[1], C]),
)
class_probs = K.softmax(class_probs)
# confidence
# taking the rest of the elements and reshaping them into a 7*7*2 tensor
# These will represent the Pr(Object) of each grid cell. Notice we have two of these, since
# we are dealing with two anchor boxes per cell
confs = K.reshape(
input[:, n_class_prob_elements:n_class_prob_and_prob_object],
(K.shape(input)[0],) + tuple([S[0], S[1], B]),
)
confs = K.sigmoid(confs)
# boxes
# Now we are taking the last elements , i.e they will represent the bounding boxes parameters .
# Each box will have 4 parameters to completely describe the box.
# And thus we are reshaping them into a 7*7*8 | 8 because we have 2 bounding boxes per cell , so (4*2) params each cell
boxes = K.reshape(
input[:, n_class_prob_and_prob_object:],
(K.shape(input)[0],) + tuple([S[0], S[1], B * 4]),
)
boxes = K.sigmoid(boxes)
outputs = K.concatenate([class_probs, confs, boxes])
return outputs
# Custom Reshape Layer END ************************************************************************************************
# Since the paper implements a custom learning rate schedule
# Let us define a Custom Learning Rate Scheduler
class CustomLearningRateScheduler(keras.callbacks.Callback):
def __init__(self, schedule):
super(CustomLearningRateScheduler, self).__init__()
self.schedule = schedule
def on_epoch_begin(self, epoch, logs=None):
if not hasattr(self.model.optimizer, "lr"):
raise ValueError('Optimizer must have a "lr" attribute.')
# Get the current learning rate from model's optimizer.
lr = float(tf.keras.backend.get_value(self.model.optimizer.learning_rate))
# Call schedule function to get the scheduled learning rate.
scheduled_lr = self.schedule(epoch, lr)
# Set the value back to the optimizer before this epoch starts
tf.keras.backend.set_value(self.model.optimizer.lr, scheduled_lr)
print("\nEpoch %05d: Learning rate is %6.4f." % (epoch, scheduled_lr))
LR_SCHEDULE = [
# (epoch to start, learning rate) tuples
(0, 0.01),
(75, 0.001),
(105, 0.0001),
]
# We define a function to get the desired learning rate at the ongoing epoch
def lr_schedule(epoch, lr):
"""Helper function to retrieve the scheduled learning rate based on epoch."""
if epoch < LR_SCHEDULE[0][0] or epoch > LR_SCHEDULE[-1][0]:
return lr
for i in range(len(LR_SCHEDULE)):
if epoch == LR_SCHEDULE[i][0]:
return LR_SCHEDULE[i][1]
return lr
# Custom Learning Rate Scheduler END ********************************************************************************
# Now let us define the loss function and the other functions required
def xywh2minmax(xy, wh):
xy_min = xy - wh / 2
xy_max = xy + wh / 2
return xy_min, xy_max
test_done = False
def iou(pred_mins, pred_maxes, true_mins, true_maxes):
global test_done
if not test_done:
print(
"iou funciton : {} | {} | {} | {}".format(
pred_mins, pred_maxes, true_mins, true_maxes
)
)
test_done = True
intersect_mins = K.maximum(pred_mins, true_mins)
intersect_maxes = K.minimum(pred_maxes, true_maxes)
intersect_wh = K.maximum(intersect_maxes - intersect_mins, 0.0)
intersect_areas = intersect_wh[..., 0] * intersect_wh[..., 1]
pred_wh = pred_maxes - pred_mins
true_wh = true_maxes - true_mins
pred_areas = pred_wh[..., 0] * pred_wh[..., 1]
true_areas = true_wh[..., 0] * true_wh[..., 1]
union_areas = pred_areas + true_areas - intersect_areas
iou_scores = intersect_areas / union_areas
return iou_scores
def yolo_head(feats):
# Dynamic implementation of conv dims for fully convolutional model.
conv_dims = K.shape(feats)[1:3] # assuming channels last
# In YOLO the height index is the inner most iteration.
conv_height_index = K.arange(0, stop=conv_dims[0])
conv_width_index = K.arange(0, stop=conv_dims[1])
conv_height_index = K.tile(conv_height_index, [conv_dims[1]])
# TODO: Repeat_elements and tf.split doesn't support dynamic splits.
# conv_width_index = K.repeat_elements(conv_width_index, conv_dims[1], axis=0)
conv_width_index = K.tile(K.expand_dims(conv_width_index, 0), [conv_dims[0], 1])
conv_width_index = K.flatten(K.transpose(conv_width_index))
conv_index = K.transpose(K.stack([conv_height_index, conv_width_index]))
conv_index = K.reshape(conv_index, [1, conv_dims[0], conv_dims[1], 1, 2])
conv_index = K.cast(conv_index, K.dtype(feats))
conv_dims = K.cast(K.reshape(conv_dims, [1, 1, 1, 1, 2]), K.dtype(feats))
box_xy = (feats[..., :2] + conv_index) / conv_dims * 448
box_wh = feats[..., 2:4] * 448
return box_xy, box_wh
def yolo_loss(y_true, y_pred):
label_class = y_true[..., :20] # ? * 7 * 7 * 20
label_box = y_true[..., 20:24] # ? * 7 * 7 * 4
response_mask = y_true[..., 24] # ? * 7 * 7
response_mask = K.expand_dims(response_mask) # ? * 7 * 7 * 1
predict_class = y_pred[..., :20] # ? * 7 * 7 * 20
predict_trust = y_pred[..., 20:22] # ? * 7 * 7 * 2
predict_box = y_pred[..., 22:] # ? * 7 * 7 * 8
_label_box = K.reshape(label_box, [-1, 7, 7, 1, 4])
_predict_box = K.reshape(predict_box, [-1, 7, 7, 2, 4])
label_xy, label_wh = yolo_head(_label_box) # ? * 7 * 7 * 1 * 2, ? * 7 * 7 * 1 * 2
label_xy = K.expand_dims(label_xy, 3) # ? * 7 * 7 * 1 * 1 * 2
label_wh = K.expand_dims(label_wh, 3) # ? * 7 * 7 * 1 * 1 * 2
label_xy_min, label_xy_max = xywh2minmax(
label_xy, label_wh
) # ? * 7 * 7 * 1 * 1 * 2, ? * 7 * 7 * 1 * 1 * 2
predict_xy, predict_wh = yolo_head(
_predict_box
) # ? * 7 * 7 * 2 * 2, ? * 7 * 7 * 2 * 2
predict_xy = K.expand_dims(predict_xy, 4) # ? * 7 * 7 * 2 * 1 * 2
predict_wh = K.expand_dims(predict_wh, 4) # ? * 7 * 7 * 2 * 1 * 2
predict_xy_min, predict_xy_max = xywh2minmax(
predict_xy, predict_wh
) # ? * 7 * 7 * 2 * 1 * 2, ? * 7 * 7 * 2 * 1 * 2
iou_scores = iou(
predict_xy_min, predict_xy_max, label_xy_min, label_xy_max
) # ? * 7 * 7 * 2 * 1
best_ious = K.max(iou_scores, axis=4) # ? * 7 * 7 * 2
best_box = K.max(best_ious, axis=3, keepdims=True) # ? * 7 * 7 * 1
box_mask = K.cast(best_ious >= best_box, K.dtype(best_ious)) # ? * 7 * 7 * 2
no_object_loss = 0.5 * (1 - box_mask * response_mask) * K.square(0 - predict_trust)
object_loss = box_mask * response_mask * K.square(1 - predict_trust)
confidence_loss = no_object_loss + object_loss
confidence_loss = K.sum(confidence_loss)
class_loss = response_mask * K.square(label_class - predict_class)
class_loss = K.sum(class_loss)
_label_box = K.reshape(label_box, [-1, 7, 7, 1, 4])
_predict_box = K.reshape(predict_box, [-1, 7, 7, 2, 4])
label_xy, label_wh = yolo_head(_label_box) # ? * 7 * 7 * 1 * 2, ? * 7 * 7 * 1 * 2
predict_xy, predict_wh = yolo_head(
_predict_box
) # ? * 7 * 7 * 2 * 2, ? * 7 * 7 * 2 * 2
box_mask = K.expand_dims(box_mask)
response_mask = K.expand_dims(response_mask)
box_loss = 5 * box_mask * response_mask * K.square((label_xy - predict_xy) / 448)
box_loss += (
5
* box_mask
* response_mask
* K.square((K.sqrt(label_wh) - K.sqrt(predict_wh)) / 448)
)
box_loss = K.sum(box_loss)
loss = confidence_loss + class_loss + box_loss
return loss
# Loss Function END ***************************************************************************************************
# Pre processing the input images and making the size uniform
def read(image_path, label):
image = cv.imread(image_path)
# print("image path:{} label:{} test print {}".format(image_path,label,image))
image = cv.cvtColor(image, cv.COLOR_BGR2RGB)
image_h, image_w = image.shape[0:2]
image = cv.resize(image, (448, 448))
image = image / 255.0
label_matrix = np.zeros([7, 7, 30])
for l in label:
l = l.split(",")
l = np.array(l, dtype=np.int)
xmin = l[0]
ymin = l[1]
xmax = l[2]
ymax = l[3]
cls = l[4]
x = (xmin + xmax) / 2 / image_w
y = (ymin + ymax) / 2 / image_h
w = (xmax - xmin) / image_w
h = (ymax - ymin) / image_h
loc = [7 * x, 7 * y]
loc_i = int(loc[1])
loc_j = int(loc[0])
y = loc[1] - loc_i
x = loc[0] - loc_j
if label_matrix[loc_i, loc_j, 24] == 0:
label_matrix[loc_i, loc_j, cls] = 1
label_matrix[loc_i, loc_j, 20:24] = [x, y, w, h]
label_matrix[loc_i, loc_j, 24] = 1 # response
return image, label_matrix
# Read function END **************************************************************************************************************
# Custom Data Generator
class My_Custom_Generator(keras.utils.Sequence):
def __init__(self, images, labels, batch_size):
self.images = images
self.labels = labels
self.batch_size = batch_size
def __len__(self):
return (np.ceil(len(self.images) / float(self.batch_size))).astype(np.int)
def __getitem__(self, idx):
batch_x = self.images[idx * self.batch_size : (idx + 1) * self.batch_size]
batch_y = self.labels[idx * self.batch_size : (idx + 1) * self.batch_size]
# print("batch_x:{}".format(batch_x))
train_image = []
train_label = []
for i in range(0, len(batch_x)):
img_path = batch_x[i]
label = batch_y[i]
# print("prem test:{}".format(img_path))
image, label_matrix = read(img_path, label)
train_image.append(image)
train_label.append(label_matrix)
return np.array(train_image), np.array(train_label)
# Custom Generator END **************************************************************************************************************