Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

fix(augmentation): permute list box padding with the RandomMosaic tiles (#5100)

awss0c8ab79a6165
diff --git a/changelog.d/5100.fixed.md b/changelog.d/5100.fixed.md
new file mode 100644
index 000000000..be686dd7b
--- /dev/null
+++ b/changelog.d/5100.fixed.md
@@ -0,0 +1,4 @@
+`RandomMosaic` with a list box input now gives each tile the padding count of the image it was drawn from. A selected
+sample used to take its box count from the wrong source images, so it could drop real boxes or return padding rows as
+boxes such as `[7, 0, 7, 0]`, from a direct call and from `AugmentationSequential`. Tensor box inputs are unchanged.
+(#4715)
diff --git a/kornia/augmentation/_2d/mix/mosaic.py b/kornia/augmentation/_2d/mix/mosaic.py
index 5796a9a7e..2db8ac00e 100644
--- a/kornia/augmentation/_2d/mix/mosaic.py
+++ b/kornia/augmentation/_2d/mix/mosaic.py
@@ -101,8 +101,7 @@ class RandomMosaic(MixAugmentationBaseV2):
- A list box input comes back as a list with one tensor per sample, from a direct call and from
:class:`~kornia.augmentation.container.AugmentationSequential` alike, whatever the gate selects. An
unselected sample's tensor is its own boxes unchanged, with no padding rows. A selected sample's tensor
- takes its box count from the wrong source images, so it can drop real boxes or keep padding rows
- (`#4715 <https://github.com/kornia/kornia/issues/4715>`_).
+ holds the boxes of each tile's source image, with no padding rows.
- In either form, a selected sample's box that falls below ``min_bbox_size`` is zeroed by
:meth:`~kornia.geometry.boxes.Boxes.filter_boxes_by_area`. A direct call exports it as ``[0, 0, 1, 1]``
in ``"bbox_xyxy"`` and ``"bbox_xywh"``, not as zero-area padding
@@ -173,6 +172,11 @@ class RandomMosaic(MixAugmentationBaseV2):
_box = input.clone()
_idx = i * flags["mosaic_grid"][1] + j
_box._data[params["permutation"][:, 0]] = _box._data[params["permutation"][:, _idx]]
+ if input._N is not None:
+ # A list input pads each sample to the longest one; the tile keeps its source's padding count.
+ _box._N = list(input._N)
+ for dst, src in params["permutation"][:, [0, _idx]].tolist():
+ _box._N[dst] = input._N[src]
_box.translate(_offset, inplace=True)
if maybe_out_boxes is None:
maybe_out_boxes = _box
diff --git a/tests/augmentation/test_augmentation_mix.py b/tests/augmentation/test_augmentation_mix.py
index d6d0030b0..fb426f09c 100644
--- a/tests/augmentation/test_augmentation_mix.py
+++ b/tests/augmentation/test_augmentation_mix.py
@@ -691,6 +691,60 @@ class TestRandomMosaic(BaseTester):
torch.testing.assert_close(output[0], boxes[0])
torch.testing.assert_close(output[2], boxes[2])
+ @pytest.mark.parametrize("sequential", [False, True])
+ @pytest.mark.parametrize("gate", [(1.0, 1.0, 1.0, 1.0), (1.0, 0.0, 1.0, 0.0)])
+ @pytest.mark.parametrize("data_key", ["bbox_xyxy", "bbox_xywh", "bbox"])
+ def test_list_boxes_follow_tile_sources_4715(self, data_key, gate, sequential, device, dtype):
+ # The samples carry 1, 3, 2 and 1 boxes, so each tile's padding depends on its source image. The window
+ # starts at the canvas origin and spans the whole 2x2 canvas, so every box is translated and none is clipped.
+ image = torch.rand(4, 1, 6, 8, device=device, dtype=dtype)
+ xyxy = [
+ torch.tensor(sample, device=device, dtype=dtype)
+ for sample in (
+ [[1.0, 1.0, 3.0, 3.0]],
+ [[1.0, 1.0, 3.0, 3.0], [2.0, 1.0, 6.0, 5.0], [0.5, 0.5, 2.0, 2.0]],
+ [[1.0, 1.0, 4.0, 4.0], [2.0, 2.0, 5.0, 5.0]],
+ [[0.0, 0.0, 2.0, 2.0]],
+ )
+ ]
+ permutation = torch.tensor([[0, 2, 2, 1], [1, 1, 3, 3], [2, 0, 0, 0], [3, 2, 1, 3]], device=device)
+
+ def convert(boxes: torch.Tensor) -> torch.Tensor:
+ if data_key == "bbox_xywh":
+ return torch.cat([boxes[:, :2], boxes[:, 2:] - boxes[:, :2]], -1)
+ if data_key == "bbox":
+ x1, y1, x2, y2 = boxes.unbind(-1)
+ return torch.stack([torch.stack(v, -1) for v in ((x1, y1), (x2, y1), (x2, y2), (x1, y2))], -2)
+ return boxes
+
+ boxes = [convert(b) for b in xyxy]
+ kwargs = {"output_size": (12, 16), "start_ratio_range": (0.0, 0.0), "p": 1.0}
+ if sequential:
+ aug = AugmentationSequential(RandomMosaic(**kwargs), data_keys=["input", data_key])
+ aug(image, boxes)
+ params = aug._params
+ params[0].data["permutation"] = permutation
+ params[0].data["batch_prob"] = params[0].data["batch_prob"].new_tensor(gate)
+ else:
+ aug = RandomMosaic(**kwargs, data_keys=["input", data_key])
+ aug(image, boxes)
+ params = dict(aug._params)
+ params["permutation"] = permutation
+ params["batch_prob"] = params["batch_prob"].new_tensor(gate)
+ _, out = aug(image, boxes, params=params)
+
+ assert isinstance(out, list) and len(out) == 4
+ for b, selected in enumerate(gate):
+ if not selected:
+ self.assert_close(out[b], boxes[b])
+ continue
+ tiles = []
+ for i in range(2):
+ for j in range(2):
+ offset = torch.tensor([8.0 * i, 6.0 * j] * 2, device=device, dtype=dtype)
+ tiles.append(xyxy[int(permutation[b, 2 * i + j])] + offset)
+ self.assert_close(out[b], convert(torch.cat(tiles)))
+
@pytest.mark.parametrize(("keepdim", "expected_shape"), [(False, (1, 1, 6, 8)), (True, (1, 6, 8))])
def test_non_square_unbatched_keepdim_4438(self, keepdim, expected_shape):
torch.manual_seed(0)