Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add box non_max_suppression #4337

Merged
merged 7 commits into from
May 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 1 addition & 33 deletions docs/source/data.rst
Original file line number Diff line number Diff line change
Expand Up @@ -314,37 +314,5 @@ PatchWSIDataset

Bounding box
------------

Box mode
~~~~~~~~
.. autoclass:: monai.data.box_utils.BoxMode
.. automodule:: monai.data.box_utils
:members:
.. autoclass:: monai.data.box_utils.CornerCornerModeTypeA
.. autoclass:: monai.data.box_utils.CornerCornerModeTypeB
.. autoclass:: monai.data.box_utils.CornerCornerModeTypeC
.. autoclass:: monai.data.box_utils.CornerSizeMode
.. autoclass:: monai.data.box_utils.CenterSizeMode

Box mode converter
~~~~~~~~~~~~~~~~~~
.. autofunction:: monai.data.box_utils.get_boxmode
.. autofunction:: monai.data.box_utils.convert_box_mode
.. autofunction:: monai.data.box_utils.convert_box_to_standard_mode

Box IoU
~~~~~~~
.. autofunction:: monai.data.box_utils.box_area
.. autofunction:: monai.data.box_utils.box_iou
.. autofunction:: monai.data.box_utils.box_giou
.. autofunction:: monai.data.box_utils.box_pair_giou

Box center
~~~~~~~~~~
.. autofunction:: monai.data.box_utils.box_centers
.. autofunction:: monai.data.box_utils.centers_in_boxes
.. autofunction:: monai.data.box_utils.boxes_center_distance

Spatial crop box
~~~~~~~~~~~~~~~~
.. autofunction:: monai.data.box_utils.spatial_crop_boxes
.. autofunction:: monai.data.box_utils.clip_boxes_to_image
4 changes: 2 additions & 2 deletions monai/apps/detection/transforms/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ def __call__(self, boxes: NdarrayOrTensor) -> NdarrayOrTensor:

class AffineBox(Transform):
"""
Applys affine matrix to the boxes
Applies affine matrix to the boxes
"""

backend = [TransformBackends.TORCH, TransformBackends.NUMPY]
Expand All @@ -147,7 +147,7 @@ def __call__(self, boxes: NdarrayOrTensor, affine: Union[NdarrayOrTensor, None])
"""
Args:
boxes: source bounding boxes, Nx4 or Nx6 torch tensor or ndarray. The box mode is assumed to be ``StandardMode``
affine: affine matric to be applied to the box coordinate
affine: affine matrix to be applied to the box coordinate
"""
if affine is None:
return boxes
Expand Down
9 changes: 4 additions & 5 deletions monai/apps/detection/transforms/box_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@
import torch

from monai.config.type_definitions import NdarrayOrTensor
from monai.data.box_utils import TO_REMOVE, get_spatial_dims
from monai.data.box_utils import COMPUTE_DTYPE, TO_REMOVE, get_spatial_dims
from monai.transforms.utils import create_scale
from monai.utils.misc import ensure_tuple, ensure_tuple_rep
from monai.utils.type_conversion import convert_data_type, convert_to_dst_type


def _apply_affine_to_points(points: torch.Tensor, affine: torch.Tensor, include_shift: bool = True) -> torch.Tensor:
"""
This internal function applies affine matrixs to the point coordinate
This internal function applies affine matrices to the point coordinate

Args:
points: point coordinates, Nx2 or Nx3 torch tensor or ndarray, representing [x, y] or [x, y, z]
Expand Down Expand Up @@ -56,7 +56,7 @@ def _apply_affine_to_points(points: torch.Tensor, affine: torch.Tensor, include_

def apply_affine_to_boxes(boxes: NdarrayOrTensor, affine: NdarrayOrTensor) -> NdarrayOrTensor:
"""
This function applies affine matrixs to the boxes
This function applies affine matrices to the boxes

Args:
boxes: bounding boxes, Nx4 or Nx6 torch tensor or ndarray. The box mode is assumed to be StandardMode
Expand All @@ -71,9 +71,8 @@ def apply_affine_to_boxes(boxes: NdarrayOrTensor, affine: NdarrayOrTensor) -> Nd

