遥感图像分类模型在华为昇腾 NPU 上的适配部署。
本模型用于遥感图像二分类任务,支持三种经典卷积神经网络架构:AlexNet、ResNet34、VGG16。模型基于 PyTorch 框架实现,已适配华为昇腾 NPU (Ascend 910) 进行推理加速。
| 项目 | 内容 |
|---|---|
| 原始模型地址 | https://www.modelscope.cn/models/qzy3386401548/Remote_sensing_image_classificatin |
| 任务类型 | 图像分类 (Image Classification) |
| 模型框架 | PyTorch |
| 输入格式 | RGB 图像 (3, 224, 224),归一化到 [-1, 1] |
| 输出格式 | 二分类 logits,经 softmax 后得到类别概率 |
| 类别数量 | 2 |
| 支持架构 | AlexNet, ResNet34, VGG16 |
| 许可证 | Apache License 2.0 |
pip install -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple确保已安装 torch_npu:
python3 -c "import torch; import torch_npu; print('NPU available:', torch.npu.is_available())"# 安装 ModelScope
pip install modelscope -i https://pypi.tuna.tsinghua.edu.cn/simple
# 下载模型
from modelscope import snapshot_download
model_dir = snapshot_download('qzy3386401548/Remote_sensing_image_classificatin')# 单架构推理
python inference.py --arch resnet34 --device cpu
# 全部架构推理
python inference.py --arch all --device cpu --output cpu_results.json# 单架构推理
python inference.py --arch resnet34 --device npu
# 全部架构推理
python inference.py --arch all --device npu --output npu_results.jsonpython compare_cpu_npu.py --arch all --output compare_results.json本模型使用原生 PyTorch 算子,无需 Triton 或 CUDA 自定义算子,可直接运行在华为昇腾 NPU 上。适配主要涉及的修改:
model.to("npu:0"), tensor.to("npu:0"))torch.npu.synchronize() 确保计算完成torch.npu.empty_cache())| 设备 | 预测类别 | 类别0概率 | 类别1概率 | 推理耗时 |
|---|---|---|---|---|
| CPU | 0 | 0.50604665 | 0.49395332 | 0.5177s |
| NPU | 0 | 0.50604600 | 0.49395403 | 1.6881s |
| 设备 | 预测类别 | 类别0概率 | 类别1概率 | 推理耗时 |
|---|---|---|---|---|
| CPU | 0 | 0.50356311 | 0.49643686 | 0.2834s |
| NPU | 0 | 0.50356048 | 0.49643952 | 0.1745s |
| 设备 | 预测类别 | 类别0概率 | 类别1概率 | 推理耗时 |
|---|---|---|---|---|
| CPU | 0 | 0.50004941 | 0.49995056 | 1.4560s |
| NPU | 0 | 0.50004941 | 0.49995056 | 0.7923s |
| 指标 | AlexNet | ResNet34 | VGG16 |
|---|---|---|---|
| Logits MAE | 1.39×10⁻⁶ | 5.30×10⁻⁶ | 3.28×10⁻⁸ |
| Logits Max AbsErr | 1.58×10⁻⁶ | 9.00×10⁻⁶ | 3.70×10⁻⁸ |
| Logits Mean RelErr | 0.0116% | 0.0145% | 0.0042% |
| Probs MAE | 6.85×10⁻⁷ | 2.64×10⁻⁶ | 0.0 |
| Probs Max AbsErr | 7.15×10⁻⁷ | 2.65×10⁻⁶ | 0.0 |
| Cosine Similarity | 0.9999998 | 0.9999999 | 0.9999178 |
| Top-1 Match | ✅ 一致 | ✅ 一致 | ✅ 一致 |
结论:NPU 与 CPU 推理结果误差 < 1%,精度完全达标。
| 架构 | CPU 推理耗时 | NPU 推理耗时 | 加速比 |
|---|---|---|---|
| AlexNet | 0.5177s | 1.6881s | 0.31× |
| ResNet34 | 0.2834s | 0.1745s | 1.62× |
| VGG16 | 1.4560s | 0.7923s | 1.84× |
注:AlexNet 在 NPU 上首次推理因包含设备初始化和 kernel 编译开销,耗时较长。ResNet34 和 VGG16 在 NPU 上均获得了显著的推理加速。对于批量推理和实际部署场景,NPU 的吞吐量优势会更加明显。
import torch
import numpy as np
from resnet34 import ResNet34
# 加载模型
model = ResNet34(num_classes=2)
# 移动到 NPU
model = model.to("npu:0")
model.eval()
# 准备输入 (模拟遥感图像)
dummy_input = torch.randn(1, 3, 224, 224).to("npu:0")
# 推理
with torch.no_grad():
output = model(dummy_input)
probs = torch.softmax(output, dim=-1)
pred_class = torch.argmax(output, dim=-1)
print(f"预测类别: {pred_class.item()}, 概率: {probs.cpu().tolist()}")import torch
from resnet34 import ResNet34
model = ResNet34(num_classes=2).to("npu:0")
model.eval()
# 批量输入
batch = torch.randn(16, 3, 224, 224).to("npu:0")
with torch.no_grad():
outputs = model(batch)
predictions = torch.argmax(outputs, dim=-1)
print(f"批量预测结果: {predictions.cpu().tolist()}")本仓库提供完整的推理脚本,支持 CPU 和 NPU 双平台推理:
# NPU 推理
python3 inference.py --device npu
# CPU 推理
python3 inference.py --device cpu推理完成后会输出推理结果和耗时,表明模型在 NPU 上推理成功。
#+NPU #+CV #+昇腾 #图像分类 #遥感 #PyTorch #Ascend
| 文件 | 说明 |
|---|---|
inference.py | 独立推理脚本,支持 CPU/NPU 和三种架构 |
compare_cpu_npu.py | CPU/NPU 精度对比脚本 |
requirements.txt | Python 依赖列表 |
readme.md | 本说明文档 |
model_files/ | 模型架构源文件 (alexnet.py, resnet34.py, vgg16.py) |
compare_results.json | CPU/NPU 精度对比详细结果 |
cpu_inference_results.json | CPU 推理结果 |
npu_inference_results.json | NPU 推理结果 |