g
gcw_HxvH3o63/test_resnet_r160_in1k_ascend_model
模型介绍文件和版本Pull Requests讨论分析
下载使用量0

timm/test_resnet.r160_in1k 昇腾 NPU 适配

1. 模型信息

  • 模型名称:timm/test_resnet.r160_in1k
  • 模型来源:Hugging Face / timm
  • 模型类型:ImageNet-1k 图像分类模型
  • 模型结构:Test ResNet
  • 输入尺寸:160 × 160
  • 输出类别数:1000
  • 推理框架:PyTorch + timm + torch-npu
  • 运行设备:Ascend NPU
  • 适配目标:完成 timm/test_resnet.r160_in1k 模型在昇腾 NPU 环境下的图像分类推理验证,并与 CPU 推理结果进行误差对比。

本项目面向昇腾 Model-Agent 模型适配大赛赛道一,完成 timm/test_resnet.r160_in1k 图像分类模型在 Ascend NPU 环境下的适配验证。项目基于 PyTorch、timm 和 torch-npu,实现模型加载、测试图片生成、CPU 推理、NPU 推理、Top-5 分类输出、CPU/NPU 输出一致性对比以及验证材料整理。

2. 项目说明

timm/test_resnet.r160_in1k 是 timm 模型库中的小型 ResNet 图像分类模型,使用 ImageNet-1k 数据集训练,输入尺寸为 160 × 160。模型输入一张 RGB 图像后输出 1000 维 ImageNet 分类 logits,并进一步得到 Top-5 分类结果。

本次适配内容包括:

  1. 在 Ascend NPU Notebook 环境中安装依赖;
  2. 从 Hugging Face / timm 加载 test_resnet.r160_in1k 模型;
  3. 构造测试图片 test.jpg;
  4. 分别执行 CPU 与 NPU 图像分类推理;
  5. 保存 CPU/NPU 的 logits、概率和 Top-5 分类结果;
  6. 对 CPU 与 NPU 输出进行误差对比;
  7. 保存日志、截图和适配报告,用于赛道一模型适配验证提交。

3. 工程结构

.
├── README.md
├── adaptation_report.md
├── download_model.sh
├── make_test_image.py
├── inference.py
├── compare_cpu_npu.py
├── requirements.txt
├── test.jpg
├── cpu_result.json
├── cpu_result.txt
├── cpu_infer.log
├── npu_result.json
├── npu_result.txt
├── npu_infer.log
├── compare_result.txt
├── fusion_result.json
├── run.log
└── screenshots/
    ├── npu_env.png
    ├── npu_result.png
    └── compare_result.png

其中:

  • README.md:项目说明文档;
  • adaptation_report.md:Ascend NPU 适配报告;
  • download_model.sh:模型下载脚本;
  • make_test_image.py:测试图片生成脚本;
  • inference.py:CPU/NPU 推理脚本;
  • compare_cpu_npu.py:CPU/NPU 输出误差对比脚本;
  • requirements.txt:Python 依赖文件;
  • test.jpg:测试图片;
  • cpu_result.json:CPU 推理结构化结果;
  • cpu_result.txt:CPU 推理日志;
  • cpu_infer.log:CPU 推理补充日志;
  • npu_result.json:NPU 推理结构化结果;
  • npu_result.txt:NPU 推理日志;
  • npu_infer.log:NPU 推理补充日志;
  • compare_result.txt:CPU/NPU 对比文本结果;
  • fusion_result.json:汇总结果文件;
  • run.log:完整运行日志;
  • screenshots/:验证截图材料。

4. 环境检查

在 Ascend NPU Notebook 中执行以下命令检查运行环境:

npu-smi info
python --version
python - <<'PY'
import torch
import timm

print("torch:", torch.__version__)
print("timm:", timm.__version__)

try:
    import torch_npu
    print("torch_npu import success")
    print("npu available:", torch.npu.is_available())
except Exception as e:
    print("torch_npu import failed:", repr(e))
PY

环境检查截图保存为:

screenshots/npu_env.png

该截图用于证明当前运行环境存在 Ascend NPU,并记录 NPU 型号、运行状态和 Python 版本信息。

