ZeroCap: Zero-Shot Image-to-Text Generation for Visual-Semantic Arithmetic

Yoad Tewel, Yoav Shalev, Idan Schwartz, Lior Wolf

Abstract

Recent text-to-image matching models apply contrastive learning to large corpora of uncurated pairs of images and sentences. While such models can provide a powerful score for matching and subsequent zero-shot tasks, they are not capable of generating captions given an image. In this work, we repurpose such models to generate a descriptive text given an image at inference time, without any further training or tuning step. This is done by combining the visual-semantic model with a large language model, benefiting from the knowledge in both web-scale models. The resulting captions are much less restrictive than those obtained by supervised captioning methods. Moreover, as a zero-shot learning method, it is extremely flexible and we demonstrate its ability to perform image arithmetic in which the inputs can be either images or text and the output is a sentence. This enables novel high-level vision capabilities such as comparing two images or solving visual analogy tests.

Introduction

Traditional image captioning models are typically trained in a supervised fashion on curated datasets like MS-COCO. While effective, these models suffer from two major drawbacks. First, they face data bottlenecks, limited by the vocabulary and "world knowledge" present in their training sets, which often results in repetitive, scripted-sounding captions. Second, they are inflexible; once trained, they cannot easily adapt to new domains or perform complex reasoning tasks like comparing images or solving visual analogies.

While models like CLIP (Contrastive Language-Image Pre-training) have incredible zero-shot matching capabilities, they are discriminative—they can identify which sentence matches an image but cannot generate a new sentence from scratch. ZeroCap bridges this gap, turning a matching model into a generative one by combining the visual intelligence of CLIP with the linguistic power of GPT-2 without any additional training or fine-tuning.

Methodology

ZeroCap operates entirely at inference time, steering the language model (GPT-2) using CLIP without updating the weights of the pre-trained models. For every token generated, the system performs a few steps of gradient descent to update the context cache (the Key-Value pairs in the Transformer's attention layers).

The optimization balances a dual-loss objective:

  1. $L_{CLIP}$: Minimizes the cosine distance between the CLIP embedding of the image and the embedding of the text generated so far, ensuring the text accurately describes the image.
  2. $L_{CE}$ (Cross-Entropy): Ensures the generated text remains fluent and follows the natural language distribution of the original GPT-2 model.

This framework enables Visual-Semantic Arithmetic. Because CLIP maps images and text into the same vector space, ZeroCap can perform math on these vectors. To solve an analogy like "A is to B as C is to X," the model calculates a target vector $V_{target} = E_{img}(C) + E_{img}(B) - E_{img}(A)$ and uses that to guide the caption generation. This allows for complex manipulations, such as subtracting a national identity from a leader and adding another to identify a different leader.

Key Results and Insights

ZeroCap demonstrates superior performance in diversity, novelty, and reasoning compared to supervised baselines. Unlike models that might simply describe "a person in a suit," ZeroCap can identify specific celebrities, landmarks, and niche cultural references due to the vast knowledge base of web-scale pre-training.

ZeroCap demonstrates superior real-world knowledge, diversity, OCR capabilities, and the ability to perform visual-semantic arithmetic compared to supervised baselines.
  • Diversity and Novelty: ZeroCap achieved a vocabulary size of 8,681 on COCO (vs. ~2,400 for baselines), and 100% of its generated sentences were novel.
  • Semantic Accuracy: On the CLIP-Score metric, ZeroCap outperformed supervised models (0.87 vs 0.78). While it scores lower on traditional NLP metrics like BLEU, the authors argue these metrics reward mimicking bland human annotations rather than semantic truth.
  • Zero-Shot Reasoning: On the new Visual Relations (VR) benchmark, ZeroCap achieved a 30% Recall@5 success rate, whereas supervised baselines failed almost entirely due to a lack of world knowledge.

Limitations and Future Work

Despite its flexibility, ZeroCap has several limitations. The inference-time optimization makes it significantly slower than standard feed-forward models. It also inherits the biases of its training data, occasionally producing Western-centric results or social biases. Furthermore, like many LLM-based systems, it can "hallucinate" details or include metadata-like text (e.g., photographer names) found in noisy web data. Future work may involve scaling to larger models like GPT-3 or incorporating better length control mechanisms.

Summary updated

BibTeX
@inproceedings{tewel2022zerocap,
  author = {Yoad Tewel and Yoav Shalev and Idan Schwartz and Lior Wolf},
  title = {{ZeroCap: Zero-Shot Image-to-Text Generation for Visual-Semantic Arithmetic}},
  booktitle = {IEEE/CVF Conference on Computer Vision and Pattern Recognition},
  year = {2022},
  url = {https://openreview.net/forum?id=o1vh05CloG},
}
Figure