m
mxy-yy/vit-age-classifier-npu
模型介绍文件和版本Pull Requests讨论分析
下载使用量0

vit-age-classifier Ascend NPU 部署指南

项目简介

vit-age-classifier 是基于 Google ViT-Base-Patch16-224 的年龄分类模型,能够对输入的人脸图像进行年龄范围分类。模型将图像分类为 9 个年龄组:0-2、3-9、10-19、20-29、30-39、40-49、50-59、60-69、70+。

特性

  • 支持 Ascend NPU 推理加速
  • CPU 与 NPU 精度对比测试(误差 < 1%)
  • 9 类年龄分类
  • 兼容 HuggingFace transformers
  • 121.20 倍加速比

环境信息

项目版本/内容
设备Ascend 910B

文件结构

vit-age-classifier-ascend/
├── inference.py          # 推理测试脚本
├── test.log              # 测试日志
├── README.md             # 本文档

部署步骤

1. 设置环境变量

source /usr/local/Ascend/ascend-toolkit/set_env.sh

2. 准备模型文件

模型文件位于 /opt/atomgit/mxy/vit-age-classifier/ 目录下:

  • model.safetensors - 模型权重
  • config.json - 模型配置
  • preprocessor_config.json - 图像预处理配置

3. 安装依赖

pip install transformers torch_npu pillow numpy

4. 执行推理

cd vit-age-classifier-ascend/
python3 inference.py

使用方式

方式一:普通推理模式

运行推理脚本进行年龄分类:

cd vit-age-classifier-ascend/
python3 inference.py --mode inference

方式二:精度测试模式 (CPU vs NPU)

运行精度对比测试:

cd vit-age-classifier-ascend/
python3 inference.py --mode precision_test

命令行参数说明

参数说明默认值
--mode测试模式: all, inference 或 precision_testall

测试验证

精度测试结果

指标实测值阈值状态
最大相对误差0.2793%< 1.00%✅ PASS
CPU 推理时间1.683s--
NPU 推理时间0.014s--
加速比121.20x> 1x✅ PASS

推理结果示例

输入: 224x224 RGB 人脸图像

输出:

  • 类别 ID: 3
  • 年龄范围: 20-29
  • Logits 维度: 9
年龄组ID
0-20
3-91
10-192
20-293
30-394
40-495
50-596
60-697
70+8

测试日志

ViT Age Classifier NPU Test
Model: google/vit-base-patch16-224-in21k (age classification)
Output: /opt/atomgit/mxy/vit-age-classifier-ascend

============================================================
Inference Test (NPU)
============================================================
Device: npu:0
Loading model and processor...
Model loaded successfully
Input shape: torch.Size([1, 3, 224, 224])
Inference time: 5.470s
Predicted class: 3 (20-29)
Logits shape: torch.Size([1, 9])

============================================================
Precision Test (CPU vs NPU)
============================================================
NPU Device: npu:0
Loading model...
Input shape: torch.Size([1, 3, 224, 224])
Running on CPU...
Running on NPU...
CPU inference time: 1.683s
NPU inference time: 0.014s
Speedup: 121.20x
Max absolute error: 3.722869e-03
Max relative error: 0.2793% (threshold: 1.0%)
Status: PASS

============================================================
Precision Test Result: PASS
============================================================

============================================================
Test Complete!
============================================================

Python API 使用示例

基本推理

import torch
from PIL import Image
from transformers import ViTForImageClassification, AutoImageProcessor

MODEL_DIR = "/opt/atomgit/mxy/vit-age-classifier"

processor = AutoImageProcessor.from_pretrained(MODEL_DIR)
model = ViTForImageClassification.from_pretrained(MODEL_DIR)
model = model.to("npu:0").eval()

image = Image.open("face.jpg").convert("RGB")
inputs = processor(images=image, return_tensors="pt")
inputs = {k: v.to("npu:0") for k, v in inputs.items()}

with torch.no_grad():
    outputs = model(**inputs)

logits = outputs.logits
predicted_class = logits.argmax(-1).item()
predicted_label = model.config.id2label[str(predicted_class)]
print(f"Predicted: {predicted_label}")

模型结构

组件说明
embeddings图像块嵌入 + 分类令牌
encoder12 层 Transformer 编码器
classifier线性分类头 (768 -> 9)

推理参数配置

参数值
hidden_size768
num_hidden_layers12
num_attention_heads12
patch_size16
image_size224

注意事项

  1. 模型使用 NPU 进行推理加速
  2. 支持 CPU 和 NPU 双模式推理
  3. 精度验证通过 (< 1% 误差)
  4. 121x 加速比