File size: 13,107 Bytes
5c2b385
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d84f453
5c2b385
 
 
 
 
 
 
 
 
 
 
 
 
 
d84f453
 
5c2b385
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d84f453
5c2b385
 
d84f453
5c2b385
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d84f453
5c2b385
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
"""PakFlood-LoRA demo: Sentinel-1 flood mapping with a LoRA-adapted TerraMind encoder (Gradio, CPU).

Run on Hugging Face Spaces or locally:   python app.py
Self-test (no UI):                        python app.py --selftest [tile id]
Files expected next to this script: assets/pakflood_lora_r32_state_dict_fp16.pt, samples/<tile>/{pre,post,label}.tif, samples.json
"""
import json
import sys
import time
from pathlib import Path

import numpy as np
import rasterio
import torch
from PIL import Image

HERE = Path(__file__).resolve().parent
CKPT = HERE / "assets" / "pakflood_lora_r32_state_dict_fp16.pt"
SAMPLES = HERE / "samples.json"
MEANS = np.array([-12.599, -20.293, -12.599, -20.293], dtype=np.float32)
STDS = np.array([5.195, 5.890, 5.195, 5.890], dtype=np.float32)
CROP, STRIDE, NCLS, BATCH = 256, 128, 3, 8
PALETTE = np.array([[230, 230, 230], [31, 119, 180], [214, 39, 40], [255, 255, 255]], dtype=np.uint8)  # bg, water, flood, ignore
CLASS_NAMES = ["background (dry land)", "permanent water", "flood"]

# exactly the model_args of the trained run (pakflood/configs/lora_r32_all.yaml), without downloading pretrained weights
MODEL_ARGS = {
    "backbone": "terratorch_terramind_v1_base",
    "backbone_pretrained": False,
    "backbone_in_chans": 4,
    "backbone_modalities": ["sen1grd"],
    "backbone_bands": {"sen1grd": ["VV", "VH", "VV", "VH"]},
    "necks": [
        {"name": "SelectIndices", "indices": [2, 5, 8, 11]},
        {"name": "ReshapeTokensToImage", "remove_cls_token": False},
        {"name": "LearnedInterpolateToPyramidal"},
    ],
    "decoder": "UNetDecoder",
    "decoder_channels": [512, 256, 128, 64],
    "num_classes": 3,
    "head_dropout": 0.3,
    "peft_config": {
        "method": "LORA",
        "replace_qkv": "qkv",
        "peft_config_kwargs": {"r": 32, "lora_alpha": 64, "lora_dropout": 0.05,
                                "target_modules": ["q_linear", "k_linear", "v_linear", "proj", "fc1", "fc2", "fc3"], "bias": "none"},
    },
}

_MODEL = None
DEVICE = torch.device("cuda" if torch.cuda.is_available() else "cpu")


def get_model():
    global _MODEL
    if _MODEL is None:
        from terratorch.models import EncoderDecoderFactory
        t0 = time.time()
        model = EncoderDecoderFactory().build_model(task="segmentation", **MODEL_ARGS)
        sd = torch.load(CKPT, map_location="cpu")
        sd = {k: (v.float() if v.is_floating_point() else v) for k, v in sd.items()}
        missing, unexpected = model.load_state_dict(sd, strict=False)
        if missing or unexpected:
            print(f"[warn] state dict: {len(missing)} missing, {len(unexpected)} unexpected keys", missing[:5], unexpected[:5])
        torch.set_num_threads(max(1, torch.get_num_threads()))
        _MODEL = model.to(DEVICE).eval()
        print(f"model ready in {time.time() - t0:.1f} s on {DEVICE}, {sum(p.numel() for p in model.parameters()) / 1e6:.1f} M parameters")
    return _MODEL


# ---------- data ----------
def read_s1_db(path):
    """2-band Sentinel-1 GRD GeoTIFF (VV, VH, linear intensity) -> (2, H, W) in dB, plus the raster profile."""
    with rasterio.open(path) as r:
        img = r.read().astype(np.float32)
        profile = r.profile
    if img.shape[0] < 2:
        raise ValueError("expected a 2-band GeoTIFF (VV, VH)")
    img = img[:2]
    np.nan_to_num(img, copy=False, nan=0.0)
    img[~np.isfinite(img) | (img <= 0) | (img > 1e3)] = 0.0
    img = np.maximum(img, np.finfo(np.float32).eps)
    return 10 * np.log10(img), profile


def to_input(pre_db, post_db):
    x = np.concatenate([pre_db, post_db]).astype(np.float32)
    return (x - MEANS[:, None, None]) / STDS[:, None, None]


def db_to_png(db):
    g = np.clip((db - (-25.0)) / 30.0, 0, 1)  # -25 .. +5 dB
    return Image.fromarray((g * 255).astype(np.uint8))


def cls_to_png(a):
    a = np.where(a == 255, 3, a).astype(np.uint8)
    return Image.fromarray(PALETTE[a])


