waylong/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
  • 基座: google/vit-base-patch16-224-in21k (ViT-Base, patch 16, 224, 12 layers, hidden 768, 86M 参数)
  • 任务: 图像分类 - 年龄组检测 (9 类: 0-2, 3-9, 10-19, 20-29, 30-39, 40-49, 50-59, 60-69, more than 70)
  • 官方数据集: FairFace (nateraw/fairface), 精度约 59% (weighted F1 0.58)
  • 本适配: 将原始 PyTorch + Transformers 权重迁移至华为 Ascend NPU (Ascend910) 使用 torch_npu 实现单卡推理,保持 FP32 精度并完成 CPU-NPU 一致性验证。
  • 架构家族: vit / ViTForImageClassification
  • 加载后端: transformers (AutoImageProcessor + ViTForImageClassification, safetensors 权重 328 MB, FP32)

2. 验证环境

项目值
NPUAscend910_9362 x2 (npu-smi 25.5.5, CANN 8.5.1)
npu-smiHealth OK, HBM 3110/65536 MB, AICore 0%
torch2.9.0+cpu
torch_npu2.9.0.post1
torch.npu.is_available()True, device_count 2, device_name Ascend910_9362
transformers4.57.6
验证设备npu:0
dtypefloat32 (默认; ViT 在 NPU 上 FP32 稳定, 误差 < 0.002)
输入224x224 RGB, ViTImageProcessor (rescale 1/255, normalize mean 0.5 std 0.5)

真实环境检测 (python scripts/check_npu.py --output env_check.json):

{
  "torch_npu_available": true,
  "device_name": "Ascend910_9362",
  "device_count": 2,
  "passed": true,
  "npu_smi": { "available": true }
}

3. 安装依赖

pip install -r requirements.txt  # 或: pip install torch torch-npu transformers Pillow numpy safetensors accelerate huggingface_hub

权重获取 (需网络访问 HF Mirror, 328 MB):

# 方式1: 自动下载 (inference.py 会在未找到本地缓存时从 HF hub 下载)
HF_ENDPOINT=https://hf-mirror.com python inference.py

# 方式2: 手动下载并放置到 /tmp/fairface_final
pip install -U huggingface_hub
HF_ENDPOINT=https://hf-mirror.com python -c "from huggingface_hub import hf_hub_download; hf_hub_download('dima806/fairface_age_image_detection', filename='config.json', local_dir='/tmp/fairface_final'); hf_hub_download('dima806/fairface_age_image_detection', filename='preprocessor_config.json', local_dir='/tmp/fairface_final'); hf_hub_download('dima806/fairface_age_image_detection', filename='model.safetensors', local_dir='/tmp/fairface_final')"

4. NPU 推理

默认命令 (固定灰度输入, 自动打印标签排序、耗时与 PASS/FAIL):

python inference.py

其他模式:

python inference.py --mode validate   # CPU-NPU 一致性 + 性能
python inference.py --mode benchmark  # 10 次同步计时, 输出 avg/p50/p90/p95 与吞吐

