tiny_vit_11m_224.dist_in22k_ft_in1k 是基于 TinyViT (Tiny Vision Transformer) 架构的图像分类模型。
图像分类
RGB 图像,224x224,归一化 mean=[0.485, 0.456, 0.406],std=[0.229, 0.224, 0.225]
1000 类 logits,通过 Softmax 转换为概率
| 组件 | 版本 |
|---|---|
| NPU | Ascend910 |
| CANN | 25.5.2 |
| PyTorch | 2.9.0 |
| torch_npu | 2.9.0.post1+gitee7ba04 |
| timm | 1.0.27 |
使用 ModelScope 下载权重,通过 torch_npu 加载到 NPU,FP32 推理,无需修改模型代码。
pip install torch torchvision timm modelscope safetensors Pillow# CPU 推理
python inference.py --device cpu
# NPU 推理
python inference.py --device npu
# 精度对比
python compare_cpu_npu.py| 指标 | CPU | NPU |
|---|---|---|
| 平均推理耗时 | 136.95 ms | 8.05 ms |
| 加速比 | - | 17.00x |
| 排名 | CPU 类别 | CPU 概率 | NPU 类别 | NPU 概率 |
|---|---|---|---|---|
| 1 | 549 | 0.004810 | 549 | 0.004786 |
| 2 | 680 | 0.004598 | 680 | 0.004567 |
| 3 | 700 | 0.004160 | 700 | 0.004135 |
| 4 | 434 | 0.003974 | 434 | 0.003949 |
| 5 | 844 | 0.003880 | 844 | 0.003848 |
| 指标 | 值 |
|---|---|
| Logits Max Abs Error | 2.0700e-02 |
| Logits Mean Abs Error | 5.3800e-03 |
| Probs Max Abs Error | 5.1000e-05 |
| Probs Mean Abs Error | 5.7800e-06 |
| Cosine Similarity | 0.99990970 |
| Prob Relative Error | 0.7713% |
| Top-1 Class Match | Yes |
NPU 与 CPU 推理结果误差 < 1%, 精度对齐通过。余弦相似度 0.99990970, Top-1 类别完全一致。

from timm import create_model
from modelscope import snapshot_download
from safetensors.torch import load_file
model = create_model('tiny_vit_11m_224.dist_in22k_ft_in1k', pretrained=False)
local_path = snapshot_download('timm/tiny_vit_11m_224.dist_in22k_ft_in1k')
state_dict = load_file(local_path + '/model.safetensors')
model.load_state_dict(state_dict, strict=False)
model = model.to('npu:0').float()torch>=2.0.0,torchvision>=0.15.0,timm>=1.0.0,modelscope>=1.0.0,safetensors,Pillow