File size: 12,907 Bytes
00bac91
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2d80f73
 
 
00bac91
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
372
373
374
375
376
"""
GROUT: Geometric Reasoning Over Unstructured Tessellations
Gradio interface for mosaic grout line segmentation.

GitHub: https://github.com/advancedtech-sk/GROUT
"""
import gradio as gr
import numpy as np
import cv2
import torch
import torch.nn as nn
from PIL import Image
from typing import Tuple
from huggingface_hub import hf_hub_download

# Try to import segmentation_models_pytorch
try:
    import segmentation_models_pytorch as smp
except ImportError:
    raise ImportError("Please install segmentation-models-pytorch: pip install segmentation-models-pytorch")


# ==================== Configuration ====================
class Config:
    """Model configuration - must match training config."""
    IMG_SIZE = 512
    ENCODER = 'efficientnet-b3'
    IN_CHANNELS = 3
    NUM_CLASSES = 1
    PREDICTION_THRESHOLD = 0.5
    MEAN = [0.485, 0.456, 0.406]
    STD = [0.229, 0.224, 0.225]

    # Model repository
    MODEL_REPO = "advancedtech-sk/GROUT"
    MODEL_FILENAME = "grout_b3_zeroshot_v1.pth"


# ==================== Model Building ====================
def build_model(encoder: str, in_channels: int, num_classes: int):
    """Build U-Net model with specified encoder."""
    model = smp.Unet(
        encoder_name=encoder,
        encoder_weights=None,
        in_channels=in_channels,
        classes=num_classes,
        activation=None,
    )
    return model


def load_model(device: str = 'cpu'):
    """Load trained model from Hugging Face Hub."""
    model = build_model(
        encoder=Config.ENCODER,
        in_channels=Config.IN_CHANNELS,
        num_classes=Config.NUM_CLASSES,
    )

    # Download model from Hugging Face Hub
    model_path = hf_hub_download(
        repo_id=Config.MODEL_REPO,
        filename=Config.MODEL_FILENAME
    )

    checkpoint = torch.load(model_path, map_location=device, weights_only=False)

    if 'model_state_dict' in checkpoint:
        model.load_state_dict(checkpoint['model_state_dict'])
    else:
        model.load_state_dict(checkpoint)

    model.to(device)
    model.eval()
    return model


# ==================== Inference ====================
def preprocess_patch(image: np.ndarray) -> torch.Tensor:
    """Preprocess a patch for inference (no resizing)."""
    image = image.astype(np.float32) / 255.0
    image = (image - np.array(Config.MEAN)) / np.array(Config.STD)
    tensor = torch.from_numpy(image).permute(2, 0, 1).unsqueeze(0).float()
    return tensor


def preprocess_image(image: np.ndarray) -> torch.Tensor:
    """Preprocess image for inference with resize."""
    image = cv2.resize(image, (Config.IMG_SIZE, Config.IMG_SIZE))
    return preprocess_patch(image)


def post_process_mask(mask: np.ndarray) -> np.ndarray:
    """Apply basic post-processing to clean up the mask."""
    kernel = np.ones((3, 3), np.uint8)
    mask = cv2.morphologyEx(mask, cv2.MORPH_OPEN, kernel)
    mask = cv2.morphologyEx(mask, cv2.MORPH_CLOSE, kernel)
    return mask


def create_overlay(image: np.ndarray, mask: np.ndarray, color: Tuple[int, int, int] = (0, 255, 0), alpha: float = 0.5) -> np.ndarray:
    """Create overlay of mask on original image."""
    overlay = image.copy()
    mask_bool = mask > 127
    overlay[mask_bool] = (
        overlay[mask_bool] * (1 - alpha) +
        np.array(color) * alpha
    ).astype(np.uint8)
    return overlay


@torch.no_grad()
def predict_resize(model, image: np.ndarray, device: str = 'cpu', threshold: float = 0.5) -> Tuple[np.ndarray, np.ndarray]:
    """Run inference using resize method."""
    original_size = image.shape[:2]

    # Preprocess with resize
    input_tensor = preprocess_image(image).to(device)

    # Inference
    output = model(input_tensor)
    prob = torch.sigmoid(output).squeeze().cpu().numpy()

    # Threshold to binary
    binary_mask = (prob > threshold).astype(np.uint8) * 255

    # Resize back to original size
    binary_mask = cv2.resize(binary_mask, (original_size[1], original_size[0]), interpolation=cv2.INTER_NEAREST)
    prob_resized = cv2.resize(prob, (original_size[1], original_size[0]))

    # Post-process
    processed_mask = post_process_mask(binary_mask)

    return prob_resized, processed_mask


