File size: 11,060 Bytes
252fe2c
 
 
 
 
6312a59
252fe2c
6312a59
 
 
 
 
252fe2c
6312a59
 
 
252fe2c
 
 
6312a59
252fe2c
6312a59
 
252fe2c
6312a59
 
 
252fe2c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6312a59
252fe2c
6312a59
 
252fe2c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6312a59
 
252fe2c
 
6312a59
252fe2c
6312a59
252fe2c
 
 
 
 
6312a59
 
252fe2c
6312a59
 
 
 
252fe2c
 
 
6312a59
 
252fe2c
6312a59
252fe2c
6312a59
252fe2c
 
 
 
 
 
 
 
 
6312a59
 
252fe2c
6312a59
 
252fe2c
 
 
 
 
 
 
 
 
 
6312a59
 
252fe2c
6312a59
252fe2c
6312a59
 
252fe2c
6312a59
 
252fe2c
6312a59
 
 
252fe2c
 
 
6312a59
 
 
 
252fe2c
6312a59
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
252fe2c
 
 
 
 
 
 
6312a59
 
252fe2c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6312a59
 
 
252fe2c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6312a59
252fe2c
 
 
 
6312a59
 
252fe2c
 
 
 
 
 
6312a59
252fe2c
 
 
 
 
 
 
 
 
6312a59
252fe2c
 
 
 
 
 
6312a59
252fe2c
6312a59
252fe2c
 
 
6312a59
252fe2c
 
 
 
 
 
 
 
 
6312a59
252fe2c
6312a59
252fe2c
6312a59
252fe2c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6312a59
 
252fe2c
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
import gc
from pathlib import Path

import gradio as gr
import matplotlib.cm as cm
import numpy as np
import spaces
import torch
import torch.nn.functional as F
from PIL import Image, ImageOps
from transformers import AutoImageProcessor, AutoModel

# Device configuration with memory management
DEVICE = "cuda" if torch.cuda.is_available() else "cpu"

MODEL_MAP = {
    "DINOv3 ViT-L/16 Satellite": "facebook/dinov3-vitl16-pretrain-sat493m",
    "DINOv3 ViT-L/16 LVD (General Web)": "facebook/dinov3-vitl16-pretrain-lvd1689m",
    "⚠️ DINOv3 ViT-7B/16 Satellite": "facebook/dinov3-vit7b16-pretrain-sat493m",
}

DEFAULT_NAME = list(MODEL_MAP.keys())[0]

# Global model state
processor = None
model = None


def cleanup_memory():
    """Aggressive memory cleanup for model switching"""
    global processor, model

    if model is not None:
        del model
        model = None

    if processor is not None:
        del processor
        processor = None

    gc.collect()

    if torch.cuda.is_available():
        torch.cuda.empty_cache()
        torch.cuda.synchronize()


def load_model(name):
    """Load model with proper memory management and dtype handling"""
    global processor, model

    try:
        # Clean up existing model
        cleanup_memory()

        model_id = MODEL_MAP[name]

        # Load with auto dtype for optimal performance
        processor = AutoImageProcessor.from_pretrained(model_id)

        # Determine optimal dtype based on model size and hardware
        if "7b" in model_id.lower() and torch.cuda.is_available():
            # For 7B model, use bfloat16 if available for memory efficiency
            dtype = torch.bfloat16 if torch.cuda.is_bf16_supported() else torch.float16
        else:
            dtype = torch.float32

        model = AutoModel.from_pretrained(
            model_id,
            torch_dtype=dtype,
            device_map="auto" if torch.cuda.is_available() else None,
        )

        if DEVICE == "cuda" and not hasattr(model, "device_map"):
            model = model.to(DEVICE)

        model.eval()

        # Get model info
        param_count = sum(p.numel() for p in model.parameters()) / 1e9
        dtype_str = str(dtype).split(".")[-1]

        return f"βœ… Loaded: {name} | {param_count:.1f}B params | {dtype_str} | {DEVICE.upper()}"

    except Exception as e:
        cleanup_memory()
        return f"❌ Failed to load {name}: {str(e)}"


# Initialize default model
load_model(DEFAULT_NAME)


