MahaVar: OOD Detection via Class-wise Mahalanobis Distance Variance under Neural Collapse

Donghwan Kim, Hyunsoo Yoon

TL;DR

Deep neural networks are notoriously overconfident when confronted with unfamiliar, out-of-distribution (OOD) inputs. While standard feature-distance detectors only ask how close an input is to its single nearest known class, they throw away the rest of the geometric picture. MahaVar fixes this with a dead-simple post-hoc tweak: by adding the variance of distances across all classes—a property naturally guaranteed by the geometry of well-trained networks—it catches anomalies that traditional distance checks miss, setting a new state of the art without a single step of retraining.

What Problem Does This Paper Address?

Imagine deploying a classifier trained exclusively on cats and dogs, and someone feeds it a picture of a toaster. Under a closed-world assumption, the network will confidently declare it a "dog" (or a "cat"). Out-of-distribution (OOD) detection acts as a bouncer at the door, flagging unfamiliar inputs before the model makes a catastrophic mistake.

The most practical bouncers are post-hoc methods: they inspect a frozen, pre-trained network's internal representations without requiring expensive retraining or access to example outlier data. A gold-standard post-hoc approach uses the Mahalanobis distance—essentially a covariance-aware Euclidean distance that accounts for how stretched or squished a cluster of points is. You model each training class as a Gaussian blob in the network's penultimate feature layer, and check how close a test sample falls to the nearest class center.

The catch? Looking only at the minimum distance leaves rich geometric information on the table. What if an unfamiliar sample happens to drift reasonably close to one class center, fooling the minimum-distance check? The authors ask whether the sample's relationship to all the other classes holds a missing clue.

Methods

MahaVar builds directly on Mahalanobis++, a strong baseline that applies $L_2$ normalization (scaling feature vectors to unit length) to penultimate-layer features before computing class-wise Mahalanobis distances $d_{\text{Maha},c}(\hat{\phi}(\mathbf{x}))$ using class means and a shared covariance matrix.

Instead of scoring a sample $\mathbf{x}$ solely by its negated minimum distance ($-\min_c d_{\text{Maha},c}$), MahaVar adds a scaled variance term across all $C$ classes:

$$S(\mathbf{x}) = -\min_{c \in \{1,\dots,C\}} d_{\text{Maha},c}(\hat{\phi}(\mathbf{x})) + \alpha \cdot \text{Var}_c\big[d_{\text{Maha},c}(\hat{\phi}(\mathbf{x}))\big]$$

Here, $\alpha \ge 0$ is a scalar hyperparameter tuned on a validation set. Familiar in-distribution (ID) samples have a tiny minimum distance and a high variance across classes, pushing $S(\mathbf{x})$ up; OOD samples have larger minimum distances and flatter, low-variance distance profiles, pushing $S(\mathbf{x})$ down.

Figure 1: Sorted class-wise dMaha,c(x) (left two panels) and the corresponding distribution of Varc[dMaha,c(x)] for ID and OOD samples (right two panels) of Mahalanobis++ [29] on CIFAR-10 and CIFAR-100 as ID datasets, evaluated on OOD datasets following the OpenOOD v1.5 benchmark protocol [47] with ResNet-18 as the backbone. In the sorted distance plots, the x-axis represents the Mahalanobis distance rank, where rank 0 corresponds to the nearest class mean, and shaded regions indicate ±0.5σ across samples at each rank. In the variance distribution plots, the dashed blue line indicates the mean variance of ID samples, and black bars indicate the median of each distribution.

Datasets

The authors follow the standardized OpenOOD v1.5 benchmark protocol (no new datasets are introduced), which splits OOD benchmarks into "Near-OOD" (semantically similar, harder) and "Far-OOD" (clearly distinct):

  • CIFAR-10 (ID): Tested against Near-OOD (CIFAR-100, TIN) and Far-OOD (MNIST, SVHN, Textures, Places365).
  • CIFAR-100 (ID): Tested against Near-OOD (CIFAR-10, TIN) and the same Far-OOD suite.
  • ImageNet-1K (ID): Tested across ResNet-50, Swin-B, and ViT-B backbones against Near-OOD (SSB-Hard, NINCO) and Far-OOD (iNaturalist, Textures, OpenImage-O).

