File size: 2,780 Bytes
25cae60
 
 
 
 
 
 
 
 
 
 
 
 
3ab688e
25cae60
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5c00c7e
25cae60
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3ab688e
907a760
 
 
25cae60
 
907a760
25cae60
 
907a760
25cae60
 
 
 
 
 
 
 
 
3ab688e
25cae60
3ab688e
 
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
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):
    fg = fg.convert('RGBA')
    bg = bg.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, preds_up)
    

title = 'FOUND'
description = 'Gradio Demo accompanying paper "Unsupervised Object Localization: Observing the Background to Discover Objects"\n \
    The app is running CPU-only, times are therefore .\n'
article = """<h2 align="center">Unsupervised Object Localization: Observing the Background to Discover Objects </h2>
    <h1 align="center"> FOUND </h1>
    """
examples = ["data/examples/VOC_000030.jpg"]


iface = gr.Interface(fn=predict,
                     title=title,
                     description=description,
                     article=article,
                     inputs=gr.Image(type='filepath'),
                     outputs=gr.Image(label="Object localization", type="pil"),
                     examples=examples,
                     cache_examples=CACHE
                     )

iface.launch(show_error=True, enable_queue=True, inline=True)