Simon Damm, Mike Laszkiewicz, Johannes Lederer, Asja Fischer
This is the official code to reproduce the experiments in the paper AnomalyDINO: Boosting Patch-based Few-shot Anomaly Detection with DINOv2.
-
Create a virtual environment (e.g.,
python -m venv .venvAnomalyDINO
), activate it (e.g.,source .venvAnomalyDINO/bin/activate
) and install the required dependencies for AnomalyDINO:pip install -r requirements.txt
Info: If you want to use
faiss
with GPU-acceleration we recommend setting up a conda environment with the required packages instead (only conda installation is supported, see, e.g., here). To perform similarity search on CPU set the additional flag--faiss_on_cpu
. -
Download and prepare the datasets MVTec-AD and VisA from their official sources. For VisA, follow the instruction in the official repo to organize the data in the official 1-class splits. The default data roots are
data/mvtec_anomaly_detection
for MVTec-AD, anddata/VisA_pytorch/1cls/
for VisA. Please adapt the function calls below if necessary. Alternatively, prepare your own dataset accordingly:your_data_root ├── object1 │ ├── ground_truth # anomaly annotations per anomaly type │ │ ├── anomaly_type1 │ │ ├── ... │ ├── test # test images per anomaly type & 'good' │ │ ├── anomaly_type1 │ │ ├── ... │ │ └── good │ └── train # train/reference images (without anomalies) │ └── good ├── object2 │ ├── ...
Get started with the minimal demo to perform few-shot anomaly detection (demo_AD_DINO.ipynb
).
For the full evaluation, run the script run_anomalydino.py
on the selected dataset for a given number of shots and repetitions (seeds).
The preprocessing to your dataset can be specified in src/utils.py
in get_dataset_info
, default is "agnostic" (apply masking whenever PCA-based masking works well & augment reference samples by rotations, see the paper).
The results for the default setting, i.e., all considered shots, three repetitions, and agnostic preprocessing, can be reproduced by calling:
python run_anomalydino.py --dataset MVTec --shots 1 2 4 8 16 --num_seeds 3 --preprocess agnostic --data_root data/mvtec_anomaly_detection
python run_anomalydino.py --dataset VisA --shots 1 2 4 8 16 --num_seeds 3 --preprocess agnostic --data_root data/VisA_pytorch/1cls/
For a faster inspection use, e.g.,
python run_anomalydino.py --dataset MVTec --shots 1 --num_seeds 1 --preprocess informed --data_root data/mvtec_anomaly_detection
The script automatically creates some example plots, plots some anomaly maps for each object, and automatically evaluates each run (activate evaluation of segementation with --eval_segm
if applicable).
Evaluation results are saved in the respective results directory as metrics_seed={seed}.json
for each seed.
To reproduce the results in the batched zero-shot scenario, run run_anomalydino_batched.py
with appropriate arguments:
python run_anomalydino_batched.py --dataset MVTec --data_root data/mvtec_anomaly_detection
python run_anomalydino_batched.py --dataset VisA --data_root data/VisA_pytorch/1cls/
This work uses the following ressources and datasets:
- DINOv2, code and model available under Apache 2.0 license.
- The MVTec-AD dataset, available under the CC BY-NC-SA 4.0 license.
- The VisA dataset, available under the CC BY 4.0 license.
If you find this repository useful in your research/project, please consider citing the paper:
@misc{damm2024anomalydino,
title={AnomalyDINO: Boosting Patch-based Few-shot Anomaly Detection with DINOv2},
author={Simon Damm and Mike Laszkiewicz and Johannes Lederer and Asja Fischer},
year={2024},
eprint={2405.14529},
url={https://arxiv.org/abs/2405.14529},
}