π Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
Adds support for multiple bounding box formats (xywh and cxcywh) to the mean_iou_bbox function, addressing the limitation mentioned in issue #3529. (#3537)
Arjit Rawat7a2e8230e5de
diff --git a/kornia/metrics/mean_iou.py b/kornia/metrics/mean_iou.pyindex e97b21ed2..f865726f8 100644--- a/kornia/metrics/mean_iou.py+++ b/kornia/metrics/mean_iou.py@@ -75,44 +75,98 @@ def mean_iou(pred: torch.Tensor, target: torch.Tensor, num_classes: int, eps: flreturn ious-def mean_iou_bbox(boxes_1: torch.Tensor, boxes_2: torch.Tensor) -> torch.Tensor:- """Compute the IoU of the cartesian product of two sets of boxes.+def _convert_boxes_to_xyxy(boxes: torch.Tensor, box_format: str) -> torch.Tensor:+ """Convert bounding boxes from various formats to xyxy format.++ Args:+ boxes: tensor of bounding boxes in shape (N, 4).+ box_format: box format - one of 'xyxy', 'xywh', or 'cxcywh'.- Each box in each set shall be (x1, y1, x2, y2).+ Returns:+ boxes in xyxy format (x1, y1, x2, y2).+ """+ if box_format == "xyxy":+ return boxes+ elif box_format == "xywh":+ # (x, y, w, h) -> (x1, y1, x2, y2)+ x, y, w, h = boxes[:, 0:1], boxes[:, 1:2], boxes[:, 2:3], boxes[:, 3:4]+ x2 = x + w+ y2 = y + h+ return torch.cat([x, y, x2, y2], dim=1)+ elif box_format == "cxcywh":+ # (cx, cy, w, h) -> (x1, y1, x2, y2)+ cx, cy, w, h = boxes[:, 0:1], boxes[:, 1:2], boxes[:, 2:3], boxes[:, 3:4]+ x1 = cx - w / 2+ y1 = cy - h / 2+ x2 = cx + w / 2+ y2 = cy + h / 2+ return torch.cat([x1, y1, x2, y2], dim=1)+ else:+ raise ValueError(f"Unsupported box format: {box_format}. Must be one of 'xyxy', 'xywh', or 'cxcywh'.")+++def mean_iou_bbox(boxes_1: torch.Tensor, boxes_2: torch.Tensor, box_format: str = "xyxy") -> torch.Tensor:+ """Compute the IoU of the cartesian product of two sets of boxes.Args:boxes_1: a tensor of bounding boxes in :math:`(B1, 4)`.boxes_2: a tensor of bounding boxes in :math:`(B2, 4)`.+ box_format: the bounding box format. Supported formats are:+ - 'xyxy': (x1, y1, x2, y2) where (x1, y1) is top-left and (x2, y2) is bottom-right+ - 'xywh': (x, y, w, h) where (x, y) is top-left, w is width, h is height+ - 'cxcywh': (cx, cy, w, h) where (cx, cy) is center, w is width, h is height+ Default: 'xyxy'.Returns:a tensor in dimensions :math:`(B1, B2)`, representing theintersection of each of the boxes in set 1 with respect to each of the boxes in set 2.Example:+ >>> # XYXY format>>> boxes_1 = torch.tensor([[40, 40, 60, 60], [30, 40, 50, 60]])>>> boxes_2 = torch.tensor([[40, 50, 60, 70], [30, 40, 40, 50]])>>> mean_iou_bbox(boxes_1, boxes_2)tensor([[0.3333, 0.0000],[0.1429, 0.2500]])+ >>> # XYWH format+ >>> boxes_1_xywh = torch.tensor([[40, 40, 20, 20], [30, 40, 20, 20]])+ >>> boxes_2_xywh = torch.tensor([[40, 50, 20, 20], [30, 40, 10, 10]])+ >>> mean_iou_bbox(boxes_1_xywh, boxes_2_xywh, box_format='xywh')+ tensor([[0.3333, 0.0000],+ [0.1429, 0.2500]])+ >>> # CXCYWH format+ >>> boxes_1_cxcywh = torch.tensor([[50, 50, 20, 20], [40, 50, 20, 20]])+ >>> boxes_2_cxcywh = torch.tensor([[50, 60, 20, 20], [35, 45, 10, 10]])+ >>> mean_iou_bbox(boxes_1_cxcywh, boxes_2_cxcywh, box_format='cxcywh')+ tensor([[0.3333, 0.0000],+ [0.1429, 0.2500]])"""- # TODO: support more box types. e.g. xywh,- if not (((boxes_1[:, 2] - boxes_1[:, 0]) > 0).all() or ((boxes_1[:, 3] - boxes_1[:, 1]) > 0).all()):- raise AssertionError("Boxes_1 does not follow (x1, y1, x2, y2) format.")- if not (((boxes_2[:, 2] - boxes_2[:, 0]) > 0).all() or ((boxes_2[:, 3] - boxes_2[:, 1]) > 0).all()):- raise AssertionError("Boxes_2 does not follow (x1, y1, x2, y2) format.")- # find intersection- lower_bounds = torch.max(boxes_1[:, :2].unsqueeze(1), boxes_2[:, :2].unsqueeze(0)) # (n1, n2, 2)- upper_bounds = torch.min(boxes_1[:, 2:].unsqueeze(1), boxes_2[:, 2:].unsqueeze(0)) # (n1, n2, 2)+ # Convert boxes to xyxy format+ boxes_1_xyxy = _convert_boxes_to_xyxy(boxes_1, box_format)+ boxes_2_xyxy = _convert_boxes_to_xyxy(boxes_2, box_format)++ # Validate boxes are in proper xyxy format+ if not (+ ((boxes_1_xyxy[:, 2] - boxes_1_xyxy[:, 0]) > 0).all() and ((boxes_1_xyxy[:, 3] - boxes_1_xyxy[:, 1]) > 0).all()+ ):+ raise AssertionError("Boxes_1 contains invalid boxes after conversion.")+ if not (+ ((boxes_2_xyxy[:, 2] - boxes_2_xyxy[:, 0]) > 0).all() and ((boxes_2_xyxy[:, 3] - boxes_2_xyxy[:, 1]) > 0).all()+ ):+ raise AssertionError("Boxes_2 contains invalid boxes after conversion.")++ # Find intersection+ lower_bounds = torch.max(boxes_1_xyxy[:, :2].unsqueeze(1), boxes_2_xyxy[:, :2].unsqueeze(0)) # (n1, n2, 2)+ upper_bounds = torch.min(boxes_1_xyxy[:, 2:].unsqueeze(1), boxes_2_xyxy[:, 2:].unsqueeze(0)) # (n1, n2, 2)intersection_dims = torch.clamp(upper_bounds - lower_bounds, min=0) # (n1, n2, 2)intersection = intersection_dims[:, :, 0] * intersection_dims[:, :, 1] # (n1, n2)# Find areas of each box in both sets- areas_set_1 = (boxes_1[:, 2] - boxes_1[:, 0]) * (boxes_1[:, 3] - boxes_1[:, 1]) # (n1)- areas_set_2 = (boxes_2[:, 2] - boxes_2[:, 0]) * (boxes_2[:, 3] - boxes_2[:, 1]) # (n2)+ areas_set_1 = (boxes_1_xyxy[:, 2] - boxes_1_xyxy[:, 0]) * (boxes_1_xyxy[:, 3] - boxes_1_xyxy[:, 1]) # (n1)+ areas_set_2 = (boxes_2_xyxy[:, 2] - boxes_2_xyxy[:, 0]) * (boxes_2_xyxy[:, 3] - boxes_2_xyxy[:, 1]) # (n2)# Find the union- # PyTorch auto-broadcasts singleton dimensionsunion = areas_set_1.unsqueeze(1) + areas_set_2.unsqueeze(0) - intersection # (n1, n2)return intersection / union # (n1, n2)diff --git a/tests/metrics/test_mean_iou.py b/tests/metrics/test_mean_iou.pyindex 77a1f39c3..8e1abad48 100644--- a/tests/metrics/test_mean_iou.py+++ b/tests/metrics/test_mean_iou.py@@ -15,6 +15,7 @@# limitations under the License.#+import pytestimport torchimport kornia@@ -86,3 +87,73 @@ class TestMeanIoU(BaseTester):mean_iou_real = torch.tensor([[0.0, 1.0, 0.5, 0.5]], device=device, dtype=torch.float32)assert mean_iou.shape == (batch_size, num_classes)self.assert_close(mean_iou, mean_iou_real)+++class TestMeanIoUBBox(BaseTester):+ """Tests for mean_iou_bbox with different box formats."""++ def test_bbox_xyxy_format(self, device, dtype):+ """Test XYXY format (original behavior)."""+ boxes_1 = torch.tensor([[40, 40, 60, 60], [30, 40, 50, 60]], device=device, dtype=dtype)+ boxes_2 = torch.tensor([[40, 50, 60, 70], [30, 40, 40, 50]], device=device, dtype=dtype)++ iou = kornia.metrics.mean_iou_bbox(boxes_1, boxes_2, box_format="xyxy")+ expected = torch.tensor([[0.3333, 0.0000], [0.1429, 0.2500]], device=device, dtype=dtype)++ self.assert_close(iou, expected, rtol=1e-3, atol=1e-4)++ def test_bbox_xywh_format(self, device, dtype):+ """Test XYWH format."""+ # Same boxes as xyxy test, but in xywh format+ boxes_1_xywh = torch.tensor([[40, 40, 20, 20], [30, 40, 20, 20]], device=device, dtype=dtype)+ boxes_2_xywh = torch.tensor([[40, 50, 20, 20], [30, 40, 10, 10]], device=device, dtype=dtype)++ iou = kornia.metrics.mean_iou_bbox(boxes_1_xywh, boxes_2_xywh, box_format="xywh")+ expected = torch.tensor([[0.3333, 0.0000], [0.1429, 0.2500]], device=device, dtype=dtype)++ self.assert_close(iou, expected, rtol=1e-3, atol=1e-4)++ def test_bbox_cxcywh_format(self, device, dtype):+ """Test CXCYWH format."""+ # Same boxes as xyxy test, but in cxcywh format+ boxes_1_cxcywh = torch.tensor([[50, 50, 20, 20], [40, 50, 20, 20]], device=device, dtype=dtype)+ boxes_2_cxcywh = torch.tensor([[50, 60, 20, 20], [35, 45, 10, 10]], device=device, dtype=dtype)++ iou = kornia.metrics.mean_iou_bbox(boxes_1_cxcywh, boxes_2_cxcywh, box_format="cxcywh")+ expected = torch.tensor([[0.3333, 0.0000], [0.1429, 0.2500]], device=device, dtype=dtype)++ self.assert_close(iou, expected, rtol=1e-3, atol=1e-4)++ def test_bbox_format_consistency(self, device, dtype):+ """Test that all formats produce same results for equivalent boxes."""+ # Define same boxes in three formats+ boxes_xyxy = torch.tensor([[10, 10, 20, 20]], device=device, dtype=dtype)+ boxes_xywh = torch.tensor([[10, 10, 10, 10]], device=device, dtype=dtype)+ boxes_cxcywh = torch.tensor([[15, 15, 10, 10]], device=device, dtype=dtype)++ iou_xyxy = kornia.metrics.mean_iou_bbox(boxes_xyxy, boxes_xyxy, box_format="xyxy")+ iou_xywh = kornia.metrics.mean_iou_bbox(boxes_xywh, boxes_xywh, box_format="xywh")+ iou_cxcywh = kornia.metrics.mean_iou_bbox(boxes_cxcywh, boxes_cxcywh, box_format="cxcywh")++ # All should give perfect IoU (1.0)+ expected = torch.tensor([[1.0]], device=device, dtype=dtype)+ self.assert_close(iou_xyxy, expected)+ self.assert_close(iou_xywh, expected)+ self.assert_close(iou_cxcywh, expected)++ def test_bbox_invalid_format(self, device, dtype):+ """Test that invalid format raises ValueError."""+ boxes = torch.tensor([[10, 10, 20, 20]], device=device, dtype=dtype)++ with pytest.raises(ValueError, match="Unsupported box format"):+ kornia.metrics.mean_iou_bbox(boxes, boxes, box_format="invalid")++ def test_bbox_default_format(self, device, dtype):+ """Test that default format is xyxy."""+ boxes_1 = torch.tensor([[40, 40, 60, 60]], device=device, dtype=dtype)+ boxes_2 = torch.tensor([[40, 50, 60, 70]], device=device, dtype=dtype)++ iou_default = kornia.metrics.mean_iou_bbox(boxes_1, boxes_2)+ iou_explicit = kornia.metrics.mean_iou_bbox(boxes_1, boxes_2, box_format="xyxy")++ self.assert_close(iou_default, iou_explicit)