-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathHICO_DET_utils.py
234 lines (212 loc) · 7.83 KB
/
HICO_DET_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
import numpy as np
import pickle
rare_index = np.array([ 9, 23,28, 45,51, 56,63, 64,67, 71,77, 78,81, 84,85, 91,100,101,105,108,113,128,136,137,150,159,166,167,169,173,180,182,185,189,190,193,196,199,206,207,215,217,223,228,230,239,240,255,256,258,261,262,263,275,280,281,282,287,290,293,304,312,316,318,326,329,334,335,346,351,352,355,359,365,380,382,390,391,392,396,398,399,400,402,403,404,405,406,408,411,417,419,427,428,430,432,437,440,441,450,452,464,470,475,483,486,499,500,505,510,515,518,521,523,527,532,536,540,547,548,549,550,551,552,553,556,557,561,579,581,582,587,593,594,596,597,598,600,]) - 1
rare = np.zeros(600)
rare[rare_index] += 2
obj_range = [
(161, 170), (11, 24), (66, 76), (147, 160), (1, 10),
(55, 65), (187, 194), (568, 576), (32, 46), (563, 567),
(326, 330), (503, 506), (415, 418), (244, 247), (25, 31),
(77, 86), (112, 129), (130, 146), (175, 186), (97, 107),
(314, 325), (236, 239), (596, 600), (343, 348), (209, 214),
(577, 584), (353, 356), (539, 546), (507, 516), (337, 342),
(464, 474), (475, 483), (489, 502), (369, 376), (225, 232),
(233, 235), (454, 463), (517, 528), (534, 538), (47, 54),
(589, 595), (296, 305), (331, 336), (377, 383), (484, 488),
(253, 257), (215, 224), (199, 208), (439, 445), (398, 407),
(258, 264), (274, 283), (357, 363), (419, 429), (306, 313),
(265, 273), (87, 92), (93, 96), (171, 174), (240, 243),
(108, 111), (551, 558), (195, 198), (384, 389), (394, 397),
(435, 438), (364, 368), (284, 290), (390, 393), (408, 414),
(547, 550), (450, 453), (430, 434), (248, 252), (291, 295),
(585, 588), (446, 449), (529, 533), (349, 352), (559, 562)
]
hoi_no_inter_all = [
10,24,31,46,54,65,76,86,92,96,107,111,129,146,160,170,174,186,194,198,208,214,
224,232,235,239,243,247,252,257,264,273,283,290,295,305,313,325,330,336,342,348,
352,356,363,368,376,383,389,393,397,407,414,418,429,434,438,445,449,453,463,474,
483,488,502,506,516,528,533,538,546,550,558,562,567,576,584,588,595,600
]
def getSigmoid(b,c,d,x,a=6):
e = 2.718281828459
return a/(1+e**(b-c*x))+d
def iou(bb1, bb2, debug = False):
x1 = bb1[2] - bb1[0]
y1 = bb1[3] - bb1[1]
if x1 < 0:
x1 = 0
if y1 < 0:
y1 = 0
x2 = bb2[1] - bb2[0]
y2 = bb2[3] - bb2[2]
if x2 < 0:
x2 = 0
if y2 < 0:
y2 = 0
xiou = min(bb1[2], bb2[1]) - max(bb1[0], bb2[0])
yiou = min(bb1[3], bb2[3]) - max(bb1[1], bb2[2])
if xiou < 0:
xiou = 0
if yiou < 0:
yiou = 0
if debug:
print(x1, y1, x2, y2, xiou, yiou)
print(x1 * y1, x2 * y2, xiou * yiou)
if xiou * yiou <= 0:
return 0
else:
return xiou * yiou / (x1 * y1 + x2 * y2 - xiou * yiou)
def calc_hit(det, gtbox):
gtbox = gtbox.astype(np.float64)
hiou = iou(det[:4], gtbox[:4])
oiou = iou(det[4:], gtbox[4:])
return min(hiou, oiou)
def calc_ap(score, bboxes, keys, hoi_id):
hit = []
idx = np.argsort(score)[::-1]
gt_bbox = pickle.load(open('gt_hoi_py2/hoi_%d.pkl' % hoi_id, 'rb'), encoding='latin1')
npos = 0
used = {}
for key in gt_bbox.keys():
npos += gt_bbox[key].shape[0]
used[key] = set()
if len(idx) == 0:
return 0, 0
for i in range(min(len(idx), 19999)):
pair_id = idx[i]
bbox = bboxes[pair_id, :]
key = keys[pair_id]
if key in gt_bbox:
maxi = 0.0
k = -1
for i in range(gt_bbox[key].shape[0]):
tmp = calc_hit(bbox, gt_bbox[key][i, :])
if maxi < tmp:
maxi = tmp
k = i
if k in used[key] or maxi < 0.5:
hit.append(0)
else:
hit.append(1)
used[key].add(k)
else:
hit.append(0)
bottom = np.array(range(len(hit))) + 1
hit = np.cumsum(hit)
rec = hit / npos
prec = hit / bottom
ap = 0.0
for i in range(11):
mask = rec >= (i / 10.0)
if np.sum(mask) > 0:
ap += np.max(prec[mask]) / 11.0
return ap, np.max(rec)
def calc_ap_ko(score, bboxes, keys, hoi_id, ko_mask):
hit, hit_ko = [], []
idx = np.argsort(score)[::-1]
gt_bbox = pickle.load(open('gt_hoi_py2/hoi_%d.pkl' % hoi_id, 'rb'))
npos = 0
used = {}
for key in gt_bbox.keys():
npos += gt_bbox[key].shape[0]
used[key] = set()
if len(idx) == 0:
output = {
'ap' : 0, 'rec': 0, 'ap_ko': 0, 'rec_ko': 0
}
return output
for i in range(min(len(idx), 19999)):
pair_id = idx[i]
bbox = bboxes[pair_id, :]
key = keys[pair_id]
if key in gt_bbox:
maxi = 0.0
k = -1
for i in range(gt_bbox[key].shape[0]):
tmp = calc_hit(bbox, gt_bbox[key][i, :])
if maxi < tmp:
maxi = tmp
k = i
if k in used[key] or maxi < 0.5:
hit.append(0)
hit_ko.append(0)
else:
hit.append(1)
hit_ko.append(1)
used[key].add(k)
else:
hit.append(0)
if key in ko_mask:
hit_ko.append(0)
bottom = np.array(range(len(hit))) + 1
hit = np.cumsum(hit)
rec = hit / npos
prec = hit / bottom
ap = 0.0
for i in range(11):
mask = rec >= (i / 10.0)
if np.sum(mask) > 0:
ap += np.max(prec[mask]) / 11.0
if len(hit_ko) == 0:
output = {
'ap' : ap, 'rec': np.max(rec), 'ap_ko': 0, 'rec_ko': 0
}
return output
bottom_ko = np.array(range(len(hit_ko))) + 1
hit_ko = np.cumsum(hit_ko)
rec_ko = hit_ko / npos
prec_ko = hit_ko / bottom_ko
ap_ko = 0.0
for i in range(11):
mask = rec_ko >= (i / 10.)
if np.sum(mask) > 0:
ap_ko += np.max(prec_ko[mask]) / 11.
output = {
'ap' : ap, 'rec': np.max(rec), 'ap_ko': ap_ko, 'rec_ko': np.max(rec_ko)
}
return output
def get_map(keys, scores, bboxes):
map = np.zeros(600)
mrec = np.zeros(600)
for i in range(80):
if len(scores[i]) == 0:
continue
begin = obj_range[i][0] - 1
end = obj_range[i][1]
for hoi_id in range(begin, end):
score = scores[i][:, hoi_id - begin]
bbox = bboxes[i]
key = keys[i]
map[hoi_id], mrec[hoi_id] = calc_ap(score, bbox, key, hoi_id)
return map, mrec
def get_mpd(res):
verb_mapping = pickle.load(open('verb_mapping.pkl', 'rb'), encoding='latin1')
mapping = {}
for i in range(1, 117):
mapping[i] = np.where(verb_mapping[i] > 0)[0]
selected = [i for i in range(1, 117) if len(mapping[i]) > 1]
res = pickle.load(open('hico_coco_vcl_map.pkl', 'rb'))
mpd = 0.0
for i in selected:
m = np.max(res[mapping[i]])
if m == 0:
mpd += 1.
else:
mpd += (m - np.mean(res[mapping[i]])) / m
mpd /= len(selected)
return mpd
def get_map_ko(keys, scores, bboxes):
map, map_ko = np.zeros(600), np.zeros(600)
mrec, mrec_ko = np.zeros(600), np.zeros(600)
for i in range(80):
begin = obj_range[i][0] - 1
end = obj_range[i][1]
ko_mask = []
for hoi_id in range(begin, end):
gt_bbox = pickle.load(open('gt_hoi_py2/hoi_%d.pkl' % hoi_id, 'rb'), encoding='latin1')
ko_mask += list(gt_bbox.keys())
ko_mask = set(ko_mask)
for hoi_id in range(begin, end):
output = calc_ap_ko(scores[i][:, hoi_id - begin], bboxes[i], keys[i], hoi_id, ko_mask)
map[hoi_id], mrec[hoi_id], map_ko[hoi_id], mrec_ko[hoi_id] = output['ap'], output['rec'], output['ap_ko'], output['rec_ko']
return map, mrec, map_ko, mrec_ko