File size: 4,588 Bytes
c6fd460
 
 
 
 
be5358c
c6fd460
 
 
 
 
 
 
 
 
be5358c
c6fd460
 
 
 
be5358c
 
c6fd460
 
 
 
 
bddde0e
c6fd460
bddde0e
defb82f
 
 
be5358c
c6fd460
 
 
 
 
 
 
 
 
 
 
 
 
 
be5358c
 
 
 
defb82f
be5358c
 
031eb10
defb82f
031eb10
defb82f
be5358c
defb82f
be5358c
 
 
 
 
 
c6fd460
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
be5358c
 
c6fd460
 
be5358c
c6fd460
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bddde0e
c6fd460
be5358c
 
c6fd460
be5358c
c6fd460
 
 
 
 
 
 
 
 
031eb10
c6fd460
be5358c
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
"""
Debug script — inspect mfaytin/mask2former-satellite predictions class by class.

Usage:
    python debug_hardscape.py --address "17531 Madison St, Omaha, NE 68135"
    python debug_hardscape.py --address "..." --imagery google

This fetches the satellite image for the address and shows what each
semantic class looks like spatially, so we can see what the model
is calling 'building', 'pavement', 'road', etc.
"""

from __future__ import annotations

import argparse
import re

import numpy as np
import torch
import requests
import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt
from PIL import Image
from transformers import AutoImageProcessor, AutoModelForUniversalSegmentation
from dotenv import load_dotenv

load_dotenv()

from lawn_estimator.config import Config, OUTPUT_DIR  # noqa: E402
from lawn_estimator.regions import resolve_region  # noqa: E402
from lawn_estimator.sources.google import GoogleStaticMaps  # noqa: E402
from lawn_estimator.sources.imagery import CountyOrthoImagery, NaipImagery  # noqa: E402

MODEL_ID = "mfaytin/mask2former-satellite"
MODEL_LABELS = {
    0: "background",
    1: "bareland",
    2: "grass",
    3: "pavement",
    4: "road",
    5: "tree",
    6: "water",
    7: "cropland",
    8: "building",
}


def fetch_image(address: str, imagery: str = "naip") -> Image.Image:
    config = Config()
    session = requests.Session()

    region, geocode = resolve_region(address, config, session)
    print(f"  lat={geocode.latitude:.6f}, lon={geocode.longitude:.6f}")

    if imagery == "county":
        client = region.display_imagery or CountyOrthoImagery(session=session)
    elif imagery == "naip":
        client = NaipImagery(session)
    else:
        client = GoogleStaticMaps(config.google_maps_api_key, session)
    return client.fetch_satellite_image(
        latitude=geocode.latitude,
        longitude=geocode.longitude,
        zoom=config.zoom,
        image_size=config.image_size,
        scale=config.image_scale,
    )


def run_model(image: Image.Image) -> np.ndarray:
    print(f"  Loading {MODEL_ID}...")
    processor = AutoImageProcessor.from_pretrained(MODEL_ID)
    model = AutoModelForUniversalSegmentation.from_pretrained(MODEL_ID)
    model.eval()

    inputs = processor(images=image, return_tensors="pt")
    with torch.inference_mode():
        outputs = model(**inputs)

    maps = processor.post_process_semantic_segmentation(
        outputs, target_sizes=[image.size[::-1]]
    )
    return maps[0].detach().cpu().numpy()


def debug(address: str, imagery: str = "naip") -> None:
    print(f"\nDebug hardscape model for: {address} (imagery: {imagery})\n")

    print("[1/3] Fetching satellite image...")
    image = fetch_image(address, imagery)
    print(f"  Image size: {image.size}")

    print("\n[2/3] Running model...")
    prediction = run_model(image)

    unique = np.unique(prediction)
    print("\n  Classes detected:")
    for code in unique:
        label = MODEL_LABELS.get(int(code), f"unknown_{code}")
        count = (prediction == code).sum()
        print(f"    {code}: {label:<15} {count:>8,} px  ({count / prediction.size * 100:.1f}%)")

    print("\n[3/3] Rendering per-class visualization...")
    fig, axes = plt.subplots(3, 3, figsize=(16, 16))
    fig.suptitle(f"mask2former-satellite — per-class predictions\n{address}", fontsize=13)

    for idx, (code, label) in enumerate(MODEL_LABELS.items()):
        ax = axes[idx // 3][idx % 3]
        mask = prediction == code
        ax.imshow(image, alpha=0.6)
        if mask.any():
            ax.imshow(
                np.ma.masked_where(~mask, np.ones_like(mask, dtype=float)),
                cmap="Reds", vmin=0, vmax=1, alpha=0.65,
            )
        ax.set_title(
            f"[{code}] {label}\n{mask.sum():,} px ({mask.sum()/mask.size*100:.1f}%)",
            fontsize=10,
        )
        ax.axis("off")

    plt.tight_layout()

    out_dir = OUTPUT_DIR
    out_dir.mkdir(parents=True, exist_ok=True)
    sanitized = re.sub(r"[^\w\s-]", "", address).strip().replace(" ", "_")[:60]
    out_path = out_dir / f"debug_hardscape_{sanitized}.png"
    plt.savefig(out_path, dpi=120, bbox_inches="tight")
    plt.close(fig)
    print(f"\n  Saved to: {out_path}")


if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument(
        "--address", type=str,
        default="17531 Madison St, Omaha, NE 68135",
    )
    parser.add_argument("--imagery", choices=["naip", "google", "county"], default="naip")
    args = parser.parse_args()
    debug(args.address, args.imagery)