File size: 10,506 Bytes
9052bb3
 
 
 
 
 
 
 
 
 
 
 
896a74b
 
9052bb3
 
 
896a74b
 
9052bb3
896a74b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1157b18
9052bb3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1157b18
9052bb3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
896a74b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1157b18
896a74b
9052bb3
 
 
896a74b
 
 
 
 
9052bb3
 
 
 
 
 
 
 
896a74b
 
 
 
 
9052bb3
896a74b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9052bb3
 
896a74b
 
9052bb3
896a74b
9052bb3
 
896a74b
 
9052bb3
896a74b
 
9052bb3
 
896a74b
 
9052bb3
 
 
896a74b
 
9052bb3
 
 
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
import gradio as gr
import spaces
import torch
from transformers import AutoProcessor, AutoModelForZeroShotObjectDetection
from PIL import Image
import time

def extract_model_short_name(model_id):
    return model_id.split("/")[-1].replace("-", " ").replace("_", " ")

model_llmdet_id = "iSEE-Laboratory/llmdet_tiny"
model_mm_grounding_id = "rziga/mm_grounding_dino_tiny_o365v1_goldg"
model_omdet_id = "omlab/omdet-turbo-swin-tiny-hf"
model_owlv2_id = "google/owlv2-large-patch14-ensemble"

model_llmdet_name = extract_model_short_name(model_llmdet_id)
model_mm_grounding_name = extract_model_short_name(model_mm_grounding_id)
model_omdet_name = extract_model_short_name(model_omdet_id)
model_owlv2_name = extract_model_short_name(model_owlv2_id)

@spaces.GPU
def detect_omdet(image: Image.Image, prompts: list, threshold: float):
    t0 = time.perf_counter()
    model_id = model_omdet_id
    device = "cuda" if torch.cuda.is_available() else "cpu"
    processor = AutoProcessor.from_pretrained(model_id)
    model = AutoModelForZeroShotObjectDetection.from_pretrained(model_id).to(device).eval()
    texts = [prompts]
    inputs = processor(images=image, text=texts, return_tensors="pt").to(device)
    with torch.no_grad():
        outputs = model(**inputs)
    results = processor.post_process_grounded_object_detection(
        outputs,
        threshold=threshold,
        target_sizes=[image.size[::-1]]
    )   
    result = results[0]
    annotations = []
    raw_results = []
    for box, score, label in zip(result["boxes"], result["scores"], result["labels"]):
        if score >= threshold:
            label_name = prompts[label]
            xmin, ymin, xmax, ymax = [int(x) for x in box.tolist()]
            annotations.append(((xmin, ymin, xmax, ymax), f"{label_name} {score:.2f}"))
            raw_results.append(f"Detected {label_name} with confidence {score:.2f} at location [{xmin}, {ymin}, {xmax}, {ymax}]")
    elapsed_ms = (time.perf_counter() - t0) * 1000
    time_taken = f"**Inference time ({model_omdet_name}):** {elapsed_ms:.0f} ms"
    raw_text = "\n".join(raw_results) if raw_results else "No detections"
    return annotations, raw_text, time_taken
@spaces.GPU
def detect_llmdet(image: Image.Image, prompts: list, threshold: float):
    t0 = time.perf_counter()
    model_id = model_llmdet_id
    device = "cuda" if torch.cuda.is_available() else "cpu"
    processor = AutoProcessor.from_pretrained(model_id)
    model = AutoModelForZeroShotObjectDetection.from_pretrained(model_id).to(device).eval()
    texts = [prompts]
    inputs = processor(images=image, text=texts, return_tensors="pt").to(device)
    with torch.no_grad():
        outputs = model(**inputs)
    results = processor.post_process_grounded_object_detection(
        outputs,
        threshold=threshold,
        target_sizes=[image.size[::-1]]
    )
    result = results[0]
    annotations = []
    raw_results = []
    for box, score, label in zip(result["boxes"], result["scores"], result["labels"]):
        if score >= threshold:
            xmin, ymin, xmax, ymax = [int(x) for x in box.tolist()]
            annotations.append(((xmin, ymin, xmax, ymax), f"{label} {score:.2f}"))
            raw_results.append(f"Detected {label} with confidence {score:.2f} at location [{xmin}, {ymin}, {xmax}, {ymax}]")
    elapsed_ms = (time.perf_counter() - t0) * 1000
    time_taken = f"**Inference time ({model_llmdet_name}):** {elapsed_ms:.0f} ms"
    raw_text = "\n".join(raw_results) if raw_results else "No detections"
    return annotations, raw_text, time_taken
