|
1 |
| -# AdaCLIP |
2 |
| -[ECCV2024] The Official Implementation for ''AdaCLIP: Adapting CLIP with Hybrid Learnable Prompts for Zero-Shot Anomaly Detection'' |
| 1 | +# AdaCLIP (Detecting Anomalies for Novel Categories) |
| 2 | +[]() |
3 | 3 |
|
4 |
| -Code will be released at the end of July 2024. |
| 4 | +> [**ECCV 24**] [**AdaCLIP: Adapting CLIP with Hybrid Learnable Prompts for Zero-Shot Anomaly Detection**](). |
| 5 | +> |
| 6 | +> by [Yunkang Cao](https://caoyunkang.github.io/), [Jiangning Zhang](https://zhangzjn.github.io/), [Luca Frittoli](https://scholar.google.com/citations?user=cdML_XUAAAAJ), |
| 7 | +> [Yuqi Cheng](https://scholar.google.com/citations?user=02BC-WgAAAAJ&hl=en), [Weiming Shen](https://scholar.google.com/citations?user=FuSHsx4AAAAJ&hl=en), [Giacomo Boracchi](https://boracchi.faculty.polimi.it/) |
| 8 | +> |
| 9 | +
|
| 10 | +## Introduction |
| 11 | +Zero-shot anomaly detection (ZSAD) targets the identification of anomalies within images from arbitrary novel categories. |
| 12 | +This study introduces AdaCLIP for the ZSAD task, leveraging a pre-trained vision-language model (VLM), CLIP. |
| 13 | +AdaCLIP incorporates learnable prompts into CLIP and optimizes them through training on auxiliary annotated anomaly detection data. |
| 14 | +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. |
| 15 | +In contrast, dynamic prompts are generated for each test image, providing CLIP with dynamic adaptation capabilities. |
| 16 | +The combination of static and dynamic prompts is referred to as hybrid prompts, and yields enhanced ZSAD performance. |
| 17 | +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. |
| 18 | +Finally, our analysis highlights the importance of diverse auxiliary data and optimized prompts for enhanced generalization capacity. |
| 19 | + |
| 20 | +## Overview of AdaCLIP |
| 21 | + |
| 22 | + |
| 23 | +## 🛠️ Getting Started |
| 24 | + |
| 25 | +### Installation |
| 26 | +To set up the AdaCLIP environment, follow one of the methods below: |
| 27 | + |
| 28 | +- Clone this repo: |
| 29 | + ```shell |
| 30 | + git clone https://github.com/caoyunkang/AdaCLIP.git && cd AdaCLIP |
| 31 | + ``` |
| 32 | +- You can use our provided installation script for an automated setup:: |
| 33 | + ```shell |
| 34 | + sh install.sh |
| 35 | + ``` |
| 36 | +- If you prefer to construct the experimental environment manually, follow these steps: |
| 37 | + ```shell |
| 38 | + conda create -n AdaCLIP python=3.9.5 -y |
| 39 | + conda activate AdaCLIP |
| 40 | + pip install torch==1.10.1+cu111 torchvision==0.11.2+cu111 torchaudio==0.10.1 -f https://download.pytorch.org/whl/cu111/torch_stable.html |
| 41 | + pip install tqdm tensorboard setuptools==58.0.4 opencv-python scikit-image scikit-learn matplotlib seaborn ftfy regex numpy==1.26.4 |
| 42 | + pip install gradio # Optional, for app |
| 43 | + ``` |
| 44 | +- Remember to update the dataset root in config.py according to your preference: |
| 45 | + ```python |
| 46 | + DATA_ROOT = '../datasets' # Original setting |
| 47 | + ``` |
| 48 | + |
| 49 | +### Dataset Preparation |
| 50 | +Please download our processed visual anomaly detection datasets to your `DATA_ROOT` as needed. |
| 51 | + |
| 52 | +#### Industrial Visual Anomaly Detection Datasets |
| 53 | +Note: some links are still in processing... |
| 54 | + |
| 55 | +| Dataset | Google Drive | Baidu Drive | Task |
| 56 | +|------------|------------------|------------------| ------------------| |
| 57 | +| MVTec AD | [Google Drive](链接) | [Baidu Drive](链接) | Anomaly Detection & Localization | |
| 58 | +| VisA | [Google Drive](链接) | [Baidu Drive](链接) | Anomaly Detection & Localization | |
| 59 | +| MPDD | [Google Drive](链接) | [Baidu Drive](链接) | Anomaly Detection & Localization | |
| 60 | +| BTAD | [Google Drive](链接) | [Baidu Drive](链接) | Anomaly Detection & Localization | |
| 61 | +| KSDD | [Google Drive](链接) | [Baidu Drive](链接) | Anomaly Detection & Localization | |
| 62 | +| DAGM | [Google Drive](链接) | [Baidu Drive](链接) | Anomaly Detection & Localization | |
| 63 | +| DTD-Synthetic | [Google Drive](链接) | [Baidu Drive](链接) | Anomaly Detection & Localization | |
| 64 | + |
| 65 | + |
| 66 | + |
| 67 | + |
| 68 | +#### Medical Visual Anomaly Detection Datasets |
| 69 | +| Dataset | Google Drive | Baidu Drive | Task |
| 70 | +|------------|------------------|------------------| ------------------| |
| 71 | +| HeadCT | [Google Drive](链接) | [Baidu Drive](链接) | Anomaly Detection | |
| 72 | +| BrainMRI | [Google Drive](链接) | [Baidu Drive](链接) | Anomaly Detection | |
| 73 | +| Br35H | [Google Drive](链接) | [Baidu Drive](链接) | Anomaly Detection | |
| 74 | +| ISIC | [Google Drive](链接) | [Baidu Drive](链接) | Anomaly Localization | |
| 75 | +| ColonDB | [Google Drive](链接) | [Baidu Drive](链接) | Anomaly Localization | |
| 76 | +| ClinicDB | [Google Drive](链接) | [Baidu Drive](链接) | Anomaly Localization | |
| 77 | +| TN3K | [Google Drive](链接) | [Baidu Drive](链接) | Anomaly Localization | |
| 78 | + |
| 79 | +#### Custom Datasets |
| 80 | +To use your custom dataset, follow these steps: |
| 81 | + |
| 82 | +1. Refer to the instructions in `./data_preprocess` to generate the JSON file for your dataset. |
| 83 | +2. Use `./dataset/base_dataset.py` to construct your own dataset. |
| 84 | + |
| 85 | + |
| 86 | +### Weight Preparation |
| 87 | + |
| 88 | +We offer various pre-trained weights on different auxiliary datasets. |
| 89 | +Please download the pre-trained weights in `./weights`. |
| 90 | + |
| 91 | +| Pre-trained Datasets | Google Drive | Baidu Drive |
| 92 | +|------------|------------------|------------------| |
| 93 | +| MVTec AD & ClinicDB | [Google Drive](https://drive.google.com/file/d/1xVXANHGuJBRx59rqPRir7iqbkYzq45W0/view?usp=drive_link) | [Baidu Drive](链接) | |
| 94 | +| VisA & ColonDB | [Google Drive](https://drive.google.com/file/d/1QGmPB0ByPZQ7FucvGODMSz7r5Ke5wx9W/view?usp=drive_link) | [Baidu Drive](链接) | |
| 95 | +| All Datasets Mentioned Above | [Google Drive](https://drive.google.com/file/d/1Cgkfx3GAaSYnXPLolx-P7pFqYV0IVzZF/view?usp=drive_link) | [Baidu Drive](链接) | |
| 96 | + |
| 97 | + |
| 98 | +### Train |
| 99 | + |
| 100 | +By default, we use MVTec AD & ClinicDB for training and VisA for validation: |
| 101 | +```shell |
| 102 | +CUDA_VISIBLE_DEVICES=0 python train.py --save_fig True --training_data mvtec colondb --testing_data visa |
| 103 | +``` |
| 104 | + |
| 105 | + |
| 106 | +Alternatively, for evaluation on MVTec AD & ClinicDB, we use VisA & ColonDB for training and MVTec AD for validation. |
| 107 | +```shell |
| 108 | +CUDA_VISIBLE_DEVICES=0 python train.py --save_fig True --training_data visa clinicdb --testing_data mvtec |
| 109 | +``` |
| 110 | +Since we have utilized half-precision (FP16) for training, the training process can occasionally be unstable. |
| 111 | +It is recommended to run the training process multiple times and choose the best model based on performance |
| 112 | +on the validation set as the final model. |
| 113 | + |
| 114 | + |
| 115 | +To construct a robust ZSAD model for demonstration, we also train our AdaCLIP on all AD datasets mentioned above: |
| 116 | +```shell |
| 117 | +CUDA_VISIBLE_DEVICES=0 python train.py --save_fig True \ |
| 118 | +--training_data \ |
| 119 | +br35h brain_mri btad clinicdb colondb \ |
| 120 | +dagm dtd headct isic mpdd mvtec sdd tn3k visa \ |
| 121 | +--testing_data mvtec |
| 122 | +``` |
| 123 | + |
| 124 | +### Test |
| 125 | + |
| 126 | +Manually select the best models from the validation set and place them in the `weights/` directory. Then, run the following testing script: |
| 127 | +```shell |
| 128 | +sh test.sh |
| 129 | +``` |
| 130 | + |
| 131 | +If you want to test on a single image, you can refer to `test_single_image.sh`: |
| 132 | +```shell |
| 133 | +CUDA_VISIBLE_DEVICES=0 python test.py --testing_model image --ckt_path weights/pretrained_all.pth --save_fig True \ |
| 134 | + --image_path asset/img.png --class_name candle --save_name test.png |
| 135 | +``` |
| 136 | + |
| 137 | +## Main Results |
| 138 | + |
| 139 | +Due to differences in versions utilized, the reported performance may vary slightly compared to the detection performance |
| 140 | +with the provided pre-trained weights. Some categories may show higher performance while others may show lower. |
| 141 | + |
| 142 | + |
| 143 | + |
| 144 | + |
| 145 | + |
| 146 | +### :page_facing_up: Demo App |
| 147 | + |
| 148 | +To run the demo application, use the following command: |
| 149 | + |
| 150 | +```bash |
| 151 | +python app.py |
| 152 | +``` |
| 153 | + |
| 154 | + |
| 155 | + |
| 156 | +## 💘 Acknowledgements |
| 157 | +Our work is largely inspired by the following projects. Thanks for their admiring contribution. |
| 158 | + |
| 159 | +- [VAND-APRIL-GAN](https://github.com/ByChelsea/VAND-APRIL-GAN) |
| 160 | +- [AnomalyCLIP](https://github.com/zqhang/AnomalyCLIP) |
| 161 | +- [SAA](https://github.com/caoyunkang/Segment-Any-Anomaly) |
| 162 | + |
| 163 | + |
| 164 | +## Stargazers over time |
| 165 | +[](https://starchart.cc/caoyunkang/AdaCLIP) |
| 166 | + |
| 167 | + |
| 168 | +## Citation |
| 169 | + |
| 170 | +If you find this project helpful for your research, please consider citing the following BibTeX entry. |
| 171 | + |
| 172 | +```BibTex |
| 173 | +
|
| 174 | +
|
| 175 | +
|
| 176 | +``` |
0 commit comments