本文档记录 test_byobnet.r160_in1k 图像分类模型在华为昇腾 NPU(Ascend 910)上的适配和推理验证结果。
test_byobnet.r160_in1k 是一个极小的 BYOBNet(Bring Your Own Backbone Network)测试模型,参数量仅 0.5M,由 Ross Wightman 在 ImageNet-1k 上训练,适用于图像分类任务的快速测试和验证。
相关获取地址:
| 组件 | 版本 |
|---|---|
| 操作系统 | Linux (aarch64) |
| NPU | Ascend 910 |
| CANN | 8.5.1 |
| Python | 3.11.14 |
| PyTorch | 2.9.0 |
| torch-npu | 2.9.0.post1 |
| timm | 1.0.27 |
| torchvision | >= 0.15.0 |
| 属性 | 值 |
|---|---|
| 模型名称 | test_byobnet.r160_in1k |
| 任务类型 | 图像分类(ImageNet-1k, 1000 类) |
| 模型框架 | PyTorch (timm) |
| 输入格式 | 图像,3×160×160 (RGB) |
| 输出格式 | 1000 类 logits 和 softmax 概率 |
| 参数量 | 0.5M |
| 输入尺寸 | 160×160 |
| 预处理 | 中心裁剪 + bicubic 插值缩放 |
| 权重路径 | pytorch_model.bin (1.9MB) |
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple torch torchvision
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple timm>=1.0.0
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple Pillow>=10.0.0
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple modelscopefrom modelscope.hub.snapshot_download import snapshot_download
model_dir = snapshot_download('timm/test_byobnet.r160_in1k', cache_dir='/path/to/models')本模型使用 PyTorch + torch_npu 在昇腾 NPU 上进行推理。适配过程无需修改模型代码,仅需:
.to("npu:0") 转移到 NPU.cpu() 转回 CPU 进行后续处理核心适配逻辑:
import torch
import torch_npu # noqa: F401
from timm import create_model
# 加载模型
model = create_model("test_byobnet.r160_in1k", pretrained=False,
checkpoint_path="pytorch_model.bin", num_classes=1000)
model = model.eval().to("npu:0")
# 推理
with torch.no_grad():
output = model(input_tensor.to("npu:0"))
probs = torch.nn.functional.softmax(output, dim=1)cd /opt/atomgit/output/test_byobnet.r160_in1k
python3 inference.py --device cpu --iterations 3cd /opt/atomgit/output/test_byobnet.r160_in1k
python3 inference.py --device npu --iterations 3
| 排名 | CPU 类别 | CPU 概率 | NPU 类别 | NPU 概率 |
|---|---|---|---|---|
| 1 | class_24 | 65.60% | class_24 | 65.59% |
| 2 | class_63 | 5.11% | class_63 | 5.10% |
| 3 | class_43 | 1.96% | class_43 | 1.96% |
| 4 | class_82 | 1.90% | class_82 | 1.92% |
| 5 | class_84 | 1.21% | class_84 | 1.21% |
| 设备 | 平均推理耗时 (ms) |
|---|---|
| CPU | 5.99 ms |
| NPU | 2.33 ms |
NPU 推理速度约为 CPU 的 2.6 倍。
test_owl.jpg)分别在 CPU 和 NPU 上运行推理cd /opt/atomgit/output/test_byobnet.r160_in1k
python3 inference.py --device cpu --iterations 3
python3 inference.py --device npu --iterations 3
python3 compare_cpu_npu.py
| 指标 | 数值 |
|---|---|
| CPU logits 范围 | [-11.984, 2.494] |
| NPU logits 范围 | [-11.994, 2.493] |
| Logits 最大绝对误差 (MAE) | 0.014517 |
| Logits 平均绝对误差 | 0.003627 |
| Logits 平均相对误差 | 0.057% |
| Probability 最大绝对误差 | 0.000112 |
| Probability 平均绝对误差 | 0.000001 |
| Top-1 一致性 | 一致(均为 class_24) |
| Top-5 重叠 | 5/5 完全一致 |
结论:NPU 与 CPU 推理结果误差 < 1%(实际概率误差仅 0.0112%,远低于 1% 阈值)
| 文件 | 说明 |
|---|---|
inference.py | 推理脚本,支持 CPU 和 NPU |
compare_cpu_npu.py | CPU/NPU 精度对比脚本 |
requirements.txt | Python 依赖 |
readme.md | 本文档 |
screenshot_npu.png | NPU 推理终端截图 |
screenshot_compare.png | 精度对比终端截图 |
import torch
from PIL import Image
from timm import create_model
from timm.data import resolve_data_config, create_transform
# 加载模型 (CPU)
model = create_model("test_byobnet.r160_in1k", pretrained=True)
model = model.eval()
# 加载模型 (NPU)
# import torch_npu
# model = model.to("npu:0")
# 预处理
config = resolve_data_config({}, model=model)
transform = create_transform(**config)
img = Image.open("test_owl.jpg").convert("RGB")
x = transform(img).unsqueeze(0)
# 推理
with torch.no_grad():
output = model(x)
probs = torch.nn.functional.softmax(output, dim=1)
# Top-5 结果
top5_prob, top5_idx = torch.topk(probs, 5)
print(top5_idx, top5_prob)torch_npu 已正确安装且可用。torch.npu.empty_cache()。