g
gyccc/timm-deit_small_patch16_224.fb_in1k-NPU
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

timm/deit_small_patch16_224.fb_in1k on Ascend NPU

1. 简介

本项目将 timm/deit_small_patch16_224.fb_in1k 图片分类模型适配到华为昇腾 NPU (Ascend910) 上进行推理验证与性能测试。

  • 模型类型: Vision Transformer (ViT) 图片分类
  • 输入尺寸: 224 x 224
  • 输出类别: 1000 (ImageNet-1k)
  • 权重来源: ModelScope snapshot_download
  • 加载方式: timm.create_model(pretrained=False) + 本地权重加载

2. 验证环境

项目版本/型号
NPUAscend910_9362
npu-smi25.5.2
PyTorch2.9.0+cpu
torch_npu已安装
timm最新版
npu_available=True, device=Ascend910_9362

3. 推理运行

pip install -r requirements.txt
python inference.py

推理日志示例:

Loading model: timm/deit_small_patch16_224.fb_in1k
Model moved to npu:0
Input shape: torch.Size([1, 3, 224, 224])
Output shape: torch.Size([1, 1000])

Top-5 predictions:
  1. class_405: 0.138455
  2. class_701: 0.093867
  3. class_895: 0.090754
  4. class_404: 0.025870
  5. class_21: 0.025128

Inference completed successfully on NPU.

4. 精度验证

python eval_accuracy.py

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

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

5. 性能参考

python benchmark.py
指标数值
Avg Latency5.31 ms
Min Latency5.26 ms
Max Latency5.35 ms
P505.32 ms
P905.35 ms
P955.35 ms
Throughput188.27 images/sec

测试条件:单卡 Ascend910,batch_size=1,预热 2 轮 + 正式 10 轮。

6. 精度评测

指标数值
Max logits diff0.01296711
Mean logits diff0.00219398
Max prob diff0.00048672
Top-1 MatchTrue
Top-5 MatchTrue

CPU 与 NPU 推理结果在概率分布和 Top-K 预测上完全一致,logits 差异处于正常浮点误差范围内。

7. 自验证截图

见 screenshots/self_verification.png 和 screenshots/self_verification.txt。

8. 日志文件

文件内容
logs/env_check.logNPU 环境检查
logs/inference.log推理结果
logs/accuracy.logCPU-NPU 精度对比
logs/benchmark.log性能基准测试
logs/paths.txt模型路径记录

9. 注意事项

  1. 权重下载: 首次运行会通过 ModelScope snapshot_download 自动下载权重,缓存于 ~/.cache/modelscope/hub/。
  2. pretrained=False: 严禁使用 pretrained=True 触发 HuggingFace 直连下载,所有权重均通过 ModelScope 获取后本地加载。
  3. 不提交权重: 项目中不包含任何权重文件(.bin / .safetensors / .pth 等已列入 .gitignore)。
  4. NPU 同步: 推理和 benchmark 中已加入 torch.npu.synchronize() 确保计时准确。

10. 标签

#NPU