LLM Learning Roadmap
✨ Awesome-Anything
🖼️ Digital Image Processing
🍃 LLM Components
🌱 LLM Pre-training
☘️ LLM Post-Training
🍀 LLM Popular Models
🪴 LLM Applications
🌿 LLM Optimization
🌾 LLM Compression
🌵 LLM Hands-on Practice
🌴 LLM Must-read Papers
🌳 LLM Q&A
🐝 VLM Image Encoders
📝 MISC.
11.2 CLIP

Contrastive Language-Image Pre-Training (CLIP) is a pre-trained model released by OpenAI in 2021, used for matching images and text. It is a classic work in the field of multimodal research.
Model Structure
The CLIP model consists of two main parts:
- Text Encoder: Used for encoding text to generate text embeddings. It employs the classic text Transformer model.
- Image Encoder: Used for encoding images to generate image embeddings. The authors experimented with ResNet and Vision Transformer.
The embeddings generated by these two encoders are both single vectors of fixed length.
Model Training
CLIP's training is based on Contrastive Learning. The specific steps are as follows:
- Data Preparation: Assume a batch contains 64 text-image pairs, where each pair includes one text and one image.
- Encoding: Use the Text Encoder and Image Encoder to generate embeddings for text and images, respectively, obtaining:
visual_embedding:[64, embedding_size]text_embedding:[64, embedding_size]
- Positive and Negative Samples:
- For each text-image pair, the paired text and image are positive samples.
- For each text, the other 63 images in the same batch are negative samples.
- For each image, the other 63 texts in the same batch are negative samples.
- Data Augmentation:
- “A random square crop from resized images is the only data augmentation used during training.”
Note that in CLIP, there is no typical data augmentation used like SimCLR, MoCo to construct additional positive pairs (such as two different enhancements to the same image), because their main goal is cross-modal contrast learning (image-text), not image-image, and they believe that using real (image-text) pairs is enough to learn strong generalization representations under web-scale data.
Web-scale is a widely used term that usually means a large scale amount of data at the Internet level.
- Similarity Calculation: Perform cross-product between
visual_embeddingandtext_embeddingto obtain a[64, 64]matrix after feature normalization. The values on the diagonal of the matrix represent the inner product of the corresponding image-text features, indicating the cosine similarity of positive samples.
- Loss Calculation: For each row (i.e., the similarity between an image and all texts), set the label for the positive sample to 1. Training is performed using the cross-entropy loss function, with the goal of maximizing the inner product of positive samples.
You can think of the InfoNCE as cross-entropy applied to similarity logits, rather than model-generated class logits.
Code Example
Prev
11.1 Vision Transformer
Next
11.3 BLIP
Loading...