K-ON111111/fairface_age_image_detection
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

fairface_age_image_detection — 昇腾 NPU 适配模型

赛事: 昇腾 Model Agent 模型适配大赛(第二季) 引擎: torch_npu 硬件: Ascend 910B4 标签: npu ascend pytorch transformers

📖 模型简介

属性值
原始模型dima806/fairface_age_image_detection
参数量86 M
任务类型人脸年龄识别(9 个年龄区间)
输入格式人脸图像(224x224)
输出格式年龄区间 + 置信度

基于 ViT(vit-base-patch16-224)在 FairFace 数据集上微调的人脸年龄识别模型, 将人脸图像分类到 9 个年龄区间:0-2 / 3-9 / 10-19 / 20-29 / 30-39 / 40-49 / 50-59 / 60-69 / more than 70。本适配在昇腾 NPU(Ascend 910B4)上完成推理。

🔧 环境依赖

⚠️ 本仓库不包含模型权重(交付件仅 inference.py / readme.md / requirements.txt / assets/), 推理前必须先从原始模型仓单独下载权重。

# 1. 下载模型权重(仓库不含权重,必须单独下载)
huggingface-cli download dima806/fairface_age_image_detection --local-dir ./ --local-dir-use-symlinks False

# 2. 安装依赖
pip install -r requirements.txt -i https://repo.huaweicloud.com/repository/pypi/simple/

🚀 推理步骤

# 运行 NPU 推理(人脸图像 -> 年龄区间)
python3 inference.py --model_path . --input test_face.jpg --device npu:0

🧪 测试用例

用例 1: 人脸年龄识别

python3 inference.py --model_path . --input test_face.jpg --device npu:0

预期输出:

[Model Agent] NPU 设备就绪: Ascend910B4 (count=1)
[Model Agent] 模型加载完成 -> npu:0 (ViTForImageClassification)
[Model Agent] 预测年龄区间: 20-29 (置信度 0.2655)
  top1: 20-29 0.2655
  top2: 30-39 0.2112
  top3: 10-19 0.1265
[Model Agent] 推理时延: 5567.3 ms

📁 目录结构

.
├── inference.py          # NPU 推理脚本
├── readme.md             # 本文件
├── requirements.txt      # 依赖清单
└── assets/               # 截图素材

📝 适配说明

本模型由 昇腾 Model Agent 自动完成 NPU 适配:

  • 代码迁移: 标准 ViTForImageClassification + AutoImageProcessor, model.to('npu:0') + torch.no_grad() 推理,无 CUDA 依赖。
  • 验证: Ascend 910B4 上推理,年龄区间输出合法,单次约 5.6 s。