-
Notifications
You must be signed in to change notification settings - Fork 12
/
test_baselines.py
261 lines (204 loc) · 9.56 KB
/
test_baselines.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
from utils import log
import resnetv2
import torch
import time
import numpy as np
from utils.test_utils import arg_parser, mk_id_ood, get_measures
import os
from sklearn.linear_model import LogisticRegressionCV
from torch.autograd import Variable
from utils.mahalanobis_lib import get_Mahalanobis_score
def iterate_data_msp(data_loader, model):
confs = []
m = torch.nn.Softmax(dim=-1).cuda()
for b, (x, y) in enumerate(data_loader):
with torch.no_grad():
x = x.cuda()
# compute output, measure accuracy and record loss.
logits = model(x)
conf, _ = torch.max(m(logits), dim=-1)
confs.extend(conf.data.cpu().numpy())
return np.array(confs)
def iterate_data_odin(data_loader, model, epsilon, temper, logger):
criterion = torch.nn.CrossEntropyLoss().cuda()
confs = []
for b, (x, y) in enumerate(data_loader):
x = Variable(x.cuda(), requires_grad=True)
outputs = model(x)
maxIndexTemp = np.argmax(outputs.data.cpu().numpy(), axis=1)
outputs = outputs / temper
labels = Variable(torch.LongTensor(maxIndexTemp).cuda())
loss = criterion(outputs, labels)
loss.backward()
# Normalizing the gradient to binary in {0, 1}
gradient = torch.ge(x.grad.data, 0)
gradient = (gradient.float() - 0.5) * 2
# Adding small perturbations to images
tempInputs = torch.add(x.data, -epsilon, gradient)
outputs = model(Variable(tempInputs))
outputs = outputs / temper
# Calculating the confidence after adding perturbations
nnOutputs = outputs.data.cpu()
nnOutputs = nnOutputs.numpy()
nnOutputs = nnOutputs - np.max(nnOutputs, axis=1, keepdims=True)
nnOutputs = np.exp(nnOutputs) / np.sum(np.exp(nnOutputs), axis=1, keepdims=True)
confs.extend(np.max(nnOutputs, axis=1))
if b % 100 == 0:
logger.info('{} batches processed'.format(b))
# debug
# if b > 500:
# break
return np.array(confs)
def iterate_data_energy(data_loader, model, temper):
confs = []
for b, (x, y) in enumerate(data_loader):
with torch.no_grad():
x = x.cuda()
# compute output, measure accuracy and record loss.
logits = model(x)
conf = temper * torch.logsumexp(logits / temper, dim=1)
confs.extend(conf.data.cpu().numpy())
return np.array(confs)
def iterate_data_mahalanobis(data_loader, model, num_classes, sample_mean, precision,
num_output, magnitude, regressor, logger):
confs = []
for b, (x, y) in enumerate(data_loader):
if b % 10 == 0:
logger.info('{} batches processed'.format(b))
x = x.cuda()
Mahalanobis_scores = get_Mahalanobis_score(x, model, num_classes, sample_mean, precision, num_output, magnitude)
scores = -regressor.predict_proba(Mahalanobis_scores)[:, 1]
confs.extend(scores)
return np.array(confs)
def iterate_data_kl_div(data_loader, model):
probs, labels = [], []
m = torch.nn.Softmax(dim=-1).cuda()
for b, (x, y) in enumerate(data_loader):
with torch.no_grad():
x = x.cuda()
# compute output, measure accuracy and record loss.
logits = model(x)
prob = m(logits)
probs.extend(prob.data.cpu().numpy())
labels.extend(y.numpy())
return np.array(probs), np.array(labels)
def kl(p, q):
"""Kullback-Leibler divergence D(P || Q) for discrete distributions
Parameters
----------
p, q : array-like, dtype=float, shape=n
Discrete probability distributions.
"""
# p = np.asarray(p, dtype=np.float)
# q = np.asarray(q, dtype=np.float)
return np.sum(np.where(p != 0, p * np.log(p / q), 0))
def run_eval(model, in_loader, out_loader, logger, args, num_classes):
# switch to evaluate mode
model.eval()
logger.info("Running test...")
logger.flush()
if args.score == 'MSP':
logger.info("Processing in-distribution data...")
in_scores = iterate_data_msp(in_loader, model)
logger.info("Processing out-of-distribution data...")
out_scores = iterate_data_msp(out_loader, model)
elif args.score == 'ODIN':
logger.info("Processing in-distribution data...")
in_scores = iterate_data_odin(in_loader, model, args.epsilon_odin, args.temperature_odin, logger)
logger.info("Processing out-of-distribution data...")
out_scores = iterate_data_odin(out_loader, model, args.epsilon_odin, args.temperature_odin, logger)
elif args.score == 'Energy':
logger.info("Processing in-distribution data...")
in_scores = iterate_data_energy(in_loader, model, args.temperature_energy)
logger.info("Processing out-of-distribution data...")
out_scores = iterate_data_energy(out_loader, model, args.temperature_energy)
elif args.score == 'Mahalanobis':
sample_mean, precision, lr_weights, lr_bias, magnitude = np.load(
os.path.join(args.mahalanobis_param_path, 'results.npy'), allow_pickle=True)
sample_mean = [s.cuda() for s in sample_mean]
precision = [p.cuda() for p in precision]
regressor = LogisticRegressionCV(cv=2).fit([[0, 0, 0, 0], [0, 0, 0, 0], [1, 1, 1, 1], [1, 1, 1, 1]],
[0, 0, 1, 1])
regressor.coef_ = lr_weights
regressor.intercept_ = lr_bias
temp_x = torch.rand(2, 3, 480, 480)
temp_x = Variable(temp_x).cuda()
temp_list = model(x=temp_x, layer_index='all')[1]
num_output = len(temp_list)
logger.info("Processing in-distribution data...")
in_scores = iterate_data_mahalanobis(in_loader, model, num_classes, sample_mean, precision,
num_output, magnitude, regressor, logger)
logger.info("Processing out-of-distribution data...")
out_scores = iterate_data_mahalanobis(out_loader, model, num_classes, sample_mean, precision,
num_output, magnitude, regressor, logger)
elif args.score == 'KL_Div':
logger.info("Processing in-distribution data...")
in_dist_logits, in_labels = iterate_data_kl_div(in_loader, model)
logger.info("Processing out-of-distribution data...")
out_dist_logits, _ = iterate_data_kl_div(out_loader, model)
class_mean_logits = []
for c in range(num_classes):
selected_idx = (in_labels == c)
selected_logits = in_dist_logits[selected_idx]
class_mean_logits.append(np.mean(selected_logits, axis=0))
class_mean_logits = np.array(class_mean_logits)
logger.info("Compute distance for in-distribution data...")
in_scores = []
for i, logit in enumerate(in_dist_logits):
if i % 100 == 0:
logger.info('{} samples processed...'.format(i))
min_div = float('inf')
for c_mean in class_mean_logits:
cur_div = kl(logit, c_mean)
if cur_div < min_div:
min_div = cur_div
in_scores.append(-min_div)
in_scores = np.array(in_scores)
logger.info("Compute distance for out-of-distribution data...")
out_scores = []
for i, logit in enumerate(out_dist_logits):
if i % 100 == 0:
logger.info('{} samples processed...'.format(i))
min_div = float('inf')
for c_mean in class_mean_logits:
cur_div = kl(logit, c_mean)
if cur_div < min_div:
min_div = cur_div
out_scores.append(-min_div)
out_scores = np.array(out_scores)
else:
raise ValueError("Unknown score type {}".format(args.score))
in_examples = in_scores.reshape((-1, 1))
out_examples = out_scores.reshape((-1, 1))
auroc, aupr_in, aupr_out, fpr95 = get_measures(in_examples, out_examples)
logger.info('============Results for {}============'.format(args.score))
logger.info('AUROC: {}'.format(auroc))
logger.info('AUPR (In): {}'.format(aupr_in))
logger.info('AUPR (Out): {}'.format(aupr_out))
logger.info('FPR95: {}'.format(fpr95))
logger.flush()
def main(args):
logger = log.setup_logger(args)
torch.backends.cudnn.benchmark = True
in_set, out_set, in_loader, out_loader = mk_id_ood(args, logger)
logger.info(f"Loading model from {args.model_path}")
model = resnetv2.KNOWN_MODELS[args.model](head_size=len(in_set.classes))
state_dict = torch.load(args.model_path)
model.load_state_dict_custom(state_dict['model'])
model = torch.nn.DataParallel(model)
model = model.cuda()
start_time = time.time()
run_eval(model, in_loader, out_loader, logger, args, num_classes=len(in_set.classes))
end_time = time.time()
logger.info("Total running time: {}".format(end_time - start_time))
if __name__ == "__main__":
parser = arg_parser()
parser.add_argument('--score', choices=['MSP', 'ODIN', 'Energy', 'Mahalanobis', 'KL_Div'], default='MSP')
parser.add_argument('--temperature_odin', default=1000, type=int,
help='temperature scaling for odin')
parser.add_argument('--epsilon_odin', default=0.0, type=float,
help='perturbation magnitude for odin')
parser.add_argument('--temperature_energy', default=1, type=int,
help='temperature scaling for energy')
parser.add_argument('--mahalanobis_param_path', help='path to tuned mahalanobis parameters')
main(parser.parse_args())