| 组件 | 规格 |
|---|---|
| NPU | Ascend 910 (64GB HBM) |
| CPU | ARM 64-core |
| 内存 | 系统内存 |
| 组件 | 版本 |
|---|---|
| 操作系统 | Linux (aarch64) |
| Python | 3.11.14 |
| PyTorch | 2.9.0 |
| torch-npu | 2.9.0.post1 |
| timm | 1.0.27 |
| CANN | 8.5.1 |
pip install timm torchvision Pillow requests safetensors modelscopefrom modelscope.hub.snapshot_download import snapshot_download
model_dir = snapshot_download("timm/convformer_s18.sail_in1k")
print(f"Model downloaded to: {model_dir}")import torch
import timm
from PIL import Image
from timm.data import resolve_data_config
from timm.data.transforms_factory import create_transform
model = timm.create_model("convformer_s18.sail_in1k", pretrained=True)
model.eval()
img = Image.open("test.jpg").convert("RGB")
config = resolve_data_config({}, model=model)
transform = create_transform(**config)
input_tensor = transform(img).unsqueeze(0)
with torch.no_grad():
output = model(input_tensor)
probs = torch.nn.functional.softmax(output[0], dim=0)
top5 = probs.topk(5)
for i in range(5):
print(f"{i+1}. class={top5.indices[i].item():5d} prob={top5.values[i].item():.6f}")import torch
import timm
from PIL import Image
from timm.data import resolve_data_config
from timm.data.transforms_factory import create_transform
model = timm.create_model("convformer_s18.sail_in1k", pretrained=True)
model = model.npu()
model.eval()
img = Image.open("test.jpg").convert("RGB")
config = resolve_data_config({}, model=model)
transform = create_transform(**config)
input_tensor = transform(img).unsqueeze(0).npu()
with torch.no_grad():
output = model(input_tensor)
probs = torch.nn.functional.softmax(output[0], dim=0)
top5 = probs.topk(5)
for i in range(5):
print(f"{i+1}. class={top5.indices[i].item():5d} prob={top5.values[i].item():.6f}")# CPU 推理
python inference.py --device cpu
# NPU 推理
python inference.py --device npu
# CPU/NPU 精度对比
python compare_cpu_npu.py使用测试图像进行推理,输入尺寸为 224×224。
| 指标 | CPU | NPU (Ascend 910) |
|---|---|---|
| 推理耗时 | 0.238s | 0.0124s |
| 加速比 | 1× | 19.19× |
| 排名 | CPU 类别 | CPU 概率 | NPU 类别 | NPU 概率 |
|---|---|---|---|---|
| 1 | 549 | 0.480434 | 549 | 0.480509 |
| 2 | 916 | 0.127432 | 916 | 0.127471 |
| 3 | 446 | 0.011479 | 446 | 0.011467 |
| 4 | 782 | 0.010046 | 782 | 0.010047 |
| 5 | 610 | 0.009587 | 610 | 0.009589 |
$ python inference.py --device cpu
Loading model: convformer_s18.sail_in1k
Model loaded on CPU
Input shape: (1, 3, 224, 224)
Running inference...
CPU inference time: 0.238s
Top-1: class=549 prob=0.480434
Top-2: class=916 prob=0.127432
$ python inference.py --device npu
Loading model: convformer_s18.sail_in1k
Model moved to NPU (Ascend 910)
Input shape: (1, 3, 224, 224)
Running inference...
NPU inference time: 0.0124s
Top-1: class=549 prob=0.480509
Top-2: class=916 prob=0.127471pretrained=True 加载)resolve_data_config + create_transform)| 指标 | 含义 | 目标值 |
|---|---|---|
| MAE | 平均绝对误差(Mean Absolute Error) | 越小越好 |
| MSE | 均方误差(Mean Squared Error) | 越小越好 |
| Max Error | 最大绝对误差(Max Absolute Error) | 越小越好 |
| Cosine Similarity | 余弦相似度 | 越接近 1 越好 |
| Mean Relative Error | 平均相对误差 | < 1% |
| 指标 | 数值 |
|---|---|
| MAE | 0.0002719831 |
| MSE | 1.213e-07 |
| Max Absolute Error | 0.001301527 |
| Cosine Similarity | 0.9999998808 |
| Mean Relative Error | 0.450367% |
| Top-5 一致数 | 5/5 |
| Top-1 一致 | 是 |
NPU 与 CPU 推理结果误差 < 1%,精度完全满足要求。
具体来说:
以上数据充分证明了 NPU 推理结果与 CPU 推理结果的高度一致性,NPU 在提供大幅性能提升的同时,保持了卓越的数值精度。
| 指标 | CPU | NPU (Ascend 910) |
|---|---|---|
| 推理耗时 | 0.238s | 0.0124s |
| 加速比 | 1× | 19.19× |
NPU (Ascend 910) 推理相比 CPU 推理取得了约 19.19× 的加速效果,同时保持了精度一致。这使得该模型适合部署在昇腾 NPU 上进行高性能图像分类推理。
# 方式一:ModelScope(推荐)
pip install modelscope
python -c "from modelscope.hub.snapshot_download import snapshot_download; snapshot_download('timm/convformer_s18.sail_in1k')"
# 方式二:HuggingFace
pip install huggingface_hub
python -c "from huggingface_hub import snapshot_download; snapshot_download('timm/convformer_s18.sail_in1k')"# 安装依赖
pip install -r requirements.txt
# CPU 推理
python inference.py --device cpu
# NPU 推理
python inference.py --device npu
# 精度对比
python compare_cpu_npu.py
#NPU #CV #图像分类 #昇腾 #Ascend #ConvFormer #timm #PyTorch #ImageNet基于现有评测数据,CPU 与 NPU 的 余弦相似度 精度误差为 0.0%,小于 1% 的精度要求。
本仓库提供完整的推理脚本,支持 CPU 和 NPU 双平台推理:
# NPU 推理
python3 inference.py --device npu
# CPU 推理
python3 inference.py --device cpu推理完成后会输出推理结果和耗时,表明模型在 NPU 上推理成功。