CLIP
19 memberslucid.models.multimodal.clipCLIP family — Radford et al., ICML 2021.
Radford, Alec, et al. "Learning Transferable Visual Models From Natural Language Supervision." ICML, 2021, pp. 8748–8763.
CLIP learns a joint embedding space for images and text by contrasting the pairings that actually occurred in a batch against the ones that did not. Given image-caption pairs, an image tower and a text tower produce embeddings each; the objective maximises the cosine similarity of the matched pairs and minimises it for the mismatched ones. Writing and , the logits are
and the loss is a cross-entropy over rows and columns alike,
The symmetry matters: a one-sided loss lets one tower collapse onto the other's geometry, because only one direction is ever scored.
The scalar is learned rather than fixed. It is stored in log space and exponentiated, so the optimiser moves it multiplicatively and it cannot become negative — a temperature is a scale, and gradient descent on the scale itself would happily cross zero. The paper initialises it at the equivalent of and caps the resulting multiplier at 100, which keeps the softmax from saturating early in training when the embeddings are still arbitrary.
What makes the representation transferable is that the text tower is an open-ended classifier. A fixed-label model can only answer with the classes it was fitted to; CLIP answers with whatever captions it is given, so a new task is posed by writing its labels as sentences rather than by training a new head. This is why the prompt matters at inference time and why the paper reports prompt ensembling as an accuracy lever rather than a curiosity.
Classes
CLIPConfig1 methodsFrozen configuration dataclass for every CLIP variant.
CLIPModel5 methodsContrastive Language-Image Pre-training.
CLIPForZeroShotImageClassification2 methodsCLIP posed as a classifier, by writing the labels as sentences.
CLIPOutput1 methodsWhat CLIPModel returns.
CLIPZeroShotOutput1 methodsWhat CLIPForZeroShotImageClassification returns.
CLIPTokenizer3 methodsCLIP's byte-level BPE with </w> word boundaries.
CLIPTokenizerFast1 methodsCLIP's tokenizer with the merge loop running in C++.
Functions
clip_vit_base_16→ CLIPModelConstruct CLIP with a ViT-B/16 image tower.
clip_vit_base_16_zero_shot→ CLIPForZeroShotImageClassificationViT-B/16 CLIP posed as an open-vocabulary classifier.
clip_vit_base_32→ CLIPModelConstruct CLIP with a ViT-B/32 image tower.
clip_vit_base_32_zero_shot→ CLIPForZeroShotImageClassificationViT-B/32 CLIP posed as an open-vocabulary classifier.
clip_vit_large_14→ CLIPModelConstruct CLIP with a ViT-L/14 image tower.
clip_vit_large_14_336→ CLIPModelConstruct CLIP with a ViT-L/14 image tower at 336 pixels.
clip_vit_large_14_336_zero_shot→ CLIPForZeroShotImageClassificationViT-L/14@336px CLIP posed as an open-vocabulary classifier.
clip_vit_large_14_zero_shot→ CLIPForZeroShotImageClassificationViT-L/14 CLIP posed as an open-vocabulary classifier.
Weights
CLIPViTBase16WeightsPretrained weights for lucid.models.clip_vit_base_16.
CLIPViTBase32WeightsPretrained weights for lucid.models.clip_vit_base_32.
CLIPViTLarge14_336WeightsPretrained weights for lucid.models.clip_vit_large_14_336.
CLIPViTLarge14WeightsPretrained weights for lucid.models.clip_vit_large_14.