1aurent/vit_small_patch8_224.lunit_dino本项目用于验证 1aurent/vit_small_patch8_224.lunit_dino 模型在 Ascend NPU 环境下的推理流程。项目完成了模型权重加载、测试图片输入、CPU 推理、NPU 推理、图像特征提取、CPU/NPU 输出误差对比以及验证材料整理。
vit_small_patch8_224.lunit_dino 是基于 Vision Transformer 的图像特征提取模型,使用 DINO 自监督学习方式进行预训练。该模型输入一张图像后,输出一个 384 维图像特征向量,可用于图像检索、聚类、分类下游任务或视觉表征分析。
本次适配重点包括:
model.safetensors 权重完成模型加载;test.jpg;.
├── 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/:验证截图材料。在 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:
1aurent/vit_small_patch8_224.lunit_dino模型权重文件保存到:
./model/model.safetensors推理日志中显示:
Loading weight: ./model/model.safetensors
Missing keys: 0
Unexpected keys: 0说明模型权重已经成功加载,且权重字段与模型结构匹配。
本项目使用测试图片:
test.jpg推理流程包括:
运行:
python inference.py --device cpu --image test.jpg --output cpu_result.json 2>&1 | tee cpu_result.txtCPU 推理输出文件:
cpu_result.json
cpu_result.txtCPU 推理日志摘要如下:
Loading weight: ./model/model.safetensors
Using device: cpu
Missing keys: 0
Unexpected keys: 0CPU 输出特征信息:
feature_shape: [384]
feature_l2_norm: 49.987586975097656
feature_mean: -0.011639314703643322
feature_std: 2.5542197227478027运行:
python inference.py --device npu --image test.jpg --output npu_result.json 2>&1 | tee npu_result.txtNPU 推理输出文件:
npu_result.json
npu_result.txtNPU 推理日志摘要如下:
Loading weight: ./model/model.safetensors
Using device: npu:0
Missing keys: 0
Unexpected keys: 0NPU 输出特征信息:
feature_shape: [384]
feature_l2_norm: 49.98368453979492
feature_mean: -0.011552532203495502
feature_std: 2.554020643234253NPU 推理结果截图保存为:
screenshots/npu_result.png日志末尾出现的:
path string is NULLpath string is NULL属于 torch-npu / CANN 环境中的提示信息,不影响前面的模型加载、NPU 推理和结果保存。本次验证已经成功获得 NPU 输出特征。
运行:
python compare_cpu_npu.py --cpu cpu_result.json --npu npu_result.json 2>&1 | tee compare_result.txt对比脚本会读取 CPU 与 NPU 的图像特征输出,并计算:
对比结果保存为:
compare_result.txt本次 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 推理验证通过。

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

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

该截图展示 CPU/NPU 输出特征误差对比结果,包括最大绝对误差、平均绝对误差、最大相对误差百分比和余弦相似度。
本项目提交材料包括:
README.mdadaptation_report.mddownload_model.shinference.pycompare_cpu_npu.pyrequirements.txttest.jpgcpu_result.jsoncpu_result.txtnpu_result.jsonnpu_result.txtcompare_result.txtrun.logscreenshots/npu_env.pngscreenshots/npu_result.pngscreenshots/compare_result.png完整运行日志可查看:
run.logCPU 推理日志可查看:
cpu_result.txtNPU 推理日志可查看:
npu_result.txtCPU/NPU 误差对比结果可查看:
compare_result.txt本项目的适配工作包括:
model.safetensors 权重加载模型;本项目完成了 1aurent/vit_small_patch8_224.lunit_dino 模型在 Ascend NPU 环境下的图像特征提取推理适配验证。
验证结果表明,NPU 推理能够正常完成模型权重加载、图像预处理和 384 维图像特征输出。CPU 与 NPU 的输出特征维度一致,最大相对误差百分比为 0.1777122343020191,特征余弦相似度达到 0.9999970197677612,并通过 1% 阈值验证。本项目可作为赛道一模型适配提交材料。