# ---------- inference ----------
@torch.no_grad()
def predict(x):
    """x: (4, H, W) normalised. Sliding window 256 / stride 128, averaged softmax; returns class map (H, W) uint8."""
    model = get_model()
    _, H, W = x.shape
    ph = max(CROP, int(np.ceil(max(H - CROP, 0) / STRIDE)) * STRIDE + CROP)
    pw = max(CROP, int(np.ceil(max(W - CROP, 0) / STRIDE)) * STRIDE + CROP)
    xp = np.zeros((4, ph, pw), dtype=np.float32)
    xp[:, :H, :W] = x
    wins = [(y, xx) for y in range(0, ph - CROP + 1, STRIDE) for xx in range(0, pw - CROP + 1, STRIDE)]
    probs = torch.zeros((NCLS, ph, pw))
    count = torch.zeros((1, ph, pw))
    for i in range(0, len(wins), BATCH):
        chunk = wins[i:i + BATCH]
        batch = torch.from_numpy(np.stack([xp[:, y:y + CROP, xx:xx + CROP] for y, xx in chunk])).to(DEVICE)
        out = model(batch)
        logits = out.output if hasattr(out, "output") else out
        p = torch.softmax(logits.float(), dim=1).cpu()
        for j, (y, xx) in enumerate(chunk):
            probs[:, y:y + CROP, xx:xx + CROP] += p[j]
            count[:, y:y + CROP, xx:xx + CROP] += 1
    return (probs / count).argmax(0).to(torch.uint8).numpy()[:H, :W]


def metrics(pred, label):
    v = label != 255
    tp = int(((pred == 2) & (label == 2) & v).sum())
    fp = int(((pred == 2) & (label != 2) & v).sum())
    fn = int(((pred != 2) & (label == 2) & v).sum())
    iou = tp / max(tp + fp + fn, 1)
    return {"flood IoU": iou, "flood F1": 2 * tp / max(2 * tp + fp + fn, 1), "precision": tp / max(tp + fp, 1), "recall": tp / max(tp + fn, 1)}


def area_km2(mask, profile):
    res = profile.get("transform", None)
    px = abs(res.a * res.e) / 1e6 if res is not None else 1e-4
    return float(mask.sum() * px)


# ---------- samples ----------
def load_samples():
    if not SAMPLES.exists():
        return {}
    return json.loads(SAMPLES.read_text())


def run_pair(pre_path, post_path, label_path=None, progress=None):
    t0 = time.time()
    pre_db, prof = read_s1_db(pre_path)
    post_db, _ = read_s1_db(post_path)
    if pre_db.shape != post_db.shape:
        raise ValueError(f"pre and post tiles differ in size: {pre_db.shape} vs {post_db.shape}")
    x = to_input(pre_db, post_db)
    pred = predict(x)
    label = None
    if label_path and Path(label_path).exists():
        with rasterio.open(label_path) as r:
            label = r.read(1)
        pred_show = np.where(label == 255, 255, pred)
    else:
        pred_show = pred
    secs = time.time() - t0
    lines = [f"**Inference time:** {secs:.1f} s on {'GPU' if DEVICE.type == 'cuda' else 'CPU'} ({x.shape[1]} × {x.shape[2]} px, {len(range(0, max(x.shape[1] - CROP, 0) + 1, STRIDE)) ** 2 if x.shape[1] == x.shape[2] else '-'} windows)",
             f"**Predicted flood area:** {area_km2((pred_show == 2), prof):,.1f} km²  ·  permanent water {area_km2((pred_show == 1), prof):,.1f} km²"]
    if label is not None:
        m = metrics(pred, label)
        lines.append(f"**Against the CEMS analyst label (never used in training):** flood IoU **{m['flood IoU']:.3f}**, F1 {m['flood F1']:.3f}, precision {m['precision']:.3f}, recall {m['recall']:.3f}")
        lines.append(f"**Official flood area:** {area_km2((label == 2), prof):,.1f} km²")
    return pred_show, prof, db_to_png(pre_db[0]), db_to_png(post_db[0]), (cls_to_png(label) if label is not None else None), cls_to_png(pred_show), "\n\n".join(lines)


