-
Notifications
You must be signed in to change notification settings - Fork 337
/
Copy paththis_util.py
163 lines (126 loc) · 4.46 KB
/
this_util.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
from collections import namedtuple
import numpy as np
__all__ = [
'detection_output'
]
def intersection_area(a, b):
if a.xmin > b.xmax or a.xmax < b.xmin or a.ymin > b.ymax or a.ymax < b.ymin:
return 0
inter_width = min(a.xmax, b.xmax) - max(a.xmin, b.xmin);
inter_height = min(a.ymax, b.ymax) - max(a.ymin, b.ymin);
return inter_width * inter_height;
def nms_sorted_bboxes(bboxes, nms_threshold):
n = len(bboxes)
picked = []
areas = []
for i in range(n):
r = bboxes[i]
width = r.xmax - r.xmin
height = r.ymax - r.ymin
areas.append(width * height)
for i in range(n):
a = bboxes[i]
keep = True
for j in range(len(picked)):
b = bboxes[picked[j]]
# intersection over union
inter_area = intersection_area(a, b)
union_area = areas[i] + areas[picked[j]] - inter_area
if inter_area / union_area > nms_threshold:
keep = False
if keep:
picked.append(i)
return picked
def detection_output(conf, loc):
num_class = 3
nms_top_k = 300
keep_top_k = 200
confidence_threshold = 0.01
nms_threshold = 0.45
priorbox = np.load("anchor.npy")
confidence = conf.reshape(-1, 3) # 1126, 3
location = loc.reshape(-1, 4) # 1126, 4
variance = priorbox[0][1].reshape(-1, 4) # 1126,4
priorbox = priorbox[0][0].reshape(-1, 4) # 1126, 6
num_prior = len(priorbox)
bboxes = np.zeros((num_prior, 4))
for i in range(num_prior):
# if score of background class is larger than confidence threshold
score = confidence[i]
if score[0] >= 1.0 - confidence_threshold:
continue
loc = location[i]
pb = priorbox[i]
var = variance[i]
# CENTER_SIZE
pb_w = pb[2] - pb[0]
pb_h = pb[3] - pb[1]
pb_cx = (pb[0] + pb[2]) / 2
pb_cy = (pb[1] + pb[3]) / 2
bbox_cx = var[0] * loc[0] * pb_w + pb_cx
bbox_cy = var[1] * loc[1] * pb_h + pb_cy
bbox_w = np.exp(var[2] * loc[2]) * pb_w
bbox_h = np.exp(var[3] * loc[3]) * pb_h
bboxes[i, :] = [
bbox_cx - bbox_w / 2,
bbox_cy - bbox_h / 2,
bbox_cx + bbox_w / 2,
bbox_cy + bbox_h / 2,
]
# sort and nms for each class
all_class_bbox_rects = [[]]
all_class_bbox_scores = [[]]
# start from 1 to ignore background class
for i in range(1, num_class):
# filter by confidence_threshold
class_bbox_rects = []
class_bbox_scores = []
for j in range(num_prior):
# prob data layout
# num_class x num_prior
score = confidence[j, i]
if score > confidence_threshold:
bbox = bboxes[j]
class_bbox_rects.append((bbox[0], bbox[1], bbox[2], bbox[3], i))
class_bbox_scores.append(score)
idx = np.argsort(class_bbox_scores)[::-1]
class_bbox_rects = np.asarray(class_bbox_rects)[idx]
class_bbox_scores = np.asarray(class_bbox_scores)[idx]
class_bbox_rects = class_bbox_rects[:nms_top_k]
class_bbox_scores = class_bbox_scores[:nms_top_k]
BBoxRect = namedtuple('BBoxRect', ['xmin', 'ymin', 'xmax', 'ymax', 'label'])
class_bbox_rects = [
BBoxRect(*b) for b in class_bbox_rects
]
# apply nms
picked = nms_sorted_bboxes(class_bbox_rects, nms_threshold)
# select
all_class_bbox_rects.append([])
all_class_bbox_scores.append([])
for z in picked:
all_class_bbox_rects[i].append(class_bbox_rects[z])
all_class_bbox_scores[i].append(class_bbox_scores[z])
# gather all class
bbox_rects = []
bbox_scores = []
for i in range(1, num_class):
class_bbox_rects = all_class_bbox_rects[i]
class_bbox_scores = all_class_bbox_scores[i]
bbox_rects.extend(class_bbox_rects)
bbox_scores.extend(class_bbox_scores)
idx = np.argsort(bbox_scores)[::-1]
bbox_rects = [bbox_rects[i] for i in idx]
bbox_scores = [bbox_scores[i] for i in idx]
bbox_rects = bbox_rects[:keep_top_k]
bbox_scores = bbox_scores[:keep_top_k]
detections = []
for i in range(len(bbox_rects)):
r = bbox_rects[i]
score = bbox_scores[i]
detections.append([
r.label,
score,
r.xmin, r.ymin,
r.xmax, r.ymax
])
return detections