import os import torch import torch.nn.functional as F import gradio as gr import numpy as np from PIL import Image, ImageDraw import torchvision.transforms.functional as TF from matplotlib import colaps from transformers import AutoModel # ---------------------------- # Configuration # ---------------------------- # ⭐ Define available models, with the smaller one as default MODELS = { "DINOv3 ViT-S+ (Small, Default)": "facebook/dinov3-vits16plus-pretrain-lvd1689m", "DINOv3 ViT-H+ (Huge)": "facebook/dinov3-vith16plus-pretrain-lvd1689m", } DEFAULT_MODEL_NAME = "DINOv3 ViT-S+ (Small, Default)" PATCH_SIZE = 16 DEVICE = "cuda" if torch.cuda.is_available() else "cpu" # Normalization constants (standard for ImageNet) IMAGENET_MEAN = (0.485, 0.456, 0.406) IMAGENET_STD = (0.229, 0.224, 0.225) # ⭐ Cache for loaded models to avoid re-downloading model_cache = {} # ---------------------------- # Model Loading (Hugging Face Hub) # ---------------------------- def load_model_from_hub(model_id: str): """Loads a DINOv3 model from the Hugging Face Hub.""" print(f"Loading model '{model_id}' from Hugging Face Hub...") try: token = os.environ.get("HF_TOKEN") model = AutoModel.from_pretrained(model_id, token=token, trust_remote_code=True) model.to(DEVICE).eval() print(f"✅ Model loaded successfully on device: {DEVICE}") return model except Exception as e: print(f"❌ Failed to load model: {e}") raise gr.Error( f"Could not load model '{model_id}'. " "This is a gated model. Please ensure you have accepted the terms on its Hugging Face page " "and set your HF_TOKEN as a secret in your Space settings. " f"Original error: {e}" ) def get_model(model_name: str): """Gets a model from the cache or loads it if not present.""" model_id = MODELS[model_name] if model_id not in model_cache: model_cache[model_id] = load_model_from_hub(model_id) return model_cache[model_id] # ---------------------------- # Helper Functions (resize, viz) - No changes here # ---------------------------- def resize_to_grid(img: Image.Image, long_side: int, patch: int) -> torch.Tensor: w, h = img.size scale = long_side / max(h, w) new_h = max(patch, int(round(h * scale))) new_w = max(patch, int(round(w * scale))) new_h = ((new_h + patch - 1) // patch) * patch new_w = ((new_w + patch - 1) // patch) * patch return TF.to_tensor(TF.resize(img.convert("RGB"), (new_h, new_w))) def colorize(sim_map_up: np.ndarray, cmap_name: str = "viridis") -> Image.Image: x = sim_map_up.astype(np.float32) x = (x - x.min()) / (x.max() - x.min() + 1e-6) rgb = (colormaps[cmap_name](x)[..., :3] * 255).astype(np.uint8) return Image.fromarray(rgb) def blend(base: Image.Image, heat: Image.Image, alpha: float = 0.55) -> Image.Image: base = base.convert("RGBA") heat = heat.convert("RGBA") a = Image.new("L", heat.size, int(255 * alpha)) heat.putalpha(a) out = Image.alpha_composite(base, heat) return out.convert("RGB") def draw_crosshair(img: Image.Image, x: int, y: int, radius: int = None) -> Image.Image: r = radius if radius is not None else max(2, PATCH_SIZE // 2) out = img.copy() draw = ImageDraw.Draw(out) draw.line([(x - r, y), (x + r, y)], fill="red", width=3) draw.line([(x, y - r), (x, y + r)], fill="red", width=3) return out def draw_boxes(img: Image.Image, boxes, outline="yellow", width=3, labels=True): out = img.copy() draw = ImageDraw.Draw(out) for i, (x0, y0, x1, y1) in enumerate(boxes, start=1): draw.rectangle([x0, y0, x1, y1], outline=outline, width=width) if labels: tx, ty = x0 + 2, y0 + 2 draw.text((tx, ty), str(i), fill=outline) return out def patch_neighborhood_box(r: int, c: int, Hp: int, Wp: int, rad: int, patch: int = PATCH_SIZE): r0 = max(0, r - rad) r1 = min(Hp - 1, r + rad) c0 = max(0, c - rad) c1 = min(Wp - 1, c + rad) x0 = int(c0 * patch) y0 = int(r0 * patch) x1 = int((c1 + 1) * patch) - 1 y1 = int((r1 + 1) * patch) - 1 return (x0, y0, x1, y1) # ---------------------------- # Feature Extraction (using transformers) # ---------------------------- @torch.inference_mode() # ⭐ Pass the model object as an argument def extract_image_features(model, image_pil: Image.Image, target_long_side: int): """ Extracts patch features from an image using the loaded Hugging Face model. """ t = resize_to_grid(image_pil, target_long_side, PATCH_SIZE) t_norm = TF.normalize(t, IMAGENET_MEAN, IMAGENET_STD).unsqueeze(0).to(DEVICE) _, _, H, W = t_norm.shape Hp, Wp = H // PATCH_SIZE, W // PATCH_SIZE outputs = model(t_norm) n_special_tokens = 5 patch_embeddings = outputs.last_hidden_state.squeeze(0)[n_special_tokens:, :] X = F.normalize(patch_embeddings, p=2, dim=-1) img_resized = TF.to_pil_image(t) return {"X": X, "Hp": Hp, "Wp": Wp, "img": img_resized} # ---------------------------- # Similarity inside the same image - No changes here # ---------------------------- def click_to_similarity_in_same_image( state: dict, click_xy: tuple[int, int], exclude_radius_patches: int = 1, topk: int = 10, alpha: float = 0.55, cmap_name: str = "viridis", box_radius_patches: int = 4, ): if not state: return None, None, None, None X = state["X"] Hp, Wp = state["Hp"], state["Wp"] base_img = state["img"] img_w, img_h = base_img.size x_pix, y_pix = click_xy col = int(np.clip(x_pix // PATCH_SIZE, 0, Wp - 1)) row = int(np.clip(y_pix // PATCH_SIZE, 0, Hp - 1)) idx = row * Wp + col q = X[idx] sims = torch.matmul(X, q) sim_map = sims.view(Hp, Wp) if exclude_radius_patches > 0: rr, cc = torch.meshgrid( torch.arange(Hp, device=sims.device), torch.arange(Wp, device=sims.device), indexing="ij", ) mask = (torch.abs(rr - row) <= exclude_radius_patches) & (torch.abs(cc - col) <= exclude_radius_patches) sim_map = sim_map.masked_fill(mask, float("-inf")) sim_up = F.interpolate( sim_map.unsqueeze(0).unsqueeze(0), size=(img_h, img_w), mode="bicubic", align_corners=False, ).squeeze().detach().cpu().numpy() heatmap_pil = colorize(sim_up, cmap_name) overlay_pil = blend(base_img, heatmap_pil, alpha=alpha) overlay_boxes_pil = overlay_pil if topk and topk > 0: flat = sim_map.view(-1) valid = torch.isfinite(flat) if valid.any(): vals = flat.clone() vals[~valid] = -1e9 k = min(topk, int(valid.sum().item())) _, top_idx = torch.topk(vals, k=k, largest=True, sorted=True) boxes = [ patch_neighborhood_box( r, c, Hp, Wp, rad=int(box_radius_patches), patch=PATCH_SIZE ) for r, c in [divmod(j.item(), Wp) for j in top_idx] ] overlay_boxes_pil = draw_boxes(overlay_pil, boxes, outline="yellow", width=3, labels=True) marked_ref = draw_crosshair(base_img, x_pix, y_pix, radius=PATCH_SIZE // 2) return marked_ref, heatmap_pil, overlay_pil, overlay_boxes_pil # ---------------------------- # Gradio UI # ---------------------------- with gr.Blocks(theme=gr.themes.Soft(), title="DINOv3 Single-Image Patch Similarity") as demo: gr.Markdown("# 🦖 DINOv3 Single-Image Patch Similarity") gr.Markdown("## Running on CPU-only Space, feature extraction can take a moment") gr.Markdown("1. **Choose a model**. 2. Upload an image. 3. Click **Process Image**. 4. **Click anywhere on the processed image** to find similar regions.") app_state = gr.State() with gr.Row(): with gr.Column(scale=1): # ⭐ ADDED MODEL DROPDOWN model_name_dd = gr.Dropdown( label="1. Choose a Model", choices=list(MODELS.keys()), value=DEFAULT_MODEL_NAME, ) input_image = gr.Image( label="2. Upload Image", type="pil", value="https://images.squarespace-cdn.com/content/v1/607f89e638219e13eee71b1e/1684821560422-SD5V37BAG28BURTLIXUQ/michael-sum-LEpfefQf4rU-unsplash.jpg" ) target_long_side = gr.Slider( minimum=224, maximum=1024, value=768, step=16, label="Processing Resolution", info="Higher values = more detail but slower processing", ) process_button = gr.Button("3. Process Image", variant="primary") with gr.Row(): alpha = gr.Slider(0.0, 1.0, value=0.55, step=0.05, label="Overlay opacity") cmap = gr.Dropdown( ["viridis", "magma", "plasma", "inferno", "turbo", "cividis"], value="viridis", label="Colormap", ) with gr.Column(scale=1): exclude_r = gr.Slider(0, 10, value=0, step=1, label="Exclude radius (patches)") topk = gr.Slider(0, 200, value=20, step=1, label="Top-K boxes") box_radius = gr.Slider(0, 10, value=1, step=1, label="Box radius (patches)") with gr.Row(): marked_image = gr.Image(label="4. Click on this image", interactive=True) heatmap_output = gr.Image(label="Similarity heatmap", interactive=False) with gr.Row(): overlay_output = gr.Image(label="Overlay (image ⊕ heatmap)", interactive=False) overlay_boxes_output = gr.Image(label="Overlay + top-K similar patch boxes", interactive=False) # ⭐ UPDATED to take model_name as input def _process_image(model_name: str, img: Image.Image, long_side: int, progress=gr.Progress(track_tqdm=True)): if img is None: gr.Warning("Please upload an image first!") return None, None progress(0, desc=f"Loading model '{model_name}'...") model = get_model(model_name) progress(0.5, desc="Extracting features...") st = extract_image_features(model, img, int(long_side)) progress(1, desc="Done! You can now click on the image.") return st["img"], st def _on_click(st, a: float, m: str, excl: int, k: int, box_rad: int, evt: gr.SelectData): if not st or evt is None: gr.Warning("Please process an image before clicking on it.") return None, None, None, None return click_to_similarity_in_same_image( st, click_xy=evt.index, exclude_radius_patches=int(excl), topk=int(k), alpha=float(a), cmap_name=m, box_radius_patches=int(box_rad), ) # ⭐ UPDATED EVENT WIRING to include the dropdown inputs_for_processing = [model_name_dd, input_image, target_long_side] outputs_for_processing = [marked_image, app_state] process_button.click( _process_image, inputs=inputs_for_processing, outputs=outputs_for_processing ) marked_image.select( _on_click, inputs=[app_state, alpha, cmap, exclude_r, topk, box_radius], outputs=[marked_image, heatmap_output, overlay_output, overlay_boxes_output], ) if __name__ == "__main__": demo.launch()