Note
Go to the end to download the full example code.
ODIN - CIFAR100
Reproduces the ODIN benchmark for OOD detection, from the paper Enhancing the reliability of out-of-distribution image detection in neural networks.
10 import pandas as pd # additional dependency, used here for convenience
11 import torch
12
13 from pytorch_ood.benchmark import CIFAR100_ODIN
14 from pytorch_ood.detector import ODIN, MaxSoftmax
15 from pytorch_ood.model import WideResNet
16 from pytorch_ood.utils import fix_random_seed
17
18 fix_random_seed(123)
19
20 device = "cuda:0"
21 loader_kwargs = {"batch_size": 64}
24 model = WideResNet(num_classes=100, pretrained="cifar100-pt").eval().to(device)
25 trans = WideResNet.transform_for("cifar100-pt")
26 norm_std = WideResNet.norm_std_for("cifar100-pt")
29 detectors = {
30 "MSP": MaxSoftmax(model),
31 "ODIN": ODIN(model, eps=0.002, norm_std=norm_std),
32 }
35 results = []
36 benchmark = CIFAR100_ODIN(root="data", transform=trans)
37
38 with torch.no_grad():
39 for detector_name, detector in detectors.items():
40 print(f"> Evaluating {detector_name}")
41 res = benchmark.evaluate(detector, loader_kwargs=loader_kwargs, device=device)
42 for r in res:
43 r.update({"Detector": detector_name})
44 results += res
45
46 df = pd.DataFrame(results)
47 print((df.set_index(["Dataset", "Detector"]) * 100).to_csv(float_format="%.2f"))
This produces a table with the following output:
Dataset |
Detector |
AUROC |
AUPR-IN |
AUPR-OUT |
FPR95TPR |
|---|---|---|---|---|---|
TinyImageNetCrop |
MSP |
86.32 |
84.81 |
88.23 |
43.35 |
TinyImageNetResize |
MSP |
74.64 |
70.91 |
77.29 |
65.52 |
LSUNResize |
MSP |
75.38 |
71.16 |
78.50 |
63.36 |
LSUNCrop |
MSP |
85.59 |
84.36 |
87.40 |
47.13 |
Uniform |
MSP |
77.92 |
16.86 |
97.60 |
40.44 |
Gaussian |
MSP |
84.78 |
23.12 |
98.41 |
30.22 |
TinyImageNetCrop |
ODIN |
86.89 |
84.01 |
89.02 |
40.54 |
TinyImageNetResize |
ODIN |
80.79 |
78.44 |
82.08 |
60.10 |
LSUNResize |
ODIN |
81.25 |
78.04 |
83.04 |
58.13 |
LSUNCrop |
ODIN |
86.91 |
85.69 |
88.79 |
42.73 |
Uniform |
ODIN |
95.52 |
59.22 |
99.55 |
15.13 |
Gaussian |
ODIN |
98.57 |
85.14 |
99.86 |
5.76 |