Kernel PCA for Out-of-Distribution Detection

Kun Fang, Qinghua Tao, Kexin Lv, Mingzhen He, Xiaolin Huang, Jie Yang

Introduction

Deep Neural Networks (DNNs) are notoriously overconfident. When presented with Out-of-Distribution (OoD) data—inputs they weren't trained on—they often assign high-confidence labels from the In-Distribution (InD) training set. This overconfidence is a significant safety hurdle for deploying AI in real-world environments where unexpected inputs are common.

Traditional approaches have attempted to use Principal Component Analysis (PCA) to detect these anomalies. The logic is straightforward: learn a linear subspace that captures the essence of InD data. If a new sample has a high reconstruction error, it is flagged as OoD. However, this method often fails because InD and OoD features are typically tangled in complex, non-linear manifolds that linear projections cannot untangle.

Methods: CoP and CoRP

To address the limitations of linear PCA, this research introduces two Kernel PCA-based detectors: CoP and CoRP. These methods map features into a high-dimensional, non-linear space where InD and OoD data become linearly separable.

  1. CoP (Cosine mapping followed by PCA): Inspired by the success of K-Nearest Neighbor (KNN) detectors, CoP applies a cosine kernel. This projects features onto a unit sphere before performing PCA, effectively normalizing the feature space.
  2. CoRP (Cosine and RFFs mapping followed by PCA): To capture even more intricate patterns, CoRP applies a Gaussian kernel on top of the cosine-normalized features.

Overcoming Computational Bottlenecks

Traditional Kernel PCA is computationally expensive, requiring an $N \times N$ kernel matrix (where $N$ is the number of training samples). This is infeasible for large datasets like ImageNet. The authors bypass this by using explicit feature mappings. For the Gaussian kernel, they utilize Random Fourier Features (RFFs) to approximate the kernel mapping. This transformation allows the problem to be solved as a standard PCA calculation in the new mapped space, ensuring high speed during inference.

Visualizing the "Aha Moment"

The core insight of this work is that linearity is the primary limitation of previous PCA-based detectors. While InD and OoD data appear as a "complete mess" in the original feature space, the right non-linear mapping—specifically the cosine-Gaussian mapping—creates a clear separation in the reconstruction error distributions.

Visualization of feature separation: The left side shows the overlapping distributions in the original space, while the right side shows the clear separation achieved through the proposed non-linear mapping.

Key Results and Efficiency

CoRP achieves state-of-the-art performance, significantly reducing the average False Positive Rate (FPR95) on ImageNet-1K to 48.60%, compared to 53.97% for the popular KNN detector. Beyond accuracy, the computational gains are massive. By decoupling the detection from the need to search through millions of training samples, CoRP offers a dramatic improvement in both speed and memory usage.

Comparison of computational complexity, inference time, and storage requirements between KNN, CoP, and CoRP.

As shown in the table above, CoRP is approximately 180x faster than KNN during inference and requires only 29 MiB of storage, compared to the 20 GiB needed for KNN on ImageNet.

Conclusion

By moving from linear to Kernel PCA, CoP and CoRP provide a robust and efficient solution for OoD detection. While the current implementation relies on manually tuned kernels, future work may involve Deep Kernel Learning to automatically learn kernel parameters from data, potentially leading to even more specialized and robust detection capabilities.

Summary updated

BibTeX
@inproceedings{fang2024kernel,
  author = {Kun Fang and Qinghua Tao and Kexin Lv and Mingzhen He and Xiaolin Huang and Jie Yang},
  title = {{Kernel PCA for Out-of-Distribution Detection}},
  booktitle = {Advances in Neural Information Processing Systems},
  year = {2024},
  url = {https://openreview.net/forum?id=EZpKBC1ohS},
}
Figure