# some operation does not support torch.float16
# convert to float32
compute_dtype = torch.float32

boxes_t = boxes_t.to(dtype=compute_dtype)
boxes_t = boxes_t.to(dtype=COMPUTE_DTYPE)
affine_t, *_ = convert_to_dst_type(src=affine, dst=boxes_t)

spatial_dims = get_spatial_dims(boxes=boxes_t)
Expand Down
18 changes: 9 additions & 9 deletions monai/apps/detection/transforms/dictionary.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,20 +176,20 @@ def inverse(self, data: Mapping[Hashable, NdarrayOrTensor]) -> Dict[Hashable, Nd

class AffineBoxToImageCoordinated(MapTransform, InvertibleTransform):
"""
Dictionary-based transfrom that converts box in world coordinate to image coordinate.
Dictionary-based transform that converts box in world coordinate to image coordinate.

Args:
box_keys: Keys to pick box data for transformation. The box mode is assumed to be ``StandardMode``.
box_ref_image_keys: The single key that represents the reference image to which ``box_keys`` are attached.
remove_empty: whether to remove the boxes that are actually empty
allow_missing_keys: don't raise exception if key is missing.
image_meta_key: explicitly indicate the key of the corresponding meta data dictionary.
image_meta_key: explicitly indicate the key of the corresponding metadata dictionary.
for example, for data with key `image`, the metadata by default is in `image_meta_dict`.
the meta data is a dictionary object which contains: filename, affine, original_shape, etc.
the metadata is a dictionary object which contains: filename, affine, original_shape, etc.
it is a string, map to the `box_ref_image_key`.
if None, will try to construct meta_keys by `box_ref_image_key_{meta_key_postfix}`.
image_meta_key_postfix: if image_meta_keys=None, use `box_ref_image_key_{postfix}` to fetch the meta data according
to the key data, default is `meta_dict`, the meta data is a dictionary object.
image_meta_key_postfix: if image_meta_keys=None, use `box_ref_image_key_{postfix}` to fetch the metadata according
to the key data, default is `meta_dict`, the metadata is a dictionary object.
For example, to handle key `image`, read/write affine matrices from the
metadata `image_meta_dict` dictionary's `affine` field.
affine_lps_to_ras: default ``False``. Yet if 1) the image is read by ITKReader,
Expand Down Expand Up @@ -255,7 +255,7 @@ def inverse(self, data: Mapping[Hashable, NdarrayOrTensor]) -> Dict[Hashable, Nd

class ZoomBoxd(MapTransform, InvertibleTransform):
"""
Dictionary-based transfrom that zooms input boxes and images with the given zoom scale.
Dictionary-based transform that zooms input boxes and images with the given zoom scale.

Args:
image_keys: Keys to pick image data for transformation.
Expand Down Expand Up @@ -384,7 +384,7 @@ def inverse(self, data: Mapping[Hashable, NdarrayOrTensor]) -> Dict[Hashable, Nd

class RandZoomBoxd(RandomizableTransform, MapTransform, InvertibleTransform):
"""
Dictionary-based transfrom that randomly zooms input boxes and images with given probability within given zoom range.
Dictionary-based transform that randomly zooms input boxes and images with given probability within given zoom range.

Args:
image_keys: Keys to pick image data for transformation.
Expand Down Expand Up @@ -547,7 +547,7 @@ def inverse(self, data: Mapping[Hashable, NdarrayOrTensor]) -> Dict[Hashable, Nd

class FlipBoxd(MapTransform, InvertibleTransform):
"""
Dictionary-based transfrom that flip boxes and images.
Dictionary-based transform that flip boxes and images.

Args:
image_keys: Keys to pick image data for transformation.
Expand Down Expand Up @@ -611,7 +611,7 @@ def inverse(self, data: Mapping[Hashable, NdarrayOrTensor]) -> Dict[Hashable, Nd

class RandFlipBoxd(RandomizableTransform, MapTransform, InvertibleTransform):
"""
Dictionary-based transfrom that randomly flip boxes and images with the given probabilities.
Dictionary-based transform that randomly flip boxes and images with the given probabilities.

Args:
image_keys: Keys to pick image data for transformation.
Expand Down
Loading