.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/detectors/gradnormkl.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_detectors_gradnormkl.py: GradNormKL ============================== Running :class:`GradNormKL ` on CIFAR 10. This method computes the :math:`\ell_1`-norm of the gradient of the KL divergence between the softmax output and a uniform distribution, with respect to the weights of the final classification head. In-distribution inputs tend to produce more peaked predictions (larger divergence from uniform), yielding higher gradient norms. The score is negated so that higher values indicate OOD. Unlike :class:`GradNorm `, no labeled OOD data or additional classifier is required. .. GENERATED FROM PYTHON SOURCE LINES 16-33 .. code-block:: Python :lineno-start: 17 import logging from torch.utils.data import DataLoader from torchvision.datasets import CIFAR10 from pytorch_ood.dataset.img import Textures from pytorch_ood.detector import GradNormKL from pytorch_ood.model import WideResNet from pytorch_ood.utils import OODMetrics, ToUnknown, fix_random_seed logging.basicConfig(level=logging.INFO) fix_random_seed(123) device = "cuda" .. GENERATED FROM PYTHON SOURCE LINES 34-35 Setup preprocessing and data .. GENERATED FROM PYTHON SOURCE LINES 35-44 .. code-block:: Python :lineno-start: 35 trans = WideResNet.transform_for("cifar10-pt") dataset_in_test = CIFAR10(root="data", train=False, download=True, transform=trans) dataset_out_test = Textures( root="data", download=True, transform=trans, target_transform=ToUnknown() ) test_loader = DataLoader(dataset_in_test + dataset_out_test, batch_size=64, num_workers=4) .. GENERATED FROM PYTHON SOURCE LINES 45-47 Stage 1: Load pre-trained WideResNet for CIFAR-10. Disable gradients for the backbone; only the final FC layer needs them. .. GENERATED FROM PYTHON SOURCE LINES 47-52 .. code-block:: Python :lineno-start: 47 model = WideResNet(num_classes=10, pretrained="cifar10-pt").to(device).eval() model.requires_grad_(False) model.fc.requires_grad_(True) .. GENERATED FROM PYTHON SOURCE LINES 53-54 Stage 2: Create detector — no fitting required. .. GENERATED FROM PYTHON SOURCE LINES 54-56 .. code-block:: Python :lineno-start: 54 detector = GradNormKL(model, param_filter=lambda name: name.startswith("fc")) .. GENERATED FROM PYTHON SOURCE LINES 57-58 Stage 3: Evaluate .. GENERATED FROM PYTHON SOURCE LINES 58-65 .. code-block:: Python :lineno-start: 58 print("Testing...") metrics = OODMetrics() for x, y in test_loader: metrics.update(detector(x.to(device)), y) print(metrics.compute()) .. _sphx_glr_download_auto_examples_detectors_gradnormkl.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: gradnormkl.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: gradnormkl.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: gradnormkl.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_