π Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
fix(geometry): support non-contiguous boxes in compute_area (#5468)
NIKHILbae7636f1126
diff --git a/changelog.d/5468.fixed.md b/changelog.d/5468.fixed.mdnew file mode 100644index 000000000..ccefb1dcc--- /dev/null+++ b/changelog.d/5468.fixed.md@@ -0,0 +1 @@+`Boxes.compute_area`, `Boxes.transform_boxes` and `Boxes.translate` (and `Boxes3D.transform_boxes`) now accept non-contiguous box tensors, such as a `(B, N, 4, 2)` tensor made by transposing the batch and box axes. They used to raise `RuntimeError: view size is not compatible with input tensor's size and stride`; `compute_area` now returns the documented `(B, N)` areas.diff --git a/kornia/geometry/boxes.py b/kornia/geometry/boxes.pyindex 42c8690d2..77761ffc0 100644--- a/kornia/geometry/boxes.py+++ b/kornia/geometry/boxes.py@@ -65,7 +65,7 @@ def _transform_boxes(boxes: torch.Tensor, M: torch.Tensor) -> torch.Tensor:boxes_per_batch, n_points_per_box, coordinates_dimension = boxes.shape[-3:]if boxes_per_batch == 0:return boxes- points = boxes.view(-1, n_points_per_box * boxes_per_batch, coordinates_dimension)+ points = boxes.reshape(-1, n_points_per_box * boxes_per_batch, coordinates_dimension)M = M if M.ndim == 3 else M.unsqueeze(0)if points.shape[0] != M.shape[0]:@@ -641,7 +641,7 @@ class Boxes:Returns:Area for each box, shaped :math:`(N,)` or :math:`(B, N)`."""- coords = self._data.view((-1, 4, 2)) if self._data.ndim == 4 else self._data+ coords = self._data.flatten(0, 1) if self._data.ndim == 4 else self._data# calculate centroid of the boxcentroid = coords.mean(dim=1, keepdim=True)# calculate the angle from centroid to each cornerdiff --git a/tests/geometry/test_boxes.py b/tests/geometry/test_boxes.pyindex 6c0546b3b..cf79813d0 100644--- a/tests/geometry/test_boxes.py+++ b/tests/geometry/test_boxes.py@@ -793,6 +793,40 @@ class TestBoxes2D(BaseTester):for computed_area, expected_area in zip(flattened_computed_areas_w_batch, expected_values))+ def test_compute_area_supports_non_contiguous_batched_boxes(self, device, dtype):+ boxes_n_b = torch.tensor(+ [+ [+ [[0.0, 0.0], [4.0, 0.0], [4.0, 3.0], [0.0, 3.0]],+ [[10.0, 10.0], [12.0, 10.0], [12.0, 12.0], [10.0, 12.0]],+ ],+ [+ [[0.0, 0.0], [2.0, 0.0], [2.0, 3.0], [0.0, 3.0]],+ [[1.0, 1.0], [6.0, 1.0], [6.0, 7.0], [1.0, 7.0]],+ ],+ [+ [[0.0, 0.0], [3.0, 0.0], [3.0, 4.0], [0.0, 4.0]],+ [[2.0, 2.0], [5.0, 2.0], [5.0, 6.0], [2.0, 6.0]],+ ],+ ],+ device=device,+ dtype=dtype,+ )+ boxes_b_n = boxes_n_b.transpose(0, 1)+ assert boxes_b_n.shape == (2, 3, 4, 2)+ assert not boxes_b_n.is_contiguous()++ area = Boxes(boxes_b_n).compute_area()++ expected = torch.tensor([[12.0, 6.0, 12.0], [4.0, 30.0, 12.0]], device=device, dtype=dtype)+ assert torch.equal(area, expected)++ # transform_boxes (and translate, which calls it) flattened the same layout with .view too.+ shift = torch.tensor([[1.0, 2.0], [3.0, 4.0]], device=device, dtype=dtype)+ translated = Boxes(boxes_b_n).translate(shift).data+ expected_translated = boxes_b_n.contiguous() + shift[:, None, None, :]+ assert torch.equal(translated, expected_translated)+def test_wart_compute_area_is_shoelace_of_inclusive_vertices_4010(self, device, dtype):# kornia#4010: compute_area applies shoelace to the stored inclusive vertices, so a valid exclusive# 2-by-1 box collapses to a line and a raw four-by-three rectangle has area six, not the twelve that