@torch.no_grad()
def predict_sliding_window(model, image: np.ndarray, device: str = 'cpu', threshold: float = 0.5,
                           patch_size: int = 512, overlap: int = 64) -> Tuple[np.ndarray, np.ndarray]:
    """Run inference using sliding window for large images."""
    h, w = image.shape[:2]

    # If image is small enough, use resize method
    if h <= patch_size and w <= patch_size:
        return predict_resize(model, image, device, threshold)

    # Create output arrays
    prob_map = np.zeros((h, w), dtype=np.float32)
    count_map = np.zeros((h, w), dtype=np.float32)

    stride = patch_size - overlap

    # Calculate patch positions
    patches_y = list(range(0, max(1, h - patch_size + 1), stride))
    if h > patch_size and (h - patch_size) % stride != 0:
        patches_y.append(h - patch_size)
    if not patches_y:
        patches_y = [0]

    patches_x = list(range(0, max(1, w - patch_size + 1), stride))
    if w > patch_size and (w - patch_size) % stride != 0:
        patches_x.append(w - patch_size)
    if not patches_x:
        patches_x = [0]

    # Process each patch
    for y in patches_y:
        for x in patches_x:
            # Extract patch
            y_end = min(y + patch_size, h)
            x_end = min(x + patch_size, w)
            patch = image[y:y_end, x:x_end]

            # Pad if needed
            if patch.shape[0] < patch_size or patch.shape[1] < patch_size:
                padded = np.zeros((patch_size, patch_size, 3), dtype=patch.dtype)
                padded[:patch.shape[0], :patch.shape[1]] = patch
                patch = padded

            # Preprocess and predict
            input_tensor = preprocess_patch(patch).to(device)
            output = model(input_tensor)
            prob = torch.sigmoid(output).squeeze().cpu().numpy()

            # Accumulate (only the valid region)
            valid_h = y_end - y
            valid_w = x_end - x
            prob_map[y:y_end, x:x_end] += prob[:valid_h, :valid_w]
            count_map[y:y_end, x:x_end] += 1

    # Average overlapping predictions
    prob_map = prob_map / np.maximum(count_map, 1)

    # Threshold to binary mask
    binary_mask = (prob_map > threshold).astype(np.uint8) * 255

    # Post-process
    processed_mask = post_process_mask(binary_mask)

    return prob_map, processed_mask


# ==================== Gradio Interface ====================
# Global model variable
MODEL = None
DEVICE = 'cuda' if torch.cuda.is_available() else 'cpu'


def load_model_on_startup():
    """Load model when app starts."""
    global MODEL
    try:
        MODEL = load_model(DEVICE)
        print(f"Model loaded successfully on {DEVICE}")
    except Exception as e:
        print(f"Error loading model: {e}")


def segment_grout(
    input_image: np.ndarray,
    inference_mode: str = "Sliding Window (Quality)",
    threshold: float = 0.5,
    overlap: int = 64,
    overlay_color: str = "Green",
    overlay_opacity: float = 0.5,
) -> Tuple[np.ndarray, np.ndarray, np.ndarray, str]:
    """
    Main inference function for Gradio.
    """
    if MODEL is None:
        raise gr.Error("Model not loaded. Please refresh the page.")

    if input_image is None:
        raise gr.Error("Please upload an image.")

    # Ensure RGB format
    if len(input_image.shape) == 2:
        input_image = cv2.cvtColor(input_image, cv2.COLOR_GRAY2RGB)
    elif input_image.shape[2] == 4:
        input_image = cv2.cvtColor(input_image, cv2.COLOR_RGBA2RGB)

    h, w = input_image.shape[:2]

    # Select inference method
    if inference_mode == "Resize (Fast)":
        prob, mask = predict_resize(MODEL, input_image, DEVICE, threshold)
        mode_used = f"Resize: {w}x{h} -> {Config.IMG_SIZE}x{Config.IMG_SIZE} -> {w}x{h}"
    else:  # Sliding Window (Quality) - default
        prob, mask = predict_sliding_window(MODEL, input_image, DEVICE, threshold,
                                            patch_size=Config.IMG_SIZE, overlap=overlap)
        mode_used = f"Sliding Window: {w}x{h}, patch={Config.IMG_SIZE}, overlap={overlap}"

    # Color mapping
    colors = {
        "Green": (0, 255, 0),
        "Red": (255, 0, 0),
        "Blue": (0, 0, 255),
        "Yellow": (255, 255, 0),
        "Cyan": (0, 255, 255),
        "Magenta": (255, 0, 255),
    }
    color = colors.get(overlay_color, (0, 255, 0))

    # Create overlay
    overlay = create_overlay(input_image, mask, color=color, alpha=overlay_opacity)

    # Create probability heatmap
    heatmap = cv2.applyColorMap((prob * 255).astype(np.uint8), cv2.COLORMAP_JET)
    heatmap = cv2.cvtColor(heatmap, cv2.COLOR_BGR2RGB)

    return mask, overlay, heatmap, mode_used


