Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

fix(augmentation): offset RandomMosaic boxes by W along x and H along y (#4550)

Dipak Chaudhari2ae72221bfe4
diff --git a/changelog.d/4550.fixed.md b/changelog.d/4550.fixed.md
new file mode 100644
index 000000000..077d0482b
--- /dev/null
+++ b/changelog.d/4550.fixed.md
@@ -0,0 +1 @@
+`RandomMosaic` places bounding boxes on the tile that holds their image on non-square inputs: the x and y tile offsets of the box composition were swapped, which only agreed with the composed image when height equals width.
diff --git a/kornia/augmentation/_2d/mix/mosaic.py b/kornia/augmentation/_2d/mix/mosaic.py
index d3fa20a43..b0576106a 100644
--- a/kornia/augmentation/_2d/mix/mosaic.py
+++ b/kornia/augmentation/_2d/mix/mosaic.py
@@ -131,8 +131,10 @@ class RandomMosaic(MixAugmentationBaseV2):
for i in range(flags["mosaic_grid"][0]):
for j in range(flags["mosaic_grid"][1]):
_offset = offset.clone()
- _offset[idx, 0] = batch_shapes_idx[:, -2] * i - src_box_idx[:, 0, 0]
- _offset[idx, 1] = batch_shapes_idx[:, -1] * j - src_box_idx[:, 0, 1]
+ # _compose_images concatenates the i loop along the width and the j loop along
+ # the height, so tile (i, j) starts at x = W * i, y = H * j.
+ _offset[idx, 0] = batch_shapes_idx[:, -1] * i - src_box_idx[:, 0, 0]
+ _offset[idx, 1] = batch_shapes_idx[:, -2] * j - src_box_idx[:, 0, 1]
_box = input.clone()
_idx = i * flags["mosaic_grid"][1] + j
_box._data[params["permutation"][:, 0]] = _box._data[params["permutation"][:, _idx]]
diff --git a/tests/augmentation/test_augmentation_mix.py b/tests/augmentation/test_augmentation_mix.py
index fd551907e..095d1e7fd 100644
--- a/tests/augmentation/test_augmentation_mix.py
+++ b/tests/augmentation/test_augmentation_mix.py
@@ -364,6 +364,31 @@ class TestRandomMosaic(BaseTester):
assert output.shape == expected_shape
+ @pytest.mark.parametrize("shape", [(8, 8), (6, 8), (8, 6), (4, 12)])
+ def test_boxes_follow_tile_content_on_non_square_input_4468(self, device, dtype, shape):
+ # _compose_images puts tile (i, j) at x = W * i, y = H * j; the box offsets used H for x
+ # and W for y, which only agree on a square input. The crop starts at each tile's centre
+ # and every patch straddles that centre, so all four copies of each patch stay partly in
+ # view and no box is clamped away: every output box must lie on marked pixels.
+ torch.manual_seed(0)
+ height, width = shape
+ cx, cy = width // 2, height // 2
+ batch_size = 3
+ image = torch.zeros(batch_size, 1, height, width, device=device, dtype=dtype)
+ for k in range(batch_size):
+ image[k, 0, cy - 1 : cy + 1, cx - 1 : cx + 1] = k + 1
+ boxes = torch.tensor([[[cx - 1.0, cy - 1.0, cx + 1.0, cy + 1.0]]] * batch_size, device=device, dtype=dtype)
+ aug = RandomMosaic(p=1.0, start_ratio_range=(0.5, 0.5), data_keys=["input", "bbox_xyxy"])
+
+ out_image, out_boxes = aug(image, boxes)
+
+ assert out_boxes.shape == (batch_size, 4, 4)
+ for n in range(batch_size):
+ for x1, y1, x2, y2 in out_boxes[n].round().long().tolist():
+ region = out_image[n, 0, y1:y2, x1:x2]
+ assert region.numel() > 0, (n, (x1, y1, x2, y2))
+ assert bool((region > 0).all()), (n, (x1, y1, x2, y2), out_image[n, 0])
+
def test_smoke(self):
f = RandomMosaic(data_keys=["input", "class"])
repr = (