Official PyTorch implementation of "Fine-grained Abnormality Prompt Learning for Zero-shot Anomaly Detection".
Current zero-shot anomaly detection (ZSAD) methods show remarkable success in prompting large pre-trained vision-language models to detect anomalies in a target dataset without using any dataset-specific training or demonstration. However, these methods often focus on crafting/learning prompts that capture only coarse-grained semantics of abnormality, e.g., high-level semantics like damaged, imperfect, or defective objects. They therefore have limited capability in recognizing diverse abnormality details that deviate from these general abnormal patterns in various ways. To address this limitation, we propose \textbf{\coolname}, a novel framework designed to learn Fine-grained Abnormality Prompts for accurate ZSAD. To this end, a novel Compound Abnormality Prompt learning (CAP) module is introduced in FAPrompt to learn a set of complementary, decomposed abnormality prompts, where abnormality prompts are enforced to model diverse abnormal patterns derived from the same normality semantic. On the other hand, the fine-grained abnormality patterns can be different from one dataset to another. To enhance the cross-dataset generalization, another novel module, namely Data-dependent Abnormality Prior learning (DAP), is introduced in FAPrompt to learn a sample-wise abnormality prior from abnormal features of each test image to dynamically adapt the abnormality prompts to individual test images. Comprehensive experiments on 19 real-world datasets, covering both industrial defects and medical anomalies, demonstrate that FAPrompt substantially outperforms state-of-the-art methods by at least 3%-5% in both image- and pixel-level ZSAD tasks.
- tqdm == 4.67.1
- timm == 0.6.12
- scikit-image == 0.19.2
- scikit-learn == 1.0.2
- scipy == 1.7.3
- seaborn == 0.11.2
- torch == 2.4.1
- torchvision == 0.19.1
- transformers == 4.31.0
- Single NVIDIA GeForce RTX 3090
-
Industrial Anomaly Detection Datasets: MVTec AD, VisA, ELPV, SDD, AITEX, BTAD, DAGM, DTD-Synthetic, MPDD.
-
Medical Anomaly Detection Datasets: BrainMRI, HeadCT, LAG, Br35H, CVC-ColonDB, CVC-ClinicDB, Kvasir, Endo, ISIC, TN3K.
Step 2. Generate the JSON file for Datasets (same as AnomalyCLIP)
Step 3. Download the Pre-train Models on Google Drive.
- Update the
checkpoint_pathto the path of pre-train model, setdatasetto the name of the test dataset, and specifydata_pathas the path to the test dataset. Then, run
bash test.sh- Train your own weights by runing
bash train.sh- If you find the implementation useful, we would appreciate your acknowledgement via citing our FAPrompt paper:
@inproceedings{zhu2024fine,
title={Fine-grained Abnormality Prompt Learning for Zero-shot Anomaly Detection},
author={Zhu, Jiawen and Ong, Yew-Soon and Shen, Chunhua and Pang, Guansong},
booktitle={Proceedings of the IEEE/CVF International Conference on Computer Vision},
year={2025}
}