forked from SeuTao/FaceBagNet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
metric.py
209 lines (156 loc) · 5.98 KB
/
metric.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
import numpy as np
import torch
from scipy import interpolate
from tqdm import tqdm
def calculate_accuracy(threshold, dist, actual_issame):
predict_issame = np.less(1-dist, 1-threshold)
tp = np.sum(np.logical_and(predict_issame, actual_issame))
fp = np.sum(np.logical_and(predict_issame, np.logical_not(actual_issame)))
tn = np.sum(np.logical_and(np.logical_not(predict_issame), np.logical_not(actual_issame)))
fn = np.sum(np.logical_and(np.logical_not(predict_issame), actual_issame))
tpr = 0 if (tp +fn==0) else float(tp) / float(tp +fn)
fpr = 0 if (fp +tn==0) else float(fp) / float(fp +tn)
acc = float(tp +tn ) /dist.shape[0]
return tpr, fpr, acc
def calculate(threshold, dist, actual_issame):
predict_issame = np.less(1-dist, 1-threshold)
tp = np.sum(np.logical_and(predict_issame, actual_issame))
fp = np.sum(np.logical_and(predict_issame, np.logical_not(actual_issame)))
tn = np.sum(np.logical_and(np.logical_not(predict_issame), np.logical_not(actual_issame)))
fn = np.sum(np.logical_and(np.logical_not(predict_issame), actual_issame))
return tp,fp,tn,fn
def ACER(threshold, dist, actual_issame):
tp, fp, tn, fn = calculate(threshold, dist, actual_issame)
apcer = fp / (tn*1.0 + fp*1.0)
npcer = fn / (fn * 1.0 + tp * 1.0)
acer = (apcer + npcer) / 2.0
return acer,tp, fp, tn,fn
def TPR_FPR( dist, actual_issame, fpr_target = 0.001):
# acer_min = 1.0
# thres_min = 0.0
# re = []
# Positive
# Rate(FPR):
# FPR = FP / (FP + TN)
# Positive
# Rate(TPR):
# TPR = TP / (TP + FN)
thresholds = np.arange(0.0, 1.0, 0.001)
nrof_thresholds = len(thresholds)
fpr = np.zeros(nrof_thresholds)
FPR = 0.0
for threshold_idx, threshold in enumerate(thresholds):
if threshold < 1.0:
tp, fp, tn, fn = calculate(threshold, dist, actual_issame)
FPR = fp / (fp*1.0 + tn*1.0)
TPR = tp / (tp*1.0 + fn*1.0)
fpr[threshold_idx] = FPR
if np.max(fpr) >= fpr_target:
f = interpolate.interp1d(np.asarray(fpr), thresholds, kind= 'slinear')
threshold = f(fpr_target)
else:
threshold = 0.0
tp, fp, tn, fn = calculate(threshold, dist, actual_issame)
FPR = fp / (fp * 1.0 + tn * 1.0)
TPR = tp / (tp * 1.0 + fn * 1.0)
print(str(FPR)+' '+str(TPR))
return FPR,TPR
import torch.nn.functional as F
def metric(logit, truth):
prob = F.softmax(logit, 1)
value, top = prob.topk(1, dim=1, largest=True, sorted=True)
correct = top.eq(truth.view(-1, 1).expand_as(top))
correct = correct.data.cpu().numpy()
correct = np.mean(correct)
return correct, prob
def do_valid( net, test_loader, criterion ):
valid_num = 0
losses = []
corrects = []
probs = []
labels = []
for input, truth in test_loader:
b,n,c,w,h = input.size()
input = input.view(b*n,c,w,h)
input = input.cuda()
truth = truth.cuda()
with torch.no_grad():
logit,_,_ = net(input)
logit = logit.view(b,n,2)
logit = torch.mean(logit, dim = 1, keepdim = False)
truth = truth.view(logit.shape[0])
loss = criterion(logit, truth, False)
correct, prob = metric(logit, truth)
valid_num += len(input)
losses.append(loss.data.cpu().numpy())
corrects.append(np.asarray(correct).reshape([1]))
probs.append(prob.data.cpu().numpy())
labels.append(truth.data.cpu().numpy())
# assert(valid_num == len(test_loader.sampler))
#----------------------------------------------
correct = np.concatenate(corrects)
loss = np.concatenate(losses)
loss = loss.mean()
correct = np.mean(correct)
probs = np.concatenate(probs)
labels = np.concatenate(labels)
tpr, fpr, acc = calculate_accuracy(0.5, probs[:,1], labels)
acer,_,_,_,_ = ACER(0.5, probs[:, 1], labels)
valid_loss = np.array([
loss, acer, acc, correct
])
return valid_loss,[probs[:, 1], labels]
def do_valid_test( net, test_loader, criterion ):
valid_num = 0
losses = []
corrects = []
probs = []
labels = []
for i, (input, truth) in enumerate(tqdm(test_loader)):
# for input, truth in test_loader:
b,n,c,w,h = input.size()
input = input.view(b*n,c,w,h)
input = input.cuda()
truth = truth.cuda()
with torch.no_grad():
logit,_,_ = net(input)
logit = logit.view(b,n,2)
logit = torch.mean(logit, dim = 1, keepdim = False)
truth = truth.view(logit.shape[0])
loss = criterion(logit, truth, False)
correct, prob = metric(logit, truth)
valid_num += len(input)
losses.append(loss.data.cpu().numpy())
corrects.append(np.asarray(correct).reshape([1]))
probs.append(prob.data.cpu().numpy())
labels.append(truth.data.cpu().numpy())
# assert(valid_num == len(test_loader.sampler))
#----------------------------------------------
correct = np.concatenate(corrects)
loss = np.concatenate(losses)
loss = loss.mean()
correct = np.mean(correct)
probs = np.concatenate(probs)
labels = np.concatenate(labels)
tpr, fpr, acc = calculate_accuracy(0.5, probs[:,1], labels)
acer,_,_,_,_ = ACER(0.5, probs[:, 1], labels)
valid_loss = np.array([
loss, acer, acc, correct
])
return valid_loss,[probs[:, 1], labels]
def infer_test( net, test_loader):
valid_num = 0
probs = []
for i, (input, truth) in enumerate(tqdm(test_loader)):
b,n,c,w,h = input.size()
input = input.view(b*n,c,w,h)
input = input.cuda()
with torch.no_grad():
logit,_,_ = net(input)
logit = logit.view(b,n,2)
logit = torch.mean(logit, dim = 1, keepdim = False)
prob = F.softmax(logit, 1)
valid_num += len(input)
probs.append(prob.data.cpu().numpy())
probs = np.concatenate(probs)
return probs[:, 1]