本工程将 ModelScope 上的 timm/swin_tiny_patch4_window7_224.ms_in1k 图片分类模型适配为可在华为昇腾 NPU(Ascend910B)上单卡运行的推理工程。
timm/swin_tiny_patch4_window7_224.ms_in1k)timm.create_model(..., pretrained=False) + ModelScope 本地权重加载| 项目 | 版本/信息 |
|---|---|
| NPU | Ascend910B4 |
| CANN | 8.5.1 |
| Python | 3.11 |
| torch | 已安装 |
| torch_npu | 已安装 |
| timm | 已安装 |
| modelscope | 已安装 |
pip install -r requirements.txtpython inference.py推理脚本会从 ModelScope 本地缓存加载 model.safetensors 权重,在 npu:0 上执行前向推理,并输出 Top-5 预测结果。
示例输出:
Model: timm/swin_tiny_patch4_window7_224.ms_in1k
Input shape: [1, 3, 224, 224]
Output shape: [1, 1000]
Top-5 predictions:
1. class_871 (index=871, prob=0.4197)
2. class_484 (index=484, prob=0.0669)
3. class_814 (index=814, prob=0.0395)
4. class_876 (index=876, prob=0.0373)
5. class_403 (index=403, prob=0.0372)注意:该模型未提供
id2label.json或labels.txt,因此预测标签以class_0~class_999形式展示。
python eval_accuracy.py对单张测试图片进行 CPU 与 NPU 一致性验证:
| 指标 | 数值 |
|---|---|
| max_abs_error | 0.013045 |
| mean_abs_error | 0.001968 |
| relative_error | 0.2812% |
| cosine_similarity | 0.999997 |
| threshold | 1.0% |
| 结果 | PASS |
python benchmark.py| 指标 | 数值 |
|---|---|
| Iterations | 10 |
| Batch size | 1 |
| Avg latency | 23.04 ms |
| Min latency | 17.24 ms |
| Max latency | 35.00 ms |
| P50 | 21.13 ms |
| P90 | 30.39 ms |
| P95 | 32.69 ms |
| Throughput | 43.40 images/sec |
本仓库未包含完整 ImageNet 验证集,因此未提供官方 Top-1/Top-5 精度数字。用户如有需要,可基于 model_utils.py 中的加载逻辑,在完整验证集上自行评测。
自验证摘要见:
screenshots/self_verification.txtscreenshots/self_verification.png| 日志 | 说明 |
|---|---|
logs/env_check.log | NPU 环境检查 |
logs/inference.log | 推理结果 |
logs/prediction.txt | 预测结果(含标签说明) |
logs/accuracy.log | CPU/NPU 一致性对比 |
logs/benchmark.log | 性能基准测试 |
logs/paths.txt | 模型路径记录 |
pretrained=False + ModelScope 本地权重加载,不触发 HuggingFace Hub 自动下载。.gitignore 已排除 *.bin, *.safetensors, *.pth, *.pt, *.ckpt, *.onnx 等权重文件。fusion_result.json 和 kernel_meta/ 已删除并加入 .gitignore。class_0 至 class_999 作为通用标签。npu:0,请确保设备可用。#NPU #Ascend #ImageClassification #timm #SwinTransformer #ModelScope