Finetuning CLIP to Reason about Pairwise Differences

Dylan Sam, Devin Willmott, João D. Semedo, J Zico Kolter

Abstract

Vision-language models (VLMs) such as CLIP are trained via contrastive learning between text and image pairs, resulting in aligned image and text embeddings that are useful for many downstream tasks. A notable drawback of CLIP, however, is that the resulting embedding space seems to lack some of the structure of its purely text-based alternatives. For instance, while text embeddings have been long noted to satisfy analogies in embedding space using vector arithmetic, CLIP has no such property. In this paper, we propose an approach to natively train CLIP in a contrastive manner to reason about differences in embedding space. We finetune CLIP so that text descriptions of differences between images correspond to their difference in image embedding space, using synthetically generated data with large language models on image-caption paired datasets. We first demonstrate that our approach yields significantly improved capabilities in ranking images by a certain attribute (e.g., elephants are larger than cats), which is useful in retrieval or constructing attribute-based classifiers, and improved zeroshot classification performance on many downstream image classification tasks. In addition, our approach enables a new mechanism for inference that we refer to as comparative prompting, where we leverage prior knowledge of text descriptions of differences between classes of interest, achieving even larger performance gains in classification. Finally, we illustrate that the resulting embeddings obey a larger degree of geometric properties in embedding space, such as in text-to-image generation.

Introduction

Vision-Language Models (VLMs) like CLIP have revolutionized multimodal tasks by aligning image and text embeddings in a shared latent space. However, these models often lack the fine-grained geometric structure found in purely text-based embeddings. While word embeddings famously support vector arithmetic (e.g., $King - Man + Woman \approx Queen$), CLIP's embedding space does not natively support such analogies. CLIP often behaves like a "bag-of-words" model, recognizing individual concepts but struggling with relative reasoning, such as understanding that one object is "larger than" another or distinguishing between visually similar classes like a crab and a lobster.

PC-CLIP: Aligning Differences in Embedding Space

We propose PC-CLIP (Pairwise Comparison CLIP), a finetuning approach that natively trains CLIP to reason about differences. The goal is to ensure that text descriptions of differences between images correspond to their vector subtraction in the image embedding space.

Training Methodology

The PC-CLIP training pipeline relies on three main components:

  1. Synthetic Data Generation: Since datasets describing visual differences between images are rare, we use a Large Language Model (LLM), specifically LLaMA2-13B, to generate synthetic training data. We prompt the LLM with captions from the COCO dataset to describe the visual differences between pairs of images (e.g., "What is the visual difference between an image of {Caption A} and an image of {Caption B}?").
  2. Contrastive Finetuning: We finetune the CLIP text encoder while keeping the image encoder frozen. The model is trained to align the text embedding of the LLM-generated difference ($f(T_{diff})$) with the vector difference of the corresponding image embeddings ($g(I_1) - g(I_2)$).
  3. Comparative Prompting: This new inference mechanism leverages the learned geometric structure. When the model is uncertain between two classes, we use an LLM to describe the difference between them and use that vector to "nudge" the class prompts apart in the embedding space, improving discriminative accuracy.
The PC-CLIP training pipeline aligns the difference in image embeddings with LLM-generated text descriptions of those differences.

Key Results and Insights

PC-CLIP demonstrates significant improvements in both relative attribute reasoning and zero-shot classification.

Attribute Ranking and Zero-Shot Classification

PC-CLIP significantly enhances the model's ability to rank images by specific attributes. For instance, in "which animal is larger?" tasks, PC-CLIP outperformed standard CLIP by up to 14 percentage points in absolute accuracy. Furthermore, the finetuned text encoder improves standard zero-shot classification performance across most datasets, including a +2.19% improvement on EuroSAT.

Performance Gains from Comparative Prompting

The introduction of comparative prompting yields even more substantial gains, particularly on datasets with visually similar categories. By using text descriptions of differences to refine class embeddings at inference time, we observe dramatic boosts in accuracy.

Comparative prompting yields significant accuracy improvements over standard CLIP, particularly on datasets with visually similar classes like Flowers102 and EuroSAT.

As shown in the results above, comparative prompting (+ comp) provides a significant boost, such as a +9.38% improvement on Flowers102 and a +7.00% increase on EuroSAT compared to the base PC-CLIP model.

Discussion and Future Work

By enforcing geometric properties like subtraction arithmetic ($I_1 - I_2 \approx T_{diff}$), PC-CLIP creates a more structured latent space. This improved structure also benefits generative tasks; when used with Stable Diffusion XL, the PC-CLIP text encoder produces images that are more visually consistent with complex descriptive attributes.

Current limitations include the reliance on caption-based synthetic data, which can lead to hallucinations if the base captions are sparse. Future research could explore using Multimodal LLMs (MLLMs) to generate differences based on actual pixel content, potentially further improving the alignment and reasoning capabilities of the model.

Summary updated

BibTeX
@article{sam2025finetuning,
  author = {Dylan Sam and Devin Willmott and João D. Semedo and J Zico Kolter},
  title = {{Finetuning CLIP to Reason about Pairwise Differences}},
  journal = {Accepted by TMLR},
  year = {2025},
  url = {https://openreview.net/forum?id=USNJFZTWPn},
}
Figure