AI Compass
Compass

Vision transformers and CLIP

Cutting an image into tiles and treating them like text: why that works, when it beats CNNs, and how a joint image-text space arises.

·2 min read·By Fachredaktion Technik
DETAIL
3 sections

The idea

A language model splits a sentence into word pieces and lets each piece look at all the others. A vision transformer does the same with an image: it cuts it into squares, usually 16 by 16 pixels, and treats each square like a word.

What is different

A CNN sees only its immediate neighbourhood at first and becomes far-sighted only across many layers. A transformer can compare a tile at the top left with one at the bottom right from the very first layer. That is an advantage for whole-image relationships and a disadvantage when data is scarce.

Zero-shot classification with CLIP

# Principle, independent of the specific library:
classes = ["a pallet loaded with boxes",
           "an empty pallet",
           "a damaged box"]

image_vec = image_encoder(image)                 # 512 values, normalised
text_vecs = [text_encoder(t) for t in classes]   # 512 values each, normalised

# Cosine similarity, since both vectors are unit length.
scores = [float(image_vec @ t) for t in text_vecs]
print(classes[int(max(range(len(scores)), key=scores.__getitem__))])

Phrasing the class strings is not a detail. "a photo of an empty pallet" regularly beats "empty pallet", because the training material was largely image captions.

What to use when

SituationRecommendation
Under 5,000 own imagesFine-tune a pre-trained CNN
5,000 to 100,000 imagesPre-trained ViT or a modern CNN, evaluate both
Over 100,000 imagesViT scales better
No annotation availableCLIP zero-shot as a baseline
Edge device, little computeCNN, usually with separable convolutions

Splitting into tiles

Tile count and sequence length

N = (H · W) / P² (+1 for the class token)

The number of tiles is the image area divided by the tile area, plus one extra classification token.

H, W
image height and width
P
tile size, usually 14 or 16
N
number of tiles, that is the sequence length

At 224 by 224 with P = 16 that is 196 + 1 = 197 tokens. At 512 by 512 already 1024 + 1. Since attention cost grows quadratically in N, doubling the image side multiplies attention cost by 16: the reason for hierarchical variants such as Swin that compute attention within windows only.

The contrastive loss

InfoNCE, symmetric

L = ½ · [ CE( zᵢ·tⱼ/τ , i ) + CE( tᵢ·zⱼ/τ , i ) ]

Within the batch every image should match its own text and no other; the same holds in the other direction.

zᵢ
the image vector of pair i
tⱼ
the text vector of pair j
τ
temperature, learned, typically around 0.01
N
batch size

Batch size is not an efficiency parameter here but part of the method: every batch element serves as a counterexample for all the others. CLIP was trained with batches of 32,768, and smaller batches yield measurably weaker representations.

Reading attention maps as explanations

Attention weights can be drawn as a map over the image and look like an explanation. They are only partly that: attention shows which tiles were computed together, not which were causal for the result. A defensible justification needs methods such as attention rollout or gradient-based attribution, and even those remain attributions rather than causal claims. Anyone putting such a map into AI Act technical documentation has to say so.

Related courses and sources

PaperFreeEN

An Image is Worth 16x16 Words

Images as a sequence of patches, processed like text. The paper that brought transformers into vision.

For anyone processing image and text in one model.

CourseFree1500 minEN

Hugging Face computer vision course

From image preprocessing through convolutional networks to vision transformers, with runnable examples for detection and segmentation.

For development with image data; assumes Python and delivers runnable examples in exchange.

Hugging FaceGo to offer
Was this page helpful?
Vision transformers and CLIP