Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

fix(augmentation): draw RandomCutMixV2 cut starts over the full legal range (#4757)

yi11146201f1a660a
diff --git a/changelog.d/4757.fixed.md b/changelog.d/4757.fixed.md
new file mode 100644
index 000000000..3222cf232
--- /dev/null
+++ b/changelog.d/4757.fixed.md
@@ -0,0 +1,6 @@
+`RandomCutMixV2` now draws each cut start uniformly over every legal position, `0` to `side - cut_side` on each
+axis. Previously the draw stopped short of the far edge: with the default float32 parameters a cut smaller than the
+image never reached its last row or column, and a full-image cut (`cut_size=(0.0, 0.0)`) practically always started
+one pixel before the image, so the last row and column kept their original pixels while lambda reported a full-image
+cut. The random stream for a fixed seed changes: cut sizes, lambdas and `mix_pairs` are unchanged, but the cut
+positions differ from earlier releases.
diff --git a/kornia/augmentation/_2d/mix/cutmix.py b/kornia/augmentation/_2d/mix/cutmix.py
index 412cd054a..0102f444e 100644
--- a/kornia/augmentation/_2d/mix/cutmix.py
+++ b/kornia/augmentation/_2d/mix/cutmix.py
@@ -98,13 +98,13 @@ class RandomCutMixV2(MixAugmentationBaseV2):
>>> label = torch.tensor([0, 1])
>>> cutmix = RandomCutMixV2(data_keys=["input", "class"], use_correct_lambda=True)
>>> cutmix(input, label)
- [tensor([[[[0.8879, 0.4510, 1.0000],
+ [tensor([[[[1.0000, 1.0000, 1.0000],
[0.1498, 0.4015, 1.0000],
- [1.0000, 1.0000, 1.0000]]],
+ [0.4594, 0.1756, 1.0000]]],
<BLANKLINE>
<BLANKLINE>
- [[[1.0000, 1.0000, 0.7995],
- [1.0000, 1.0000, 0.0542],
+ [[[0.8879, 1.0000, 1.0000],
+ [0.1498, 1.0000, 1.0000],
[0.4594, 0.1756, 0.9492]]]]), tensor([[[0.0000, 1.0000, 0.5556],
[1.0000, 0.0000, 0.5556]]])]
diff --git a/kornia/augmentation/random_generator/_2d/cutmix.py b/kornia/augmentation/random_generator/_2d/cutmix.py
index 8d916944d..009225dc7 100644
--- a/kornia/augmentation/random_generator/_2d/cutmix.py
+++ b/kornia/augmentation/random_generator/_2d/cutmix.py
@@ -154,16 +154,16 @@ class CutmixGenerator(RandomGeneratorBase):
cut_width = cut_width[0]
# One start position per cut, like the sizes above; with same_on_batch the helper repeats a single draw.
- # Reserve at least 1 pixel for cropping.
+ # Legal starts run 0 .. side - cut_side inclusive; the clamp only guards a u -> 1 float round-up.
_gen_shape = (batch_size * self.num_mix,)
x_start = _adapted_rsampling(_gen_shape, self.rand_sampler, same_on_batch).to(device=_device, dtype=_dtype) * (
- width - cut_width - 1
+ width - cut_width + 1
)
y_start = _adapted_rsampling(_gen_shape, self.rand_sampler, same_on_batch).to(device=_device, dtype=_dtype) * (
- height - cut_height - 1
+ height - cut_height + 1
)
- x_start = x_start.floor()
- y_start = y_start.floor()
+ x_start = torch.minimum(x_start.floor(), width - cut_width)
+ y_start = torch.minimum(y_start.floor(), height - cut_height)
crop_src = bbox_generator(x_start.squeeze(), y_start.squeeze(), cut_width, cut_height)
diff --git a/testing/half_precision_xfails/cpu_bfloat16.txt b/testing/half_precision_xfails/cpu_bfloat16.txt
index 1fd593715..84b6910cd 100644
--- a/testing/half_precision_xfails/cpu_bfloat16.txt
+++ b/testing/half_precision_xfails/cpu_bfloat16.txt
@@ -57,7 +57,6 @@ call builtins.AssertionError tests/augmentation/test_random_generator.py::TestCo
call builtins.AssertionError tests/augmentation/test_random_generator.py::TestRandomAffineGen::test_same_on_batch[cpu-bfloat16]
call builtins.AssertionError tests/augmentation/test_random_generator.py::TestRandomCropSizeGen::test_random_gen[cpu-bfloat16]
call builtins.AssertionError tests/augmentation/test_random_generator.py::TestRandomCropSizeGen::test_same_on_batch[cpu-bfloat16]
-call builtins.AssertionError tests/augmentation/test_random_generator.py::TestRandomCutMixGen::test_same_on_batch[cpu-bfloat16]
call builtins.AssertionError tests/augmentation/test_random_generator_3d.py::TestRandomAffineGen3D::test_random_gen[cpu-bfloat16]
call builtins.AssertionError tests/augmentation/test_random_generator_3d.py::TestRandomCropGen3D::test_random_gen[cpu-bfloat16]
call builtins.AssertionError tests/augmentation/test_random_generator_3d.py::TestRandomPerspectiveGen3D::test_random_gen[cpu-bfloat16]
diff --git a/tests/augmentation/test_augmentation_mix.py b/tests/augmentation/test_augmentation_mix.py
index 30901cf1c..1da56c01b 100644
--- a/tests/augmentation/test_augmentation_mix.py
+++ b/tests/augmentation/test_augmentation_mix.py
@@ -310,7 +310,7 @@ class TestRandomCutMixV2(BaseTester):
expected = torch.tensor(
[
[[[0.0, 0.0, 0.0, 1.0], [0.0, 0.0, 0.0, 1.0], [1.0, 1.0, 1.0, 1.0]]],
- [[[1.0, 1.0, 1.0, 0.0], [1.0, 1.0, 1.0, 0.0], [0.0, 0.0, 0.0, 0.0]]],
+ [[[0.0, 0.0, 0.0, 0.0], [0.0, 1.0, 1.0, 1.0], [0.0, 1.0, 1.0, 1.0]]],
],
device=device,
dtype=dtype,
@@ -327,6 +327,21 @@ class TestRandomCutMixV2(BaseTester):
)
self.assert_close(out_label[0, :, 2], torch.tensor([0.5, 0.5], device=device, dtype=label_dtype))
+ def test_full_image_cut_pastes_the_whole_partner_4730(self, device, dtype):
+ # kornia#4730: a full-image cut used to start at -1, so the box sat one pixel outside the image and the
+ # last row and column kept the original pixels while lambda reported a full cut.
+ torch.manual_seed(0)
+ batch, height, width = 3, 5, 7
+ image = torch.arange(1, batch + 1, device=device, dtype=dtype).view(batch, 1, 1, 1)
+ image = image.expand(batch, 2, height, width).contiguous()
+ f = RandomCutMixV2(p=1.0, cut_size=(0.0, 0.0), data_keys=["input", "class"], use_correct_lambda=True)
+ out_image, out_label = f(image, torch.arange(batch, device=device))
+ pairs = f._params["mix_pairs"][0].to(device)
+ box = torch.tensor([[0.0, 0.0], [width - 1, 0.0], [width - 1, height - 1], [0.0, height - 1]], device=device)
+ self.assert_close(f._params["crop_src"][0].to(box), box.expand(batch, 4, 2), rtol=0.0, atol=0.0)
+ self.assert_close(out_image, image[pairs], rtol=0.0, atol=0.0)
+ self.assert_close(out_label[0, :, 2], torch.zeros_like(out_label[0, :, 2]), rtol=0.0, atol=0.0)
+
def test_random_mixup_p0(self, device, dtype):
torch.manual_seed(76)
f = RandomCutMixV2(p=0.0, data_keys=["input", "class"], use_correct_lambda=True)
@@ -359,7 +374,7 @@ class TestRandomCutMixV2(BaseTester):
expected = torch.tensor(
[
[[[0.0, 0.0, 1.0, 1.0], [0.0, 0.0, 1.0, 1.0], [1.0, 1.0, 1.0, 1.0]]],
- [[[1.0, 1.0, 0.0, 0.0], [1.0, 1.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0]]],
+ [[[0.0, 0.0, 0.0, 0.0], [1.0, 1.0, 0.0, 0.0], [1.0, 1.0, 0.0, 0.0]]],
],
device=device,
dtype=dtype,
@@ -393,7 +408,7 @@ class TestRandomCutMixV2(BaseTester):
expected = torch.tensor(
[
- [[[0.0, 0.0, 0.0, 1.0], [0.0, 0.0, 0.0, 1.0], [1.0, 1.0, 1.0, 1.0]]],
+ [[[1.0, 0.0, 0.0, 0.0], [1.0, 0.0, 0.0, 0.0], [1.0, 1.0, 1.0, 1.0]]],
[[[1.0, 1.0, 0.0, 0.0], [1.0, 1.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0]]],
],
device=device,
@@ -436,8 +451,8 @@ class TestRandomCutMixV2(BaseTester):
expected = torch.tensor(
[
- [[[0.0, 0.0, 0.0, 1.0], [0.0, 0.0, 0.0, 1.0], [1.0, 1.0, 1.0, 1.0]]],
- [[[1.0, 1.0, 1.0, 0.0], [1.0, 1.0, 1.0, 0.0], [0.0, 0.0, 0.0, 0.0]]],
+ [[[1.0, 0.0, 0.0, 0.0], [1.0, 0.0, 0.0, 0.0], [1.0, 1.0, 1.0, 1.0]]],
+ [[[0.0, 1.0, 1.0, 1.0], [0.0, 1.0, 1.0, 1.0], [0.0, 0.0, 0.0, 0.0]]],
],
device=device,
dtype=dtype,
diff --git a/tests/augmentation/test_random_generator.py b/tests/augmentation/test_random_generator.py
index cf3f47920..e95e623e3 100644
--- a/tests/augmentation/test_random_generator.py
+++ b/tests/augmentation/test_random_generator.py
@@ -1741,6 +1741,32 @@ class TestRandomCutMixGen(RandomGeneratorBaseTests):
side.append(float(box[:, 0].max() - box[:, 0].min()))
assert side[0] > side[1]
+ def test_start_reaches_every_legal_position_uniformly_4730(self, device, dtype):
+ # kornia#4730: starts were drawn from 0 .. side - cut - 2, so the last two legal starts were never drawn.
+ torch.manual_seed(0)
+ gen = CutmixGenerator(cut_size=torch.tensor([0.75, 0.75], device=device, dtype=dtype), p=1.0)
+ crop = gen(torch.Size([3000, 1, 7, 10]))["crop_src"][0]
+ # sqrt(1 - 0.75) = 0.5: every cut is 5 wide and 3 tall, so x starts 0..5 and y starts 0..4.
+ assert ((crop[:, 1, 0] - crop[:, 0, 0] + 1) == 5).all()
+ assert ((crop[:, 2, 1] - crop[:, 0, 1] + 1) == 3).all()
+ for start, n_legal in ((crop[:, 0, 0], 6), (crop[:, 0, 1], 5)):
+ counts = torch.bincount(start.long().cpu(), minlength=n_legal)
+ assert counts.numel() == n_legal, counts # no start past side - cut
+ expected = len(start) / n_legal
+ assert counts.min() > 0.8 * expected and counts.max() < 1.2 * expected, counts
+
+ def test_start_stays_inside_when_the_draw_rounds_to_one_4730(self, device, dtype, monkeypatch):
+ # A float16/bfloat16 parameter dtype rounds a float32 draw above 1 - 2**-12 (float16) or 1 - 2**-9
+ # (bfloat16) to exactly 1.0, and MPS half-precision rand can return 1.0; floor(1.0 * (side - cut + 1)) is one
+ # past the last legal start, so the generator must clamp it.
+ rand = torch.rand
+ monkeypatch.setattr(torch, "rand", lambda *args, **kwargs: torch.ones_like(rand(*args, **kwargs)))
+ gen = CutmixGenerator(cut_size=torch.tensor([0.75, 0.75], device=device, dtype=dtype), p=1.0)
+ crop = gen(torch.Size([2, 1, 7, 10]))["crop_src"]
+ # 5 x 3 cut in a 10 x 7 image: the last legal start is (5, 4).
+ box = torch.tensor([[5.0, 4.0], [9.0, 4.0], [9.0, 6.0], [5.0, 6.0]], device=device, dtype=dtype)
+ assert_close(crop, box.expand_as(crop), rtol=0.0, atol=0.0)
+
def test_random_gen(self, device, dtype):
torch.manual_seed(42)
image_shape = torch.Size([8, 3, 200, 200])
@@ -1757,12 +1783,12 @@ class TestRandomCutMixGen(RandomGeneratorBaseTests):
[
[
[[48.0, 49.0], [47.0, 49.0], [47.0, 48.0], [48.0, 48.0]],
- [[31.0, 129.0], [30.0, 129.0], [30.0, 128.0], [31.0, 128.0]],
- [[56.0, 44.0], [180.0, 44.0], [180.0, 168.0], [56.0, 168.0]],
+ [[31.0, 131.0], [30.0, 131.0], [30.0, 130.0], [31.0, 130.0]],
+ [[58.0, 46.0], [182.0, 46.0], [182.0, 170.0], [58.0, 170.0]],
[[59.0, 74.0], [58.0, 74.0], [58.0, 73.0], [59.0, 73.0]],
- [[53.0, 53.0], [184.0, 53.0], [184.0, 184.0], [53.0, 184.0]],
- [[75.0, 167.0], [74.0, 167.0], [74.0, 166.0], [75.0, 166.0]],
- [[27.0, 4.0], [190.0, 4.0], [190.0, 167.0], [27.0, 167.0]],
+ [[55.0, 55.0], [186.0, 55.0], [186.0, 186.0], [55.0, 186.0]],
+ [[76.0, 168.0], [75.0, 168.0], [75.0, 167.0], [76.0, 167.0]],
+ [[29.0, 5.0], [192.0, 5.0], [192.0, 168.0], [29.0, 168.0]],
[[22.0, 46.0], [21.0, 46.0], [21.0, 45.0], [22.0, 45.0]],
]
],
@@ -1773,8 +1799,10 @@ class TestRandomCutMixGen(RandomGeneratorBaseTests):
}
assert res.keys() == expected.keys(), res.keys()
assert_close(res["mix_pairs"], expected["mix_pairs"], rtol=1e-4, atol=1e-4)
- # bfloat16 keeps 8 significant bits, so ``floor(u * span)`` can land one pixel from the float32 literal.
- tolerance = {"rtol": 0.0, "atol": 1.0} if dtype == torch.bfloat16 else {"rtol": 1e-4, "atol": 1e-4}
+ # The start is computed in the parameter dtype: float16 and bfloat16 keep 11 and 8 significant bits, so
+ # ``floor(u * span)`` can land one pixel from the float32 literal.
+ half = dtype in (torch.float16, torch.bfloat16)
+ tolerance = {"rtol": 0.0, "atol": 1.0} if half else {"rtol": 1e-4, "atol": 1e-4}
assert_close(res["crop_src"], expected["crop_src"], **tolerance)
def test_same_on_batch(self, device, dtype):
@@ -1791,8 +1819,8 @@ class TestRandomCutMixGen(RandomGeneratorBaseTests):
"crop_src": torch.tensor(
[
[
- [[114, 53], [113, 53], [113, 52], [114, 52]],
- [[114, 53], [113, 53], [113, 52], [114, 52]],
+ [[115, 53], [114, 53], [114, 52], [115, 52]],
+ [[115, 53], [114, 53], [114, 52], [115, 52]],
]
],
device=device,