-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathutils.py
379 lines (343 loc) · 11.4 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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
"""
create by Qiang Zhang
function: provide some util apis
"""
import torch
import random
import pickle
import math
import numpy as np
import numpy.linalg as linagy
from sklearn.metrics import average_precision_score
def readpkl(file):
fr = open(file,'rb')
data = pickle.load(fr,encoding='latin1')
return data
def _compute_iou(a, b):
x1 = max(a[0], b[0])
y1 = max(a[1], b[1])
x2 = min(a[2], b[2])
y2 = min(a[3], b[3])
inter = max(0, x2 - x1) * max(0, y2 - y1)
union = (a[2] - a[0]) * (a[3] - a[1]) + (b[2] - b[0]) * (b[3] - b[1]) - inter
return inter * 1.0 / union
def _compute_dist(a, b):
center0 = (a[0]+a[2])/2,(a[1]+a[3])/2
center1 = (b[0]+b[2])/2,(b[1]+b[3])/2
return (center0[0]-center1[0])**2+(center0[1]-center1[1])**2
def select_p(det,feat_p,roi):
def _compute_comp(a, b,iou_thresh=0.0):
x1 = max(a[0], b[0])
y1 = max(a[1], b[1])
x2 = min(a[2], b[2])
y2 = min(a[3], b[3])
inter = max(0, x2 - x1) * max(0, y2 - y1)
union = (a[2] - a[0]) * (a[3] - a[1]) + (b[2] - b[0]) * (b[3] - b[1]) - inter
iou = inter * 1.0 / union
if iou>iou_thresh:
return 0
else:
return a[4]
detN = det.shape[0]
if detN==1:
feat1 = feat_p[0]
feat3 = [feat_p[0],feat_p[0],feat_p[0]]
elif detN==2:
feat1 = feat_p[0]
feat3 = [feat_p[0], feat_p[1], feat_p[1]]
else:
index = np.zeros(detN)
for i in range(detN):
index[i] = _compute_iou(det[i],roi)
# index[i] = _compute_comp(det[i], roi)
index = [[x, i] for i, x in enumerate(index)]
index = sorted(index, key=lambda x: x[0], reverse=True)
feat1 = feat_p[index[0][1]]
feat3 = [feat_p[index[0][1]],feat_p[index[1][1]],feat_p[index[2][1]]]
index = np.zeros(detN)
for i in range(detN):
# index[i] = _compute_iou(det[i],roi)
index[i] = _compute_comp(det[i], roi)
index = [[x, i] for i, x in enumerate(index)]
index = sorted(index, key=lambda x: x[0], reverse=True)
feat3 = [feat3[0], feat_p[index[1][1]], feat_p[index[2][1]]]
return feat1,np.asarray(feat3)
def select_p_4(det,feat_p,roi):
def _compute_comp(a, b,iou_thresh=0.0):
x1 = max(a[0], b[0])
y1 = max(a[1], b[1])
x2 = min(a[2], b[2])
y2 = min(a[3], b[3])
inter = max(0, x2 - x1) * max(0, y2 - y1)
union = (a[2] - a[0]) * (a[3] - a[1]) + (b[2] - b[0]) * (b[3] - b[1]) - inter
iou = inter * 1.0 / union
if iou>iou_thresh:
return 0
else:
return a[4]
detN = det.shape[0]
if detN==1:
feat1 = feat_p[0]
feat4 = [feat_p[0],feat_p[0],feat_p[0],feat_p[0]]
elif detN==2:
feat1 = feat_p[0]
feat4 = [feat_p[0], feat_p[1], feat_p[1],feat_p[1]]
elif detN==3:
feat1 = feat_p[0]
feat4 = [feat_p[0], feat_p[1], feat_p[2],feat_p[2]]
else:
index = np.zeros(detN)
for i in range(detN):
index[i] = _compute_iou(det[i],roi)
index = [[x, i] for i, x in enumerate(index)]
index = sorted(index, key=lambda x: x[0], reverse=True)
feat1 = feat_p[index[0][1]]
feat4 = [feat_p[index[0][1]],feat_p[index[1][1]],feat_p[index[2][1]]]
index = np.zeros(detN)
for i in range(detN):
index[i] = _compute_comp(det[i], roi)
index = [[x, i] for i, x in enumerate(index)]
index = sorted(index, key=lambda x: x[0], reverse=True)
feat4 = [feat4[0], feat_p[index[1][1]], feat_p[index[2][1]],feat_p[index[3][1]]]
return feat1,np.asarray(feat4)
def select_p_n(det,feat_p,roi,num=4):
def _compute_comp(a, b,iou_thresh=0.0):
x1 = max(a[0], b[0])
y1 = max(a[1], b[1])
x2 = min(a[2], b[2])
y2 = min(a[3], b[3])
inter = max(0, x2 - x1) * max(0, y2 - y1)
union = (a[2] - a[0]) * (a[3] - a[1]) + (b[2] - b[0]) * (b[3] - b[1]) - inter
iou = inter * 1.0 / union
if iou>iou_thresh:
return 0
else:
return a[4]
detN = det.shape[0]
if detN==1:
feat1 = feat_p[0]
featn = [feat_p[0]]*num
return feat1,np.asarray(featn)
index = np.zeros(detN)
for i in range(detN):
index[i] = _compute_iou(det[i],roi)
index = [[x, i] for i, x in enumerate(index)]
index = sorted(index, key=lambda x: x[0], reverse=True)
feat1 = feat_p[index[0][1]]
index = np.zeros(detN)
for i in range(detN):
index[i] = _compute_comp(det[i], roi)
index = [[x, i] for i, x in enumerate(index)]
index = sorted(index, key=lambda x: x[0], reverse=True)
featn = [feat1]
for i in range(num-1):
if i>=detN-1:
featn.append(feat_p[index[1][1]])
else:
featn.append(feat_p[index[i+1][1]])
return feat1,np.asarray(featn)
def Eval(label,pred, topk):
# acc metric
#topk = [1, 2, 3, 4, 5, 10]
y_score = np.asarray(pred)
y_true = np.asarray(label)
ap = 0 if sum(y_true) == 0 else \
average_precision_score(y_true, y_score)
inds = np.argsort(y_score)[::-1]
y_score = y_score[inds]
y_true = y_true[inds]
acc = [min(1, sum(y_true[:k])) for k in topk]
return ap, acc
def Eval_det(label,pred,det,num=100):
correct = np.zeros([pred.shape[0],num+1])
correct0 = np.zeros(num+1)
for i in range(label.shape[0]):
ids = np.where(np.asarray(det[i])==1)[0]
# for item in ids:
# id = np.where(pred[i]==item)
# correct[i][id] += 1
for j in range(1, num + 1):
if any([item in pred[i][0:j] for item in ids]):
correct[i][j] += 1
# for j in range(1, num + 1):
# if min(pred[i][0:j])<=sum(label[i]) and det[i][min(pred[i][0:j])]:
# correct[i][j] += 1
for j in range(1,num+1):
correct0[j] = float(correct[:,j].sum())/label.shape[0]
return correct0[1:]
def shuffle0(data, label):
ids = range(data.shape[0])
random.shuffle(ids)
ids = torch.tensor(ids).cuda()
data = torch.index_select(data, 0, ids)
label = torch.index_select(label, 0, ids)
return data, label, ids.sort()[1]
def Sim2(feat1,feat2):
sim = -np.linalg.norm(feat1 - feat2)
return sim
def Sim1(feat1,feat2):
sim = feat1.dot(feat2)/(linagy.norm(feat1)*linagy.norm(feat2))
return sim
def gallery_gcn_det(probe,gallery_feat,num=3):
detsave = [[],[],[],[],[],[],[],[],[],[]]
featuresave = [[],[],[],[],[],[],[],[],[],[]]
for j,item in enumerate(gallery_feat):
for i in range(num):
sim0 = -1
id0 = 0
try:
nowfeature = item[0]
except:
continue
for id,person in enumerate(item):
sim = Sim1(probe[i],person)
if sim>sim0:
sim0 = sim
id0 =id
nowfeature = person
for id, person in enumerate(item):
if i == 0:
featuresave[i].append(person)
else:
featuresave[i].append(nowfeature)
#detsave[i].append(id0)
#featuresave[i].append(nowfeature)
abc_data = []
count = 0
for i in range(len(gallery_feat)):
for k in range(gallery_feat[i].shape[0]):
feature = []
for j in range(num):
try:
feature.append(featuresave[j][count])
except:
break
else:
abc_data.append(np.asarray(feature))
count += 1
abc_data = np.asarray(abc_data)
return abc_data
def gallery_gcn(probe,gallery_feat,num=3):
if len(gallery_feat) == 99:
gallery_feat.append(gallery_feat[-1])
featuresave = [[],[],[],[],[],[],[],[],[],[]]
for i in range(num):
for j,item in enumerate(gallery_feat):
sim0 = -1
try:
nowfeature = item[0]
except:
continue
for id,person in enumerate(item):
sim = Sim1(probe[i],person)
if sim>sim0:
sim0 = sim
nowfeature = person
featuresave[i].append(nowfeature)
abc_data = []
for i in range(len(gallery_feat)):
feature = []
for j in range(num):
try:
feature.append(featuresave[j][i])
except:
break
else:
abc_data.append(np.asarray(feature))
if len(abc_data)==99:
abc_data.append(abc_data[-1])
abc_data = np.asarray(abc_data)
return abc_data
def Rank0(probe,gallery_feat):
"""
:param probe: probe feat:3 x 256
:param gallery_feat: 100 x 256
:return: baseline method , new method calculated by math formula
"""
if len(gallery_feat) == 99:
gallery_feat.append(gallery_feat[-1])
if len(gallery_feat[0]) == 0:
gallery_feat[0] = gallery_feat[-1]
index1 = []
idsave1 = []
featuresave1 = []
for item in gallery_feat:
sim0 = -1
nowid = 0
nowfeature = item[0]
for id,person in enumerate(item):
sim = Sim2(probe[0],person)
# sim = np.random.rand(1)[0]
if sim>sim0:
sim0 = sim
nowid = id
nowfeature = person
index1.append(sim0)
idsave1.append(nowid)
featuresave1.append(nowfeature)
index0 = np.asarray(index1)
index0 = np.argsort(-index0)
index2 = []
idsave2 = []
featuresave2 = []
for item in gallery_feat:
sim0 = -1
nowid = 0
nowfeature = item[0]
for id,person in enumerate(item):
if id==idsave1[id]:
continue
sim = Sim2(probe[1],person)
# sim = np.random.rand(1)[0]
if sim>sim0:
sim0 = sim
nowid = id
nowfeature = person
index2.append(sim0)
idsave2.append(nowid)
featuresave2.append(nowfeature)
# index2 = np.asarray(index2)
# index2 = np.argsort(-index2)
index3 = []
idsave3 = []
featuresave3 = []
for item in gallery_feat:
sim0 = -1
nowid = 0
nowfeature = item[0]
for id,person in enumerate(item):
if id == idsave1[id]:
continue
if id == idsave2[id]:
continue
sim = Sim2(probe[2],person)
# sim = np.random.rand(1)[0]
if sim>sim0:
sim0 = sim
nowid = id
nowfeature = person
index3.append(sim0)
idsave3.append(nowid)
featuresave3.append(nowfeature)
# index3 = np.asarray(index3)
# index3 = np.argsort(-index3)
index_all = []
for i in range(len(index1)):
a = index1[i]
b = index2[i]
c = index3[i]
tmp = a*(math.exp(b*b)+0.3*math.exp(c*c)+2.5)
index_all.append(tmp)
index_all = np.asarray(index_all)
index_all = np.argsort(-index_all)
return index0+1,index_all+1
def padding(gallery):
maxlen = 0
for item in gallery:
if item.shape[0]>maxlen:
maxlen = item.shape[0]
tmp = []
for i,item in enumerate(gallery):
gallery[i] = np.concatenate((item,np.zeros((maxlen-item.shape[0],256))),0)
tmp.append(gallery[i])
return np.asarray(tmp)