SkalskiP commited on
Commit
f991fc8
·
1 Parent(s): 9823e08

Upgrade rfdetr to v1.6.5.post2 and migrate to proper segmentation checkpoints

Browse files
Files changed (1) hide show
  1. app.py +59 -12
app.py CHANGED
@@ -6,7 +6,11 @@ import gradio as gr
6
  import numpy as np
7
  import supervision as sv
8
  from PIL import Image
9
- from rfdetr import RFDETRNano, RFDETRSmall, RFDETRMedium, RFDETRBase, RFDETRLarge, RFDETRSegPreview
 
 
 
 
10
  from rfdetr.detr import RFDETR
11
  from rfdetr.util.coco_classes import COCO_CLASSES
12
 
@@ -31,6 +35,9 @@ IMAGE_PROCESSING_EXAMPLES = [
31
  ['https://media.roboflow.com/notebooks/examples/dog-2.jpeg', 0.5, 512, "nano (object detection)"],
32
  ['https://media.roboflow.com/notebooks/examples/dog-3.jpeg', 0.5, 512, "nano (object detection)"],
33
  ['https://media.roboflow.com/supervision/image-examples/basketball-1.png', 0.5, 512, "nano (object detection)"],
 
 
 
34
  ]
35
  VIDEO_PROCESSING_EXAMPLES = [
36
  ["videos/people-walking.mp4", 0.3, 1024, "medium (object detection)"],
@@ -76,10 +83,10 @@ def detect_and_annotate(
76
  ]
77
  print(detections)
78
  annotated_image = image.copy()
 
 
79
  annotated_image = bbox_annotator.annotate(annotated_image, detections)
80
  annotated_image = label_annotator.annotate(annotated_image, detections, labels)
81
- if checkpoint == "segmentation preview":
82
- annotated_image = mask_annotator.annotate(annotated_image, detections)
83
  return annotated_image
84
 
85
 
@@ -92,15 +99,35 @@ def load_model(resolution: int, checkpoint: str) -> RFDETR:
92
  return RFDETRMedium(resolution=resolution)
93
  if checkpoint == "base (object detection)":
94
  return RFDETRBase(resolution=resolution)
95
- elif checkpoint == "large (object detection)":
96
  return RFDETRLarge(resolution=resolution)
97
- elif checkpoint == "segmentation preview":
98
- return RFDETRSegPreview(resolution=resolution)
99
- raise TypeError("Checkpoint must be a base or large.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
100
 
101
 
102
  def adjust_resolution(checkpoint: str, resolution: int) -> int:
103
- if checkpoint == "segmentation preview":
104
  divisor = 24
105
  elif checkpoint in {"nano (object detection)", "small (object detection)", "medium (object detection)"}:
106
  divisor = 32
@@ -198,8 +225,18 @@ with gr.Blocks() as demo:
198
  )
199
  image_processing_checkpoint_dropdown = gr.Dropdown(
200
  label="Checkpoint",
201
- choices=["nano (object detection)", "small (object detection)", "medium (object detection)", "segmentation preview"],
202
- value="segmentation preview"
 
 
 
 
 
 
 
 
 
 
203
  )
204
  with gr.Column():
205
  image_processing_submit_button = gr.Button("Submit", value="primary")
@@ -254,8 +291,18 @@ with gr.Blocks() as demo:
254
  )
255
  video_processing_checkpoint_dropdown = gr.Dropdown(
256
  label="Checkpoint",
257
- choices=["nano (object detection)", "small (object detection)", "medium (object detection)", "segmentation preview"],
258
- value="segmentation preview"
 
 
 
 
 
 
 
 
 
 
259
  )
260
  with gr.Column():
261
  video_processing_submit_button = gr.Button("Submit", value="primary")
 
6
  import numpy as np
7
  import supervision as sv
8
  from PIL import Image
9
+ from rfdetr import (
10
+ RFDETRNano, RFDETRSmall, RFDETRMedium, RFDETRBase, RFDETRLarge,
11
+ RFDETRSegNano, RFDETRSegSmall, RFDETRSegMedium,
12
+ RFDETRSegLarge, RFDETRSegXLarge, RFDETRSeg2XLarge,
13
+ )
14
  from rfdetr.detr import RFDETR
15
  from rfdetr.util.coco_classes import COCO_CLASSES
16
 
 
35
  ['https://media.roboflow.com/notebooks/examples/dog-2.jpeg', 0.5, 512, "nano (object detection)"],
