z
zhangyuge147/dima806-fairface_age_image_detection-NPU
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

dima806/fairface_age_image_detection on Ascend NPU

1. 模型简介

  • 模型名称: dima806/fairface_age_image_detection
  • 权重URL: https://huggingface.co/dima806/fairface_age_image_detection
  • 架构: ViTForImageClassification (google/vit-base-patch16-224-in21k 微调)
  • 模型类型: vit
  • 任务: 人脸年龄段 9 分类 (0-2, 3-9, 10-19, 20-29, 30-39, 40-49, 50-59, 60-69, more than 70)
  • 处理器: ViTImageProcessor (size 224x224, mean=[0.5,0.5,0.5], std=[0.5,0.5,0.5])
  • 权重大小: 343 MB (model.safetensors, ~86M params)
  • 精度: float32
  • 设备: npu:0 (Ascend910_9362) 单卡, batch size 1
  • 代码: inference.py 默认使用本地 HF cache 加载, 无需运行时下载

FairFace 年龄检测模型将人脸图像分类到 9 个年龄段, 适用于人脸分析、人口统计、内容审核等场景。基于 Vision Transformer backbone, 输入 224x224 RGB, 输出 9 类 logits 经 softmax 得概率。

2. 验证环境

  • NPU: Ascend910_9362 x2, CANN 8.5.1
  • npu-smi: 25.5.5
  • torch: 2.9.0+cpu
  • torch_npu: 2.9.0.post1+gitee7ba04
  • torch.npu.is_available(): True
  • 显存: 61.27 GiB total, 推理峰值 ~7.4 GiB used (含模型 327 MiB 参数 + 激活)
  • Python: 3.11.14
  • transformers: 4.47.0
  • Pillow: 10.4.0

真实 npu-smi info 与 torch.npu 探测见 assets/npu_device_call.png。

3. 安装依赖

pip install -r requirements.txt
# 已在 NPU 镜像中预装 torch_npu, CANN 驱动需匹配

requirements.txt:

torch>=2.9.0
torch-npu>=2.9.0
transformers>=4.47.0
Pillow>=9.0.0
numpy>=1.24.0
safetensors>=0.4.0

4. NPU 推理

默认命令 (使用本地权重, 确定性 dummy 输入, 真实 NPU 执行):

python inference.py

其他模式:

python inference.py --mode infer       # 仅 NPU 推理
python inference.py --mode validate    # CPU vs NPU 一致性
python inference.py --mode benchmark   # 性能测试
python inference.py --model-dir /path/to/local/snapshot  # 指定权重目录

脚本自动打印: 模型名、任务、后端、device、dtype、输入摘要、真实 logits/probs、耗时、PASS/FAIL。

5. 真实推理结果

使用 seed=42 的确定性 224x224 随机图像 (pixel mean ~127) 在 npu:0 上推理:

Device: npu:0
Model first param device: npu:0 dtype: torch.float32
Input: dummy image 224x224x3, seed=42

=== NPU Inference ===
Inference time: 218.80 ms (首次含编译) -> 稳定 6.47 ms (见性能节)
Logits: [-0.2437 -0.2671 -0.1643  0.2667  0.1143 -0.0968 -0.0972 -0.2080 -0.4223]
Probs (softmax): [0.0967 0.0944 0.1047 0.1610 0.1383 0.1120 0.1119 0.1002 0.0809]
  3:        20-29  prob=0.1610 logit=0.2667  <- Predicted
  4:        30-39  prob=0.1383 logit=0.1143
  5:        40-49  prob=0.1120 logit=-0.0968
  6:        50-59  prob=0.1119 logit=-0.0972
  2:        10-19  prob=0.1047 logit=-0.1643
  7:        60-69  prob=0.1002 logit=-0.2080
  0:          0-2  prob=0.0967 logit=-0.2437
  1:          3-9  prob=0.0944 logit=-0.2671
  8: more than 70  prob=0.0809 logit=-0.4223
Predicted: 3 -> 20-29 (prob=0.1610)
Output shape: torch.Size([1, 9])

完整输出见 assets/model_result.png。首次推理含 NPU 编译开销 (~218ms), 后续稳定推理见第 7 节。

6. CPU-NPU 一致性验证

同一输入、同一权重、float32, 对比 CPU 与 NPU logits:

=== CPU-NPU Consistency Validation ===
CPU logits: [-0.2434 -0.2652 -0.1652  0.2673  0.1119 -0.0981 -0.0943 -0.2082 -0.4211]
NPU logits: [-0.2437 -0.2671 -0.1643  0.2667  0.1143 -0.0968 -0.0972 -0.2080 -0.4223]
CPU pred: 3 -> 20-29
NPU pred: 3 -> 20-29
Top-1 match: True
Max abs diff: 0.002863
Mean abs diff: 0.001285
Cosine similarity: 0.999980
Consistency: PASS
  • 验证类型: 单样本确定性一致性 (smoke consistency), 非完整数据集评测
  • 指标: Top-1 一致, 最大绝对误差 <0.01, 余弦 >0.999
  • 结论: PASS, CPU/NPU 数值高度一致, 差异来自浮点累积误差, 分类结果完全一致

说明: 单样本一致性用于验证 NPU 数值正确性, 不代表在 FairFace 全量测试集上的精度评测。

7. 性能测试

所有计时在 torch.npu.synchronize() 前后, warmup 3 次, 正式 10 次, batch=1, 224x224, float32:

=== Performance Benchmark ===
Warmup: 3, Iters: 10, Batch: 1, Image: 224x224, Dtype: float32
  iter 1: 6.32 ms
  iter 2: 6.69 ms
  iter 3: 6.31 ms
  iter 4: 6.60 ms
  iter 5: 6.28 ms
  iter 6: 6.60 ms
  iter 7: 6.36 ms
  iter 8: 6.61 ms
  iter 9: 6.33 ms
  iter 10: 6.61 ms
Avg: 6.47 ms | Min: 6.28 ms | Max: 6.69 ms | p50: 6.48 ms | p90: 6.62 ms | p95: 6.65 ms
Throughput: 154.54 images/s (batch=1)
NPU memory after benchmark: free 53.88 GiB / total 61.27 GiB used 7.39 GiB
  • 首次推理: 218.80 ms (含图编译/算子缓存)
  • 稳定推理: 6.47 ms avg
  • 吞吐: 154.5 images/s
  • 峰值显存: ~7.39 GiB (batch 1)

8. 自验证截图

  • assets/agent_workflow.png - 阶段日志: 下载、侦察、NPU 推理、一致性、性能
  • assets/npu_device_call.png - npu-smi info + torch.npu 设备探测 + 模型参数在 npu:0
  • assets/model_result.png - python inference.py 真实输出、结果与 PASS

9. 已知限制

  • 输入固定 224x224, 不支持可变分辨率 (ViT patch embedding 限制)
  • 仅验证单卡 npu:0, 未测试多卡并行
  • 精度固定 float32, 未量化为 FP16/BF16 (ViT-B/16 可进一步优化)
  • 验证为单样本一致性, 非 FairFace 测试集全量评测
  • 权重通过 HuggingFace cache 加载, 首次需下载 343 MB (hf-mirror.com)
  • 模型仅做年龄段分类, 需上游人脸检测裁剪

10. 标签

Hardware: NPU, NPU, Ascend, Ascend910, ViT, Image Classification, Age Detection, FairFace