prithivMLmods/open-age-detectionChild 0-12、Teenager 13-20、Adult 21-44、Middle Age 45-64、Aged 65+prithivMLmods/open-age-detection 模型在昇腾 NPU 环境下的年龄段图像分类推理验证,并与 CPU 推理结果进行误差对比。本项目面向昇腾 Model-Agent 模型适配大赛赛道一,完成 prithivMLmods/open-age-detection 年龄段图像分类模型在 Ascend NPU 环境下的适配验证。项目基于 PyTorch、Transformers 和 torch-npu,实现模型加载、测试图片生成、CPU 推理、NPU 推理、分类输出、CPU/NPU 输出一致性对比以及验证材料整理。
prithivMLmods/open-age-detection 是 Hugging Face 上的年龄段图像分类模型,用于根据输入人物图像估计年龄组。模型输出 5 个年龄段类别,分别为 Child 0-12、Teenager 13-20、Adult 21-44、Middle Age 45-64、Aged 65+。
本项目使用固定测试图片 test.jpg 作为输入,分别在 CPU 与 Ascend NPU 上执行推理,并比较两端输出 logits 与 probability 是否一致。
本次适配内容包括:
prithivMLmods/open-age-detection 模型;test.jpg;.
├── 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/:验证截图材料。在 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:
prithivMLmods/open-age-detection模型文件下载完成后,推理脚本会加载模型并执行 CPU/NPU 前向推理。
本项目使用测试图片:
test.jpg推理流程包括:
prithivMLmods/open-age-detection 年龄段分类模型;输入张量形状为:
[1, 3, 224, 224]运行:
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_infer.logCPU 推理日志摘要如下:
model: prithivMLmods/open-age-detection
device: cpu
image: test.jpg
input_shape: [1, 3, 224, 224]
output_shape: [1, 5]
elapsed_seconds: 2.210237
num_labels: 5
prediction: class_id=2, label=Adult 21-44, prob=0.39242312, logit=1.46203494CPU Top-5 输出如下:
rank 1: class_id=2, label=Adult 21-44, logit=1.46203494, prob=0.39242312
rank 2: class_id=0, label=Child 0-12, logit=1.20922554, prob=0.30476201
rank 3: class_id=3, label=Middle Age 45-64, logit=0.69469684, prob=0.18218133
rank 4: class_id=1, label=Teenager 13-20, logit=0.10532619, prob=0.10105167
rank 5: class_id=4, label=Aged 65+, logit=-1.53570437, prob=0.01958182CPU logits 统计信息如下:
min: -1.53570437
max: 1.46203494
mean: 0.38711581
std: 1.06794798运行:
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_infer.logNPU 推理日志摘要如下:
model: prithivMLmods/open-age-detection
device: npu
image: test.jpg
input_shape: [1, 3, 224, 224]
output_shape: [1, 5]
elapsed_seconds: 13.692967
num_labels: 5
prediction: class_id=2, label=Adult 21-44, prob=0.39268222, logit=1.46300626NPU Top-5 输出如下:
rank 1: class_id=2, label=Adult 21-44, logit=1.46300626, prob=0.39268222
rank 2: class_id=0, label=Child 0-12, logit=1.20915020, prob=0.30464423
rank 3: class_id=3, label=Middle Age 45-64, logit=0.69433546, prob=0.18205884
rank 4: class_id=1, label=Teenager 13-20, logit=0.10564665, prob=0.10105259
rank 5: class_id=4, label=Aged 65+, logit=-1.53639579, prob=0.01956219NPU logits 统计信息如下:
min: -1.53639579
max: 1.46300626
mean: 0.38714853
std: 1.06834328NPU 推理结果截图保存为:
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本次 prithivMLmods/open-age-detection 适配验证的 CPU/NPU 误差结果如下:
| 指标 | 结果 |
|---|---|
| CPU 输出形状 | (5,) |
| NPU 输出形状 | (5,) |
| CPU 预测类别 | Adult 21-44 |
| NPU 预测类别 | Adult 21-44 |
| CPU 预测概率 | 0.3924231231212616 |
| NPU 预测概率 | 0.3926822245121002 |
| CPU Top-5 | [2, 0, 3, 1, 4] |
| NPU Top-5 | [2, 0, 3, 1, 4] |
| CPU/NPU 预测标签是否一致 | True |
| CPU/NPU Top-1 是否一致 | True |
| CPU/NPU Top-5 顺序是否一致 | True |
| Top-5 重合数量 | 5/5 |
| logits 最大绝对误差 | 0.0009713173 |
| logits 平均绝对误差 | 0.0004839823 |
| RMSE | 0.0005762772 |
| logits 最大相对误差 | 0.0006324898 |
| logits 平均相对误差 | 0.0004833068 |
| logits 余弦相似度 | 0.9999999281 |
| probability 最大绝对误差 | 0.0002591014 |
| probability 平均绝对误差 | 0.0001039833 |
| 是否通过验证 | True |
对应的 compare_result.txt 内容如下:
CPU/NPU comparison result
==================================================
model: prithivMLmods/open-age-detection
cpu_result: cpu_result.json
npu_result: npu_result.json
cpu_output_shape: (5,)
npu_output_shape: (5,)
cpu_prediction: {'class_id': 2, 'label': 'Adult 21-44', 'prob': 0.3924231231212616, 'logit': 1.4620349407196045}
npu_prediction: {'class_id': 2, 'label': 'Adult 21-44', 'prob': 0.3926822245121002, 'logit': 1.4630062580108643}
same_pred_label: True
cpu_top5: [2, 0, 3, 1, 4]
npu_top5: [2, 0, 3, 1, 4]
same_top1: True
same_top_order: True
top_overlap: 5/5
max_abs_error: 0.0009713173
mean_abs_error: 0.0004839823
rmse: 0.0005762772
max_relative_error: 0.0006324898
mean_relative_error: 0.0004833068
cosine_similarity: 0.9999999281
max_prob_abs_error: 0.0002591014
mean_prob_abs_error: 0.0001039833
passed: True根据上述结果,CPU 与 NPU 的预测标签均为 Adult 21-44,Top-5 顺序完全一致,Top-5 重合数量为 5/5。logits 最大绝对误差为 0.0009713173,probability 最大绝对误差为 0.0002591014,logits 余弦相似度达到 0.9999999281,验证结果为 passed: True。因此,本次 prithivMLmods/open-age-detection 昇腾 NPU 推理验证通过。

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

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

