.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/benchmarks/interface/cifar100_odin.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_benchmarks_interface_cifar100_odin.py: ODIN - CIFAR100 ================== Reproduces the ODIN benchmark for OOD detection, from the paper *Enhancing the reliability of out-of-distribution image detection in neural networks*. .. GENERATED FROM PYTHON SOURCE LINES 10-24 .. code-block:: Python :lineno-start: 11 import pandas as pd # additional dependency, used here for convenience import torch from pytorch_ood.benchmark import CIFAR100_ODIN from pytorch_ood.detector import ODIN, MaxSoftmax from pytorch_ood.model import WideResNet from pytorch_ood.utils import fix_random_seed fix_random_seed(123) device = "cuda:0" loader_kwargs = {"batch_size": 64} .. GENERATED FROM PYTHON SOURCE LINES 25-29 .. code-block:: Python :lineno-start: 25 model = WideResNet(num_classes=100, pretrained="cifar100-pt").eval().to(device) trans = WideResNet.transform_for("cifar100-pt") norm_std = WideResNet.norm_std_for("cifar100-pt") .. GENERATED FROM PYTHON SOURCE LINES 30-35 .. code-block:: Python :lineno-start: 30 detectors = { "MSP": MaxSoftmax(model), "ODIN": ODIN(model, eps=0.002, norm_std=norm_std), } .. GENERATED FROM PYTHON SOURCE LINES 36-50 .. code-block:: Python :lineno-start: 36 results = [] benchmark = CIFAR100_ODIN(root="data", transform=trans) with torch.no_grad(): for detector_name, detector in detectors.items(): print(f"> Evaluating {detector_name}") res = benchmark.evaluate(detector, loader_kwargs=loader_kwargs, device=device) for r in res: r.update({"Detector": detector_name}) results += res df = pd.DataFrame(results) print((df.set_index(["Dataset", "Detector"]) * 100).to_csv(float_format="%.2f")) .. GENERATED FROM PYTHON SOURCE LINES 51-80 This produces a table with the following output: +--------------------+----------+-------+-------+---------+----------+----------+ | Dataset | Detector | AUROC | AUTC | AUPR-IN | AUPR-OUT | FPR95TPR | +====================+==========+=======+=======+=========+==========+==========+ | TinyImageNetCrop | MSP | 86.32 | 31.64 | 88.23 | 84.81 | 43.36 | +--------------------+----------+-------+-------+---------+----------+----------+ | TinyImageNetResize | MSP | 74.64 | 40.18 | 77.29 | 70.91 | 65.56 | +--------------------+----------+-------+-------+---------+----------+----------+ | LSUNResize | MSP | 75.38 | 39.77 | 78.50 | 71.16 | 63.36 | +--------------------+----------+-------+-------+---------+----------+----------+ | LSUNCrop | MSP | 85.59 | 32.32 | 87.40 | 84.35 | 47.14 | +--------------------+----------+-------+-------+---------+----------+----------+ | Uniform | MSP | 77.80 | 41.27 | 97.58 | 16.76 | 40.49 | +--------------------+----------+-------+-------+---------+----------+----------+ | Gaussian | MSP | 84.97 | 35.02 | 98.43 | 23.36 | 29.45 | +--------------------+----------+-------+-------+---------+----------+----------+ | TinyImageNetCrop | ODIN | 86.89 | 44.10 | 89.02 | 84.01 | 40.46 | +--------------------+----------+-------+-------+---------+----------+----------+ | TinyImageNetResize | ODIN | 80.79 | 44.87 | 82.08 | 78.43 | 60.08 | +--------------------+----------+-------+-------+---------+----------+----------+ | LSUNResize | ODIN | 81.25 | 45.24 | 83.04 | 78.04 | 58.13 | +--------------------+----------+-------+-------+---------+----------+----------+ | LSUNCrop | ODIN | 86.91 | 41.91 | 88.79 | 85.69 | 42.79 | +--------------------+----------+-------+-------+---------+----------+----------+ | Uniform | ODIN | 95.42 | 35.01 | 99.54 | 58.03 | 14.21 | +--------------------+----------+-------+-------+---------+----------+----------+ | Gaussian | ODIN | 98.51 | 24.27 | 99.85 | 84.87 | 5.76 | +--------------------+----------+-------+-------+---------+----------+----------+ .. _sphx_glr_download_auto_examples_benchmarks_interface_cifar100_odin.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: cifar100_odin.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: cifar100_odin.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: cifar100_odin.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_