Traditional automated optical inspection (AOI) relies on golden templates or manually tuned rule-based filters. While effective for simple defects, these methods struggle with complex, variable backgrounds or undefined defect types.
Recently, unsupervised anomaly detection using pre-trained deep convolutional neural networks has emerged as a robust alternative. This guide provides an engineering review of two leading algorithms: PaDiM and PatchCore.
1. The Paradigm of Unsupervised Anomaly Detection
Unlike supervised classifiers, which require thousands of labeled defect images, unsupervised anomaly detection models are trained only on defect-free (normal) samples.
The model learns the statistical distribution of normal parts. During inspection, any region of a part that deviates from this learned normal distribution is flagged as an anomaly. This is ideal for manufacturing where defect rates are low and new defect modes can emerge unexpectedly.
2. PaDiM: Patch Distribution Modeling
PaDiM (Patch Distribution Modeling) models the normal state using patch-level features extracted from different layers of a pre-trained convolutional neural network (such as ResNet or Wide-ResNet).
How it Works
- Feature Extraction: Normal images are passed through the CNN, and patch features are extracted at multiple resolutions.
- Statistical Profiling: For every patch location across all normal training images, the model calculates the mean ($\mu$) and covariance matrix ($\Sigma$) of the feature vectors.
- Anomaly Scoring: During inference, the Mahalanobis distance is calculated between the test patch’s feature vector and the learned Gaussian distribution ($\mu, \Sigma$) at that exact location. A high distance indicates an anomaly.
Engineering Trade-Offs
- Pros: Low computational latency during training; no backpropagation required; anomaly output is natively localized (generates a pixel-level heat map).
- Cons: Relies on strict alignment. If the part is tilted, rotated, or shifted relative to the camera frame, the spatial Gaussian alignment breaks down, leading to false-positive anomaly detections.
3. PatchCore: Memory-Bank Neighborhood Features
PatchCore addresses PaDiM’s alignment sensitivity by using a local neighborhood memory bank of normal features.
How it Works
- Feature Collection: Mid-level CNN features are extracted from normal training images. Local neighborhood patches are pooled to incorporate spatial context.
- Core-Set Reduction: To prevent the memory bank from becoming too large, a greedy core-set selection algorithm is run to select a representative subset of features that covers the feature space.
- Anomaly Scoring: During inference, the feature vectors of the test image are compared to the memory bank using a nearest-neighbor distance calculation. The anomaly score is determined by the distance to the closest normal patch in the memory bank.
Engineering Trade-Offs
- Pros: State-of-the-art anomaly detection rates (AUROC); highly robust to minor part rotation, translation, and local variations; does not require strict pixel-level alignment.
- Cons: Higher memory footprint due to storing the core-set bank; inference latency can be high if the core-set is not optimized or if GPU acceleration is unavailable.
4. Selection Matrix for Manufacturing Deployment (Synthetic Benchmarks)
The choice between PaDiM and PatchCore depends on the cycle time, compute resources, and mechanical alignment capabilities of the inspection station:
| Metric | PaDiM | PatchCore |
|---|---|---|
| Tolerance to Part Rotation | Low (requires sub-pixel hardware alignment) | Moderate-High (local neighborhood tolerance) |
| Training Speed | Extremely Fast (seconds, direct matrix math) | Fast (minutes, depends on core-set reduction) |
| Inference Latency | Low (highly optimized matrix distance) | Moderate (nearest-neighbor search overhead) |
| RAM / Disk Storage | Low | Moderate-High (core-set memory bank size) |
| Suitable Defect Types | Scratches, cosmetic blemishes, missing parts | Complex structural shape defects, surface contamination |
Assumptions and Limitations
- Pre-trained Weight Bias: Both algorithms rely on features pre-trained on ImageNet. While these transfer well to many industrial parts (metals, plastics, glass), they may be less sensitive to highly specialized, sub-micron optical textures.
- Environment Stability: If factory lighting changes or camera focus drifts, the normal distribution changes, causing false positives. Unsupervised models require stable lighting and mechanical fixturing.
- No Defect Classification: These models only flag that something is abnormal and where it is located. They do not classify the defect type (e.g., separating a harmful scratch from a harmless dust particle). A rule-based classifier or secondary model is still required for sorting.