g
gcw_AVRCax4T/granite-geospatial-uki-flooddetection
模型介绍文件和版本Pull Requests讨论分析
下载使用量0

Granite Geospatial UK/I Flood Detection (Ascend NPU 适配版)

基于华为昇腾 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)

快速开始

1. 下载模型

pip install modelscope
modelscope download --model ibm-granite/granite-geospatial-uki-flooddetection --local_dir ./model

2. 环境依赖

pip install torch==2.9.0
pip install torch_npu==2.9.0
pip install numpy

3. 运行推理

python3 inference.py

4. 推理代码示例

import 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) 技术路线:

  1. 解析 PyTorch Lightning 检查点:从 .ckpt 文件中提取完整的模型参数
  2. 重建模型架构:基于 timm 和 PyTorch 原生组件重构 Prithvi-EO ViT + FCN Decoder
  3. 权重复制与验证:将原始权重映射到重构模型,验证 178 个参数层完整加载
  4. NPU 自动迁移:通过 model.to('npu:0') 将模型自动迁移到 Ascend NPU

关键适配点

适配项方案
3D Patch EmbeddingConv3d(9, 768, kernel=(1,16,16)) 直接在 NPU 执行
Position Embedding 插值从 (14×14) 双三次插值到 (32×32) 适配 512×512 输入
FCN DecoderConvTranspose2d + LayerNorm,原生支持 NPU
Segmentation Head嵌套 Sequential 结构保持权重 Key 兼容

技术难点与解决方案

1. 自定义 Backbone 依赖

问题:模型依赖 TerraTorch 框架和 granite_geospatial_uki 自定义 backbone,该模块托管于 GitHub,无法通过网络获取。

解决方案:通过分析 checkpoint 中 178 个参数层的 shape、dtype 和命名规律,重建完整的模型架构定义,实现零额外依赖的独立推理。

2. Position Embedding 尺寸不匹配

问题:checkpoint 中 pos_embed 为 [1, 197, 768](对应 224×224 预训练尺寸),但输入为 512×512(对应 1024+1=1025 tokens)。

解决方案:实现双三次插值 (bicubic interpolation) 将 14×14 的 position embedding 重采样至 32×32,适配任意输入尺寸。

性能测试

推理性能

指标CPUNPU (Ascend 910)加速比
平均推理时间22.39 s0.0311 s720.8×
吞吐量 (FPS)0.04532.19715×
标准差0.174 s0.00008 s-

精度验证 (CPU vs NPU)

指标值要求状态
最大绝对误差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.pyNPU 推理与精度验证脚本(含完整模型定义)
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

许可与致谢

  • 模型许可:Apache 2.0
  • 原始模型:ibm-granite/granite-geospatial-uki-flooddetection
  • 基础模型:ibm-granite/granite-geospatial-uki
  • 作者:Helen Tamura-Wicks, Andrew Taylor, Chris Dearden, Geoffrey Dawson, Paolo Fraccaro, Anne Jones (IBM / Hartree Centre)
  • NPU 适配:基于 Ascend NPU 的独立推理适配

免责声明

本仓库中的所有内容(包括代码)由 IBM 根据相关开源软件许可提供,IBM 没有义务提供增强、更新或支持。IBM 开发人员将本代码作为开源项目(而非 IBM 产品)制作,IBM 不对质量或安全级别做任何声明,并且将来不会维护此代码。


NPU 适配完成日期:2026-05-20 | 适配平台:华为 Ascend 910 (Atlas 800 A2)