tiny_vit_21m_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 |
|---|---|---|
| 平均推理耗时 | 232.50 ms | 8.12 ms |
| 加速比 | - | 28.64x |
| 排名 | CPU 类别 | CPU 概率 | NPU 类别 | NPU 概率 |
|---|---|---|---|---|
| 1 | 405 | 0.004439 | 405 | 0.004455 |
| 2 | 700 | 0.004405 | 700 | 0.004435 |
| 3 | 680 | 0.004390 | 680 | 0.004405 |
| 4 | 647 | 0.004102 | 647 | 0.004120 |
| 5 | 549 | 0.004042 | 549 | 0.004038 |
| 指标 | 值 |
|---|---|
| Logits Max Abs Error | 1.2800e-02 |
| Logits Mean Abs Error | 2.3600e-03 |
| Probs Max Abs Error | 4.4400e-05 |
| Probs Mean Abs Error | 2.5800e-06 |
| Cosine Similarity | 0.99998820 |
| Prob Relative Error | 0.3608% |
| Top-1 Class Match | Yes |
NPU 与 CPU 推理结果误差 < 1%,精度对齐通过。余弦相似度为 0.99998820,Top-1 类别完全一致。

from timm import create_model
from modelscope import snapshot_download
from safetensors.torch import load_file
model = create_model('tiny_vit_21m_224.dist_in22k_ft_in1k', pretrained=False)
local_path = snapshot_download('timm/tiny_vit_21m_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