5. 模型下载

运行:

bash download_model.sh

模型来源为 Hugging Face / timm:

timm/test_resnet.r160_in1k

推理日志中显示模型加载名称为:

loaded_model_name: hf_hub:timm/test_resnet.r160_in1k

说明本次验证使用 Hugging Face Hub 上的 timm 模型进行加载和推理。

6. 测试输入

本项目使用测试图片:

test.jpg

推理流程包括:

  1. 读取测试图片;
  2. 使用 timm 对图片进行预处理;
  3. 构造模型输入张量;
  4. 输入 test_resnet.r160_in1k 图像分类模型;
  5. 输出 1000 维 ImageNet 分类 logits;
  6. 计算 softmax 概率;
  7. 输出 Top-5 分类结果。

输入张量形状为:

[1, 3, 160, 160]

7. CPU 推理

运行:

python inference.py --device cpu --image test.jpg --output cpu_result.json 2>&1 | tee cpu_result.txt

CPU 推理输出文件:

cpu_result.json
cpu_result.txt
cpu_infer.log

CPU 推理日志摘要如下:

model: timm/test_resnet.r160_in1k
loaded_model_name: hf_hub:timm/test_resnet.r160_in1k
device: cpu
image: test.jpg
input_shape: [1, 3, 160, 160]
output_shape: [1, 1000]
elapsed_seconds: 0.025727

CPU Top-5 输出如下:

rank 1: class_id=556, logit=0.41540262, prob=0.03323406
rank 2: class_id=772, logit=0.23925491, prob=0.02786656
rank 3: class_id=600, logit=0.11829598, prob=0.02469173
rank 4: class_id=545, logit=-0.04350952, prob=0.02100295
rank 5: class_id=446, logit=-0.07507758, prob=0.02035029

CPU logits 统计信息如下:

min: -8.91219711
max: 0.41540262
mean: -4.58447027
std: 1.64707851

8. NPU 推理

运行:

python inference.py --device npu --image test.jpg --output npu_result.json 2>&1 | tee npu_result.txt

NPU 推理输出文件:

npu_result.json
npu_result.txt
npu_infer.log

NPU 推理日志摘要如下:

model: timm/test_resnet.r160_in1k
loaded_model_name: hf_hub:timm/test_resnet.r160_in1k
device: npu
image: test.jpg
input_shape: [1, 3, 160, 160]
output_shape: [1, 1000]
elapsed_seconds: 14.204015

NPU Top-5 输出如下:

rank 1: class_id=556, logit=0.41562754, prob=0.03322843
rank 2: class_id=772, logit=0.23950188, prob=0.02786246
rank 3: class_id=600, logit=0.11855546, prob=0.02468841
rank 4: class_id=545, logit=-0.04416156, prob=0.02098099
rank 5: class_id=446, logit=-0.07306349, prob=0.02038328

NPU logits 统计信息如下:

min: -8.91153717
max: 0.41562754
mean: -4.58359671
std: 1.64683425

NPU 推理结果截图保存为:

screenshots/npu_result.png

9. CPU/NPU 误差对比

运行:

python compare_cpu_npu.py --cpu cpu_result.json --npu npu_result.json 2>&1 | tee compare_result.txt

对比脚本会读取 CPU 与 NPU 的 logits 输出,并计算:

  • CPU/NPU 输出形状;
  • CPU Top-5;
  • NPU Top-5;
  • CPU/NPU Top-1 是否一致;
  • CPU/NPU Top-5 顺序是否一致;
  • Top-5 重合数量;
  • 最大绝对误差;
  • 平均绝对误差;
  • RMSE;
  • 最大相对误差;
  • 平均相对误差;
  • 余弦相似度;
  • 是否通过验证。

对比结果保存为:

compare_result.txt

10. 自验证结果

本次 timm/test_resnet.r160_in1k 适配验证的 CPU/NPU 误差结果如下:

