wrhhh/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
  • 基础模型: google/vit-base-patch16-224-in21k
  • 架构: ViTForImageClassification (ViT-B/16, 86M params, 12 layers, hidden 768)
  • 任务: 图像分类 - 年龄组预测,9 类:0-2, 3-9, 10-19, 20-29, 30-39, 40-49, 50-59, 60-69, more than 70
  • 输入: RGB 图像 224x224, ViTImageProcessor (rescale 1/255, normalize mean 0.5 std 0.5)
  • 输出: logits [1,9] -> softmax probs + argmax
  • 精度: float32
  • 原始论文/卡片: 基于 FairFace 数据集训练,验证集 accuracy ~58.92%
  • 本仓已在单卡 Ascend910B (npu:0) 上完成真实权重推理与 CPU-NPU 一致性验证。

2. 验证环境

  • NPU: Ascend910_9362 (Ascend910B) x2, 64GB HBM each
  • npu-smi: 25.5.5
  • CANN: 8.5.1
  • torch: 2.9.0+cpu
  • torch_npu: 2.9.0.post1+gitee7ba04
  • transformers: 4.47.0
  • Python: 3.11.14
  • 设备: npu:0
  • HF_ENDPOINT: https://hf-mirror.com (镜像直连,本地加载)

3. 安装依赖

pip install -r requirements.txt
# requirements.txt 包含:
# torch==2.9.0
# torch-npu==2.9.0.post1
# transformers==4.47.0
# pillow>=10.0.0
# numpy>=1.24.0

4. NPU 推理

本地权重路径优先: /opt/atomgit/adapt-npu-agent/working/fairface_model_final (由 checkpoint-9376 整合),若不存在则自动尝试 dima806/fairface_age_image_detection 的 snapshot_download 缓存。

默认命令 (NPU):

python inference.py

其他模式:

python inference.py --mode validate   # 一致性验证
python inference.py --mode benchmark  # 性能压测

模型加载: ViTForImageClassification.from_pretrained(local_dir).to("npu:0"), dtype float32, 无 trust_remote_code。

5. 真实推理结果

基于固定随机种子 42 生成的合成肤色人脸图像 (224x224) 的真实 NPU 推理输出 (本次执行):

model_dir: /opt/atomgit/adapt-npu-agent/working/fairface_model_final
processor: ViTImageProcessor size={'height': 224, 'width': 224} mean=[0.5, 0.5, 0.5]
input: synthetic face image 224x224 RGB, pixel_values shape [1,3,224,224]
CPU logits: [-0.3259, 0.0325, 0.0690, 0.2547, 0.2786, 0.0106, -0.2148, -0.2940, -0.5244]
NPU logits: [-0.3266, 0.0322, 0.0686, 0.2543, 0.2808, 0.0127, -0.2158, -0.2966, -0.5259]
CPU probs: [0.0840, 0.1202, 0.1247, 0.1501, 0.1537, 0.1176, 0.0938, 0.0867, 0.0688]
NPU probs: [0.0839, 0.1201, 0.1246, 0.1500, 0.1541, 0.1178, 0.0937, 0.0865, 0.0687]
NPU pred: 4 -> 30-39 (prob 0.1541)

Top-9 排序 (NPU):

1. 30-39 : 0.1541 (logit 0.2808)
2. 20-29 : 0.1500 (logit 0.2543)
3. 10-19 : 0.1246 (logit 0.0686)
4. 3-9 : 0.1201 (logit 0.0322)
5. 40-49 : 0.1178 (logit 0.0127)
6. 50-59 : 0.0937 (logit -0.2158)
7. 60-69 : 0.0865 (logit -0.2966)
8. 0-2 : 0.0839 (logit -0.3266)
9. more than 70 : 0.0687 (logit -0.5259)

输出契约: logits shape [1,9], probs shape [1,9], 任务专属为分类 Top-9 排序与 argmax。

6. CPU-NPU 一致性验证

  • 方法: 同一输入、同一权重、固定种子,分别在 CPU 与 npu:0 上前向,对比 logits
  • 指标: max_abs_diff, mean_abs_diff, pred_match
  • 本次结果:
    • max_abs_diff (logits): 0.002669
    • mean_abs_diff (logits): 0.001262
    • pred_match: True (both 30-39)
    • 阈值: max_diff < 0.01 且 pred 一致 => PASS
    • 结论: PASS (单样本 smoke consistency, 非全量数据集评测)
  • 说明: 误差来自 NPU 算子融合与 float32 数值差异,符合预期。

7. 性能测试

测试条件: batch_size=1, image 224x224, dtype float32, 设备 npu:0, 预热 3 次, 正式 10 次, 计时区间包含 torch.npu.synchronize()。

本次结果:

NPU latency (single): 6.83 ms
avg: 6.73 ms
min: 6.62 ms
max: 6.93 ms
p50: 6.69 ms
p90: 6.84 ms
p95: 6.88 ms
throughput: 148.68 images/s
CPU latency: 689.32 ms (对比, 单次)
NPU speedup vs CPU: ~102x
npu_mem_free: 61908.6 MB, total: 62740.0 MB
peak weight memory: ~328 MB (model.safetensors)

环境差异会导致小幅波动,但量级应在 5-10ms。

8. 自验证截图

三张证据图由 scripts/render_xterm_evidence.mjs 从真实日志渲染 (raw 模式):

  • assets/agent_workflow.png - 下载、侦察、加载、NPU 推理、验证与性能全流程
  • assets/npu_device_call.png - npu-smi、torch.npu.is_available、device_name、显存信息
  • assets/model_result.png - python inference.py 真实输出、排序、一致性与性能

9. 已知限制

  • 权重未包含在仓库,需通过 HuggingFace snapshot_download 下载 (约 328MB),已整合 checkpoint-9376 为最终权重 (best_metric 1.001)
  • 合成随机图像用于固定可复现验证,非真实人脸;真实人脸需自行提供 224x224 裁剪
  • 仅验证 batch=1, FP32;FP16/BF16 未测试
  • 评估为单样本一致性,非 FairFace 全量 10000 张评测;完整评测需数据集授权
  • NPU 编译首次耗时略高,已通过 warmup 排除

10. 标签

  • Hardware: NPU
  • NPU
  • Ascend
  • Ascend910
  • vision
  • image-classification
  • vit
  • fairface

生成时间: 2026-08-21 适配脚本: inference.py (支持 default/validate/benchmark) 仓库类型: model (公开)