Skip to content

About

Self-supervised learning for low-annotation surface defect detection and inspection

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

16 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 

Repository files navigation

surface-inspection

A small surface inspection system that segments and scores defects on manufactured surfaces. Given one image of a part, it produces a defect map that localizes any anomaly and a per part pass or fail decision. The whole thing runs on CPU, needs no downloads, and is driven by synthetic textured surfaces so the behavior can be tested against known ground truth.

The idea

A clean manufactured surface has a regular texture. Locally it oscillates around a stable level: think of a woven or machined finish. A defect breaks that regularity in a localized way. Here a defect is either a soft bright or dark blob (a dent, a stain, a burn mark) or a thin scratch line. Each synthetic defective sample comes with a binary mask that marks exactly where the defect sits, which is what lets the tests check localization rather than just guessing.

The detector works at the right spatial scale instead of looking at raw pixels. It runs a difference of Gaussians band pass over the image. The smaller Gaussian keeps the defect, the larger one captures slow shading, and subtracting them removes the fine grained texture and the global gradient at the same time. What survives is the mid scale bump or ridge that a defect creates.

That band pass response is then turned into a robust z score map using the median and the median absolute deviation. Robust statistics matter here: if you used the plain mean and standard deviation, a strong defect would inflate the spread it is being measured against and hide itself. The median absolute deviation is barely moved by a small bright region, so the defect stands out clearly against it.

Finally the z score map is thresholded, cleaned of single pixel speckle, and split into connected components. A component only counts as a defect if its area clears a minimum, which separates a compact real defect from the scattered noise a clean surface produces. A part fails inspection when at least one such region is present, and the union of those regions is the defect map.

Layout

src/
  synth.py        synthetic surfaces and defects with ground truth masks
  inspector.py    band pass anomaly detector, defect map, pass or fail
  metrics.py      IoU, centroid distance, pass or fail accuracy
tests/
  test_synth.py
  test_inspector.py
  test_metrics.py

Usage

from src.synth import make_surface
from src.inspector import SurfaceInspector

surface = make_surface(size=64, defective=True, defect_type="blob", seed=0)
inspector = SurfaceInspector()
result = inspector.inspect(surface.image)

print(result.is_defective)   # True when the part fails
print(result.defect_area)    # number of pixels flagged
result.defect_map            # bool array localizing the defect
result.score_map             # per pixel anomaly z score

make_dataset builds a balanced mix of clean and defective parts for batch evaluation, and src/metrics.py scores both the localization and the pass or fail calls.

What a run produced

On a balanced synthetic set of 200 parts at 64 by 64 (seed=99), the default inspector reached a pass or fail accuracy of 0.955 against a chance baseline of 0.5. On the defective parts the predicted defect map had a mean intersection over union of 0.547 with the ground truth mask, and the predicted defect centroid sat 0.75 pixels on average from the true defect center. These numbers come from the code in this repo and will shift with the seed and the parameters.

Tests

python -m pytest tests/ -q

The tests are behavior checks rather than fixed number snapshots. They confirm that clean surfaces carry empty masks while defective ones do not, that the synthesis is reproducible under a seed, that the defect map overlaps the true defect region and its centroid lands within a few pixels of the truth, that clean parts mostly pass while defective parts are caught, and that the pass or fail accuracy clears chance by a wide margin. The metric helpers are checked on hand worked cases with known answers.

About

Self-supervised learning for low-annotation surface defect detection and inspection

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages