Anomaly detection for small factories: start with good parts
Why models trained only on good parts are the practical way to start automated inspection when you have almost no defect images.
Ask a quality manager at a small machining or moulding plant for a thousand labelled images of each defect type and the answer is usually a laugh. Defects are rare by design. A good line might make one bad part in five hundred, and each one is scrapped, reworked or shipped back before anyone thinks to photograph it. A supervised detector that needs hundreds of examples per class is the wrong place to start.
Learn what normal looks like
Unsupervised anomaly detection turns the problem around. Instead of learning each defect, the model learns what a good part looks like and flags anything that departs from it. Good parts are the one thing every plant has in quantity. Fifty to two hundred images of good parts, taken on the real station with the real light, are usually enough to start.
The methods that work best today do not train a network from scratch. They borrow features from a network pretrained on a large image collection and model the distribution of those features on good parts. PaDiM fits a Gaussian to the features at each patch position (Defard et al., 2021). PatchCore keeps a compact memory bank of patch features and scores a new patch by its distance to the nearest stored one (Roth et al., 2022). On the MVTec AD benchmark, PatchCore reported image-level AUROC above 99% across fifteen categories, which is why it became the reference method (Bergmann et al., 2019; Roth et al., 2022).
Benchmarks are not your line
Public benchmark numbers are a floor for comparison, not a promise. MVTec AD images are well lit, centred and taken one at a time. VisA was built to be harder, with multiple instances, complex structures and misalignment (Zou et al., 2022), and scores drop on it. A real line adds variation that neither captures: parts arriving rotated, oil films, a lamp ageing over months, a new batch of raw material with a slightly different finish.
Most of that variation must be removed before the model sees it. A fixture or a cheap alignment step that rotates every part to the same pose is worth more than a cleverer network. So is strobed light that drowns out the windows behind the line. The anomaly model should spend its capacity on defects, not on the sun moving across the shop floor.
Thresholds are a business decision
An anomaly model produces a score, not a verdict. Where the threshold goes decides the trade between escapes (bad parts shipped) and false rejects (good parts scrapped). That trade has a cost on each side, and only the plant knows those costs. We set thresholds per region of the part, because a scratch on a sealing face matters and the same scratch on a hidden face may not. We then run the station in shadow mode, with rejects not yet wired, while quality staff review every flagged part. Their decisions set the final thresholds.
When to add a supervised model
Anomaly detection says "this is different". Sometimes the plant needs to know what is different: a missing clip counted separately from a cosmetic mark, or a burr measured in millimetres. Once shadow mode has collected a few hundred confirmed defects, a supervised detector or segmentation model becomes practical, fine-tuned from a pretrained backbone. The anomaly model stays as a safety net for defect types nobody has seen yet.
Making it fast enough
Memory-bank methods can be slow if the bank is large. Coreset subsampling, as in PatchCore, shrinks it to a small fraction with little accuracy loss. Lighter student-teacher designs such as EfficientAD report millisecond-level latency on a GPU (Batzner et al., 2024). Libraries such as Anomalib make it straightforward to compare these methods on your own images before choosing one (Akcay et al., 2022). On the station we compile the chosen model to a TensorRT engine for the Jetson it will run on, and measure latency there, on the device, not on a desktop GPU.
The short version: start with good parts, control the optics, let quality staff set the thresholds, and add supervised models only when the data exists to train them.
References
- P. Bergmann, M. Fauser, D. Sattlegger, C. Steger. MVTec AD: A Comprehensive Real-World Dataset for Unsupervised Anomaly Detection. CVPR 2019.
- T. Defard, A. Setkov, A. Loesch, R. Audigier. PaDiM: a Patch Distribution Modeling Framework for Anomaly Detection and Localization. ICPR Workshops 2021.
- K. Roth, L. Pemula, J. Zepeda, B. Schölkopf, T. Brox, P. Gehler. Towards Total Recall in Industrial Anomaly Detection. CVPR 2022.
- Y. Zou, J. Jeong, L. Pemula, D. Zhang, O. Dabeer. SPot-the-Difference Self-supervised Pre-training for Anomaly Detection and Segmentation. ECCV 2022.
- K. Batzner, L. Heckler, R. König. EfficientAD: Accurate Visual Anomaly Detection at Millisecond-Level Latencies. WACV 2024.
- S. Akcay, D. Ameln, A. Vaidya, B. Lakshmanan, N. Ahuja, U. Genc. Anomalib: A Deep Learning Library for Anomaly Detection. ICIP 2022.