Understanding the properties and limitations of contrastive learning for Out-of-Distribution detection

Nawid Keshtmand, Raul Santos-Rodriguez, Jonathan Lawry

This paper explores why and when Contrastive Learning (CL)—a popular self-supervised technique—succeeds or fails at Out-of-Distribution (OOD) detection. By comparing instance-level discrimination (treating every image as its own class) with supervised class-level discrimination, the authors reveal that the "richness" of the feature space is a double-edged sword: it helps separate vastly different data but can blur the lines between similar ones.

TL;DR

Unsupervised contrastive learning is surprisingly effective at detecting "far-OOD" data (like distinguishing digits from natural scenes) but struggles with "near-OOD" tasks (like distinguishing two different types of natural objects). The paper identifies that contrastive models learn a high-variance feature space that requires supervised inference methods to be truly effective.

What Problem Does This Paper Address?

Deep neural networks are notoriously overconfident, often making high-certainty predictions on data they have never seen before (OOD data). This is a major safety concern for real-world deployments.

While contrastive learning has emerged as a powerful way to learn robust features without needing massive labeled datasets, we don't fully understand its mechanics for OOD detection. Specifically, the authors investigate the trade-offs between instance discrimination (e.g., MoCo), which learns to tell every single image apart, and supervised contrastive learning (e.g., SupCLR), which learns to group images by class.

Methods

The authors evaluate three training objectives using a ResNet-50 backbone:

  1. Cross-Entropy (CE): The standard supervised baseline.
  2. MoCo (Momentum Contrast): An unsupervised approach that treats each data point as a unique class.
  3. SupCLR (Supervised Contrastive): A supervised approach that pulls samples from the same class together and pushes different classes apart.

To detect OOD samples, they use two different inference strategies:

  • Kernel Density Estimation (KDE): An unsupervised method that measures how "dense" the training data is around a new test point.
  • Mahalanobis Distance: A supervised method that uses class labels to measure how far a test point is from the "center" of known ID classes.

They also introduce a metric called Spectral Decay ($\rho$) to quantify the "richness" of the feature space—essentially measuring how many different "directions of variance" the model has learned.

Datasets

The study uses standard benchmarks paired to create different levels of OOD difficulty:

  • Grayscale pairs: MNIST, FashionMNIST, and KMNIST.
  • RGB pairs: CIFAR-10, CIFAR-100, and SVHN.

The authors categorize these pairs into "near-OOD" (semantically similar, like CIFAR-10 vs. CIFAR-100) and "far-OOD" (semantically unrelated, like CIFAR-10 vs. SVHN) using a Confusion Log Probability (CLP) metric.

Table 1: CLP bounds for different ID-OOD dataset pairs.

Key Insights and Ideas

  • The "Richness" Paradox: Unsupervised contrastive learning (MoCo) learns the most "factors of variation" (lowest spectral decay). While this sounds good, it actually makes unsupervised OOD detection (KDE) harder because the OOD data is more likely to overlap with some of these many learned features.
  • The Generic Class Trap: When OOD samples are misclassified, they aren't assigned to random classes. Instead, they gravitate toward ID classes that look most like the entire dataset. These "generic" classes capture non-discriminatory features (like background textures) rather than specific semantic features.
  • Supervision is the Anchor: For contrastive learning to excel at OOD detection, it needs a supervised inference step (like Mahalanobis distance). This allows the model to ignore the "noise" of its rich feature space and focus only on the directions that actually separate known classes.

Key Results

The quantitative results highlight a stark contrast between inference methods. When using KDE (unsupervised inference), MoCo performs significantly worse than the standard Cross-Entropy baseline across almost all datasets.

Table 2. AUROC, AUPR and FPR for different models on different ID-OOD dataset pairs using the KDE approach and * indicates that an approach is better than the CE baseline.

However, when switching to Mahalanobis Distance (supervised inference), the story changes. MoCo becomes highly competitive for far-OOD tasks. For example, on the CIFAR10 (ID) vs. SVHN (OOD) task, MoCo achieves an AUROC of 0.908, outperforming the CE baseline of 0.891.

Table 3: OOD performance using Mahalanobis Distance. MoCo shows significant improvement, especially in far-OOD scenarios.

Limitations and Future Work

The authors acknowledge that their study is limited to contrastive methods that use "explicit negatives" (pushing different samples apart). They suggest that future work should look at "redundancy reduction" methods like Barlow Twins, which don't rely on negative samples.

Additionally, they propose that near-OOD detection could be improved by regularizing the contrastive loss to prevent individual classes from becoming too similar to the overall dataset distribution, thereby forcing the model to learn more distinctive, class-specific features.

Summary updated

BibTeX
@inproceedings{keshtmand2022understanding,
  author = {Nawid Keshtmand and Raul Santos-Rodriguez and Jonathan Lawry},
  title = {{Understanding the properties and limitations of contrastive learning for Out-of-Distribution detection}},
  booktitle = {Workshops @ ICPR},
  year = {2022},
  doi = {10.1007/978-3-031-37660-3\_23},
}
Figure