-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
358 lines (307 loc) · 13.7 KB
/
utils.py
File metadata and controls
358 lines (307 loc) · 13.7 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
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
import os
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.patches as patches
import xml.etree.ElementTree as ET
import tensorflow.keras.backend as K
import imgaug as ia
from imgaug import augmenters as iaa
import tensorflow as tf
class WeightReader:
def __init__(self, weight_file):
self.offset = 4
self.all_weights = np.fromfile(weight_file, dtype='float32')
def read_bytes(self, size):
self.offset = self.offset + size
return self.all_weights[self.offset - size:self.offset]
def reset(self):
self.offset = 4
def parse_annotation(ann_dir, img_dir, labels, image_w, image_h):
'''
Parse XML files in PASCAL VOC format.
Parameters
----------
- ann_dir : annotations files directory
- img_dir : images files directory
- labels : labels list
- image_h : Image height
- image_w : Image width
Returns
-------
- imgs_name : numpy array of images files path (shape : images count, 1)
- true_boxes : numpy array of annotations for each image (shape : image count, max annotation count, 5)
annotation format : xmin, ymin, xmax, ymax, class
xmin, ymin, xmax, ymax : image unit (pixel)
class = label index
'''
max_annot = 0
imgs_name = []
annots = []
# Parse file
for ann in sorted(os.listdir(ann_dir)):
annot_count = 0
boxes = []
tree = ET.parse(ann_dir + ann)
for elem in tree.iter():
if 'filename' in elem.tag:
imgs_name.append(img_dir + elem.text)
if 'width' in elem.tag:
w = int(elem.text)
if 'height' in elem.tag:
h = int(elem.text)
if 'object' in elem.tag or 'part' in elem.tag:
box = np.zeros((5))
for attr in list(elem):
if 'name' in attr.tag:
box[4] = labels.index(attr.text) + 1 # 0:label for no bounding box
if 'bndbox' in attr.tag:
annot_count += 1
for dim in list(attr):
if 'xmin' in dim.tag:
box[0] = int(round(float(dim.text)))
if 'ymin' in dim.tag:
box[1] = int(round(float(dim.text)))
if 'xmax' in dim.tag:
box[2] = int(round(float(dim.text)))
if 'ymax' in dim.tag:
box[3] = int(round(float(dim.text)))
boxes.append(np.asarray(box))
if w != image_w or h != image_h:
print('Image size error')
break
annots.append(np.asarray(boxes))
if annot_count > max_annot:
max_annot = annot_count
# Rectify annotations boxes : len -> max_annot
imgs_name = np.array(imgs_name)
true_boxes = np.zeros((imgs_name.shape[0], max_annot, 5))
for idx, boxes in enumerate(annots):
true_boxes[idx, :boxes.shape[0], :5] = boxes
return imgs_name, true_boxes
def parse_function(img_obj, true_boxes):
x_img_string = tf.io.read_file(img_obj)
x_img = tf.image.decode_png(x_img_string, channels=3) # dtype=tf.uint8
x_img = tf.image.convert_image_dtype(x_img, tf.float32) # pixel value /255, dtype=tf.float32, channels : RGB
return x_img, true_boxes
def get_dataset(label, img_dir, ann_dir, labels, image_w, image_h, batch_size):
'''
Creates a YOLO dataset
Parameters
----------
- label : Name of dataset
- ann_dir : annotations files directory
- img_dir : images files directory
- labels : labels list
- batch_size : int
Returns
-------
- YOLO dataset : generate batch
batch : tupple(images, annotations)
batch[0] : images : tensor (shape : batch_size, IMAGE_W, IMAGE_H, 3)
batch[1] : annotations : tensor (shape : batch_size, max annot, 5)
Note : image pixel values = pixels value / 255. channels : RGB
'''
imgs_name, bbox = parse_annotation(ann_dir, img_dir, labels, image_w, image_h)
dataset = tf.data.Dataset.from_tensor_slices((imgs_name, bbox))
dataset = dataset.shuffle(len(imgs_name))
dataset = dataset.repeat()
dataset = dataset.map(parse_function, num_parallel_calls=6)
dataset = dataset.batch(batch_size)
dataset = dataset.prefetch(10)
print('-------------------')
print('Dataset: {}'.format(label))
print('Images count: {}'.format(len(imgs_name)))
print('Step per epoch: {}'.format(len(imgs_name) // batch_size))
print('Images per epoch: {}'.format(batch_size * (len(imgs_name) // batch_size)))
return dataset
def test_sample(dataset):
for batch in dataset:
img = batch[0][0]
label = batch[1][0]
#plt.figure(figsize=(2,2))
f, (ax1) = plt.subplots(1,1, figsize=(10, 10))
ax1.imshow(img)
ax1.set_title('Input image. Shape : {}'.format(img.shape))
for i in range(label.shape[0]):
box = label[i,:]
box = box.numpy()
x = box[0]
y = box[1]
w = box[2] - box[0]
h = box[3] - box[1]
if box[4] == 1:
color = (0, 1, 0)
else:
color = (1, 0, 0)
rect = patches.Rectangle((x, y), w, h, linewidth = 2, edgecolor=color,facecolor='none')
ax1.add_patch(rect)
plt.show()
break
def augmentation_generator(yolo_dataset,image_w, image_h):
'''
Augmented batch generator from a yolo dataset
Parameters
----------
- YOLO dataset
Returns
-------
- augmented batch : tensor (shape : batch_size, IMAGE_W, IMAGE_H, 3)
batch : tupple(images, annotations)
batch[0] : images : tensor (shape : batch_size, IMAGE_W, IMAGE_H, 3)
batch[1] : annotations : tensor (shape : batch_size, max annot, 5)
'''
for batch in yolo_dataset:
# conversion tensor->numpy
img = batch[0].numpy()
boxes = batch[1].numpy()
# conversion bbox numpy->ia object
ia_boxes = []
for i in range(img.shape[0]):
ia_bbs = [ia.BoundingBox(x1=bb[0],
y1=bb[1],
x2=bb[2],
y2=bb[3]) for bb in boxes[i]
if (bb[0] + bb[1] + bb[2] + bb[3] > 0)]
ia_boxes.append(ia.BoundingBoxesOnImage(ia_bbs, shape=(image_w, image_h)))
# data augmentation
seq = iaa.Sequential([
iaa.Fliplr(0.5),
iaa.Flipud(0.5),
iaa.Multiply((0.4, 1.6)), # change brightness
iaa.LinearContrast((0.5, 1.5)),
#iaa.Affine(translate_px={"x": (-100,100), "y": (-100,100)}, scale=(0.7, 1.30))
])
# seq = iaa.Sequential([])
seq_det = seq.to_deterministic()
img_aug = seq_det.augment_images(img)
img_aug = np.clip(img_aug, 0, 1)
boxes_aug = seq_det.augment_bounding_boxes(ia_boxes)
# conversion ia object -> bbox numpy
for i in range(img.shape[0]):
boxes_aug[i] = boxes_aug[i].remove_out_of_image().clip_out_of_image()
for j, bb in enumerate(boxes_aug[i].bounding_boxes):
boxes[i, j, 0] = bb.x1
boxes[i, j, 1] = bb.y1
boxes[i, j, 2] = bb.x2
boxes[i, j, 3] = bb.y2
# conversion numpy->tensor
batch = (tf.convert_to_tensor(img_aug), tf.convert_to_tensor(boxes))
# batch = (img_aug, boxes)
yield batch
def process_true_boxes(true_boxes, anchors, image_width, image_height, grid_width, grid_height):
'''
Build image ground truth in YOLO format from image true_boxes and anchors of an image.
Parameters
----------
- true_boxes : bounding box tensor, shape (max_annot, 5), format : x1 y1 x2 y2 c, coords unit : image pixel
- anchors : list [anchor_1_width, anchor_1_height, anchor_2_width, anchor_2_height...]
anchors coords unit : grid cell
- image_width, image_height : int (pixels)process_true_boxes
Returns
-------
- detector_mask : array, shape (GRID_W, GRID_H, anchors_count, 1)
1 if bounding box detected by grid cell, else 0
- matching_true_boxes : array, shape (GRID_W, GRID_H, anchors_count, 5)
Contains adjusted coords of bounding box in YOLO format
-true_boxes_grid : array, same shape than true_boxes (max_annot, 5),
format : x, y, w, h, c, coords unit : grid cell
Note:
-----
Bounding box in YOLO Format : x, y, w, h, c
x, y : center of bounding box, unit : grid cell
w, h : width and height of bounding box, unit : grid cell
c : label index
'''
scale = image_width / grid_width # scale = 32
anchors_count = len(anchors) // 2
anchors = np.array(anchors)
anchors = anchors.reshape(len(anchors) // 2, 2)
detector_mask = np.zeros((grid_width, grid_height, anchors_count, 1))
#print(detector_mask.shape)
matching_true_boxes = np.zeros((grid_width, grid_height, anchors_count, 5))
# convert true_boxes numpy array -> tensor
true_boxes = true_boxes.numpy()
true_boxes_grid = np.zeros(true_boxes.shape)
# convert bounding box coords and localize bounding box
for i, box in enumerate(true_boxes): # for each bounding box in the image
# convert box coords to x, y, w, h and convert to grids coord
# box[0] = x1
# box[1] = y1
# box[2] = x2
# box[3] = y2
# box[4] = c
w = (box[2] - box[0]) / scale
h = (box[3] - box[1]) / scale
x = ((box[0] + box[2]) / 2) / scale
y = ((box[1] + box[3]) / 2) / scale
# reassign the bounding box in the form x, y, w, h, c
true_boxes_grid[i, ...] = np.array([x, y, w, h, box[4]])
if w * h > 0: # box exists sufficiently large
# calculate iou between box and each of the anchors and find best anchors
best_iou = 0
best_anchor = 0
for i in range(anchors_count): # scan all the anchor ratios
# iou (anchor and box are shifted to 0,0)
intersect = np.minimum(w, anchors[i, 0]) * np.minimum(h, anchors[i, 1])
union = (anchors[i, 0] * anchors[i, 1]) + (w * h) - intersect
iou = intersect / union
if iou > best_iou:
best_iou = iou
best_anchor = i
# localize box in detector_mask and matching true_boxes
if best_iou > 0:
x_coord = np.floor(x).astype('int')
y_coord = np.floor(y).astype('int')
detector_mask[y_coord, x_coord, best_anchor] = 1
yolo_box = np.array([x, y, w, h, box[4]])
matching_true_boxes[y_coord, x_coord, best_anchor] = yolo_box
return matching_true_boxes, detector_mask, true_boxes_grid
def ground_truth_generator(dataset, ANCHORS, IMAGE_W, IMAGE_H, GRID_W, GRID_H, CLASS):
'''
Ground truth batch generator from a yolo dataset, ready to compare with YOLO prediction in loss function.
Parameters
----------
- YOLO dataset. Generate batch:
batch : tupple(images, annotations)
batch[0] : images : tensor (shape : batch_size, IMAGE_W, IMAGE_H, 3)
batch[1] : annotations : tensor (shape : batch_size, max annot, 5)
Returns
-------
- imgs : images to predict. tensor (shape : batch_size, IMAGE_H, IMAGE_W, 3)
- detector_mask : tensor, shape (batch size, GRID_W, GRID_H, anchors_count, 1)
1 if bounding box detected by grid cell, else 0
- matching_true_boxes : tensor, shape (batch_size, GRID_W, GRID_H, anchors_count, 5)
Contains adjusted coords of bounding box in YOLO format
- class_one_hot : tensor, shape (batch_size, GRID_W, GRID_H, anchors_count, class_count)
One hot representation of bounding box label
- true_boxes_grid : annotations : tensor (shape : batch_size, max annot, 5)
true_boxes format : x, y, w, h, c, coords unit : grid cell
'''
for batch in dataset:
# imgs
imgs = batch[0]
# true boxes
true_boxes = batch[1]
# matching_true_boxes and detector_mask
batch_matching_true_boxes = []
batch_detector_mask = []
batch_true_boxes_grid = []
for i in range(true_boxes.shape[0]): # for each image in batch
one_matching_true_boxes, one_detector_mask, true_boxes_grid = process_true_boxes(true_boxes[i],
ANCHORS,
IMAGE_W,
IMAGE_H,
GRID_W,
GRID_H)
batch_matching_true_boxes.append(one_matching_true_boxes)
batch_detector_mask.append(one_detector_mask)
batch_true_boxes_grid.append(true_boxes_grid)
detector_mask = tf.convert_to_tensor(np.array(batch_detector_mask), dtype='float32')
matching_true_boxes = tf.convert_to_tensor(np.array(batch_matching_true_boxes), dtype='float32')
true_boxes_grid = tf.convert_to_tensor(np.array(batch_true_boxes_grid), dtype='float32')
# class one_hot
matching_classes = K.cast(matching_true_boxes[..., 4], 'int32')
class_one_hot = K.one_hot(matching_classes, CLASS + 1)[:, :, :, :, 1:]
class_one_hot = tf.cast(class_one_hot, dtype='float32')
batch = (imgs, detector_mask, matching_true_boxes, class_one_hot, true_boxes_grid)
yield batch