本文档记录 apple/mobilevit-xx-small MobileViT 轻量级图像分类模型在昇腾 NPU(Ascend 910B3)上的迁移适配、精度评测与性能验证结果。
MobileViT 将 CNN 的局部特征提取能力与 ViT 的全局建模能力结合,在移动端实现轻量高效的图像分类。xx-small 是最小变体(1.3M 参数),适合资源极度受限的边缘设备。该模型在 ImageNet-1k 上训练,支持 1000 类分类。
相关获取地址:
| 组件 | 版本 |
|---|---|
torch | 2.8.0 |
torch_npu | 2.8.0.post4 |
transformers | 5.8.1 |
CANN | 8.5.1 |
8 × Ascend 910B3conda create -n mobilevit-xx-small python=3.11 -y
conda activate mobilevit-xx-small
pip install torch==2.8.0 torch_npu==2.8.0.post4 \
-i https://pypi.tuna.tsinghua.edu.cn/simple
pip install transformers torchvision pillow numpy \
-i https://pypi.tuna.tsinghua.edu.cn/simplepython inference.py --image photo.jpg --device npufrom inference import AgeClassifier
clf = AgeClassifier(model_path="./mobilevit-xx-small", device="npu")
results = clf.predict(["photo.jpg"])python inference.py --image photo.jpg --device npu预期输出:Top-5 分类标签及置信度,无运行时错误。
测试条件:10 张合成 224×224 图像,batch_size=8,NPU 预热 1 轮。
| 指标 | 数值 |
|---|---|
| CPU 吞吐量 | 16.3 img/s |
| NPU 吞吐量 | 122.1 img/s |
| CPU/NPU 加速比 | 7.5 × |
MobileViT-xx-small 仅 1.3M 参数,推理极快,适合移动端部署。
分别在 CPU 和 NPU 上推理 10 张合成图像,比较 1000 维 softmax 概率向量的余弦相似度和 Top-1 分类一致性。
| 指标 | 数值 |
|---|---|
| 精度误差率 | 0.0035% |
| Top-1 准确率 | 100.0% |
结论:精度误差率 0.0035%,远低于 1% 要求,评测通过。
AutoModelForImageClassification.from_pretrained() 加载model.to("npu:0") 迁移,CNN 卷积 + Transformer 注意力 NPU 原生支持from transformers import AutoImageProcessor, AutoModelForImageClassification
model = AutoModelForImageClassification.from_pretrained("mobilevit-xx-small").to("npu:0")
processor = AutoImageProcessor.from_pretrained("mobilevit-xx-small")
inputs = processor(images=image, return_tensors="pt")
inputs = {k: v.to("npu:0") for k, v in inputs.items()}
with torch.no_grad():
probs = torch.softmax(model(**inputs).logits, dim=-1)