.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/benchmarks/interface/cifar10_openood.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_cifar10_openood.py: OpenOOD - CIFAR10 ================== Reproduces the OpenOOD benchmark for OOD detection, using the WideResNet model from the Hendrycks baseline paper. .. warning :: This is currently incomplete, see :class:`CIFAR10-OpenOOD `. .. GENERATED FROM PYTHON SOURCE LINES 12-25 .. code-block:: Python :lineno-start: 12 import pandas as pd # additional dependency, used here for convenience import torch from pytorch_ood.benchmark import CIFAR10_OpenOOD from pytorch_ood.detector import MaxSoftmax, ReAct, ASH 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 26-30 .. code-block:: Python :lineno-start: 26 model = WideResNet(num_classes=10, pretrained="cifar10-pt").eval().to(device) trans = WideResNet.transform_for("cifar10-pt") norm_std = WideResNet.norm_std_for("cifar10-pt") .. GENERATED FROM PYTHON SOURCE LINES 31-32 Just add more detectors here if you want to test more .. GENERATED FROM PYTHON SOURCE LINES 32-36 .. code-block:: Python :lineno-start: 32 detectors = { "MSP": MaxSoftmax(model), } .. GENERATED FROM PYTHON SOURCE LINES 37-51 .. code-block:: Python :lineno-start: 37 results = [] benchmark = CIFAR10_OpenOOD(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 52-70 This should produce the following table: +--------------+----------+-------+---------+----------+----------+ | Dataset | Detector | AUROC | AUPR-IN | AUPR-OUT | FPR95TPR | +==============+==========+=======+=========+==========+==========+ | CIFAR100 | MSP | 87.83 | 85.20 | 88.42 | 43.08 | +--------------+----------+-------+---------+----------+----------+ | TinyImageNet | MSP | 87.06 | 85.05 | 86.82 | 51.27 | +--------------+----------+-------+---------+----------+----------+ | MNIST | MSP | 92.66 | 90.29 | 94.33 | 22.47 | +--------------+----------+-------+---------+----------+----------+ | FashionMNIST | MSP | 94.95 | 93.36 | 96.18 | 15.59 | +--------------+----------+-------+---------+----------+----------+ | Textures | MSP | 88.51 | 78.50 | 92.99 | 40.86 | +--------------+----------+-------+---------+----------+----------+ | Places365 | MSP | 88.24 | 95.61 | 71.17 | 44.65 | +--------------+----------+-------+---------+----------+----------+ .. _sphx_glr_download_auto_examples_benchmarks_interface_cifar10_openood.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: cifar10_openood.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: cifar10_openood.py ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_