‹ all projects
MSc thesis · 2019–2020

Defect detection on metal surfaces

A supervised classifier for these surface defects already existed and worked. My MSc thesis asked a harder question: could an unsupervised model catch the same defects without ever being shown a labelled one? Autoencoders learn what a good surface looks like and flag whatever they can't reproduce.

anomaly detectionunsupervisedcomputer visiondeep learning
Defect detection on metal surfaces

The question

A supervised classifier for these surface defects already existed, and it worked. My MSc thesis at SUPSI went the other way: could an unsupervised model find the same problems without labels?

The question comes from the shop floor, not the lab. Defects are rare, each one looks different, and nobody on the line has time to draw boxes around them. What you have in quantity is scans of good surfaces. So the plan was to learn what “normal” looks like and treat everything else as suspect.

A real scan from the thesis: a machined metal surface at inspection resolution. The even speckle is the healthy texture the model learns to reproduce; the dark pits and bright clusters are the kind of anomaly it has to catch without ever being trained on one.

Learning normal

Convolutional autoencoders trained on small patches cut from defect-free scans. Each patch gets squeezed through a narrow bottleneck and rebuilt, so the network only ever gets good at the textures it keeps seeing. Show it a new patch and the reconstruction error becomes the anomaly score. Healthy texture comes back almost perfectly. A defect never fit through the bottleneck, so the model redraws the texture it expected, and that gap gives it away.

Two choices did most of the work. “Normal” depends on the finish: a rough surface’s healthy texture would read as a defect on a fine one, so I binned the scans by measured roughness (Ra) and trained a separate model for each band. The second idea set the network a harder exam. It saw each patch’s 3×3 neighbourhood with the centre blanked out and had to predict the centre from what surrounded it. Healthy texture is predictable from its context. A defect isn’t.

Turning scores into an inspection was the plain part. Sample around a thousand patches across a scan, score each one, flag everything above a threshold. The threshold came from ROC analysis on a small labelled set kept aside for evaluation and never used in training. Training itself ran on a university HPC cluster that capped each job at under an hour, so I wrote it as a relay: every job saved the model and submitted its own successor before the clock ran out.

The dataset

Around 5,000 grayscale scans, off EDM and laser machines, each measured for roughness. Defective examples were scarce and unlabelled in production, which is the whole reason the unsupervised route was worth trying. The dataset itself is confidential.

Inspect a real scan

The industrial dataset stays private, but one real scan patch from the thesis sits in the demo below. A model learns its normal texture on the page, with no defect labels, then hunts the flaws in it.

Launch the interactive demo

The model trains live in your browser. Switch between the flag boxes and the error heatmap, drag the detection threshold along the two histograms, and click any patch to see what the model saw, what it redrew, and where the two disagree. Standing in for the thesis's convolutional autoencoder is its linear cousin, PCA, small enough to fit in milliseconds on the page.

Afterwards

It was a thesis built on the teacher’s request, and I gave it less of myself than a project I’d have chosen. The work still stands: a detector that learns what a good surface looks like and catches defects it was never shown, because it never needed defect examples to begin with.

next project: Knowledge Graph: a 3D, self-growing map of what I've studied →