Thursday, February 13, 2025

Zero-Shot Learning (ZSL)


 Zero-Shot Learning (ZSL) is when a model can correctly classify or generate outputs for unseen categories without being explicitly trained on them.


1️⃣ Example: Zero-Shot Image Classification (Using CLIP)

💡 Suppose we have a model trained on general image-text pairs but never trained specifically to classify a "zebra."
👉 Yet, when we give it an image of a zebra, it can classify it correctly by understanding textual descriptions.


from transformers import CLIPProcessor, CLIPModel
from PIL import Image

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

# Load an image (Example: A zebra image)
image = Image.open("zebra.jpg") # Replace with your image path

# Define candidate labels (even if the model has never explicitly seen them)
labels = ["cat", "dog", "horse", "zebra", "elephant"]

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

# Predict using CLIP
outputs = model(**inputs)
logits_per_image = outputs.logits_per_image # Similarity scores
probs = logits_per_image.softmax(dim=1) # Convert to probabilities

# Print the predicted label
predicted_label = labels[probs.argmax()]
print(f"Predicted label: {predicted_label}")

Even if the model was never trained on "zebra," it can recognize it using text descriptions!


2️⃣ Example: Zero-Shot Text Classification

💡 Suppose we have a sentiment analysis model trained only on positive/negative reviews, but we want it to classify news articles into topics without retraining.


from transformers import pipeline

# Load a zero-shot classification pipeline
classifier = pipeline("zero-shot-classification", model="facebook/bart-large-mnli")

# Input text (Example: A finance-related news article)
text = "The stock market saw a significant rise in tech shares today."

# Candidate labels (The model was never explicitly trained on these)
labels = ["sports", "politics", "finance", "technology"]

# Classify the text
result = classifier(text, candidate_labels=labels)
print(result)

Even without training on these specific categories, the model can classify the news article correctly!


3️⃣ Applications of Zero-Shot Learning

🔹 Image recognition (e.g., CLIP for unseen object classification).
🔹 Text classification (e.g., news categorization, intent recognition).
🔹 Machine translation (translate to a language not seen before).
🔹 Medical AI (detect diseases from descriptions instead of labeled data).

No comments:

Search This Blog