Unofficial Implementation of CVPR'23 paper "WinCLIP: Zero-/few-shot anomaly classification and segmentation".
🎯 昇腾 NPU 适配版 — 在 Ascend 910B4 上运行,含 2.67x 性能优化。
pip install -r requirements.txtNPU 用户: 额外安装
torch_npu(参考 昇腾官方文档)CUDA 用户: 确保 torch 版本与 CUDA 版本匹配
依赖版本:
| 包 | 最低版本 | 已验证版本 |
|---|---|---|
| python | >= 3.10.11 | 3.10.11 |
| torch | >= 2.1.0 | 2.10.0 |
| torchvision | >= 0.14.0 | 0.25.0 |
| torch_npu | — | 2.10.0 (Ascend) |
| open-clip-torch | >= 3.3.0 | 3.3.0 |
| scipy | >= 1.10.1 | 1.17.1 |
| scikit-image | >= 0.21.0 | 0.25.2 |
| scikit-learn | >= 1.3.0 | 1.8.0 |
| numpy | >= 1.24.3 | 2.4.6 |
| tqdm | >= 4.64.0 | 4.67.3 |
chmod +x run.sh && ./run.sh脚本会自动:
vit_b_16_plus_240-laion400m_e31-8fb26589.pt)# 基准测试 + 精度验证
python tools/benchmark.py --device npu --obj_type candle --shot 2 --mode benchmark
# 仅精度验证
python tools/benchmark.py --device npu --obj_type candle --shot 2 --mode precision参数说明:
--device: 运行设备 (npu 或 cuda)--obj_type: 类别名称 (如 candle, capsules, pcb1 等)--shot: few-shot 样本数 (0 或 2)--mode: 运行模式
benchmark: 性能基准测试 + 精度评估precision: 仅精度验证 (run-to-run)--data_dir: 数据集根目录 (默认: ./mvtec_datasets/visa)修改 main.py 中的配置:
dataset_root_dir = "/visa_anomaly_detection"
datasetname = "visa"
shot = 0
OBJECT_TYPE = ["candle"]然后运行:
python main.py$VISA_ROOT)python prepare_visa_data.py --visa_root $VISA_ROOT --output_root ./mvtec_datasets/visa转换后的目录结构:
mvtec_datasets/visa/
candle/
train/good/
test/good/
test/anomaly/
ground_truth/anomaly/
capsules/
...| 指标 | 优化前 | 优化后 | 提升 |
|---|---|---|---|
| 平均耗时 | 414.95 ms | 155.20 ms | 2.67x 加速 |
| FPS | 2.41 it/s | 6.44 it/s | +167% |
| AUROC | 0.9495 | 0.9495 | ✅ 精度不变 |
| AUPR | 0.9540 | 0.9540 | ✅ 精度不变 |
| Max F1 | 0.8710 | 0.8710 | ✅ 精度不变 |
.double(): NPU 不支持 fp64,消除无用的 fp32→fp64→fp32 转换链window_mask.T 缓存为预转置 bufferThis implementation is used to produce the WinCLIP results in our CVPR'24 InCTRL paper. If you find the implementation useful, we would appreciate your acknowledgement via citing the InCTRL paper:
@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}
}