GROOD: Gradient-Aware Out-of-Distribution Detection

September 3, 2025 · View on GitHub

This repository provides the official PyTorch implementation of:

GROOD: Gradient-Aware Out-of-Distribution Detection
TMLR'25
Mostafa ElAraby, et al.


Abstract

Out-of-Distribution (OOD) detection is critical for deploying neural networks safely. GROOD introduces a gradient-aware framework that leverages gradients with respect to a synthetic OOD prototype as a discriminative signal. Unlike conventional methods that rely solely on feature distances or logits, GROOD projects samples into gradient space, where ID samples yield coherent, low-sensitivity patterns while OOD samples form outliers in magnitude and direction.

To operationalize this, GROOD:

  • Constructs an OOD prototype from synthetic or auxiliary data.
  • Computes gradients of the NCP softmax loss w.r.t. the OOD prototype.
  • Uses nearest-neighbor scoring in gradient space to distinguish ID/OOD.

This approach achieves state-of-the-art performance across CIFAR-10, CIFAR-100, ImageNet-200, and ImageNet-1K benchmarks.


Highlights in OpenOOD v1.5

  • CIFAR-10: Best near-OOD AUROC (91.16%).
  • CIFAR-100: Strong far-OOD performance (84.44%).
  • ImageNet-200: Competitive across both near/far OOD.
  • ImageNet-1K: Outperforms prior methods with 78.9% near-OOD AUROC.

Method Overview

The GROOD methodology is illustrated in the following diagram:

GROOD Method
  • Input: An input sample.
  • Feature Extractor: A pre-trained neural network (f_pen) extracts features.
  • Feature Representation: The extracted features (h).
  • Distance to Prototypes: Distances between the feature representation and the ID class prototypes (p_dog, p_cat) and the OOD prototype (p_OOD) are calculated.
  • Gradients: Gradients of the softmax loss are computed with respect to the OOD prototype.
  • OOD Score: The final OOD score is determined using nearest neighbor distance in the gradient space.

How to Run

The code provides a bash script (run.sh) to run GROOD on several datasets. You can use the run.sh script to run any of the experiments by specifying the i variable.

Prerequisites:

  • Python 3.8
  • CUDA 11.1
  • OpenOOD v2
  • Wandb (for experiment tracking)
  • Required python packages (see the paper or environment.yml)

Datasets:

The script supports the following datasets:

  • MNIST
  • CIFAR-10
  • CIFAR-100
  • ImageNet (ResNet-50, ViT, Swin)
  • ImageNet-200 (ResNet-18)

Running the Experiments:

  1. Set up the environment:

    • Install the required dependencies.
    • Install OpenOOD v2.
  2. Download Pretrained Checkpoints: Download teh checkpoints from OpenOOD repository (https://github.com/Jingkang50/OpenOOD)

    • The script assumes that the pretrained models are located in the checkpoints_dir directory. Make sure the checkpoints are downloaded.
    • The script uses the following checkpoints:
      • mnist_lenet_acc99.60.ckpt for Mnist
      • cifar10_resnet18_32x32_base_e100_lr0.1_default for Cifar-10
      • cifar100_resnet18_32x32_base_e100_lr0.1_default for Cifar-100
      • imagenet_res50_acc76.10.pth for ImageNet ResNet-50
      • vit_b_16-c867db91.pth for ImageNet ViT
      • swin_t-704ceda3.pth for ImageNet Swin
      • imagenet200_resnet18_224x224_base_e90_lr0.1_default for ImageNet-200 ResNet-18
  3. Set Wandb API Key (Optional):

    • If you want to use Wandb, set the WANDB_API_KEY environment variable in the run.sh script.
  4. Run the script:

    bash run.sh
    

The script will run GROOD on CIFAR-10 (i=1). To run on a different dataset, change the value of the variable i in the run.sh script.

By default, this runs CIFAR-10 (i=1). Change i in the script to select dataset:

  • i=0 → MNIST
  • i=1 → CIFAR-10
  • i=2 → CIFAR-100
  • i=3 → ImageNet-200
  • i=4 → ImageNet-1K

Results

Main Results (AUROC, OpenOOD v1.5)

MethodCIFAR-10 NearCIFAR-10 FarCIFAR-100 NearCIFAR-100 FarIN-200 NearIN-200 FarIN-1K NearIN-1K Far
MSP88.090.780.377.883.390.176.085.2
ODIN82.987.979.979.380.291.774.789.5
ReAct87.190.480.780.481.992.377.493.7
KNN90.693.080.282.481.693.271.190.2
VIM88.793.575.081.778.791.372.192.7
GEN88.291.481.379.782.991.476.989.8
NCI88.891.381.081.383.593.778.695.5
GROOD (Ours)91.293.878.984.483.492.278.994.8

Citation

If you find this work useful, please cite:

@article{elaraby2023grood,
  title={GROOD: GRadient-aware Out-Of-Distribution detection in interpolated manifolds},
  author={ElAraby, Mostafa and Sahoo, Sabyasachi and Pequignot, Yann and Novello, Paul and Paull, Liam},
  journal={arXiv preprint arXiv:2312.14427},
  year={2023}
}

Acknowledgment

This codebase builds upon OpenOOD.