本项目将 ModelScope apple/mobilevit-x-small 图像分类模型适配到华为昇腾 NPU (Ascend910)。模型通过 ModelScope 下载权重,使用 transformers 库的 AutoModelForImageClassification 进行本地加载,并在单卡 NPU 上完成推理验证、CPU-NPU 精度一致性检查和性能基准测试。
| 项目 | 版本/信息 |
|---|---|
| NPU | Ascend910 |
| CANN | 8.5.1 |
| PyTorch | 2.x |
| torch_npu | 适配 CANN 8.5.1 |
| transformers | 最新版 |
完整环境信息见 logs/env_check.log。
pip install -r requirements.txt
python inference.py推理结果 (NPU):
=== NPU Inference Result ===
Input image: assets/test.jpg
Logits shape: torch.Size([1, 1000])
Logits sample (first 10): [4.1390, 0.2492, 1.0306, 1.3025, 1.6823, 1.7179, 1.0253, 1.5865, 2.3747, 3.4093]
Top-5 predictions:
1. class_958: 0.2071
2. class_984: 0.1256
3. class_595: 0.1200
4. class_856: 0.0260
5. class_730: 0.0184日志保存在 logs/inference.log。
对单张测试输入进行 CPU 与 NPU 一致性验证:
| 指标 | 数值 |
|---|---|
| max_abs_error | 0.013688 |
| mean_abs_error | 0.006430 |
| relative_error | 0.4342% |
| cosine_similarity | 0.999996 |
| threshold | 1.0% |
| 结果 | PASS |
单张图片 batch=1 在 Ascend910 上的推理性能:
| 指标 | 数值 |
|---|---|
| avg_latency_ms | 8.406 |
| min_latency_ms | 8.362 |
| max_latency_ms | 8.477 |
| p50_latency_ms | 8.396 |
| p90_latency_ms | 8.457 |
| p95_latency_ms | 8.467 |
| throughput_img/s | 118.96 |
本项目包含单输入 smoke consistency 验证,非官方完整验证集评测。详细指标见第 4 节。
见 screenshots/self_verification.txt 和 screenshots/self_verification.png。
logs/env_check.log — 环境检查logs/inference.log — NPU 推理输出logs/accuracy.log — CPU-NPU 精度一致性logs/benchmark.log — 性能基准测试snapshot_download 自动下载,首次运行需要联网。local_files_only=True 加载本地权重,不触发 HuggingFace 自动下载。#NPU #Ascend #Ascend910 #MobileViT #ImageClassification