Key Insights and Ideas

  • The "Sharp Minimum" Signature: When you sort a sample's distances to all classes from smallest to largest, ID samples plummet at rank 0 (their true class) and rocket up to a flat plateau for all other classes. OOD samples lack a home base—their sorted distances rise sluggishly without a sharp drop, yielding much lower variance across classes.
  • Neural Collapse Explains Why: Why does that plateau exist? During training, deep networks naturally undergo Neural Collapse: features of the same class squeeze tightly around their class mean, while the class means push apart into a maximally symmetric geometric structure called a simplex Equiangular Tight Frame (ETF) (think of the vertices of a regular tetrahedron, generalized to high dimensions). Because an ID sample sits on one vertex, it is equidistant and far from all other vertices—mathematically guaranteeing high distance variance.
  • Subspace Alignment Catches Near-OOD Sneaks: Even if a tricky Near-OOD sample lands at the exact same distance to the nearest class as a genuine ID sample, theoretical analysis shows its variance will still be lower as long as its feature vector sticks out of the subspace spanned by the class means.
  • Complementary, Not Standalone: Variance alone is a mediocre OOD detector (59.91% AUROC on ImageNet). Its magic lies in complementing the nearest-class distance.

Key Results

  • ImageNet SOTA: Averaged across ResNet-50, Swin-B, and ViT-B, MahaVar achieves 88.64% AUROC and 44.74% FPR@95 (false positive rate at 95% true positive rate—lower is better), beating Mahalanobis++ (87.75% / 47.42%) and 15 other baselines.
  • Backbone Stability: Popular activation-shaping methods like ASH-S and SCALE shine on ResNet-50, but collapse catastrophically on Vision Transformers (e.g., ASH-S drops to ~23% AUROC on Swin-B and ViT-B). MahaVar stays rock-solid, taking first place overall on both Swin-B and ViT-B.
  • CIFAR Benchmarks: MahaVar achieves state-of-the-art performance across all methods on CIFAR-100 (83.80% AUROC, 68.03% FPR@95), and beats every Mahalanobis-family baseline on CIFAR-10 (91.12% AUROC, 45.56% FPR@95).

Limitations and Future Work

  • Feature Dimension Bottleneck ($d < C - 1$): Forming a complete simplex ETF for $C$ classes mathematically requires a penultimate feature dimension $d \ge C - 1$. On ImageNet ($C = 1000$), ViT-B only has $d = 768$ dimensions. This geometric squeeze weakens ETF formation and shrinks MahaVar's AUROC gain to +0.30% on ViT-B (compared to +1.19% on ResNet-50, where $d = 2048$).
  • Hyperparameter Tuning: MahaVar requires a validation set to tune $\alpha$ (pushing $\alpha$ too high lets variance drown out the minimum distance). An initial hyperparameter-free ratio variant looks promising empirically on ResNet-50 (88.40% AUROC), but lacks theoretical guarantees.
  • Theory-Practice Gap & Future Directions: The theoretical proofs assume centered features, whereas the practical algorithm omits centering. Bridging this mathematical gap, proving guarantees for ratio-based scoring, and exploring higher-order moments (like skewness) remain open avenues for future work.

Summary updated

Abstract

Out-of-distribution (OOD) detection is a critical component for ensuring the reliability of deep neural networks in safety-critical applications. In this work, we present a key empirical observation: for in-distribution (ID) samples, class-wise Mahalanobis distances exhibit a pronounced sharp minimum structure, where the distance to the nearest class is small while distances to all other classes remain large, resulting in high variance across classes. In contrast, OOD samples tend to exhibit a less pronounced sharp minimum structure, producing comparatively lower variance across classes. We further provide a theoretical analysis grounding this observation in Neural Collapse geometry: under relaxed Neural Collapse assumptions on within-class compactness and inter-class separation, ID samples are shown to structurally exhibit high class-wise distance variance, offering a theoretical basis for its use as an OOD score. Motivated by this observation and its theoretical backing, we propose MahaVar, a simple and effective post-hoc OOD detector that augments the Mahalanobis distance with a class-wise distance variance term. Following the OpenOOD v1.5 benchmark protocol, MahaVar achieves state-of-the-art performance on CIFAR-100 and ImageNet, with consistent improvements in both AUROC and FPR@95 over existing Mahalanobis-based methods across all benchmarks.
BibTeX
@inproceedings{kim2026mahavar,
  author = {Donghwan Kim and Hyunsoo Yoon},
  title = {{MahaVar: OOD Detection via Class-wise Mahalanobis Distance Variance under Neural Collapse}},
  booktitle = {Advances in Neural Information Processing Systems},
  year = {2026},
  url = {https://openreview.net/forum?id=1FalHL84no},
}
Figure