dima806/fairface_age_image_detectiongoogle/vit-base-patch16-224-in21k 微调[0-2, 3-9, 10-19, 20-29, 30-39, 40-49, 50-59, 60-69, more than 70]nateraw/fairface (FairFace)transformers.AutoModel / ViTForImageClassification, ViTImageProcessor, 无 trust_remote_codetorch.npu.is_available()==True, device_count=2, device_name Ascend910_9362, npu:0 可用/tmp/dima806_fairface_age (snapshot_download 本地加载)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.txtrequirements.txt 内容:
torch==2.9.0
torch-npu==2.9.0.post1
transformers==4.47.0
pillow
numpy
safetensors
huggingface_hub默认命令 (自动使用 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节。
以下为本次在 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)
相同权重、同一输入、同一预处理分别在 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)计时区间前后均调用 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: PASSassets/agent_workflow.png - 阶段日志 (下载、侦察、加载、NPU 推理、验证、性能)assets/npu_device_call.png - npu-smi + torch.npu 可用性、设备名、模型参数 npu:0assets/model_result.png - 默认 python inference.py 任务输出与 PASS三图由
scripts/render_xterm_evidence.mjs --style raw从本次真实日志渲染, 源日志 SHA-256 显示于图脚。
--dtype float16)torch_npu 与 CANN 8.5.1 兼容环境, CPU 仅用于一致性对比, 不宣称 NPU 精度为 CPU 基准替代NPU, Ascend, Ascend910, Vision, Image-Classification, ViT, FairFace, Age-Estimation硬件声明: hardware: NPU (见文件头 YAML)