cafeai/cafe_styleanime、real_life、3d、manga_like、othercafeai/cafe_style 模型在昇腾 NPU 环境下的图像风格分类推理验证,并与 CPU 推理结果进行误差对比。本项目面向昇腾 Model-Agent 模型适配大赛赛道一,完成 cafeai/cafe_style 图像风格分类模型在 Ascend NPU 环境下的适配验证。项目基于 PyTorch、Transformers 和 torch-npu,实现模型加载、测试图片生成、CPU 推理、NPU 推理、Top-K 分类输出、CPU/NPU 输出一致性对比以及验证材料整理。
cafeai/cafe_style 是 Hugging Face 上的图像风格分类模型,用于判断输入图像属于 anime、real_life、3d、manga_like 或 other 等风格类别。本项目使用固定测试图片 test.jpg 作为输入,分别在 CPU 与 Ascend NPU 上执行推理,并比较两端输出 logits 与 probability 是否一致。
本次适配内容包括:
cafeai/cafe_style 模型;test.jpg;.
├── README.md
├── download_model.sh
├── make_test_image.py
├── find_stable_test_image.py
├── inference.py
├── compare_cpu_npu.py
├── requirements.txt
├── test.jpg
├── stable_seed.txt
├── stable_search_result.txt
├── 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:项目说明文档;download_model.sh:模型下载脚本;make_test_image.py:测试图片生成脚本;find_stable_test_image.py:稳定测试图片搜索脚本;stable_seed.txt:稳定测试图片种子记录;stable_search_result.txt:稳定测试图片搜索结果;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:
cafeai/cafe_style模型文件下载完成后,推理脚本会加载模型并执行 CPU/NPU 前向推理。
本项目使用测试图片:
test.jpg当前版本使用稳定测试图片重新生成了 CPU/NPU 推理结果。相关稳定性搜索记录保存在:
stable_seed.txt
stable_search_result.txt推理流程包括:
cafeai/cafe_style 图像风格分类模型;输入张量形状为:
[1, 3, 384, 384]运行:
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: cafeai/cafe_style
device: cpu
image: test.jpg
input_shape: [1, 3, 384, 384]
num_labels: 5
top_k: 5
elapsed_seconds: 5.982582CPU Top-5 输出如下:
rank=1, index=4, label=other, score=0.81790006, logit=2.42794585
rank=2, index=3, label=manga_like, score=0.08801279, logit=0.19868772
rank=3, index=0, label=anime, score=0.03414356, logit=-0.74822038
rank=4, index=1, label=real_life, score=0.03345937, logit=-0.76846230
rank=5, index=2, label=3d, score=0.02648420, logit=-1.00224626运行:
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: cafeai/cafe_style
device: npu
image: test.jpg
input_shape: [1, 3, 384, 384]
num_labels: 5
top_k: 5
elapsed_seconds: 9.243773NPU Top-5 输出如下:
rank=1, index=4, label=other, score=0.81819558, logit=2.42795730
rank=2, index=3, label=manga_like, score=0.08774902, logit=0.19533660
rank=3, index=0, label=anime, score=0.03384041, logit=-0.75748819
rank=4, index=1, label=real_life, score=0.03365098, logit=-0.76310194
rank=5, index=2, label=3d, score=0.02656407, logit=-0.99958438NPU 推理结果截图保存为:
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 的 logits 和 probability 输出,并计算:
对比结果保存为:
compare_result.txt本次 cafeai/cafe_style 适配验证的 CPU/NPU 误差结果如下:
| 指标 | 结果 |
|---|---|
| 类别数 | 5 |
| Top-K | 5 |
| CPU Top-5 | [4, 3, 0, 1, 2] |
| NPU Top-5 | [4, 3, 0, 1, 2] |
| CPU Top-1 | other |
| NPU Top-1 | other |
| CPU/NPU Top-1 是否一致 | True |
| CPU/NPU Top-5 顺序是否一致 | True |
| CPU/NPU Top-5 集合是否一致 | True |
| logits 最大绝对误差 | 0.0092678070 |
| logits 平均绝对误差 | 0.0041305246 |
| logits 余弦相似度 | 0.9999918938 |
| probability 最大绝对误差 | 0.0003031418 |
| probability 平均绝对误差 | 0.0002267852 |
| Top-K probability 最大相对误差 | 0.0088784476 |
| 验证结论 | NPU inference passed |
对应的 compare_result.txt 内容如下:
CPU/NPU comparison for Ascend NPU adaptation
model: cafeai/cafe_style
cpu_result: cpu_result.json
npu_result: npu_result.json
num_labels: 5
top_k: 5
Top-k consistency:
CPU top5: [4, 3, 0, 1, 2]
NPU top5: [4, 3, 0, 1, 2]
top1_same: True
top5_order_same: True
top5_set_same: True
Numerical metrics:
logit_max_abs: 0.0092678070
logit_mean_abs: 0.0041305246
logit_cosine_similarity: 0.9999918938
probability_max_abs: 0.0003031418
probability_mean_abs: 0.0002267852
top5_probability_max_relative_error: 0.0088784476
Conclusion:
NPU inference passed. CPU/NPU Top-k results are consistent and Top-k relative error is below 1%.根据上述结果,CPU 与 NPU 的 Top-1 结果均为 other,Top-5 顺序完全一致,Top-5 集合一致。logits 最大绝对误差为 0.0092678070,probability 最大绝对误差为 0.0003031418,logits 余弦相似度达到 0.9999918938,Top-K probability 最大相对误差为 0.0088784476,低于 1% 阈值。因此,本次 cafeai/cafe_style 昇腾 NPU 推理验证通过。

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

该截图展示 NPU 推理日志,包括模型名称、运行设备、输入图片、输入张量形状和 Top-5 分类结果。

该截图展示 CPU/NPU 输出误差对比结果,包括 Top-K 一致性、logits 误差、probability 误差和余弦相似度。
本项目提交材料包括:
README.mddownload_model.shmake_test_image.pyfind_stable_test_image.pyinference.pycompare_cpu_npu.pyrequirements.txttest.jpgstable_seed.txtstable_search_result.txtcpu_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本项目的适配工作包括:
cafeai/cafe_style 模型;本项目完成了 cafeai/cafe_style 模型在 Ascend NPU 环境下的图像风格分类推理适配验证。
验证结果表明,NPU 推理能够正常完成模型加载、图像预处理和 Top-5 分类输出。CPU 与 NPU 的 Top-1 结果均为 other,Top-5 顺序完全一致,logits 余弦相似度达到 0.9999918938,probability 最大绝对误差为 0.0003031418,Top-K probability 最大相对误差为 0.0088784476,低于 1% 阈值,并通过验证。本项目可作为赛道一模型适配提交材料。