Nearest Neighbor Guidance for Out-of-Distribution Detection

Jaewoo Park, Yoon Gyo Jung, Andrew Beng Jin TeohICCV 2023 · DOI

Introduction

Out-of-distribution (OOD) detection is a critical safety mechanism for machine learning models deployed in the real world. A robust system must identify when an input does not belong to any of its training classes, preventing confident but incorrect predictions. Current research typically splits into two methodologies: classifier-based scores (like Softmax or Energy) and distance-based scores (like K-Nearest Neighbors). While the former excels at "near-OOD" detection, it suffers from overconfidence in regions far from the training data. Conversely, distance-based methods are robust against "far-OOD" samples but lack the fine-grained discrimination required for subtle anomalies.

The Problem: Overconfidence vs. Coarseness

The fundamental challenge lies in the geometry of the feature space. Classifiers often create "high-confidence islands" in empty regions of the feature manifold, leading to spuriously high scores for far-OOD samples. Distance-based methods like KNN avoid this by measuring proximity to training data, but they often fail to capture the nuanced decision boundaries that a trained classifier provides.

Visual comparison of OOD detection scores on a 2D toy dataset. NNGuide (d) eliminates overconfidence in regions far from training data (b) while maintaining the fine-grained discrimination of the classifier, unlike standard KNN (c).

NNGuide: A Best-of-Both-Worlds Approach

To bridge this gap, the authors propose Nearest Neighbor Guidance (NNGuide), a training-free, post-hoc method that "guides" a model's confidence score using a confidence-scaled nearest neighbor search. The core logic is defined by the multiplication of a base classifier score and a guidance term:

$$S_{NNGuide}(x) = S_{base}(x) \cdot G(x)$$

Unlike standard KNN, the guidance term $G(x)$ uses a confidence-scaled similarity. It identifies the $k$ nearest neighbors in the training set but weights their contribution by the classifier's own confidence in those neighbors. This acts as a "sanity check": if a test sample is in a region where the training data has high confidence, the score is maintained. If the sample is in a "far-OOD" region devoid of high-confidence training points, the guidance term drops toward zero, effectively muting any overconfident signals from the base classifier.

Experimental Results

NNGuide was evaluated on large-scale benchmarks using ImageNet-1k as the in-distribution dataset. The results demonstrate that NNGuide achieves state-of-the-art performance across various architectures, including ResNet-50, MobileNet, ViT, and RegNet.

Comparison of FPR95 (lower is better) across various OOD detection methods and model architectures. NNGuide consistently achieves the lowest median error.

On the RegNet-Y/16GF backbone, NNGuide achieved an FPR95 of 16.53%, a significant improvement over the base Energy score (24.73%) and standard KNN (31.26%). Furthermore, the method proved robust to natural distribution shifts (ImageNet-V2) and showed consistent improvements when paired with various base scores like MSP, MaxLogit, and KL Divergence.

Conclusion

NNGuide offers a simple yet powerful way to enhance OOD detection without retraining. By forcing detection scores to respect the actual boundary geometry of the data manifold through confidence-scaled guidance, it effectively mitigates the overconfidence of modern neural networks. While it introduces some computational overhead due to the nearest neighbor search, its plug-and-play nature and superior performance make it a compelling choice for safety-critical applications.

Summary updated

BibTeX
@inproceedings{park2023nearest,
  author = {Jaewoo Park and Yoon Gyo Jung and Andrew Beng Jin Teoh},
  title = {{Nearest Neighbor Guidance for Out-of-Distribution Detection}},
  booktitle = {IEEE/CVF International Conference on Computer Vision},
  year = {2023},
  doi = {10.1109/iccv51070.2023.00162},
}
Figure