@spaces.GPU(duration=60)
def _extract_grid(img):
    """Extract feature grid from image"""
    with torch.inference_mode():
        pv = processor(images=img, return_tensors="pt").pixel_values

        if DEVICE == "cuda":
            pv = pv.to(DEVICE)

        out = model(pixel_values=pv)
        last = out.last_hidden_state[0].to(torch.float32)

        num_reg = getattr(model.config, "num_register_tokens", 0)
        p = model.config.patch_size
        _, _, Ht, Wt = pv.shape
        gh, gw = Ht // p, Wt // p

        feats = last[1 + num_reg :, :].reshape(gh, gw, -1).cpu()

    return feats, gh, gw


def _overlay(orig, heat01, alpha=0.55, box=None):
    """Create heatmap overlay with improved visualization"""
    H, W = orig.height, orig.width
    heat = Image.fromarray((heat01 * 255).astype(np.uint8)).resize(
        (W, H), resample=Image.LANCZOS
    )

    # Use a better colormap for satellite imagery
    rgba = (cm.get_cmap("turbo")(np.asarray(heat) / 255.0) * 255).astype(np.uint8)
    ov = Image.fromarray(rgba, "RGBA")
    ov.putalpha(int(alpha * 255))

    base = orig.copy().convert("RGBA")
    out = Image.alpha_composite(base, ov)

    if box:
        from PIL import ImageDraw

        draw = ImageDraw.Draw(out, "RGBA")
        # Enhanced box visualization
        draw.rectangle(box, outline=(255, 255, 255, 255), width=3)
        draw.rectangle(
            (box[0] - 1, box[1] - 1, box[2] + 1, box[3] + 1),
            outline=(0, 0, 0, 200),
            width=1,
        )

    return out


def prepare(img):
    """Prepare image and extract features"""
    if img is None:
        return None

    base = ImageOps.exif_transpose(img.convert("RGB"))
    feats, gh, gw = _extract_grid(base)

    return {"orig": base, "feats": feats, "gh": gh, "gw": gw}


