本目录将 Hugging Face 模型 dima806/fairface_age_image_detection(
ViTForImageClassification,基于 google/vit-base-patch16-224-in21k 微调,
在 FairFace 数据集上训练,用于 9 个年龄段分类)适配到 华为昇腾 Ascend NPU,
使用 torch_npu 完成端到端推理。该适配只运行在 NPU 上,不做静默 CPU 回退。
| 字段 | 值 |
|---|---|
| 模型 ID | dima806/fairface_age_image_detection |
| 任务 | image-classification(9 类年龄段) |
| 架构 | ViTForImageClassification |
| 上游来源 | https://huggingface.co/dima806/fairface_age_image_detection |
| 固定源码修订 | 4e02ab8057ea7fd74b1670940995c5dfda3e6ec0 |
| 推理引擎 | torch_npu(设备 npu:0) |
| 上游许可证 | Apache-2.0 |
| 权重文件 | model.safetensors(343,245,508 字节) |
| 权重 SHA256 | 1265d8e5a8a363a615898be873c224aebc6f310654bf23b1745da0975a7b7555 |
权重获取来源(retrieval provenance):本部署的本地权重由编排侧通过 提供的 AtomGit hf_mirrors 仓库
https://atomgit.com/hf_mirrors/dima806/fairface_age_image_detection.git获取,固定修订4e02ab8057ea7fd74b1670940995c5dfda3e6ec0。下载日志显示该镜像 缺少旧式权重名pytorch_model.bin(missing-weight condition,该文件并非本模型 实际使用的权重),但model.safetensors完整存在,且其 SHA2561265d8e5...7b7555与本地文件校验一致。上述字节是上游模型权重经由镜像分发, 以本地校验和为准。
FairFace Age Group Detection 是一个视觉 Transformer(ViT)图像分类模型,把一张 人脸图片预测到 9 个年龄段之一:
0-2、3-9、10-19、20-29、30-39、40-49、50-59、60-69、
more than 70。
224×224,使用 mean=0.5, std=0.5 归一化
(ViTImageProcessor)。accuracy ≈ 0.5892、f1 ≈ 0.5844。本适配目标:在昇腾 910B4 NPU(npu:0)上加载本地权重,执行一次真实前向推理,
并断言输入张量、模型参数、输出张量都位于 npu:0。
本容器已提供 匹配好的 torch==2.9.0+cpu 与 torch_npu==2.9.0,二者禁止
被重新安装或替换。其余依赖从阿里云 PyPI 镜像安装。
| 依赖 | 版本(本部署实测) | 说明 |
|---|---|---|
| Python | 3.11.14 | 容器自带 |
| torch | 2.9.0+cpu | 容器提供,禁止替换 |
| torch_npu | 2.9.0 | 容器提供,禁止替换 |
| CANN | 8.5.1 | /usr/local/Ascend/cann-8.5.1 |
| NPU 驱动/固件 | 25.5.1 | npu-smi 25.5.1 |
| transformers | 4.47.0 | 与模型 config.json 记录的 transformers_version 一致 |
| tokenizers | 0.21.1 | transformers 运行时依赖 |
| accelerate | 1.14.0 | 可选,模型加载加速 |
| safetensors | 0.8.0 | 安全加载权重 |
| regex | 2026.7.19 | transformers 运行时依赖 |
| Pillow | 12.3.0 | 图像读取/测试图生成 |
| numpy | 1.26.4 | 与 CANN 兼容版本 |
全部命令以 可移植的默认模型路径 为基准。将
MODEL_PATH指到你本地的模型 目录即可复用(例如/work/pipeline/models/cv9/fairface-age-image-detection)。
export MODEL_PATH=/work/pipeline/models/cv9/fairface-age-image-detection
export VENV=/work/pipeline/runtime/venvs-cv9/fairface-age-image-detection-npu
python3 -m venv --system-site-packages "$VENV"
# 从阿里云镜像安装非 torch 依赖(--no-deps 防止 pip 触碰 torch)
"$VENV/bin/pip" install --no-deps -i https://mirrors.aliyun.com/pypi/simple/ \
-r requirements.txtnpu-smi info # 应看到 8 张 910B4-1,Health=OK
"$VENV/bin/python" - <<'PY'
import torch, torch_npu
print("torch", torch.__version__)
print("torch_npu", torch_npu.__version__)
print("npu_available", torch.npu.is_available())
print("device_name", torch.npu.get_device_name(0))
assert torch.npu.is_available()
PYcd /work/pipeline/jobs/cv9/fairface-age-image-detection/fairface-age-image-detection-npu
ASCEND_RT_VISIBLE_DEVICES=0 \
"$VENV/bin/python" inference.py \
--model-path "$MODEL_PATH" \
--device npu:0 \
--image-path assets/test_input.png \
--output-json logs/npu_result.json \
--output-image assets/prediction_output.png脚本会:初始化 NPU → 加载本地权重 → CPU 预处理(transformers 要求)→
显式把 pixel_values 移到 npu:0 → 前向推理 → 断言输入/模型/输出都在
npu:0 → 输出 logs/npu_result.json 与标注图 assets/prediction_output.png。
assets/test_input.png是一张 自绘、CC0 授权 的合成人脸示意图,不涉及任何 受版权保护的图片。
命令
ASCEND_RT_VISIBLE_DEVICES=0 \
/work/pipeline/runtime/venvs-cv9/fairface-age-image-detection-npu/bin/python \
inference.py --model-path /work/pipeline/models/cv9/fairface-age-image-detection \
--device npu:0关键日志(真实运行,见 logs/inference_raw.log)
[start] 2026-08-20T08:34:23+0000
[hardware] aarch64 / Ascend NPU
[cann_version] 8.5.1
[torch_version] 2.9.0+cpu
[torch_npu_version] 2.9.0
[transformers_version] 4.47.0
[device] npu:0
[device_name] Ascend910B4-1
[model_path] /work/pipeline/models/cv9/fairface-age-image-detection
[source_revision] 4e02ab8057ea7fd74b1670940995c5dfda3e6ec0
[weight_bytes] 343245508
[weight_sha256] 1265d8e5a8a363a615898be873c224aebc6f310654bf23b1745da0975a7b7555
[model_arch] ViTForImageClassification
[num_labels] 9
[input_device] npu:0
[model_device] npu:0
[input_shape] [1, 3, 224, 224]
[output_device] npu:0
[output_shape] [1, 9]
[predicted_index] 3
[predicted_label] 20-29
[predicted_probability] 0.23084351420402527
[top5] ["20-29=0.2308", "30-39=0.1677", "40-49=0.1166", "10-19=0.1060", "3-9=0.1029"]
[elapsed_s] 4.6876
[exit_code] 0
[done] inference completed successfully on npu:0NPU 并发监控(真实运行,见 logs/npu_device_call.log)
推理进程在 NPU 0 上可见,同时 NPU 0 的 HBM 使用量从 ~3408 MB 上升到 ~3853 MB,
随后回落,证明推理确实发生在 npu:0:
SAMPLE=5 UTC=2026-08-20T08:34:25Z
| 0 0 | 248442 | python3 | 110 |logs/npu_result.json 结构化结果(节选):
{
"device": "npu:0",
"device_name": "Ascend910B4-1",
"cann_version": "8.5.1",
"torch_version": "2.9.0+cpu",
"torch_npu_version": "2.9.0",
"input_device": "npu:0",
"model_device": "npu:0",
"output_device": "npu:0",
"output_shape": [1, 9],
"predicted_index": 3,
"predicted_label": "20-29",
"probabilities": {
"0-2": 0.0665, "3-9": 0.1029, "10-19": 0.1060,
"20-29": 0.2308, "30-39": 0.1677, "40-49": 0.1166,
"50-59": 0.0799, "60-69": 0.0787, "more than 70": 0.0511
},
"elapsed_s": 4.6876,
"exit_code": 0,
"weight": {
"bytes": 343245508,
"sha256": "1265d8e5a8a363a615898be873c224aebc6f310654bf23b1745da0975a7b7555"
}
}输出文件:
logs/npu_result.json —— 结构化结果(设备、权重、预测、耗时、退出码)。assets/prediction_output.png —— 在测试图上叠加预测标签的标注图。assets/test_input.png —— 自绘 CC0 合成测试图。assets/agent_workflow.png —— 由真实日志渲染的工作流截图。assets/npu_device_call.png —— 由真实 npu-smi 监控日志渲染的设备调用截图。assets/model_result.png —— 由真实结果日志渲染的结果截图。logs/inference_raw.log —— 推理原始(脱敏)日志。logs/npu_device_call.log —— npu-smi info 并发监控原始日志。| 问题 | 可能原因 | 解决 |
|---|---|---|
torch.npu.is_available() 为 False | CANN/torch_npu 环境未加载 | source /usr/local/Ascend/ascend-toolkit/set_env.sh;确认 import torch_npu 成功 |
| 误装普通 PyPI torch | pip 替换了容器 torch/torch_npu | 重新用 python3 -m venv --system-site-packages + pip --no-deps 安装;不要重装 torch/torch_npu |
ModuleNotFoundError: transformers | 未在 venv 中安装 | 执行 pip install --no-deps -i https://mirrors.aliyun.com/pypi/simple/ -r requirements.txt |
预测标签显示成数字(如 3) | config.id2label 键为 int | 使用 inference.py 中的 lookup_label(),已兼容 int/str 键 |
fusion_result.json 在目录里出现 | CANN 图融合运行期产物 | 属正常运行缓存,已加入 .gitignore |
torch 自动以 fp32 执行。dima806/fairface_age_image_detection:Apache-2.0(见上游模型卡)。google/vit-base-patch16-224-in21k:Apache-2.0。nateraw/fairface 数据集卡;请遵守其许可与
隐私/伦理要求(涉及人脸数据)。inference.py、readme.md、assets/test_input.png、
日志/截图等)为任务交付物,按 Apache-2.0 提供;测试图为 CC0-1.0。下图是本次适配与第二轮上游规范复核的 ModelAgent 流式会话只读回放,来源为公开交付中的脱敏会话日志;命令、问题定位、修复和验收结论均来自真实会话。



