File size: 5,182 Bytes
ea3b7ec
 
 
 
 
 
 
 
 
cdb3af7
 
ea3b7ec
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cdb3af7
 
 
 
 
 
 
ea3b7ec
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import streamlit as st
from text2image import get_model, get_tokenizer, get_image_transform
from utils import text_encoder
from torchvision import transforms
from PIL import Image
from jax import numpy as jnp
import pandas as pd
import numpy as np
import requests
import psutil
import time
import jax
import gc


preprocess = transforms.Compose([
    transforms.ToTensor(),
    transforms.Normalize((0.48145466, 0.4578275, 0.40821073), (0.26862954, 0.26130258, 0.27577711)),
])


def pad_to_square(image, size=224):
    ratio = float(size) / max(image.size)
    new_size = tuple([int(x * ratio) for x in image.size])
    image = image.resize(new_size, Image.ANTIALIAS)
    new_image = Image.new("RGB", size=(size, size), color=(128, 128, 128))
    new_image.paste(image, ((size - new_size[0]) // 2, (size - new_size[1]) // 2))
    return new_image


def image_encoder(image, model):
    image = np.transpose(image, (0, 2, 3, 1))
    features = model.get_image_features(image)
    features /= jnp.linalg.norm(features, keepdims=True)
    return features


def gen_image_batch(image_url, image_size=224, pixel_size=10):
    n_pixels = image_size // pixel_size + 1

    image_batch = []
    masks = []
    image_raw = requests.get(image_url, stream=True).raw
    image = Image.open(image_raw).convert("RGB")
    image = pad_to_square(image, size=image_size)
    gray = np.ones_like(image) * 128
    mask = np.ones_like(image)

    image_batch.append(image)
    masks.append(mask)

    for i in range(0, n_pixels):
        for j in range(i+1, n_pixels):
            m = mask.copy()
            m[:min(i*pixel_size, image_size) + 1, :] = 0
            m[min(j*pixel_size, image_size) + 1:, :] = 0
            neg_m = 1 - m
            image_batch.append(image * m + gray * neg_m)
            masks.append(m)

    for i in range(0, n_pixels+1):
        for j in range(i+1, n_pixels+1):
            m = mask.copy()
            m[:, :min(i*pixel_size + 1, image_size)] = 0
            m[:, min(j*pixel_size + 1, image_size):] = 0
            neg_m = 1 - m
            image_batch.append(image * m + gray * neg_m)
            masks.append(m)

    return image_batch, masks


def get_heatmap(image_url, text, pixel_size=10, iterations=3):
    tokenizer = get_tokenizer()
    model = get_model()
    image_size = model.config.vision_config.image_size
    text_embedding = text_encoder(text, model, tokenizer)
    images, masks = gen_image_batch(image_url, image_size=image_size, pixel_size=pixel_size)

    input_image = images[0].copy()
    images = np.stack([preprocess(image) for image in images], axis=0)
    image_embeddings = jnp.asarray(image_encoder(images, model))

    sims = []
    scores = []
    mask_val = jnp.zeros_like(masks[0])

    for e, m in zip(image_embeddings, masks):
        sim = jnp.matmul(e, text_embedding.T)
        sims.append(sim)
        if len(sims) > 1:
            scores.append(sim * m)
            mask_val += 1 - m

    score = jnp.mean(jnp.clip(jnp.array(scores) - sims[0], 0, jnp.inf), axis=0)
    for i in range(iterations):
        score = jnp.clip(score - jnp.mean(score), 0, jnp.inf)
    score = (score - jnp.min(score)) / (jnp.max(score) - jnp.min(score))
    return np.asarray(score), input_image


def app():
    st.title("Zero-Shot Localization")
    st.markdown(
        """

        ### 👋 Ciao!

        Here you can find an exaple for zero shot localization that will show you where in an image the model sees an object.

        🤌 Italian mode on! 🤌

        For example, try typing "gatto" (cat) or "cane" (dog) in the space for label and click "locate"!

        """
    )

    image_url = st.text_input(
        "You can input the URL of an image here...",
        value="https://www.tuttosuigatti.it/files/styles/full_width/public/images/featured/205/cani-e-gatti.jpg?itok=WAAiTGS6",
    )

    MAX_ITER = 1

    col1, col2 = st.beta_columns([3, 1])

    with col2:
        pixel_size = st.selectbox(
            "Pixel Size", options=range(10, 21, 5), index=0
        )

        iterations = st.selectbox(
            "Refinement Steps", options=range(3, 30, 3), index=0
        )

        compute = st.button("LOCATE")

    with col1:
        caption = st.text_input(f"Insert label...")

    if compute:

        with st.spinner('Waiting for resources...'):
            sleep_time = 5
            print('CPU_load', psutil.cpu_percent())
            while psutil.cpu_percent() > 90:
                time.sleep(sleep_time)


        if not caption or not image_url:
            st.error("Please choose one image and at least one label")
        else:
            with st.spinner("Computing..."):
                heatmap, image = get_heatmap(image_url, caption, pixel_size, iterations)

                with col1:
                    st.image(image, use_column_width=True)
                    st.image(heatmap, use_column_width=True)
                    st.image(np.asarray(image) / 255.0 * heatmap, use_column_width=True)
        gc.collect()

    elif image_url:
        image_raw = requests.get(image_url, stream=True, ).raw
        image = Image.open(image_raw).convert("RGB")
        with col1:
            st.image(image)