Swin Transformer (Shifted Window Transformer) 是一种基于层级式移动窗口的 Vision Transformer 架构,由 Microsoft Research 提出。该模型通过引入移位窗口分区机制,在保持线性计算复杂度的同时,实现了跨窗口的信息交互,适用于图像分类任务。
本仓库提供 microsoft/swin-large-patch4-window7-224 在华为昇腾 NPU 上的适配与推理实现,包含完整的推理脚本、精度对比工具和测试结果。
图像分类 (Image Classification - ImageNet-1K, 1000 classes)
| 参数 | 值 |
|---|---|
| 参数量 | 197M |
| embed_dim | 192 |
| depths | [2, 2, 18, 2] |
| num_heads | [6, 12, 24, 48] |
| image_size | 224 |
| window_size | 7 |
| patch_size | 4 |
| num_classes | 1000 (ImageNet) |
| 组件 | 版本 |
|---|---|
| Python | 3.11 |
| PyTorch | 2.9.0+cpu |
| torch_npu | 2.9.0.post1 |
| Transformers | 4.57.6 |
| ModelScope | 1.35.3 |
| CANN | 8.5.1 |
| NPU | Ascend910 |
| OS | Linux (aarch64) |
该模型使用 HuggingFace Transformers 框架的 SwinForImageClassification 实现,在昇腾 NPU 上无需额外修改即可运行。适配过程:
snapshot_download)SwinForImageClassification.from_pretrained() 加载模型.to("npu:0") 将模型移至 NPU 设备AutoImageProcessor 进行图像预处理# 安装依赖(使用清华 PyPI 镜像)
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple torch torchvision transformers modelscope pillow numpycd swin-large-patch4-window7-224
python3 inference.py --device cpucd swin-large-patch4-window7-224
python3 inference.py --device npucd swin-large-patch4-window7-224
python3 inference.py --device npu --image /path/to/your/image.jpg使用合成测试图像(包含天空、草地、太阳、树木等元素的 224x224 图像)进行推理。
| Rank | Class ID | Probability |
|---|---|---|
| 1 | 610 | ~0.666 |
| 2 | 549 | ~0.121 |
| 3 | 916 | ~0.011 |
| 4 | 417 | ~0.008 |
| 5 | 723 | ~0.005 |
预测类别: 610
| Rank | Class ID | Probability |
|---|---|---|
| 1 | 610 | ~0.666 |
| 2 | 549 | ~0.120 |
| 3 | 916 | ~0.011 |
| 4 | 417 | ~0.008 |
| 5 | 723 | ~0.005 |
预测类别: 610
| 指标 | 值 |
|---|---|
| 最大绝对 Logit 差异 | 0.01777637 |
| 平均绝对 Logit 差异 | 0.00221416 |
| 最大绝对概率差异 | 0.00094752 |
| 平均绝对概率差异 | 0.00000578 |
| 最大相对误差 | 0.2803% |
| 平均相对误差 | 0.0349% |
| Logits 余弦相似度 | 0.99999589 |
| 概率余弦相似度 | 0.99998128 |
| CPU 预测类别 | 610 |
| NPU 预测类别 | 610 |
| Top-1 类别一致 | 是 |
| Top-5 类别一致 | 是 |
NPU 与 CPU 推理结果误差 < 1%(最大相对误差: 0.2803%)。
NPU 与 CPU 的推理结果在数值上高度一致,余弦相似度达到 0.9999 以上,Top-1 和 Top-5 预测类别完全一致。昇腾 NPU (Ascend910) 在该模型上的推理精度完全满足要求。
| 设备 | 推理耗时 (ms) | 加速比 |
|---|---|---|
| CPU (Intel Xeon, 4 threads) | 1156.44 | 1x |
| NPU (Ascend910) | 265.93 | 4.35x |
NPU 推理相比 CPU 推理显著加速,特别是在大模型上 NPU 优势更加明显。

swin-large-patch4-window7-224/
├── inference.py # NPU/CPU 推理脚本
├── compare_cpu_npu.py # CPU vs NPU 精度对比脚本
├── requirements.txt # 依赖包列表
├── precision_results.json # 精度测试结果 (JSON)
├── precision_test.log # 精度测试日志
├── terminal_screenshot.png # 模拟终端输出截图
└── README.md # 本文件import torch
import torch_npu
from PIL import Image
from transformers import AutoImageProcessor, SwinForImageClassification
model_name = "microsoft/swin-large-patch4-window7-224"
model_dir = "./models"
processor = AutoImageProcessor.from_pretrained(model_dir)
model = SwinForImageClassification.from_pretrained(model_dir)
model.eval()
model.to("npu:0")
image = Image.open("test.jpg").convert("RGB")
inputs = processor(images=image, return_tensors="pt")
inputs = {k: v.to("npu:0") for k, v in inputs.items()}
with torch.no_grad():
outputs = model(**inputs)
logits = outputs.logits
predicted = torch.argmax(logits, dim=-1).item()
print(f"Predicted class: {predicted}")python3 compare_cpu_npu.py该脚本会依次在 CPU 和 NPU 上运行推理,输出详细的精度对比结果,并保存在 precision_results.json 中。
以下日志展示了 NPU 推理成功的关键信息:
Using a slow image processor as `use_fast` is unset and a slow processor was saved with this model. `use_fast=True` will be the default behavior in v4.52, even if the model was saved with a slow processor. This will result in minor differences in outputs. You'll still be able to use a slow processor with `use_fast=False`.
CPU inference time: 1156.44 ms
NPU inference time: 265.93 ms
[3/4] Comparing CPU vs NPU results...
CPU vs NPU Precision Comparison Results
Top-5 Match 1