File size: 4,236 Bytes
8358707
40a1094
8358707
 
 
 
 
 
7079d99
bac69fc
7079d99
8358707
 
 
7079d99
8358707
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7079d99
 
40a1094
8358707
 
 
 
40a1094
8358707
 
 
 
 
40a1094
8358707
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
40a1094
7079d99
 
 
 
 
 
 
 
 
 
 
 
 
 
8358707
 
71cdb5c
 
8358707
 
 
 
 
71cdb5c
8358707
 
 
 
 
 
 
 
 
71cdb5c
8358707
 
 
 
 
 
 
 
 
71cdb5c
8358707
 
 
71cdb5c
 
8358707
 
 
 
7079d99
 
 
 
 
 
 
 
 
 
8358707
 
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
import gradio as gr
import spaces

from PIL import Image as PImage
from torch import cuda

from transformers import AutoModel, AutoProcessor, pipeline

from utils.image_utils import draw_results, heatmap_image, heatmap_image_rbf, mask_image
from utils.clip_utils import embed_image, embed_word, idxs_along_axes, idxs_by_dist, make_image
from utils.SigLip2 import SigLip2

DEVICE = "cuda" if cuda.is_available() else "cpu"

SIGLIP_MODEL = "google/siglip2-so400m-patch16-256"
CLIP_MODEL = "openai/clip-vit-large-patch14"
DETR_MODEL = "facebook/detr-resnet-50"
OWL_MODEL = "google/owlv2-base-patch16"

detr = pipeline(task="object-detection",
                model=DETR_MODEL,
                device=DEVICE)

owl = pipeline(task="zero-shot-object-detection",
               model=OWL_MODEL,
               device=DEVICE)

clip_processor = AutoProcessor.from_pretrained(CLIP_MODEL)
clip = AutoModel.from_pretrained(CLIP_MODEL, device_map="auto").to(DEVICE)

siglip = SigLip2(SIGLIP_MODEL)

@spaces.GPU
def run_detr(img):
  predictions = detr(img)
  return draw_results(img, predictions)

@spaces.GPU
def run_owl(img, classes_str):
  classes = [c.strip() for c in classes_str.split(",")]
  predictions = owl(img, candidate_labels=classes)
  return draw_results(img, predictions)

@spaces.GPU
def run_clip(files, word0, word1=""):
  w0e = embed_word(word0, clip_processor, clip, DEVICE)
  w1e = embed_word(word1, clip_processor, clip, DEVICE)
  
  ies = []
  imgs = []
  for f in files:
    img = PImage.open(f.name).convert("RGB")
    img = img.resize((int(256 * img.width/img.height), 256))
    imgs.append(img)
    ies.append(embed_image(img, clip_processor, clip, DEVICE))

  if word1 == "":
    ordered_idxs = idxs_by_dist(ies, w0e)
    return make_image(imgs, ordered_idxs)
  else:
    ordered_idxs = idxs_along_axes(ies, (w0e, w1e))
    return make_image(imgs, ordered_idxs)

@spaces.GPU
def run_siglip2(img, text):
  text = [text]
  similarity_map_np = siglip.get_gradient_activation_map(img, text)

  masked_img = mask_image(img, similarity_map_np)

  # heatmap_img = heatmap_image(similarity_map_np, size=img.size, sampling=PImage.Resampling.BILINEAR)
  # overlay_img = PImage.blend(img, heatmap_img.resize(img.size), 0.65)

  heatmap_img_rbf = heatmap_image_rbf(similarity_map_np, size=img.size)
  overlay_img_rbf = PImage.blend(img, heatmap_img_rbf.resize(img.size), 0.65)

  return [masked_img, overlay_img_rbf]


examples = [
  ("painted portrait young person", "painted portrait old person"),
  ("painted portrait happy person", "painted portrait worried person"),
]

with gr.Blocks() as demo:
  gr.Interface(
    title="Object Detection",
    description="[DETR](https://huggingface.co/facebook/detr-resnet-50) model from facebook (2020), trained on [COCO 2017](https://github.com/amikelive/coco-labels/blob/master/coco-labels-2014_2017.txt) dataset and labels.",
    api_name="object",
    fn=run_detr,
    inputs=gr.Image(type="pil"),
    outputs=gr.Image(format="jpeg"),
    flagging_mode="never",
  )

  gr.Interface(
    title="Zero-Shot Object Detection",
    description="[OWLv2](https://huggingface.co/google/owlv2-large-patch14-ensemble) model from google (2023).",
    api_name="zero",
    fn=run_owl,
    inputs=[gr.Image(type="pil"), gr.Textbox(label="Object", show_label=True)],
    outputs=gr.Image(format="jpeg"),
    flagging_mode="never",
  )

  gr.Interface(
    title="Contrastive Embedding",
    description="[CLIP](https://huggingface.co/openai/clip-vit-large-patch14) model from openai (2021).",
    api_name="clip",
    fn=run_clip,
    inputs=[gr.File(file_count="multiple"),
            gr.Textbox(label="1st Descriptor", show_label=True),
            gr.Textbox(label="2nd Descriptor", show_label=True)],
    outputs=gr.Image(format="jpeg"),
    flagging_mode="never",
  )

  gr.Interface(
    title="Activations",
    description="[SigLip2](https://huggingface.co/google/siglip2-so400m-patch16-256) model from Google (2025).",
    api_name="siglip2",
    fn=run_siglip2,
    inputs=[gr.Image(type="pil"), gr.Textbox(label="Activation Term", show_label=True)],
    outputs=[gr.Image(format="jpeg"), gr.Image(format="jpeg")],
    flagging_mode="never",
  )

if __name__ == "__main__":
  demo.launch()