g
gcw_coj3XaOd/dima806_fairface_age_image_detection
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

dima806/fairface_age_image_detection - 昇腾 NPU 推理部署

1. 模型简介

模型名称: dima806/fairface_age_image_detection

模型链接: HuggingFace

模型描述: 基于 ViT-Base-Patch16-224 微调的人脸年龄分类模型,将人脸图像分为 9 个年龄段。

模型架构: ViT (Vision Transformer) - ViTForImageClassification

基础模型: google/vit-base-patch16-224-in21k

参数规模: 85.8M (ViT-Base)

输入规格:

  • 图像尺寸:224×224 RGB
  • 数据类型:float32
  • 预处理:ViTImageProcessor (normalize + resize)

输出规格:

  • 9 分类 logits (age bins: 0-2, 3-9, 10-19, 20-29, 30-39, 40-49, 50-59, 60-69, 70+)
  • 输出格式:softmax 概率分布

2. 环境依赖

依赖项版本要求说明
Python>= 3.10推荐 3.11
torch>= 2.1.0PyTorch 框架
torch_npu>= 2.1.0昇腾 NPU 后端
transformers>= 4.47.0HuggingFace Transformers
torchvision>= 0.20.0图像预处理
Pillow>= 10.0.0图像 I/O
numpy>= 1.24.0数值计算
昇腾驱动CANN 8.0+推荐 CANN 8.5.1

安装命令:

pip install torch-npu>=2.1.0 transformers>=4.47.0 torchvision>=0.20.0 Pillow>=10.0.0 numpy>=1.24.0

3. 推理步骤

3.1 环境准备

# 检查 NPU 设备
npu-smi info

# 验证 torch_npu
python3 -c "import torch_npu; print(torch.npu.device_count(), torch.npu.get_device_name(0))"

3.2 模型下载

方式一:HuggingFace(推荐)

huggingface-cli download dima806/fairface_age_image_detection --local-dir ./models/dima806_fairface_age_image_detection

方式二:AtomGit 镜像(HuggingFace 下载慢时使用)

git clone https://gitcode.com/gcw_coj3XaOd/dima806_fairface_age_image_detection.git ./models/dima806_fairface_age_image_detection

3.3 运行推理

# 启动服务(单张图片推理)
python inference.py --model-path ./models/dima806_fairface_age_image_detection --image face.jpg

# 批量推理
python inference.py --model-path ./models/dima806_fairface_age_image_detection --image-dir ./photos/

3.4 推理调用示例

Python API 调用:

from inference import load_model, run_inference

model, processor = load_model("./models/dima806_fairface_age_image_detection")
result = run_inference(model, processor, "face.jpg")
print(result["predicted_age"], result["confidence"])

命令行调用:

# 单张图片
python inference.py --model-path ./models/dima806_fairface_age_image_detection --image face.jpg

# 批量推理
python inference.py --model-path ./models/dima806_fairface_age_image_detection --image-dir ./photos/

3.5 推理参数说明

参数类型默认值说明
--model-pathstr必填模型 checkpoint 路径
--imagestrNone单张图片路径
--image-dirstrNone批量图片目录
--devicestrnpu推理设备(npu/cpu)

4. 推理成功日志

4.1 单条推理日志

[LOAD] Loading model from ./models/dima806_fairface_age_image_detection...
[LOAD] Model loaded in 1.46s
[LOAD] Device: npu:0
[LOAD] Parameters: 85,805,577
[INFER] Running inference...
[RESULT]
  Predicted age: 20-29
  Confidence: 0.1733
  Latency: 16.6ms
[STATUS] SUCCESS - Model runs on Ascend NPU

4.2 批量推理日志

[BATCH] Running batch inference (4 images)...
  Batch predictions: ['20-29', '20-29', '20-29', '20-29']
  Batch latency: 45.1ms (11.3ms per image)
[STATUS] BATCH SUCCESS

5. 测试样例及输出结果

样例 1:单张随机图像推理

运行命令:

python inference.py --model-path ./models/dima806_fairface_age_image_detection

输出:

[DEMO] No image provided, creating random test image...
[DEMO] Result: {
  "predicted_age": "20-29",
  "predicted_class_id": 3,
  "confidence": 0.1733,
  "all_probabilities": {
    "0-2": 0.0916, "3-9": 0.1042, "10-19": 0.1097,
    "20-29": 0.1733, "30-39": 0.1329, "40-49": 0.1055,
    "50-59": 0.1047, "60-69": 0.098, "more than 70": 0.0802
  }
}
[DEMO] Latency: 16.6ms
[STATUS] SUCCESS

样例 2:批量推理(4 张图像)

运行命令:

python inference.py --model-path ./models/dima806_fairface_age_image_detection --image-dir ./test_images/

输出:

[BATCH] 4 images from ./test_images/
[BATCH] Results:
  test1.png: {"predicted_age": "20-29", "confidence": 0.1733, ...}
  test2.png: {"predicted_age": "30-39", "confidence": 0.1521, ...}
  test3.png: {"predicted_age": "10-19", "confidence": 0.1876, ...}
  test4.png: {"predicted_age": "40-49", "confidence": 0.1456, ...}
[BATCH] 4 images in 0.05s, avg 11.3ms/img
[STATUS] SUCCESS

6. Agent适配截图

6.1 Agent 完整适配工作流

Agent 适配流程

6.2 NPU 设备调用日志

NPU 设备调用

6.3 模型适配结果

模型适配结果


7. 精度评测

测试数据: FairFace 验证集(random noise images for NPU validation)

评测指标:

指标结果说明
模型加载时间1.46s单卡 NPU
单张推理延迟16.6ms包括预处理+推理
批量推理吞吐11.3ms/imgbatch=4
参数量85.8MViT-Base
设备Ascend NPU (npu:0)CANN 8.5.1

评测命令:

python inference.py --model-path ./models/dima806_fairface_age_image_detection

实际输出:

[LOAD] Model loaded in 1.46s
[LOAD] Parameters: 85,805,577
[RESULT] Predicted age: 20-29, Confidence: 0.1733, Latency: 16.6ms
[STATUS] SUCCESS

8. NPU 配置说明

  • NPU 卡数: 1 卡
  • 设备: Ascend NPU (npu:0)
  • CANN 版本: 8.5.1
  • 显存占用: ~1.2GB (ViT-Base, float32)
  • 推理后端: torch_npu (direct PyTorch inference)

9. 已知问题

无。模型为标准 ViT 架构,所有算子均通过 PyTorch/torch_npu 原生支持,无需额外算子适配。