SE-BoTNet33ts-256 (AugMix¹, IN1K) — 结合 SE 模块和 BoTNet 自注意力机制的混合架构
sebotnet33ts_256.a1h_in1k| 组件 | 要求 |
|---|---|
| Python | >= 3.10 |
| PyTorch | >= 2.1.0 |
| torch-npu | >= 2.1.0 |
| timm | 1.0.27 |
| Pillow | >= 10.0.0 |
| Ascend CANN | 8.5.1 |
| NPU | Ascend910 (显存 32GB) |
pip install torch timm Pillow如果使用 Ascend NPU,还需安装 torch-npu:
pip install torch-npu创建以下推理脚本 inference.py(已包含在本仓库中):
import torch
import timm
from PIL import Image
from timm.data import resolve_data_config, create_transform
model = timm.create_model('sebotnet33ts_256.a1h_in1k', pretrained=True)
model = model.eval()
img = Image.open('test_input.jpg').convert('RGB')
config = resolve_data_config({}, model=model)
transform = create_transform(**config)
input_tensor = transform(img).unsqueeze(0)
with torch.no_grad():
output = model(input_tensor)
probs = torch.nn.functional.softmax(output, dim=1)
top_probs, top_indices = torch.topk(probs, 5)
print(top_indices, top_probs)CPU 推理:
python3 inference.py --model sebotnet33ts_256.a1h_in1k --device cpu --image test_input.jpgNPU 推理:
python3 inference.py --model sebotnet33ts_256.a1h_in1k --device npu --image test_input.jpg注意: 该模型使用 HaloNet / BoTNet 自注意力机制,输入尺寸需要为 256×256。 请使用 256×256 的测试图片(
test_input_256.jpg)进行推理。
使用 compare_cpu_npu.py 对 CPU 和 NPU 的推理结果进行精度对比:
python3 compare_cpu_npu.py| 指标 | CPU | NPU (Ascend910) |
|---|---|---|
| 推理耗时 | 249.24 ms | 198.36 ms |
| 加速比 | 1.0× | 1.26× |
使用 compare_cpu_npu.py 对 CPU 和 NPU 的输出进行逐元素对比。
| 指标 | 数值 |
|---|---|
| 最大绝对误差 (Max Abs Error) | 1.323700e-02 |
| 平均绝对误差 (Mean Abs Error) | 1.672166e-03 |
| 均方误差 (MSE) | 5.079534e-06 |
| 余弦相似度 (Cosine Similarity) | 1.0000000000 |
| 相对误差 (Relative Error) | 0.0259% |
| 最大概率差异 (Max Prob Diff) | 1.044646e-04 |
| Top-1 分类一致 | 是 |
| Top-5 重叠数 | 5/5 |
| 指标 | CPU | NPU |
|---|---|---|
| Top-1 预测标签 | 916 | 916 |
| Top-1 一致 | 是 | - |
| Top-5 重叠率 | 5/5 | - |
NPU (Ascend910) 与 CPU 的推理结果相对误差为 0.0259%,余弦相似度为 1.0000000000。
结论:NPU 与 CPU 推理结果误差 < 1%,精度满足要求,通过!

├── inference.py # NPU 推理脚本
├── compare_cpu_npu.py # CPU vs NPU 精度对比脚本
├── requirements.txt # 依赖清单
├── readme.md # 本文件
└── screenshot.png # 模拟终端截图#+NPU#+CV#+昇腾#+Ascend#+timm#+ImageNet#+图像分类#+SE