本文档记录 apple/mobilevit-small MobileViT 图像分类模型在昇腾 NPU(Ascend 910B3)上的迁移适配、精度评测与性能验证结果。
MobileViT-small 是 MobileViT 系列中最大的标准变体(5.6M 参数),在 ImageNet-1k 上训练,支持 1000 类分类。相比 xx-small(1.3M)和 x-small(2.3M),small 版本精度最高,适合对分类质量要求较高但仍需移动端部署的场景。
相关获取地址:
| 组件 | 版本 |
|---|---|
torch | 2.8.0 |
torch_npu | 2.8.0.post4 |
transformers | 5.8.1 |
CANN | 8.5.1 |
8 × Ascend 910B3conda create -n mobilevit-small python=3.11 -y
conda activate mobilevit-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-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 吞吐量 | 5.6 img/s |
| NPU 吞吐量 | 161.1 img/s |
| CPU/NPU 加速比 | 28.9 × |
MobileViT-small 5.6M 参数在 NPU 上获得 28.9× 加速,兼顾精度与效率。
分别在 CPU 和 NPU 上推理 10 张合成图像,比较 1000 维 softmax 余弦相似度和 Top-1 一致性。
| 指标 | 数值 |
|---|---|
| 精度误差率 | 0.0004% |
| Top-1 准确率 | 100.0% |
结论:精度误差率 0.0004%,远低于 1% 要求,评测通过。
AutoModelForImageClassification.from_pretrained() 加载model.to("npu:0") 迁移from transformers import AutoImageProcessor, AutoModelForImageClassification
model = AutoModelForImageClassification.from_pretrained("mobilevit-small").to("npu:0")
processor = AutoImageProcessor.from_pretrained("mobilevit-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)