基于华为昇腾 NPU (Ascend 910) 适配的 IBM Granite Geospatial UK/I 洪水检测模型。
| 属性 | 描述 |
|---|---|
| 模型名称 | ibm-granite/granite-geospatial-uki-flooddetection |
| 任务 | 洪水/地表水语义分割 (Binary Semantic Segmentation) |
| 架构 | Prithvi-EO ViT Encoder + FCN Decoder |
| 参数量 | 91,505,154 (~91.5M) |
| 输入 | 9 波段多光谱 + SAR 卫星影像 (512×512 px) |
| 输入波段 | Blue, Green, Red, Narrow NIR, SWIR1, SWIR2, VV, VH, Cloud Mask |
| 输出 | 2 类分割图 (0: 无水, 1: 地表水) 512×512 px |
| 训练数据 | Copernicus EMS 英国/爱尔兰 6 次洪水事件 (69 景 512×512 影像) |
| 基础模型 | ibm-granite/granite-geospatial-uki |
| 原始框架 | TerraTorch / PyTorch Lightning |
| 项目 | 规格 |
|---|---|
| NPU 型号 | Ascend 910 (Atlas 800 A2) |
| NPU 数量 | 2 × Ascend910_9362 |
| CANN 版本 | CANN 8.5.1 |
| torch_npu 版本 | 2.9.0.post1 |
| PyTorch 版本 | 2.9.0 |
| Python 版本 | 3.11.14 |
| 操作系统 | Linux (aarch64) |
pip install modelscope
modelscope download --model ibm-granite/granite-geospatial-uki-flooddetection --local_dir ./modelpip install torch==2.9.0
pip install torch_npu==2.9.0
pip install numpypython3 inference.pyimport torch
import torch_npu
from inference import GraniteFloodDetection, load_model_from_checkpoint, prepare_input
# 加载模型
model = load_model_from_checkpoint(
'model/granite_geospatial_uki_flood_detection_v1.ckpt',
device='cpu'
)
# 迁移至 NPU
model_npu = GraniteFloodDetection()
model_npu.load_state_dict(model.state_dict())
model_npu = model_npu.to('npu:0')
model_npu.eval()
# 准备输入 (9 波段, 512×512)
input_data = prepare_input(batch_size=1, img_size=512, num_bands=9, device='npu:0')
# NPU 推理
with torch.no_grad():
output = model_npu(input_data) # [1, 2, 512, 512]
# 获取预测结果
prediction = output.argmax(dim=1) # [1, 512, 512], 0=无水, 1=地表水本模型适配采用了直接权重重构 (Direct Weight Reconstruction) 技术路线:
.ckpt 文件中提取完整的模型参数model.to('npu:0') 将模型自动迁移到 Ascend NPU| 适配项 | 方案 |
|---|---|
| 3D Patch Embedding | Conv3d(9, 768, kernel=(1,16,16)) 直接在 NPU 执行 |
| Position Embedding 插值 | 从 (14×14) 双三次插值到 (32×32) 适配 512×512 输入 |
| FCN Decoder | ConvTranspose2d + LayerNorm,原生支持 NPU |
| Segmentation Head | 嵌套 Sequential 结构保持权重 Key 兼容 |
问题:模型依赖 TerraTorch 框架和 granite_geospatial_uki 自定义 backbone,该模块托管于 GitHub,无法通过网络获取。
解决方案:通过分析 checkpoint 中 178 个参数层的 shape、dtype 和命名规律,重建完整的模型架构定义,实现零额外依赖的独立推理。
问题:checkpoint 中 pos_embed 为 [1, 197, 768](对应 224×224 预训练尺寸),但输入为 512×512(对应 1024+1=1025 tokens)。
解决方案:实现双三次插值 (bicubic interpolation) 将 14×14 的 position embedding 重采样至 32×32,适配任意输入尺寸。
| 指标 | CPU | NPU (Ascend 910) | 加速比 |
|---|---|---|---|
| 平均推理时间 | 22.39 s | 0.0311 s | 720.8× |
| 吞吐量 (FPS) | 0.045 | 32.19 | 715× |
| 标准差 | 0.174 s | 0.00008 s | - |
| 指标 | 值 | 要求 | 状态 |
|---|---|---|---|
| 最大绝对误差 | 0.001003 | < 0.01 | ✅ PASS |
| 平均绝对误差 | 0.000176 | < 0.01 | ✅ PASS |
| 平均相对误差 | 0.0034 (0.34%) | < 1% | ✅ PASS |
| 余弦相似度 | 0.999999 | > 0.999 | ✅ PASS |
| 像素级一致率 | 99.99% | > 99% | ✅ PASS |
精度结论:NPU 推理输出与 CPU 参考输出高度一致,所有精度指标均满足 < 1% 误差要求。
| 文件 | 说明 |
|---|---|
inference.py | NPU 推理与精度验证脚本(含完整模型定义) |
README.md | 模型部署文档(本文件) |
output/inference_results.json | 性能与精度量化数据 |
output/inference_full.log | 完整运行日志 |
model/ | 原始模型文件(ModelScope 下载) |
======================================================================
Granite Geospatial Flood Detection - Ascend NPU Inference
======================================================================
[ENV] torch_npu: 2.9.0.post1+gitee7ba04, NPU available: True
[ENV] NPU count: 2
[ENV] NPU device: Ascend910_9362
[1/5] Loading model from checkpoint...
Loading checkpoint from model/granite_geospatial_uki_flood_detection_v1.ckpt...
Loaded 178 parameters, 0 missing
Total parameters: 91,505,154
[2/5] Preparing input data...
[3/5] Running CPU inference (reference)...
CPU: 22.3913s avg, 0.04 FPS, output shape: [1, 2, 512, 512]
[4/5] Running NPU inference...
NPU: 0.0311s avg, 32.19 FPS, output shape: [1, 2, 512, 512]
[5/5] Precision validation (CPU vs NPU)...
Max absolute error: 0.001003
Mean absolute error: 0.000176
Mean relative error: 0.003443
Cosine similarity: 1.000009
Pixel accuracy match: 99.9924%
Precision check: PASSED (tolerance=0.01)
======================================================================
INFERENCE SUMMARY
======================================================================
Model: Granite Geospatial UK/I Flood Detection
Parameters: 91,505,154
Input: 9-band, 512x512 px
Output: 2-class segmentation (512x512)
CPU Inference:
Avg time: 22.3913s
FPS: 0.04
NPU Inference:
Avg time: 0.0311s
FPS: 32.19
Speedup vs CPU: 720.76x
Precision (CPU vs NPU):
Max abs error: 0.001003
Mean rel error: 0.003443
Cosine sim: 1.000009
Pixel match: 99.9924%
Status: PASSED本仓库中的所有内容(包括代码)由 IBM 根据相关开源软件许可提供,IBM 没有义务提供增强、更新或支持。IBM 开发人员将本代码作为开源项目(而非 IBM 产品)制作,IBM 不对质量或安全级别做任何声明,并且将来不会维护此代码。
NPU 适配完成日期:2026-05-20 | 适配平台:华为 Ascend 910 (Atlas 800 A2)