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

dima806/man_woman_face_image_detection 昇腾 NPU 适配

1. 模型信息

  • 模型名称:dima806/man_woman_face_image_detection
  • 模型来源:Hugging Face / Transformers
  • 模型类型:图像二分类模型
  • 任务类型:人脸图像 man / woman 分类
  • 输出类别数:2
  • 标签类别:man、woman
  • 推理框架:PyTorch + Transformers + torch-npu
  • 运行设备:Ascend NPU
  • 适配目标:完成 dima806/man_woman_face_image_detection 模型在昇腾 NPU 环境下的图像分类推理验证,并与 CPU 推理结果进行误差对比。

本项目面向昇腾 Model-Agent 模型适配大赛赛道一,完成 dima806/man_woman_face_image_detection 图像分类模型在 Ascend NPU 环境下的适配验证。项目基于 PyTorch、Transformers 和 torch-npu,实现模型加载、测试图片生成、CPU 推理、NPU 推理、分类输出、CPU/NPU 输出一致性对比以及验证材料整理。

2. 项目说明

dima806/man_woman_face_image_detection 是 Hugging Face 上的图像二分类模型,用于根据输入人脸图像输出 man 或 woman 分类结果。本项目使用固定测试图片 test.jpg 作为输入,分别在 CPU 与 Ascend NPU 上执行推理,并比较两端输出 logits 与 probability 是否一致。

本次适配内容包括:

  1. 在 Ascend NPU Notebook 环境中安装依赖;
  2. 从 Hugging Face 下载并加载 dima806/man_woman_face_image_detection 模型;
  3. 构造测试图片 test.jpg;
  4. 分别执行 CPU 与 NPU 图像分类推理;
  5. 保存 CPU/NPU 的 logits、probability 和 Top-2 分类结果;
  6. 对 CPU 与 NPU 输出进行误差对比;
  7. 保存日志、截图和适配报告,用于赛道一模型适配验证提交。

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
├── cpu_infer.log
├── npu_result.json
├── npu_result.txt
├── npu_infer.log
├── 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 推理日志;
  • cpu_infer.log:CPU 推理补充日志;
  • npu_result.json:NPU 推理结构化结果;
  • npu_result.txt:NPU 推理日志;
  • npu_infer.log: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:

dima806/man_woman_face_image_detection

模型文件下载完成后,推理脚本会加载模型并执行 CPU/NPU 前向推理。

6. 测试输入

本项目使用测试图片:

test.jpg

推理流程包括:

  1. 读取测试图片;
  2. 使用模型对应的图像预处理流程;
  3. 构造输入张量;
  4. 输入 dima806/man_woman_face_image_detection 图像二分类模型;
  5. 输出 2 维分类 logits;
  6. 计算 softmax probability;
  7. 输出 Top-2 分类结果。

输入张量形状为:

