将 timm/densenet201.tv_in1k 图片分类模型适配到华为昇腾 NPU(Ascend910B),使用 ModelScope snapshot_download 下载权重,timm.create_model(pretrained=False) 加载本地权重,包含推理验证、CPU-NPU 精度一致性检查和性能基准测试。
pip install -r requirements.txt
python inference.py推理结果:
Model: timm/densenet201.tv_in1k
Output shape: [1, 1000]
Top-5 predictions:
class_819: 0.6530 (65.30%)
class_413: 0.0450 (4.50%)
class_764: 0.0431 (4.31%)
class_650: 0.0294 (2.94%)
class_851: 0.0283 (2.83%)python eval_accuracy.py对单张测试图片进行 CPU 与 NPU 一致性验证:
| 指标 | 数值 |
|---|---|
| max_abs_error | 0.010974 |
| mean_abs_error | 0.002029 |
| relative_error | 0.1085% |
| cosine_similarity | 0.999999 |
| threshold | 1.0% |
| 结果 | PASS |
单卡 Ascend910B,batch=1,10 次推理取均值:
python benchmark.py=== Benchmark (NPU, single inference) ===
Runs: 10
Avg: 22.40 ms
Min: 21.96 ms
Max: 22.77 ms
P50: 22.39 ms
P90: 22.74 ms
P95: 22.75 ms
Throughput: 44.64 images/sec见 screenshots/self_verification.png
| 文件 | 说明 |
|---|---|
logs/inference.log | 推理结果日志 |
logs/accuracy.log | 精度验证日志 |
logs/benchmark.log | 性能基准日志 |
logs/env_check.log | 环境检查日志 |
#NPU