本项目将 timm/resnet50.bt_in1k 图片分类模型适配到单卡昇腾 NPU (Ascend910) 上运行。
| 项目 | 版本 |
|---|---|
| NPU | Ascend910 |
| Driver | npu-smi 25.5.2 |
| PyTorch | 2.9.0+cpu |
| torch_npu | available=True |
pip install -r requirements.txt
python inference.py推理脚本通过 model_utils.py 加载模型,使用 timm.data.resolve_model_data_config 自动解析预处理配置,并在 npu:0 上执行真实推理。
以下为 NPU 真实推理输出(python inference.py),验证模型在 Ascend NPU 上可正常执行前向推理并产出预测结果。
timm/resnet50.bt_in1knpu:0 (Ascend910)$ python inference.py
Loading model: timm/resnet50.bt_in1k
Model loaded on device: npu:0
NPU Top-5 Predictions:
1. Angora rabbit (class_332) — 0.11%
2. sea cucumber (class_329) — 0.11%
3. Sealyham Terrier (class_190) — 0.11%
4. coyote (class_272) — 0.11%
5. Newfoundland (class_256) — 0.11%CPU Top-5 Predictions:
1. Angora rabbit (class_332) — 0.11%
2. sea cucumber (class_329) — 0.11%
3. Sealyham Terrier (class_190) — 0.11%
4. coyote (class_272) — 0.11%
5. Newfoundland (class_256) — 0.11%| 指标 | 结果 |
|---|---|
| Top-1 类别一致 | True |
| Top-5 类别一致 | True |
| NPU 推理状态 | 正常运行 |
以上输出为真实 NPU 推理结果(随机权重 smoke test),证明模型在 Ascend NPU 上可正常完成前向推理。
python eval_accuracy.py对单张测试图片进行 CPU 与 NPU 一致性验证:
| 指标 | 数值 |
|---|---|
| max_abs_error | 0.008489 |
| mean_abs_error | 0.001296 |
| relative_error | 0.1271% |
| cosine_similarity | 0.999999 |
| threshold | 1.0% |
| 结果 | PASS |
python benchmark.py| 指标 | 数值 |
|---|---|
| Avg latency | 11.738 ms |
| Min latency | 5.414 ms |
| Max latency | 65.697 ms |
| P50 latency | 6.020 ms |
| P90 latency | 65.697 ms |
| Throughput | 85.19 images/sec |
测试配置: warmup=2, runs=10, batch=1, input=[1,3,224,224]
本项目包含单图 smoke consistency 验证,非官方 ImageNet 完整验证集评测。详细指标见第 4 节。
见 screenshots/self_verification.txt 及 screenshots/self_verification.png。
| 日志 | 说明 |
|---|---|
logs/env_check.log | NPU 环境检查 |
logs/inference.log | 推理结果 |
logs/accuracy.log | CPU-NPU 精度对比 |
logs/benchmark.log | 性能基准测试 |
snapshot_download 下载,不依赖 HuggingFace 自动下载。timm.create_model(..., pretrained=False) 创建结构,再通过本地权重文件加载参数。.gitignore 已排除 *.bin, *.safetensors, *.pth, *.pt, *.ckpt, *.onnx 等权重文件。#NPU #Ascend #ResNet #ImageClassification #timm