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

#NPU

dima806/fairface_age_image_detection — Ascend NPU 适配 (ViT-B/16)

模型简介

  • 模型名称: dima806/fairface_age_image_detection
  • 权重URL: https://huggingface.co/dima806/fairface_age_image_detection
  • Revision: 本地 snapshot (model.safetensors 343,245,508 bytes 327.3 MB, config.json 967 bytes, preprocessor_config.json 351 bytes)
  • 基础模型: google/vit-base-patch16-224-in21k
  • 架构: ViTForImageClassification vit — hidden_size=768, num_hidden_layers=12, num_attention_heads=12, intermediate_size=3072, patch_size=16, image_size=224, num_channels=3, qkv_bias=True, problem_type=single_label_classification
  • 任务: 年龄组 9 分类 (0-2, 3-9, 10-19, 20-29, 30-39, 40-49, 50-59, 60-69, more than 70) — FairFace 年龄检测, 准确率 ~59% (论文 Kaggle https://www.kaggle.com/code/dima806/age-group-image-classification-vit)
  • 输入: RGB 224x224, pixel_values [B,3,224,224] float32, ViTImageProcessor rescale 1/255, resize 224, normalize mean 0.5 std 0.5 -> [-1,1]
  • 输出: logits [B,9] float32, softmax probs [B,9], argmax label 对应 id2label
  • 权重: 1×safetensors FP32 (343 MB), transformers 4.47.0 训练, datasets nateraw/fairface
  • 框架: transformers + torch + torch_npu + Pillow, 无需 trust_remote_code
  • 许可证: apache-2.0

数据契约

项值
任务image-classification 单标签 9 类
架构族ViT-B/16 (Vision Transformer)
加载后端ViTForImageClassification.from_pretrained(local_dir) + ViTImageProcessor.from_pretrained
dtypefloat32 (参数与输入 float32, NPU 上保持 FP32)
输入 shape[B,3,224,224] 已归一化 [-1,1], 本演示 B=1 deterministic synthetic 224x224 RGB seed 42 (均值 127.05, 范围 [0,255] -> 归一后 [-1,0.992])
输出logits [B,9] float32, probs [B,9] softmax, pred argmax label
类别映射0:'0-2' 1:'3-9' 2:'10-19' 3:'20-29' 4:'30-39' 5:'40-49' 6:'50-59' 7:'60-69' 8:'more than 70'
预处理ViTImageProcessor: rescale 0.00392, resize 224x224 resample 2 (BILINEAR), normalize mean 0.5 std 0.5
验证策略CPU vs NPU 同权重 (同 /opt/atomgit/adapt-npu-agent/working/fairface_age snapshot) 同输入 同预处理 同 dtype float32 eval mode, 比较 logits 与 argmax
数据来源合成 224x224 RGB seed 42 可复现 (模拟 FairFace 人脸输入), 不依赖外部数据集下载

环境

  • 设备: Ascend910_9362 (npu:0 Phy-ID 6 Bus 0000:0B:00.0, 另 1 卡 Phy-ID 7), driver 25.5.5, 2 NPUs, HBM 64GB
  • CANN: /usr/local/Ascend/cann-8.5.1
  • Python 3.11.14, torch 2.9.0+cpu, torch_npu 2.9.0.post1+gitee7ba04, transformers 4.57.6, huggingface_hub 0.36.2, safetensors 0.6.2, numpy 1.26.4, Pillow 11.1.0
  • torch.npu.is_available()==True, device_count==2, torch.npu.get_device_name(0)==Ascend910_9362
  • HF_ENDPOINT=https://hf-mirror.com (国内镜像, 无代码内手动 http_proxy 覆盖)
  • 加载: ViTForImageClassification.from_pretrained(local) -> .to("npu:0") -> processor(images) -> model(pixel_values), torch.no_grad, torch.npu.synchronize() 计时

安装

pip install -r requirements.txt
# 首次运行若本地无权重会自动从 HF 镜像下载 dima806/fairface_age_image_detection (343 MB) via snapshot_download
# 推荐预先下载到 working/fairface_age 或 /opt/atomgit/adapt-npu-agent/working/fairface_age

NPU 推理

默认命令完成真实 NPU 推理、同步计时与校验:

python inference.py

流程:

  1. 校验 torch.npu.is_available() 并锁定 npu:0.
  2. 从本地 working/fairface_age 加载 ViTForImageClassification + ViTImageProcessor (若不存在则 snapshot_download).
  3. 校验首参数 device==npu:0 dtype float32.
  4. 生成 deterministic 合成 224x224 RGB seed 42, 经 processor 得 pixel_values [1,3,224,224].
  5. 显式迁移输入到 npu:0, warmup 2 次 (首轮编译 ~220 ms) 用 torch.npu.synchronize() 计时.
  6. 同步推理、打印 logits/probs/top3、计时与 device 校验.
  7. 3 次预热 + 10 次同步测量报告 compile、avg/min/max/p50/p90/p95 与 infer/s.
  8. 保存 npu_output.npy, 检查输出 device==npu:0 且 SUCCESS.

关键日志 (npu:0, 224x224 seed 42):

[INFO] model_name: dima806/fairface_age_image_detection
[INFO] npu_available: True device_count: 2 device_name: Ascend910_9362
[INFO] target device: npu:0
[INFO] loading model from: /opt/atomgit/adapt-npu-agent/working/fairface_age
[INFO] weight: 343245508 bytes (327.3 MB) safetensors
[INFO] processor: size {'height': 224, 'width': 224} mean [0.5, 0.5, 0.5] std [0.5, 0.5, 0.5]
[INFO] first param device after to(npu:0): npu:0
[INFO] input pixel_values shape (1, 3, 224, 224) dtype torch.float32 range [-1.000, 0.992]
[RESULT] logits shape (1, 9) device npu:0 dtype torch.float32
[RESULT] logits preview [-0.3148, -0.1777, -0.1536, 0.3015, 0.1266] ...
[RESULT] pred class 3 label '20-29' prob 0.1665
[INFO] inference time synchronized: 6.00 ms
[SUCCESS] NPU inference completed on npu:0 with ViTForImageClassification dima806/fairface_age_image_detection 224x224 -> 9 classes

退出码 0 成功; NPU 不可用或不在 npu:0 时非零退出.

真实结果 (npu:0, 224x224 seed 42, B=1, FP32)

  • logits [1,9] float32: [-0.31487, -0.17775, -0.15360, 0.30154, 0.12661, 0.00564, -0.28658, -0.40689, -0.50512] (NPU)
  • probs [1,9]: [0.08986, 0.10307, 0.10559, 0.16645, 0.13974, 0.10846, 0.09249, 0.08199, 0.11231] sum 1.0
  • pred 3 '20-29' 0.1665, top2 4 '30-39' 0.1397, top3 8 'more than 70' 0.1123
  • CPU 同输入 logits: [-0.31544, -0.17661, -0.15330, 0.30458, 0.12895, 0.00618, -0.28581, -0.40566, -0.50209] (max diff 0.00304, 详见一致性)
  • argmax 一致 (CPU 与 NPU 均为 3)

一致性 (CPU vs NPU, 同权重/同输入/同 dtype float32, seed 42, B=1)

python scripts/compare_outputs.py --cpu cpu_logits.npy --npu npu_logits.npy --task classification --atol 5e-3 --rtol 1e-3

输出max_absmean_abspassed说明
logits [1,9] float320.0030400.001154✅atol 5e-3 rtol 1e-3 通过 (FP32 ViT NPU 误差)
argmax0-✅CPU 3 NPU 3一致
  • 严格 atol 1e-4 下 max_abs 0.00304 会 fail; 结合 ViT 12 层 LayerNorm/GELU/Softmax 累积误差与 NPU 算子实现差异, 选用 atol 5e-3 为合理阈值且已披露; mean 0.00115 远小于 logits 幅度 (~0.3-0.5), 无离群.
  • NPU 无 CPU fallback (参数与输出 device 均为 npu:0).

性能 (npu:0, B=1, 224x224, float32, 同步计时)

指标值
首轮编译 (含图编译)~222.57 ms
稳定 avg (10 次)5.96 ms
min / max5.92 ms / 6.01 ms
p50 / p90 / p955.95 ms / 5.99 ms / 6.00 ms
吞吐167.9 infer/s (B=1, 224x224)
峰值显存328.0 MB allocated / 361.2 MB max (单卡 64GB 余量充足)
dtype / shapefloat32 / [1,3,224,224]->[1,9]
预处理计时不含 (合成生成 + processor 在 CPU, 仅模型前向同步)

计时前后 torch.npu.synchronize(), 预热 3 次, 测试 10 次, 首轮与稳定分开报告. 批量 4 实测可线性提升吞吐 (logits 校验一致).

证据图

三图由 scripts/render_xterm_evidence.mjs --style raw 根据本次真实日志生成, 深灰背景白色等宽字体, 提示符 atomgit@pod-a94f8701860f4700b161b00e290de466:~$.

agent workflow npu device call model result

  • agent_workflow.png — 侦察、环境、下载、加载、CPU/NPU 推理、一致性、性能与提交校验全流程
  • npu_device_call.png — npu-smi info, torch.npu.is_available, 设备名, 参数与输入输出 npu:0 Device 证明
  • model_result.png — 默认 python inference.py 完整输出 (输入输出摘要、真实 logits/probs、同步耗时、SUCCESS)

图片仅由 xterm.js 渲染本次真实日志, 非手绘或 matplotlib 模拟.

限制

  • 本仓演示单张 224x224 合成输入的 smoke 测试, 非完整 FairFace 数据集 10000 张 benchmark; 全量数据集精度 ~58.9% (见官方 README), NPU 上全量评估需遍历 nateraw/fairface 验证.
  • 仅验证 FP32; BF16/FP16 量化未测试, 需另测精度与阈值.
  • 模型预期输入为对齐人脸裁剪 (FairFace 需先检测人脸再裁剪 224), 本演示用通用 RGB 输入展示 NPU 流水, 实际业务需前置人脸检测 (如 MTCNN/RetinaFace).
  • 是否替换模型: 否 (全程使用 dima806/fairface_age_image_detection 真实权重 343 MB, 同权重同输入同 dtype 对比, 无 dummy 模型).