""" 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()