smp-hub/mit_b3.imagenetmit_b3imagenetsmp-hub/mit_b3.imagenet 模型在昇腾 NPU 环境下的 encoder 前向推理验证,并与 CPU 推理结果进行特征输出误差对比。本项目面向昇腾 Model-Agent 模型适配大赛赛道一,完成 smp-hub/mit_b3.imagenet 模型在 Ascend NPU 环境下的适配验证。该模型为 segmentation-models-pytorch 中的 mit_b3 ImageNet 预训练 encoder 权重,主要用于语义分割等下游任务中的多尺度图像特征提取。项目基于 PyTorch、segmentation-models-pytorch 和 torch-npu,实现模型加载、测试图片生成、CPU 推理、NPU 推理、encoder 特征输出、CPU/NPU 输出一致性对比以及验证材料整理。
smp-hub/mit_b3.imagenet 是 Hugging Face / smp-hub 提供的 MiT-B3 ImageNet 预训练 encoder 权重。本项目使用固定测试图片 test.jpg 作为输入,分别在 CPU 与 Ascend NPU 上执行 encoder 前向推理,并比较两端输出的多尺度特征是否一致。
由于该模型是 encoder 特征提取模型,不是普通 ImageNet 分类模型,因此本项目不比较分类 Top-5,而是对 encoder 输出的最后一层特征、池化特征以及 Top-5 feature channel 进行一致性验证。
本次适配内容包括:
smp-hub/mit_b3.imagenet 模型;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
├── npu_result.json
├── npu_result.txt
├── 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 推理日志;npu_result.json:NPU 推理结构化结果;npu_result.txt:NPU 推理日志;compare_result.txt:CPU/NPU 对比文本结果;fusion_result.json:汇总结果文件;run.log:完整运行日志;screenshots/:验证截图材料。在 Ascend NPU Notebook 中执行以下命令检查运行环境:
npu-smi info
python --version
python - <<'PY'
import torch
print("torch:", torch.__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 / smp-hub:
smp-hub/mit_b3.imagenet模型加载信息如下:
model: smp-hub/mit_b3.imagenet
encoder_name: mit_b3
encoder_weights: imagenet模型文件下载完成后,推理脚本会加载 MiT-B3 encoder,并执行 CPU/NPU 前向推理。
本项目使用测试图片:
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.txtCPU 推理日志摘要如下:
model: smp-hub/mit_b3.imagenet
encoder_name: mit_b3
encoder_weights: imagenet
device: cpu
image: test.jpg
input_shape: [1, 3, 224, 224]
feature_shapes: [[1, 3, 224, 224], [1, 0, 112, 112], [1, 64, 56, 56], [1, 128, 28, 28], [1, 320, 14, 14], [1, 512, 7, 7]]
last_feature_shape: [512, 7, 7]
pooled_feature_shape: [512]
elapsed_seconds: 0.949306CPU Top-5 feature channel 输出如下:
rank=1, index=469, label=feature_channel_469, score=0.00991715, value=1.82783306
rank=2, index=370, label=feature_channel_370, score=0.00986980, value=1.82304740
rank=3, index=176, label=feature_channel_176, score=0.00923377, value=1.75643563
rank=4, index=239, label=feature_channel_239, score=0.00863632, value=1.68954456
rank=5, index=336, label=feature_channel_336, score=0.00768318, value=1.57260084运行:
python inference.py --device npu --image test.jpg --output npu_result.json 2>&1 | tee npu_result.txtNPU 推理输出文件:
npu_result.json
npu_result.txtNPU 推理日志摘要如下:
model: smp-hub/mit_b3.imagenet
encoder_name: mit_b3
encoder_weights: imagenet
device: npu
image: test.jpg
input_shape: [1, 3, 224, 224]
feature_shapes: [[1, 3, 224, 224], [1, 0, 112, 112], [1, 64, 56, 56], [1, 128, 28, 28], [1, 320, 14, 14], [1, 512, 7, 7]]
last_feature_shape: [512, 7, 7]
pooled_feature_shape: [512]
elapsed_seconds: 13.256840NPU Top-5 feature channel 输出如下:
rank=1, index=469, label=feature_channel_469, score=0.00998318, value=1.83459377
rank=2, index=370, label=feature_channel_370, score=0.00988211, value=1.82441854
rank=3, index=176, label=feature_channel_176, score=0.00922776, value=1.75590825
rank=4, index=239, label=feature_channel_239, score=0.00864738, value=1.69094896
rank=5, index=336, label=feature_channel_336, score=0.00765425, value=1.56895328NPU 推理结果截图保存为:
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 的 encoder 特征输出,并计算:
对比结果保存为:
compare_result.txt本次 smp-hub/mit_b3.imagenet 适配验证的 CPU/NPU 误差结果如下:
| 指标 | 结果 |
|---|---|
| CPU Top-5 feature channels | [469, 370, 176, 239, 336] |
| NPU Top-5 feature channels | [469, 370, 176, 239, 336] |
| CPU/NPU Top-1 是否一致 | True |
| CPU/NPU Top-5 顺序是否一致 | True |
| CPU/NPU Top-5 集合是否一致 | True |
| last feature 最大绝对误差 | 0.0450406075 |
| last feature 平均绝对误差 | 0.0034910999 |
| last feature 余弦相似度 | 0.9999889135 |
| pooled feature 最大绝对误差 | 0.0077773333 |
| pooled feature 平均绝对误差 | 0.0018430050 |
| pooled feature 余弦相似度 | 0.9999940395 |
| probability 最大绝对误差 | 0.0000660308 |
| probability 平均绝对误差 | 0.0000036911 |
| Top-5 probability 最大相对误差 | 0.0066582435 |
| 验证结论 | NPU inference passed |
对应的 compare_result.txt 内容如下:
CPU/NPU comparison for Ascend NPU adaptation
model: smp-hub/mit_b3.imagenet
encoder_name: mit_b3
encoder_weights: imagenet
cpu_result: cpu_result.json
npu_result: npu_result.json
Feature-channel Top-k consistency:
CPU top5 feature channels: [469, 370, 176, 239, 336]
NPU top5 feature channels: [469, 370, 176, 239, 336]
top1_same: True
top5_order_same: True
top5_set_same: True
Numerical metrics:
last_feature_max_abs: 0.0450406075
last_feature_mean_abs: 0.0034910999
last_feature_cosine_similarity: 0.9999889135
pooled_feature_max_abs: 0.0077773333
pooled_feature_mean_abs: 0.0018430050
pooled_feature_cosine_similarity: 0.9999940395
probability_max_abs: 0.0000660308
probability_mean_abs: 0.0000036911
top5_probability_max_relative_error: 0.0066582435
Conclusion:
NPU inference passed. CPU/NPU encoder feature outputs are consistent and Top-5 relative error is below 1%.根据上述结果,CPU 与 NPU 的 Top-1 feature channel 一致,Top-5 feature channel 顺序完全一致,Top-5 集合一致。last feature 余弦相似度达到 0.9999889135,pooled feature 余弦相似度达到 0.9999940395,probability 最大绝对误差为 0.0000660308,Top-5 probability 最大相对误差为 0.0066582435,低于 1% 阈值。因此,本次 smp-hub/mit_b3.imagenet 昇腾 NPU encoder 推理验证通过。

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

该截图展示 NPU 推理日志,包括模型名称、运行设备、输入图片、输入张量形状、多尺度 feature shapes、last feature shape、pooled feature shape 和 Top-5 feature channel。

该截图展示 CPU/NPU encoder 特征输出误差对比结果,包括 Top-K 一致性、last feature 误差、pooled feature 误差、probability 误差和余弦相似度。
本项目提交材料包括:
README.mdadaptation_report.mddownload_model.shmake_test_image.pyinference.pycompare_cpu_npu.pyrequirements.txttest.jpgcpu_result.jsoncpu_result.txtnpu_result.jsonnpu_result.txtcompare_result.txtfusion_result.jsonrun.logscreenshots/npu_env.pngscreenshots/npu_result.pngscreenshots/compare_result.png完整运行日志可查看:
run.logCPU 推理日志可查看:
cpu_result.txtNPU 推理日志可查看:
npu_result.txtCPU/NPU 误差对比结果可查看:
compare_result.txt适配报告可查看:
adaptation_report.md本项目的适配工作包括:
smp-hub/mit_b3.imagenet 模型;本项目完成了 smp-hub/mit_b3.imagenet 模型在 Ascend NPU 环境下的 encoder 特征提取推理适配验证。
验证结果表明,NPU 推理能够正常完成模型加载、图像预处理和多尺度特征输出。CPU 与 NPU 的 Top-5 feature channel 顺序完全一致,last feature 余弦相似度达到 0.9999889135,pooled feature 余弦相似度达到 0.9999940395,Top-5 probability 最大相对误差为 0.0066582435,低于 1% 阈值,并通过验证。本项目可作为赛道一模型适配提交材料。