Strengthen Out-of-Distribution Detection via Adaptive Mahalanobis Gap

Kunpeng Sui, Rundong He, Jie SuNeurIPS 2026

TL;DR

Neural networks are notorious for making high-confidence guesses on completely unfamiliar data, making Out-of-Distribution (OOD) detection critical for safe deployment. This paper introduces Adaptive Mahalanobis Gap (AMG), a training-free, test-time method that reshapes how we measure distances in feature space. By dynamically adapting feature geometry per test sample, accounting for how "tight" or "loose" different classes are, and comparing distances across all classes rather than just the closest one, AMG achieves faster and more accurate OOD detection than existing state-of-the-art methods.

What Problem Does This Paper Address?

Imagine a model trained to classify dog breeds suddenly being shown a picture of a toaster. Instead of saying "I don't know," standard networks often confidently declare it a Poodle. Test-time OOD detection tries to catch these unfamiliar inputs during inference without retraining the frozen network.

A classic way to do this is using the Mahalanobis distance, which measures how far a test sample's feature vector sits from known class centers (prototypes), accounting for the shape (covariance) of the training data. However, current Mahalanobis approaches suffer from three blind spots:

  1. Rigid or one-sided geometry: Static methods assume test data won't distort the feature space. Newer dynamic methods (like DCC) adjust the noisy "residual" subspace at test time so OOD samples aren't mistaken for in-distribution (ID) data, but they neglect the core discriminative ("principal") subspace—causing valid ID samples to be falsely rejected.
  2. Class-agnostic scaling: They treat all classes identically, ignoring that a slight deviation from a tightly clustered class is much more suspicious than the same deviation from a sprawling, high-variance class.
  3. Tunnel vision on the nearest neighbor: Uncertainty is usually scored solely by the distance to the single closest class center. If an OOD sample happens to land near one ID prototype, the detector is easily fooled.
Figure 1: Visualization of the differences between the baseline and the proposed AMG framework. The top row (a-c) illustrates the limitations of the baseline in residual-space adjustment, unified scaling, and MinDist-based scoring. The bottom row (d-f) demonstrates the improvements introduced by AMG, including (d) Adaptive Covariance Calibrating, (e) Class-aware Deviation Scaling, and (f) Mahalanobis Gap Scoring.

Methods