[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_infer.log

CPU 推理日志摘要如下:

model: dima806/man_woman_face_image_detection
device: cpu
image: test.jpg
input_shape: [1, 3, 224, 224]
output_shape: [1, 2]
elapsed_seconds: 1.827008
num_labels: 2
prediction: class_id=1, label=woman, prob=0.53836143, logit=0.08858444

CPU Top-2 输出如下:

rank 1: class_id=1, label=woman, logit=0.08858444, prob=0.53836143
rank 2: class_id=0, label=man, logit=-0.06516350, prob=0.46163854

CPU logits 统计信息如下:

min: -0.06516350
max: 0.08858444
mean: 0.01171047
std: 0.07687397

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_infer.log

NPU 推理日志摘要如下:

model: dima806/man_woman_face_image_detection
device: npu
image: test.jpg
input_shape: [1, 3, 224, 224]
output_shape: [1, 2]
elapsed_seconds: 13.874904
num_labels: 2
prediction: class_id=1, label=woman, prob=0.53837645, logit=0.08950491

NPU Top-2 输出如下:

rank 1: class_id=1, label=woman, logit=0.08950491, prob=0.53837645
rank 2: class_id=0, label=man, logit=-0.06430329, prob=0.46162361

NPU logits 统计信息如下:

min: -0.06430329
max: 0.08950491
mean: 0.01260081
std: 0.07690410

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 的 logits 和 probability 输出,并计算:

  • CPU/NPU 输出形状;
  • CPU 预测类别;
  • NPU 预测类别;
  • CPU/NPU 预测标签是否一致;
  • CPU Top-2;
  • NPU Top-2;
  • CPU/NPU Top-1 是否一致;
  • CPU/NPU Top-2 顺序是否一致;
  • Top-2 重合数量;
  • logits 最大绝对误差;
  • logits 平均绝对误差;
  • RMSE;
  • logits 最大相对误差;
  • logits 平均相对误差;
  • logits 余弦相似度;
  • probability 最大绝对误差;
  • probability 平均绝对误差;
  • 是否通过验证。

对比结果保存为:

compare_result.txt

10. 自验证结果

本次 dima806/man_woman_face_image_detection 适配验证的 CPU/NPU 误差结果如下:

指标结果
CPU 输出形状(2,)
NPU 输出形状(2,)
CPU 预测类别woman
NPU 预测类别woman
CPU 预测概率0.5383614301681519
NPU 预测概率0.5383764505386353
CPU Top-2[1, 0]
NPU Top-2[1, 0]
CPU/NPU 预测标签是否一致True
CPU/NPU Top-1 是否一致True
CPU/NPU Top-2 顺序是否一致True
Top-2 重合数量2/2
logits 最大绝对误差0.0009204671
logits 平均绝对误差0.0008903407
RMSE0.0008908502
logits 最大相对误差0.0103908440
logits 平均相对误差0.0115818224
logits 余弦相似度0.9999368694
probability 最大绝对误差0.0000150204
probability 平均绝对误差0.0000149757
是否通过验证True

对应的 compare_result.txt 内容如下:

CPU/NPU comparison result
==================================================
model: dima806/man_woman_face_image_detection
cpu_result: cpu_result.json
npu_result: npu_result.json

cpu_output_shape: (2,)
npu_output_shape: (2,)

cpu_prediction: {'class_id': 1, 'label': 'woman', 'prob': 0.5383614301681519, 'logit': 0.08858443796634674}
npu_prediction: {'class_id': 1, 'label': 'woman', 'prob': 0.5383764505386353, 'logit': 0.0895049050450325}
same_pred_label: True

cpu_top2: [1, 0]
npu_top2: [1, 0]
same_top1: True
same_top_order: True
top_overlap: 2/2

max_abs_error: 0.0009204671
mean_abs_error: 0.0008903407
rmse: 0.0008908502
max_relative_error: 0.0103908440
mean_relative_error: 0.0115818224
cosine_similarity: 0.9999368694

max_prob_abs_error: 0.0000150204
mean_prob_abs_error: 0.0000149757

passed: True

根据上述结果,CPU 与 NPU 的预测标签均为 woman,Top-2 顺序完全一致,Top-2 重合数量为 2/2。logits 最大绝对误差为 0.0009204671,probability 最大绝对误差为 0.0000150204,logits 余弦相似度达到 0.9999368694,验证结果为 passed: True。因此,本次 dima806/man_woman_face_image_detection 昇腾 NPU 推理验证通过。

11. 验证截图材料

11.1 NPU 环境截图

npu_env

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

11.2 NPU 推理结果截图

npu_result

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

11.3 CPU/NPU 误差对比截图

compare_result

该截图展示 CPU/NPU 输出误差对比结果,包括预测标签一致性、Top-2 一致性、最大绝对误差、平均绝对误差、RMSE、相对误差和余弦相似度。

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

NPU 推理日志可查看:

npu_result.txt
npu_infer.log

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

compare_result.txt

适配报告可查看:

adaptation_report.md

13. 适配说明

本项目的适配工作包括:

  1. 在 Ascend NPU 环境中完成依赖安装;
  2. 编写 Hugging Face / Transformers 模型下载脚本;
  3. 加载 dima806/man_woman_face_image_detection 模型;
  4. 编写测试图片生成脚本;
  5. 编写 CPU/NPU 统一推理脚本;
  6. 支持测试图片输入和模型图像预处理;
  7. 保存 CPU 与 NPU 的结构化分类结果;
  8. 编写 CPU/NPU logits 和 probability 误差对比脚本;
  9. 计算预测标签一致性、Top-2 顺序一致性、最大绝对误差、平均绝对误差、RMSE、相对误差和余弦相似度;
  10. 输出完整日志、截图和适配报告。

14. 结论

本项目完成了 dima806/man_woman_face_image_detection 模型在 Ascend NPU 环境下的图像二分类推理适配验证。

验证结果表明,NPU 推理能够正常完成模型加载、图像预处理和二分类输出。CPU 与 NPU 的预测标签均为 woman,Top-2 顺序完全一致,logits 最大绝对误差为 0.0009204671,probability 最大绝对误差为 0.0000150204,logits 余弦相似度达到 0.9999368694,并通过验证。本项目可作为赛道一模型适配提交材料。