本项目将 HuggingFace google/efficientnet-b4 图像分类模型适配到华为昇腾 NPU (Ascend910)。模型通过 ModelScope 下载权重,使用 transformers 库的 AutoModelForImageClassification 进行本地加载,并在单卡 NPU 上完成推理验证、CPU-NPU 精度一致性检查和性能基准测试。
| 项目 | 版本/信息 |
|---|---|
| NPU | Ascend910 |
| CANN | 8.5.1 |
| PyTorch | 2.x |
| torch_npu | 适配 CANN 8.5.1 |
| transformers | 最新版 |
完整环境信息见 logs/env_check.log。
pip install -r requirements.txt
python inference.py推理结果 (NPU):
=== NPU Inference Result ===
Input image: assets/test.jpg
Logits shape: torch.Size([1, 1000])
Logits sample (first 10): [-0.4476, -0.5143, -0.0919, -0.2052, -0.1999, -0.2789, -0.1137, -0.1936, -0.4504, 0.0855]
Top-5 predictions:
1. class_405: 0.0048
2. class_21: 0.0040
3. class_701: 0.0040
4. class_657: 0.0038
5. class_895: 0.0038日志保存在 logs/inference.log。
对单张测试输入进行 CPU 与 NPU 一致性验证:
| 指标 | 数值 |
|---|---|
| max_abs_error | 0.004179 |
| mean_abs_error | 0.000734 |
| relative_error | 0.1887% |
| cosine_similarity | 0.999999 |
| threshold | 1.0% |
| 结果 | PASS |
单张图片 batch=1 在 Ascend910 上的推理性能:
| 指标 | 数值 |
|---|---|
| avg_latency_ms | 15.376 |
| min_latency_ms | 15.270 |
| max_latency_ms | 15.497 |
| p50_latency_ms | 15.363 |
| p90_latency_ms | 15.463 |
| p95_latency_ms | 15.480 |
| throughput_img/s | 65.04 |
本项目包含单输入 smoke consistency 验证,非官方完整验证集评测。详细指标见第 4 节。
见 screenshots/self_verification.txt 和 screenshots/self_verification.png。
logs/env_check.log — 环境检查logs/inference.log — NPU 推理输出logs/accuracy.log — CPU-NPU 精度一致性logs/benchmark.log — 性能基准测试snapshot_download 自动下载,首次运行需要联网。local_files_only=True 加载本地权重,不触发 HuggingFace 自动下载。#NPU #Ascend #Ascend910 #EfficientNet #ImageClassification