Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import spaces
|
| 2 |
+
from transformers import Owlv2Processor, Owlv2ForObjectDetection, AutoProcessor, AutoModelForZeroShotObjectDetection
|
| 3 |
+
import torch
|
| 4 |
+
import gradio as gr
|
| 5 |
+
|
| 6 |
+
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
|
| 7 |
+
|
| 8 |
+
owl_model = Owlv2ForObjectDetection.from_pretrained("google/owlv2-base-patch16-ensemble").to(device)
|
| 9 |
+
owl_processor = Owlv2Processor.from_pretrained("google/owlv2-base-patch16-ensemble")
|
| 10 |
+
|
| 11 |
+
dino_processor = AutoProcessor.from_pretrained("IDEA-Research/grounding-dino-base")
|
| 12 |
+
dino_model = AutoModelForZeroShotObjectDetection.from_pretrained("IDEA-Research/grounding-dino-base").to(device)
|
| 13 |
+
|
| 14 |
+
@spaces.GPU
|
| 15 |
+
def infer(img, text_queries, score_threshold, model):
|
| 16 |
+
|
| 17 |
+
if model == "dino":
|
| 18 |
+
queries=""
|
| 19 |
+
for query in text_queries:
|
| 20 |
+
queries += f"{query}. "
|
| 21 |
+
|
| 22 |
+
width, height = img.shape[:2]
|
| 23 |
+
|
| 24 |
+
target_sizes=[(width, height)]
|
| 25 |
+
inputs = dino_processor(text=queries, images=img, return_tensors="pt").to(device)
|
| 26 |
+
|
| 27 |
+
with torch.no_grad():
|
| 28 |
+
outputs = dino_model(**inputs)
|
| 29 |
+
outputs.logits = outputs.logits.cpu()
|
| 30 |
+
outputs.pred_boxes = outputs.pred_boxes.cpu()
|
| 31 |
+
results = dino_processor.post_process_grounded_object_detection(outputs=outputs, input_ids=inputs.input_ids,
|
| 32 |
+
box_threshold=score_threshold,
|
| 33 |
+
target_sizes=target_sizes)
|
| 34 |
+
elif model == "owl":
|
| 35 |
+
size = max(img.shape[:2])
|
| 36 |
+
target_sizes = torch.Tensor([[size, size]])
|
| 37 |
+
inputs = owl_processor(text=text_queries, images=img, return_tensors="pt").to(device)
|
| 38 |
+
|
| 39 |
+
with torch.no_grad():
|
| 40 |
+
outputs = owl_model(**inputs)
|
| 41 |
+
outputs.logits = outputs.logits.cpu()
|
| 42 |
+
outputs.pred_boxes = outputs.pred_boxes.cpu()
|
| 43 |
+
results = owl_processor.post_process_object_detection(outputs=outputs, target_sizes=target_sizes)
|
| 44 |
+
|
| 45 |
+
boxes, scores, labels = results[0]["boxes"], results[0]["scores"], results[0]["labels"]
|
| 46 |
+
result_labels = []
|
| 47 |
+
|
| 48 |
+
for box, score, label in zip(boxes, scores, labels):
|
| 49 |
+
box = [int(i) for i in box.tolist()]
|
| 50 |
+
if score < score_threshold:
|
| 51 |
+
continue
|
| 52 |
+
if model == "owl":
|
| 53 |
+
label = text_queries[label.cpu().item()]
|
| 54 |
+
result_labels.append((box, label))
|
| 55 |
+
return result_labels
|
| 56 |
+
|
| 57 |
+
def query_image(img, text_queries, owl_threshold, dino_threshold):
|
| 58 |
+
text_queries = text_queries
|
| 59 |
+
text_queries = text_queries.split(",")
|
| 60 |
+
owl_output = infer(img, text_queries, owl_threshold, "owl")
|
| 61 |
+
dino_output = infer(img, text_queries, owl_threshold, "dino")
|
| 62 |
+
|
| 63 |
+
|
| 64 |
+
return (img, owl_output), (img, dino_output)
|
| 65 |
+
|
| 66 |
+
|
| 67 |
+
owl_threshold = gr.Slider(0, 1, value=0.16, label="OWL Threshold")
|
| 68 |
+
dino_threshold = gr.Slider(0, 1, value=0.12, label="Grounding DINO Threshold")
|
| 69 |
+
owl_output = gr.AnnotatedImage(label="OWL Output")
|
| 70 |
+
dino_output = gr.AnnotatedImage(label="Grounding DINO Output")
|
| 71 |
+
demo = gr.Interface(
|
| 72 |
+
query_image,
|
| 73 |
+
inputs=[gr.Image(label="Input Image"), gr.Textbox("Candidate Labels"), owl_threshold, dino_threshold],
|
| 74 |
+
outputs=[owl_output, dino_output],
|
| 75 |
+
title="Zero-Shot Object Detection with OWLv2",
|
| 76 |
+
examples=[["./bee.jpg", "bee, flower", 0.16, 0.12]]
|
| 77 |
+
)
|
| 78 |
+
demo.launch(debug=True)
|