-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.py
130 lines (103 loc) · 3.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
import torch
import glob
import re
import numpy as np
import torch.nn.functional as F
def euclidean_metric(a, b):
n = a.shape[0]
m = b.shape[0]
a = a.unsqueeze(1).expand(n, m, -1)
b = b.unsqueeze(0).expand(n, m, -1)
logits = -((a - b) ** 2).sum(dim=2)
return logits
def decode(proto, data_query, temperature=1):
logits = (
euclidean_metric(data_query, proto)
/ temperature
)
return logits
def cosine_decode(proto, data_query, temperature=1):
proto=F.normalize(proto)
data_query=F.normalize(data_query)
logits = (torch.mm(data_query,proto.t())/ temperature)
return logits
def float_or_string(arg):
try:
return float(arg)
except (ValueError, TypeError):
return arg
def flip_img(img):
inv_idx = torch.arange(img.size(3)-1, -1, -1).long() # N x C x H x W
img_flipped = img.index_select(3, inv_idx)
return img_flipped
def get_id(img_path, dataset):
camera_id = []
labels = []
for path in img_path:
filename = path.split('/')[-1]
label = filename[0:4]
camera = filename.split('c')[1]
if label[0:2] == '-1':
labels.append(-1)
else:
labels.append(int(label))
camera_id.append(int(camera[0]))
return camera_id, labels
def get_label_camera(dataset, imgs):
camera_id = []
labels = []
for path in imgs:
filename = path.split('/')[-1]
camera = dataset.camera(filename)
camera_id.append(int(camera))
label = dataset.id(filename)
labels.append(int(label))
return camera_id, labels
def get_camera(dataset, imgs):
camera_id = []
for path in imgs:
filename = path.split('/')[-1]
camera = dataset.camera(filename)
camera_id.append(int(camera))
return camera_id
def compute_map(index, good_index, junk_index):
ap = 0
cmc = torch.IntTensor(len(index)).zero_()
if good_index.size==0: # if empty
cmc[0] = -1
return ap, cmc
# remove junk_index
mask = np.in1d(index, junk_index, invert=True)
index = index[mask]
# find good_index
ngood = len(good_index)
mask = np.in1d(index, good_index)
rows_good = np.argwhere(mask==True)
rows_good = rows_good.flatten()
cmc[rows_good[0]:] = 1
for i in range(ngood):
d_recall = 1.0 / ngood
precision = (i+1) * 1.0 / (rows_good[i] + 1)
if rows_good[i] != 0:
old_precision = i * 1.0 / rows_good[i]
else:
old_precision = 1.0
ap = ap + d_recall * (old_precision + precision) / 2
return ap, cmc
def get_label_camera(dataset, imgs):
camera_id = []
labels = []
for path in imgs:
filename = path.split('/')[-1]
camera = dataset.camera(filename)
camera_id.append(int(camera))
label = dataset.id(filename)
labels.append(int(label))
return camera_id, labels
def get_camera(dataset, imgs):
camera_id = []
for path in imgs:
filename = path.split('/')[-1]
camera = dataset.camera(filename)
camera_id.append(int(camera))
return camera_id