def click(state, opacity, colormap, img_value, evt: gr.SelectData):
    """Handle click events for similarity visualization"""
    # If state wasn't prepared, build it now
    if state is None and img_value is not None:
        state = prepare(img_value)

    if not state or evt.index is None:
        return img_value, state, None

    base, feats, gh, gw = state["orig"], state["feats"], state["gh"], state["gw"]

    x, y = evt.index
    px_x, px_y = base.width / gw, base.height / gh
    i = min(int(x // px_x), gw - 1)
    j = min(int(y // px_y), gh - 1)

    d = feats.shape[-1]
    grid = F.normalize(feats.reshape(-1, d), dim=1)
    v = F.normalize(feats[j, i].reshape(1, d), dim=1)
    sims = (grid @ v.T).reshape(gh, gw).numpy()

    smin, smax = float(sims.min()), float(sims.max())
    heat01 = (sims - smin) / (smax - smin + 1e-12)

    # Update colormap dynamically
    cm_func = cm.get_cmap(colormap.lower())
    rgba = (cm_func(heat01) * 255).astype(np.uint8)
    ov = Image.fromarray(rgba, "RGBA")
    ov.putalpha(int(opacity * 255))

    base_rgba = base.copy().convert("RGBA")
    box = (int(i * px_x), int(j * px_y), int((i + 1) * px_x), int((j + 1) * px_y))

    out = Image.alpha_composite(base_rgba, ov)
    if box:
        from PIL import ImageDraw

        draw = ImageDraw.Draw(out, "RGBA")
        draw.rectangle(box, outline=(255, 255, 255, 255), width=3)
        draw.rectangle(
            (box[0] - 1, box[1] - 1, box[2] + 1, box[3] + 1),
            outline=(0, 0, 0, 200),
            width=1,
        )

    # Stats for info panel
    stats = f"""πŸ“Š **Similarity Statistics**
- Min: {smin:.3f}
- Max: {smax:.3f}
- Range: {smax - smin:.3f}
- Patch: ({i}, {j})
- Grid: {gw}Γ—{gh}"""

    return out, state, stats


def reset():
    """Reset the interface"""
    return None, None, None


# Build the interface
with gr.Blocks(
    theme=gr.themes.Soft(
        primary_hue="blue",
        secondary_hue="gray",
        neutral_hue="gray",
        font=gr.themes.GoogleFont("Inter"),
    ),
    css="""
    .container {max-width: 1200px; margin: auto;}
    .header {text-align: center; padding: 20px;}
    .info-box {
        background: rgba(0,0,0,0.03); 
        border-radius: 8px; 
        padding: 12px; 
        margin: 10px 0;
        border-left: 4px solid #2563eb;
    }
    """,
) as demo:
    gr.HTML(
        """
    <div class="header">
        <h1>πŸ›°οΈ DINOv3 Satellite Vision: Interactive Patch Similarity</h1>
        <p style="font-size: 1.1em; color: #666;">
            Explore how DINOv3 models trained on satellite imagery understand visual patterns
        </p>
    </div>
    """
    )

    with gr.Row():
        with gr.Column(scale=3):
            gr.Markdown(
                """
            ### How it works
            1. **Select a model** - Satellite-pretrained models are optimized for aerial/satellite imagery
            2. **Upload or select an image** - Works best with satellite, aerial, or outdoor scenes
            3. **Click any region** - See how similar other patches are to your selection
            4. **Adjust visualization** - Fine-tune opacity and colormap for clarity
            """
            )

        with gr.Column(scale=2):
            gr.HTML(
                """
            <div class="info-box">
                <b>πŸ’‘ Model Info:</b><br>
                β€’ <b>Satellite models</b>: Trained on 493M satellite images<br>
                β€’ <b>LVD model</b>: Trained on 1.7B diverse images<br>
                β€’ <b>7B model</b>: Massive capacity, slower but more nuanced
            </div>
            """
            )

    with gr.Row():
        with gr.Column(scale=1):
            model_choice = gr.Dropdown(
                choices=list(MODEL_MAP.keys()),
                value=DEFAULT_NAME,
                label="πŸ€– Model Selection",
                info="Satellite models excel at geographic and structural patterns",
            )

            status = gr.Textbox(
                label="πŸ“‘ Model Status",
                value=f"Ready: {DEFAULT_NAME}",
                interactive=False,
                lines=1,
            )

            with gr.Row():
                opacity = gr.Slider(
                    0.2,
                    0.9,
                    0.55,
                    step=0.05,
                    label="🎨 Heatmap Opacity",
                    info="Balance between image and similarity map",
                )

                colormap = gr.Dropdown(
                    choices=["Turbo", "Inferno", "Viridis", "Plasma", "Magma", "Jet"],
                    value="Turbo",
                    label="🌈 Colormap",
                    info="Different maps for different contrasts",
                )

            info_panel = gr.Markdown(value=None, label="Statistics", visible=True)

            with gr.Row():
                reset_btn = gr.Button("πŸ”„ Reset", variant="secondary", scale=1)
                clear_btn = gr.ClearButton(value="πŸ—‘οΈ Clear All", scale=1)

        with gr.Column(scale=2):
            img = gr.Image(
                type="pil",
                label="Interactive Canvas (Click to explore)",
                interactive=True,
                height=600,
                show_download_button=True,
                show_share_button=False,
            )

    state = gr.State()

    # Examples focused on satellite-relevant imagery
    gr.Examples(
        examples=[
            [_filepath.name]
            for _filepath in Path.cwd().iterdir()
            if _filepath.suffix.lower() in [".jpg", ".png", ".webp"]
        ],
        inputs=img,
        fn=prepare,
        outputs=[state],
        label="Example Images",
        examples_per_page=6,
        cache_examples=False,
    )

    # Event handlers
    model_choice.change(
        load_model, inputs=model_choice, outputs=status, show_progress="full"
    )

    img.upload(prepare, inputs=img, outputs=state, show_progress="minimal")

    img.select(
        click,
        inputs=[state, opacity, colormap, img],
        outputs=[img, state, info_panel],
        show_progress="minimal",
    )

    reset_btn.click(reset, outputs=[img, state, info_panel], show_progress=False)

    clear_btn.add([img, state, info_panel])

    gr.Markdown(
        """
    ---
    <div style="text-align: center; color: #666; font-size: 0.9em;">
        <b>Performance Notes:</b> Satellite models are optimized for geographic patterns, land use classification, 
        and structural analysis. The 7B model provides exceptional detail but requires significant compute.
        <br><br>
        Built with DINOv3 | Optimized for satellite and aerial imagery analysis
    </div>
    """
    )

if __name__ == "__main__":
    demo.launch(share=False, show_error=True)