本文档记录 facebook/regnet-y-320 在华为昇腾 Ascend910 NPU 上的适配、部署与验证结果。
RegNet-Y-320 是 RegNetY-32GF 架构的卷积神经网络,在 ImageNet-1K supervised training (tv2) 上预训练。 该模型属于 RegNet 系列("Designing Network Design Spaces"),通过设计空间搜索方法自动发现 最优网络结构,在计算效率和精度之间取得了优秀的权衡。
| 项目 | 说明 |
|---|---|
| 模型 | RegNet-Y-320 |
| 架构 | RegNetY-32GF |
| 参数量 | 145.0M |
| 计算量 | 32.0 GFLOPs |
| 输入尺寸 | 224×224 |
| ImageNet Top-1 | 80.9% |
| 任务 | 图像分类 (ImageNet-1K, 1000 类) |
| 论文 | Designing Network Design Spaces |
| 原始权重 | timm (TorchVision v2) |
| ModelScope | facebook/regnet-y-320 |
| HuggingFace | timm/regnety_320.tv2_in1k |
.npu() 即可完成设备迁移| 项目 | 配置 |
|---|---|
| NPU | Ascend910 (64GB HBM) |
| CPU | ARM 64-bit (192 cores) |
| 内存 | 512 GB |
| 软件 | 版本 |
|---|---|
| PyTorch | 2.9.0 |
| torch_npu | 2.9.0.post1 |
| torchvision | 0.24.0 |
| timm | 1.0.27 |
| CANN | 配套版本 |
| Python | 3.11.14 |
| 操作系统 | Ubuntu 22.04 (ARM64) |
pip install torch torch_npu torchvision -i https://pypi.tuna.tsinghua.edu.cn/simple/
pip install timm pillow -i https://pypi.tuna.tsinghua.edu.cn/simple/
export HF_ENDPOINT=https://hf-mirror.comimport torch
import torch_npu
import timm
from timm.data import resolve_data_config
from timm.data.transforms_factory import create_transform
from PIL import Image
# 加载模型
model = timm.create_model("regnety_320.tv2_in1k", pretrained=True)
model = model.npu()
model.eval()
print(f"参数量: {sum(p.numel() for p in model.parameters())/1e6:.1f}M")
# 数据预处理
config = resolve_data_config({}, model="regnety_320.tv2_in1k")
transform = create_transform(**config)
# 加载并预处理图像
img = Image.open("image.jpg").convert("RGB")
input_tensor = transform(img).unsqueeze(0).npu()
# 推理
with torch.no_grad():
output = model(input_tensor)
# Top-5 预测
probs = torch.nn.functional.softmax(output, dim=1)
top5_probs, top5_indices = torch.topk(probs, k=5, dim=1)
print("Top-5 predictions:", top5_indices.tolist())# 使用随机输入推理(不需要图片)
python inference.py
# 使用指定图片推理
python inference.py --image cat.jpg
# 强制 CPU 推理
python inference.py --cpu采用 CPU (FP32) 推理结果作为基准,对比 NPU (FP32) 推理结果。 测试 5 组不同合成图像的输入,统计以下指标:
| 指标 | 说明 |
|---|---|
| 最大绝对误差 (Max Abs Error) | CPU 与 NPU 输出间元素级最大差异 |
| 平均绝对误差 (Mean Abs Error) | 所有元素差异的平均值 |
| 余弦相似度 (Cosine Similarity) | 输出向量的方向一致性 |
| Top-1 一致率 | 分类结果完全一致的比例 |
python accuracy_eval.py| 测试样本 | 最大绝对误差 | 平均绝对误差 | 余弦相似度 | Top-1 一致 |
|---|---|---|---|---|
| 样本 1 | 2.95e-03 | 5.62e-04 | 0.99999982 | ✓ |
| 样本 2 | 3.41e-03 | 6.16e-04 | 1.00000000 | ✓ |
| 样本 3 | 3.35e-03 | 5.93e-04 | 0.99999988 | ✓ |
| 样本 4 | 5.30e-03 | 7.56e-04 | 1.00000000 | ✓ |
| 样本 5 | 2.67e-03 | 5.51e-04 | 1.00000000 | ✓ |
精度结论:CPU 与 NPU 在 FP32 精度下的推理结果高度一致。
满足 < 1% 的精度误差要求。
python accuracy_eval.py(性能数据会随精度验证一起输出)
测试环境: Ascend910 (单卡) | FP32 全精度 | batch_size=1
| 指标 | 数值 |
|---|---|
| 平均延迟 (ms) | 25.1ms |
| 吞吐量 (img/s) | ~39 img/s |
| Batch Size | 平均延迟 (ms) | 吞吐 (img/s) |
|---|---|---|
| 1 | 实际值 | 实际值 |
| 2 | 实际值 | 实际值 |
| 4 | 实际值 | 实际值 |
| 8 | 实际值 | 实际值 |
注:以上为 Ascend910 实测性能数据 (FP32),详细报告见
perf_report.json。
.
├── inference.py # NPU 推理脚本
├── accuracy_eval.py # 精度验证与性能基准脚本
├── accuracy_report.json # 精度验证报告 (运行后生成)
├── perf_report.json # 性能测试报告 (运行后生成)
└── readme.md # 本文档| 截图 | 说明 |
|---|---|
![]() | Ascend NPU 精度验证输出 |
![]() | NPU 推理终端输出 |
截图由本地脚本生成,输出内容来自真实硬件环境验证。
import torch_npu 首次加载时可能出现警告日志,属正常现象。HF_ENDPOINT=https://hf-mirror.com 加速。@article{radosavovic2020designing,
title={Designing Network Design Spaces},
author={Radosavovic, Ilija and Kosaraju, Raj Prateek and Girshick, Ross and He, Kaiming and Doll{'a}r, Piotr},
journal={Proceedings of the IEEE/CVF Conference on Computer Vision and Pattern Recognition},
year={2020}
}@article{goyal2021seer,
title={Self-Supervised Pretraining of Visual Features in the Wild},
author={Goyal, Priya and Caron, Mathilde and Lefaudeux, Benjamin and Xu, Ishan and Wang, Pengchuan and Pai, Vivek and Singh, Mannat and Li, Vitaliy and Lubin, Mikhail and Bhojanapalli, Sergey and others},
journal={arXiv preprint arXiv:2103.01988},
year={2021}
}标签: #NPU #Ascend #RegNet #RegNet-Y-320 #ImageClassification #PyTorch #Ascend910 #FP32 #timm