kkirchheim/pytorch-ood

👽 Out-of-Distribution Detection with PyTorch

📦 pytorch‑ood – Out‑of‑Distribution Detection for PyTorch

What it is – A Python library that bundles a large collection of OOD (out‑of‑distribution), open‑set, novelty‑detection and anomaly‑detection methods built on top of PyTorch. It supplies ready‑to‑use detectors, loss functions, datasets, model architectures (with pre‑trained weights) and utilities, and it plays nicely with pytorch‑lightning and other PyTorch‑ecosystem tools.

Why it matters – Detecting when a model sees data that differ from its training distribution is a core safety problem for vision systems, medical AI, autonomous agents, etc. Implementing each method from the literature separately is tedious; pytorch‑ood gives a single, well‑tested API that covers more than 30 published techniques ranging from classic soft‑max baselines to recent energy‑based and transformer‑based detectors.


🎯 Core features (as listed in the README)

Category What you get
Detectors Implementations of OpenMax, MC‑Dropout, MaxSoftmax, ODIN, Mahalanobis (single‑ and multi‑layer), GRAM, Energy‑Based, GradNorm, ReAct, ViM, ASH, SHE, NNGuide, GEN, fDBD, VRA, NAC‑UE, SCALE, NCI and many more (2024‑2025 papers included).
Loss functions Objectosphere, Center Loss, Outlier Exposure, etc., useful for training models that are robust to OOD inputs.
Models & weights Pre‑trained WideResNet‑40‑2, ResNet, and other architectures on CIFAR‑10/100, ImageNet, etc., with weights matching the original papers.
Datasets & transforms Helpers to download standard OOD benchmarks (e.g., OpenOOD CIFAR‑10 v1.5) and to obtain the exact preprocessing pipelines used in the literature.
Utilities OODMetrics for computing AUROC, AUPR, FPR@95TPR, etc.; caching of logits/features for fast benchmark runs; optional integration with torchmetrics, pandas, scikit‑learn, and segmentation‑models‑pytorch.
Compatibility Works with plain PyTorch, pytorch‑lightning, and segmentation libraries; provides a Binder demo notebook for quick experimentation.

🛠️ Installation

pip install pytorch-ood

Core dependencies: torch, torchvision, scipy, torchmetrics. Optional extras (install as needed):

  • scikit-learn – required for ViM and k‑NN detectors.
  • gdown – for automatic download of some large model files.
  • pandas – used in the benchmark examples.
  • segmentation-models-pytorch – for anomaly‑segmentation demos.

🚀 Quick‑start example (from the README)

from pytorch_ood.detector import EnergyBased
from pytorch_ood.utils import OODMetrics
from pytorch_ood.model import load_model, load_transform

# 1️⃣ Load a pre‑trained WideResNet‑40‑2 (CIFAR‑10, trained with Energy‑Based loss)
model = load_model("wrn-40-2/cifar10/energy/s1").cuda()
preprocess = load_transform("wrn-40-2/cifar10/energy/s1")

# 2️⃣ Build the detector
 detector = EnergyBased(model)

# 3️⃣ Evaluate on a DataLoader where OOD samples have label < 0
metrics = OODMetrics()
for x, y in data_loader:
    x = preprocess(x).cuda()
    scores = detector(x)               # higher → more outlier‑like
    metrics.update(scores, y)

print(metrics.compute())  # prints AUROC, AUPR, etc.

The library also provides a higher‑level benchmark API (see the Benchmarks (Beta) section) that can evaluate many detectors on standard OOD suites with automatic caching of intermediate features.


📚 Documentation & support


📖 Citing

If you use pytorch‑ood in research, cite the CVPR‑2022 workshop paper:

@inproceedings{kirchheim2022pytorch,
  title={Pytorch‑ood: A library for out‑of‑distribution detection based on pytorch},
  author={Kirchheim, Konstantin and Filax, Marco and Ortmeier, Frank},
  booktitle={Proceedings of the IEEE/CVF Conference on Computer Vision and Pattern Recognition},
  pages={4351--4360},
  year={2022}
}

✅ Bottom line

pytorch‑ood is a mature, actively maintained toolbox that lets practitioners and researchers plug a wide variety of OOD detection methods into any PyTorch model with minimal boilerplate. It is squarely in the AI/ML domain and is a genuine software project rather than a curated list or tutorial collection.

Related

  • Project
  • Project
  • Project
  • Project
  • Project