基于 dima806/fairface_age_image_detection 的昇腾 NPU 适配推理仓库。模型以人脸图片为输入,输出 9 个年龄组(0-2 / 3-9 / 10-19 / 20-29 / 30-39 / 40-49 / 50-59 / 60-69 / more than 70)的分类概率。
| 项目 | 值 |
|---|---|
| 模型来源 | dima806/fairface_age_image_detection |
| 任务类型 | 单标签图像分类(人脸年龄组检测) |
| 架构 | ViTForImageClassification(google/vit-base-patch16-224-in21k 微调) |
| 输入 | 人脸图片,resize 至 224×224,RGB,归一化 mean/std = 0.5 |
| 输出 | 9 个年龄组的 logits / 概率 |
| 参数量 | 85.81M |
| 权重 | model.safetensors(343.2 MB,float32) |
| 许可证 | Apache-2.0 |
见 requirements.txt:transformers>=4.47.0、torch、torch_npu、Pillow、numpy、huggingface_hub。
# 1. 使用仓库内本地权重(默认权重目录为仓库根目录)
python inference.py --source ./test_face.jpg --weights-dir . --output ./output.json
# 2. 自动从 HuggingFace 下载权重(HF 直连失败自动切换 hf-mirror.com 镜像)
python inference.py --source ./test_face.jpg
# 3. 指定设备(默认自动检测 npu:0 > cuda:0 > cpu)
python inference.py --source ./test_face.jpg --device npu:0
# 4. 批量目录推理
python inference.py --source ./faces/ --weights-dir . --output ./output.json| 参数 | 说明 |
|---|---|
--source | 输入图片路径或目录(默认 ./test_face.jpg) |
--weights-dir | 本地权重目录(含 config.json / model.safetensors) |
--output | 结果 JSON 输出路径(默认打印 stdout) |
--device | 设备:npu:0 / cuda:0 / cpu(默认自动检测) |
--top-k | 输出 Top-K 年龄组(默认 3) |
--no-hf | 禁止自动从 HuggingFace 下载权重 |
| 项 | CPU | NPU(Ascend 910) |
|---|---|---|
| 单张推理耗时 | 0.67 s | 0.18 s |
| 预测结果 | 30-39(Top1) | 30-39(Top1,与 CPU 一致) |
| 输出有效性 | 概率归一化、finite | 概率归一化、finite |
实测命令(Ascend 910,CANN 8.5.1,torch 2.9 + torch_npu):
python inference.py --source ./test_face.jpg --weights-dir . --device npu:0 --no-hf输出示例(Top-3):
{
"predicted_age_group": "30-39",
"top_k": [
{"age_group": "30-39", "probability": 0.2103},
{"age_group": "20-29", "probability": 0.1498},
{"age_group": "10-19", "probability": 0.1274}
],
"inference_time_s": 0.18
}TORCHDYNAMO_DISABLE=1,NPU 上强制 eager 模式,避免 dynamo 图捕获与昇腾算子兼容问题。hf-mirror.com 兜底)下载。