This repository was archived by the owner on Oct 31, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 103
/
Copy pathcalMetric.py
333 lines (300 loc) · 10.5 KB
/
calMetric.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
# -*- coding: utf-8 -*-
# Copyright (c) 2017-present, Facebook, Inc.
# All rights reserved.
#
# This source code is licensed under the license found in the
# LICENSE file in the root directory of this source tree.
#
"""
Created on Sat Sep 19 20:55:56 2015
@author: liangshiyu
"""
from __future__ import print_function
import torch
from torch.autograd import Variable
import torch.nn as nn
import torch.nn.functional as F
import numpy as np
import torch.optim as optim
import torchvision
import torchvision.transforms as transforms
#import matplotlib.pyplot as plt
import numpy as np
import time
from scipy import misc
def tpr95(name):
#calculate the falsepositive error when tpr is 95%
# calculate baseline
T = 1
cifar = np.loadtxt('./softmax_scores/confidence_Base_In.txt', delimiter=',')
other = np.loadtxt('./softmax_scores/confidence_Base_Out.txt', delimiter=',')
if name == "CIFAR-10":
start = 0.1
end = 1
if name == "CIFAR-100":
start = 0.01
end = 1
gap = (end- start)/100000
#f = open("./{}/{}/T_{}.txt".format(nnName, dataName, T), 'w')
Y1 = other[:, 2]
X1 = cifar[:, 2]
total = 0.0
fpr = 0.0
for delta in np.arange(start, end, gap):
tpr = np.sum(np.sum(X1 >= delta)) / np.float(len(X1))
error2 = np.sum(np.sum(Y1 > delta)) / np.float(len(Y1))
if tpr <= 0.9505 and tpr >= 0.9495:
fpr += error2
total += 1
fprBase = fpr/total
# calculate our algorithm
T = 1000
cifar = np.loadtxt('./softmax_scores/confidence_Our_In.txt', delimiter=',')
other = np.loadtxt('./softmax_scores/confidence_Our_Out.txt', delimiter=',')
if name == "CIFAR-10":
start = 0.1
end = 0.12
if name == "CIFAR-100":
start = 0.01
end = 0.0104
gap = (end- start)/100000
#f = open("./{}/{}/T_{}.txt".format(nnName, dataName, T), 'w')
Y1 = other[:, 2]
X1 = cifar[:, 2]
total = 0.0
fpr = 0.0
for delta in np.arange(start, end, gap):
tpr = np.sum(np.sum(X1 >= delta)) / np.float(len(X1))
error2 = np.sum(np.sum(Y1 > delta)) / np.float(len(Y1))
if tpr <= 0.9505 and tpr >= 0.9495:
fpr += error2
total += 1
fprNew = fpr/total
return fprBase, fprNew
def auroc(name):
#calculate the AUROC
# calculate baseline
T = 1
cifar = np.loadtxt('./softmax_scores/confidence_Base_In.txt', delimiter=',')
other = np.loadtxt('./softmax_scores/confidence_Base_Out.txt', delimiter=',')
if name == "CIFAR-10":
start = 0.1
end = 1
if name == "CIFAR-100":
start = 0.01
end = 1
gap = (end- start)/100000
#f = open("./{}/{}/T_{}.txt".format(nnName, dataName, T), 'w')
Y1 = other[:, 2]
X1 = cifar[:, 2]
aurocBase = 0.0
fprTemp = 1.0
for delta in np.arange(start, end, gap):
tpr = np.sum(np.sum(X1 >= delta)) / np.float(len(X1))
fpr = np.sum(np.sum(Y1 > delta)) / np.float(len(Y1))
aurocBase += (-fpr+fprTemp)*tpr
fprTemp = fpr
aurocBase += fpr * tpr
# calculate our algorithm
T = 1000
cifar = np.loadtxt('./softmax_scores/confidence_Our_In.txt', delimiter=',')
other = np.loadtxt('./softmax_scores/confidence_Our_Out.txt', delimiter=',')
if name == "CIFAR-10":
start = 0.1
end = 0.12
if name == "CIFAR-100":
start = 0.01
end = 0.0104
gap = (end- start)/100000
#f = open("./{}/{}/T_{}.txt".format(nnName, dataName, T), 'w')
Y1 = other[:, 2]
X1 = cifar[:, 2]
aurocNew = 0.0
fprTemp = 1.0
for delta in np.arange(start, end, gap):
tpr = np.sum(np.sum(X1 >= delta)) / np.float(len(X1))
fpr = np.sum(np.sum(Y1 >= delta)) / np.float(len(Y1))
aurocNew += (-fpr+fprTemp)*tpr
fprTemp = fpr
aurocNew += fpr * tpr
return aurocBase, aurocNew
def auprIn(name):
#calculate the AUPR
# calculate baseline
T = 1
cifar = np.loadtxt('./softmax_scores/confidence_Base_In.txt', delimiter=',')
other = np.loadtxt('./softmax_scores/confidence_Base_Out.txt', delimiter=',')
if name == "CIFAR-10":
start = 0.1
end = 1
if name == "CIFAR-100":
start = 0.01
end = 1
gap = (end- start)/100000
precisionVec = []
recallVec = []
#f = open("./{}/{}/T_{}.txt".format(nnName, dataName, T), 'w')
Y1 = other[:, 2]
X1 = cifar[:, 2]
auprBase = 0.0
recallTemp = 1.0
for delta in np.arange(start, end, gap):
tp = np.sum(np.sum(X1 >= delta)) / np.float(len(X1))
fp = np.sum(np.sum(Y1 >= delta)) / np.float(len(Y1))
if tp + fp == 0: continue
precision = tp / (tp + fp)
recall = tp
precisionVec.append(precision)
recallVec.append(recall)
auprBase += (recallTemp-recall)*precision
recallTemp = recall
auprBase += recall * precision
#print(recall, precision)
# calculate our algorithm
T = 1000
cifar = np.loadtxt('./softmax_scores/confidence_Our_In.txt', delimiter=',')
other = np.loadtxt('./softmax_scores/confidence_Our_Out.txt', delimiter=',')
if name == "CIFAR-10":
start = 0.1
end = 0.12
if name == "CIFAR-100":
start = 0.01
end = 0.0104
gap = (end- start)/100000
#f = open("./{}/{}/T_{}.txt".format(nnName, dataName, T), 'w')
Y1 = other[:, 2]
X1 = cifar[:, 2]
auprNew = 0.0
recallTemp = 1.0
for delta in np.arange(start, end, gap):
tp = np.sum(np.sum(X1 >= delta)) / np.float(len(X1))
fp = np.sum(np.sum(Y1 >= delta)) / np.float(len(Y1))
if tp + fp == 0: continue
precision = tp / (tp + fp)
recall = tp
#precisionVec.append(precision)
#recallVec.append(recall)
auprNew += (recallTemp-recall)*precision
recallTemp = recall
auprNew += recall * precision
return auprBase, auprNew
def auprOut(name):
#calculate the AUPR
# calculate baseline
T = 1
cifar = np.loadtxt('./softmax_scores/confidence_Base_In.txt', delimiter=',')
other = np.loadtxt('./softmax_scores/confidence_Base_Out.txt', delimiter=',')
if name == "CIFAR-10":
start = 0.1
end = 1
if name == "CIFAR-100":
start = 0.01
end = 1
gap = (end- start)/100000
Y1 = other[:, 2]
X1 = cifar[:, 2]
auprBase = 0.0
recallTemp = 1.0
for delta in np.arange(end, start, -gap):
fp = np.sum(np.sum(X1 < delta)) / np.float(len(X1))
tp = np.sum(np.sum(Y1 < delta)) / np.float(len(Y1))
if tp + fp == 0: break
precision = tp / (tp + fp)
recall = tp
auprBase += (recallTemp-recall)*precision
recallTemp = recall
auprBase += recall * precision
# calculate our algorithm
T = 1000
cifar = np.loadtxt('./softmax_scores/confidence_Our_In.txt', delimiter=',')
other = np.loadtxt('./softmax_scores/confidence_Our_Out.txt', delimiter=',')
if name == "CIFAR-10":
start = 0.1
end = 0.12
if name == "CIFAR-100":
start = 0.01
end = 0.0104
gap = (end- start)/100000
#f = open("./{}/{}/T_{}.txt".format(nnName, dataName, T), 'w')
Y1 = other[:, 2]
X1 = cifar[:, 2]
auprNew = 0.0
recallTemp = 1.0
for delta in np.arange(end, start, -gap):
fp = np.sum(np.sum(X1 < delta)) / np.float(len(X1))
tp = np.sum(np.sum(Y1 < delta)) / np.float(len(Y1))
if tp + fp == 0: break
precision = tp / (tp + fp)
recall = tp
auprNew += (recallTemp-recall)*precision
recallTemp = recall
auprNew += recall * precision
return auprBase, auprNew
def detection(name):
#calculate the minimum detection error
# calculate baseline
T = 1
cifar = np.loadtxt('./softmax_scores/confidence_Base_In.txt', delimiter=',')
other = np.loadtxt('./softmax_scores/confidence_Base_Out.txt', delimiter=',')
if name == "CIFAR-10":
start = 0.1
end = 1
if name == "CIFAR-100":
start = 0.01
end = 1
gap = (end- start)/100000
#f = open("./{}/{}/T_{}.txt".format(nnName, dataName, T), 'w')
Y1 = other[:, 2]
X1 = cifar[:, 2]
errorBase = 1.0
for delta in np.arange(start, end, gap):
tpr = np.sum(np.sum(X1 < delta)) / np.float(len(X1))
error2 = np.sum(np.sum(Y1 > delta)) / np.float(len(Y1))
errorBase = np.minimum(errorBase, (tpr+error2)/2.0)
# calculate our algorithm
T = 1000
cifar = np.loadtxt('./softmax_scores/confidence_Our_In.txt', delimiter=',')
other = np.loadtxt('./softmax_scores/confidence_Our_Out.txt', delimiter=',')
if name == "CIFAR-10":
start = 0.1
end = 0.12
if name == "CIFAR-100":
start = 0.01
end = 0.0104
gap = (end- start)/100000
#f = open("./{}/{}/T_{}.txt".format(nnName, dataName, T), 'w')
Y1 = other[:, 2]
X1 = cifar[:, 2]
errorNew = 1.0
for delta in np.arange(start, end, gap):
tpr = np.sum(np.sum(X1 < delta)) / np.float(len(X1))
error2 = np.sum(np.sum(Y1 > delta)) / np.float(len(Y1))
errorNew = np.minimum(errorNew, (tpr+error2)/2.0)
return errorBase, errorNew
def metric(nn, data):
if nn == "densenet10" or nn == "wideresnet10": indis = "CIFAR-10"
if nn == "densenet100" or nn == "wideresnet100": indis = "CIFAR-100"
if nn == "densenet10" or nn == "densenet100": nnStructure = "DenseNet-BC-100"
if nn == "wideresnet10" or nn == "wideresnet100": nnStructure = "Wide-ResNet-28-10"
if data == "Imagenet": dataName = "Tiny-ImageNet (crop)"
if data == "Imagenet_resize": dataName = "Tiny-ImageNet (resize)"
if data == "LSUN": dataName = "LSUN (crop)"
if data == "LSUN_resize": dataName = "LSUN (resize)"
if data == "iSUN": dataName = "iSUN"
if data == "Gaussian": dataName = "Gaussian noise"
if data == "Uniform": dataName = "Uniform Noise"
fprBase, fprNew = tpr95(indis)
errorBase, errorNew = detection(indis)
aurocBase, aurocNew = auroc(indis)
auprinBase, auprinNew = auprIn(indis)
auproutBase, auproutNew = auprOut(indis)
print("{:31}{:>22}".format("Neural network architecture:", nnStructure))
print("{:31}{:>22}".format("In-distribution dataset:", indis))
print("{:31}{:>22}".format("Out-of-distribution dataset:", dataName))
print("")
print("{:>34}{:>19}".format("Baseline", "Our Method"))
print("{:20}{:13.1f}%{:>18.1f}% ".format("FPR at TPR 95%:",fprBase*100, fprNew*100))
print("{:20}{:13.1f}%{:>18.1f}%".format("Detection error:",errorBase*100, errorNew*100))
print("{:20}{:13.1f}%{:>18.1f}%".format("AUROC:",aurocBase*100, aurocNew*100))
print("{:20}{:13.1f}%{:>18.1f}%".format("AUPR In:",auprinBase*100, auprinNew*100))
print("{:20}{:13.1f}%{:>18.1f}%".format("AUPR Out:",auproutBase*100, auproutNew*100))