MCPcopy Create free account
hub / github.com/Project-MONAI/MONAI / box_area

Function box_area

monai/data/box_utils.py:740–779  ·  view source on GitHub ↗

This function computes the area (2D) or volume (3D) of each box. Half precision is not recommended for this function as it may cause overflow, especially for 3D images. Args: boxes: bounding boxes, Nx4 or Nx6 torch tensor or ndarray. The box mode is assumed to be ``StandardMode

(boxes: NdarrayOrTensor)

Source from the content-addressed store, hash-verified

738
739
740def box_area(boxes: NdarrayOrTensor) -> NdarrayOrTensor:
741 """
742 This function computes the area (2D) or volume (3D) of each box.
743 Half precision is not recommended for this function as it may cause overflow, especially for 3D images.
744
745 Args:
746 boxes: bounding boxes, Nx4 or Nx6 torch tensor or ndarray. The box mode is assumed to be ``StandardMode``
747
748 Returns:
749 area (2D) or volume (3D) of boxes, with size of (N,).
750
751 Example:
752 .. code-block:: python
753
754 boxes = torch.ones(10,6)
755 # we do computation with torch.float32 to avoid overflow
756 compute_dtype = torch.float32
757 area = box_area(boxes=boxes.to(dtype=compute_dtype)) # torch.float32, size of (10,)
758 """
759
760 if not is_valid_box_values(boxes):
761 raise ValueError("Given boxes has invalid values. The box size must be non-negative.")
762
763 spatial_dims = get_spatial_dims(boxes=boxes)
764
765 area = boxes[:, spatial_dims] - boxes[:, 0] + TO_REMOVE
766 for axis in range(1, spatial_dims):
767 area = area * (boxes[:, axis + spatial_dims] - boxes[:, axis] + TO_REMOVE)
768
769 # convert numpy to tensor if needed
770 area_t, *_ = convert_data_type(area, torch.Tensor)
771
772 # check if NaN or Inf, especially for half precision
773 if area_t.isnan().any() or area_t.isinf().any():
774 if area_t.dtype is torch.float16:
775 raise ValueError("Box area is NaN or Inf. boxes is float16. Please change to float32 and test it again.")
776 else:
777 raise ValueError("Box area is NaN or Inf.")
778
779 return area
780
781
782def _box_inter_union(

Callers 3

test_valueMethod · 0.90
_box_inter_unionFunction · 0.85
box_pair_giouFunction · 0.85

Calls 3

convert_data_typeFunction · 0.90
is_valid_box_valuesFunction · 0.85
get_spatial_dimsFunction · 0.85

Tested by 1

test_valueMethod · 0.72

Used in the wild real call sites across dependent graphs

searching dependent graphs…