Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

fix(augmentation): correct RandomMosaic H/W axes (#4459)

Hrithik Sharmae2f7daa7592e
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 21c1234a3..189d56d7f 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -381,6 +381,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Bug fixes
+* `RandomMosaic` now preserves `(H, W)` for non-square inputs when `output_size=None`; it previously
+ returned `(W, H)`. `start_ratio_range` now scales x by width and y by height; it previously scaled
+ x by height and y by width. (#4459)
+
* `RandomThinPlateSpline(scale=0.0)` now generates zero control-point displacement instead of
raising an error when constructing a degenerate uniform distribution. (#4463)
* `RandomRain` with `same_on_batch=True` now samples the same number of rain drops for all samples in the batch. (#4453)
diff --git a/kornia/augmentation/random_generator/_2d/mosaic.py b/kornia/augmentation/random_generator/_2d/mosaic.py
index 26e7176f0..ac203cf3d 100644
--- a/kornia/augmentation/random_generator/_2d/mosaic.py
+++ b/kornia/augmentation/random_generator/_2d/mosaic.py
@@ -96,13 +96,13 @@ class MosaicGenerator(RandomGeneratorBase):
start_corner_factor = _adapted_rsampling(
(batch_size, 2), self.start_ratio_range_sampler, same_on_batch=False
).to(device=_device, dtype=_dtype)
- start_corner_x = start_corner_factor[:, 0] * batch_shape[-2]
- start_corner_y = start_corner_factor[:, 1] * batch_shape[-1]
+ start_corner_x = start_corner_factor[:, 0] * batch_shape[-1]
+ start_corner_y = start_corner_factor[:, 1] * batch_shape[-2]
crop_src = bbox_generator(
start_corner_x,
start_corner_y,
- start_corner_x.clone().fill_(input_sizes[0]),
- start_corner_y.clone().fill_(input_sizes[1]),
+ start_corner_x.clone().fill_(input_sizes[1]),
+ start_corner_y.clone().fill_(input_sizes[0]),
)
crop_dst = torch.tensor(
[[[0, 0], [input_sizes[1] - 1, 0], [input_sizes[1] - 1, input_sizes[0] - 1], [0, input_sizes[0] - 1]]],
diff --git a/tests/augmentation/test_augmentation_mix.py b/tests/augmentation/test_augmentation_mix.py
index e39fb5795..eb96b0ed0 100644
--- a/tests/augmentation/test_augmentation_mix.py
+++ b/tests/augmentation/test_augmentation_mix.py
@@ -300,6 +300,42 @@ class TestRandomCutMixV2(BaseTester):
class TestRandomMosaic(BaseTester):
+ def test_non_square_input_preserves_hw_4438(self):
+ torch.manual_seed(0)
+
+ input = torch.rand(4, 3, 6, 8)
+ output = RandomMosaic(p=1.0)(input)
+
+ assert output.shape == (4, 3, 6, 8)
+
+ def test_start_ratio_uses_xy_axes_4438(self):
+ torch.manual_seed(0)
+
+ aug = RandomMosaic(
+ p=1.0,
+ start_ratio_range=(0.5, 0.5),
+ )
+ aug(torch.rand(4, 3, 6, 8))
+
+ top_left = aug._params["src"][0, 0]
+
+ expected = torch.tensor(
+ [4.0, 3.0],
+ device=top_left.device,
+ dtype=top_left.dtype,
+ )
+
+ torch.testing.assert_close(top_left, expected)
+
+ @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)
+
+ input = torch.rand(1, 6, 8)
+ output = RandomMosaic(p=1.0, keepdim=keepdim)(input)
+
+ assert output.shape == expected_shape
+
def test_smoke(self):
f = RandomMosaic(data_keys=["input", "class"])
repr = (