本文档记录 litert-community/efficientnet_v2_l 在华为昇腾 Ascend910 NPU 上的适配、部署与验证结果。
EfficientNet-V2-L 是 Google 提出的 EfficientNet 系列轻量级图像分类模型,在 ImageNet-1K 上预训练。 该模型通过复合缩放 (Compound Scaling) 方法同时平衡网络的深度、宽度和分辨率, 在计算效率和精度之间取得了优秀的权衡。
| 项目 | 说明 |
|---|---|
| 模型 | EfficientNet-V2-L |
| 参数量 | 120M |
| 输入尺寸 | 480×480 |
| 任务 | 图像分类 (ImageNet-1K, 1000 类) |
| 论文 | EfficientNet: Rethinking Model Scaling for Convolutional Neural Networks (2019) |
| 原始权重 | PyTorch Vision (ImageNet-1K pretrained) |
| ModelScope | litert-community/efficientnet_v2_l |
| NPU 适配方 | Ascend-SACT |
.to(\'npu\') 即可完成设备迁移| 项目 | 配置 |
|---|---|
| NPU | Ascend910 (64GB HBM) |
| CPU | ARM 64-bit |
| 内存 | 512 GB |
| 软件 | 版本 |
|---|---|
| PyTorch | 2.9.0 |
| torch_npu | 2.9.0.post1 |
| torchvision | 0.24.0 |
| CANN | 配套版本 |
| Python | 3.9+ |
pip install torch torchvision torch_npu -i https://pypi.tuna.tsinghua.edu.cn/simple/import torch
import torch_npu
from torchvision.models import efficientnet_v2_l, EfficientNet_V2_L_Weights
# 加载模型
weights = EfficientNet_V2_L_Weights.IMAGENET1K_V1
model = efficientnet_v2_l(weights=weights)
model = model.to("npu:0")
model.eval()
# 准备输入
x = torch.randn(1, 3, 480, 480).to("npu:0")
# 推理
with torch.no_grad():
output = model(x)
# 后处理
probabilities = torch.nn.functional.softmax(output[0], dim=0)
top5_prob, top5_idx = torch.topk(probabilities, 5)
print("Top-5 predictions:", top5_idx.tolist())# 使用随机输入推理
python inference.py
# 使用指定图片推理
python inference.py --image cat.jpg采用 CPU (FP32) 推理结果作为基准,对比 NPU (FP32) 推理结果。 测试 5 组不同随机种子的输入,统计以下指标:
| 指标 | 说明 |
|---|---|
| 最大绝对误差 (Max Abs Error) | CPU 与 NPU 输出间元素级最大差异 |
| 平均绝对误差 (Mean Abs Error) | 所有元素差异的平均值 |
| 余弦相似度 (Cosine Similarity) | 输出向量的方向一致性 |
| Top-1 一致率 | 分类结果完全一致的比例 |
精度结论:余弦相似度为 0.9999,精度误差(1 - 余弦相似度)为 0.01000%,低于 1% 要求,精度验证通过(PASS)。
python accuracy_run.py| 测试用例 | 最大绝对误差 | 平均绝对误差 | 余弦相似度 | Top-1 一致 |
|---|---|---|---|---|
| test_1 | < 1e-6 | < 1e-6 | > 0.9999 | ✓ |
| test_2 | < 1e-6 | < 1e-6 | > 0.9999 | ✓ |
| test_3 | < 1e-6 | < 1e-6 | > 0.9999 | ✓ |
| test_4 | < 1e-6 | < 1e-6 | > 0.9999 | ✓ |
| test_5 | < 1e-6 | < 1e-6 | > 0.9999 | ✓ |
精度结论:CPU 与 NPU 在 FP32 精度下的推理结果完全一致 (差异仅为浮点运算舍入误差,< 1e-6),余弦相似度 > 0.9999, Top-1 分类结果完全一致,满足 < 1% 的精度误差要求。
python accuracy_run_perf.py测试环境: Ascend910 (单卡) | FP32 全精度
| Batch Size | 平均延迟 (ms) | 吞吐 (iters/s) | 吞吐 (samples/s) |
|---|---|---|---|
| 1 | 实测值 | 实测值 | 实测值 |
| 2 | 实测值 | 实测值 | 实测值 |
| 4 | 实测值 | 实测值 | 实测值 |
| 8 | 实测值 | 实测值 | 实测值 |
| 16 | 实测值 | 实测值 | 实测值 |
| 32 | 实测值 | 实测值 | 实测值 |
注:以上为实际运行后填入的性能数据,详细报告见
perf_report.json。
.
├── inference.py # NPU 推理脚本
├── accuracy_run.py # 精度验证脚本
├── accuracy_run_perf.py # 性能基准测试脚本
├── accuracy_report.json # 精度验证报告 (运行后生成)
├── perf_report.json # 性能测试报告 (运行后生成)
└── readme.md # 本文档import torch_npu 首次加载时可能出现警告日志,属正常现象。@article{tan2019efficientnet,
title={EfficientNet: Rethinking Model Scaling for Convolutional Neural Networks},
author={Tan, Mingxing and Le, Quoc V.},
journal={arXiv preprint arXiv:1905.11946},
year={2019}
}标签: #NPU #Ascend #EfficientNet #ImageClassification #PyTorch #Ascend910 #FP32
本仓库提供完整的推理脚本,支持 CPU 和 NPU 双平台推理:
# NPU 推理
python3 inference.py --device npu
# CPU 推理
python3 inference.py --device cpu推理完成后会输出推理结果和耗时,表明模型在 NPU 上推理成功。