36
  ['https://media.roboflow.com/notebooks/examples/dog-3.jpeg', 0.5, 512, "nano (object detection)"],
37
  ['https://media.roboflow.com/supervision/image-examples/basketball-1.png', 0.5, 512, "nano (object detection)"],
38
+ ['https://media.roboflow.com/notebooks/examples/dog-2.jpeg', 0.5, 512, "medium (instance segmentation)"],
39
+ ['https://media.roboflow.com/notebooks/examples/dog-3.jpeg', 0.5, 512, "medium (instance segmentation)"],
40
+ ['https://media.roboflow.com/supervision/image-examples/basketball-1.png', 0.5, 512, "medium (instance segmentation)"],
41
  ]
42
  VIDEO_PROCESSING_EXAMPLES = [
43
  ["videos/people-walking.mp4", 0.3, 1024, "medium (object detection)"],
 
83
  ]
84
  print(detections)
85
  annotated_image = image.copy()
86
+ if checkpoint in SEGMENTATION_CHECKPOINTS:
87
+ annotated_image = mask_annotator.annotate(annotated_image, detections)
88
  annotated_image = bbox_annotator.annotate(annotated_image, detections)
89
  annotated_image = label_annotator.annotate(annotated_image, detections, labels)
 
 
90
  return annotated_image
91
 
92
 
 
99
  return RFDETRMedium(resolution=resolution)
100
  if checkpoint == "base (object detection)":
101
  return RFDETRBase(resolution=resolution)
102
+ if checkpoint == "large (object detection)":
103
  return RFDETRLarge(resolution=resolution)
104
+ if checkpoint == "nano (instance segmentation)":
105
+ return RFDETRSegNano(resolution=resolution)
106
+ if checkpoint == "small (instance segmentation)":
107
+ return RFDETRSegSmall(resolution=resolution)
108
+ if checkpoint == "medium (instance segmentation)":
109
+ return RFDETRSegMedium(resolution=resolution)
110
+ if checkpoint == "large (instance segmentation)":
111
+ return RFDETRSegLarge(resolution=resolution)
112
+ if checkpoint == "xlarge (instance segmentation)":
113
+ return RFDETRSegXLarge(resolution=resolution)
114
+ if checkpoint == "2xlarge (instance segmentation)":
115
+ return RFDETRSeg2XLarge(resolution=resolution)
116
+ raise TypeError(f"Unknown checkpoint: {checkpoint}")
117
+
118
+
119
+ SEGMENTATION_CHECKPOINTS = {
120
+ "nano (instance segmentation)",
121
+ "small (instance segmentation)",
122
+ "medium (instance segmentation)",
123
+ "large (instance segmentation)",
124
+ "xlarge (instance segmentation)",
125
+ "2xlarge (instance segmentation)",
126
+ }
127
 
128
 
129
  def adjust_resolution(checkpoint: str, resolution: int) -> int:
130
+ if checkpoint in SEGMENTATION_CHECKPOINTS:
131
  divisor = 24
132
  elif checkpoint in {"nano (object detection)", "small (object detection)", "medium (object detection)"}:
133
  divisor = 32
 
225
  )
226
  image_processing_checkpoint_dropdown = gr.Dropdown(
227
  label="Checkpoint",
228
+ choices=[
229
+ "nano (object detection)",
230
+ "small (object detection)",
231
+ "medium (object detection)",
232
+ "nano (instance segmentation)",
233
+ "small (instance segmentation)",
234
+ "medium (instance segmentation)",
235
+ "large (instance segmentation)",
236
+ "xlarge (instance segmentation)",
237
+ "2xlarge (instance segmentation)",
238
+ ],
239
+ value="medium (object detection)"
240
  )
241
  with gr.Column():
242
  image_processing_submit_button = gr.Button("Submit", value="primary")
 
291
  )
292
  video_processing_checkpoint_dropdown = gr.Dropdown(
293
  label="Checkpoint",
294
+ choices=[
295
+ "nano (object detection)",
296
+ "small (object detection)",
297
+ "medium (object detection)",
298
+ "nano (instance segmentation)",
299
+ "small (instance segmentation)",
300
+ "medium (instance segmentation)",
301
+ "large (instance segmentation)",
302
+ "xlarge (instance segmentation)",
303
+ "2xlarge (instance segmentation)",
304
+ ],
305
+ value="medium (object detection)"
306
  )
307
  with gr.Column():
308
  video_processing_submit_button = gr.Button("Submit", value="primary")