@spaces.GPU
def detect_mm_grounding(image: Image.Image, prompts: list, threshold: float):
    t0 = time.perf_counter()
    model_id = model_mm_grounding_id
    device = "cuda" if torch.cuda.is_available() else "cpu"
    processor = AutoProcessor.from_pretrained(model_id)
    model = AutoModelForZeroShotObjectDetection.from_pretrained(model_id).to(device).eval()
    texts = [prompts]
    inputs = processor(images=image, text=texts, return_tensors="pt").to(device)
    with torch.no_grad():
        outputs = model(**inputs)
    results = processor.post_process_grounded_object_detection(
        outputs,
        threshold=threshold,
        target_sizes=[image.size[::-1]]
    )
    result = results[0]
    annotations = []
    raw_results = []
    for box, score, label in zip(result["boxes"], result["scores"], result["labels"]):
        if score >= threshold:
            xmin, ymin, xmax, ymax = [int(x) for x in box.tolist()]
            annotations.append(((xmin, ymin, xmax, ymax), f"{label} {score:.2f}"))
            raw_results.append(f"Detected {label} with confidence {score:.2f} at location [{xmin}, {ymin}, {xmax}, {ymax}]")
    elapsed_ms = (time.perf_counter() - t0) * 1000
    time_taken = f"**Inference time ({model_mm_grounding_name}):** {elapsed_ms:.0f} ms"
    raw_text = "\n".join(raw_results) if raw_results else "No detections"
    return annotations, raw_text, time_taken

@spaces.GPU
def detect_owlv2(image: Image.Image, prompts: list, threshold: float):
    t0 = time.perf_counter()
    model_id = model_owlv2_id
    device = "cuda" if torch.cuda.is_available() else "cpu"
    processor = AutoProcessor.from_pretrained(model_id)
    model = AutoModelForZeroShotObjectDetection.from_pretrained(model_id).to(device).eval()
    texts = [prompts]
    inputs = processor(images=image, text=texts, return_tensors="pt").to(device)
    with torch.no_grad():
        outputs = model(**inputs)
    results = processor.post_process_grounded_object_detection(
        outputs,
        threshold=threshold,
        target_sizes=[image.size[::-1]]
    )
    result = results[0]
    annotations = []
    raw_results = []
    for box, score, label in zip(result["boxes"], result["scores"], result["labels"]):
        if score >= threshold:
            label_name = prompts[label]
            xmin, ymin, xmax, ymax = [int(x) for x in box.tolist()]
            annotations.append(((xmin, ymin, xmax, ymax), f"{label_name} {score:.2f}"))
            raw_results.append(f"Detected {label_name} with confidence {score:.2f} at location [{xmin}, {ymin}, {xmax}, {ymax}]")
    elapsed_ms = (time.perf_counter() - t0) * 1000
    time_taken = f"**Inference time ({model_owlv2_name}):** {elapsed_ms:.0f} ms"
    raw_text = "\n".join(raw_results) if raw_results else "No detections"
    return annotations, raw_text, time_taken


