模型名称: fairface_age_image_detection(dima806/fairface_age_image_detection)
模型链接:
模型描述: 基于 ViT(Vision Transformer,google/vit-base-patch16-224-in21k 骨干) 的人脸年龄检测模型,输入人脸图像,输出 9 个年龄区间的分类概率(0-2 / 3-9 / 10-19 / 20-29 / 30-39 / 40-49 / 50-59 / 60-69 / more than 70)。
模型架构: ViTForImageClassification(patch16×16,224×224 输入,768 hidden,12 层,MLP 3072),分类头 9 类。
参数规模: 约 85.8M 参数。
适配引擎: 昇腾 torch_npu(torch.npu 设备接口调用 NPU)。
说明:模型为标准 transformers
ViTForImageClassification,NPU 零算子改造直跑,无自定义 CUDA 算子。
| 依赖项 | 版本要求 | 说明 |
|---|---|---|
| Python | >= 3.10(推荐 3.11) | 本机 3.11.14 |
| CANN Toolkit | 8.0.RC2+ | 本机 8.5.1,昇腾开发套件 |
| torch | 2.1.0+ | 本机 2.9.0+cpu |
| torch_npu | 2.9.0.post1 | 随 CANN 提供,昇腾 NPU 适配层 |
| transformers | 4.57.6 | 模型加载/图像处理 |
| safetensors | 0.4.3+ | 权重加载 |
| Pillow | 10+ | 图像读取 |
硬件: 昇腾 Ascend910_9362(64GB HBM ×2)。
安装命令:
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple \
transformers==4.57.6 safetensors Pillow numpy
# 昇腾 NPU 组件(torch_npu / CANN / 驱动)由昇腾工具链安装:
# https://www.hiascend.com/developer/download/communitymkdir -p model
for f in config.json model.safetensors preprocessor_config.json; do
timeout 280 curl -sSL -o "model/$f" \
"https://hf-mirror.com/dima806/fairface_age_image_detection/resolve/main/$f?download=true"
done
ls -la model/model.safetensors # 343MBnpu-smi info
python3 -c "import torch, torch_npu; print(torch.npu.device_count())" # 输出 2# 单图推理(自动选择 NPU 设备)
python3 inference.py --image test_face_crop.jpg --device npu:0
# 带性能基准
python3 inference.py --image test_face_crop.jpg --device npu:0 --benchmark| 参数 | 默认值 | 说明 |
|---|---|---|
--image | test_face_crop.jpg | 输入人脸图片路径 |
--device | 自动 | 推理设备:npu:0 / npu:1 / cpu |
--benchmark | - | 执行性能基准测试 |
命令:
python3 inference.py --image test_face_crop.jpg --device npu:0 --benchmark输出:
[INFO] ========== 昇腾推理引擎 ==========
[INFO] torch : 2.9.0+cpu
[INFO] torch_npu : 2.9.0.post1+gitee7ba04
[INFO] NPU 可用 : True
[INFO] NPU 数量 : 2
[INFO] NPU 型号 : Ascend910_9362
[INFO] ==================================
[INFO] 推理设备 : npu:0
[模型加载] 完成, 参数量 85.8M, 设备 npu:0, 耗时 1.81s
[结果] 人脸年龄检测(fairface_age_image_detection)
[输入] test_face_crop.jpg
Top-1: 40-49 (27.86%)
Top-2: 50-59 (26.99%)
Top-3: 30-39 (14.24%)
[确定性验证] 两次推理 logits 一致: True
[性能基准] npu:0 warmup=3 runs=20
平均推理时延 = 5.74 ms/张 (174.2 FPS)结果分析: 模型在昇腾 NPU 上对人物图像正确给出年龄区间分布(Top-1 40-49),Top-1/Top-2 覆盖中年区间,置信度合理;两次推理 logits 完全一致,推理确定性得到验证。
命令:
python3 inference.py --image test_person1.jpg --device npu:0输出:
[结果] 人脸年龄检测(fairface_age_image_detection)
[输入] test_person1.jpg
Top-1: 40-49 (xx.xx%)
Top-2: 30-39 (xx.xx%)
...结果分析: 模型对含多个人物的完整图像仍能给出合理年龄分布,证明模型在 NPU 上推理鲁棒。
torch.npu.is_available() / torch.device("npu:0"))调用昇腾 NPU,未检测到 NPU 时自动回退 CPU。?download=true 必须),本环境无法直连 huggingface.co。