Skip to content
Open
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
4 changes: 2 additions & 2 deletions monai/data/box_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -1035,8 +1035,8 @@ def spatial_crop_boxes(
# convert to float32 since torch.clamp_ does not support float16
boxes_t = boxes_t.to(dtype=COMPUTE_DTYPE)

roi_start_t = convert_to_dst_type(src=roi_start, dst=boxes_t, wrap_sequence=True)[0].to(torch.int16)
roi_end_t = convert_to_dst_type(src=roi_end, dst=boxes_t, wrap_sequence=True)[0].to(torch.int16)
roi_start_t = convert_to_dst_type(src=roi_start, dst=boxes_t, wrap_sequence=True)[0]
roi_end_t = convert_to_dst_type(src=roi_end, dst=boxes_t, wrap_sequence=True)[0]
roi_end_t = torch.maximum(roi_end_t, roi_start_t)

# makes sure the bounding boxes are within the patch
Expand Down
15 changes: 15 additions & 0 deletions tests/data/test_box_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
convert_box_mode,
convert_box_to_standard_mode,
non_max_suppression,
spatial_crop_boxes,
)
from monai.utils.type_conversion import convert_data_type
from tests.test_utils import TEST_NDARRAYS, assert_allclose
Expand Down Expand Up @@ -269,6 +270,20 @@ def test_integer_truncation_bug(self):
self.assertTrue(np.issubdtype(iou.dtype, np.floating))
self.assertGreater(iou[0, 0], 0.0, "IoU should not be truncated to 0")

def test_large_coordinates_are_not_dropped(self):
"""Verify large-coordinate boxes are preserved by cropping and clipping."""
boxes = torch.tensor([[41000.0, 5000.0, 45000.0, 15000.0]], dtype=torch.float32)

cropped_boxes, keep = spatial_crop_boxes(
boxes=boxes, roi_start=[40000, 0], roi_end=[50000, 20000], remove_empty=True
)
assert_allclose(keep, torch.tensor([True]))
assert_allclose(cropped_boxes, torch.tensor([[1000.0, 5000.0, 5000.0, 15000.0]]))

clipped_boxes, keep = clip_boxes_to_image(boxes=boxes, spatial_size=[50000, 50000], remove_empty=True)
assert_allclose(keep, torch.tensor([True]))
assert_allclose(clipped_boxes, boxes)


class TestBatchedNms(unittest.TestCase):
@parameterized.expand(TEST_NDARRAYS)
Expand Down
Loading