s
stella4528/fairface_age_image_detection-npu
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

FairFace Age Image Detection(NPU 适配版)

基于 dima806/fairface_age_image_detection 的昇腾 NPU 适配推理仓库。模型以人脸图片为输入,输出 9 个年龄组(0-2 / 3-9 / 10-19 / 20-29 / 30-39 / 40-49 / 50-59 / 60-69 / more than 70)的分类概率。

模型信息

项目值
模型来源dima806/fairface_age_image_detection
任务类型单标签图像分类(人脸年龄组检测)
架构ViTForImageClassification(google/vit-base-patch16-224-in21k 微调)
输入人脸图片,resize 至 224×224,RGB,归一化 mean/std = 0.5
输出9 个年龄组的 logits / 概率
参数量85.81M
权重model.safetensors(343.2 MB,float32)
许可证Apache-2.0

环境依赖

见 requirements.txt:transformers>=4.47.0、torch、torch_npu、Pillow、numpy、huggingface_hub。

推理脚本用法

# 1. 使用仓库内本地权重(默认权重目录为仓库根目录)
python inference.py --source ./test_face.jpg --weights-dir . --output ./output.json

# 2. 自动从 HuggingFace 下载权重(HF 直连失败自动切换 hf-mirror.com 镜像)
python inference.py --source ./test_face.jpg

# 3. 指定设备(默认自动检测 npu:0 > cuda:0 > cpu)
python inference.py --source ./test_face.jpg --device npu:0

# 4. 批量目录推理
python inference.py --source ./faces/ --weights-dir . --output ./output.json

参数说明

参数说明
--source输入图片路径或目录(默认 ./test_face.jpg)
--weights-dir本地权重目录(含 config.json / model.safetensors)
--output结果 JSON 输出路径(默认打印 stdout)
--device设备:npu:0 / cuda:0 / cpu(默认自动检测)
--top-k输出 Top-K 年龄组(默认 3)
--no-hf禁止自动从 HuggingFace 下载权重

运行验证(实测,Ascend 910)

项CPUNPU(Ascend 910)
单张推理耗时0.67 s0.18 s
预测结果30-39(Top1)30-39(Top1,与 CPU 一致)
输出有效性概率归一化、finite概率归一化、finite

实测命令(Ascend 910,CANN 8.5.1,torch 2.9 + torch_npu):

python inference.py --source ./test_face.jpg --weights-dir . --device npu:0 --no-hf

输出示例(Top-3):

{
  "predicted_age_group": "30-39",
  "top_k": [
    {"age_group": "30-39", "probability": 0.2103},
    {"age_group": "20-29", "probability": 0.1498},
    {"age_group": "10-19", "probability": 0.1274}
  ],
  "inference_time_s": 0.18
}

注意事项

  • 推理脚本内置 TORCHDYNAMO_DISABLE=1,NPU 上强制 eager 模式,避免 dynamo 图捕获与昇腾算子兼容问题。
  • 权重可从仓库本地加载,也可由脚本自动从 HuggingFace(hf-mirror.com 兜底)下载。
  • 模型为 ViT 架构,输入图片会被自动 resize 至 224×224 并做标准化,无需手工预处理。