-
Notifications
You must be signed in to change notification settings - Fork 2
/
utils.py
356 lines (282 loc) · 12 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
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
import numpy as np
import os
import xml.etree.ElementTree as ET
import tensorflow as tf
import copy
import cv2
ann_dir = 'VOCdevkit/VOC2012/Annotations/'
img_dir = 'VOCdevkit/VOC2012/JPEGImages/'
LABELS = ['aeroplane', 'bicycle', 'bird', 'boat', 'bottle', 'bus', 'car', 'cat', 'chair', 'cow', 'diningtable', 'dog', 'horse', 'motorbike', 'person', 'pottedplant', 'sheep', 'sofa', 'train', 'tvmonitor']
COLORS = [(43,206,72),(255,204,153),(128,128,128),(148,255,181),(143,124,0),(157,204,0),(194,0,136),(0,51,128),(255,164,5),(255,168,187),(66,102,0),(255,0,16),(94,241,242),(0,153,143),(224,255,102),(116,10,255),(153,0,0),(255,255,128),(255,255,0),(255,80,5)]
NORM_H, NORM_W = 416, 416
GRID_H, GRID_W = 13 , 13
BATCH_SIZE = 8
BOX = 5
CLASS = 20
THRESHOLD = 0.2
ANCHORS = '1.08,1.19, 3.42,4.41, 6.63,11.38, 9.42,5.11, 16.62,10.52'
ANCHORS = [float(ANCHORS.strip()) for ANCHORS in ANCHORS.split(',')]
SCALE_NOOB, SCALE_CONF, SCALE_COOR, SCALE_PROB = 0.5, 5.0, 5.0, 1.0
class BoundBox:
def __init__(self, class_num):
self.x, self.y, self.w, self.h, self.c = 0., 0., 0., 0., 0.
self.probs = np.zeros((class_num,))
def iou(self, box):
intersection = self.intersect(box)
union = self.w*self.h + box.w*box.h - intersection
return intersection/union
def intersect(self, box):
width = self.__overlap([self.x-self.w/2, self.x+self.w/2], [box.x-box.w/2, box.x+box.w/2])
height = self.__overlap([self.y-self.h/2, self.y+self.h/2], [box.y-box.h/2, box.y+box.h/2])
return width * height
def __overlap(self, interval_a, interval_b):
x1, x2 = interval_a
x3, x4 = interval_b
if x3 < x1:
if x4 < x1:
return 0
else:
return min(x2,x4) - x1
else:
if x2 < x3:
return 0
else:
return min(x2,x4) - x3
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 interpret_netout(image, netout):
boxes = []
# interpret the output by the network
for row in range(GRID_H):
for col in range(GRID_W):
for b in range(BOX):
box = BoundBox(CLASS)
# first 5 weights for x, y, w, h and confidence
box.x, box.y, box.w, box.h, box.c = netout[row,col,b,:5]
box.x = (col + sigmoid(box.x)) / GRID_W
box.y = (row + sigmoid(box.y)) / GRID_H
box.w = ANCHORS[2 * b + 0] * np.exp(box.w) / GRID_W
box.h = ANCHORS[2 * b + 1] * np.exp(box.h) / GRID_H
box.c = sigmoid(box.c)
# last 20 weights for class likelihoods
classes = netout[row,col,b,5:]
box.probs = softmax(classes) * box.c
box.probs *= box.probs > THRESHOLD
boxes.append(box)
# suppress non-maximal boxes
for c in range(CLASS):
sorted_indices = list(reversed(np.argsort([box.probs[c] for box in boxes])))
for i in xrange(len(sorted_indices)):
index_i = sorted_indices[i]
if boxes[index_i].probs[c] == 0:
continue
else:
for j in xrange(i+1, len(sorted_indices)):
index_j = sorted_indices[j]
if boxes[index_i].iou(boxes[index_j]) >= 0.4:
boxes[index_j].probs[c] = 0
# draw the boxes using a threshold
for box in boxes:
max_indx = np.argmax(box.probs)
max_prob = box.probs[max_indx]
if max_prob > THRESHOLD:
xmin = int((box.x - box.w/2) * image.shape[1])
xmax = int((box.x + box.w/2) * image.shape[1])
ymin = int((box.y - box.h/2) * image.shape[0])
ymax = int((box.y + box.h/2) * image.shape[0])
cv2.rectangle(image, (xmin,ymin), (xmax,ymax), COLORS[max_indx], 2)
cv2.putText(image, LABELS[max_indx], (xmin, ymin - 12), 0, 1e-3 * image.shape[0], (0,255,0), 2)
return image
def parse_annotation(ann_dir):
all_img = []
for ann in os.listdir(ann_dir):
img = {'object':[]}
tree = ET.parse(ann_dir + ann)
for elem in tree.iter():
if 'filename' in elem.tag:
all_img += [img]
img['filename'] = elem.text
if 'width' in elem.tag:
img['width'] = int(elem.text)
if 'height' in elem.tag:
img['height'] = int(elem.text)
if 'object' in elem.tag or 'part' in elem.tag:
obj = {}
for attr in list(elem):
if 'name' in attr.tag:
obj['name'] = attr.text
if obj['name'] in LABELS:
img['object'] += [obj]
else:
break
if 'bndbox' in attr.tag:
for dim in list(attr):
if 'xmin' in dim.tag:
obj['xmin'] = int(round(float(dim.text)))
if 'ymin' in dim.tag:
obj['ymin'] = int(round(float(dim.text)))
if 'xmax' in dim.tag:
obj['xmax'] = int(round(float(dim.text)))
if 'ymax' in dim.tag:
obj['ymax'] = int(round(float(dim.text)))
return all_img
def aug_img(train_instance):
path = train_instance['filename']
all_obj = copy.deepcopy(train_instance['object'][:])
img = cv2.imread(img_dir + path)
h, w, c = img.shape
# scale the image
scale = np.random.uniform() / 10. + 1.
img = cv2.resize(img, (0,0), fx = scale, fy = scale)
# translate the image
max_offx = (scale-1.) * w
max_offy = (scale-1.) * h
offx = int(np.random.uniform() * max_offx)
offy = int(np.random.uniform() * max_offy)
img = img[offy : (offy + h), offx : (offx + w)]
# flip the image
flip = np.random.binomial(1, .5)
if flip > 0.5: img = cv2.flip(img, 1)
# re-color
t = [np.random.uniform()]
t += [np.random.uniform()]
t += [np.random.uniform()]
t = np.array(t)
img = img * (1 + t)
img = img / (255. * 2.)
# resize the image to standard size
img = cv2.resize(img, (NORM_H, NORM_W))
img = img[:,:,::-1]
# fix object's position and size
for obj in all_obj:
for attr in ['xmin', 'xmax']:
obj[attr] = int(obj[attr] * scale - offx)
obj[attr] = int(obj[attr] * float(NORM_W) / w)
obj[attr] = max(min(obj[attr], NORM_W), 0)
for attr in ['ymin', 'ymax']:
obj[attr] = int(obj[attr] * scale - offy)
obj[attr] = int(obj[attr] * float(NORM_H) / h)
obj[attr] = max(min(obj[attr], NORM_H), 0)
if flip > 0.5:
xmin = obj['xmin']
obj['xmin'] = NORM_W - obj['xmax']
obj['xmax'] = NORM_W - xmin
return img, all_obj
def data_gen(all_img, batch_size):
num_img = len(all_img)
shuffled_indices = np.random.permutation(np.arange(num_img))
l_bound = 0
r_bound = batch_size if batch_size < num_img else num_img
while True:
if l_bound == r_bound:
l_bound = 0
r_bound = batch_size if batch_size < num_img else num_img
shuffled_indices = np.random.permutation(np.arange(num_img))
batch_size = r_bound - l_bound
currt_inst = 0
x_batch = np.zeros((batch_size, NORM_W, NORM_H, 3))
y_batch = np.zeros((batch_size, GRID_W, GRID_H, BOX, 5+CLASS))
for index in shuffled_indices[l_bound:r_bound]:
train_instance = all_img[index]
# augment input image and fix object's position and size
img, all_obj = aug_img(train_instance)
#for obj in all_obj:
# cv2.rectangle(img[:,:,::-1], (obj['xmin'],obj['ymin']), (obj['xmax'],obj['ymax']), (1,1,0), 3)
#plt.imshow(img); plt.show()
# construct output from object's position and size
for obj in all_obj:
box = []
center_x = .5*(obj['xmin'] + obj['xmax']) #xmin, xmax
center_x = center_x / (float(NORM_W) / GRID_W)
center_y = .5*(obj['ymin'] + obj['ymax']) #ymin, ymax
center_y = center_y / (float(NORM_H) / GRID_H)
grid_x = int(np.floor(center_x))
grid_y = int(np.floor(center_y))
if grid_x < GRID_W and grid_y < GRID_H:
obj_indx = LABELS.index(obj['name'])
box = [obj['xmin'], obj['ymin'], obj['xmax'], obj['ymax']]
y_batch[currt_inst, grid_y, grid_x, :, 0:4] = BOX * [box]
y_batch[currt_inst, grid_y, grid_x, :, 4 ] = BOX * [1.]
y_batch[currt_inst, grid_y, grid_x, :, 5: ] = BOX * [[0.]*CLASS]
y_batch[currt_inst, grid_y, grid_x, :, 5+obj_indx] = 1.0
# concatenate batch input from the image
x_batch[currt_inst] = img
currt_inst += 1
del img, all_obj
yield x_batch, y_batch
l_bound = r_bound
r_bound = r_bound + batch_size
if r_bound > num_img: r_bound = num_img
def sigmoid(x):
return 1. / (1. + np.exp(-x))
def softmax(x):
return np.exp(x) / np.sum(np.exp(x), axis=0)
class YoloUtils():
def __init__(self, size=480, grid=7, nb_class=20):
self.size = size
self.grid = grid
self.nb_class = nb_class
self.side = 1.0 / self.grid
def make_train_map(self, label):
'''
params:
labels: a tuples (class1, x1, y1, w1, h1)
return:
class_map: a numpy array.
'''
class_map = np.zeros([self.grid, self.grid], dtype='int32')
class_map[:] = 20
cl,x,y,w,h = label
object_map = np.zeros([10, self.grid, self.grid])
gp = self._convert(self._get_grid_point(x, y))
object_map[:, gp[0], gp[1]] = (label[1:] + [1,]) * 2
p1_x = x - w/2
p1_y = y - h/2
w_step = int(w // self.side)
h_step = int(h // self.side)
p1 = self._get_grid_point(p1_x, p1_y)
p1 = self._convert(p1)
class_map[p1] = cl
for dw in range(w_step+1):
for dh in range(h_step+1):
class_map[(p1[0]+dh, p1[1]+dw)] = cl
class_map = self._binarylab(class_map)
return np.concatenate([object_map,class_map])
def _get_grid_point(self, x, y):
'''
params:
x: normalized x coordinate of a image (ex. 0.5)
y: normalized y coordinate of a image (ex. 0.4)
return:
coordinates of a grid
'''
for i in range(self.grid):
if x < self.side * (i+1): break
for j in range(self.grid):
if y < self.side * (j+1): break
return (i, j)
def _binarylab(self, class_map):
y = np.zeros((self.nb_class,self.grid,self.grid))
for i in range(self.grid):
for j in range(self.grid):
cl = class_map[i][j]
if cl == self.nb_class: continue
y[cl, i, j] = 1
return y
def _convert(self, xy):
c, r = xy[1], xy[0]
return (c,r)
if __name__ == "__main__":
utils = YoloUtils(480, 7)
print(utils.make_class_map([(18,0.546,0.5165165165165165,0.908,0.9669669669669669),
(14,0.145,0.6501501501501501,0.042,0.15915915915915915)]))
print(utils.make_class_map([(7,0.372,0.7794943820224719,0.48,0.44101123595505615),
(11,0.338,0.40308988764044945,0.24,0.4353932584269663)]))
print(utils.make_class_map([ (0,0.489,0.5813333333333334,0.922,0.3733333333333333) ]))