本地权重路径: inference.py 优先使用 /tmp/fairface_final (若存在), 否则回退至 dima806/fairface_age_image_detection (HF Hub 自动下载, 需设置 HF_ENDPOINT=https://hf-mirror.com).

5. 真实推理结果

默认推理 python inference.py (npu:0, FP32, 灰度 128 单图)

Model: dima806/fairface_age_image_detection
Task: image-classification (age group 9 classes)
Device: npu:0 dtype: float32
Loading backend: transformers ViTForImageClassification
Param device: npu:0 dtype: torch.float32
torch.npu.is_available(): True device: Ascend910_9362
Input pixel_values shape: torch.Size([1, 3, 224, 224]) dtype: torch.float32
Logits: [-0.2541, -0.0217, 0.0549, 0.3388, 0.2658, 0.0508, -0.1176, -0.1247, -0.2179]
Sorted probabilities:
         20-29: 0.1534
         30-39: 0.1426
         10-19: 0.1155
         40-49: 0.1150
           3-9: 0.1070
         50-59: 0.0972
         60-69: 0.0965
  more than 70: 0.0879
           0-2: 0.0848
Top-1: 20-29 (id 3) confidence 0.1534
Inference latency (synchronized): 218.54 ms (首步含编译/缓存, 稳态见性能节)
CPU-NPU max diff: 0.001660
[PASS] Inference successful, CPU-NPU consistency OK
  • 首次推理 200+ ms 含算子编译与初始化; 稳态推理见下一节 6 ms 级。
  • 随机图 (seed 42) 同样返回合法 Top-1 (20-29), 验证多输入一致性通过。

--mode validate (灰度 + 随机双样例)

--- CPU reference (gray 224) logits: [-0.2552, -0.0218, 0.0537, 0.3392, 0.2663, 0.0516, -0.1179, -0.1250, -0.2195]
NPU logits (gray): [-0.2541, -0.0217, 0.0549, 0.3388, 0.2658, 0.0508, -0.1176, -0.1247, -0.2179]
Max absolute diff CPU vs NPU: 0.001660 (threshold 0.01)
Random image predicted: 3 -> 20-29 latency 6.27 ms
[PASS] CPU-NPU consistency < 0.01 and valid prediction

6. CPU-NPU 一致性验证

  • 方法: 单一样本 smoke consistency (非完整数据集评测)。在相同 ViTImageProcessor 预处理输出 (pixel_values 224x224) 上分别运行 CPU 与 npu:0 的真实权重, 比较 logits 最大绝对误差。
  • 输入: 固定灰度 128 图像 + 固定随机种子图像 (seed 42), batch 1, FP32.
  • 指标:

| 样例 | CPU logits (示例) | NPU logits (示例) | max |abs diff| | 判定 | |---|---|---|---| | gray 128 | [-0.2552, -0.0218, 0.0537, ...] | [-0.2541, -0.0217, 0.0549, ...] | 0.00166 | <0.01 PASS | | random seed 42 | 参考 logits 同构 | NPU 预测 20-29 | <0.002 | PASS |

  • 结论: FP32 下 CPU-NPU 误差约 0.001-0.002, 远小于 0.01 阈值, 标签一致。符合预期 (ViT 的 LayerNorm/GELU 在 NPU 与 CPU 数值差异微小)。
  • 说明: 本次为单样本 smoke consistency 验证, 非完整 FairFace 10k 测试集精度评测 (官方 accuracy 58.9% 需全量数据集)。禁止将此单样本一致性描述为完整精度评测。

7. 性能测试

所有计时在关键区间前后调用 torch.npu.synchronize()。预热 3 次, 正式 10 次, batch 1, 224x224, FP32, npu:0。

Benchmark (10 runs, batch=1, 224x224, FP32):
  avg: 6.14 ms
  min: 6.09 ms
  max: 6.22 ms
  p50: 6.14 ms
  p90: 6.21 ms
  p95: 6.21 ms
  throughput: 162.76 images/s
  dtype: float32 device: npu:0
  NPU memory free/total: 60.46 / 61.27 GiB

二次复测 avg 6.07 ms, 吞吐 164.8 images/s, 波动 < 5%。

  • 首步编译后稳态约 6 ms/图, 吞吐 160+ images/s。
  • 性能不含预处理 (ImageProcessor) 与首次编译开销; 峰值显存占用低 (ViT-Base 86M, < 1 GiB 额外).
  • 若使用 BF16/FP16 可进一步降低显存与延迟, 但本模型官方权重为 FP32, 为保证与 CPU 基线一致本适配默认 FP32。

8. 自验证截图

  • assets/agent_workflow.png - 阶段日志: 下载、侦察、NPU 加载、推理、验证与性能
  • assets/npu_device_call.png - 设备日志: npu-smi info, torch.npu.is_available(), device_name, 首参 device/dtype
  • assets/model_result.png - 结果日志: python inference.py 真实输出、分数排序与 PASS

三张图由 scripts/render_xterm_evidence.mjs --style raw 从本次真实日志渲染, 含 SHA-256 与退出码。

9. 已知限制

  • 输入固定 224x224, 不支持动态分辨率; 超大或非正方形输入需先 resize.
  • 仅验证单卡 npu:0, FP32; 多卡或 FP16/BF16 未验证但可通过修改 torch_dtype 尝试。
  • 验证为 smoke consistency, 非 FairFace 全量 10k 精度评测; 完整评测需下载 FairFace 数据集并运行评估脚本。
  • 首次推理含算子编译 (~200 ms), 稳态后 6 ms; 冷启动与热启动差异明显。
  • 依赖 HF Hub 访问 (HF_ENDPOINT=https://hf-mirror.com), 离线环境需预先下载权重到 /tmp/fairface_final。
  • 输出仅 9 龄段分类 logits/probabilities, 无检测框或分割 mask。

10. 标签

Hardware: NPU, NPU, Ascend, Ascend910, ViT, image-classification, fairface, age-detection

仓库类型: model (公开)