andrewkatumba commited on
Commit
1ebfb13
·
1 Parent(s): d100deb

Changes to label handiling

Browse files
Files changed (1) hide show
  1. app.py +35 -36
app.py CHANGED
@@ -1,54 +1,53 @@
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
  dino_processor = AutoProcessor.from_pretrained("IDEA-Research/grounding-dino-base")
9
- dino_model = AutoModelForZeroShotObjectDetection.from_pretrained("IDEA-Research/grounding-dino-base").to("cuda")
10
 
11
  @spaces.GPU
12
  def infer(img, text_queries, score_threshold, model):
13
 
14
- if model == "dino":
15
- queries=""
16
- for query in text_queries:
17
- queries += f"{query}. "
18
-
19
- width, height = img.shape[:2]
20
-
21
- target_sizes=[(width, height)]
22
- inputs = dino_processor(text=queries, images=img, return_tensors="pt").to(device)
23
-
24
- with torch.no_grad():
25
- outputs = dino_model(**inputs)
26
- outputs.logits = outputs.logits.cpu()
27
- outputs.pred_boxes = outputs.pred_boxes.cpu()
28
- results = dino_processor.post_process_grounded_object_detection(outputs=outputs, input_ids=inputs.input_ids,
29
- box_threshold=score_threshold,
30
- target_sizes=target_sizes)
31
 
32
- boxes, scores, labels = results[0]["boxes"], results[0]["scores"], results[0]["labels"]
33
- result_labels = []
34
 
35
- for box, score, label in zip(boxes, scores, labels):
36
- box = [int(i) for i in box.tolist()]
37
- if score < score_threshold:
38
- continue
39
 
40
- if model == "dino":
41
- if label != "":
42
- result_labels.append((box, label))
43
- return result_labels
44
 
45
  def query_image(img, text_queries, dino_threshold):
46
- text_queries = text_queries
47
  text_queries = text_queries.split(",")
48
  dino_output = infer(img, text_queries, dino_threshold, "dino")
49
-
50
-
51
- return (img, dino_output)
 
52
 
53
 
54
  dino_threshold = gr.Slider(0, 1, value=0.12, label="Grounding DINO Threshold")
@@ -56,9 +55,9 @@ dino_output = gr.AnnotatedImage(label="Grounding DINO Output")
56
  demo = gr.Interface(
57
  query_image,
58
  inputs=[gr.Image(label="Input Image"), gr.Textbox(label="Candidate Labels"), dino_threshold],
59
- outputs=[ dino_output],
60
  title="OWLv2 ⚔ Grounding DINO",
61
- description="Evaluate state-of-the-art [Grounding DINO](https://huggingface.co/IDEA-Research/grounding-dino-base) zero-shot object detection models. Simply enter an image and the objects you want to find with comma, or try one of the examples. Play with the threshold to filter out low confidence predictions in the model.",
62
  examples=[["./warthog.jpg", "zebra, warthog", 0.16], ["./zebra.jpg", "zebra, lion", 0.16]]
63
  )
64
- demo.launch(debug=True)
 
1
  import spaces
2
+ from transformers import 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
  dino_processor = AutoProcessor.from_pretrained("IDEA-Research/grounding-dino-base")
9
+ dino_model = AutoModelForZeroShotObjectDetection.from_pretrained("IDEA-Research/grounding-dino-base").to(device)
10
 
11
  @spaces.GPU
12
  def infer(img, text_queries, score_threshold, model):
13
 
14
+ if model == "dino":
15
+ queries = ""
16
+ for query in text_queries:
17
+ queries += f"{query}. "
18
+
19
+ height, width = img.shape[:2]
20
+ target_sizes = [(height, width)]
21
+ inputs = dino_processor(text=queries, images=img, return_tensors="pt").to(device)
22
+
23
+ with torch.no_grad():
24
+ outputs = dino_model(**inputs)
25
+ outputs.logits = outputs.logits.cpu()
26
+ outputs.pred_boxes = outputs.pred_boxes.cpu()
27
+ results = dino_processor.post_process_grounded_object_detection(outputs=outputs, input_ids=inputs.input_ids,
28
+ box_threshold=score_threshold,
29
+ target_sizes=target_sizes)
 
30
 
31
+ boxes, scores, labels = results[0]["boxes"], results[0]["scores"], results[0]["labels"]
32
+ result_labels = []
33
 
34
+ for box, score, label in zip(boxes, scores, labels):
35
+ box = [int(i) for i in box.tolist()]
36
+ if score < score_threshold:
37
+ continue
38
 
39
+ if model == "dino":
40
+ if label != "":
41
+ result_labels.append((box, label))
42
+ return result_labels
43
 
44
  def query_image(img, text_queries, dino_threshold):
 
45
  text_queries = text_queries.split(",")
46
  dino_output = infer(img, text_queries, dino_threshold, "dino")
47
+ annotations = []
48
+ for box, label in dino_output:
49
+ annotations.append({"label": label, "coordinates": {"x": box[0], "y": box[1], "width": box[2] - box[0], "height": box[3] - box[1]}})
50
+ return (img, {"boxes": annotations})
51
 
52
 
53
  dino_threshold = gr.Slider(0, 1, value=0.12, label="Grounding DINO Threshold")
 
55
  demo = gr.Interface(
56
  query_image,
57
  inputs=[gr.Image(label="Input Image"), gr.Textbox(label="Candidate Labels"), dino_threshold],
58
+ outputs=[dino_output],
59
  title="OWLv2 ⚔ Grounding DINO",
60
+ description="Evaluate state-of-the-art [Grounding DINO](https://huggingface.co/IDEA-Research/grounding-dino-base) zero-shot object detection models. Simply enter an image and the objects you want to find with comma, or try one of the examples. Play with the threshold to filter out low confidence predictions in the model.",
61
  examples=[["./warthog.jpg", "zebra, warthog", 0.16], ["./zebra.jpg", "zebra, lion", 0.16]]
62
  )
63
+ demo.launch(debug=True)