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

smp-hub/mit_b3.imagenet 昇腾 NPU 适配

1. 模型信息

  • 模型名称:smp-hub/mit_b3.imagenet
  • 模型来源:Hugging Face / smp-hub
  • 模型类型:图像特征提取模型 / Encoder 模型
  • Encoder 名称:mit_b3
  • Encoder 权重:imagenet
  • 推理框架:PyTorch + segmentation-models-pytorch + torch-npu
  • 运行设备:Ascend NPU
  • 适配目标:完成 smp-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 输出一致性对比以及验证材料整理。

2. 项目说明

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 进行一致性验证。

本次适配内容包括:

  1. 在 Ascend NPU Notebook 环境中安装依赖;
  2. 从 Hugging Face / smp-hub 加载 smp-hub/mit_b3.imagenet 模型;
  3. 构造测试图片 test.jpg;
  4. 分别执行 CPU 与 NPU encoder 前向推理;
  5. 保存 CPU/NPU 的多尺度特征输出;
  6. 对最后一层特征和池化特征进行误差对比;
  7. 对 Top-5 feature channel 结果进行一致性验证;
  8. 保存日志、截图和适配报告,用于赛道一模型适配验证提交。

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
├── 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/:验证截图材料。

4. 环境检查

在 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 版本信息。

5. 模型下载

运行:

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 前向推理。

6. 测试输入

本项目使用测试图片:

test.jpg

推理流程包括:

  1. 读取测试图片;
  2. 对图片进行模型要求的预处理;
  3. 构造输入张量;
  4. 输入 MiT-B3 encoder;
  5. 输出多尺度特征;
  6. 提取最后一层特征;
  7. 对最后一层特征进行池化,得到 512 维 pooled feature;
  8. 基于 pooled feature 输出 Top-5 feature channel;
  9. 对 CPU 与 NPU 输出进行误差对比。

输入张量形状为:

[1, 3, 224, 224]

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 推理日志摘要如下:

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.949306

CPU 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

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 推理日志摘要如下:

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.256840

NPU 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.56895328

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 的 encoder 特征输出,并计算:

  • CPU Top-5 feature channel;
  • NPU Top-5 feature channel;
  • CPU/NPU Top-1 是否一致;
  • CPU/NPU Top-5 顺序是否一致;
  • CPU/NPU Top-5 集合是否一致;
  • last feature 最大绝对误差;
  • last feature 平均绝对误差;
  • last feature 余弦相似度;
  • pooled feature 最大绝对误差;
  • pooled feature 平均绝对误差;
  • pooled feature 余弦相似度;
  • probability 最大绝对误差;
  • probability 平均绝对误差;
  • Top-5 probability 最大相对误差;
  • 最终验证结论。

对比结果保存为:

compare_result.txt

10. 自验证结果

本次 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 推理验证通过。

11. 验证截图材料

11.1 NPU 环境截图

npu_env

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

11.2 NPU 推理结果截图

npu_result

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

11.3 CPU/NPU 误差对比截图

compare_result

该截图展示 CPU/NPU encoder 特征输出误差对比结果,包括 Top-K 一致性、last feature 误差、pooled feature 误差、probability 误差和余弦相似度。

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
  • npu_result.json
  • npu_result.txt
  • 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

NPU 推理日志可查看:

npu_result.txt

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

compare_result.txt

适配报告可查看:

adaptation_report.md

13. 适配说明

本项目的适配工作包括:

  1. 在 Ascend NPU 环境中完成依赖安装;
  2. 编写 Hugging Face / smp-hub 模型下载脚本;
  3. 加载 smp-hub/mit_b3.imagenet 模型;
  4. 编写测试图片生成脚本;
  5. 编写 CPU/NPU 统一推理脚本;
  6. 支持测试图片输入和模型图像预处理;
  7. 保存 CPU 与 NPU 的多尺度 encoder 特征输出;
  8. 提取 last feature 与 pooled feature;
  9. 基于 pooled feature 计算 Top-5 feature channel;
  10. 编写 CPU/NPU 特征误差对比脚本;
  11. 计算 Top-1 一致性、Top-5 顺序一致性、last feature 误差、pooled feature 误差、probability 误差和余弦相似度;
  12. 输出完整日志、截图和适配报告。

14. 结论

本项目完成了 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% 阈值,并通过验证。本项目可作为赛道一模型适配提交材料。