GenAIHub
← Back to Technical Section

CLIP

Connecting Vision and Language Through Contrastive Learning

What is CLIP?

CLIP (Contrastive Language-Image Pre-training) is a breakthrough neural network introduced by OpenAI in 2021 that efficiently learns visual concepts from natural language supervision. Unlike traditional computer vision models that require manually labeled datasets, CLIP learns from image-text pairs found on the internet, enabling powerful zero-shot learning capabilities.

πŸ’‘ Key Innovation: CLIP can be applied to any visual classification task by simply providing the names of visual categories in natural language, similar to the zero-shot capabilities of GPT-2 and GPT-3.

CLIP has become the foundation for most modern vision-language models and multimodal AI systems, revolutionizing how machines understand visual content.

Zero-Shot

No training needed

Multimodal

Vision + Language

Scalable

400M+ pairs

Flexible

Any visual task

CLIP Architecture

CLIP uses a dual-encoder architecture with separate vision and text encoders trained using contrastive learning:

Image Input Vision Encoder (ViT/ResNet) Text Input Text Encoder (Transformer) Shared Feature Space Cosine Similarity Score

Vision Encoder

Transforms images into feature vectors

ViT or ResNet architecture

Text Encoder

Converts text to feature vectors

Transformer architecture

Contrastive Learning

Matches image-text pairs

Cosine similarity optimization

Training Process

CLIP is trained on massive datasets of image-text pairs using contrastive learning. The model learns to match corresponding pairs while pushing non-matching pairs apart in the shared feature space.

Training Statistics

400M+

Image-Text Pairs

32

Training Days

592

V100 GPUs

0.5B

Parameters (ViT-B/32)

πŸš€ 2025 Update: Meta CLIP 2 and OpenVision 2 have pushed the boundaries further with worldwide scaling recipes and improved multimodal capabilities.

Zero-Shot Learning

CLIP's most powerful capability is zero-shot learning - the ability to classify images without any task-specific training. Simply provide text descriptions of the categories you want to recognize.

Zero-Shot Classification Example

# Text prompts for zero-shot classification
categories = [
    "a photo of a cat",
    "a photo of a dog", 
    "a photo of a car",
    "a photo of a bird",
    "a photo of a tree"
]

# CLIP computes similarity scores
scores = clip_model(image, categories)
# Returns: [0.85, 0.12, 0.03, 0.05, 0.02]
# Prediction: "cat" with 85% confidence
                

βœ… Advantages

  • β€’ No training data required
  • β€’ Flexible category definitions
  • β€’ Robust to distribution shifts
  • β€’ Easy to implement

🎯 Use Cases

  • β€’ Image search and retrieval
  • β€’ Content moderation
  • β€’ Product categorization
  • β€’ Visual question answering

Model Variants

Model Vision Encoder Parameters Image Size Performance
CLIP ViT-B/32 ViT-Base/32 151M 224Γ—224 76.2% ImageNet
CLIP ViT-L/14 ViT-Large/14 304M 224Γ—224 79.8% ImageNet
CLIP ViT-H/14 ViT-Huge/14 632M 224Γ—224 81.5% ImageNet
CLIP ResNet-50 ResNet-50 77M 224Γ—224 76.7% ImageNet

Applications

πŸ”

Image Search

🏷️

Classification

πŸ€–

Robotics

🎨

Creative AI

πŸ›‘οΈ

Content Safety

πŸ›’

E-commerce

Implementation

Basic CLIP Usage

import torch
from PIL import Image
from transformers import CLIPProcessor, CLIPModel

# Load pre-trained CLIP model
model = CLIPModel.from_pretrained("openai/clip-vit-base-patch32")
processor = CLIPProcessor.from_pretrained("openai/clip-vit-base-patch32")

# Load image and prepare text
image = Image.open("example.jpg")
texts = ["a photo of a cat", "a photo of a dog", "a photo of a car"]

# Process inputs
inputs = processor(text=texts, images=image, return_tensors="pt", padding=True)

# Get features and similarity scores
outputs = model(**inputs)
logits_per_image = outputs.logits_per_image
probs = logits_per_image.softmax(dim=1)

print("Classification probabilities:", probs)
                

Zero-Shot Classification Function

def zero_shot_classify(image_path, candidate_labels):
    """Classify image using CLIP zero-shot learning"""
    image = Image.open(image_path)
    
    # Format text prompts
    text_inputs = [f"a photo of a {label}" for label in candidate_labels]
    
    # Process inputs
    inputs = processor(
        text=text_inputs,
        images=image, 
        return_tensors="pt", 
        padding=True
    )
    
    # Get predictions
    with torch.no_grad():
        outputs = model(**inputs)
        probs = outputs.logits_per_image.softmax(dim=1)
    
    # Return sorted results
    results = []
    for i, label in enumerate(candidate_labels):
        results.append((label, probs[0][i].item()))
    
    return sorted(results, key=lambda x: x[1], reverse=True)

# Example usage
labels = ["cat", "dog", "bird", "car", "tree"]
results = zero_shot_classify("image.jpg", labels)
for label, score in results:
    print(f"{label}: {score:.3f}")
                

Recent Developments (2024-2025)

πŸš€ Meta CLIP 2

Worldwide scaling recipe with improved training efficiency and performance on multimodal tasks.

πŸ‘οΈ OpenVision 2

Family of generative pretrained visual encoders for enhanced multimodal learning capabilities.

🎯 HQ-CLIP

High-quality image-text datasets and improved CLIP models using large vision-language models.

πŸ”„ LVLM Integration

CLIP as foundation encoder for Large Vision-Language Models like GPT-4V and LLaVA.

Limitations

⚠️ Technical Limitations

  • β€’ Struggles with fine-grained classification
  • β€’ Limited to single object/concept per image
  • β€’ Large computational requirements
  • β€’ Limited spatial reasoning capabilities

πŸ“Š Data Limitations

  • β€’ Can be biased by training data
  • β€’ Limited to concepts in training data
  • β€’ Performance varies across languages
  • β€’ May reflect internet biases

Learn More

Related Topics

Test Your Knowledge

Score 8/10 or higher to pass