Unsupervised anomaly detection for factory parts, built around the MVTec AD dataset you already have downloaded. The idea: train a model only on images of good parts, then use it to flag anything that looks different at test time.
The first baseline implemented is a compact PatchCore-style model:
- A pretrained CNN (ResNet-18 by default) embeds every image into patch-level features.
- Training stores all patch features from the good images in a memory bank (optionally subsampled to a smaller "coreset").
- At inference, every test patch is scored by its k-nearest-neighbour distance to the memory bank. Far-from-normal patches are anomalous.
- Patch scores are upsampled to full image resolution and written as TIFF anomaly maps, ready for the official MVTec evaluation scripts.
python3 -m venv .venv
.venv/bin/pip install -r requirements.txt
.venv/bin/pip install -e . --no-depsThe pretrained backbone weights are downloaded automatically on first use.
Everything runs on CPU out of the box; pass --device cuda when a GPU is
available.
Train, predict, and evaluate a single category end to end:
.venv/bin/factory-defect run --category metal_nutOr run the steps separately:
# 1. Build the memory bank from good training images
.venv/bin/factory-defect train --category metal_nut
# 2. Score every test image and write anomaly maps
.venv/bin/factory-defect predict --category metal_nut
# 3. Compute official metrics (image AUROC + pixel-level AU-PRO)
.venv/bin/factory-defect evaluate --category metal_nutUse --category all to process all 15 categories. Useful options:
--backbone wide_resnet50_2— stronger (and slower) feature extractor--coreset-ratio 0.1— memory-bank size (default 10%)--k-neighbors 9— k for the kNN patch scoring--max-train-images 50— limit training images for quick experiments
Inspect the results visually:
.venv/bin/python scripts/visualize.py --category metal_nutThis writes side-by-side panels (input / ground truth / anomaly overlay) to
outputs/visualizations/.
Generate the full presentation figure set from the evaluation results:
.venv/bin/python scripts/plot_results.pyFigures are written to outputs/figures/:
| File | Shows |
|---|---|
01_metrics_bar.png |
Grouped AUROC / AU-PRO bars per category |
02_auroc_vs_aupro.png |
Detection vs. localization scatter |
03_roc_curves.png |
All image-level ROC curves + mean |
04_pro_curves.png |
All pixel-level PRO curves + mean |
05_metrics_heatmap.png |
Annotated results matrix |
06_examples_grid.png |
Input / ground truth / prediction panels |
07_hardest_defects.png |
15 hardest defect types by AUROC |
08_model_comparison.png |
ResNet-18 → WRN-50-2 (random → greedy coreset) |
Pixel-level curves are computed once with the official algorithm and cached
compactly under outputs/metrics/<category>/curves.npz, so re-running the
script is fast.
A complete walkthrough of the project is in
Factory_Defect_Detection.ipynb: environment
and GPU check, dataset overview with example images, the PatchCore method,
model inspection, reproduction commands, the full results table, every figure,
model progression, per-defect-type analysis, and next steps.
Launch it from the project root:
.venv/bin/jupyter notebook Factory_Defect_Detection.ipynbMost cells read the artifacts already in outputs/, so they run quickly. The
only slow cell is the optional full-benchmark reproduction, which is guarded by
RUN_FULL_BENCHMARK = False.
Everything is written under outputs/:
| Path | Contents |
|---|---|
outputs/checkpoints/<category>.pt |
Memory bank + model config |
outputs/anomaly_maps/<category>/test/<defect>/<id>.tiff |
Full-resolution anomaly maps |
outputs/predictions/<category>_scores.csv |
Per-image scores and labels |
outputs/metrics/<category>/metrics.json |
Official evaluation results |
outputs/metrics/summary.json |
Aggregated table across categories |
Best configuration so far: Wide-ResNet-50-2, 10% greedy coreset, k = 9, RTX 5070 Ti. Reproduce with:
.venv/bin/factory-defect run --category all --device cuda \
--backbone wide_resnet50_2 --coreset-method greedy| category | image AUROC | AU-PRO |
|---|---|---|
| bottle | 0.998 | 0.911 |
| cable | 0.928 | 0.875 |
| capsule | 0.924 | 0.929 |
| carpet | 0.991 | 0.940 |
| grid | 0.843 | 0.806 |
| hazelnut | 0.992 | 0.952 |
| leather | 0.998 | 0.968 |
| metal_nut | 0.971 | 0.822 |
| pill | 0.823 | 0.914 |
| screw | 0.849 | 0.924 |
| tile | 0.917 | 0.800 |
| toothbrush | 0.983 | 0.878 |
| transistor | 0.880 | 0.849 |
| wood | 0.985 | 0.838 |
| zipper | 0.920 | 0.905 |
| mean | 0.933 | 0.887 |
Progression across configurations:
| configuration | mean AUROC | mean AU-PRO |
|---|---|---|
| ResNet-18, random coreset | 0.835 | 0.801 |
| WRN-50-2, random coreset | 0.905 | 0.863 |
| WRN-50-2, greedy coreset | 0.933 | 0.887 |
The greedy coreset run is slow to train (roughly 15–20 minutes on GPU) because the farthest-point selection is sequential; the remaining categories can still be improved with a larger coreset ratio or higher-resolution input.
Note: a full run writes about 5 GB of anomaly maps plus checkpoints under
outputs/ — that directory is git-ignored.
src/factory_defect/
cli.py # train / predict / evaluate / run commands
data.py # MVTec dataset loaders and test metadata
patchcore.py # feature extraction, memory bank, kNN scoring
coreset.py # random and farthest-point coreset selection
constants.py # categories and backbone defaults
scripts/visualize.py # anomaly-map visualization helper
mvtec_ad_evaluation/ # official MVTec evaluation scripts
mvtec_anomaly_detection/ # dataset
- Swap in
wide_resnet50_2and compare metrics across all categories. - Add PaDiM or FastFlow-style baselines behind the same CLI.
- Use the greedy coreset when you have a GPU (it is more faithful to PatchCore but slow on CPU).
- Add image-level threshold selection on a validation split for deployment.
- Note: MVTec AD is licensed for non-commercial research; check the license in
mvtec_anomaly_detection/<category>/license.txtbefore any production use.