Abstract
Out-of-Distribution (OOD) detection is a critical safety requirement for machine learning models deployed in real-world environments. While recent research has favored complex generative models, this paper revisits simpler, non-parametric baselines. We demonstrate that measuring the K-Nearest Neighbor (KNN) distance within a pre-trained model's embedding space provides a remarkably powerful and robust OOD detection signal. Our comprehensive benchmarks across various architectures, including ResNet-50 and Swin Transformers, show that KNN consistently outperforms standard prediction-based methods like Maximum Softmax Probability (MSP) and maintains high performance across diverse dataset pairs.
Introduction
In machine learning, "knowing what you don't know" is essential for reliability. As models transition from research labs to production, their ability to flag Out-of-Distribution (OOD) data—inputs significantly different from the training set—becomes a matter of trust. A recent study by Johnson Kuan and Jonas Mueller from Cleanlab, presented at an ICML 2022 workshop, suggests that the most effective solution might be one of the simplest: leveraging the geometry of learned representations.
The OOD Detection Problem
Deep learning models are notoriously overconfident. A classifier trained on animal images will often predict a specific breed with high confidence when shown an unrelated object, such as a toaster. This overconfidence stems from the model's inability to quantify "epistemic uncertainty"—the uncertainty arising from a lack of knowledge about the input domain.
Existing solutions generally fall into two categories:
- Prediction-based: Utilizing the model's final softmax probabilities (e.g., MSP).
- Generative-based: Training auxiliary models like GANs or Autoencoders to model data density.
While prediction-based methods are often too simplistic, generative approaches are difficult to tune and computationally expensive. This research seeks a middle ground that is model-agnostic, easy to implement, and high-performing.
Methodology: KNN Distance in Embedding Space
The authors evaluate several model-agnostic methods that rely on either a classifier's predictions ($p$) or its learned representations ($z$) from the penultimate layer. The standout method is KNN Distance.
For a new test input, the model extracts its embedding vector. The OOD score is then calculated as the average distance to its $K$ nearest neighbors in the training set. A large average distance indicates that the input lies in a region of the feature space sparsely populated by training data, signaling it is likely OOD. By applying KNN to high-level feature spaces rather than raw pixels, the method captures semantic similarity rather than superficial texture or color overlaps.
Experimental Results
The authors conducted benchmarks using pairs of datasets (CIFAR-10, CIFAR-100, MNIST, Fashion-MNIST, and Roman-numerals) where one is treated as "In-Distribution" and the other as "OOD."
Key findings include:
- Superiority of KNN: In the CIFAR-10 vs. CIFAR-100 task, KNN achieved an AUROC of 0.9917, significantly outperforming the MSP baseline (0.9689).
- Scaling with Modern Architectures: As classification models improve (e.g., moving from ResNet to Swin Transformers), the quality of their embeddings improves, which naturally boosts the performance of representation-based OOD methods.
- Hyperparameter Stability: KNN performance remained stable across different values of $K$ (from 5 to 100), reducing the need for extensive tuning.
- Inconsistency of Parametric Methods: While Relative Mahalanobis Distance (RMD) performed well in some cases, it was inconsistent. On the Roman-numeral vs. MNIST task with a Swin Transformer, RMD's AUROC dropped to 0.8753, while KNN maintained a near-perfect 0.9957.
Discussion and Insights
The paper highlights that softmax probabilities primarily estimate "aleatoric uncertainty" (noise in the data) rather than the epistemic uncertainty needed for OOD detection. In contrast, intermediate representations contain far more information about the data distribution. Even though KNN is non-parametric, the embeddings it relies on are shaped by supervised learning, creating a "label-informed geometry" that effectively separates known classes from outliers.
Limitations and Future Work
While KNN is highly effective, it faces computational challenges when scaling to massive training sets. Although approximate KNN algorithms can mitigate this, it remains a consideration for real-time systems. Future research should explore "covariate shift" (the same classes under different environmental conditions) and provide deeper theoretical insights into why Transformer embeddings specifically benefit distance-based detection.
Conclusion
By revisiting simple baselines, this work demonstrates that complex generative models may not be necessary for robust OOD detection. KNN distance in the embedding space of a strong classifier provides a simple, scalable, and high-performing solution that should be considered a primary baseline for future OOD research.
Summary updated