original_docstring: Calculate overlap between two set of axis aligned 3D bboxes. If
      ``is_aligned`` is ``False``, then calculate the overlaps between each bbox
      of bboxes1 and bboxes2, otherwise the overlaps between each aligned pair of
      bboxes1 and bboxes2.
  
      Args:
          bboxes1 (Tensor): shape (B, m, 6) in <x1, y1, z1, x2, y2, z2>
              format or empty.
          bboxes2 (Tensor): shape (B, n, 6) in <x1, y1, z1, x2, y2, z2>
              format or empty.
              B indicates the batch dim, in shape (B1, B2, ..., Bn).
              If ``is_aligned`` is ``True``, then m and n must be equal.
          mode (str): "iou" (intersection over union) or "giou" (generalized
              intersection over union).
          is_aligned (bool, optional): If True, then m and n must be equal.
              Default False.
          eps (float, optional): A value added to the denominator for numerical
              stability. Default 1e-6.
  
      Returns:
          Tensor: shape (m, n) if ``is_aligned`` is False else shape (m,)
  
      Example:
          >>> bboxes1 = torch.FloatTensor([
          >>>     [0, 0, 0, 10, 10, 10],
          >>>     [10, 10, 10, 20, 20, 20],
          >>>     [32, 32, 32, 38, 40, 42],
          >>> ])
          >>> bboxes2 = torch.FloatTensor([
          >>>     [0, 0, 0, 10, 20, 20],
          >>>     [0, 10, 10, 10, 19, 20],
          >>>     [10, 10, 10, 20, 20, 20],
          >>> ])
          >>> overlaps = axis_aligned_bbox_overlaps_3d(bboxes1, bboxes2)
          >>> assert overlaps.shape == (3, 3)
          >>> overlaps = bbox_overlaps(bboxes1, bboxes2, is_aligned=True)
          >>> assert overlaps.shape == (3, )
      Example:
          >>> empty = torch.empty(0, 6)
          >>> nonempty = torch.FloatTensor([[0, 0, 0, 10, 9, 10]])
          >>> assert tuple(bbox_overlaps(empty, nonempty).shape) == (0, 1)
          >>> assert tuple(bbox_overlaps(nonempty, empty).shape) == (1, 0)
          >>> assert tuple(bbox_overlaps(empty, empty).shape) == (0, 0)
      
