本文档记录 vit-face-expression(ViT 面部表情分类模型)在昇腾 NPU(Ascend 910B3)环境的适配部署与精度验证结果。
vit-face-expression 基于 Vision Transformer(ViT-Base)架构,参数量约 86.4M,对人脸图像进行表情分类。本项目完成该模型在昇腾 NPU 上的推理适配,验证 NPU 与 CPU 推理结果的精度误差 < 1%。
相关获取地址:
权重下载地址(HuggingFace):https://huggingface.co/google/vit-base-patch16-224-in21k
适配代码仓库:https://gitcode.com/dingdang666/vit-face-expression-NPU
| 组件 | 版本 |
|---|---|
python | 3.11.x |
torch | 2.10.0+cpu |
torch_npu | 2.10.0 |
transformers | 5.8.1 |
CANN | 8.5.1 |
Ascend 910B3(8卡)/path/to/modelPyTorch + transformers| 项目 | 值 |
|---|---|
| 模型架构 | ViT-Base (ViTForImageClassification) |
| 参数量 | ~86.4M |
| 输入尺寸 | 3×224×224 |
| 权重格式 | safetensors |
conda create -n vit-expr python=3.11 -y
conda activate vit-expr
pip install torch==2.10.0 torchvision==0.25.0 --index-url https://repo.huaweicloud.com/repository/pypi/simple/
pip install torch_npu==2.10.0 --index-url https://repo.huaweicloud.com/repository/pypi/simple/
pip install transformers safetensors pillow --index-url https://repo.huaweicloud.com/repository/pypi/simple/python3 inference.py --model_path /path/to/model --image_path /path/to/img.jpg
python3 inference.py ... --device cpu
python3 benchmark.py --model_path /path/to/model| 脚本 | 参数 | 说明 | 默认值 |
|---|---|---|---|
| inference.py | --model_path / --image_path / --device | 模型/图像/设备 | 必需 / 必需 / npu:0 |
| benchmark.py | --model_path / --npu_device / --num_warmup | 模型/NPU/预热 | 必需 / npu:0 / 3 |
使用同一测试图像在 CPU(FP32)和 NPU(FP32)上推理,对比分类 logits。
| 指标 | 数值 |
|---|---|
| 向量级相对误差 | 0.319003% |
| 余弦相似度 | 0.9999959469 |
| 评价指标 | 实测值 | 阈值 | 状态 |
|---|---|---|---|
| 向量级相对误差 | 0.32% | < 1% | PASS |
| 操作 | 耗时 |
|---|---|
| CPU 推理时间(FP32) | 0.83 s |
| NPU 推理时间(FP32,3轮预热后) | 0.31 s |
| 加速比 (CPU / NPU) | 2.64 x |
export HF_ENDPOINT=https://hf-mirror.com/。