-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
167 lines (131 loc) · 4.58 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
import numpy as np
import torch
import logging
import losses
import json
from tqdm import tqdm
import torch.nn.functional as F
import math
def l2_norm(input):
input_size = input.size()
buffer = torch.pow(input, 2)
normp = torch.sum(buffer, 1).add_(1e-12)
norm = torch.sqrt(normp)
_output = torch.div(input, norm.view(-1, 1).expand_as(input))
output = _output.view(input_size)
return output
def calc_recall_at_k(T, Y, k):
"""
T : [nb_samples] (target labels)
Y : [nb_samples x k] (k predicted labels/neighbours)
"""
count = 0
for t,y in zip(T,Y):
if t in torch.Tensor(y).long()[:k]:
count += 1
return count / (1. * len(T))
def predict_batchwise(model, dataloader):
device = "cuda"
model_is_training = model.training
model.eval()
ds = dataloader.dataset
A = [[] for i in range(len(ds[0]))]
with torch.no_grad():
# extract batches (A becomes list of samples)
for batch in tqdm(dataloader):
for i, J in enumerate(batch):
# i = 0: sz_batch * images
# i = 1: sz_batch * labels
# i = 2: sz_batch * indices
if i == 0:
# move images to device of model (approximate device)
J = model(J.cuda())
for j in J:
A[i].append(j)
model.train()
model.train(model_is_training) # revert to previous training state
return [torch.stack(A[i]) for i in range(len(A))]
def proxy_init_calc(model, dataloader):
nb_classes = dataloader.dataset.nb_classes()
X, T, *_ = predict_batchwise(model, dataloader)
proxy_mean = torch.stack([X[T==class_idx].mean(0) for class_idx in range(nb_classes)])
return proxy_mean
def evaluate_cos(model, dataloader):
nb_classes = dataloader.dataset.nb_classes()
# calculate embeddings with model and get targets
X, T = predict_batchwise(model, dataloader)
X = l2_norm(X)
# get predictions by assigning nearest 8 neighbors with cosine
K = 32
Y = []
xs = []
cos_sim = F.linear(X, X)
Y = T[cos_sim.topk(1 + K)[1][:,1:]]
Y = Y.float().cpu()
recall = []
for k in [1, 2, 4, 8, 16, 32]:
r_at_k = calc_recall_at_k(T, Y, k)
recall.append(r_at_k)
print("R@{} : {:.3f}".format(k, 100 * r_at_k))
return recall
def evaluate_cos_Inshop(model, query_dataloader, gallery_dataloader):
nb_classes = query_dataloader.dataset.nb_classes()
# calculate embeddings with model and get targets
query_X, query_T = predict_batchwise(model, query_dataloader)
gallery_X, gallery_T = predict_batchwise(model, gallery_dataloader)
query_X = l2_norm(query_X)
gallery_X = l2_norm(gallery_X)
# get predictions by assigning nearest 8 neighbors with cosine
K = 50
Y = []
xs = []
cos_sim = F.linear(query_X, gallery_X)
def recall_k(cos_sim, query_T, gallery_T, k):
m = len(cos_sim)
match_counter = 0
for i in range(m):
pos_sim = cos_sim[i][gallery_T == query_T[i]]
neg_sim = cos_sim[i][gallery_T != query_T[i]]
thresh = torch.max(pos_sim).item()
if torch.sum(neg_sim > thresh) < k:
match_counter += 1
return match_counter / m
# calculate recall @ 1, 2, 4, 8
recall = []
for k in [1, 10, 20, 30, 40, 50]:
r_at_k = recall_k(cos_sim, query_T, gallery_T, k)
recall.append(r_at_k)
print("R@{} : {:.3f}".format(k, 100 * r_at_k))
return recall
def evaluate_cos_SOP(model, dataloader):
nb_classes = dataloader.dataset.nb_classes()
# calculate embeddings with model and get targets
X, T = predict_batchwise(model, dataloader)
X = l2_norm(X)
# get predictions by assigning nearest 8 neighbors with cosine
K = 1000
Y = []
xs = []
for x in X:
if len(xs)<10000:
xs.append(x)
else:
xs.append(x)
xs = torch.stack(xs,dim=0)
cos_sim = F.linear(xs,X)
y = T[cos_sim.topk(1 + K)[1][:,1:]]
Y.append(y.float().cpu())
xs = []
# Last Loop
xs = torch.stack(xs,dim=0)
cos_sim = F.linear(xs,X)
y = T[cos_sim.topk(1 + K)[1][:,1:]]
Y.append(y.float().cpu())
Y = torch.cat(Y, dim=0)
# calculate recall @ 1, 2, 4, 8
recall = []
for k in [1, 10, 100, 1000]:
r_at_k = calc_recall_at_k(T, Y, k)
recall.append(r_at_k)
print("R@{} : {:.3f}".format(k, 100 * r_at_k))
return recall