Sub topic 5
The Image Encoder: ResNet and Vision Transformer Variants
This chapter assumes you know basic neural-network layers, tensor shapes, convolutional networks, and Transformers. You should also understand that an embedding is a vector representation of an input.
CLIP uses an image encoder to convert an image into a vector. A learned projection then maps that vector into the same dimensional space used by the text encoder. The image encoder does not generate a caption. It extracts visual information that can be compared with a text embedding.
Modified ResNet encoders
A ResNet is a convolutional neural network with residual blocks. Each residual block learns a transformation and adds its output to the block input:
y = F(x) + xThis skip connection makes deep networks easier to optimize. Convolutions detect local patterns such as edges, textures, and object parts. Deeper layers combine these patterns into larger visual structures.
CLIP's ResNet variants are modified rather than copied directly from standard image-classification models. Important changes include:
- Attention pooling: Instead of using only global average pooling, the model uses attention to combine spatial feature locations.
- Anti-aliased downsampling: Blur-like filtering can reduce artifacts when feature maps are downsampled.
- A projection layer: The final visual features are mapped to CLIP's shared embedding dimension.
A ResNet processes the image as a grid throughout the network. This gives it a strong inductive bias toward local structure and translation, meaning similar patterns can be recognized at different image locations.
Vision Transformer encoders
A Vision Transformer, or ViT, treats an image as a sequence of patches. For example, a 224 × 224 image split into 16 × 16 patches produces:
(224 / 16)^2 = 196image patches. Each patch is flattened and projected into a token vector. Positional information is added so the Transformer can distinguish one patch location from another.
Self-attention lets every patch exchange information with every other patch. Early layers can model local relationships, while later layers can connect distant regions, such as a person and the object they are holding.
CLIP ViTs commonly use a special class token or pooled representation as the image summary. A final projection maps that summary into the joint image-text space.
| Encoder | Main operation | Strength | Common trade-off |
|---|---|---|---|
| Modified ResNet | Convolutions and residual blocks | Strong local and spatial bias | Less direct global interaction |
| Vision Transformer | Patch tokens and self-attention | Flexible global relationships | More computation and data demand |
Choosing between the variants
A ResNet can be attractive when computational efficiency and stable spatial processing matter. A ViT often performs better as model scale and training data increase, especially when global composition is important. Neither encoder automatically understands every detail. Both depend on the training data and on the projection learned for image-text alignment.
Exercise
Why does CLIP need a projection layer after its image encoder?
Show answer
The backbone's feature size and representation are not necessarily compatible with the text encoder. The projection maps visual features into the shared image-text embedding space.