weixin_72661020/winclip-vit-l14-336px-npu
模型介绍文件和版本Pull Requests讨论分析
下载使用量0

WinCLIP ViT-L/14-336px NPU

基于 CLIP ViT-L/14-336px 的零样本异常检测模型,适配华为昇腾 NPU (Ascend 910B)。

模型信息

项目说明
模型WinCLIP (ViT-L/14-336px)
任务零样本异常检测 (Zero-shot Anomaly Detection)
数据集VisA (Visual Anomaly detection)
目标硬件Ascend 910B NPU
CANN 版本8.5.1
框架PyTorch 2.9.0 + torch_npu

快速部署

1. 环境准备

# 安装依赖
pip install -r requirements.txt

# 下载预训练权重 (从 GitHub 镜像)
~/.local/bin/aria2c --console-log-level=warn -x 16 -s 16 \
  -o ViT-L-14-336px.pt \
  "https://ghfast.top/https://openaipublic.azureedge.net/clip/models/3035c92b350959924f9f00213499208652fc7ea050643e8b385c2dac08641f02/ViT-L-14-336px.pt"

2. 数据集准备

# 下载 VisA 数据集
~/.local/bin/atomgit download weixin_72661020/VisA_20220922 -d ./models

# 解压
cd models && tar xf VisA_20220922.tar && cd ..

# 转换为 MVTec 格式
python3 convert_visa2mvtec.py --visa_root ./models --output_root ./data

3. 运行推理

# 一键运行
bash run_npu.sh

# 或手动运行
python3 inference.py

性能测试

端到端时间 (12个类别)

模型时间 (s)提升
ViT-B/16-plus-240 (基线)195.00-
ViT-L/14-336px (优化后)57.97-70.3%

精度对比

类别AUROCAUPRF1-Max
candle93.11%93.69%87.80%
capsules75.22%84.39%78.07%
cashew91.64%96.11%89.11%
chewinggum97.52%98.97%95.92%
fryum90.96%95.85%87.68%
macaroni175.43%76.69%72.20%
macaroni268.19%63.93%70.68%
pcb156.50%59.60%66.67%
pcb264.27%68.22%66.67%
pcb355.37%58.60%67.12%
pcb483.18%85.90%75.89%
pipe_fryum94.65%97.40%90.91%
平均79.31%81.61%79.06%

性能基准

指标结果
单图延迟7.56 ms
单图吞吐133 img/s
批量=4395 img/s
批量=8523 img/s
批量=16631 img/s
峰值吞吐 (bs=128)691 img/s

优化技术

  1. 混合精度推理: FP16 用于所有参数,LayerNorm 保持 FP32
  2. 文本特征缓存: 每个类别只计算一次文本特征
  3. 批量文本编码: 一次编码所有文本模板
  4. 优化文本模板: 从 22 个减少到 11 个(最优平衡点)
  5. NPU 环境优化: TASK_QUEUE_ENABLE, CPU_AFFINITY_CONF, 内存分配优化
  6. 异步数据传输: non_blocking=True, pin_memory=True

文件结构

.
├── inference.py              # 主推理脚本
├── README.md                 # 本文件
├── requirements.txt          # 依赖列表
├── run_npu.sh               # 一键启动脚本
├── benchmark_accuracy.py     # 精度评测
├── benchmark_throughput.py   # 吞吐量评测
├── benchmark_latency.py      # 延迟评测
├── ViT-L-14-336.json        # 模型配置
├── logs/                     # 运行日志
│   ├── inference_log.txt
│   └── benchmark_log.txt
└── data/                     # 数据集 (需自行准备)

参考

  • WinCLIP (CVPR 2023)
  • OpenCLIP
  • CANN 文档