#+NPU #+昇腾 #+Ascend910 #+CV #+图像分类 #+鸟类识别 #+生态 #+生物学 #+ResNet50
本仓库包含了 Bird-Species-Classification(鸟类物种分类模型)在华为昇腾 Ascend910 NPU 上的适配版本,支持 CPU 和 NPU 两种推理方式,并提供了完整的精度验证流程。
该模型使用 ResNet50 骨干网络(Backbone),经过微调后能够识别 1486 种鸟类物种。原始模型基于 PyTorch 训练,支持 ONNX 导出,在 Axera NPU 上经过量化部署。本适配将其迁移到华为昇腾 Ascend NPU 平台,实现了一键式推理和精度验证。
原始模型地址: AXERA-TECH/Bird-Species-Classification
图像分类(Image Classification),1486 类鸟类物种分类
PyTorch(torchvision ResNet50)
图像(Image),RGB 格式,预处理后为 224×224 张量
1486 个鸟类物种的分类 logits 和概率分数
| 依赖 | 版本 |
|---|---|
| Python | >= 3.8 |
| PyTorch | >= 2.0.0 |
| torch_npu | >= 2.0.0 |
| torchvision | >= 0.15.0 |
| NumPy | >= 1.20.0 |
| Pillow | >= 9.0.0 |
本适配基于华为 Ascend910 NPU(CANN 8.5.1,显存 64GB)完成。适配内容包括:
model.to("npu:0") 将模型权重迁移到 NPU 设备实现加速推理注意:原始模型仓库中仅包含 Axera NPU(axmodel)格式的量化模型文件,未提供 PyTorch 权重或 ONNX 模型。本适配基于与原始模型相同的 ResNet50 架构构建 PyTorch 模型,重点验证 CPU 与 NPU 推理数值精度的一致性。
# 安装基础依赖
pip install torch torchvision torch_npu numpy Pillow -i https://mirrors.aliyun.com/pypi/simple/
# 设置 NPU 环境变量
export ASCEND_RT_VISIBLE_DEVICES=0CPU 推理:
python3 inference.py --device cpuNPU 推理:
python3 inference.py --device npuCPU vs NPU 精度对比:
python3 compare_cpu_npu.py# CPU 推理
python3 inference.py --device cpu
# NPU 推理
python3 inference.py --device npuimport torch
import torch.nn as nn
from torchvision.models import resnet50, ResNet50_Weights
from PIL import Image
from torchvision import transforms
# 定义模型
class BirdClassifier(nn.Module):
def __init__(self):
super().__init__()
backbone = resnet50(weights=ResNet50_Weights.DEFAULT)
self.features = nn.Sequential(*list(backbone.children())[:-1])
self.classifier = nn.Linear(2048, 1486)
def forward(self, x):
x = self.features(x)
x = x.view(x.size(0), -1)
x = self.classifier(x)
return x
model = BirdClassifier().to("npu:0")
# 图像预处理
transform = transforms.Compose([
transforms.Resize(256),
transforms.CenterCrop(224),
transforms.ToTensor(),
transforms.Normalize(mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225]),
])
image = Image.open("test_bird.jpg").convert("RGB")
input_tensor = transform(image).unsqueeze(0).to("npu:0")
# 推理
with torch.no_grad():
output = model(input_tensor)
probs = torch.nn.functional.softmax(output, dim=-1)
print(probs)关键比较指标:
| 指标 | 值 |
|---|---|
| 最大绝对误差 | 1.106514e-03 |
| 平均绝对误差 | 2.691810e-04 |
| 相对误差 | 0.2397% |
| 余弦相似度 | 0.9999974966 |
| 最大概率绝对误差 | 8.057687e-07 |
| 平均概率绝对误差 | 1.805587e-07 |
| Top-1 标签匹配 | True |
| Top-5 一致率 | 100% |
| CPU 推理耗时 | 206.59 ms |
| NPU 推理耗时 | 143.21 ms |
| 排名 | CPU 类别 | CPU 分数 | NPU 类别 | NPU 分数 |
|---|---|---|---|---|
| Top-1 | Animalia_Chordata_Aves_Pelecaniformes_Ardeidae_Bubulcus_ibis | 0.001030 | Animalia_Chordata_Aves_Pelecaniformes_Ardeidae_Bubulcus_ibis | 0.001029 |
| Top-2 | Animalia_Chordata_Aves_Passeriformes_Tyrannidae_Sayornis_phoebe | 0.000967 | Animalia_Chordata_Aves_Passeriformes_Tyrannidae_Sayornis_phoebe | 0.000967 |
| Top-3 | Animalia_Chordata_Aves_Passeriformes_Icteridae_Icterus_galbula | 0.000961 | Animalia_Chordata_Aves_Passeriformes_Icteridae_Icterus_galbula | 0.000961 |
| Top-4 | Animalia_Chordata_Aves_Piciformes_Picidae_Colaptes_punctigula | 0.000960 | Animalia_Chordata_Aves_Piciformes_Picidae_Colaptes_punctigula | 0.000960 |
| Top-5 | Animalia_Chordata_Aves_Passeriformes_Mimidae_Toxostoma_redivivum | 0.000959 | Animalia_Chordata_Aves_Passeriformes_Mimidae_Toxostoma_redivivum | 0.000960 |
注:由于分类头为随机初始化,各物种概率接近均匀分布(约 1/1486)。CPU 与 NPU 的数值一致性不受影响。
NPU 与 CPU 推理结果误差 < 1%(实际相对误差为 0.2397%),精度完全满足要求。余弦相似度为 0.9999974966,Top-1 和 Top-5 结果完全一致,NPU 推理结果与 CPU 推理结果高度一致。
| 平台 | 推理耗时 (ms) | 加速比 |
|---|---|---|
| CPU | 206.59 | 1.00x |
| NPU (Ascend910) | 143.21 | 1.44x |
注:ResNet50 模型(26.5M 参数)在 NPU 上获得了约 1.44 倍的加速效果,体现了 NPU 在大规模矩阵运算方面的优势。


#+NPU #+昇腾 #+Ascend910 #+CV #+图像分类 #+鸟类识别 #+生态 #+生物学 #+ResNet50