需先把数据集转换为 MVTec AD 目录格式并生成训练/测试 JSON 文件;需从 Google Drive 额外下载少样本正常样本和预训练模型;运行前要改 config 中 TEST.CHECKPOINT_FILE_PATH 指向预训练模型路径
InCTRL
mala-labApache-2.0活跃维护CVPR'24 论文官方实现,用上下文残差学习加少量样本提示,让一个通用模型跨类别做异常检测,免去每类单独训练。
Official implementation of CVPR'24 paper 'Toward Generalist Anomaly Detection via In-context Residual Learning with Few-shot Sample Prompts'.
python test.py --val_normal_json_path $normal-json-files-for-testing --val_outlier_json_path $abnormal-json-files-for-testing --category $dataset-class-name --few_shot_dir $path-to-few-shot-samplesREADME 说明使用单张 NVIDIA GeForce RTX 3090;未说明 CPU 运行或其他 GPU 型号。
CVPR'24 通用异常检测官方实现,用少量正常样本做提示即可跨数据集检测异常
CVPR'24 论文官方实现,用上下文残差学习加少量样本提示,让一个通用模型跨类别做异常检测,免去每类单独训练。
核心能力
- 用 in-context 残差学习比较查询图像与少样本正常提示
- 在辅助数据集上训练后可跨域泛化,无需再训练
- 支持 one-vs-all 与多类设置下的工业、医学、语义异常检测
- 在九个异常检测数据集上建立 GAD 基准并优于对比方法
- 提供预训练模型与少样本正常样本下载
容易踩的坑
暂无 release 数据(可能没有发版,或尚未同步)。
用户点评
登录用户的真实使用记录,可按版本和硬件筛选还没有人点评。第一个说出真实体验的人最有价值。
InCTRL (CVPR 2024)
Official PyTorch implementation of "Toward Generalist Anomaly Detection via In-context Residual Learning with Few-shot Sample Prompts".
Overview
In this work, we propose to train a Generalist Anomaly Detection (GAD) model with few-shot normal images as sample prompts for AD on diverse datasets on the fly. To this end, we introduce a novel approach that learns an incontext residual learning model for GAD, termed InCTRL. It is trained on an auxiliary dataset to discriminate anomalies from normal samples based on a holistic evaluation of the residuals between query images and few-shot normal sample prompts. Regardless of the datasets, per definition of anomaly, larger residuals are expected for anomalies than normal samples, thereby enabling InCTRL to generalize across different domains without further training. Comprehensive experiments on nine AD datasets are performed to establish a GAD benchmark that encapsulate the detection of industrial defect anomalies, medical anomalies, and semantic anomalies in both one-vs-all and multi-class setting, on which InCTRL is the best performer and significantly outperforms state-of-the-art competing methods.

Setup
- python >= 3.10.11
- torch >= 1.13.0
- torchvision >= 0.14.0
- scipy >= 1.10.1
- scikit-image >= 0.21.0
- numpy >= 1.24.3
- tqdm >= 4.64.0
Device
Single NVIDIA GeForce RTX 3090
Run
Step 1. Download the Anomaly Detection Dataset(ELPV, SDD, AITEX, VisA, MVTec AD, BrainMRI, HeadCT, MNIST, CIFAR-10) and Convert it to MVTec AD Format(the convert script).
The dataset folder structure should look like:
DATA_PATH/
subset_1/
train/
good/
test/
good/
defect_class_1/
defect_class_2/
defect_class_3/
...
...
Step 2. Generate Training/Test Json Files of Anomaly Detection Datasets(the generate script).
The json folder structure should look like:
JSON_PATH/
dataset_1/
subset_1/
subset_1_train_normal.json
subset_1_train_outlier.json
subset_1_val_normal.json
subset_1_val_outlier.json
subset_2/
subset_3/
...
...
Step 3. Download the Few-shot Normal Samples for Inference on Google Drive
Step 4. Download the Pre-train Models on Google Drive
Step 5. Quick Start
Change the TEST.CHECKPOINT_FILE_PATH in config to the path of pre-train model and run
python test.py --val_normal_json_path $normal-json-files-for-testing --val_outlier_json_path $abnormal-json-files-for-testing --category $dataset-class-name --few_shot_dir $path-to-few-shot-samples
For example, if run on the category candle of visa with k=2:
python test.py --val_normal_json_path /AD_json/visa/candle_val_normal.json --val_outlier_json_path /AD_json/visa/candle_val_outlier.json --category candle --few_shot_dir /fs_samples/visa/2/
Training
python main.py --normal_json_path $normal-json-files-for-training --outlier_json_path $abnormal-json-files-for-training --val_normal_json_path $normal-json-files-for-testing --val_outlier_json_path $abnormal-json-files-for-testing
Implementation of WinCLIP
WinCLIP is one main competing method to ours, but its official implentation is not publicly available. We have successfully reproduced the results of WinCLIP based on our extensive communications with its authors and used our implementation to perform experiments in the paper. Our implementation has been released at WinCLIP.
Citation
@inproceedings{zhu2024toward,
title={Toward generalist anomaly detection via in-context residual learning with few-shot sample prompts},
author={Zhu, Jiawen and Pang, Guansong},
booktitle={Proceedings of the IEEE/CVF conference on computer vision and pattern recognition},
pages={17826--17836},
year={2024}
}