Sub topic 4
CLIP's Dual-Encoder Architecture
This chapter assumes you know basic neural-network layers, vector embeddings, dot products, and PyTorch tensor shapes. You should also understand that a Transformer processes token sequences and that an image encoder converts pixels into learned features.
The dual-encoder design
CLIP uses two separate neural networks:
- An image encoder maps an image to a vector.
- A text encoder maps a caption or prompt to another vector.
These networks are called a dual encoder because they encode the two input types independently. Each encoder can process its input without waiting for the other encoder.
Both outputs are passed through learned projection layers. The projection layers convert the image and text features into the same dimension and coordinate system. The result is a joint embedding space, where matching images and text should have similar vector directions.
The encoders do not exchange intermediate features. This differs from a cross-encoder, which combines image and text representations inside later layers. A cross-encoder can model detailed relationships, but it usually must process every image-text pair together. CLIP's independent encoders make large-scale search much faster.
What each encoder does
The image encoder receives a preprocessed image. CLIP variants use either a convolutional network, such as a ResNet, or a Vision Transformer. The encoder produces a feature vector that summarizes visual content.
The text encoder receives token IDs representing a caption. It is a Transformer that uses self-attention to combine information across the token sequence. A final representation summarizes the meaning of the text.
Each encoder then applies a learned linear projection:
z_I = W_I h_I,\qquad z_T = W_T h_THere, \(hI\) and \(hT\) are the encoder features, \(WI\) and \(WT\) are projection matrices, and \(zI\) and \(zT\) are shared-space embeddings.
The embeddings are commonly normalized before comparison:
\hat{z}_I = \frac{z_I}{\|z_I\|},\qquad
\hat{z}_T = \frac{z_T}{\|z_T\|}After normalization, their dot product equals cosine similarity. A high value means that the image and text point in similar directions.
| Component | Input | Output | Main purpose |
|---|---|---|---|
| Image encoder | Pixels | Visual feature | Represent image content |
| Text encoder | Token IDs | Language feature | Represent text meaning |
| Projection layer | Encoder feature | Shared embedding | Align both modalities |
| Similarity function | Two embeddings | Scalar score | Measure image-text compatibility |
Training and inference
During training, CLIP receives batches of matching image-caption pairs. The model compares every image with every caption in the batch. The matching pair should receive the highest similarity score. The other pairs provide negative examples.
At inference time, the same structure supports several tasks. To classify an image, encode candidate labels as prompts and select the text embedding with the highest similarity. To retrieve images for a query, encode the query and rank stored image embeddings. Stored embeddings can be indexed once, so retrieval does not require rerunning the image encoder for every query.
Exercise
Why can CLIP search a large image collection efficiently after computing image embeddings once?
Show answer
The image and text encoders operate independently. Image embeddings can be stored and indexed, while a new text query only needs one text-encoder pass before similarity ranking.