docstring: Calculate overlap between two set of axis aligned 3D bboxes.
docstring_tokens: Calculate overlap between two set of axis aligned 3D bboxes .
code: def axis_aligned_bbox_overlaps_3d(bboxes1,
                                    bboxes2,
                                    mode='iou',
                                    is_aligned=False,
                                    eps=1e-6):
      """Calculate overlap between two set of axis aligned 3D bboxes. If
      ``is_aligned`` is ``False``, then calculate the overlaps between each bbox
      of bboxes1 and bboxes2, otherwise the overlaps between each aligned pair of
      bboxes1 and bboxes2.
  
      Args:
          bboxes1 (Tensor): shape (B, m, 6) in <x1, y1, z1, x2, y2, z2>
              format or empty.
          bboxes2 (Tensor): shape (B, n, 6) in <x1, y1, z1, x2, y2, z2>
              format or empty.
              B indicates the batch dim, in shape (B1, B2, ..., Bn).
              If ``is_aligned`` is ``True``, then m and n must be equal.
          mode (str): "iou" (intersection over union) or "giou" (generalized
              intersection over union).
          is_aligned (bool, optional): If True, then m and n must be equal.
              Default False.
          eps (float, optional): A value added to the denominator for numerical
              stability. Default 1e-6.
  
      Returns:
          Tensor: shape (m, n) if ``is_aligned`` is False else shape (m,)
  
      Example:
          >>> bboxes1 = torch.FloatTensor([
          >>>     [0, 0, 0, 10, 10, 10],
          >>>     [10, 10, 10, 20, 20, 20],
          >>>     [32, 32, 32, 38, 40, 42],
          >>> ])
          >>> bboxes2 = torch.FloatTensor([
          >>>     [0, 0, 0, 10, 20, 20],
          >>>     [0, 10, 10, 10, 19, 20],
          >>>     [10, 10, 10, 20, 20, 20],
          >>> ])
          >>> overlaps = axis_aligned_bbox_overlaps_3d(bboxes1, bboxes2)
          >>> assert overlaps.shape == (3, 3)
          >>> overlaps = bbox_overlaps(bboxes1, bboxes2, is_aligned=True)
          >>> assert overlaps.shape == (3, )
      Example:
          >>> empty = torch.empty(0, 6)
          >>> nonempty = torch.FloatTensor([[0, 0, 0, 10, 9, 10]])
          >>> assert tuple(bbox_overlaps(empty, nonempty).shape) == (0, 1)
          >>> assert tuple(bbox_overlaps(nonempty, empty).shape) == (1, 0)
          >>> assert tuple(bbox_overlaps(empty, empty).shape) == (0, 0)
      """
  
      assert mode in ['iou', 'giou'], f'Unsupported mode {mode}'
      # Either the boxes are empty or the length of boxes's last dimenstion is 6
      assert (bboxes1.size(-1) == 6 or bboxes1.size(0) == 0)
      assert (bboxes2.size(-1) == 6 or bboxes2.size(0) == 0)
  
      # Batch dim must be the same
      # Batch dim: (B1, B2, ... Bn)
      assert bboxes1.shape[:-2] == bboxes2.shape[:-2]
      batch_shape = bboxes1.shape[:-2]
  
      rows = bboxes1.size(-2)
      cols = bboxes2.size(-2)
      if is_aligned:
          assert rows == cols
  
      if rows * cols == 0:
          if is_aligned:
              return bboxes1.new(batch_shape + (rows, ))
          else:
              return bboxes1.new(batch_shape + (rows, cols))
  
      area1 = (bboxes1[..., 3] -
               bboxes1[..., 0]) * (bboxes1[..., 4] - bboxes1[..., 1]) * (
                   bboxes1[..., 5] - bboxes1[..., 2])
      area2 = (bboxes2[..., 3] -
               bboxes2[..., 0]) * (bboxes2[..., 4] - bboxes2[..., 1]) * (
                   bboxes2[..., 5] - bboxes2[..., 2])
  
      if is_aligned:
          lt = torch.max(bboxes1[..., :3], bboxes2[..., :3])  # [B, rows, 3]
          rb = torch.min(bboxes1[..., 3:], bboxes2[..., 3:])  # [B, rows, 3]
  
          wh = (rb - lt).clamp(min=0)  # [B, rows, 2]
          overlap = wh[..., 0] * wh[..., 1] * wh[..., 2]
  
          if mode in ['iou', 'giou']:
              union = area1 + area2 - overlap
          else:
              union = area1
          if mode == 'giou':
              enclosed_lt = torch.min(bboxes1[..., :3], bboxes2[..., :3])
              enclosed_rb = torch.max(bboxes1[..., 3:], bboxes2[..., 3:])
      else:
          lt = torch.max(bboxes1[..., :, None, :3],
                         bboxes2[..., None, :, :3])  # [B, rows, cols, 3]
          rb = torch.min(bboxes1[..., :, None, 3:],
                         bboxes2[..., None, :, 3:])  # [B, rows, cols, 3]
  
          wh = (rb - lt).clamp(min=0)  # [B, rows, cols, 3]
          overlap = wh[..., 0] * wh[..., 1] * wh[..., 2]
  
          if mode in ['iou', 'giou']:
              union = area1[..., None] + area2[..., None, :] - overlap
          if mode == 'giou':
              enclosed_lt = torch.min(bboxes1[..., :, None, :3],
                                      bboxes2[..., None, :, :3])
              enclosed_rb = torch.max(bboxes1[..., :, None, 3:],
                                      bboxes2[..., None, :, 3:])
  
      eps = union.new_tensor([eps])
      union = torch.max(union, eps)
      ious = overlap / union
      if mode in ['iou']:
          return ious
      # calculate gious
      enclose_wh = (enclosed_rb - enclosed_lt).clamp(min=0)
      enclose_area = enclose_wh[..., 0] * enclose_wh[..., 1] * enclose_wh[..., 2]
      enclose_area = torch.max(enclose_area, eps)
      gious = ious - (enclose_area - union) / enclose_area
      return gious
