The problem
CLIP (Contrastive Language-Image Pre-Training) learns a shared space for images and text from pairs of an image and its caption. An image encoder and a text encoder each produce an embedding vector; training pulls the embedding of an image close to that of its own caption, and pushes it away from the captions of the other images of the batch. You will write the loss, following the pseudocode in Figure 3 of Radford et al. (2021), and the zero-shot classifier built on it.
The loss
Take a batch of matching pairs: image goes with caption .
- Normalize each image embedding and each text embedding to unit length (divide by the L2 norm: the usual length of a vector, over the last axis). Now the dot product of two embeddings is their cosine similarity: 1 when they point the same way, 0 when unrelated.
- Logits:
img @ txt.T / temperature, an table. Entry compares image with caption ; the correct pairs are on the diagonal. The temperature, a small number, makes the differences bigger. - Two cross-entropies (section 2) with the diagonal as the target:
loss_iover each row (image must pick its caption among ), andloss_tover each column (caption must pick its image). - Return
(loss_i + loss_t) / 2.
Example. Two images with embeddings and , two captions with embeddings and .
- Cosine similarities: : the diagonal is the largest in each row and column.
- With
temperature = 0.5the logits are these times 2; the row cross-entropies are small, and the loss is . Withtemperature = 1: . With the learned CLIP temperature : almost 0 ().- If the embeddings carried no information, so that every caption looked the same for every image, the loss would be : two equally likely choices.
The column cross-entropy is the row cross-entropy of logits.T.
Zero-shot classification
Without training for the task, a CLIP model can classify an image: write one caption per class ("a photo of a dog", "a photo of a cat"), embed the captions, and for each image pick the class whose caption embedding has the highest cosine similarity.
Example. With the embeddings above, treat the two captions as two classes. Image is most similar to caption 0 (0.98 against 0.10): class 0. Image : class 1.
What to implement
clip_loss(img_emb, txt_emb, temperature): the symmetric loss, a float.zero_shot_predict(img_emb, class_txt_emb): for each image, the index of the class text embedding with the highest cosine similarity.
Requirements
- Stable log-softmax (the temperature 0.07 makes logits large).
- No loops.