Home

Awesome

AdaCLIP (Detecting Anomalies for Novel Categories)

HuggingFace Space

[ECCV 24] AdaCLIP: Adapting CLIP with Hybrid Learnable Prompts for Zero-Shot Anomaly Detection.

by Yunkang Cao, Jiangning Zhang, Luca Frittoli, Yuqi Cheng, Weiming Shen, Giacomo Boracchi

Introduction

Zero-shot anomaly detection (ZSAD) targets the identification of anomalies within images from arbitrary novel categories. This study introduces AdaCLIP for the ZSAD task, leveraging a pre-trained vision-language model (VLM), CLIP. AdaCLIP incorporates learnable prompts into CLIP and optimizes them through training on auxiliary annotated anomaly detection data. Two types of learnable prompts are proposed: \textit{static} and \textit{dynamic}. Static prompts are shared across all images, serving to preliminarily adapt CLIP for ZSAD. In contrast, dynamic prompts are generated for each test image, providing CLIP with dynamic adaptation capabilities. The combination of static and dynamic prompts is referred to as hybrid prompts, and yields enhanced ZSAD performance. Extensive experiments conducted across 14 real-world anomaly detection datasets from industrial and medical domains indicate that AdaCLIP outperforms other ZSAD methods and can generalize better to different categories and even domains. Finally, our analysis highlights the importance of diverse auxiliary data and optimized prompts for enhanced generalization capacity.

Corrections

Overview of AdaCLIP

overview

🛠️ Getting Started

Installation

To set up the AdaCLIP environment, follow one of the methods below:

Dataset Preparation

Please download our processed visual anomaly detection datasets to your DATA_ROOT as needed.

Industrial Visual Anomaly Detection Datasets

Note: some links are still in processing...

DatasetGoogle DriveBaidu DriveTask
MVTec ADGoogle DriveBaidu DriveAnomaly Detection & Localization
VisAGoogle DriveBaidu DriveAnomaly Detection & Localization
MPDDGoogle DriveBaidu DriveAnomaly Detection & Localization
BTADGoogle DriveBaidu DriveAnomaly Detection & Localization
KSDDGoogle DriveBaidu DriveAnomaly Detection & Localization
DAGMGoogle DriveBaidu DriveAnomaly Detection & Localization
DTD-SyntheticGoogle DriveBaidu DriveAnomaly Detection & Localization

Medical Visual Anomaly Detection Datasets

DatasetGoogle DriveBaidu DriveTask
HeadCTGoogle DriveBaidu DriveAnomaly Detection
BrainMRIGoogle DriveBaidu DriveAnomaly Detection
Br35HGoogle DriveBaidu DriveAnomaly Detection
ISICGoogle DriveBaidu DriveAnomaly Localization
ColonDBGoogle DriveBaidu DriveAnomaly Localization
ClinicDBGoogle DriveBaidu DriveAnomaly Localization
TN3KGoogle DriveBaidu DriveAnomaly Localization

Custom Datasets

To use your custom dataset, follow these steps:

  1. Refer to the instructions in ./data_preprocess to generate the JSON file for your dataset.
  2. Use ./dataset/base_dataset.py to construct your own dataset.

Weight Preparation

We offer various pre-trained weights on different auxiliary datasets. Please download the pre-trained weights in ./weights.

Pre-trained DatasetsGoogle DriveBaidu Drive
MVTec AD & ClinicDBGoogle DriveBaidu Drive
VisA & ColonDBGoogle DriveBaidu Drive
All Datasets Mentioned AboveGoogle DriveBaidu Drive

Train

By default, we use MVTec AD & Colondb for training and VisA for validation:

CUDA_VISIBLE_DEVICES=0 python train.py --save_fig True --training_data mvtec colondb --testing_data visa

Alternatively, for evaluation on MVTec AD & Colondb, we use VisA & ClinicDB for training and MVTec AD for validation.

CUDA_VISIBLE_DEVICES=0 python train.py --save_fig True --training_data visa clinicdb --testing_data mvtec

Since we have utilized half-precision (FP16) for training, the training process can occasionally be unstable. It is recommended to run the training process multiple times and choose the best model based on performance on the validation set as the final model.

To construct a robust ZSAD model for demonstration, we also train our AdaCLIP on all AD datasets mentioned above:

CUDA_VISIBLE_DEVICES=0 python train.py --save_fig True \
--training_data \
br35h brain_mri btad clinicdb colondb \
dagm dtd headct isic mpdd mvtec sdd tn3k visa \
--testing_data mvtec

Test

Manually select the best models from the validation set and place them in the weights/ directory. Then, run the following testing script:

sh test.sh

If you want to test on a single image, you can refer to test_single_image.sh:

CUDA_VISIBLE_DEVICES=0 python test.py --testing_model image --ckt_path weights/pretrained_all.pth --save_fig True \
 --image_path asset/img.png --class_name candle --save_name test.png

Main Results

Due to differences in versions utilized, the reported performance may vary slightly compared to the detection performance with the provided pre-trained weights. Some categories may show higher performance while others may show lower.

Table_industrial Table_medical Fig_detection_results

:page_facing_up: Demo App

To run the demo application, use the following command:

python app.py

Or visit our Online Demo for a quick start. The three pre-trained weights mentioned are available there. Feel free to test them with your own data!

Please note that we currently do not have a GPU environment for our Hugging Face Space, so inference for a single image may take approximately 50 seconds.

Demo

💘 Acknowledgements

Our work is largely inspired by the following projects. Thanks for their admiring contribution.

Stargazers over time

Stargazers over time

Citation

If you find this project helpful for your research, please consider citing the following BibTeX entry.


@inproceedings{AdaCLIP,
  title={AdaCLIP: Adapting CLIP with Hybrid Learnable Prompts for Zero-Shot Anomaly Detection},
  author={Cao, Yunkang and Zhang, Jiangning and Frittoli, Luca and Cheng, Yuqi and Shen, Weiming and Boracchi, Giacomo},
  booktitle={European Conference on Computer Vision},
  year={2024}
}