Official implementation of paper Beyond Normal References: Discriminative Few-Shot Anomaly Detection.
This paper considers a practical few-shot anomaly detection (FSAD) setting, termed discriminative FSAD, where a limited number of both normal and anomalous examples are available as references during inference. Existing FSAD methods rely on normal-only references through normality matching, ignoring the discriminative clues in anomalous references, while directly fitting both references can overfit to the seen anomalies. We introduce IDEAL, an intrinsic deviation learning framework that leverages both reference types to learn intrinsic deviation patterns characterizing generalizable abnormality as deviations from normality. IDEAL decomposes the learning process into two novel components: 1) a Normal Variation Eraser (NVE) to suppress nuisance normal variations that may lead to noisy deviations from normality, thereby highlighting anomaly-relevant deviation representations; 2) an Intrinsic Deviation Encoder (IDE) to decompose these denoised deviation representations into intrinsic deviation vectors capturing the most discriminative orthogonal deviation directions. At inference, IDEAL scores query-to-normal deviations preserved after projection onto the learned intrinsic deviation vectors, enabling generalization for both seen and unseen anomalies. The framework diagram of the proposed IDEAL is shown below:
- python >= 3.10.11
- torch == 2.4.1
- torchvision == 0.19.1
- scipy == 1.7.3
- scikit-image == 0.19.2
- numpy >= 1.24.3
- tqdm >= 4.64.0
- transformers == 4.31.0
- Industrial Anomaly Detection Datasets: MVTecAD, VisA, AITEX, BTAD, MPDD.
- Medical Anomaly Detection Datasets: BraTS, Liver (from BMAD benchmark), RESC (from BMAD benchmark).
- Please put these anomaly detection datasets in the
./dataset/data/directory.
- Normal Few-Shot Reference Samples: The few-shot reference normal samples are used for normal-based FSAD methods at inference. Please download the few-shot normal reference samples from Google Drive and put these data samples in the
./dataset/data/directory. - Normal and Abnormal Few-Shot Reference Samples: Please download the few-shot normal and abnormal reference samples from Google Drive and put these data samples in the
./dataset/data/directory.
- Dataset Json File: Please run the following code for generating json file for each dataset (taking the MVTecAD dataset as an example):
python3 dataset/gen_mvtec_json.py --data_path ./dataset/data/MVTecAD
- Download Pre-train Weights: Please download the pre-train models from Google Drive.
- Updating the checkpoint path and reference path (e.g., MVTecAD --> VisA):
python3 -u main_infer.py \ --data_root ./dataset/data \ --data_target VisA \ --test_ano_setting general \ --trace_path ./trace_n1a1_mvtec_2_visa.pt \ --ref_root ./dataset/data/fewshot_both_ref \ --gpu_id 0 --n_shot 1 --a_shot 1 \ > ./outputs/infer_VisA_n1a1_mvtec_2_visa.log 2>&1
- The full code will be uploaded soon. Thank you for your attention.
-
If you would like to discuss any details about this work, please feel free to email me (huanwang1018@gmail.com) or open a GitHub issue (email is usually replied faster; sorry for any delay).
-
If this repository or the paper is helpful to your work, please cite:
@article{wang2026beyond, title={Beyond Normal References: Discriminative Few-Shot Anomaly Detection}, author={Wang, Huan and Shen, Jun and Yan, Jun and Pang, Guansong}, journal={arXiv preprint arXiv:2605.23231}, year={2026} }