code_tokens: def axis_aligned_bbox_overlaps_3d ( bboxes1 , bboxes2 , mode = 'iou' , is_aligned = False , eps = 1e-6 ) : assert mode in [ 'iou' , 'giou' ] , f'Unsupported mode {mode}' assert ( bboxes1 . size ( - 1 ) == 6 or bboxes1 . size ( 0 ) == 0 ) assert ( bboxes2 . size ( - 1 ) == 6 or bboxes2 . size ( 0 ) == 0 ) assert bboxes1 . shape [ : - 2 ] == bboxes2 . shape [ : - 2 ] batch_shape = bboxes1 . shape [ : - 2 ] rows = bboxes1 . size ( - 2 ) cols = bboxes2 . size ( - 2 ) if is_aligned : assert rows == cols if rows * cols == 0 : if is_aligned : return bboxes1 . new ( batch_shape + ( rows , ) ) else : return bboxes1 . new ( batch_shape + ( rows , cols ) ) area1 = ( bboxes1 [ ... , 3 ] - bboxes1 [ ... , 0 ] ) * ( bboxes1 [ ... , 4 ] - bboxes1 [ ... , 1 ] ) * ( bboxes1 [ ... , 5 ] - bboxes1 [ ... , 2 ] ) area2 = ( bboxes2 [ ... , 3 ] - bboxes2 [ ... , 0 ] ) * ( bboxes2 [ ... , 4 ] - bboxes2 [ ... , 1 ] ) * ( bboxes2 [ ... , 5 ] - bboxes2 [ ... , 2 ] ) if is_aligned : lt = torch . max ( bboxes1 [ ... , : 3 ] , bboxes2 [ ... , : 3 ] ) rb = torch . min ( bboxes1 [ ... , 3 : ] , bboxes2 [ ... , 3 : ] ) wh = ( rb - lt ) . clamp ( min = 0 ) overlap = wh [ ... , 0 ] * wh [ ... , 1 ] * wh [ ... , 2 ] if mode in [ 'iou' , 'giou' ] : union = area1 + area2 - overlap else : union = area1 if mode == 'giou' : enclosed_lt = torch . min ( bboxes1 [ ... , : 3 ] , bboxes2 [ ... , : 3 ] ) enclosed_rb = torch . max ( bboxes1 [ ... , 3 : ] , bboxes2 [ ... , 3 : ] ) else : lt = torch . max ( bboxes1 [ ... , : , None , : 3 ] , bboxes2 [ ... , None , : , : 3 ] ) rb = torch . min ( bboxes1 [ ... , : , None , 3 : ] , bboxes2 [ ... , None , : , 3 : ] ) wh = ( rb - lt ) . clamp ( min = 0 ) overlap = wh [ ... , 0 ] * wh [ ... , 1 ] * wh [ ... , 2 ] if mode in [ 'iou' , 'giou' ] : union = area1 [ ... , None ] + area2 [ ... , None , : ] - overlap if mode == 'giou' : enclosed_lt = torch . min ( bboxes1 [ ... , : , None , : 3 ] , bboxes2 [ ... , None , : , : 3 ] ) enclosed_rb = torch . max ( bboxes1 [ ... , : , None , 3 : ] , bboxes2 [ ... , None , : , 3 : ] ) eps = union . new_tensor ( [ eps ] ) union = torch . max ( union , eps ) ious = overlap / union if mode in [ 'iou' ] : return ious enclose_wh = ( enclosed_rb - enclosed_lt ) . clamp ( min = 0 ) enclose_area = enclose_wh [ ... , 0 ] * enclose_wh [ ... , 1 ] * enclose_wh [ ... , 2 ] enclose_area = torch . max ( enclose_area , eps ) gious = ious - ( enclose_area - union ) / enclose_area return gious
short_docstring: Calculate overlap between two set of axis aligned 3D bboxes.
short_docstring_tokens: Calculate overlap between two set of axis aligned 3D bboxes .
comment: """Calculate overlap between two set of axis aligned 3D bboxes. If
      ``is_aligned`` is ``False``, then calculate the overlaps between each bbox
      of bboxes1 and bboxes2, otherwise the overlaps between each aligned pair of
      bboxes1 and bboxes2.
  
      Args:
          bboxes1 (Tensor): shape (B, m, 6) in <x1, y1, z1, x2, y2, z2>
              format or empty.
          bboxes2 (Tensor): shape (B, n, 6) in <x1, y1, z1, x2, y2, z2>
              format or empty.
              B indicates the batch dim, in shape (B1, B2, ..., Bn).
              If ``is_aligned`` is ``True``, then m and n must be equal.
          mode (str): "iou" (intersection over union) or "giou" (generalized
              intersection over union).
          is_aligned (bool, optional): If True, then m and n must be equal.
              Default False.
          eps (float, optional): A value added to the denominator for numerical
              stability. Default 1e-6.
  
      Returns:
          Tensor: shape (m, n) if ``is_aligned`` is False else shape (m,)
  
      Example:
          >>> bboxes1 = torch.FloatTensor([
          >>>     [0, 0, 0, 10, 10, 10],
          >>>     [10, 10, 10, 20, 20, 20],
          >>>     [32, 32, 32, 38, 40, 42],
          >>> ])
          >>> bboxes2 = torch.FloatTensor([
          >>>     [0, 0, 0, 10, 20, 20],
          >>>     [0, 10, 10, 10, 19, 20],
          >>>     [10, 10, 10, 20, 20, 20],
          >>> ])
          >>> overlaps = axis_aligned_bbox_overlaps_3d(bboxes1, bboxes2)
          >>> assert overlaps.shape == (3, 3)
          >>> overlaps = bbox_overlaps(bboxes1, bboxes2, is_aligned=True)
          >>> assert overlaps.shape == (3, )
      Example:
          >>> empty = torch.empty(0, 6)
          >>> nonempty = torch.FloatTensor([[0, 0, 0, 10, 9, 10]])
          >>> assert tuple(bbox_overlaps(empty, nonempty).shape) == (0, 1)
          >>> assert tuple(bbox_overlaps(nonempty, empty).shape) == (1, 0)
          >>> assert tuple(bbox_overlaps(empty, empty).shape) == (0, 0)
      """$SEP$# Either the boxes are empty or the length of boxes's last dimenstion is 6$SEP$# Batch dim must be the same$SEP$# Batch dim: (B1, B2, ... Bn)$SEP$# [B, rows, 3]$SEP$# [B, rows, 3]$SEP$# [B, rows, 2]$SEP$# [B, rows, cols, 3]$SEP$# [B, rows, cols, 3]$SEP$# [B, rows, cols, 3]$SEP$# calculate gious