g
gyccc/timm-swin_base_patch4_window7_224.ms_in1k-NPU
模型介绍文件和版本Pull Requests讨论分析
下载使用量0

timm/swin_base_patch4_window7_224.ms_in1k on Ascend NPU

1. 简介

本项目将 ModelScope 图片分类模型 timm/swin_base_patch4_window7_224.ms_in1k 适配为可在单卡昇腾 NPU(Ascend910B)上运行的提交工程。

  • 模型类型:Swin Transformer Base(图像分类)
  • 输入尺寸:224 x 224
  • 输出类别数:1000
  • 权重来源:ModelScope snapshot_download(本地加载)
  • 创建方式:timm.create_model(..., pretrained=False) + 本地 model.safetensors 权重

注意:该模型未提供 id2label 映射文件,推理结果使用 class_0、class_1、...、class_999 作为标签占位。

2. 验证环境

组件版本/型号
NPUAscend910B4
CANN8.5.1
PyTorch2.x
torch_npu适配 CANN 8.5.1
timmlatest

环境检查日志见 logs/env_check.log。

3. 推理运行

pip install -r requirements.txt
python inference.py

推理脚本加载 ModelScope 本地缓存的 model.safetensors,在 npu:0 上执行真实推理,并将结果写入 logs/inference.log。

示例输出:

Output shape: torch.Size([1, 1000])
Top-5 predictions (NPU):
  1. class_818 (index=818, prob=0.146275)
  2. class_704 (index=704, prob=0.146007)
  3. class_862 (index=862, prob=0.145107)
  4. class_920 (index=920, prob=0.061193)
  5. class_470 (index=470, prob=0.043189)

4. 精度验证

python eval_accuracy.py

对单张测试图片进行 CPU 与 NPU 一致性验证:

指标数值
max_abs_error0.018697
mean_abs_error0.002485
relative_error0.2653%
cosine_similarity0.999997
threshold1.0%
结果PASS
  • CPU Top-1 与 NPU Top-1 类别一致
  • CPU Top-5 与 NPU Top-5 类别一致

5. 性能参考

python benchmark.py

在单卡 Ascend910B4、batch_size=1 条件下的测试结果:

指标数值
Avg latency60.73 ms
Min latency41.47 ms
Max latency90.27 ms
P5058.65 ms
P9073.51 ms
P9581.89 ms
Throughput16.47 images/sec

6. 精度评测

本仓库仅提供 smoke consistency 验证。若需官方 ImageNet-1K Top-1/Top-5 精度,请使用标准评测数据集运行 timm 官方验证流程。

7. 自验证截图

关键验证摘要已保存至:

  • screenshots/self_verification.txt
  • screenshots/self_verification.png

8. 日志文件

文件说明
logs/env_check.logNPU 环境检查
logs/stage0.log阶段 0 模型预检
logs/paths.txt模型路径记录
logs/inference.logNPU 推理日志
logs/accuracy.logCPU/NPU 一致性对比
logs/benchmark.log性能基准测试
logs/prediction.txt推理结果摘要

9. 注意事项

  1. 权重加载:本工程严格使用 pretrained=False 创建模型结构,并从 ModelScope 本地缓存加载权重,不触发 HuggingFace Hub 下载。
  2. 不提交权重:*.bin、*.safetensors、*.pth、*.pt、*.ckpt、*.onnx 已加入 .gitignore,不会提交到仓库。
  3. 编译产物清理:fusion_result.json 和 kernel_meta/ 已加入 .gitignore 并在最终检查时清理。
  4. 标签占位:模型无 id2label 文件,README 和 logs/prediction.txt 中均使用 class_x 形式说明。

10. 标签

#NPU #Ascend #ImageClassification #timm #SwinTransformer #ModelScope