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

vit_small_patch8_224.lunit_dino 昇腾 NPU 适配

1. 模型信息

  • 模型名称:1aurent/vit_small_patch8_224.lunit_dino
  • 模型来源:Hugging Face / timm
  • 模型类型:图像特征提取模型
  • 模型结构:Vision Transformer Small Patch8
  • 预训练方式:DINO 自监督学习
  • 输出特征维度:384
  • 推理框架:PyTorch + timm + torch-npu
  • 运行设备:Ascend NPU
  • 适配目标:完成 ViT-DINO 模型在昇腾 NPU 环境下的图像特征提取推理验证,并与 CPU 推理结果进行误差对比。

本项目用于验证 1aurent/vit_small_patch8_224.lunit_dino 模型在 Ascend NPU 环境下的推理流程。项目完成了模型权重加载、测试图片输入、CPU 推理、NPU 推理、图像特征提取、CPU/NPU 输出误差对比以及验证材料整理。

2. 项目说明

vit_small_patch8_224.lunit_dino 是基于 Vision Transformer 的图像特征提取模型,使用 DINO 自监督学习方式进行预训练。该模型输入一张图像后,输出一个 384 维图像特征向量,可用于图像检索、聚类、分类下游任务或视觉表征分析。

本次适配重点包括:

  1. 在 Ascend NPU Notebook 环境中加载 ViT-DINO 模型;
  2. 使用本地 model.safetensors 权重完成模型加载;
  3. 生成或读取测试图片 test.jpg;
  4. 分别执行 CPU 与 NPU 图像特征提取;
  5. 保存 CPU/NPU 结构化推理结果;
  6. 对 CPU 与 NPU 输出特征进行误差对比;
  7. 保存日志、截图和适配报告,用于赛道一模型适配验证提交。

3. 工程结构

.
├── README.md
├── adaptation_report.md
├── download_model.sh
├── 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
├── run.log
└── screenshots/
    ├── npu_env.png
    ├── npu_result.png
    └── compare_result.png

其中:

  • download_model.sh:模型下载脚本;
  • inference.py:CPU/NPU 推理脚本;
  • compare_cpu_npu.py:CPU/NPU 输出误差对比脚本;
  • cpu_result.json:CPU 推理结构化结果;
  • cpu_result.txt:CPU 推理日志;
  • npu_result.json:NPU 推理结构化结果;
  • npu_result.txt:NPU 推理日志;
  • compare_result.txt:CPU/NPU 误差对比结果;
  • run.log:完整运行日志;
  • adaptation_report.md:适配报告;
  • 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:

1aurent/vit_small_patch8_224.lunit_dino

模型权重文件保存到:

./model/model.safetensors

推理日志中显示:

Loading weight: ./model/model.safetensors
Missing keys: 0
Unexpected keys: 0

说明模型权重已经成功加载,且权重字段与模型结构匹配。

6. 测试输入

本项目使用测试图片:

test.jpg

推理流程包括:

  1. 读取测试图片;
  2. 对图片进行 timm 预处理;
  3. 构造模型输入张量;
  4. 输入 ViT-DINO 模型;
  5. 输出 384 维图像特征向量;
  6. 保存结构化特征结果。

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

Loading weight: ./model/model.safetensors
Using device: cpu
Missing keys: 0
Unexpected keys: 0

CPU 输出特征信息:

feature_shape: [384]
feature_l2_norm: 49.987586975097656
feature_mean: -0.011639314703643322
feature_std: 2.5542197227478027

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

Loading weight: ./model/model.safetensors
Using device: npu:0
Missing keys: 0
Unexpected keys: 0

NPU 输出特征信息:

feature_shape: [384]
feature_l2_norm: 49.98368453979492
feature_mean: -0.011552532203495502
feature_std: 2.554020643234253

NPU 推理结果截图保存为:

screenshots/npu_result.png

日志末尾出现的:

path string is NULLpath string is NULL

属于 torch-npu / CANN 环境中的提示信息,不影响前面的模型加载、NPU 推理和结果保存。本次验证已经成功获得 NPU 输出特征。

9. CPU/NPU 误差对比

运行:

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

对比脚本会读取 CPU 与 NPU 的图像特征输出,并计算:

  • 输出特征维度;
  • 最大绝对误差;
  • 平均绝对误差;
  • CPU 输出最大绝对值;
  • 最大相对误差百分比;
  • CPU/NPU 特征余弦相似度;
  • 是否通过 1% 阈值验证。

对比结果保存为:

compare_result.txt

10. 自验证结果

本次 vit_small_patch8_224.lunit_dino 适配验证的 CPU/NPU 误差结果如下:

指标结果
输出特征维度[384]
最大绝对误差0.025088608264923096
平均绝对误差0.004773363005369902
CPU 输出最大绝对值14.117547035217285
最大相对误差百分比0.1777122343020191
CPU/NPU 特征余弦相似度0.9999970197677612
是否通过 1% 阈值true

对应的 compare_result.txt 内容如下:

{
  "feature_shape": [
    384
  ],
  "max_abs_diff": 0.025088608264923096,
  "mean_abs_diff": 0.004773363005369902,
  "cpu_max_abs": 14.117547035217285,
  "max_rel_diff_percent": 0.1777122343020191,
  "cosine_similarity": 0.9999970197677612,
  "pass_1_percent_threshold": true
}

根据上述结果,CPU 与 NPU 的输出特征维度一致,最大相对误差百分比为 0.1777122343020191,低于 1% 阈值;特征余弦相似度达到 0.9999970197677612。因此,本次 ViT-DINO 昇腾 NPU 推理验证通过。

11. 验证截图材料

11.1 NPU 环境截图

npu_env

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

11.2 NPU 推理结果截图

npu_result

该截图展示 NPU 推理日志,包括模型权重加载、运行设备、输入图片、输出特征形状和特征摘要信息。

11.3 CPU/NPU 误差对比截图

compare_result

该截图展示 CPU/NPU 输出特征误差对比结果,包括最大绝对误差、平均绝对误差、最大相对误差百分比和余弦相似度。

12. 运行日志与提交材料

本项目提交材料包括:

  • README.md
  • adaptation_report.md
  • download_model.sh
  • 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
  • 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

13. 适配说明

本项目的适配工作包括:

  1. 在 Ascend NPU 环境中完成 ViT-DINO 依赖安装;
  2. 编写模型下载脚本;
  3. 使用本地 model.safetensors 权重加载模型;
  4. 编写 CPU/NPU 统一推理脚本;
  5. 支持测试图片输入和 timm 图像预处理;
  6. 保存 CPU 与 NPU 的结构化特征输出结果;
  7. 编写 CPU/NPU 特征误差对比脚本;
  8. 计算最大绝对误差、平均绝对误差、最大相对误差百分比和余弦相似度;
  9. 输出完整日志、截图和适配报告。

14. 结论

本项目完成了 1aurent/vit_small_patch8_224.lunit_dino 模型在 Ascend NPU 环境下的图像特征提取推理适配验证。

验证结果表明,NPU 推理能够正常完成模型权重加载、图像预处理和 384 维图像特征输出。CPU 与 NPU 的输出特征维度一致,最大相对误差百分比为 0.1777122343020191,特征余弦相似度达到 0.9999970197677612,并通过 1% 阈值验证。本项目可作为赛道一模型适配提交材料。