2. Few-shot learning fundamentals

Industrial visual inspection rarely enjoys the luxury of thousands of labeled defect images per class. A new product line may launch with only a handful of confirmed scratch, dent, or solder-bridge examples, yet the model must generalize to unseen units on the line. Few-shot learning (FSL) addresses exactly this regime by changing not only the model but also the way data is organized and evaluated. The central abstraction is the episode: a self-contained mini-task sampled from the dataset that mimics how the model will be used at deployment.

From Classification to Episode-Based Tasks

In conventional supervised classification, a model is trained once on a fixed label set and evaluated on held-out samples of the same classes. Few-shot learning instead trains and evaluates on many small tasks. Each task is defined by an N-way K-shot specification:

  • N — the number of classes (ways) in the episode. For inspection, these are defect categories such as scratch, dent, contamination, solder_bridge, or ok.
  • K — the number of labeled examples per class (shots) provided as the support set.
  • Q — the number of unlabeled examples per class used as the query set for evaluation.

A typical inspection episode might be 5-way 1-shot with 15 queries per class, meaning the model sees one labeled image for each of five defect types and must classify 75 unlabeled images. The support set plays the role of a "reference card" the inspector would carry; the query set plays the role of production parts arriving on the conveyor.

Key idea: The support set defines the classes for that episode. Different episodes use different class subsets, so the model must learn to compare, not to memorize a fixed classifier head.

Support and Query Sets in Detail

The support set S = {(x_i, y_i)} contains K labeled examples for each of the N classes. The query set Q = {(x_j, y_j)} contains unlabeled (or label-held-out) examples drawn from the same N classes. Crucially, the classes in the support and query sets are identical within an episode, but the class identity itself is only meaningful inside that episode. Class index 0 in episode A may correspond to scratch, while in episode B it may correspond to dent.

This design forces the model to solve a matching problem rather than a fixed-label classification problem. Prototypical Networks, for example, compute a class prototype as the mean embedding of the K support examples:

c_n = (1/K) * Σ_{i=1..K} f_θ(x_i^n)

p(y = n | x_q) = softmax( -|| f_θ(x_q) - c_n ||² )

where f_θ is the embedding network. The query is assigned to the nearest prototype. In inspection, this means a new scratch image is classified by comparing its embedding to the average embedding of the one or few reference scratches in the support set.

Constructing Episodes from an Inspection Dataset

Suppose you have a defect dataset with 12 classes and roughly 40–200 images per class. To build episodes:

  1. Split classes into meta-train, meta-validation, and meta-test sets — for example, 7 / 2 / 3 classes. Class disjointness is mandatory; otherwise the evaluation leaks information.
  2. For each episode, sample N classes from the current split without replacement.
  3. For each sampled class, sample K support images and Q query images without replacement.
  4. Repeat until you have thousands of episodes for meta-training and a fixed, larger set (e.g., 600–2000 episodes) for meta-testing.

A practical inspection configuration might be:

SplitClassesEpisodesN-wayK-shotQ per class
meta-train710,00051 and 515
meta-val21,00051 and 515
meta-test32,00051 and 515

Note that N cannot exceed the number of classes in the split. If meta-test has only 3 classes, a 5-way evaluation is impossible; either add classes or reduce N to 3.

Evaluation Protocols and Metrics

Few-shot evaluation reports mean accuracy over episodes with a 95% confidence interval. Because each episode is a small sample, single-episode accuracy is noisy; averaging over 600–2000 episodes stabilizes the estimate. The standard report format is:

5-way 1-shot: 78.4% ± 0.6%
5-way 5-shot: 89.1% ± 0.4%

The ± value is typically the standard error of the mean across episodes, or a bootstrap confidence interval. For inspection, accuracy alone is insufficient. You should also report:

  • Per-class recall — a rare but safety-critical defect (e.g., solder_bridge) may be systematically confused with ok.
  • False negative rate (FNR) — the fraction of true defects classified as ok. In inspection, FNR is usually the dominant cost.
  • Balanced accuracy — the mean of per-class recalls, which corrects for class imbalance across episodes.
Common pitfall: Reusing the same query images across many episodes inflates confidence intervals and can leak information if the model is meta-trained on overlapping data. Always sample query sets without replacement within an episode and keep meta-test episodes fixed before any hyperparameter tuning.

Why Episode Design Matters for Inspection

Inspection tasks differ from generic FSL benchmarks in three ways. First, class imbalance is extreme: ok samples vastly outnumber defects. Episode sampling should therefore either exclude ok from N-way tasks or treat it as one class among many with balanced sampling. Second, defect appearance is highly localized: a scratch may occupy 2% of the image. Embedding networks should use sufficient input resolution or patch-based features. Third, domain shift is constant: lighting, camera angle, and material change between production batches. Episodes should be sampled across batches so the model learns batch-invariant features.

The SVG below summarizes the episode construction and evaluation pipeline for an inspection scenario.

N-Way K-Shot Episode Pipeline for Industrial Inspection Inspection Dataset 12 defect classes 40–200 imgs / class Class Disjoint Split meta-train: 7 classes meta-val: 2 · meta-test: 3 Episode Sampler Sample N classes K support + Q query Meta-Learner ProtoNet / MAML embedding f_θ Episode: 5-way 1-shot, Q=15 Support Set K=1 per class 5 labeled images scratch, dent, ... Query Set Q=15 per class 75 unlabeled imgs same 5 classes Prototype Matching c_n = mean(f_θ(support)) d = ||f_θ(query) − c_n||² softmax(−d) → class nearest prototype wins Evaluation mean acc ± CI per-class recall false negative rate balanced accuracy 600–2000 episodes Meta-Training Loop update θ to minimize query loss across sampled episodes Orange path: gradient updates from query loss feed back into the embedding network across episodes.

Practical Checklist for Inspection FSL Experiments

Before reporting few-shot results on an inspection dataset, verify the following:

  • Class splits are disjoint across meta-train, meta-val, and meta-test.
  • Support and query images are sampled without replacement within each episode.
  • Meta-test episodes are fixed and never used for hyperparameter selection.
  • Reported accuracy includes a confidence interval over at least 600 episodes.
  • Per-class recall and FNR are reported, especially for safety-critical defect classes.
  • Episode sampling is stratified across production batches to reflect domain shift.

With these protocols in place, few-shot learning becomes a reproducible and meaningful evaluation framework for industrial inspection, where labeled defects are scarce but the cost of a missed defect is high.