e
gcw_GSiqzzLf/inception_next_small_sail_in1k-npu
模型介绍文件和版本Pull Requests讨论分析
下载使用量0

inception_next_small.sail_in1k NPU 推理适配

模型介绍

InceptionNeXt Small 是基于 CNN 的图像分类模型,在 ImageNet-1k 数据集上训练。该模型在华为昇腾 NPU 上完成推理适配。

  • 原始模型地址: https://huggingface.co/timm/inception_next_small.sail_in1k
  • ModelScope 地址: https://www.modelscope.cn/models/timm/inception_next_small.sail_in1k
  • 任务类型: 图像分类 (Image Classification)
  • 模型框架: PyTorch + timm

模型信息

属性值
输入尺寸224 × 224
类别数1000
预处理均值[0.485, 0.456, 0.406]
预处理标准差[0.229, 0.224, 0.225]

NPU 适配说明

此模型基于 torch_npu 在华为昇腾 NPU 上完成推理适配。模型权重通过 ModelScope 下载,加载到 timm 定义的模型结构中。CPU 和 NPU 推理结果对比验证精度满足要求。

环境准备

系统要求

  • 华为昇腾 NPU (Ascend910)
  • CANN 8.5.1
  • Python 3.11
  • torch 2.9.0 + torch_npu 2.9.0

安装依赖

pip install -r requirements.txt

推理命令

CPU 推理

python3 inference.py --device cpu

NPU 推理

python3 inference.py --device npu

推理结果

CPU 推理结果

=== Running inference on CPU ===
Average inference time: 444.25 ms (10 runs)

Top-5 predictions:
  Class 21: 0.085479
  Class 23: 0.043686
  Class 22: 0.030085
  Class 127: 0.028972
  Class 701: 0.024431

NPU 推理结果

=== Running inference on NPU ===
Average inference time: 11.69 ms (10 runs)

Top-5 predictions:
  Class 21: 0.085584
  Class 23: 0.043381
  Class 22: 0.029996
  Class 127: 0.029142
  Class 701: 0.024665

推理性能对比

设备平均推理耗时 (ms)加速比
CPU444.251.0×
NPU (Ascend910)11.6938.0×

NPU 推理相比 CPU 加速约 38.0 倍。

CPU/NPU 精度测试

测试方法

使用相同输入(固定随机种子)分别在 CPU 和 NPU 上运行推理,对比输出 logits 和概率分布。

运行精度测试脚本:

python3 compare_cpu_npu.py

精度测试结果

指标值
Logits 最大绝对误差0.07451981
Logits 余弦相似度0.99994716
Probabilities 最大绝对误差0.00030471
Top-1 类别一致率100.00%
Top-5 类别一致率100.00%

Top-5 概率详细对比

RankCPU ClassCPU ProbNPU ClassNPU ProbProb Diff
1210.085479210.0855840.00010500
2230.043686230.0433810.00030500
3220.030085220.0299960.00008900
41270.0289721270.0291420.00017000
57010.0244317010.0246650.00023400

精度测试结论

NPU 与 CPU 推理误差为 0.03%(概率最大绝对误差),符合精度误差小于 1% 的要求。

Top-1 和 Top-5 类别完全一致,余弦相似度为 0.99994716,表明 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_small.sail_in1k'
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: 444.25 ms (10 runs)
Top-5 predictions:
Inference completed on cpu
Average inference time: 444.25 ms

模型标签

  • #+NPU
  • #+CV
  • #+图像分类
  • #+昇腾
  • #+InceptionNeXt
  • #+CNN

仓库说明

本仓库为 inception_next_small.sail_in1k 模型在华为昇腾 NPU 上的适配仓库,包含完整的推理脚本、精度测试脚本和部署说明。