File size: 10,726 Bytes
e4c8837
58788ff
 
e4c8837
 
 
 
 
 
 
373d67f
e4c8837
 
 
 
58788ff
 
 
 
 
 
 
373d67f
 
58788ff
 
373d67f
 
58788ff
e4c8837
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
373d67f
e4c8837
ee7c477
e4c8837
 
ee7c477
 
 
 
 
373d67f
e4c8837
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ee7c477
 
 
 
 
 
 
 
 
 
 
 
 
e4c8837
 
 
 
 
 
 
 
 
d7e3d51
e4c8837
 
d7e3d51
 
 
 
 
 
 
 
 
 
e4c8837
 
d7e3d51
e4c8837
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ee7c477
 
e4c8837
 
373d67f
ee7c477
 
 
 
 
e4c8837
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ee7c477
 
 
 
 
e4c8837
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d7e3d51
e4c8837
 
 
 
 
 
 
 
 
373d67f
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
import io
import os
import subprocess
import sys
from pathlib import Path

import gradio as gr
import matplotlib.pyplot as plt
import numpy as np
import PIL.Image
import spaces
import torch
import torch.nn.functional as F
import torchvision.transforms as T

# Set environment variable for pip
env = os.environ.copy()

try:
    import natten
except ImportError:
    print("NATTEN not found. Installing NATTEN...")
    print("Torch Version:", torch.__version__)
    print("CUDA Version:", torch.version.cuda)
    # Install NATTEN
    subprocess.run(
        "pip3 install natten==0.17.4+torch240cu121 -f https://shi-labs.com/natten/wheels/", shell=True, env=env, check=True
    )

# Add project root to path
sys.path.append(str(Path(__file__).parent))
from src.backbone.vit_wrapper import PretrainedViTWrapper
from utils.training import round_to_nearest_multiple
from utils.visualization import plot_feats

# Load NAF model
device = "cuda" if torch.cuda.is_available() else "cpu"
model = torch.hub.load("valeoai/NAF", "naf", pretrained=True, device=device)
model.eval()

# Normalization for upsampling
ups_norm = T.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])

# Sample images
SAMPLE_IMAGES = [
    "asset/Cartoon.png",
    "asset/Natural.png",
    "asset/Satellite.png",
    "asset/Medical.png",
    "asset/Ecosystems.png",
    "asset/Driving.jpg",
    "asset/Manufacturing.png",
]


def resize_with_aspect_ratio(img, max_size, patch_size):
    """Resize image maintaining aspect ratio with max dimension and patch size constraints"""
    w, h = img.size

    # Calculate scaling factor to fit within max_size
    scale = min(max_size / w, max_size / h)
    new_w = int(w * scale)
    new_h = int(h * scale)

    # Round to nearest patch size multiple
    new_w = round_to_nearest_multiple(new_w, patch_size)
    new_h = round_to_nearest_multiple(new_h, patch_size)

    # Ensure minimum size
    new_w = max(new_w, patch_size)
    new_h = max(new_h, patch_size)

    return new_w, new_h


@spaces.GPU
@torch.no_grad()
def process_image(image, model_selection, custom_model, output_resolution):
    """Process image with selected model and resolution"""
    try:
        # Determine which model to use
        if custom_model.strip():
            model_name = custom_model.strip()
        else:
            model_name = MODEL_MAPPING.get(model_selection, model_selection)

        # Load the backbone using vit_wrapper
        backbone = PretrainedViTWrapper(model_name, norm=True).to(device)
        backbone.eval()

        # Get model config for normalization and input size
        mean = backbone.config["mean"]
        std = backbone.config["std"]
        patch_size = backbone.patch_size
        back_norm = T.Normalize(mean=mean, std=std)

        # Prepare image at model's expected resolution
        img = PIL.Image.fromarray(image).convert("RGB")
        new_w, new_h = resize_with_aspect_ratio(img, max_size=512, patch_size=patch_size)

        transform = T.Compose(
            [
                T.Resize((new_h, new_w)),
                T.ToTensor(),
            ]
        )
        img_tensor = transform(img).unsqueeze(0).to(device)

        # Normalize for backbone
        img_back = back_norm(img_tensor)
        lr_feats = backbone(img_back)

        # vit_wrapper already returns features in [B, C, H, W] format
        if not isinstance(lr_feats, torch.Tensor):
            raise ValueError(f"Unexpected feature type: {type(lr_feats)}")

        if len(lr_feats.shape) != 4:
            raise ValueError(f"Unexpected feature shape: {lr_feats.shape}. Expected [B, C, H, W].")

        # Normalize for upsampling
        img_ups = ups_norm(img_tensor)

        # Calculate output resolution maintaining aspect ratio
        _, _, h, w = lr_feats.shape
        aspect_ratio = w / h
        if aspect_ratio > 1:  # Width > Height
            out_h = round_to_nearest_multiple(int(output_resolution / aspect_ratio), patch_size)
            out_w = output_resolution
        else:  # Height >= Width
            out_h = output_resolution
            out_w = round_to_nearest_multiple(int(output_resolution * aspect_ratio), patch_size)

        upsampled_feats = model(img_ups, lr_feats, (out_h, out_w))

        # Create visualization using plot_feats
        plot_feats(
            img_tensor[0],
            lr_feats[0],
            [upsampled_feats[0]],
            legend=["Image", f"Low-Res: {h}x{w}", f"High-Res: {out_h}x{out_w}"],
            font_size=14,
        )

        # Convert matplotlib figure to PIL Image
        fig = plt.gcf()  # Get current figure
        buf = io.BytesIO()
        fig.savefig(buf, format="png", dpi=100, bbox_inches="tight")
        buf.seek(0)
        result_img = PIL.Image.open(buf)
        plt.close(fig)

        return result_img

    except Exception as e:
        print(f"Error processing image: {e}")
        import traceback

        traceback.print_exc()
        return None


