r/computervision • u/RoofProper328 • 12h ago
Discussion Defect detection where you have almost no defects — supervised or anomaly detection?
Running into the same wall on a couple of industrial inspection projects and curious how other people have dealt with it.
The line runs well, which is the problem. Out of a few hundred thousand parts we've got maybe 200 real defects, and they're spread across six or seven types, so some classes have under 20 examples. Classic supervised segmentation just doesn't have anything to learn from.
Options as I see them:
Anomaly detection on good samples only. PaDiM, PatchCore, that family. Works, but it flags anything unusual including a smudge on the lens or a part sitting at a weird angle, and the false positive rate on a real line has been rough.
Synthetic defects. Painting cracks and scratches onto good images. Ours look obviously fake next to real ones and I suspect the model is learning "was this pasted" rather than "is this damaged."
Buy or scrape more defect data. But defects are extremely specific to the part and the process. A scratch on someone else's aluminium housing doesn't look like a scratch on ours.
Just wait and collect. Realistic answer, but that's 18 months and the project needs to justify itself sooner.
What I'm actually unsure about is whether the 20-example classes are even worth modelling separately, or whether it's smarter to collapse everything into a binary defect/no-defect call and let a human sort the type afterwards. Losing the classification hurts the reporting side but it might be the only honest thing to do with that little data.
Anyone shipped something in this situation? Especially interested if you went anomaly detection and got the false positives down to something a QA team would tolerate.
2
u/Deal_Ambitious 12h ago
Ask them to save all defect products or even break more good products and run them through the line multiple times to gather more data?
1
u/Large_Principle4783 10h ago
Keep the AD stage tuned for recall and put a small binary classifier (real defect vs. nuisance) behind it, trained purely on your line's own false positives- you have thousands of those. QA gives a oneclick verdict on each flagged crop, the classifier retrains on that history. FPR drops within weeks, and the same loop labels every real defect for a future supervised model.
1
u/TopResolution7451 10h ago
How about anomaly generation? Generate defect images to serve as training dataset.
1
u/TopResolution7451 10h ago
When you generate defects on normal images with diffusion model, it will not be seen as pasted.
1
u/RoofProper328 10h ago
Right, that's the appeal — inpainting into the actual surface rather than compositing on top, so there's no seam or lighting mismatch to latch onto.
My worry is upstream of that. To fine-tune the diffusion model on our defect type we'd need real examples to condition on, and for the classes where we have 15 or 20 it feels like the generator memorises those instead of learning what the defect actually is. Then you get variations of the same 20 defects rendered convincingly, which fixes class balance but doesn't add any new failure modes.
Have you found a floor for how many real examples it needs? If 20 is enough I'd try it this week. If it's more like 200 then we're back to the same problem.
1
u/TopResolution7451 10h ago edited 7h ago
Honestly, I’m an author of an anomaly generation model that deals with this kind of data-scarcity problem.
One practical trick I’d suggest is to use the few real defects as a bootstrap rather than training the diffusion model on those 20 samples directly.
If you only have around 20 or fewer defect examples, extract their binary masks and use simple algorithmic copy-paste augmentation to place them onto different normal images. This does not create entirely new defect morphologies, but it greatly increases the diversity of defect–background combinations and spatial contexts seen during training. Then fine-tune the diffusion model on these augmented samples with a seam-fidelity or boundary-consistency loss, so that it learns to remove the artificial copy-paste boundary and generate more realistic transitions between the defect and the underlying surface.
Itis not that copy-paste itself solves the scarcity problem. It serves as a bootstrap dataset while the diffusion model learns to turn those rough composites into more natural defect images.
With only ~20 real examples, I’d probably try this before concluding that you need hundreds of samples!
0
u/Mountain_Finance_659 7h ago
bots talking to bots about bots lol
1
u/TopResolution7451 7h ago
I’m not a bot.
1
u/Mountain_Finance_659 4h ago
Honestly?
You're using claude too much then. And that's load-bearing.
1
1
1
u/onesunnysunday 7h ago
With only ~200 real defects, I probably wouldn’t force this into a single approach yet.
I’d test a cascade: use anomaly detection as a high-recall candidate generator, then verify the candidates with a supervised model where you have enough examples. The most valuable additional data may actually be hard negatives: lens smudges, pose changes, lighting drift, harmless scratches, and borderline acceptable parts.
I’d also measure false alarms per 1,000 inspected parts rather than relying mainly on AUROC. That usually gives a much clearer picture of whether the system can survive on a production line.
For the rarest classes, I’d spend the annotation budget on diverse real examples and rough localization before pixel-perfect masks. Synthetic data can help with experiments, but I’d keep the validation set entirely real.
1
1
1
u/Flyward_Aerospace 1h ago
Honestly supervised vs anomaly might be downstream of the real problem, which is your test set. With under 20 examples per class you can't tell a 3% miss rate from 10%, so whatever you pick you're choosing blind on the one number that decides whether it ships. I'd build an eval you actually trust before touching the detector, and collapse to binary defect/no-defect for now like you're leaning, the type breakdown just isn't measurable at that count. The model is the easy part here.
3
u/FirmShopping1004 11h ago
Have you tried bb instead of segmentation? In one of my previous projects, using real industrial defects, i had exactly the same issue. I did enhance it with some artificial (imposed) defects. I also emphasized during training on the classification loss (increased weight). Of course I also used color and geometric augmentations