dima806/fairface_age_image_detectiongoogle/vit-base-patch16-224-in21k (ViT-Base, patch 16, 224, 12 layers, hidden 768, 86M 参数)torch_npu 实现单卡推理,保持 FP32 精度并完成 CPU-NPU 一致性验证。vit / ViTForImageClassificationtransformers (AutoImageProcessor + ViTForImageClassification, safetensors 权重 328 MB, FP32)| 项目 | 值 |
|---|---|
| NPU | Ascend910_9362 x2 (npu-smi 25.5.5, CANN 8.5.1) |
| npu-smi | Health OK, HBM 3110/65536 MB, AICore 0% |
| torch | 2.9.0+cpu |
| torch_npu | 2.9.0.post1 |
| torch.npu.is_available() | True, device_count 2, device_name Ascend910_9362 |
| transformers | 4.57.6 |
| 验证设备 | npu:0 |
| dtype | float32 (默认; 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 }
}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')"默认命令 (固定灰度输入, 自动打印标签排序、耗时与 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).
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--- 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| 样例 | 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 |
所有计时在关键区间前后调用 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%。
assets/agent_workflow.png - 阶段日志: 下载、侦察、NPU 加载、推理、验证与性能assets/npu_device_call.png - 设备日志: npu-smi info, torch.npu.is_available(), device_name, 首参 device/dtypeassets/model_result.png - 结果日志: python inference.py 真实输出、分数排序与 PASS三张图由 scripts/render_xterm_evidence.mjs --style raw 从本次真实日志渲染, 含 SHA-256 与退出码。
torch_dtype 尝试。HF_ENDPOINT=https://hf-mirror.com), 离线环境需预先下载权重到 /tmp/fairface_final。Hardware: NPU, NPU, Ascend, Ascend910, ViT, image-classification, fairface, age-detection
仓库类型: model (公开)