Team Ai
Apppublic

pranayr710/Self-Supervised-Defect-Detection

sourceHugging Faceupdated 7mo agoView on Hugging Face
0likes
App README

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

PropertyExplanation
No defect labels neededTrains only on "good" images โ€” no annotation cost.
Detects unknown anomaliesBecause it models normalcy, it catches any deviation โ€” scratches, dents, cracks, holes โ€” even types never seen before.
Nearest-neighbor matchingUses a memory bank of patch-level features from normal images. Test patches far from any neighbor are anomalous.

๐Ÿ“ 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

bash
pip install -r requirements.txt

2. 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)

bash
python train.py

Extracts patch features from all good images and saves memory_bank.pt.

Step 2 โ€” Evaluate (Compute AUROC)

bash
python evaluate.py

Runs all test images and prints the Image-Level AUROC score.

Step 3 โ€” Inference (Single Image)

bash
python inference.py --image dataset/hazelnut/test/crack/000.png
python inference.py --image dataset/hazelnut/test/crack/000.png --output my_heatmap.png

Outputs:

  • โ€”Anomaly score (printed to console)
  • โ€”Heatmap image saved to output_heatmap.png (or custom path)

๐Ÿ“Š Performance

MetricValue
Image-Level AUROC0.9971 (hazelnut category)
Training time~10โ€“20 min (Google Colab / single GPU)
Inference time< 1 second per image
BackboneImage AUROCNotes
ResNet180.9971Fast, lightweight
DINOv2-ViTS14TBDRun with BACKBONE=dinov2

๐Ÿ–ผ๏ธ 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.

[image] 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.

[image] 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.

[image] 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: layer3 features are scaled by 0.5 before concatenation with layer2, preventing deeper semantic features from dominating fine-grained texture signals.
  • โ€”Batched Distance Computation: torch.cdist is run in configurable batches (default 1024) to prevent OOM on standard GPUs.
  • โ€”Safe Heatmap Normalization: Division-by-zero guarded with 1e-8 epsilon.
  • โ€”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.