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

inception_resnet_v2.tf_ens_adv_in1k NPU 推理适配

模型介绍

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

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

模型信息

属性值
输入尺寸299 × 299
类别数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: 628.26 ms (10 runs)

Top-5 predictions:
  Class 904: 0.152281
  Class 446: 0.144242
  Class 556: 0.051202
  Class 549: 0.026720
  Class 844: 0.016184

NPU 推理结果

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

Top-5 predictions:
  Class 904: 0.152546
  Class 446: 0.144232
  Class 556: 0.051285
  Class 549: 0.026734
  Class 844: 0.016150

推理性能对比

设备平均推理耗时 (ms)加速比
CPU628.261.0×
NPU (Ascend910)24.8625.3×

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

CPU/NPU 精度测试

测试方法

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

运行精度测试脚本:

python3 compare_cpu_npu.py

精度测试结果

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

Top-5 概率详细对比

RankCPU ClassCPU ProbNPU ClassNPU ProbProb Diff
19040.1522819040.1525460.00026500
24460.1442424460.1442320.00001000
35560.0512025560.0512850.00008300
45490.0267205490.0267340.00001400
58440.0161848440.0161500.00003400

精度测试结论

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

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

模型标签

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

仓库说明

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