eadali commited on
Commit
7d4d822
Β·
1 Parent(s): 2383ce5

Ready for testing

Browse files
.gitignore CHANGED
@@ -1,6 +1,3 @@
1
- # Virtual environment
2
- pesar_env/
3
-
4
  # Byte-compiled / optimized / DLL files
5
  __pycache__/
6
  *.py[cod]
@@ -137,6 +134,8 @@ venv/
137
  ENV/
138
  env.bak/
139
  venv.bak/
 
 
140
 
141
  # Spyder project settings
142
  .spyderproject
 
 
 
 
1
  # Byte-compiled / optimized / DLL files
2
  __pycache__/
3
  *.py[cod]
 
134
  ENV/
135
  env.bak/
136
  venv.bak/
137
+ pisar_env/
138
+
139
 
140
  # Spyder project settings
141
  .spyderproject
README.md CHANGED
@@ -1,6 +1,6 @@
1
  ---
2
- title: PeSAR
3
- emoji: πŸ“‘
4
  colorFrom: green
5
  colorTo: yellow
6
  sdk: gradio
@@ -8,7 +8,7 @@ sdk_version: 5.29.0
8
  app_file: app.py
9
  pinned: false
10
  license: agpl-3.0
11
- short_description: 'Perception for Aerial Search and Rescue'
12
  ---
13
 
14
  Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
1
  ---
2
+ title: PiSAR
3
+ emoji: 🚁
4
  colorFrom: green
5
  colorTo: yellow
6
  sdk: gradio
 
8
  app_file: app.py
9
  pinned: false
10
  license: agpl-3.0
11
+ short_description: 'Pipelines for Aerial Search and Rescue'
12
  ---
13
 
14
  Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
app.py CHANGED
@@ -24,32 +24,19 @@ from utils import cfg, load_config, load_onnx_model
24
 
25
 
26
  # Configuration constants
27
- CHECKPOINTS = [
28
- "yolo8n-cpu",
29
- # "ustc-community/dfine-medium-obj2coco",
30
- # "ustc-community/dfine-medium-coco",
31
- # "ustc-community/dfine-medium-obj365",
32
- # "ustc-community/dfine-nano-coco",
33
- # "ustc-community/dfine-small-coco",
34
- # "ustc-community/dfine-large-coco",
35
- # "ustc-community/dfine-xlarge-coco",
36
- # "ustc-community/dfine-small-obj365",
37
- # "ustc-community/dfine-large-obj365",
38
- # "ustc-community/dfine-xlarge-obj365",
39
- # "ustc-community/dfine-small-obj2coco",
40
- # "ustc-community/dfine-large-obj2coco-e25",
41
- # "ustc-community/dfine-xlarge-obj2coco",
42
- ]
43
- DEFAULT_CHECKPOINT = CHECKPOINTS[0]
44
- DEFAULT_CONFIDENCE_THRESHOLD = 0.3
45
 
46
  TORCH_DTYPE = torch.float32
47
 
48
  # Image
