One-Shot Learning
One-shot learning recognizes a new class from a single labeled example, usually by comparing embeddings. Covers Siamese networks, uses, and code.
One-shot learning is the task of recognizing a new class from a single labeled example. A conventional image classification model needs hundreds of labeled images per class and retraining whenever a class is added, while a person can usually recognize a new face or a new product after seeing it once. One-shot systems get closer to that ability by reusing knowledge from many previously learned classes: a network trained on those classes maps images to embeddings in which images of the same thing land close together, so a single example is enough to mark where a new class lies. Face verification, signature checking, and product recognition all work this way, and in large language models (LLMs), one-shot prompting gives the model a single worked example of a task in its prompt.
How One-Shot Learning Works#
Most one-shot systems replace classification with comparison. A neural network performs feature extraction on the stored example and on each new query, and a distance between the two embeddings, such as Euclidean distance or cosine similarity, decides whether they match. A Siamese neural network formalizes this with two branches that share the same weights. Jane Bromley, Yann LeCun, and colleagues used one to verify signatures in 1993, and Gregory Koch and colleagues trained one in 2015 to classify handwritten characters from new alphabets after seeing one example of each:
The embedding network is trained beforehand on many other classes with contrastive or triplet losses, which pull images of the same class together and push different classes apart, so the learned distance generalizes to classes that appear only later. Meta-learning methods such as Matching Networks train on thousands of simulated one-shot tasks, each with one example of several classes, so the model practices exactly the problem it will face.
One-Shot Classification with Embeddings#
Ultralytics YOLO26 classification models return image embeddings through embed(), which pools the output of the layer before the classification head into one vector per image. This example builds a 10-way one-shot classifier for CIFAR-10 from those embeddings: one stored image per class is the only labeled data, and each query takes the label of the most similar stored image:
from pathlib import Path
import torch
import torch.nn.functional as F
from torchvision.datasets.utils import download_and_extract_archive
from ultralytics import YOLO
# CIFAR-10 test images, one folder per class
download_and_extract_archive("https://github.com/ultralytics/assets/releases/download/v0.0.0/cifar10.zip", "data")
images = [sorted(folder.glob("*.png")) for folder in sorted(Path("data/cifar10/test").iterdir()) if folder.is_dir()]
model = YOLO("yolo26n-cls.pt")
# Support set: the first image of each class is its only labeled example (one shot)
shots = torch.stack([model.embed(paths[0], verbose=False)[0] for paths in images])
# Query set: the next 50 images of each class, each labeled with the class of its most similar shot
correct = 0
for label, paths in enumerate(images):
for path in paths[1:51]:
query = model.embed(path, verbose=False)[0]
correct += F.cosine_similarity(shots, query.unsqueeze(0)).argmax().item() == label
print(f"10-way 1-shot accuracy: {correct / 500:.1%} (random guessing: 10%)")The model never trains on CIFAR-10, so the printed accuracy measures how well one example per class defines that class in YOLO26's embedding space, and adding an eleventh class would take one more stored embedding.
Applications#
- Face verification: Automated border gates compare a live camera image with the single photo stored on a passport chip, and phones and access-control systems match each attempt against the face recorded at enrollment.
- Retail product recognition: Stores add new products constantly, and a single catalog photo per item lets a system recognize it on the shelf without retraining.
- One-shot imitation learning: Trains robots to perform a new instance of a task, such as stacking blocks in a new order, after watching a single demonstration.
Challenges#
A single example cannot show how a class varies, so a reference photo taken in poor light, at an unusual angle, or with the object partly hidden gives a poor anchor for every later comparison. The match threshold sets the balance between false accepts and false rejects and has to be tuned on validation pairs for each application, and an embedding trained on one domain, such as everyday photos, separates classes less well in another, such as medical images.
One-Shot Learning vs. Related Terms#
- Few-shot learning: The general problem of learning from a few labeled examples per class. One-shot learning is the case with exactly one, written 1-shot in N-way K-shot notation.
- Zero-shot learning: Recognizes classes with no examples at all, from a text or attribute description.
- Transfer learning: Reuses a pretrained model for a new task, usually by fine-tuning on many labeled examples. One-shot systems rely on transferred features too, but compare them directly without updating any weights.
- Anomaly detection: Learns what normal looks like and flags anything far from it, answering whether an item is unusual instead of which class it belongs to.










