Visual defect detection
An autoencoder that learns what a good product looks like, rejects anything it can't rebuild, and watches for drift in production.
The problem
Manual visual inspection is slow and inconsistent, and defects are rare, so there are too few examples of each fault to train a normal classifier.
Objectives
- Learn from good items only
- Flag cracks, contamination and breakage without labelled defects
- Serve predictions fast enough for a line
- Notice when the camera or lighting changes
Process
- DiscoverChose an anomaly-detection framing because defect examples are scarce.
- DefineReconstruction error against a threshold picked on validation data.
- BuildA convolutional autoencoder trained with MSE plus SSIM loss, exported to ONNX and served by FastAPI.
- ProveAUROC per epoch, ROC curve, reconstructions and an Evidently drift report.
Who it's for
Get a pass or reject with the error score behind it.
See when inputs drift and the model needs a look.
Challenges
- Very few defective examples
- Pixel loss alone misses fine texture damage
- Deployment must not depend on PyTorch
- Lighting changes quietly break vision models
What we built
The model only ever sees good bottles, so any defect shows up as reconstruction error.
Structural similarity catches texture damage that plain pixel loss blurs over.
A FastAPI endpoint takes an image and returns the error, threshold and verdict.
Evidently compares live inputs with training data and raises drift.
Flow
- Camera image
- Resize and normalise
- Autoencoder rebuilds it
- Reconstruction error
- Compare with threshold
- Pass or reject
- Drift report
Tools & languages
The code
Training, ONNX export, the FastAPI server, drift detection, tests and the proof images (loss curves, AUROC, ROC and reconstructions).