该截图展示 CPU/NPU 输出误差对比结果,包括预测标签一致性、Top-5 一致性、最大绝对误差、平均绝对误差、RMSE、相对误差和余弦相似度。
本项目提交材料包括:
README.mdadaptation_report.mddownload_model.shmake_test_image.pyinference.pycompare_cpu_npu.pyrequirements.txttest.jpgcpu_result.jsoncpu_result.txtcpu_infer.lognpu_result.jsonnpu_result.txtnpu_infer.logcompare_result.txtfusion_result.jsonrun.logscreenshots/npu_env.pngscreenshots/npu_result.pngscreenshots/compare_result.png完整运行日志可查看:
run.logCPU 推理日志可查看:
cpu_result.txt
cpu_infer.logNPU 推理日志可查看:
npu_result.txt
npu_infer.logCPU/NPU 误差对比结果可查看:
compare_result.txt适配报告可查看:
adaptation_report.md本项目的适配工作包括:
prithivMLmods/open-age-detection 模型;本项目完成了 prithivMLmods/open-age-detection 模型在 Ascend NPU 环境下的年龄段图像分类推理适配验证。
验证结果表明,NPU 推理能够正常完成模型加载、图像预处理和 5 类年龄段分类输出。CPU 与 NPU 的预测标签均为 Adult 21-44,Top-5 顺序完全一致,logits 最大绝对误差为 0.0009713173,probability 最大绝对误差为 0.0002591014,logits 余弦相似度达到 0.9999999281,并通过验证。本项目可作为赛道一模型适配提交材料。