.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_tutorials/Classification/tutorial_distribution_shift.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_tutorials_Classification_tutorial_distribution_shift.py: Evaluating Model Performance Under Distribution Shift with TorchUncertainty =========================================================================== In this tutorial, we explore how to assess a model's robustness when faced with distribution shifts. Specifically, we will: - Shortly train a **ResNet18** model on the standard **CIFAR-10** dataset. - Evaluate its performance on both the original CIFAR-10 test set and a corrupted version of CIFAR-10 to simulate distribution shift. - Analyze the model's performance and robustness under these conditions. By the end of this tutorial, you will understand how to use TorchUncertainty to evaluate and interpret model behavior under distribution shifts. .. GENERATED FROM PYTHON SOURCE LINES 17-22 Imports and Setup ----------------- First, we need to import the necessary libraries and set up our environment. This includes importing PyTorch, TorchUncertainty components, and TorchUncertainty's Trainer (built on top of Lightning's). .. GENERATED FROM PYTHON SOURCE LINES 22-30 .. code-block:: Python from torch import nn, optim from torch_uncertainty import TUTrainer from torch_uncertainty.datamodules import CIFAR10DataModule from torch_uncertainty.models.classification.resnet import resnet from torch_uncertainty.routines.classification import ClassificationRoutine .. GENERATED FROM PYTHON SOURCE LINES 31-39 DataModule Setup ---------------- TorchUncertainty provides convenient DataModules for standard datasets like CIFAR-10. DataModules handle data loading, preprocessing, and batching, simplifying the data pipeline. Each datamodule also include the corresponding out-of-distribution and distribution shift datasets, which are then used by the routine. For CIFAR-10, the corresponding distribution-shift dataset is CIFAR-10C as used in the community. To enable Distribution Shift evaluation, activate the `eval_shift` flag as done below. .. GENERATED FROM PYTHON SOURCE LINES 39-49 .. code-block:: Python # Initialize the CIFAR-10 DataModule datamodule = CIFAR10DataModule( root="./data", batch_size=512, num_workers=8, eval_shift=True, shift_severity=5, # Set severity level of the corruption (1 to 5): max-strength! ) .. GENERATED FROM PYTHON SOURCE LINES 50-59 CIFAR-10C --------- CIFAR-10C is a transformed version of CIFAR-10 test set. Dan Hendrycks and Thomas Dietterich applied computer vision transforms, known as corruptions to degrade the quality of the image and test deep learning models in adverse conditions. There are 15 (+4 optional) corruptions in total, including noise, blur, weather effects, etc. Each corruption has 5 different levels of severity ranging from small corruptions to very strong effects on the image. You can set the desired corruption level with the shift-severity argument. We refer to [1] for more details. You can get a more detailed overview and examples of the corruptions on the corresponding tutorial. .. GENERATED FROM PYTHON SOURCE LINES 59-68 .. code-block:: Python # These lines are usually not necessary (they are called by the Trainer), # but we want to get access to the dataset before training datamodule.prepare_data() datamodule.setup("test") # Let's check the CIFAR-10C, it should contain (15+4)*10000 images for the selected severity level. print(datamodule.shift) .. rst-class:: sphx-glr-script-out .. code-block:: none 0%| | 0.00/170M [00:00` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: tutorial_distribution_shift.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: tutorial_distribution_shift.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_