-
Notifications
You must be signed in to change notification settings - Fork 1
/
eval_ood_detection_deep_ensembles.py
414 lines (357 loc) · 14.5 KB
/
eval_ood_detection_deep_ensembles.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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
import os
from os.path import abspath, exists, expanduser, join
import numpy as np
import pandas as pd
from pytorch_lightning import Trainer
from sacred import Experiment
from tabulate import tabulate
import torch
from torch.utils.data import ConcatDataset, DataLoader
from cls_models import cls_models, load_cls_model
from config import Config
from datasets import datasets, load_data
from eval.binary import aupr, auroc, ece, fprxtpr
from logging_utils import log_config
from options import print_options
from utils import (
entropy,
extract_exp_id_from_path,
format_int_list,
get_accelerator_device,
get_range,
init_experiment,
)
ex = Experiment(
"evaluate OOD detection deep ensembles", ingredients=[datasets, cls_models]
)
init_experiment(ex, mongo_observer=False)
@ex.config
def config():
args = dict( # noqa: F841
gpu=0,
batch_size=64,
num_workers=8,
ood_datasets=None,
export=False,
root_metric_export_folder=Config.root_metric_export_folder,
save_folder=Config.root_save_folder,
exp_ids=None,
ensemble_size=5,
method_overwrite="entropy",
)
@ex.command(unobserved=True)
def options(args, dataset, cls_model):
used_options = set(["enable_progress_bar", "cls_models", "datasets"])
used_options = used_options.union(
set(
list(args.keys())
+ list(dataset["cfg"].keys())
+ list(cls_model["cfg"].keys())
)
)
print_options(used_options)
@ex.automain
def main(args, cls_model, dataset, _run, _log):
log_config(_run, _log)
########################################
# Set devices
########################################
accelerator, devices = get_accelerator_device(args["gpu"])
########################################
# Load dataset and model
########################################
indistdat, _ = load_data()
_log.info(f"{len(indistdat)} In-Samples of '{dataset['cfg']['name']}'")
indistloader = DataLoader(
indistdat,
batch_size=args["batch_size"],
shuffle=False,
num_workers=args["num_workers"],
)
ood_datasets = []
if args["ood_datasets"] is None:
raise ValueError("No OOD dataset specified")
else:
for d in args["ood_datasets"].split(","):
ood_cfg = dict(
name=d,
mode=dataset["cfg"]["mode"],
static=True,
image_channels=dataset["cfg"]["image_channels"],
)
if "image_size" in ood_cfg:
ood_cfg["image_size"] = dataset["cfg"]["image_size"]
ood_datasets.append(load_data(**ood_cfg)[0])
_log.info(f"{len(ood_datasets[-1])} OOD-Samples of '{d}'")
########################################
# Load checkpoints
########################################
if isinstance(args["exp_ids"], str):
exp_ids = get_range(args["exp_ids"])
elif isinstance(args["exp_ids"], int):
exp_ids = [args["exp_ids"]]
else:
exp_ids = args["exp_ids"]
if args["save_folder"] is not None and exp_ids is not None:
checkpoints = []
for id in exp_ids:
if not exists(
join(args["save_folder"], f"experiment_{id}", "classifier.ckpt")
):
raise ValueError(
f"There is no checkpoint for experiment with id '{id}''."
)
checkpoints.append(
join(args["save_folder"], f"experiment_{id}", "classifier.ckpt")
)
elif isinstance(cls_model["cfg"]["checkpoint"], str):
checkpoints = [cls_model["cfg"]["checkpoint"]]
elif isinstance(cls_model["cfg"]["checkpoint"], list):
checkpoints = cls_model["cfg"]["checkpoint"]
else:
raise ValueError(
(
"'checkpoint' has to be either of type 'str' or 'list' "
f"but found '{type(cls_model['cfg']['checkpoint'])}'"
)
)
if len(checkpoints) % args["ensemble_size"] != 0:
raise ValueError(
(
f"Number of checkpoints ({len(checkpoints)}) is not divisible by "
f"the ensemble size ({args['ensemble_size']})"
)
)
results = np.zeros(
(len(checkpoints) // args["ensemble_size"], len(ood_datasets) + 2, 4)
)
results_acc = np.zeros(len(checkpoints) // args["ensemble_size"])
results_ece = np.zeros(len(checkpoints) // args["ensemble_size"])
columns = ["auroc", "aupr-in", "aupr-out", "fpr95tpr"]
index = args["ood_datasets"].split(",") + ["all", "succ/fail"]
trainer = Trainer(
logger=False,
enable_progress_bar=args.get("enable_progress_bar", True),
max_epochs=-1,
accelerator=accelerator,
devices=devices,
)
for ensemble_index, sub_checkpoints in enumerate(
[
checkpoints[i : i + args["ensemble_size"]]
for i in range(0, len(checkpoints), args["ensemble_size"])
]
):
list_in_class_probs = []
list_out_class_probs = []
for sub_cp_index, sub_cp in enumerate(sub_checkpoints):
_log.info(
(
f"Predicting with ensemble '{ensemble_index}' "
f"- Sub Checkpoint '{sub_cp_index}'"
)
)
classifier = load_cls_model(
checkpoint=sub_cp, cp_overwrite={"method": args["method_overwrite"]}
)
########################################
# Predict
########################################
_log.info("Predicting in-distribution set...")
if sub_cp_index == (len(sub_checkpoints) - 1):
class_target = torch.empty((0,))
for _, y in indistloader:
class_target = torch.cat((class_target, y))
tmp_out = trainer.predict(classifier, indistloader)
class_probs, *_ = [torch.cat(o) for o in zip(*tmp_out)]
list_in_class_probs.append(class_probs)
_log.info("Predicting ood sets...")
out_class_probs = torch.empty((0,))
ooddat = ConcatDataset(ood_datasets)
oodloader = DataLoader(
ooddat,
batch_size=args["batch_size"],
shuffle=False,
num_workers=args["num_workers"],
)
tmp_out = trainer.predict(classifier, oodloader)
out_class_probs = torch.cat([o[0] for o in tmp_out])
list_out_class_probs.append(out_class_probs)
_log.info(f"Evaluating ensemble '{ensemble_index}'")
in_class_probs = torch.stack(list_in_class_probs).mean(0)
class_pred = in_class_probs.argmax(1)
wrong_predictions = (class_pred != class_target).float()
if classifier.method == "entropy":
in_dist_metric = entropy(in_class_probs)
elif classifier.method == "softmax":
in_dist_metric = in_class_probs.max(1)[0]
####################################
out_class_probs = torch.stack(list_out_class_probs).mean(0)
if classifier.method == "entropy":
ood_metric = entropy(out_class_probs)
elif classifier.method == "softmax":
ood_metric = out_class_probs.max(1)[0]
####################################
cumulative_dataset_sizes = np.cumsum([len(d) for d in ood_datasets])
ood_metrics = [ood_metric[: len(ood_datasets[0])]]
for i in range(1, len(cumulative_dataset_sizes)):
ood_metrics.append(
ood_metric[
cumulative_dataset_sizes[i - 1] : cumulative_dataset_sizes[i]
]
)
####################################
_log.info("Evaluating...")
for i, d in enumerate(args["ood_datasets"].split(",")):
pred = torch.cat((in_dist_metric, ood_metrics[i]))
pred = (pred - pred.min()) / (max(pred.max() - pred.min(), 1e-16))
if classifier.method == "entropy":
target = torch.cat(
(torch.zeros_like(in_dist_metric), torch.ones_like(ood_metrics[i]))
)
results[ensemble_index, i, 0] = auroc(pred=pred, target=target)
results[ensemble_index, i, 1] = aupr(pred=1 - pred, target=1 - target)
results[ensemble_index, i, 2] = aupr(pred=pred, target=target)
results[ensemble_index, i, 3] = fprxtpr(
pred=1 - pred, target=1 - target
)
elif classifier.method == "softmax":
target = torch.cat(
(torch.ones_like(in_dist_metric), torch.zeros_like(ood_metrics[i]))
)
results[ensemble_index, i, 0] = auroc(pred=pred, target=target)
results[ensemble_index, i, 1] = aupr(pred=pred, target=target)
results[ensemble_index, i, 2] = aupr(pred=1 - pred, target=1 - target)
results[ensemble_index, i, 3] = fprxtpr(pred=pred, target=target)
metrics_all = torch.cat([in_dist_metric] + ood_metrics)
metrics_all = (metrics_all - metrics_all.min()) / (
max(metrics_all.max() - metrics_all.min(), 1e-16)
)
if classifier.method == "entropy":
targets_all = torch.cat(
(
torch.zeros_like(in_dist_metric),
torch.ones_like(torch.cat(ood_metrics)),
)
)
results[ensemble_index, len(ood_datasets), 0] = auroc(
metrics_all, targets_all
)
results[ensemble_index, len(ood_datasets), 1] = aupr(
1 - metrics_all, 1 - targets_all
)
results[ensemble_index, len(ood_datasets), 2] = aupr(
metrics_all, targets_all
)
results[ensemble_index, len(ood_datasets), 3] = fprxtpr(
1 - metrics_all, 1 - targets_all
)
results[ensemble_index, len(ood_datasets) + 1, 0] = auroc(
in_dist_metric, wrong_predictions, autonorm=True
)
results[ensemble_index, len(ood_datasets) + 1, 1] = aupr(
1 - in_dist_metric, 1 - wrong_predictions, autonorm=True
)
results[ensemble_index, len(ood_datasets) + 1, 2] = aupr(
in_dist_metric, wrong_predictions, autonorm=True
)
results[ensemble_index, len(ood_datasets) + 1, 3] = fprxtpr(
1 - in_dist_metric, 1 - wrong_predictions, autonorm=True
)
elif classifier.method == "softmax":
targets_all = torch.cat(
(
torch.ones_like(in_dist_metric),
torch.zeros_like(torch.cat(ood_metrics)),
)
)
results[ensemble_index, len(ood_datasets), 0] = auroc(
metrics_all, targets_all
)
results[ensemble_index, len(ood_datasets), 1] = aupr(
metrics_all, targets_all
)
results[ensemble_index, len(ood_datasets), 2] = aupr(
1 - metrics_all, 1 - targets_all
)
results[ensemble_index, len(ood_datasets), 3] = fprxtpr(
metrics_all, targets_all
)
results[ensemble_index, len(ood_datasets) + 1, 0] = auroc(
in_dist_metric, 1 - wrong_predictions, autonorm=True
)
results[ensemble_index, len(ood_datasets) + 1, 1] = aupr(
in_dist_metric, 1 - wrong_predictions, autonorm=True
)
results[ensemble_index, len(ood_datasets) + 1, 2] = aupr(
1 - in_dist_metric, wrong_predictions, autonorm=True
)
results[ensemble_index, len(ood_datasets) + 1, 3] = fprxtpr(
in_dist_metric, 1 - wrong_predictions, autonorm=True
)
results_ece[ensemble_index] = ece(
class_pred, class_target, class_probs.max(1)[0]
)
results_acc[ensemble_index] = (class_pred == class_target).float().mean().item()
result = np.mean(results, axis=0)
result_std = np.std(results, axis=0)
acc_mean = np.mean(results_acc)
acc_std = np.std(results_acc)
ece_mean = np.mean(results_ece)
ece_std = np.std(results_ece)
_log.info(f"In-Distribution class accuracy: {acc_mean:.2%} +- {acc_std:.2%}")
_log.info(f"Expected Calibration Error: {ece_mean:.2%} +- {ece_std:.2%}")
_log.info(
"\n" * 2
+ tabulate(
{
"OOD Dataset": args["ood_datasets"].split(",") + ["all", "succ/fail"],
"AUROC": [
f"{result[i, 0]:.2%} +- {result_std[i, 0]:.2%}"
for i in range(result.shape[0])
],
"AUPR-In": [
f"{result[i, 1]:.2%} +- {result_std[i, 1]:.2%}"
for i in range(result.shape[0])
],
"AUPR-Out": [
f"{result[i, 2]:.2%} +- {result_std[i, 2]:.2%}"
for i in range(result.shape[0])
],
"FPR @ 95% TPR": [
f"{result[i, 3]:.2%} +- {result_std[i, 3]:.2%}"
for i in range(result.shape[0])
],
},
headers="keys",
floatfmt="3.2%",
tablefmt="github",
)
+ "\n"
)
if args["export"]:
if not exists(abspath(expanduser(args["root_metric_export_folder"]))):
os.makedirs(abspath(expanduser(args["root_metric_export_folder"])))
if exp_ids is not None:
exp_id = format_int_list(exp_ids)
else:
exp_id = extract_exp_id_from_path(cls_model["cfg"]["checkpoint"])
pd.DataFrame(result, index=index, columns=columns).to_csv(
join(
args["root_metric_export_folder"],
f"results_{args['type']}_deep_ensembles_oodd_succfail_{exp_id}.csv",
),
index_label="datasets",
)
pd.DataFrame(
np.concatenate((acc_mean.reshape(-1, 1), ece_mean.reshape(-1, 1)), axis=1),
index=["all"],
columns=["accuracy", "ece"],
).to_csv(
join(
args["root_metric_export_folder"],
f"results_{args['type']}_deep_ensembles_accuracy_ece_{exp_id}.csv",
),
index_label="datasets",
)
return result