4. Data challenges in industrial inspection
Industrial visual inspection datasets rarely resemble the balanced, well-annotated benchmarks used in academic few-shot learning research. A production line may produce tens of thousands of conforming parts per shift while a critical defect appears only a few times per month. This section examines the four dominant data challenges — class imbalance, rare defects, labeling cost, and dataset construction — and describes practical strategies for building datasets that support few-shot and transfer learning pipelines.
Class Imbalance and Its Consequences
Class imbalance in inspection data is not merely a statistical nuisance; it directly shapes what a model learns. In a typical surface-defect dataset, the ratio of OK (conforming) to NG (non-conforming) samples can exceed 1000:1. When trained naively with cross-entropy loss, the model minimizes overall error by predicting the majority class almost everywhere. The resulting classifier achieves high accuracy but near-zero recall on the defect class — precisely the failure mode that matters most in quality control.
Two complementary remedies are standard. The first operates at the loss level: focal loss down-weights easy majority-class examples so that gradient updates concentrate on hard, rare samples. The second operates at the sampling level: class-balanced sampling or oversampling of minority classes ensures each mini-batch contains a meaningful proportion of defect images. In practice, a combination works best. For a dataset with 50,000 OK images and 200 NG images, a sampler that draws 8 NG and 24 OK per batch (1:3 ratio) with focal loss (gamma=2.0, alpha=0.25) typically yields substantially higher defect recall than either technique alone.
Rare Defects and the Long Tail
Beyond simple binary imbalance, real inspection data exhibits a long-tailed distribution over defect types. Scratch, dent, stain, and discoloration may each have hundreds of examples, while crack, void, and foreign-material inclusion may have fewer than ten. The tail categories are often the most safety-critical, yet they are the least represented.
Few-shot learning directly addresses this tail. The practical workflow is:
- Identify tail categories by computing per-class instance counts and flagging classes below a threshold (e.g., fewer than 20 instances).
- Construct N-way K-shot episodes from these categories. For a 5-way 5-shot setup, each episode samples 5 defect types with 5 support images each, plus a query set for evaluation.
- Pre-train on head classes using standard supervised learning or self-supervised methods (e.g., SimCLR, MoCo) to learn general visual features.
- Fine-tune or meta-train on episodic tasks so the model learns to adapt quickly to unseen defect types.
A common pitfall is treating rare defects as a homogeneous "other" class. This collapses distinct failure modes into one label and destroys the model's ability to distinguish a crack from a stain — a distinction that determines whether the part is scrapped or reworked.
Labeling Cost and Annotation Strategies
Annotation for industrial inspection is expensive because it requires domain expertise. A trained quality engineer may need 30–60 seconds to label a single complex image, and ambiguous cases require consensus among multiple inspectors. For a dataset of 10,000 images, this translates to hundreds of person-hours.
Several strategies reduce labeling cost without sacrificing quality:
- Active learning: Train an initial model on a small labeled set, then select the most informative unlabeled samples (e.g., highest prediction entropy or lowest confidence) for human annotation. This typically achieves the same model performance with 40–60% fewer labels.
- Weak supervision: Use image-level labels (OK/NG) instead of pixel-level segmentation masks. For defect localization, class activation maps (CAM) or Grad-CAM can provide coarse localization from image-level supervision.
- Semi-supervised learning: Leverage large volumes of unlabeled images through consistency regularization (e.g., FixMatch) or pseudo-labeling. In inspection settings where unlabeled data is abundant, this is often the highest-leverage technique.
- Synthetic defect generation: Render or composite synthetic defects onto conforming images using physics-based rendering or generative models. This augments rare classes but must be validated — synthetic defects that do not match real defect appearance can hurt performance.
Dataset Construction Strategies
Building an inspection dataset is an engineering task with its own methodology. The following steps outline a reproducible approach:
Step 1 — Define the taxonomy. Work with quality engineers to enumerate defect types, severity levels, and acceptance criteria. Each category should have a clear textual definition and at least 3–5 reference images agreed upon by the team.
Step 2 — Collect data across conditions. Inspection images vary with lighting, camera angle, part orientation, and surface finish. Collect data that spans these nuisance variables so the model does not learn spurious correlations. A useful rule of thumb is to capture at least 20–30 images per defect type per distinct imaging condition.
Step 3 — Annotate with quality control. Use a two-pass process: a first annotator labels, a second reviews. Compute inter-annotator agreement (e.g., Cohen's kappa). If kappa falls below 0.8 for a category, the category definition is ambiguous and should be revised before proceeding.
Step 4 — Split by production lot, not randomly. Random splits leak near-duplicate images (same part, adjacent frames) into both training and test sets, inflating reported performance. Split by production lot, shift, or time period to simulate real deployment conditions.
Step 5 — Version and document. Maintain a dataset card recording the number of images per class, imaging conditions, annotation guidelines, known limitations, and intended use. This documentation is essential for reproducibility and for auditing model behavior after deployment.
Putting It Together: A Practical Example
Consider a metal stamping line producing 30,000 parts per day. The inspection camera captures 30,000 images daily. After one month, the dataset contains roughly 900,000 images, of which approximately 1,200 show defects. The defect distribution is: scratch (600), dent (350), stain (180), crack (45), foreign material (25).
A practical construction plan would be:
| Defect Type | Count | Strategy | Target Support Set |
|---|---|---|---|
| Scratch | 600 | Standard supervised + oversampling | — |
| Dent | 350 | Standard supervised + oversampling | — |
| Stain | 180 | Semi-supervised (pseudo-labeling) | — |
| Crack | 45 | Few-shot (5-shot) + synthetic augmentation | 5 per episode |
| Foreign material | 25 | Few-shot (5-shot) + active learning | 5 per episode |
For the crack and foreign-material classes, the model is evaluated on 5-way 5-shot episodes drawn from a held-out set of production lots. The expected outcome is not perfect classification but a measurable improvement over a zero-shot baseline — for example, raising crack recall from 0.12 (pre-trained only) to 0.65 (after episodic fine-tuning). These numbers are illustrative targets, not guaranteed results; actual performance depends on defect visual distinctiveness and imaging consistency.
The feedback loop shown in the diagram is essential. After deployment, collect false negatives and false positives, re-annotate them, and feed them back into the training set. Over successive iterations, the dataset grows where the model is weakest, and the few-shot capability extends to newly discovered defect types without requiring a full re-annotation of the entire dataset.