-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmetric.py
101 lines (95 loc) · 3.45 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
import numpy as np
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-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-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-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-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
if __name__ == '__main__':
indis = "CIFAR-100"
nnStructure = "DenseNet-BC-100"
fprBase, fprNew = tpr95(indis)
aurocBase, aurocNew = auroc(indis)
print("{:31}{:>22}".format("Neural network architecture:", nnStructure))
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("AUROC:",aurocBase*100, aurocNew*100))