MobileNetV3 Small 0.75
该模型是基于 MobileNetV3 架构的图像分类模型,在 ImageNet-1K 数据集上预训练。本仓库提供了在昇腾 NPU 上的完整适配和推理方案。
模型信息
- 模型名称: timm/tf_mobilenetv3_small_075.in1k
- 原始模型: ModelScope
- 任务类型: 图像分类 (Image Classification)
- 模型框架: PyTorch / timm
- 输入格式: RGB 图像 (3×224×224)
- 输出格式: 1000 类 ImageNet 分类概率
- NPU 适配: 使用 torch_npu 在昇腾 Ascend910 上完成推理验证
依赖环境
- torch>=2.0.0
- timm>=1.0.0
- torch-npu
- Pillow
- safetensors
- modelscope
环境准备
安装依赖:
pip install torch timm torch-npu Pillow safetensors modelscope
模型权重自动从 ModelScope 下载(已适配国内网络环境)。
推理命令
使用随机输入进行推理:
python3 inference.py --model timm/tf_mobilenetv3_small_075.in1k
使用自定义图片推理:
python3 inference.py --model timm/tf_mobilenetv3_small_075.in1k --image test_input.jpg
CPU 与 NPU 精度对比:
python3 compare_cpu_npu.py --model timm/tf_mobilenetv3_small_075.in1k --image test_input.jpg
推理结果
在 Ascend910 NPU 上的推理结果如下:
| 指标 | CPU | NPU |
|---|
| 推理耗时 | 16.20 ms | 173.56 ms |
| Top-1 类别 | 446 | 446 |
CPU Top-5 预测:
- Class 446: 0.266223
- Class 549: 0.065508
- Class 722: 0.050486
- Class 605: 0.045052
- Class 551: 0.028652
NPU Top-5 预测:
- Class 446: 0.261351
- Class 549: 0.066191
- Class 722: 0.051319
- Class 605: 0.044828
- Class 551: 0.028780
部署和推理方法
- 模型加载:使用 timm.create_model() 创建模型架构,从 ModelScope 本地缓存加载预训练权重。
- 数据预处理:使用 timm 的 resolve_data_config 和 create_transform 进行标准化预处理。
- CPU 推理:将模型加载到 CPU 设备上进行推理。
- NPU 推理:将模型加载到 Ascend NPU(npu:0)设备上,使用 torch_npu 扩展进行推理加速。
CPU/NPU 精度测试方法
- 加载预训练模型权重。
- 在 CPU 上进行推理,得到 logits 输出。
- 在 NPU 上进行推理,得到 logits 输出。
- 对比 CPU 和 NPU 输出的差异,评估指标包括:
- Max absolute diff: 最大绝对差异
- Mean absolute diff: 平均绝对差异
- MSE: 均方误差
- Cosine similarity: 余弦相似度
- Relative error (%): 相对误差百分比
- Class agreement (%): Top-1 类别一致率
CPU/NPU 精度测试结果
| 精度指标 | 数值 |
|---|
| Max absolute diff | 0.03553104 |
| Mean absolute diff | 0.00775075 |
| MSE | 0.00009949 |
| Cosine similarity | 0.99997932 |
| Relative error (%) | 0.0749% |
| Class agreement (%) | 100.00% |
结论:NPU 与 CPU 推理结果误差 < 1%(相对误差 0.0749%),精度通过验证。
性能测试结果
| 设备 | 推理耗时 (ms) |
|---|
| CPU | 16.20 |
| NPU (Ascend910) | 173.56 |
终端输出截图

标签
#+NPU #+CV #+图像分类 #+昇腾 #+Ascend #+PyTorch #+MobileNetV3