w
gcw_uQ09W7jl/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 (Vision Transformer, patch 16, image 224, hidden 768, 12 layers, 12 heads, intermediate 3072, encoder stride 16)
  • 任务: 年龄组图像分类 9 类 [0-2, 3-9, 10-19, 20-29, 30-39, 40-49, 50-59, 60-69, more than 70]
  • 数据集: nateraw/fairface (FairFace)
  • 官方精度: 准确率 58.92% (macro F1 0.5459) 在 10k 测试集,见模型卡报告
  • 输入: 3x224x224 RGB, ViTImageProcessor (rescale 1/255, normalize mean 0.5 std 0.5, resize 224)
  • 输出: logits [1,9] + softmax 概率,Top-1 年龄组标签
  • 权重格式: safetensors (343 MB, 单文件 model.safetensors), torch_dtype float32, transformers 4.47.0
  • 代码: transformers.AutoModel / ViTForImageClassification, ViTImageProcessor, 无 trust_remote_code
  • 适配说明: 本仓在单卡 Ascend910 (npu:0, 64GB) 上完成真实权重加载与推理验证,未替换目标模型,不提交权重

2. 验证环境

  • NPU: Ascend910_9362 x2 (芯片 3, Phy-ID 6/7) / npu-smi 25.5.5 OK
  • OS: Linux aarch64, CANN 8.5.1
  • Python: 3.11.14
  • torch: 2.9.0+cpu, torch_npu: 2.9.0.post1+gitee7ba04, torch.npu.is_available()==True, device_count=2, device_name Ascend910_9362, npu:0 可用
  • transformers: 4.47.0, safetensors, pillow, numpy, huggingface_hub
  • HF_ENDPOINT: https://hf-mirror.com (环境变量镜像)
  • 模型目录: /tmp/dima806_fairface_age (snapshot_download 本地加载)
  • 显存: HBM 65536 MB, free ~61851 MB, 权重 FP32 328 MB 需求充足

3. 安装依赖

pip install torch==2.9.0 torch-npu==2.9.0.post1 transformers==4.47.0 pillow numpy safetensors huggingface_hub
# 或
pip install -r requirements.txt

requirements.txt 内容:

torch==2.9.0
torch-npu==2.9.0.post1
transformers==4.47.0
pillow
numpy
safetensors
huggingface_hub

4. NPU 推理

默认命令 (自动使用 npu:0, 本地权重若存在否则从 HF Hub 缓存):

python inference.py

其他模式:

python inference.py --mode infer --dtype float32 --device npu:0
python inference.py --mode validate   # CPU-NPU 一致性
python inference.py --mode benchmark  # 性能 (warmup 3, iters 10, torch.npu.synchronize)

示例输出见第5节。

5. 真实推理结果

以下为本次在 npu:0 上的真实默认推理输出 (synthetic deterministic face 224x224, seed 42, 单图 batch=1, float32):

Model: dima806/fairface_age_image_detection
Task: image-classification (age group 9-way)
Loading backend: transformers.ViTForImageClassification
Source: /tmp/dima806_fairface_age (local)
Device: npu:0 (param device: npu:0)
Dtype: torch.float32 (requested: float32)
Labels: ['0-2', '3-9', '10-19', '20-29', '30-39', '40-49', '50-59', '60-69', 'more than 70']
Image size: 224x224x3, patch16, hidden 768, 12 layers
Input: synthetic face image 224x224 saved to /tmp/fairface_test_face.png
Input tensor: shape=(1, 3, 224, 224), dtype=torch.float32, mean=0.1210, std=0.2867
Processor: ViTImageProcessor do_resize=True do_normalize=True image_mean=[0.5,0.5,0.5] image_std=[0.5,0.5,0.5] rescale 1/255

--- Outputs ---
logits shape: (1, 9) (num_classes=9)
logits: [-0.1488, 0.4355, 0.4022, 0.4010, -0.0958, -0.2410, -0.3489, -0.3003, -0.5855]
probs (softmax): [0.09476, 0.16998, 0.16441, 0.16423, 0.09992, 0.08642, 0.07758, 0.08145, 0.06124] sum=1.00000
Top-1: class_id=1 label=3-9 score=0.16998
Top-3 ranking:
  1. 3-9 (id=1): 0.16998 <- TOP-1
  2. 10-19 (id=2): 0.16441
  3. 20-29 (id=3): 0.16423
Full ranking:
  1. 3-9: 0.16998
  2. 10-19: 0.16441
  3. 20-29: 0.16423
  4. 30-39: 0.09992
  5. 0-2: 0.09476
  6. 40-49: 0.08642
  7. 60-69: 0.08145
  8. 50-59: 0.07758
  9. more than 70: 0.06124

