-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmetrics.py
169 lines (137 loc) · 5.51 KB
/
metrics.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
from __future__ import print_function
import argparse
import os
import sys
import numpy as np
def cal_metric(known, novel, ratio=0.95, method=None):
tp, fp, fpr_at_tpr95 = get_curve(known, novel, method, ratio=ratio)
results = dict()
mtypes = ['FPR', 'AUROC', 'DTERR', 'AUIN', 'AUOUT']
results = dict()
# FPR
mtype = 'FPR'
results[mtype] = fpr_at_tpr95
# AUROC
mtype = 'AUROC'
tpr = np.concatenate([[1.], tp/tp[0], [0.]])
fpr = np.concatenate([[1.], fp/fp[0], [0.]])
results[mtype] = -np.trapz(1.-fpr, tpr)
# DTERR
mtype = 'DTERR'
results[mtype] = ((tp[0] - tp + fp) / (tp[0] + fp[0])).min()
# AUIN
mtype = 'AUIN'
denom = tp+fp
denom[denom == 0.] = -1.
pin_ind = np.concatenate([[True], denom > 0., [True]])
pin = np.concatenate([[.5], tp/denom, [0.]])
results[mtype] = -np.trapz(pin[pin_ind], tpr[pin_ind])
# AUOUT
mtype = 'AUOUT'
denom = tp[0]-tp+fp[0]-fp
denom[denom == 0.] = -1.
pout_ind = np.concatenate([[True], denom > 0., [True]])
pout = np.concatenate([[0.], (fp[0]-fp)/denom, [.5]])
results[mtype] = np.trapz(pout[pout_ind], 1.-fpr[pout_ind])
return results
def get_curve(known, novel, method=None, ratio=0.95):
tp, fp = dict(), dict()
fpr_at_tpr95 = dict()
known.sort()
novel.sort()
end = np.max([np.max(known), np.max(novel)])
start = np.min([np.min(known),np.min(novel)])
all = np.concatenate((known, novel))
all.sort()
num_k = known.shape[0]
num_n = novel.shape[0]
if method == 'row':
threshold = -0.5
else:
threshold = known[round(ratio * num_k)]
tp = -np.ones([num_k+num_n+1], dtype=int)
fp = -np.ones([num_k+num_n+1], dtype=int)
tp[0], fp[0] = num_k, num_n
k, n = 0, 0
for l in range(num_k+num_n):
if k == num_k:
tp[l+1:] = tp[l]
fp[l+1:] = np.arange(fp[l]-1, -1, -1)
break
elif n == num_n:
tp[l+1:] = np.arange(tp[l]-1, -1, -1)
fp[l+1:] = fp[l]
break
else:
if novel[n] < known[k]:
n += 1
tp[l+1] = tp[l]
fp[l+1] = fp[l] - 1
else:
k += 1
tp[l+1] = tp[l] - 1
fp[l+1] = fp[l]
j = num_k+num_n-1
for l in range(num_k+num_n-1):
if all[j] == all[j-1]:
tp[j] = tp[j+1]
fp[j] = fp[j+1]
j -= 1
fpr_at_tpr95 = np.sum(novel < threshold) / float(num_n)
return tp, fp, fpr_at_tpr95
def print_results(results, in_dataset, out_dataset, name, method):
mtypes = ['FPR', 'DTERR', 'AUROC', 'AUIN', 'AUOUT']
print('in_distribution: ' + in_dataset)
print('out_distribution: '+ out_dataset)
print('Model Name: ' + name)
print('')
print(' OOD detection method: ' + method)
for mtype in mtypes:
print(' {mtype:6s}'.format(mtype=mtype), end='')
print('\n{val:6.2f}'.format(val=100.*results['FPR']), end='')
print(' {val:6.2f}'.format(val=100.*results['DTERR']), end='')
print(' {val:6.2f}'.format(val=100.*results['AUROC']), end='')
print(' {val:6.2f}'.format(val=100.*results['AUIN']), end='')
print(' {val:6.2f}\n'.format(val=100.*results['AUOUT']), end='')
print('')
# print(' {val:6.2f}'.format(val=100.*results['FPR']), end='')
# print(' {val:6.2f}'.format(val=100.*results['DTERR']), end='')
# print(' {val:6.2f}'.format(val=100.*results['AUROC']), end='')
# print(' {val:6.2f}'.format(val=100.*results['AUIN']), end='')
# print(' {val:6.2f}'.format(val=100.*results['AUOUT']), end='')
def print_all_results_tab(results, datasets, method):
[print('{:6.2f}\t{:6.2f}\t{:6.2f}\t'.format(100.*result['FPR'], 100.*result['AUROC'], 100.*result['AUIN']), end='') for result in results]
def print_all_results(results, datasets, method):
mtypes = ['FPR', 'AUROC', 'AUIN']#['FPR', 'DTERR', 'AUROC', 'AUIN', 'AUOUT']
avg_results = compute_average_results(results)
print(' OOD detection method: ' + method)
print(' ', end='')
for mtype in mtypes:
print(' {mtype:6s}'.format(mtype=mtype), end='')
for result, dataset in zip(results,datasets):
print('\n{dataset:12s}'.format(dataset=dataset), end='')
print(' {val:6.2f}'.format(val=100.*result['FPR']), end='')
# print(' {val:6.2f}'.format(val=100.*result['DTERR']), end='')
print(' {val:6.2f}'.format(val=100.*result['AUROC']), end='')
print(' {val:6.2f}'.format(val=100.*result['AUIN']), end='')
# print(' {val:6.2f}'.format(val=100.*result['AUOUT']), end='')
print('\nAVG ', end='')
print(' {val:6.2f}'.format(val=100.*avg_results['FPR']), end='')
# print(' {val:6.2f}'.format(val=100.*avg_results['DTERR']), end='')
print(' {val:6.2f}'.format(val=100.*avg_results['AUROC']), end='')
print(' {val:6.2f}'.format(val=100.*avg_results['AUIN']), end='')
print()
# [print('{:6.2f}\t{:6.2f}\t{:6.2f}\t'.format(100.*result['FPR'], 100.*result['AUROC'], 100.*result['AUIN']), end='') for result in results]
# print()
# print(' {val:6.2f}\n'.format(val=100.*avg_results['AUOUT']), end='')
def compute_average_results(all_results):
mtypes = ['FPR', 'DTERR', 'AUROC', 'AUIN', 'AUOUT']
avg_results = dict()
for mtype in mtypes:
avg_results[mtype] = 0.0
for results in all_results:
for mtype in mtypes:
avg_results[mtype] += results[mtype]
for mtype in mtypes:
avg_results[mtype] /= float(len(all_results))
return avg_results