Table of Contents
Fetching ...

Mahalanobis++: Improving OOD Detection via Feature Normalization

Maximilian Mueller, Matthias Hein

TL;DR

This work addresses the instability of Mahalanobis distance-based OOD detection across diverse pretrained models, tracing failures to violations of Gaussian feature-space assumptions caused by varying feature norms. It introduces Mahalanobis++—a post-hoc fix that applies $l_2$-normalization to pre-logit features, projecting them onto the unit sphere before estimating class means and a shared covariance. The approach improves alignment with Gaussian assumptions, reduces norm-driven biases, and yields consistent OOD detection gains across a wide range of architectures and pretraining schemes, outperforming both the vanilla Mahalanobis method and several baselines on large-scale benchmarks. The results suggest a simple, effective post-hoc normalization can substantially enhance reliability of OOD rejection in real-world deployments without retraining.

Abstract

Detecting out-of-distribution (OOD) examples is an important task for deploying reliable machine learning models in safety-critial applications. While post-hoc methods based on the Mahalanobis distance applied to pre-logit features are among the most effective for ImageNet-scale OOD detection, their performance varies significantly across models. We connect this inconsistency to strong variations in feature norms, indicating severe violations of the Gaussian assumption underlying the Mahalanobis distance estimation. We show that simple $\ell_2$-normalization of the features mitigates this problem effectively, aligning better with the premise of normally distributed data with shared covariance matrix. Extensive experiments on 44 models across diverse architectures and pretraining schemes show that $\ell_2$-normalization improves the conventional Mahalanobis distance-based approaches significantly and consistently, and outperforms other recently proposed OOD detection methods.

Mahalanobis++: Improving OOD Detection via Feature Normalization

TL;DR

This work addresses the instability of Mahalanobis distance-based OOD detection across diverse pretrained models, tracing failures to violations of Gaussian feature-space assumptions caused by varying feature norms. It introduces Mahalanobis++—a post-hoc fix that applies -normalization to pre-logit features, projecting them onto the unit sphere before estimating class means and a shared covariance. The approach improves alignment with Gaussian assumptions, reduces norm-driven biases, and yields consistent OOD detection gains across a wide range of architectures and pretraining schemes, outperforming both the vanilla Mahalanobis method and several baselines on large-scale benchmarks. The results suggest a simple, effective post-hoc normalization can substantially enhance reliability of OOD rejection in real-world deployments without retraining.

Abstract

Detecting out-of-distribution (OOD) examples is an important task for deploying reliable machine learning models in safety-critial applications. While post-hoc methods based on the Mahalanobis distance applied to pre-logit features are among the most effective for ImageNet-scale OOD detection, their performance varies significantly across models. We connect this inconsistency to strong variations in feature norms, indicating severe violations of the Gaussian assumption underlying the Mahalanobis distance estimation. We show that simple -normalization of the features mitigates this problem effectively, aligning better with the premise of normally distributed data with shared covariance matrix. Extensive experiments on 44 models across diverse architectures and pretraining schemes show that -normalization improves the conventional Mahalanobis distance-based approaches significantly and consistently, and outperforms other recently proposed OOD detection methods.

Paper Structure

This paper contains 16 sections, 1 theorem, 34 equations, 12 figures, 20 tables.

Key Result

Lemma 3.1

Let $\Phi(X) \sim \mathcal{N}(\mu,\Sigma)$. Then where $\mathrm{Var}(\left\|\Phi(X)\right\|_2^2)\space:=\space\sum\limits_{i=1}^d (3\lambda_i^2+6 \mu_i^2 \lambda_i +\mu_i^4)- (\lambda_i+\mu_i^2)^2$ and $(\lambda_i)_{i=1}^d$ are the eigenvalues of $\Sigma$.

Figures (12)

  • Figure 1: Normalizing features improves OOD detection with the Mahalanobis distance consistently. Shown is the difference in false-positive rate at true positive rate of 95% between unnormalized and normalized features for 44 ImageNet models, averaged over five OOD datasets of the OpenOOD benchmark.
  • Figure 2: Mahalanobis++: We illustrate how to improve Mahalanobis-based OOD detection. Left: For unnormalized features, assuming a shared covariance matrix for all classes leads to suboptimal OOD detection (bottom) with the Mahalanobis score. Center: Normalizing the features, i.e. projecting them onto the unit sphere mitigates this problem effectively. Right: After normalization, the fit of the shared covariance matrix is tighter for all classes, leading to improved OOD detection as in- and out-distribution are better separated. Shown are the Mahalanobis++ scores for a pretrained ConvNextV2-L on NINCO, which achieves a new state-of-the-art FPR of 18.4% (see Tab. \ref{['tab:fpr-big-NINCO']}).
  • Figure 3: The feature norms vary strongly across and within classes.Left: We simulate how the feature norms per class would be distributed if they were sampled from Gaussians with the means and covariance matrix used for the Mahalanobis distance estimation. Right: The actual feature norm distribution observed in practice. Both the average norms across classes and the norms within each class vary much stronger than expected.
  • Figure 4: QQ-plot: $\ell_2 -$normalization helps transform the features to be more aligned with a normal distribution. For a SwinV2 and DeiT3 model (where the feature norms vary strongly across and within classes) normalization shifts the distribution towards a Gaussian (black line).
  • Figure 5: The feature norm correlates with the Mahalanobis score for SwinV2-B:Left: The smaller the feature norm, the smaller the Mahalanobis OOD score $s_{Maha}$, irrespective of whether a sample is ID or not. OOD samples with small feature norms are systematically classified as ID. Right: After normalization, OOD samples with small feature norms can be detected, and OOD detection is significantly improved.
  • ...and 7 more figures

Theorems & Definitions (2)

  • Lemma 3.1
  • proof