import os import torch import argparse import torch.nn as nn import torch.nn.functional as F import matplotlib.pyplot as plt from PIL import Image from model import FoundModel from misc import load_config from torchvision import transforms as T import gradio as gr NORMALIZE = T.Normalize((0.485, 0.456, 0.406), (0.229, 0.224, 0.225)) CACHE = True def blend_images(bg, fg, alpha=0.5): bg = bg.convert('RGBA') fg = fg.convert('RGBA') blended = Image.blend(bg, fg, alpha=alpha) return blended def predict(img_input): config = "configs/found_DUTS-TR.yaml" model_weights = "data/weights/decoder_weights.pt" # Configuration config = load_config(config) # ------------------------------------ # Load the model model = FoundModel(vit_model=config.model["pre_training"], vit_arch=config.model["arch"], vit_patch_size=config.model["patch_size"], enc_type_feats=config.found["feats"], bkg_type_feats=config.found["feats"], bkg_th=config.found["bkg_th"]) # Load weights model.decoder_load_weights(model_weights) model.eval() print(f"Model {model_weights} loaded correctly.") # Load the image img_pil = Image.open(img_input) img = img_pil.convert("RGB") t = T.Compose([T.ToTensor(), NORMALIZE]) img_t = t(img)[None,:,:,:] inputs = img_t # Forward step with torch.no_grad(): preds, _, _, _ = model.forward_step(inputs, for_eval=True) # Apply FOUND sigmoid = nn.Sigmoid() h, w = img_t.shape[-2:] preds_up = F.interpolate( preds, scale_factor=model.vit_patch_size, mode="bilinear", align_corners=False )[..., :h, :w] preds_up = ( (sigmoid(preds_up.detach()) > 0.5).squeeze(0).float() ) return blend_images(img_pil, T.ToPILImage()(preds_up)) title = 'FOUND - unsupervised object localization' description = 'Gradio Demo for our CVPR23 paper "Unsupervised Object Localization: Observing the Background to Discover Objects"\n \ The app is running on CPUs, inference times are therefore longer than those expected on GPU (80 FPS on a V100 GPU).\n \ Please see below for more details.' article = """