# Popular vision models with friendly names
MODEL_MAPPING = {
    "DINOv3-B": "vit_base_patch16_dinov3.lvd1689m",
    "RADIOv2.5-B": "radio_v2.5-b",
    "DINOv2-B": "vit_base_patch14_dinov2.lvd142m",
    "DINOv2-R-B": "vit_base_patch14_reg4_dinov2",
    "DINO-B": "vit_base_patch16_224.dino",
    "SigLIP2-B": "vit_base_patch16_siglip_512.v2_webli",
    "PE-Core-B": "vit_pe_core_base_patch16_224.fb",
    "CLIP-B": "vit_base_patch16_clip_224.openai",
}

FRIENDLY_MODEL_NAMES = list(MODEL_MAPPING.keys())

# Create Gradio interface
with gr.Blocks(title="NAF: Zero-Shot Feature Upsampling") as demo:
    gr.HTML(
        """
        <div style="text-align: center; margin-bottom: 2rem;">
            <h1 class="title-text" style="font-size: 3rem; margin-bottom: 0.5rem;">
                🎯 NAF: Zero-Shot Feature Upsampling
            </h1>
            <p style="font-size: 1.2rem; color: #666; margin-bottom: 0.5rem;">
                via Neighborhood Attention Filtering
            </p>
            <div style="margin-bottom: 1rem;">
                <a href="https://github.com/valeoai/NAF" target="_blank" 
                   style="margin: 0 0.5rem; text-decoration: none; color: #667eea; font-weight: bold;">
                    πŸ“¦ Code
                </a>
                <a href="https://arxiv.org/abs/2511.18452" target="_blank" 
                   style="margin: 0 0.5rem; text-decoration: none; color: #667eea; font-weight: bold;">
                    πŸ“„ Paper
                </a>
            </div>
            <div class="info-box" style="max-width: 900px; margin: 0 auto;">
                <p style="font-size: 1.1rem; margin-bottom: 0.8rem;">
                    πŸš€ <strong>Upsample features from any Vision Foundation Model to any resolution using a single upsampler!</strong>
                </p>
                <p style="font-size: 0.95rem; margin: 0;">
                    Upload an image, select a model, choose your target resolution, and see NAF in action.
                </p>
            </div>
        </div>
        """
    )

    with gr.Row():
        with gr.Column(scale=1):
            gr.Markdown("### πŸ“€ Input Configuration")

            image_input = gr.Image(label="Upload Your Image", type="numpy")

            # Sample images
            if any(Path(p).exists() for p in SAMPLE_IMAGES):
                gr.Examples(
                    examples=[[p] for p in SAMPLE_IMAGES if Path(p).exists()],
                    inputs=image_input,
                    label="πŸ–ΌοΈ Try Sample Images",
                    examples_per_page=4,
                )

            gr.Markdown("### βš™οΈ Model Settings")

            model_dropdown = gr.Dropdown(
                choices=FRIENDLY_MODEL_NAMES,
                value=FRIENDLY_MODEL_NAMES[0],
                label="πŸ€– Vision Foundation Model",
            )

            custom_model_input = gr.Textbox(
                label="✍️ Or Use Custom Model (timm reference name)",
                placeholder="e.g., vit_large_patch14_dinov2.lvd142m",
                value="",
            )

            resolution_slider = gr.Slider(
                minimum=64,
                maximum=512,
                step=64,
                value=448,
                label="πŸ“ Output Resolution (max dimension)",
            )

            process_btn = gr.Button("✨ Upsample Features", variant="primary")

        with gr.Column(scale=2):
            gr.Markdown("### 🎨 Visualization Results")
            output_image = gr.Image(label="Feature Comparison", type="pil")

            gr.Markdown(
                """
                <div style="background: #f0f7ff; padding: 1rem; border-radius: 8px; border-left: 4px solid #667eea;">
                    <strong>πŸ“Š Visualization Guide:</strong>
                    <ul style="margin: 0.5rem 0;">
                        <li><strong>Left:</strong> Original input image</li>
                        <li><strong>Center:</strong> Low-resolution features (PCA visualization)</li>
                        <li><strong>Right:</strong> High-resolution features upsampled by NAF</li>
                    </ul>
                    <p style="margin-top: 0.5rem; font-size: 0.9rem; color: #555;">
                        <em>Note: Output features maintain the aspect ratio of the input image.</em>
                    </p>
                </div>
                """
            )

    process_btn.click(
        fn=process_image,
        inputs=[image_input, model_dropdown, custom_model_input, resolution_slider],
        outputs=output_image,
    )

    gr.Markdown(
        """
        ---
        <div style="text-align: center; padding: 2rem 0;">
            <h3 style="color: #667eea;">πŸ’‘ About NAF</h3>
            <p style="max-width: 800px; margin: 1rem auto; font-size: 1.05rem; color: #555;">
                NAF enables <strong>zero-shot feature upsampling</strong> from any Vision Foundation Model 
                to any resolution. It learns to filter and combine features using neighborhood attention, 
                without requiring model-specific training.
            </p>
            <div style="margin-top: 1.5rem;">
                <a href="https://github.com/valeoai/NAF" target="_blank" 
                   style="margin: 0 1rem; text-decoration: none; color: #667eea; font-weight: bold;">
                    πŸ“¦ GitHub Repository
                </a>
                <a href="https://arxiv.org/abs/2511.18452" target="_blank" 
                   style="margin: 0 1rem; text-decoration: none; color: #667eea; font-weight: bold;">
                    πŸ“„ Research Paper
                </a>
            </div>
        </div>
        """
    )

if __name__ == "__main__":
    demo.launch()