FOUND / app.py
osimeoni's picture
CPU
5c00c7e
raw
history blame
2.78 kB
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)