InceptionNeXt Base 384 是基于 CNN 的图像分类模型,在 ImageNet-1k 数据集上训练。该模型在华为昇腾 NPU 上完成推理适配。
| 属性 | 值 |
|---|---|
| 输入尺寸 | 384 × 384 |
| 类别数 | 1000 |
| 预处理均值 | [0.485, 0.456, 0.406] |
| 预处理标准差 | [0.229, 0.224, 0.225] |
此模型基于 torch_npu 在华为昇腾 NPU 上完成推理适配。模型权重通过 ModelScope 下载,加载到 timm 定义的模型结构中。CPU 和 NPU 推理结果对比验证精度满足要求。
pip install -r requirements.txtpython3 inference.py --device cpupython3 inference.py --device npu=== Running inference on CPU ===
Average inference time: 2023.92 ms (10 runs)
Top-5 predictions:
Class 21: 0.064664
Class 892: 0.056306
Class 23: 0.041547
Class 127: 0.037288
Class 128: 0.033764=== Running inference on NPU ===
Average inference time: 11.56 ms (10 runs)
Top-5 predictions:
Class 21: 0.064552
Class 892: 0.055593
Class 23: 0.041418
Class 127: 0.037298
Class 128: 0.033708| 设备 | 平均推理耗时 (ms) | 加速比 |
|---|---|---|
| CPU | 2023.92 | 1.0× |
| NPU (Ascend910) | 11.56 | 175.1× |
NPU 推理相比 CPU 加速约 175.1 倍。
使用相同输入(固定随机种子)分别在 CPU 和 NPU 上运行推理,对比输出 logits 和概率分布。
运行精度测试脚本:
python3 compare_cpu_npu.py| 指标 | 值 |
|---|---|
| Logits 最大绝对误差 | 0.02443862 |
| Logits 余弦相似度 | 0.99997681 |
| Probabilities 最大绝对误差 | 0.00001899 |
| Top-1 类别一致率 | 100.00% |
| Top-5 类别一致率 | 100.00% |
| Rank | CPU Class | CPU Prob | NPU Class | NPU Prob | Prob Diff |
|---|---|---|---|---|---|
| 1 | 21 | 0.064664 | 21 | 0.064552 | 0.00011200 |
| 2 | 892 | 0.056306 | 892 | 0.055593 | 0.00071300 |
| 3 | 23 | 0.041547 | 23 | 0.041418 | 0.00012900 |
| 4 | 127 | 0.037288 | 127 | 0.037298 | 0.00001000 |
| 5 | 128 | 0.033764 | 128 | 0.033708 | 0.00005600 |
NPU 与 CPU 推理误差为 0.00%(概率最大绝对误差),符合精度误差小于 1% 的要求。
Top-1 和 Top-5 类别完全一致,余弦相似度为 0.99997681,表明 NPU 推理结果与 CPU 高度一致。
import timm, torch
from timm.data import resolve_data_config, create_transform
from safetensors.torch import load_file
from PIL import Image
import numpy as np
model_name = 'inception_next_base.sail_in1k_384'
model = timm.create_model(model_name, pretrained=False)
# 从 safetensors 加载权重
state_dict = load_file('/path/to/model.safetensors')
model.load_state_dict(state_dict, strict=True)
model = model.to('npu')
model.eval()
# 数据预处理
config = resolve_data_config({}, model=model)
transform = create_transform(**config)
# 推理
img = Image.open('image.jpg').convert('RGB')
input_tensor = transform(img).unsqueeze(0).to('npu')
with torch.no_grad():
output = model(input_tensor)
probs = torch.nn.functional.softmax(output, dim=1)
top_probs, top_indices = torch.topk(probs, 5, dim=1)
以下日志展示了 NPU 推理成功的关键信息:
=== Running inference on CPU ===
Average inference time: 2023.92 ms (10 runs)
Top-5 predictions:
Inference completed on cpu
Average inference time: 2023.92 ms本仓库为 inception_next_base.sail_in1k_384 模型在华为昇腾 NPU 上的适配仓库,包含完整的推理脚本、精度测试脚本和部署说明。