Inference latency: 259.98 ms (batch=1, sync)  # 首轮含编译
NPU memory: free 61851.3 MB / total 62740.0 MB
NPU torch allocated 328.0 MB reserved 418.0 MB

[PASS] NPU inference successful on npu:0

证据截图: assets/model_result.png 来自同一命令的完整日志 (SHA-256 见图脚).

测试为固定合成人脸确定性样例验证,非完整 FairFace 数据集评测 (完整数据集需 10000 图,本文验证为单样本 smoke consistency)

6. CPU-NPU 一致性验证

相同权重、同一输入、同一预处理分别在 CPU 与 NPU 上运行 validate 模式:

=== CPU-NPU Consistency Validation ===
Preprocess max diff CPU vs NPU input (should be 0): 0.000000
Logits CPU: [-0.1509, 0.4325, 0.4019, 0.4021, -0.0944, -0.2436, -0.3484, -0.2990, -0.5856]
Logits NPU: [-0.1488, 0.4355, 0.4022, 0.4010, -0.0958, -0.2410, -0.3489, -0.3003, -0.5855]
Probs CPU: [0.09462, 0.16956, 0.16446, 0.16448, 0.10011, 0.08624, 0.07766, 0.08159, 0.06126]
Probs NPU: [0.09476, 0.16998, 0.16441, 0.16423, 0.09992, 0.08642, 0.07758, 0.08145, 0.06124]
Logits max AE: 2.987832e-03 mean AE: 1.362180e-03
Probs max AE: 4.199892e-04 mean AE: 1.660126e-04
Cosine similarity (logits): 0.999989
Top-1 CPU: 3-9 (0.16956) vs NPU: 3-9 (0.16998) -> MATCH
Full ranking match: False (2nd/3rd swapped by 0.0002, within tolerance)
Top-3 overlap: 100.0%

Validation: PASS (thresholds: cosine>=0.99 Top-1 match, mae<5e-2 for synthetic)
  • 判定标准 (FP32 单样本): cosine >=0.99, Top-1 必须一致, max AE <5e-2, Top-3 overlap 100%
  • 实际: cosine 0.999989, Top-1 一致, max AE 2.98e-03, 满足
  • 说明: 第2/3 名 (10-19 vs 20-29) 概率差 0.00018 属于数值抖动范围,不影响 Top-1 正确性

7. 性能测试

计时区间前后均调用 torch.npu.synchronize(), warmup 3 次, 正式 10 次, batch=1, 224x224, dtype float32, device npu:0:

=== Benchmark (warmup=3, iters=10) on npu:0 ===
Batch size: 1, Image: 224x224x3, dtype=torch.float32, device=npu:0
Latency avg=6.69 ms min=6.41 max=7.15 p50=6.72 p90=6.87 p95=7.01
Throughput: 149.49 images/s
NPU memory free 61850.8 MB total 62740.0 MB
Benchmark done: PASS
  • 首轮推理 224-259 ms 含 NPU 图编译, 稳定后 6.69 ms (含统计见上)
  • 若包含预处理 (processor) 平均 ~1-2 ms 额外 (已在输入计时外)
  • 大模型建议: FP16 可进一步降低显存与延迟, 本次 FP32 已满足单卡要求故未切换

8. 自验证截图

  • assets/agent_workflow.png - 阶段日志 (下载、侦察、加载、NPU 推理、验证、性能)
  • assets/npu_device_call.png - npu-smi + torch.npu 可用性、设备名、模型参数 npu:0
  • assets/model_result.png - 默认 python inference.py 任务输出与 PASS

三图由 scripts/render_xterm_evidence.mjs --style raw 从本次真实日志渲染, 源日志 SHA-256 显示于图脚。

9. 已知限制

  • 输入仅支持 224x224 RGB (processor 会 resize), 非人脸或极端模糊图像精度可能低于 FairFace 基准 59% (官方测试集中性偏差)
  • 输出为 9 类 softmax,未提供 bounding box 人脸检测,需外部检测器裁剪人脸后送入以获得最佳效果
  • 权重为 FP32 328 MB, NPU FP32 推理已验证; FP16 未在本次提交中切换 (可按需 --dtype float16)
  • 合成测试图为确定性验证样例, 不代表完整 FairFace 基准评测
  • 需 torch_npu 与 CANN 8.5.1 兼容环境, CPU 仅用于一致性对比, 不宣称 NPU 精度为 CPU 基准替代

10. 标签

NPU, Ascend, Ascend910, Vision, Image-Classification, ViT, FairFace, Age-Estimation

硬件声明: hardware: NPU (见文件头 YAML)