Official PyTorch Implementation of Dual-Masked and Discriminative Reconstruction for Unified Vision Anomaly Detection, IEEE TIP.
- Support training and evaluation for large-scale Real-IAD-Variety
- Add the results of MVTec, VisA, BTAD, Medical and Real-IAD-Variety
D2Rec is a simple, effective, general and robust unified (multi-class) vision anomaly detection framework that integrates unsupervised dual-masked reconstruction and a self-supervised discriminator, achieving competitive performance on both industrial and medical anomaly detection benchmarks.
Evaluation with 224x224 input resolution and the last checkpoint (the 50-th epoch).
| datasets | #classes | #test images | I-AUROC | P-AUROC | I-AUPR | P-AUPR |
|---|---|---|---|---|---|---|
| MVTec | 15 | 1725 | 98.9 | 99.6 | 98.9 | 74.3 |
| VisA | 12 | 2162 | 95.4 | 96.3 | 99.0 | 48.5 |
| BTAD | 3 | 741 | 96.2 | 96.6 | 97.6 | 61.3 |
| Medical | 3 | 7013 | 88.6 | 88.5 | 98.0 | 60.6 |
| Real-IAD-Variety | 160 | 178995 | 88.1 | 97.7 | 93.8 | 46.5 |
Evaluation with 448x448 input resolution and the last checkpoint (the 50-th epoch).
| datasets | #classes | #test images | I-AUROC | P-AUROC | I-AUPR | P-AUPR |
|---|---|---|---|---|---|---|
| MVTec | 15 | 1725 | 99.3 | 99.7 | 98.6 | 77.6 |
| VisA | 12 | 2162 | 97.2 | 97.9 | 98.6 | 53.6 |
| BTAD | 3 | 741 | 95.5 | 97.3 | 97.5 | 65.5 |
| Medical | 3 | 7013 | 89.0 | 88.9 | 97.3 | 62.0 |
| Real-IAD-Variety | 160 | 178995 | 84.5 | 97.0 | 92.9 | 45.5 |
Please see more detailed results in the results folder.
Create a new conda environment and install required packages.
conda create -n d2rec python=3.8.12
conda activate d2rec
pip install -r requirements.txt
Download MVTec, VisA, BTAD, Medical and Real-IAD-Variety datasets from the official websites and unzip them to ./data/.
You can freely use the provided 'meta.json' files in './data'. You can also use the scripts in ./gen_meta_json/ to generate meta.json for each dataset with the following command:
python3 ./gen_meta_json/mvtec.py
python3 ./gen_meta_json/visa.py
python3 ./gen_meta_json/btad.py
python3 ./gen_meta_json/medical.py
python3 ./gen_meta_json/real-iad-variety.py
using unified (i.e., multi-class) vision anomaly setting
image_size=224
for dataset in mvtec visa btad medical Real-IAD-Variety
do
CUDA_VISIBLE_DEVICES=0 python3 main.py \
--data_path "./datasets/"$dataset \
--dataset $dataset \
--image_size ${image_size} \
--batch_size 16 \
--dual_mask \
--mask_head
done
image_size=224
for dataset in mvtec visa btad medical Real-IAD-Variety
do
CUDA_VISIBLE_DEVICES=0 python3 main.py \
-e \
--data_path "./datasets/"$dataset \
--dataset $dataset \
--save_path "./checkpoints/" \
--image_size ${image_size} \
--batch_size 16 \
--dual_mask \
--mask_head
done
If you find this code useful in your research, please consider citing us:
@article{gao2026d2rec,
title = {Dual-Masked and Discriminative Reconstruction for Unified Vision Anomaly Detection},
author = {Gao, Bin-Bin},
booktitle = {IEEE Transactions on Image Processing},
pages = {4701-4712},
year = {2026}
}
