Christen Millerdurai commited on
Commit
b583cbf
·
1 Parent(s): b59067d
Files changed (3) hide show
  1. app.py +1 -0
  2. egoforce_runtime_patches.py +188 -9
  3. requirements.txt +4 -0
app.py CHANGED
@@ -36,6 +36,7 @@ def configure_runtime_environment() -> None:
36
  os.environ.setdefault("PYOPENGL_PLATFORM", "egl")
37
  os.environ.setdefault("MPLBACKEND", "Agg")
38
  os.environ.setdefault("TOKENIZERS_PARALLELISM", "false")
 
39
 
40
 
41
  def configure_torch_cuda_arch_list() -> None:
 
36
  os.environ.setdefault("PYOPENGL_PLATFORM", "egl")
37
  os.environ.setdefault("MPLBACKEND", "Agg")
38
  os.environ.setdefault("TOKENIZERS_PARALLELISM", "false")
39
+ os.environ.setdefault("TORCH_FORCE_NO_WEIGHTS_ONLY_LOAD", "1")
40
 
41
 
42
  def configure_torch_cuda_arch_list() -> None:
egoforce_runtime_patches.py CHANGED
@@ -30,6 +30,52 @@ def _torchvision_roi_pool_module():
30
  return RoIPool
31
 
32
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
33
  def _bbox_overlaps(
34
  bboxes1: Any,
35
  bboxes2: Any,
@@ -162,6 +208,100 @@ def _batched_nms(
162
  return dets, keep
163
 
164
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
165
  def apply_runtime_patches() -> None:
166
  try:
167
  mmcv = importlib.import_module("mmcv")
@@ -174,26 +314,65 @@ def apply_runtime_patches() -> None:
174
  sys.modules["mmcv.ops"] = ops_module
175
  ops_module.__path__ = []
176
 
177
- nms_module = sys.modules.get("mmcv.ops.nms")
178
- if nms_module is None:
179
- nms_module = types.ModuleType("mmcv.ops.nms")
180
- sys.modules["mmcv.ops.nms"] = nms_module
181
-
182
- roi_align_module = sys.modules.get("mmcv.ops.roi_align")
183
- if roi_align_module is None:
184
- roi_align_module = types.ModuleType("mmcv.ops.roi_align")
185
- sys.modules["mmcv.ops.roi_align"] = roi_align_module
 
 
 
 
 
 
 
 
 
 
186
 
187
  ops_module.nms = _nms
188
  ops_module.batched_nms = _batched_nms
 
 
 
 
189
  ops_module.bbox_overlaps = _bbox_overlaps
190
  ops_module.roi_align = _torchvision_roi_align()
191
  ops_module.RoIAlign = _torchvision_roi_align_module()
192
  ops_module.RoIPool = _torchvision_roi_pool_module()
 
 
 
 
 
 
 
 
 
 
193
  nms_module.nms = _nms
194
  nms_module.batched_nms = _batched_nms
195
  roi_align_module.roi_align = ops_module.roi_align
196
  roi_align_module.RoIAlign = ops_module.RoIAlign
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
197
 
198
  if mmcv is not None:
199
  mmcv.ops = ops_module
 
30
  return RoIPool
31
 
32
 
33
+ def _multiscale_deformable_attention_class():
34
+ import torch.nn as nn
35
+
36
+ class MultiScaleDeformableAttention(nn.Module):
37
+ """Import-only fallback for mmdet registries when running with mmcv-lite."""
38
+
39
+ def __init__(self, *args: Any, **kwargs: Any) -> None:
40
+ super().__init__()
41
+
42
+ def init_weights(self) -> None:
43
+ return None
44
+
45
+ def forward(self, *args: Any, **kwargs: Any) -> Any:
46
+ raise RuntimeError(
47
+ "MultiScaleDeformableAttention requires full mmcv with compiled ops. "
48
+ "The EgoForce demo uses RTMDet and should not execute this layer."
49
+ )
50
+
51
+ return MultiScaleDeformableAttention
52
+
53
+
54
+ def _unsupported_module_class(name: str):
55
+ import torch.nn as nn
56
+
57
+ class UnsupportedMMCVOp(nn.Module):
58
+ def __init__(self, *args: Any, **kwargs: Any) -> None:
59
+ super().__init__()
60
+
61
+ def init_weights(self) -> None:
62
+ return None
63
+
64
+ def forward(self, *args: Any, **kwargs: Any) -> Any:
65
+ raise RuntimeError(f"{name} requires full mmcv with compiled ops and is not used by the EgoForce RTMDet demo.")
66
+
67
+ UnsupportedMMCVOp.__name__ = name
68
+ return UnsupportedMMCVOp
69
+
70
+
71
+ def _unsupported_function(name: str):
72
+ def unsupported(*args: Any, **kwargs: Any) -> Any:
73
+ raise RuntimeError(f"{name} requires full mmcv with compiled ops and is not used by the EgoForce RTMDet demo.")
74
+
75
+ unsupported.__name__ = name
76
+ return unsupported
77
+
78
+
79
  def _bbox_overlaps(
80
  bboxes1: Any,
81
  bboxes2: Any,
 
208
  return dets, keep
209
 
210
 
211
+ def _nms_match(dets: Any, iou_threshold: float) -> list[Any]:
212
+ """Pure PyTorch fallback for mmcv.ops.nms_match import paths."""
213
+ import torch
214
+
215
+ if dets.numel() == 0:
216
+ return []
217
+
218
+ boxes = dets[:, :4]
219
+ scores = dets[:, 4]
220
+ order = scores.argsort(descending=True)
221
+ groups = []
222
+
223
+ while order.numel() > 0:
224
+ current = order[0]
225
+ if order.numel() == 1:
226
+ groups.append(current.reshape(1))
227
+ break
228
+
229
+ rest = order[1:]
230
+ lt = torch.maximum(boxes[current, :2], boxes[rest, :2])
231
+ rb = torch.minimum(boxes[current, 2:], boxes[rest, 2:])
232
+ wh = (rb - lt).clamp(min=0)
233
+ inter = wh[:, 0] * wh[:, 1]
234
+ current_area = (boxes[current, 2] - boxes[current, 0]).clamp(min=0) * (
235
+ boxes[current, 3] - boxes[current, 1]
236
+ ).clamp(min=0)
237
+ rest_area = (boxes[rest, 2] - boxes[rest, 0]).clamp(min=0) * (
238
+ boxes[rest, 3] - boxes[rest, 1]
239
+ ).clamp(min=0)
240
+ iou = inter / (current_area + rest_area - inter).clamp(min=1e-6)
241
+ matched = rest[iou > float(iou_threshold)]
242
+ groups.append(torch.cat((current.reshape(1), matched)))
243
+ order = rest[iou <= float(iou_threshold)]
244
+
245
+ return groups
246
+
247
+
248
+ def _point_sample(input: Any, points: Any, align_corners: bool = False, **kwargs: Any) -> Any:
249
+ import torch.nn.functional as F
250
+
251
+ add_dim = False
252
+ if points.dim() == 3:
253
+ add_dim = True
254
+ points = points.unsqueeze(2)
255
+
256
+ output = F.grid_sample(input, points.mul(2).sub(1), align_corners=align_corners, **kwargs)
257
+ if add_dim:
258
+ output = output.squeeze(3)
259
+ return output
260
+
261
+
262
+ def _rel_roi_point_to_rel_img_point(rois: Any, rel_roi_points: Any, img_shape: Any) -> Any:
263
+ x1, y1, x2, y2 = rois[:, 1], rois[:, 2], rois[:, 3], rois[:, 4]
264
+ roi_w = (x2 - x1).clamp(min=1)
265
+ roi_h = (y2 - y1).clamp(min=1)
266
+ img_h, img_w = img_shape[:2]
267
+ rel_img_points = rel_roi_points.clone()
268
+ rel_img_points[..., 0] = (x1[:, None] + rel_roi_points[..., 0] * roi_w[:, None]) / float(img_w)
269
+ rel_img_points[..., 1] = (y1[:, None] + rel_roi_points[..., 1] * roi_h[:, None]) / float(img_h)
270
+ return rel_img_points
271
+
272
+
273
+ def _sigmoid_focal_loss(
274
+ pred: Any,
275
+ target: Any,
276
+ gamma: float = 2.0,
277
+ alpha: float = 0.25,
278
+ weight: Any = None,
279
+ reduction: str = "mean",
280
+ ) -> Any:
281
+ import torch.nn.functional as F
282
+
283
+ pred_sigmoid = pred.sigmoid()
284
+ target = target.type_as(pred)
285
+ pt = (1 - pred_sigmoid) * target + pred_sigmoid * (1 - target)
286
+ focal_weight = (alpha * target + (1 - alpha) * (1 - target)) * pt.pow(gamma)
287
+ loss = F.binary_cross_entropy_with_logits(pred, target, reduction="none") * focal_weight
288
+ if weight is not None:
289
+ loss = loss * weight
290
+ if reduction == "sum":
291
+ return loss.sum()
292
+ if reduction == "mean":
293
+ return loss.mean()
294
+ return loss
295
+
296
+
297
+ def _ensure_module(module_name: str) -> types.ModuleType:
298
+ module = sys.modules.get(module_name)
299
+ if module is None:
300
+ module = types.ModuleType(module_name)
301
+ sys.modules[module_name] = module
302
+ return module
303
+
304
+
305
  def apply_runtime_patches() -> None:
306
  try:
307
  mmcv = importlib.import_module("mmcv")
 
314
  sys.modules["mmcv.ops"] = ops_module
315
  ops_module.__path__ = []
316
 
317
+ nms_module = _ensure_module("mmcv.ops.nms")
318
+
319
+ roi_align_module = _ensure_module("mmcv.ops.roi_align")
320
+ deform_conv_module = _ensure_module("mmcv.ops.deform_conv")
321
+ modulated_deform_conv_module = _ensure_module("mmcv.ops.modulated_deform_conv")
322
+ carafe_module = _ensure_module("mmcv.ops.carafe")
323
+ merge_cells_module = _ensure_module("mmcv.ops.merge_cells")
324
+ multi_scale_deform_attn_module = _ensure_module("mmcv.ops.multi_scale_deform_attn")
325
+
326
+ deform_conv2d = _unsupported_function("deform_conv2d")
327
+ DeformConv2d = _unsupported_module_class("DeformConv2d")
328
+ ModulatedDeformConv2d = _unsupported_module_class("ModulatedDeformConv2d")
329
+ MaskedConv2d = _unsupported_module_class("MaskedConv2d")
330
+ CornerPool = _unsupported_module_class("CornerPool")
331
+ CARAFEPack = _unsupported_module_class("CARAFEPack")
332
+ GlobalPoolingCell = _unsupported_module_class("GlobalPoolingCell")
333
+ SumCell = _unsupported_module_class("SumCell")
334
+ ConcatCell = _unsupported_module_class("ConcatCell")
335
+ MultiScaleDeformableAttention = _multiscale_deformable_attention_class()
336
 
337
  ops_module.nms = _nms
338
  ops_module.batched_nms = _batched_nms
339
+ ops_module.nms_match = _nms_match
340
+ ops_module.point_sample = _point_sample
341
+ ops_module.rel_roi_point_to_rel_img_point = _rel_roi_point_to_rel_img_point
342
+ ops_module.sigmoid_focal_loss = _sigmoid_focal_loss
343
  ops_module.bbox_overlaps = _bbox_overlaps
344
  ops_module.roi_align = _torchvision_roi_align()
345
  ops_module.RoIAlign = _torchvision_roi_align_module()
346
  ops_module.RoIPool = _torchvision_roi_pool_module()
347
+ ops_module.deform_conv2d = deform_conv2d
348
+ ops_module.DeformConv2d = DeformConv2d
349
+ ops_module.ModulatedDeformConv2d = ModulatedDeformConv2d
350
+ ops_module.MaskedConv2d = MaskedConv2d
351
+ ops_module.CornerPool = CornerPool
352
+ ops_module.CARAFEPack = CARAFEPack
353
+ ops_module.GlobalPoolingCell = GlobalPoolingCell
354
+ ops_module.SumCell = SumCell
355
+ ops_module.ConcatCell = ConcatCell
356
+ ops_module.MultiScaleDeformableAttention = MultiScaleDeformableAttention
357
  nms_module.nms = _nms
358
  nms_module.batched_nms = _batched_nms
359
  roi_align_module.roi_align = ops_module.roi_align
360
  roi_align_module.RoIAlign = ops_module.RoIAlign
361
+ deform_conv_module.deform_conv2d = deform_conv2d
362
+ deform_conv_module.DeformConv2d = DeformConv2d
363
+ modulated_deform_conv_module.ModulatedDeformConv2d = ModulatedDeformConv2d
364
+ carafe_module.CARAFEPack = CARAFEPack
365
+ merge_cells_module.GlobalPoolingCell = GlobalPoolingCell
366
+ merge_cells_module.SumCell = SumCell
367
+ merge_cells_module.ConcatCell = ConcatCell
368
+ multi_scale_deform_attn_module.MultiScaleDeformableAttention = MultiScaleDeformableAttention
369
+
370
+ try:
371
+ transformer_module = importlib.import_module("mmcv.cnn.bricks.transformer")
372
+ if not hasattr(transformer_module, "MultiScaleDeformableAttention"):
373
+ transformer_module.MultiScaleDeformableAttention = MultiScaleDeformableAttention
374
+ except ImportError:
375
+ pass
376
 
377
  if mmcv is not None:
378
  mmcv.ops = ops_module
requirements.txt CHANGED
@@ -9,10 +9,14 @@ pytorch3d==0.7.9+pt2.8.0cu128
9
  opencv-python==4.11.0.86
10
  pillow==11.3.0
11
  matplotlib==3.10.6
 
 
12
  pyyaml==6.0.2
13
  easydict==1.13
14
  h5py==3.15.1
15
  tqdm==4.67.1
 
 
16
  aiofiles==24.1.0
17
  async-lru==2.0.5
18
  timm==1.0.20
 
9
  opencv-python==4.11.0.86
10
  pillow==11.3.0
11
  matplotlib==3.10.6
12
+ scipy==1.15.3
13
+ shapely==2.1.2
14
  pyyaml==6.0.2
15
  easydict==1.13
16
  h5py==3.15.1
17
  tqdm==4.67.1
18
+ six==1.17.0
19
+ terminaltables==3.1.10
20
  aiofiles==24.1.0
21
  async-lru==2.0.5
22
  timm==1.0.20