Editing
Few Shot Zero Shot
(section)
Jump to navigation
Jump to search
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
== <span style="color: #FFFFFF;">Applying</span> == '''CLIP zero-shot classification:''' <syntaxhighlight lang="python"> import torch import clip from PIL import Image model, preprocess = clip.load("ViT-B/32", device="cuda") # Zero-shot classification without any task-specific training def zero_shot_classify(image_path: str, class_names: list) -> dict: image = preprocess(Image.open(image_path)).unsqueeze(0).to("cuda") # Create text descriptions for each class texts = clip.tokenize([f"a photo of a {cls}" for cls in class_names]).to("cuda") with torch.no_grad(): image_features = model.encode_image(image) text_features = model.encode_text(texts) # Normalize and compute cosine similarity image_features /= image_features.norm(dim=-1, keepdim=True) text_features /= text_features.norm(dim=-1, keepdim=True) similarity = (100.0 * image_features @ text_features.T).softmax(dim=-1) return {cls: float(sim) for cls, sim in zip(class_names, similarity[0])} # Works for ANY class names β zero training examples needed! results = zero_shot_classify("wildlife_photo.jpg", ["lion", "elephant", "giraffe", "zebra", "cheetah", "rhinoceros"]) print(sorted(results.items(), key=lambda x: -x[1])) </syntaxhighlight> '''Few-shot classification with Prototypical Networks:''' <syntaxhighlight lang="python"> import torch import torch.nn.functional as F def prototypical_classify(support_embeddings, support_labels, query_embeddings, n_classes): """ support_embeddings: (n_classes * k_shot, D) support set embeddings query_embeddings: (n_query, D) query embeddings Returns: predicted class for each query """ # Compute class prototypes (mean of support embeddings per class) prototypes = torch.stack([ support_embeddings[support_labels == c].mean(0) for c in range(n_classes) ]) # (n_classes, D) # Classify queries by nearest prototype dists = torch.cdist(query_embeddings, prototypes) # (n_query, n_classes) return dists.argmin(dim=1) </syntaxhighlight> ; Few-shot / zero-shot approach selection : '''Vision, zero-shot''' β CLIP (ViT-L/14 for best quality) : '''NLP, zero-shot''' β LLM with task description in system prompt : '''NLP, few-shot''' β LLM with 3-10 examples in context : '''Vision, few-shot''' β Fine-tune CLIP or DINO with support set : '''Structured few-shot''' β Prototypical Networks for consistent task structure </div> <div style="background-color: #8B4500; color: #FFFFFF; padding: 20px; border-radius: 8px; margin-bottom: 15px;">
Summary:
Please note that all contributions to BloomWiki may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
BloomWiki:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Navigation menu
Personal tools
Not logged in
Talk
Contributions
Create account
Log in
Namespaces
Page
Discussion
English
Views
Read
Edit
View history
More
Search
Navigation
Main page
Recent changes
Random page
Help about MediaWiki
Tools
What links here
Related changes
Special pages
Page information