microsoft/BiomedCLIP-PubMedBERT_256-vit_base_patch16_224本项目用于验证 BioMedCLIP 模型在 Ascend NPU 环境下的推理流程。项目完成了模型下载、测试图片准备、文本候选类别构建、CPU 推理、NPU 推理、Top-K 分类结果输出、图像特征提取以及 CPU/NPU 输出误差对比。
BioMedCLIP 是面向生物医学场景的图文对比学习模型,能够同时编码医学图像和医学文本,并计算图像与文本之间的相似度。本项目基于 BioMedCLIP 的图像编码器和文本编码器完成推理验证,输入一张测试图片和多组候选文本类别,输出最匹配的 Top-K 文本标签。
本次适配重点包括:
.
├── 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
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:
microsoft/BiomedCLIP-PubMedBERT_256-vit_base_patch16_224如果 Notebook 环境访问 Hugging Face 较慢,可以提前下载模型缓存,或者使用已有缓存继续运行推理脚本。
本项目使用测试图片:
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.txt其中:
cpu_result.json 保存 CPU 结构化推理结果;cpu_result.txt 保存 CPU 推理终端日志。运行:
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_result.json 保存 NPU 结构化推理结果;npu_result.txt 保存 NPU 推理终端日志。NPU 推理结果截图保存为:
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 的推理结果,并对以下内容进行比较:
对比结果保存为:
compare_result.txt本次 BioMedCLIP 适配验证的 CPU/NPU 误差结果如下:
| 指标 | 结果 |
|---|---|
| CPU Top-1 | immunohistochemistry histopathology |
| NPU Top-1 | immunohistochemistry histopathology |
| Top-1 是否一致 | true |
| 概率最大绝对误差 | 0.004524692893028259 |
| 概率平均绝对误差 | 0.0010454049333930016 |
| 概率最大相对误差百分比 | 0.5143578836046665 |
| 图像特征维度 | 512 |
| 图像特征最大绝对误差 | 0.0018687844276428223 |
| 图像特征平均绝对误差 | 0.00022039709438104182 |
| 图像特征最大相对误差百分比 | 1.0085875343270965 |
| 图像特征余弦相似度 | 0.9999788999557495 |
| 是否通过 1% 阈值 | true |
对应的 compare_result.txt 内容如下:
{
"cpu_top1": "immunohistochemistry histopathology",
"npu_top1": "immunohistochemistry histopathology",
"same_top1": true,
"prob_max_abs_diff": 0.004524692893028259,
"prob_mean_abs_diff": 0.0010454049333930016,
"prob_max_rel_diff_percent": 0.5143578836046665,
"image_feature_shape": [
512
],
"image_feature_max_abs_diff": 0.0018687844276428223,
"image_feature_mean_abs_diff": 0.00022039709438104182,
"image_feature_max_rel_diff_percent": 1.0085875343270965,
"image_feature_cosine_similarity": 0.9999788999557495,
"pass_1_percent_threshold": true
}根据上述结果,CPU 与 NPU 的 Top-1 预测结果一致,图像特征余弦相似度达到 0.9999788999557495,概率输出误差和图像特征误差均处于可接受范围内,本次 BioMedCLIP 昇腾 NPU 推理验证通过。

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

该截图展示 NPU 推理日志,包括模型加载、运行设备、测试图片、Top-K 输出和图像特征信息。

该截图展示 CPU/NPU 误差对比结果,包括 Top-1 一致性、概率误差、图像特征误差和余弦相似度。
本项目提交材料包括:
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本项目的适配工作包括:
本项目完成了 BioMedCLIP 模型在 Ascend NPU 环境下的推理适配验证。
验证结果表明,NPU 推理能够正常完成图像特征提取、文本特征匹配和 Top-K 输出。CPU 与 NPU 的 Top-1 结果一致,图像特征余弦相似度达到 0.9999788999557495,误差结果满足验证要求。本项目可作为赛道一模型适配提交材料。