Applied AI
Finding defects when you barely have any examples of them
By the Raw Code team · · 3 min read
Short answer
If labelled defects are rare, don't train a model to recognize defects. Train it to recognize normal products, and flag anything that doesn't fit. That's the approach our ML lead used for Wienerberger's roof tile line: Vision Transformer features scored by a One-Class SVM.
Why the obvious approach doesn't work
The textbook way to catch broken products is a classifier: show a model thousands of photos labelled "good" and "broken" and let it learn the difference. That needs thousands of broken examples, and a well-run line doesn't produce many. The ones it does produce come in a dozen shapes: chips, cracks, warping, glaze faults. You'd need plenty of each.
That was the situation on Wienerberger's roof tile line. Tiles came through at high volume, people watching the line caught the broken ones, and labelled examples of defects were scarce.
Flip the question: learn what normal looks like
Sound tiles, on the other hand, come off the line all day. So instead of asking "is this a defect?", the model asks "does this look like the tiles I've seen before?" Anything that doesn't is flagged. This is unsupervised anomaly detection, and it has a useful side effect: it can flag a kind of defect nobody has seen yet, because it never needed an example of one.
How the model is built
The system has two parts, built in Python and PyTorch:
- A Vision Transformer as a feature extractor. A model already trained on a large image collection turns each tile photo into a vector describing its shape and texture. Nobody has to train that part from scratch on factory data.
- A One-Class SVM on top. It's fitted only on the vectors from sound tiles and draws a boundary around them. A new tile is scored by how far outside that boundary it lands.
Splitting it this way keeps the factory-specific part small. The heavy lifting of seeing comes from the pretrained model; the part that learns "our tiles, our lighting, our camera" is a much simpler model that can be refitted quickly.
The threshold is a business decision
The model gives each tile a score, not a verdict. Somewhere a line has to be drawn: above this score, pull the tile. Draw it too low and the line stops for tiles that were fine. Draw it too high and broken tiles ship.
Where that line goes depends on what each mistake costs, which is a question for whoever runs the line, not for the model. The handful of labelled defects that do exist are what you use to set it: they show how far outside the boundary real defects tend to land.
What you need to start
- A camera in a fixed position with consistent lighting. The model learns the photos, not the tiles, so a moved camera looks like a defect.
- Plenty of images of normal products under real production conditions.
- A small set of real defects to check the model and set the threshold.
- Someone on the line who can say what a false alarm costs compared with a missed defect.
Where it struggles
Anything that changes what "normal" looks like, such as a new product colour, a new mould, or a new camera, needs the normal set refreshed and the model refitted. And a defect that looks almost exactly like a sound product, such as a hairline crack at the wrong angle, can sit inside the boundary. That's a reason to test on real defects before trusting the scores, not a reason to skip the approach.
We build these as part of our computer vision work. The same few-labels problem shows up well beyond roof tiles: in any inspection task where faults are rare and varied.
Related
More from the blog
Have a problem like this?
Tell us about it on a free call and we'll say what we'd try first.
Book free consultation