def run_detection(image, prompts_str, threshold_llm, threshold_mm, threshold_owlv2, threshold_omdet):
    if image is None:
        return (None, []), "No detections", "", (None, []), "No detections", ""
    prompts = [p.strip() for p in prompts_str.split(",")]
    ann_llm, raw_llm, time_llm = detect_llmdet(image, prompts, threshold_llm)
    ann_mm, raw_mm, time_mm = detect_mm_grounding(image, prompts, threshold_mm)
    ann_owlv2, raw_owlv2, time_owlv2 = detect_owlv2(image, prompts, threshold_owlv2)
    ann_omdet, raw_omdet, time_omdet = detect_omdet(image, prompts, threshold_omdet)
    return (image, ann_llm), raw_llm, time_llm, (image, ann_mm), raw_mm, time_mm, (image, ann_owlv2), raw_owlv2, time_owlv2, (image, ann_omdet), raw_omdet, time_omdet

with gr.Blocks() as app:
    gr.Markdown("# Zero-Shot Object Detection Arena")
    gr.Markdown("### Compare different zero-shot object detection models on the same image and prompts.")
    with gr.Row():
        with gr.Column(scale=1):
            image = gr.Image(type="pil", label="Upload an image", height=400)
            prompts = gr.Textbox(label="Prompts (comma-separated)", value="a cat, a remote control")
            with gr.Accordion("Per-model confidence thresholds", open=True):
                threshold_llm = gr.Slider(label="Threshold for LLMDet", minimum=0.0, maximum=1.0, value=0.3)
                threshold_mm = gr.Slider(label="Threshold for MM GroundingDINO Tiny", minimum=0.0, maximum=1.0, value=0.3)
                threshold_owlv2 = gr.Slider(label="Threshold for OwlV2 Large", minimum=0.0, maximum=1.0, value=0.1)
                threshold_omdet = gr.Slider(label="Threshold for OMDet Turbo Swin Tiny", minimum=0.0, maximum=1.0, value=0.2)
            generate_btn = gr.Button(value="Detect")
        with gr.Row():
            with gr.Column(scale=2):
                output_image_llm = gr.AnnotatedImage(label=f"Annotated image for {model_llmdet_name}", height=400)
                output_text_llm = gr.Textbox(label=f"Model detections for {model_llmdet_name}", lines=5)
                output_time_llm = gr.Markdown()
            with gr.Column(scale=2):
                output_image_mm = gr.AnnotatedImage(label=f"Annotated image for {model_mm_grounding_name}", height=400)
                output_text_mm = gr.Textbox(label=f"Model detections for {model_mm_grounding_name}", lines=5)
                output_time_mm = gr.Markdown()
        with gr.Row():
            with gr.Column(scale=2):
                output_image_owlv2 = gr.AnnotatedImage(label=f"Annotated image for {model_owlv2_name}", height=400)
                output_text_owlv2 = gr.Textbox(label=f"Model detections for {model_owlv2_name}", lines=5)
                output_time_owlv2 = gr.Markdown()
            with gr.Column(scale=2):
                output_image_omdet = gr.AnnotatedImage(label=f"Annotated image for {model_omdet_name}", height=400)
                output_text_omdet = gr.Textbox(label=f"Model detections for {model_omdet_name}", lines=5)
                output_time_omdet = gr.Markdown()
    gr.Markdown("### Examples")
    example_data = [
        ["http://images.cocodataset.org/val2017/000000039769.jpg", "a cat, a remote control", 0.30, 0.30, 0.10, 0.30],
        ["http://images.cocodataset.org/val2017/000000000139.jpg", "a person, a tv, a remote", 0.35, 0.30, 0.12, 0.30],
    ]

    gr.Examples(
        examples=example_data,
        inputs=[image, prompts, threshold_llm, threshold_mm, threshold_owlv2, threshold_omdet],
        label="Click an example to populate the inputs",
    )
    inputs = [image, prompts, threshold_llm, threshold_mm, threshold_owlv2, threshold_omdet]
    outputs = [output_image_llm, output_text_llm, output_time_llm, output_image_mm, output_text_mm, output_time_mm, output_image_owlv2, output_text_owlv2, output_time_owlv2, output_image_omdet, output_text_omdet, output_time_omdet]
    generate_btn.click(
        fn=run_detection,
        inputs=inputs,
        outputs=outputs,
    )
    image.upload(
        fn=run_detection,
        inputs=inputs,
        outputs=outputs,
    )

app.launch()