pranayr710/Self-Supervised-Defect-Detection
0
Self-Supervised Defect Detection (PatchCore-Lite)
An end-to-end "Label-Free" computer vision pipeline for detecting unknown surface defects, training exclusively on images of perfect parts.
๐ง Core Concept
The model never sees a single defect during training. Instead, it memorizes the spatial feature distribution of normal surfaces. At test time, any region whose features deviate significantly from this learned distribution is flagged as anomalous.
In short: Defects = deviations in feature space, not a classification problem.
๐ฌ Why PatchCore-Lite Works
๐ Project Structure
project/
โโโ config.py # Device, paths, backbone, hyperparameters
โโโ dataset.py # MVTec AD PyTorch Dataset + augmentations
โโโ model.py # Frozen feature extractor (ResNet18 / DINOv2)
โโโ train.py # Builds and saves the coreset memory bank
โโโ inference.py # Single-image / folder heatmap generation + CSV export
โโโ evaluate.py # Image-level & pixel-level AUROC evaluation
โโโ calibrate.py # Optimal threshold via Youden J statistic
โโโ app.py # Gradio web UI for interactive inspection
โโโ utils.py # Distance computation, heatmap rendering
โโโ requirements.txt # Python dependencies
โโโ results/ # Generated heatmaps & score distribution plot
โโโ README.mdโ๏ธ Setup
1. Install Dependencies
pip install -r requirements.txt2. Download Dataset
Download the MVTec AD Dataset and extract the hazelnut category so the structure looks like:
dataset/
โโโ hazelnut/
โโโ train/
โ โโโ good/ โ only normal images
โโโ test/
โโโ good/ โ normal test images
โโโ crack/ โ defect type 1
โโโ cut/ โ defect type 2
โโโ hole/ โ defect type 3
โโโ print/ โ defect type 4๐ Usage
Step 1 โ Train (Build Memory Bank)
python train.pyExtracts patch features from all good images and saves memory_bank.pt.
Step 2 โ Evaluate (Compute AUROC)
python evaluate.pyRuns all test images and prints the Image-Level AUROC score.
Step 3 โ Inference (Single Image)
python inference.py --image dataset/hazelnut/test/crack/000.png
python inference.py --image dataset/hazelnut/test/crack/000.png --output my_heatmap.pngOutputs:
- Anomaly score (printed to console)
- Heatmap image saved to
output_heatmap.png(or custom path)
๐ Performance
๐ผ๏ธ Example Results
Generate these files first by running: `` python inference.py --image dataset/hazelnut/test/good/000.png --output results/heatmap_good.png python inference.py --image dataset/hazelnut/test/crack/000.png --output results/heatmap_crack.png python inference.py --image dataset/hazelnut/test/hole/000.png --output results/heatmap_hole.png ``Good Image โ Low Activation (No Defect)
Input: test/good/000.png
Anomaly Score: 0.7778
Result: Uniform cool blue โ no anomaly detected.Normal hazelnut: Low-intensity blue heatmap (no red regions)
Defect Image โ Crack Detected
Input: test/crack/000.png
Anomaly Score: 0.8465
Result: Bright red concentrated on the crack location.Hazelnut with crack: Intense red overlay precisely on the crack
Defect Image โ Hole Detected
Input: test/hole/000.png
Anomaly Score: 0.8681
Result: Sharp red hotspot on the hole area.Hazelnut with hole: Focused red highlight on the hole
๐ก๏ธ Key Implementation Details
- Feature Normalization: L2 normalization applied before memory bank storage and before distance computation to ensure scale-invariant matching.
- Balanced Feature Fusion:
layer3features are scaled by0.5before concatenation withlayer2, preventing deeper semantic features from dominating fine-grained texture signals. - Batched Distance Computation:
torch.cdistis run in configurable batches (default 1024) to prevent OOM on standard GPUs. - Safe Heatmap Normalization: Division-by-zero guarded with
1e-8epsilon. - Cross-Device Compatibility: Memory bank is saved on CPU and loaded to the correct device at runtime.
- Reproducibility: Fixed seeds (
torch.manual_seed(42),np.random.seed(42)).
๐ License
This project is for educational and research purposes.
