timm/efficientvit_b2.r224_in1ktimm/efficientvit_b2.r224_in1k 模型在昇腾 NPU 环境下的图像分类推理验证,并与 CPU 推理结果进行误差对比。本项目面向昇腾 Model-Agent 模型适配大赛赛道一,完成 timm/efficientvit_b2.r224_in1k 图像分类模型在 Ascend NPU 环境下的适配验证。项目基于 PyTorch、timm 和 torch-npu,完成模型加载、测试图片生成、CPU 推理、NPU 推理、Top-5 分类输出、CPU/NPU 输出一致性对比以及验证材料整理。
efficientvit_b2.r224_in1k 是 timm 模型库中的 ImageNet-1k 图像分类模型。模型结构为 EfficientViT-B2,输入一张 RGB 图像后输出 1000 维 ImageNet 分类 logits,并进一步得到 Top-5 分类结果。
本次适配重点包括:
efficientvit_b2.r224_in1k 模型;test.jpg;.
├── 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其中:
download_model.sh:模型下载脚本;make_test_image.py:测试图片生成脚本;inference.py:CPU/NPU 推理脚本;compare_cpu_npu.py:CPU/NPU 输出误差对比脚本;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:完整运行日志;adaptation_report.md:适配报告;screenshots/:验证截图材料。在 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 版本信息。
运行:
bash download_model.sh模型来源为 Hugging Face / timm:
timm/efficientvit_b2.r224_in1k推理日志中显示模型加载名称为:
loaded_model_name: hf_hub:timm/efficientvit_b2.r224_in1k说明本次验证使用 Hugging Face Hub 上的 timm 模型进行加载和推理。
本项目使用测试图片:
test.jpg推理流程包括:
输入张量形状为:
[1, 3, 224, 224]运行:
python inference.py --device cpu --image test.jpg --output cpu_result.json 2>&1 | tee cpu_result.txtCPU 推理输出文件:
cpu_result.json
cpu_result.txt
cpu_infer.logCPU 推理日志摘要如下:
model: timm/efficientvit_b2.r224_in1k
loaded_model_name: hf_hub:timm/efficientvit_b2.r224_in1k
device: cpu
image: test.jpg
input_shape: [1, 3, 224, 224]
output_shape: [1, 1000]
elapsed_seconds: 0.305590CPU Top-5 输出如下:
rank 1: class_id=644, logit=-1.03248060, prob=0.42571017
rank 2: class_id=542, logit=-2.16423535, prob=0.13727744
rank 3: class_id=409, logit=-3.14174128, prob=0.05165041
rank 4: class_id=456, logit=-3.37931061, prob=0.04072853
rank 5: class_id=616, logit=-3.38664913, prob=0.04043074CPU logits 统计信息如下:
min: -11.22676468
max: -1.03248060
mean: -9.20418167
std: 1.01067781运行:
python inference.py --device npu --image test.jpg --output npu_result.json 2>&1 | tee npu_result.txtNPU 推理输出文件:
npu_result.json
npu_result.txt
npu_infer.logNPU 推理日志摘要如下:
model: timm/efficientvit_b2.r224_in1k
loaded_model_name: hf_hub:timm/efficientvit_b2.r224_in1k
device: npu
image: test.jpg
input_shape: [1, 3, 224, 224]
output_shape: [1, 1000]
elapsed_seconds: 21.862732NPU Top-5 输出如下:
rank 1: class_id=644, logit=-1.02553749, prob=0.42828050
rank 2: class_id=542, logit=-2.17374396, prob=0.13585280
rank 3: class_id=409, logit=-3.13673091, prob=0.05186193
rank 4: class_id=456, logit=-3.37691236, prob=0.04078864
rank 5: class_id=616, logit=-3.39943552, prob=0.03988021NPU logits 统计信息如下:
min: -11.23059177
max: -1.02553749
mean: -9.20420551
std: 1.01016331NPU 推理结果截图保存为:
screenshots/npu_result.png运行:
python compare_cpu_npu.py --cpu cpu_result.json --npu npu_result.json 2>&1 | tee compare_result.txt对比脚本会读取 CPU 与 NPU 的 logits 输出,并计算:
对比结果保存为:
compare_result.txt本次 timm/efficientvit_b2.r224_in1k 适配验证的 CPU/NPU 误差结果如下:
| 指标 | 结果 |
|---|---|
| CPU 输出形状 | (1000,) |
| NPU 输出形状 | (1000,) |
| CPU Top-5 | [644, 542, 409, 456, 616] |
| NPU Top-5 | [644, 542, 409, 456, 616] |
| CPU/NPU Top-1 是否一致 | True |
| CPU/NPU Top-5 顺序是否一致 | True |
| Top-5 重合数量 | 5/5 |
| 最大绝对误差 | 0.0141611099 |
| 平均绝对误差 | 0.0021718391 |
| RMSE | 0.0029005274 |
| 最大相对误差 | 0.0012613705 |
| 平均相对误差 | 0.0002359622 |
| 余弦相似度 | 0.9999999509 |
| 是否通过验证 | True |
对应的 compare_result.txt 内容如下:
CPU/NPU comparison result
==================================================
model: timm/efficientvit_b2.r224_in1k
cpu_result: cpu_result.json
npu_result: npu_result.json
cpu_output_shape: (1000,)
npu_output_shape: (1000,)
cpu_top5: [644, 542, 409, 456, 616]
npu_top5: [644, 542, 409, 456, 616]
same_top1: True
same_top5_order: True
top5_overlap: 5/5
max_abs_error: 0.0141611099
mean_abs_error: 0.0021718391
rmse: 0.0029005274
max_relative_error: 0.0012613705
mean_relative_error: 0.0002359622
cosine_similarity: 0.9999999509
passed: True根据上述结果,CPU 与 NPU 的 Top-1 结果一致,Top-5 顺序完全一致,Top-5 重合数量为 5/5。最大绝对误差为 0.0141611099,平均绝对误差为 0.0021718391,余弦相似度达到 0.9999999509,验证结果为 passed: True。因此,本次 timm/efficientvit_b2.r224_in1k 昇腾 NPU 推理验证通过。

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

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

该截图展示 CPU/NPU 输出误差对比结果,包括 Top-1 一致性、Top-5 一致性、最大绝对误差、平均绝对误差、RMSE、相对误差和余弦相似度。
本项目提交材料包括:
README.mdadaptation_report.mddownload_model.shmake_test_image.pyinference.pycompare_cpu_npu.pyrequirements.txttest.jpgcpu_result.jsoncpu_result.txtcpu_infer.lognpu_result.jsonnpu_result.txtnpu_infer.logcompare_result.txtfusion_result.jsonrun.logscreenshots/npu_env.pngscreenshots/npu_result.pngscreenshots/compare_result.png完整运行日志可查看:
run.logCPU 推理日志可查看:
cpu_result.txt
cpu_infer.logNPU 推理日志可查看:
npu_result.txt
npu_infer.logCPU/NPU 误差对比结果可查看:
compare_result.txt本项目的适配工作包括:
efficientvit_b2.r224_in1k 模型;本项目完成了 timm/efficientvit_b2.r224_in1k 模型在 Ascend NPU 环境下的图像分类推理适配验证。
验证结果表明,NPU 推理能够正常完成模型加载、图像预处理和 Top-5 分类输出。CPU 与 NPU 的 Top-1 结果一致,Top-5 顺序完全一致,最大绝对误差为 0.0141611099,平均绝对误差为 0.0021718391,余弦相似度达到 0.9999999509,并通过验证。本项目可作为赛道一模型适配提交材料。