CLIPood: Generalizing CLIP to Out-of-Distributions

Yang Shu, Xingzhuo Guo, Jialong Wu, Ximei Wang, Jianmin Wang, Mingsheng Long · DOI

Abstract

Contrastive Language-Image Pre-training (CLIP) has set a new standard for zero-shot visual recognition. However, fine-tuning these models for specific downstream tasks often results in "distributional collapse," where the model's broad out-of-distribution (OOD) robustness is sacrificed for narrow task-specific accuracy. We present CLIPood, a novel fine-tuning framework that preserves CLIP's generalist capabilities while enhancing its specialist performance. By introducing Margin Metric Softmax (MMS) to maintain semantic relationships and Beta Moving Average (BMA) for optimal temporal ensembling, CLIPood achieves state-of-the-art results across domain-shift and open-class benchmarks.

Introduction: The Challenge of Distributional Collapse

While CLIP provides remarkable zero-shot capabilities, practitioners often need to fine-tune it on specific datasets to maximize performance. This process typically leads to a specialist model that forgets its general knowledge, failing when encountering images from different domains (domain shift) or entirely new object classes (open classes).

Overview of Domain Shift and Open Class challenges.

Existing adaptation methods like standard fine-tuning or prompt tuning (e.g., CoOp) usually fail to balance these two challenges effectively, often improving one at the expense of the other. The authors identify a significant gap in "in-the-wild" scenarios where a model must simultaneously handle both shifts in style and the appearance of unseen categories.

Methodology: Preserving Semantic Structure

CLIPood introduces two core innovations to the fine-tuning pipeline to prevent the collapse of representation space:

1. Margin Metric Softmax (MMS)

Unlike standard fine-tuning which treats all incorrect labels as equally wrong, MMS utilizes the pre-trained text encoder as a semantic anchor. By calculating distances between class labels in the embedding space, MMS applies an adaptive margin. This ensures that semantically similar classes (e.g., "cat" and "tiger") remain closer than dissimilar ones (e.g., "cat" and "car"), forcing the model to preserve the semantic structure of the original CLIP space during adaptation.

2. Beta Moving Average (BMA)

To improve robustness without the overhead of multiple models, CLIPood employs BMA. While standard Exponential Moving Averages (EMA) tend to favor the most recent fine-tuned weights, BMA uses a weighting scheme based on the Beta distribution. This creates a "U-shaped" temporal ensemble that prioritizes both the initial pre-trained weights (the generalist) and the final fine-tuned weights (the specialist), capturing the best of both worlds.

The CLIPood framework architecture.

Crucially, the framework fine-tunes the image encoder while freezing the text encoder to prevent the semantic relationships between concepts from collapsing.

Experimental Evaluation

The researchers evaluated CLIPood across a massive battery of benchmarks, including the DomainBed suite (PACS, VLCS, etc.), ImageNet variants (Sketch, Adversarial, Rendition), and 11 diverse datasets for open-class generalization.

Key Results

  • Superior Robustness: On the DomainBed benchmark, CLIPood achieved an average accuracy of 78.6%, significantly outperforming standard fine-tuning (76.7%) and zero-shot CLIP (70.2%).
  • Balanced Generalization: In open-class experiments, it maintained high accuracy on base classes (83.9%) while significantly leading on new classes (74.5%), achieving a harmonic mean (H) score of 78.9%.
  • Efficiency: These gains come with almost zero additional computational cost during inference and negligible overhead during training.

Conclusion

CLIPood demonstrates that the text modality in CLIP can serve as a vital stabilizer during fine-tuning. By anchoring the image encoder's learning to the semantic structure of the text space and utilizing a balanced temporal ensemble, CLIPood successfully navigates the trade-off between task-specific adaptation and general OOD robustness. Future work may explore the simultaneous adaptation of both modalities without inducing representation collapse.

Summary updated

BibTeX
@inproceedings{shu2023clipood,
  author = {Yang Shu and Xingzhuo Guo and Jialong Wu and Ximei Wang and Jianmin Wang and Mingsheng Long},
  title = {{CLIPood: Generalizing CLIP to Out-of-Distributions}},
  booktitle = {International Conference on Machine Learning},
  year = {2023},
  url = {https://openreview.net/forum?id=DTM83ccsMA},
}
Figure