指标结果
CPU 输出形状(1000,)
NPU 输出形状(1000,)
CPU Top-5[556, 772, 600, 545, 446]
NPU Top-5[556, 772, 600, 545, 446]
CPU/NPU Top-1 是否一致True
CPU/NPU Top-5 顺序是否一致True
Top-5 重合数量5/5
最大绝对误差0.0061316490
平均绝对误差0.0014256469
RMSE0.0017895884
最大相对误差0.0006880064
平均相对误差0.0003108682
余弦相似度0.9999999498
是否通过验证True

对应的 compare_result.txt 内容如下:

CPU/NPU comparison result
==================================================
model: timm/test_resnet.r160_in1k
cpu_result: cpu_result.json
npu_result: npu_result.json

cpu_output_shape: (1000,)
npu_output_shape: (1000,)

cpu_top5: [556, 772, 600, 545, 446]
npu_top5: [556, 772, 600, 545, 446]
same_top1: True
same_top5_order: True
top5_overlap: 5/5

max_abs_error: 0.0061316490
mean_abs_error: 0.0014256469
rmse: 0.0017895884
max_relative_error: 0.0006880064
mean_relative_error: 0.0003108682
cosine_similarity: 0.9999999498

passed: True

根据上述结果,CPU 与 NPU 的 Top-1 结果一致,Top-5 顺序完全一致,Top-5 重合数量为 5/5。最大绝对误差为 0.0061316490,平均绝对误差为 0.0014256469,余弦相似度达到 0.9999999498,验证结果为 passed: True。因此,本次 timm/test_resnet.r160_in1k 昇腾 NPU 推理验证通过。

11. 验证截图材料

11.1 NPU 环境截图

npu_env

该截图展示 Ascend NPU Notebook 环境、npu-smi info 输出和 Python 版本信息。

11.2 NPU 推理结果截图

npu_result

该截图展示 NPU 推理日志,包括模型名称、运行设备、输入图片、输入张量形状、输出张量形状和 Top-5 分类结果。

11.3 CPU/NPU 误差对比截图

compare_result

该截图展示 CPU/NPU 输出误差对比结果,包括 Top-1 一致性、Top-5 一致性、最大绝对误差、平均绝对误差、RMSE、相对误差和余弦相似度。

12. 运行日志与提交材料

本项目提交材料包括:

  • README.md
  • adaptation_report.md
  • download_model.sh
  • make_test_image.py
  • inference.py
  • compare_cpu_npu.py
  • requirements.txt
  • test.jpg
  • cpu_result.json
  • cpu_result.txt
  • cpu_infer.log
  • npu_result.json
  • npu_result.txt
  • npu_infer.log
  • compare_result.txt
  • fusion_result.json
  • run.log
  • screenshots/npu_env.png
  • screenshots/npu_result.png
  • screenshots/compare_result.png

完整运行日志可查看:

run.log

CPU 推理日志可查看:

cpu_result.txt
cpu_infer.log

NPU 推理日志可查看:

npu_result.txt
npu_infer.log

CPU/NPU 误差对比结果可查看:

compare_result.txt

13. 适配说明

本项目的适配工作包括:

  1. 在 Ascend NPU 环境中完成依赖安装;
  2. 编写 Hugging Face / timm 模型下载脚本;
  3. 使用 timm 加载 test_resnet.r160_in1k 模型;
  4. 编写测试图片生成脚本;
  5. 编写 CPU/NPU 统一推理脚本;
  6. 支持测试图片输入和 timm 图像预处理;
  7. 保存 CPU 与 NPU 的结构化分类结果;
  8. 编写 CPU/NPU logits 误差对比脚本;
  9. 计算 Top-1 一致性、Top-5 顺序一致性、最大绝对误差、平均绝对误差、RMSE、相对误差和余弦相似度;
  10. 输出完整日志、截图和适配报告。

14. 结论

本项目完成了 timm/test_resnet.r160_in1k 模型在 Ascend NPU 环境下的图像分类推理适配验证。

验证结果表明,NPU 推理能够正常完成模型加载、图像预处理和 Top-5 分类输出。CPU 与 NPU 的 Top-1 结果一致,Top-5 顺序完全一致,最大绝对误差为 0.0061316490,平均绝对误差为 0.0014256469,余弦相似度达到 0.9999999498,并通过验证。本项目可作为赛道一模型适配提交材料。