# ---------- UI ----------
def build_ui():
    import gradio as gr
    samples = load_samples()
    ids = list(samples)
    header = """# PakFlood-LoRA: flood mapping from Sentinel-1 radar
A frozen **TerraMind v1 base** encoder with a **rank-32 LoRA adapter** (5.7 M trained encoder weights) and a UNet decoder, trained on GEOID-Flood.
Input: the pre-event and post-event Sentinel-1 GRD images (VV, VH). Output: every 10 m pixel as dry land, permanent water or **flood**.
Official test flood IoU 0.525 (equal to full fine-tuning of all 101 M weights). Runs on CPU here, about 30 to 90 s per 1024 × 1024 tile.
"""
    legend = "Legend: grey = background, blue = permanent water, red = flood, white = outside the mapped area."
    with gr.Blocks(title="PakFlood-LoRA") as demo:
        gr.Markdown(header)
        with gr.Tab("Pakistan test tiles (EMSR838, August 2025)"):
            gr.Markdown("These are the 15 **official test tiles** of the Pakistan 2025 activation: the model never saw them in training. Pick one and run the model.")
            dd = gr.Dropdown(choices=[(f"{k}  (official flood {v.get('flood_km2_label', 0):.1f} km²)", k) for k, v in samples.items()], value=ids[0] if ids else None, label="Tile")
            with gr.Row():
                pre_i = gr.Image(label="pre-event S1 VV (dB)", type="pil", height=300)
                post_i = gr.Image(label="post-event S1 VV (dB)", type="pil", height=300)
                lab_i = gr.Image(label="CEMS official label", type="pil", height=300)
            run = gr.Button("Run PakFlood-LoRA on this tile", variant="primary")
            with gr.Row():
                pred_i = gr.Image(label="model prediction", type="pil", height=360)
                info = gr.Markdown()
            gr.Markdown(legend)

            def show(k):
                s = samples[k]
                pre_db, _ = read_s1_db(HERE / s["pre"]); post_db, _ = read_s1_db(HERE / s["post"])
                with rasterio.open(HERE / s["label"]) as r:
                    lab = r.read(1)
                return db_to_png(pre_db[0]), db_to_png(post_db[0]), cls_to_png(lab)

            def run_sample(k):
                s = samples[k]
                _, _, _, _, _, pred_png, text = run_pair(HERE / s["pre"], HERE / s["post"], HERE / s["label"])
                return pred_png, text

            if ids:
                demo.load(show, dd, [pre_i, post_i, lab_i])
            dd.change(show, dd, [pre_i, post_i, lab_i])
            run.click(run_sample, dd, [pred_i, info])

        with gr.Tab("Your own Sentinel-1 pair"):
            gr.Markdown("Upload a **pre-event** and a **post-event** Sentinel-1 GRD GeoTIFF of the same area (2 bands: VV, VH in linear intensity, 10 m, same size; 256 px or larger). The output map is also returned as a GeoTIFF.")
            with gr.Row():
                pre_f = gr.File(label="pre-event GeoTIFF", file_types=[".tif", ".tiff"])
                post_f = gr.File(label="post-event GeoTIFF", file_types=[".tif", ".tiff"])
            run2 = gr.Button("Run PakFlood-LoRA", variant="primary")
            with gr.Row():
                u_pre = gr.Image(label="pre-event VV (dB)", type="pil", height=280)
                u_post = gr.Image(label="post-event VV (dB)", type="pil", height=280)
                u_pred = gr.Image(label="model prediction", type="pil", height=280)
            u_info = gr.Markdown()
            u_file = gr.File(label="prediction GeoTIFF (0 background, 1 permanent water, 2 flood)")

            def run_upload(pre, post):
                if pre is None or post is None:
                    raise gr.Error("Please upload both the pre-event and the post-event GeoTIFF.")
                pre_p = pre if isinstance(pre, str) else pre.name
                post_p = post if isinstance(post, str) else post.name
                pred, prof, pre_png, post_png, _, pred_png, text = run_pair(pre_p, post_p)
                out = Path(pre_p).with_name("pakflood_prediction.tif")
                prof.update(count=1, dtype="uint8", nodata=255, compress="deflate")
                with rasterio.open(out, "w", **prof) as w:
                    w.write(pred.astype(np.uint8), 1)
                return pre_png, post_png, pred_png, text, str(out)

            run2.click(run_upload, [pre_f, post_f], [u_pre, u_post, u_pred, u_info, u_file])
            gr.Markdown(legend)

        gr.Markdown("""### About
MS AI deep learning course project, NED University of Engineering & Technology (Muhammad Shahwar Uddin, supervised by Dr. Sharaf Hussain).
Data: GEOID-Flood v1.1 (Fondazione LINKS, CC BY 4.0), built from Copernicus Emergency Management Service activations. Model: TerraMind v1 base (IBM and ESA) through TerraTorch, LoRA via Hugging Face PEFT.
Results on the official GEOID-Flood test split (2,417 tiles): frozen encoder 0.433, LoRA r16 0.493, **LoRA r32 0.525**, full fine-tune 0.525 flood IoU. On Pakistan's 15 official test tiles: LoRA r32 0.786.
The sample tiles are the official test tiles of activation EMSR838 (Punjab, August 2025) and were never used for training.""")
    return demo


if __name__ == "__main__":
    if "--selftest" in sys.argv:
        samples = load_samples()
        k = sys.argv[sys.argv.index("--selftest") + 1] if len(sys.argv) > sys.argv.index("--selftest") + 1 else next(iter(samples))
        s = samples[k]
        print("self-test on", k, "| torch threads:", torch.get_num_threads(), "| cuda:", torch.cuda.is_available())
        _, _, _, _, _, pred_png, text = run_pair(HERE / s["pre"], HERE / s["post"], HERE / s["label"])
        pred_png.save(HERE / "selftest_prediction.png")
        print(text.replace("**", ""))
        print("expected flood IoU from the evaluation run:", s.get("iou_r32"))
    else:
        get_model()
        build_ui().launch()