# Create Gradio interface
def create_demo():
    """Create the Gradio demo interface."""

    with gr.Blocks(title="GROUT: Geometric Reasoning Over Unstructured Tessellations", theme=gr.themes.Soft()) as demo:
        gr.Markdown(
            """
            # GROUT: Geometric Reasoning Over Unstructured Tessellations

            Upload an image of a mosaic to detect and segment the grout lines between tiles.

            **Model:** GROUT v1 (U-Net with EfficientNet-B3 encoder) trained on synthetic mosaic data.

            **Author:** Radoslav Lovecky, Institute of Advanced Technologies, Slovakia

            [GitHub Repository](https://github.com/advancedtech-sk/GROUT) | [Model Weights](https://huggingface.co/advancedtech-sk/GROUT)

            ---
            """
        )

        with gr.Row():
            with gr.Column():
                input_image = gr.Image(label="Input Image", type="numpy")

                with gr.Group():
                    gr.Markdown("### Inference Settings")
                    inference_mode = gr.Radio(
                        choices=["Sliding Window (Quality)", "Resize (Fast)"],
                        value="Sliding Window (Quality)",
                        label="Inference Mode",
                        info="Sliding Window preserves full resolution, Resize is faster"
                    )
                    threshold = gr.Slider(
                        minimum=0.1,
                        maximum=0.9,
                        value=0.5,
                        step=0.05,
                        label="Detection Threshold",
                        info="Lower = more sensitive, Higher = more strict"
                    )
                    overlap = gr.Slider(
                        minimum=0,
                        maximum=256,
                        value=64,
                        step=16,
                        label="Patch Overlap (Sliding Window)",
                        info="Higher = smoother results, slower processing"
                    )

                with gr.Group():
                    gr.Markdown("### Visualization Settings")
                    overlay_color = gr.Dropdown(
                        choices=["Green", "Red", "Blue", "Yellow", "Cyan", "Magenta"],
                        value="Green",
                        label="Overlay Color"
                    )
                    overlay_opacity = gr.Slider(
                        minimum=0.1,
                        maximum=1.0,
                        value=0.5,
                        step=0.1,
                        label="Overlay Opacity"
                    )

                segment_btn = gr.Button("Segment Grout Lines", variant="primary")

            with gr.Column():
                output_mask = gr.Image(label="Grout Mask (Binary)")
                output_overlay = gr.Image(label="Overlay on Original")
                output_heatmap = gr.Image(label="Probability Heatmap")
                mode_info = gr.Textbox(label="Mode Used", interactive=False)

        segment_btn.click(
            fn=segment_grout,
            inputs=[input_image, inference_mode, threshold, overlap, overlay_color, overlay_opacity],
            outputs=[output_mask, output_overlay, output_heatmap, mode_info],
        )

        gr.Markdown(
            """
            ---
            **Inference Modes:**
            - **Sliding Window (Quality):** Processes image in overlapping 512x512 patches, preserves full resolution
            - **Resize (Fast):** Resizes image to 512x512, faster but may lose detail on large images

            **Tips:**
            - For large images (>512px), Sliding Window gives best quality
            - Increase overlap for smoother results at patch boundaries
            - The heatmap shows model confidence (red = high, blue = low)
            - Lower threshold for faint grout lines, higher for cleaner results
            """
        )

    return demo


# Main entry point
if __name__ == "__main__":
    load_model_on_startup()
    demo = create_demo()
    demo.launch()