AMG upgrades traditional Mahalanobis distance at inference time through three modular steps:

  • Adaptive Covariance Calibrating (ACC): The authors split the training covariance matrix into a principal subspace (top eigenvectors capturing core ID features) and a residual subspace (remaining noisy directions). When a test sample arrives, ACC contracts the residual subspace while expanding/reinforcing the principal subspace along the real-time feature's direction. This dual adjustment keeps OOD samples out while preventing valid ID samples from being misclassified.
  • Class-aware Deviation Scaling (CDS): Before computing distances, AMG scales the deviation vector between a sample and a class center using two training statistics: intra-class variance and prototype norm (feature strength). Tighter classes and stronger representations get amplified responses to catch subtle anomalies, while looser, weaker classes are dampened to reduce noise sensitivity.
  • Mahalanobis Gap Scoring (MGS): Instead of just taking the minimum distance to the nearest class ($z'_1$), AMG calculates the distances to all class prototypes, sorts them, and computes the average gap between the nearest class and every other class. The final OOD score combines this relative "gap" with the absolute nearest-prototype distance.

Datasets

No new datasets are introduced; the method is evaluated on standard, non-overlapping ID/OOD pairings:

  • Small/Medium-scale ID: CIFAR-10 and CIFAR-100, tested against six OOD benchmarks: SVHN, Textures (DTD), Places365, LSUN, LSUN-Resize, and iSUN. Also tested on corrupted covariate-shift variants (CIFAR-10C, CIFAR-100C) and mutually as "Near-OOD" tasks.
  • Large-scale ID: ImageNet-1k (1.2M images, 1,000 classes), tested against Textures, SUN, Places, iNaturalist, ImageNet-O, and OpenImage-O.

Key Insights and Ideas

  • The "Flat Profile" of Hard OOD Samples: Why look beyond the nearest neighbor? A genuine ID sample usually sits cozy with its true class and far from others—creating a steep jump in sorted distances. Hard OOD samples, even when awkwardly close to one class center, tend to be roughly equidistant to several others, yielding a much flatter distance profile. Measuring the cumulative gap exposes them.
Figure 2: Sorted Mahalanobis distance profiles for ID and OOD samples. The inset shows ID-OOD differences: 1 for nearest-prototype scoring, and 2/3 for cumulative gaps up to the second/third prototypes.
  • MGS is a Plug-and-Play Booster: You don't even need the full AMG pipeline to benefit from gap scoring. Dropping MGS directly into existing distance methods (like Mahalanobis++ and DCC) immediately lowers their false positive rates.
  • Resilience to Data Scarcity: Because AMG calibrates covariance dynamically at test time and uses global inter-class structure, it doesn't fall apart when reference data is scarce—maintaining strong performance even when given only 10% of the ID training data to build its statistics.

Key Results

AMG is evaluated primarily on FPR@95 (False Positive Rate of OOD samples when True Positive Rate of ID is 95%—lower is better) and AUROC (higher is better):

  • CIFAR Benchmarks (DenseNet): On CIFAR-10, AMG hits 11.19% FPR@95 and 97.77% AUROC (beating dynamic baseline DCC by 3.44 points in FPR@95). On CIFAR-100, it reaches 27.43% FPR@95 and 93.20% AUROC.
  • ImageNet-1k (ResNet-50): Reduces mean FPR@95 from DCC's 51.77% down to 48.33% (88.42% AUROC), with massive drops on hard datasets like OpenImage-O (-15.35 points in FPR@95).
  • Cross-Architecture & Speed: Gains hold consistently across CNNs (ResNet-50) and Vision Transformers (Swin-B, DeiT). Furthermore, an optimized implementation runs 17.3% faster than DCC on CIFAR-100 (8m 30s vs. 10m 17s).

Limitations and Future Work

Not discussed in the paper. (While the authors analyze hyperparameter sensitivity—showing that four weighting parameters like $\alpha$, $\alpha_{\text{var}}$, $\alpha_{\text{norm}}$, and $\lambda_{\text{gap}}$ require proper tuning for optimal performance—they do not include a dedicated section discussing formal limitations or future research directions in the main text.)

Summary updated

Abstract

Deep neural networks have achieved significant success on in-distribution (ID) tasks, but out-of-distribution (OOD) samples during inference can lead to unreliable predictions. To address this issue, distance-based methods exploit the geometric structure of feature space for OOD detection. However, methods based on static information geometry fail to address geometry distorted by ill-distributed samples. Although prior work reduces the misclassification of OOD samples as ID by adjusting the residual space with real-time features, it fails to address the misclassification of ID samples as OOD. In addition, existing distance-based methods ignore class-specific characteristics when computing deviation feature and rely on the nearest prototype for uncertainty estimation, leading to suboptimal OOD detection performance. To address these issues, we propose Adaptive Mahalanobis Gap (AMG). Specifically, we first strengthen the principal space along real-time feature directions to reduce the misclassification of ID samples as OOD. Subsequently, we incorporate class variance and prototype norms to mitigate bias of deviation feature from class-wise distributional differences. Finally, we introduce the Mahalanobis gap scoring for uncertainty scoring to overcome bias from single-prototype reliance. Experiments on CIFAR-10/100 and ImageNet-1k show that AMG consistently outperforms state-of-the-art methods in FPR@95 and AUROC. Our code is available at https://github.com/S-Skp/AMG.
BibTeX
@inproceedings{sui2026strengthen,
  author = {Kunpeng Sui and Rundong He and Jie Su},
  title = {{Strengthen Out-of-Distribution Detection via Adaptive Mahalanobis Gap}},
  booktitle = {Advances in Neural Information Processing Systems},
  year = {2026},
  url = {https://openreview.net/forum?id=zYv58OEor2},
}
Figure