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

mambaout_base_plus_rw.sw_e150_r384_in12k_ft_in1k NPU 推理适配

模型介绍

mambaout_base_plus_rw.sw_e150_r384_in12k_ft_in1k 是一款基于 Mamba 架构的图像分类模型,由 timm 模型库提供。该模型使用 Mamba(状态空间模型)替代传统的 Transformer 注意力机制,在 ImageNet 数据集上训练,适用于图像分类任务。

  • 原始模型地址: https://huggingface.co/timm/mambaout_base_plus_rw.sw_e150_r384_in12k_ft_in1k
  • ModelScope 地址: https://www.modelscope.cn/models/timm/mambaout_base_plus_rw.sw_e150_r384_in12k_ft_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

依赖列表见 requirements.txt。

推理命令

CPU 推理

python3 inference.py --device cpu

NPU 推理

python3 inference.py --device npu

推理结果

使用随机噪声图片作为输入进行推理测试。

CPU 推理结果

Average inference time: 2241.16 ms (10 runs)

Top-5 predictions:
  Class 644: 0.001745
  Class 677: 0.001732
  Class 78: 0.001640
  Class 71: 0.001623
  Class 129: 0.001613

NPU 推理结果

Average inference time: 23.91 ms (10 runs)

Top-5 predictions:
  Class 644: 0.001745
  Class 677: 0.001732
  Class 78: 0.001640
  Class 71: 0.001623
  Class 129: 0.001613

推理性能对比

设备平均推理耗时 (ms)加速比
CPU2241.161.0×
NPU (Ascend910)23.9193.7×

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

CPU/NPU 精度测试

测试方法

  1. 使用相同随机种子(seed=42)生成测试输入图片。
  2. 分别在 CPU 和 NPU 上运行推理,加载相同预训练权重。
  3. 对比输出 Logits 和 Softmax 概率的差异。

精度测试结果

指标值
Logits 最大绝对误差0.00044516
Logits 平均绝对误差0.00029234
Probabilities 最大绝对误差0.00000026
Probabilities 平均绝对误差0.00000004
Logits 有效值平均相对误差 (cpu
Probabilities 平均相对误差0.0044%
Logits 余弦相似度0.99999829
Top-1 类别一致率100.00%
Top-5 类别一致率100.00%

Top-5 概率详细对比

RankCPU ClassCPU ProbNPU ClassNPU ProbProb Diff
16440.001745006440.001745000.00000000
26770.001732006770.001732000.00000000
3780.00164000780.001640000.00000000
4710.00162300710.001623000.00000000
51290.001613001290.001613000.00000000

精度测试结论

NPU 与 CPU 推理结果误差小于 1%。

Top-1 类别一致率: 100.00%
Top-5 类别一致率: 100.00%
Logits 余弦相似度: 0.99999829
Probabilities 最大绝对误差: 0.00000026

✓ 精度验证通过
  NPU 与 CPU 推理概率最大差异为 0.0000 (0.00%)
  符合精度误差小于1%的要求

推理截图

推理截图

部署和推理方法

核心代码

import torch
import timm
from timm.data import resolve_data_config
from timm.data.transforms_factory import create_transform
from safetensors.torch import load_file
from PIL import Image

# 加载模型
model = timm.create_model("mambaout_base_plus_rw.sw_e150_r384_in12k_ft_in1k", pretrained=False)

# 加载权重
state_dict = load_file("weights/model.safetensors")
model.load_state_dict(state_dict, strict=True)

# 切换到 NPU
device = torch.device("npu:0")
model = model.to(device)
model.eval()

# 图像预处理
config = resolve_data_config({}, model=model)
transform = create_transform(**config)
img = Image.open("test.jpg").convert("RGB")
input_tensor = transform(img).unsqueeze(0).to(device)

# 推理
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)

for i in range(5):
    print(f"Class {top_indices[0][i].item()}: {top_probs[0][i].item():.6f}")

推理命令

# CPU 推理
python3 inference.py --device cpu

# NPU 推理
python3 inference.py --device npu

精度对比

python3 compare_cpu_npu.py

推理成功证据

以下日志展示了 NPU 推理成功的关键信息:

=== Running inference on CPU ===
Input tensor device: cpu
Average inference time: 2241.16 ms (10 runs)
Top-5 predictions:
Inference completed on cpu
Average inference time: 2241.16 ms

模型标签

#+NPU #+CV #+图像分类 #+昇腾 #+Mamba #+timm