google/efficientnet-b3 在昇腾 NPU 上的部署
1. 简介
将 ModelScope 上的 google/efficientnet-b3 图片分类模型适配到华为昇腾 NPU (Ascend910B) 上运行。
2. 验证环境
- 硬件:华为昇腾 910B NPU
- 操作系统:Linux (aarch64)
- Python:3.11
- PyTorch:2.x + torch_npu
- CANN:8.5.1
- transformers:4.57.6
- ModelScope:latest
3. 推理运行
pip install -r requirements.txt
python inference.py
推理结果 (NPU Top-5):
- Top-1: 公园长椅 (0.8660)
- Top-2: 黄铜牌、纪念牌、匾额 (0.0101)
- Top-3: 垃圾桶、 trash can、 garbage can、 wastebin、 ash bin、 ash-bin、 ashbin、 dustbin、 trash barrel、 trash bin (0.0015)
- Top-4: 间歇泉 (0.0012)
- Top-5: CD播放机 (0.0012)
日志保存在 logs/inference.log。
4. 精度验证
对单张测试图片进行 CPU 与 NPU 一致性验证:
| 指标 | 数值 |
|---|
| max_abs_error | 0.002447 |
| mean_abs_error | 0.000478 |
| relative_error | 0.0797% |
| cosine_similarity | 1.000000 |
| threshold | 1.0% |
| 结果 | PASS |
- CPU Top-1: 公园长椅
- NPU Top-1: 公园长椅
- CPU Top-5: 公园长椅、黄铜牌、纪念牌、匾额、垃圾桶、 trash can、 garbage can、 wastebin、 ash bin、 ash-bin、 ashbin、 dustbin、 trash barrel、 trash bin、间歇泉、CD播放机
- NPU Top-5: 公园长椅、黄铜牌、纪念牌、匾额、垃圾桶、 trash can、 garbage can、 wastebin、 ash bin、 ash-bin、 ashbin、 dustbin、 trash barrel、 trash bin、间歇泉、CD播放机
- Top-1 匹配: True
- Top-5 匹配: True
5. 性能参考
| 指标 | 数值 |
|---|
| 平均延迟 | 毫秒 |
| 最小延迟 | 毫秒 |
| 最大延迟 | 毫秒 |
| P50 延迟 | 毫秒 |
| P90 延迟 | 毫秒 |
| P95 延迟 | 毫秒 |
| 吞吐量 | 张/秒 |
测试条件:2 次预热 + 10 次正式测量,batch_size=1,单卡 NPU。
6. 精度评测说明
本项目包含单图 smoke consistency 验证,非官方 ImageNet 完整验证集评测。详细指标见第 4 节。
7. 自验证截图

8. 日志文件
logs/inference.log — NPU 推理结果
logs/accuracy.log — CPU-NPU 精度一致性验证
logs/benchmark.log — 性能基准测试
9. 注意事项
- 使用
ModelScope snapshot_download 下载权重,禁止 HuggingFace 自动下载
- 使用
EfficientNetForImageClassification.from_pretrained 加载预训练权重
- 推理使用
torch.npu.synchronize() 确保 NPU 计算完成
- 精度验证使用官方 Ascend 一致性指标:max_abs_error、mean_abs_error、relative_error、cosine_similarity
10. 标签 #NPU