49
  IMAGE_EXAMPLES = [
50
- {"path": "./examples/images/road.jpg", "use_url": False, "url": "", "label": "Local Image"},
51
- {"path": "./examples/images/bay.jpg", "use_url": False, "url": "", "label": "Local Image"},
52
- {"path": "./examples/images/airport.jpg", "use_url": False, "url": "", "label": "Local Image"},
53
  {
54
  "path": None,
55
  "use_url": True,
@@ -73,7 +60,7 @@ class TrackingAlgorithm:
73
  TRACKERS = [None, TrackingAlgorithm.BYTETRACK, TrackingAlgorithm.DEEPSORT, TrackingAlgorithm.SORT]
74
  VIDEO_EXAMPLES = [
75
  {"path": "./examples/videos/dogs_running.mp4", "label": "Local Video", "tracker": None, "classes": "all"},
76
- {"path": "./examples/videos/traffic.mp4", "label": "Local Video", "tracker": TrackingAlgorithm.BYTETRACK, "classes": "car, truck, bus"},
77
  {"path": "./examples/videos/fast_and_furious.mp4", "label": "Local Video", "tracker": None, "classes": "all"},
78
  {"path": "./examples/videos/break_dance.mp4", "label": "Local Video", "tracker": None, "classes": "all"},
79
  ]
@@ -93,16 +80,15 @@ logging.basicConfig(
93
  logger = logging.getLogger(__name__)
94
 
95
 
96
- @lru_cache(maxsize=3)
97
- def get_model_and_processor(checkpoint: str):
98
- model = AutoModelForObjectDetection.from_pretrained(checkpoint, torch_dtype=TORCH_DTYPE)
99
- image_processor = AutoImageProcessor.from_pretrained(checkpoint)
100
- return model, image_processor
101
 
102
 
103
- # @spaces.GPU(duration=20)
104
  def detect_objects(
105
- checkpoint: str,
 
106
  images: List[np.ndarray] | np.ndarray,
107
  confidence_threshold: float = DEFAULT_CONFIDENCE_THRESHOLD,
108
  target_size: Optional[Tuple[int, int]] = None,
@@ -114,15 +100,20 @@ def detect_objects(
114
  # model, image_processor = get_model_and_processor(checkpoint)
115
  # model = model.to(device)
116
 
117
- load_config(cfg, f'configs/{checkpoint}.yaml')
118
- pipeline = build_pipeline(cfg.pipeline)
119
- load_onnx_model(pipeline.detector, 'downloads/yolo8n-416.onnx')
120
-
 
 
 
 
 
121
  if classes is not None:
122
- wrong_classes = [cls for cls in classes if cls not in model.config.label2id]
123
  if wrong_classes:
124
  gr.Warning(f"Classes not found in model config: {wrong_classes}")
125
- keep_ids = [model.config.label2id[cls] for cls in classes if cls in model.config.label2id]
126
  else:
127
  keep_ids = None
128
 
@@ -142,16 +133,16 @@ def detect_objects(
142
  # with torch.no_grad():
143
  # outputs = model(**inputs)
144
 
145
- outputs = []
146
  for i in range(len(batch)):
147
  img = batch[i]
148
  output_ = pipeline(img)
149
- output_1 = {
150
  "scores": torch.from_numpy(output_.confidence) if isinstance(output_.confidence, np.ndarray) else output_.confidence,
151
  "labels": torch.from_numpy(output_.class_id) if isinstance(output_.class_id, np.ndarray) else output_.class_id,
152
  "boxes": torch.from_numpy(output_.xyxy) if isinstance(output_.xyxy, np.ndarray) else output_.xyxy,
153
  }
154
- outputs.append(output_1)
155
 
156
 
157
  # postprocess outputs
@@ -163,16 +154,15 @@ def detect_objects(
163
  # batch_results = image_processor.post_process_object_detection(
164
  # outputs, target_sizes=target_sizes, threshold=confidence_threshold
165
  # )
166
- batch_results = outputs
167
 
168
  results.extend(batch_results)
169
 
170
- # move results to cpu
171
- for i, result in enumerate(results):
172
- results[i] = {k: v.cpu() for k, v in result.items()}
173
- if keep_ids is not None:
174
- keep = torch.isin(results[i]["labels"], torch.tensor(keep_ids))
175
- results[i] = {k: v[keep] for k, v in results[i].items()}
176
 
177
  # return results, model.config.id2label
178
  return results, pipeline.detector.get_category_mapping()
@@ -193,9 +183,11 @@ def process_image(
193
 
194
  if url:
195
  image = load_image(url)
196
-
 
197
  results, id2label = detect_objects(
198
- checkpoint=checkpoint,
 
199
  images=[np.array(image)],
200
  confidence_threshold=confidence_threshold,
201
  )
@@ -313,9 +305,11 @@ def process_video(
313
  else:
314
  classes_list = None
315
 
 
316
  results, id2label = detect_objects(
 
 
317
  images=np.array(frames),
318
- checkpoint=checkpoint,
319
  confidence_threshold=confidence_threshold,
320
  target_size=(target_height, target_width),
321
  classes=classes_list,
@@ -368,7 +362,7 @@ def create_image_inputs() -> List[gr.components.Component]:
368
  elem_classes="input-component",
369
  ),
370
  gr.Dropdown(
371
- choices=CHECKPOINTS,
372
  label="Select Model Checkpoint",
373
  value=DEFAULT_CHECKPOINT,
374
  elem_classes="input-component",
@@ -394,7 +388,7 @@ def create_video_inputs() -> List[gr.components.Component]:
394
  elem_classes="input-component",
395
  ),
396
  gr.Dropdown(
397
- choices=CHECKPOINTS,
398
  label="Select Model Checkpoint",
399
  value=DEFAULT_CHECKPOINT,
400
  elem_classes="input-component",
 
24
 
25
 
26
  # Configuration constants
27
+ DETECTORS = {
28
+ "yolo8n-640": 'downloads/yolo8n-640.onnx',
29
+ "yolo8n-416": 'downloads/yolo8n-416.onnx',
30
+ }
31
+ DEFAULT_CHECKPOINT = list(DETECTORS.keys())[0]
32
+ DEFAULT_CONFIDENCE_THRESHOLD = 0.6
 
 
 
 
 
 
 
 
 
 
 
 
33
 
34
  TORCH_DTYPE = torch.float32
35
 
36
  # Image
37
  IMAGE_EXAMPLES = [
38
+ {"path": "./examples/images/forest.jpg", "use_url": False, "url": "", "label": "Local Image"},
39
+ {"path": "./examples/images/coast.jpg", "use_url": False, "url": "", "label": "Local Image"},
 
40
  {
41
  "path": None,
42
  "use_url": True,
 
60
  TRACKERS = [None, TrackingAlgorithm.BYTETRACK, TrackingAlgorithm.DEEPSORT, TrackingAlgorithm.SORT]
61
  VIDEO_EXAMPLES = [
62
  {"path": "./examples/videos/dogs_running.mp4", "label": "Local Video", "tracker": None, "classes": "all"},
63
+ {"path": "./examples/videos/forest.mp4", "label": "Local Video", "tracker": TrackingAlgorithm.BYTETRACK, "classes": "car, truck, bus"},
64
  {"path": "./examples/videos/fast_and_furious.mp4", "label": "Local Video", "tracker": None, "classes": "all"},
65
  {"path": "./examples/videos/break_dance.mp4", "label": "Local Video", "tracker": None, "classes": "all"},
66
  ]
 
80
  logger = logging.getLogger(__name__)
81
 
82
 
83
+ def get_pipeline(config: dict, onnx_path: str):
84
+ pipeline = build_pipeline(config)
85
+ load_onnx_model(pipeline.detector, onnx_path)
86
+ return pipeline
 
87
 
88
 
 
89
  def detect_objects(
90
+ config: dict,
91
+ onnx_path: str,
92
  images: List[np.ndarray] | np.ndarray,
93
  confidence_threshold: float = DEFAULT_CONFIDENCE_THRESHOLD,
94
  target_size: Optional[Tuple[int, int]] = None,
 
100
  # model, image_processor = get_model_and_processor(checkpoint)
101
  # model = model.to(device)
102
 
103
+ # load_config(cfg, f'configs/{checkpoint}.yaml')
104
+ # pipeline = build_pipeline(cfg.pipeline)
105
+ # load_onnx_model(pipeline.detector, 'downloads/yolo8n-416.onnx')
106
+ # config.detector.thresholds.confidence = confidence_threshold
107
+ config.defrost()
108
+ config.detector.thresholds.confidence = confidence_threshold
109
+ config.freeze()
110
+ pipeline = get_pipeline(config, onnx_path)
111
+ detector_category_mapping = pipeline.detector.get_category_mapping()
112
  if classes is not None:
113
+ wrong_classes = [cls for cls in classes if cls not in detector_category_mapping]
114
  if wrong_classes:
115
  gr.Warning(f"Classes not found in model config: {wrong_classes}")
116
+ keep_ids = [detector_category_mapping[cls] for cls in classes if cls in detector_category_mapping]
117
  else:
118
  keep_ids = None
119
 
 
133
  # with torch.no_grad():
134
  # outputs = model(**inputs)
135
 
136
+ batch_results = []
137
  for i in range(len(batch)):
138
  img = batch[i]
139
  output_ = pipeline(img)
140
+ output = {
141
  "scores": torch.from_numpy(output_.confidence) if isinstance(output_.confidence, np.ndarray) else output_.confidence,
142
  "labels": torch.from_numpy(output_.class_id) if isinstance(output_.class_id, np.ndarray) else output_.class_id,
143
  "boxes": torch.from_numpy(output_.xyxy) if isinstance(output_.xyxy, np.ndarray) else output_.xyxy,
144
  }
145
+ batch_results.append(output)
146
 
147
 
148
  # postprocess outputs
 
154
  # batch_results = image_processor.post_process_object_detection(
155
  # outputs, target_sizes=target_sizes, threshold=confidence_threshold
156
  # )
 
157
 
158
  results.extend(batch_results)
159
 
160
+ # # move results to cpu
161
+ # for i, result in enumerate(results):
162
+ # results[i] = {k: v.cpu() for k, v in result.items()}
163
+ # if keep_ids is not None:
164
+ # keep = torch.isin(results[i]["labels"], torch.tensor(keep_ids))
165
+ # results[i] = {k: v[keep] for k, v in results[i].items()}
166
 
167
  # return results, model.config.id2label
168
  return results, pipeline.detector.get_category_mapping()
 
183
 
184
  if url:
185
  image = load_image(url)
186
+
187
+ load_config(cfg, f'configs/{checkpoint}.yaml')
188
  results, id2label = detect_objects(
189
+ config=cfg.pipeline,
190
+ onnx_path=DETECTORS[checkpoint],
191
  images=[np.array(image)],
192
  confidence_threshold=confidence_threshold,
193
  )
 
305
  else:
306
  classes_list = None
307
 
308
+ load_config(cfg, f'configs/{checkpoint}.yaml')
309
  results, id2label = detect_objects(
310
+ config=cfg.pipeline,
311
+ onnx_path=DETECTORS[checkpoint],
312
  images=np.array(frames),
 
313
  confidence_threshold=confidence_threshold,
314
  target_size=(target_height, target_width),
315
  classes=classes_list,
 
362
  elem_classes="input-component",
363
  ),
364
  gr.Dropdown(
365
+ choices=list(DETECTORS.keys()),
366
  label="Select Model Checkpoint",
367
  value=DEFAULT_CHECKPOINT,
368
  elem_classes="input-component",
 
388
  elem_classes="input-component",
389
  ),
390
  gr.Dropdown(
391
+ choices=list(DETECTORS.keys()),
392
  label="Select Model Checkpoint",
393
  value=DEFAULT_CHECKPOINT,
394
  elem_classes="input-component",
configs/{yolo8n-cpu.yaml β†’ yolo8n-416.yaml} RENAMED
File without changes
configs/yolo8n-640.yaml ADDED
@@ -0,0 +1,15 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # YOLOv8n + ByteTrack Configuration
2
+ pipeline:
3
+ detector:
4
+ model: yolov8n
5
+ categories: ['LightVehicle', 'Person', 'Building', 'UPole', 'Boat', 'Bike', 'Container', 'Truck', 'Gastank', 'Digger', 'Solarpanels', 'Bus']
6
+ thresholds:
7
+ confidence: 0.6
8
+ iou: 0.4
9
+ slicing:
10
+ overlap: 0.2
11
+ device: cpu
12
+
13
+ tracker:
14
+ algorithm: dummytrack
15
+
examples/images/road.jpg β†’ downloads/yolo8n-640.onnx RENAMED
@@ -1,3 +1,3 @@
1
  version https://git-lfs.github.com/spec/v1
2
- oid sha256:fe183df95295eb7a5a0e55810c1c5e46412eeb6f99029267a8cd45f4437280af
3
- size 5325543
 
1
  version https://git-lfs.github.com/spec/v1
2
+ oid sha256:346359fb811146dc1793406cbad1ac5f88dddce172276f24beae789e85d5efba
3
+ size 12259807
examples/images/{airport.jpg β†’ coast.jpg} RENAMED
File without changes
examples/images/{bay.jpg β†’ forest.jpg} RENAMED
File without changes
examples/videos/dogs_running.mp4 DELETED
@@ -1,3 +0,0 @@
1
- version https://git-lfs.github.com/spec/v1
2
- oid sha256:043c1a28bddcd9beeddbab946c38d1a91de2a18da3ae114d307cd01b30b64ca2
3
- size 22119043
 
 
 
 
examples/videos/fast_and_furious.mp4 DELETED
@@ -1,3 +0,0 @@
1
- version https://git-lfs.github.com/spec/v1
2
- oid sha256:5980eada9d80c65b4da5b536427ccf8ff8ea2707ee3e4aa52fb2c4e1b1979dae
3
- size 16872922
 
 
 
 
examples/videos/{break_dance.mp4 β†’ forest.mp4} RENAMED
@@ -1,3 +1,3 @@
1
  version https://git-lfs.github.com/spec/v1
2
- oid sha256:747f9c2f9d19e4955603e1a13b69663187882d4c6a8fbcad18ddbd04ee792d4d
3
- size 1972564
 
1
  version https://git-lfs.github.com/spec/v1
2
+ oid sha256:a6e1f240ad106045504afbd114e79387cceb11877af61f759f5371c548b6dab6
3
+ size 14931145
examples/videos/traffic.mp4 DELETED
@@ -1,3 +0,0 @@
1
- version https://git-lfs.github.com/spec/v1
2
- oid sha256:71908c136bba6b50b9071fb2015553f651c91a7ee857924f33616c046011aaed
3
- size 8591523