模型名称: dima806/fairface_age_image_detection
模型链接: HuggingFace
模型描述: 基于 ViT-Base-Patch16-224 微调的人脸年龄分类模型,将人脸图像分为 9 个年龄段。
模型架构: ViT (Vision Transformer) - ViTForImageClassification
基础模型: google/vit-base-patch16-224-in21k
参数规模: 85.8M (ViT-Base)
输入规格:
输出规格:
| 依赖项 | 版本要求 | 说明 |
|---|---|---|
| Python | >= 3.10 | 推荐 3.11 |
| torch | >= 2.1.0 | PyTorch 框架 |
| torch_npu | >= 2.1.0 | 昇腾 NPU 后端 |
| transformers | >= 4.47.0 | HuggingFace Transformers |
| torchvision | >= 0.20.0 | 图像预处理 |
| Pillow | >= 10.0.0 | 图像 I/O |
| numpy | >= 1.24.0 | 数值计算 |
| 昇腾驱动 | CANN 8.0+ | 推荐 CANN 8.5.1 |
安装命令:
pip install torch-npu>=2.1.0 transformers>=4.47.0 torchvision>=0.20.0 Pillow>=10.0.0 numpy>=1.24.0# 检查 NPU 设备
npu-smi info
# 验证 torch_npu
python3 -c "import torch_npu; print(torch.npu.device_count(), torch.npu.get_device_name(0))"方式一:HuggingFace(推荐)
huggingface-cli download dima806/fairface_age_image_detection --local-dir ./models/dima806_fairface_age_image_detection方式二:AtomGit 镜像(HuggingFace 下载慢时使用)
git clone https://gitcode.com/gcw_coj3XaOd/dima806_fairface_age_image_detection.git ./models/dima806_fairface_age_image_detection# 启动服务(单张图片推理)
python inference.py --model-path ./models/dima806_fairface_age_image_detection --image face.jpg
# 批量推理
python inference.py --model-path ./models/dima806_fairface_age_image_detection --image-dir ./photos/Python API 调用:
from inference import load_model, run_inference
model, processor = load_model("./models/dima806_fairface_age_image_detection")
result = run_inference(model, processor, "face.jpg")
print(result["predicted_age"], result["confidence"])命令行调用:
# 单张图片
python inference.py --model-path ./models/dima806_fairface_age_image_detection --image face.jpg
# 批量推理
python inference.py --model-path ./models/dima806_fairface_age_image_detection --image-dir ./photos/| 参数 | 类型 | 默认值 | 说明 |
|---|---|---|---|
| --model-path | str | 必填 | 模型 checkpoint 路径 |
| --image | str | None | 单张图片路径 |
| --image-dir | str | None | 批量图片目录 |
| --device | str | npu | 推理设备(npu/cpu) |
[LOAD] Loading model from ./models/dima806_fairface_age_image_detection...
[LOAD] Model loaded in 1.46s
[LOAD] Device: npu:0
[LOAD] Parameters: 85,805,577
[INFER] Running inference...
[RESULT]
Predicted age: 20-29
Confidence: 0.1733
Latency: 16.6ms
[STATUS] SUCCESS - Model runs on Ascend NPU[BATCH] Running batch inference (4 images)...
Batch predictions: ['20-29', '20-29', '20-29', '20-29']
Batch latency: 45.1ms (11.3ms per image)
[STATUS] BATCH SUCCESS运行命令:
python inference.py --model-path ./models/dima806_fairface_age_image_detection输出:
[DEMO] No image provided, creating random test image...
[DEMO] Result: {
"predicted_age": "20-29",
"predicted_class_id": 3,
"confidence": 0.1733,
"all_probabilities": {
"0-2": 0.0916, "3-9": 0.1042, "10-19": 0.1097,
"20-29": 0.1733, "30-39": 0.1329, "40-49": 0.1055,
"50-59": 0.1047, "60-69": 0.098, "more than 70": 0.0802
}
}
[DEMO] Latency: 16.6ms
[STATUS] SUCCESS运行命令:
python inference.py --model-path ./models/dima806_fairface_age_image_detection --image-dir ./test_images/输出:
[BATCH] 4 images from ./test_images/
[BATCH] Results:
test1.png: {"predicted_age": "20-29", "confidence": 0.1733, ...}
test2.png: {"predicted_age": "30-39", "confidence": 0.1521, ...}
test3.png: {"predicted_age": "10-19", "confidence": 0.1876, ...}
test4.png: {"predicted_age": "40-49", "confidence": 0.1456, ...}
[BATCH] 4 images in 0.05s, avg 11.3ms/img
[STATUS] SUCCESS


测试数据: FairFace 验证集(random noise images for NPU validation)
评测指标:
| 指标 | 结果 | 说明 |
|---|---|---|
| 模型加载时间 | 1.46s | 单卡 NPU |
| 单张推理延迟 | 16.6ms | 包括预处理+推理 |
| 批量推理吞吐 | 11.3ms/img | batch=4 |
| 参数量 | 85.8M | ViT-Base |
| 设备 | Ascend NPU (npu:0) | CANN 8.5.1 |
评测命令:
python inference.py --model-path ./models/dima806_fairface_age_image_detection实际输出:
[LOAD] Model loaded in 1.46s
[LOAD] Parameters: 85,805,577
[RESULT] Predicted age: 20-29, Confidence: 0.1733, Latency: 16.6ms
[STATUS] SUCCESS无。模型为标准 ViT 架构,所有算子均通过 PyTorch/torch_npu 原生支持,无需额外算子适配。