Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

fix(augmentation): place RandomGaussianIllumination's peak at center * L - 0.5 (#4990)

Rızgar Ozanaeec9b2426ee
diff --git a/changelog.d/4990.breaking.md b/changelog.d/4990.breaking.md
new file mode 100644
index 000000000..2d0e33505
--- /dev/null
+++ b/changelog.d/4990.breaking.md
@@ -0,0 +1,5 @@
+`RandomGaussianIllumination` places the peak of its field at the pixel-centre position `center * L - 0.5`
+on each axis. It used to round `center * L` to a whole pixel (half to even), and on an even-length axis the
+Gaussian kernel took another half pixel off, so the peak moved with the parity of the axis: `center=0.5` sat
+one pixel past the middle of a 3-, 7- or 11-pixel axis but on it for a 5- or 9-pixel one. The drawn field now
+shifts by up to one pixel, so outputs under a fixed seed change. Fixes #4811.
diff --git a/kornia/augmentation/_2d/intensity/gaussian_illumination.py b/kornia/augmentation/_2d/intensity/gaussian_illumination.py
index 09cd6a1bb..0dbda628a 100644
--- a/kornia/augmentation/_2d/intensity/gaussian_illumination.py
+++ b/kornia/augmentation/_2d/intensity/gaussian_illumination.py
@@ -75,11 +75,10 @@ class RandomGaussianIllumination(_PicklableCompileMixin, IntensityAugmentationBa
- ``sigma`` is a fraction of the axis length, not an absolute width: the generator draws it and
multiplies by the image's width and height before building the kernel, so the same ``sigma`` is a
narrower kernel on a smaller image. Every admitted ``sigma`` gives a finite kernel, ``0`` included.
-
- .. warning::
- The drawn ``center`` is rounded to a whole pixel, half to even, so on an odd-length axis the peak can sit
- a pixel past the pixel-centre position ``center * L - 0.5``: ``center=0.5`` lands on column ``4`` of a
- 7-pixel-wide image. Tracked in `#4811 <https://github.com/kornia/kornia/issues/4811>`_.
+ - ``center`` is a fraction of the axis length too, and the peak sits at the pixel-centre position
+ ``center * L - 0.5`` without rounding to a whole pixel: ``center=0.5`` is the middle of any axis, ``0``
+ and ``1`` are the outer edges of the first and last pixel. At ``sigma=0`` the field is an impulse on the
+ pixel nearest that position, split between two pixels when it falls halfway.
.. warning::
An all-negative input can come back as an all-zero image, depending on the sampled gradient. Tracked in
diff --git a/kornia/augmentation/random_generator/_2d/gaussian_illumination.py b/kornia/augmentation/random_generator/_2d/gaussian_illumination.py
index e408fd10b..5e6caf7af 100644
--- a/kornia/augmentation/random_generator/_2d/gaussian_illumination.py
+++ b/kornia/augmentation/random_generator/_2d/gaussian_illumination.py
@@ -108,11 +108,15 @@ class GaussianIlluminationGenerator(RandomGeneratorBase):
sigma_x = width * _adapted_rsampling((batch_size, 1), self.sigma_sampler, same_on_batch)
- center_x = torch.round(width * _adapted_rsampling((batch_size, 1), self.center_sampler, same_on_batch))
+ # ``center`` maps to the pixel-centre coordinate ``center * L - 0.5``, not rounded to a whole pixel.
+ # ``gaussian`` adds 0.5 to its offsets on an even-length axis, so its mean there is ``center * L``.
+ center_x = width * _adapted_rsampling((batch_size, 1), self.center_sampler, same_on_batch)
+ center_x = center_x - (0.5 if width % 2 else 0.0)
sigma_y = height * _adapted_rsampling((batch_size, 1), self.sigma_sampler, same_on_batch)
- center_y = torch.round(height * _adapted_rsampling((batch_size, 1), self.center_sampler, same_on_batch))
+ center_y = height * _adapted_rsampling((batch_size, 1), self.center_sampler, same_on_batch)
+ center_y = center_y - (0.5 if height % 2 else 0.0)
sign = (_adapted_rsampling((batch_size, 1, 1, 1), self.sign_sampler, same_on_batch) >= 0.0).to(
device=_device, dtype=_dtype
diff --git a/tests/augmentation/test_augmentation.py b/tests/augmentation/test_augmentation.py
index e19a9383a..888f76270 100644
--- a/tests/augmentation/test_augmentation.py
+++ b/tests/augmentation/test_augmentation.py
@@ -4680,19 +4680,19 @@ class TestRandomGaussianIllumination(BaseTester):
[
[
[
- [0.726599991321564, 1.000000000000000, 0.726599991321564],
- [0.662100017070770, 0.912100017070770, 0.662100017070770],
- [0.500000000000000, 0.691100001335144, 0.500000000000000],
+ [0.867512464523315, 0.999994516372681, 0.683359920978546],
+ [0.789115190505981, 0.907470583915710, 0.624598741531372],
+ [0.622879266738892, 0.711280286312103, 0.500000000000000],
],
[
- [0.726599991321564, 1.000000000000000, 0.726599991321564],
- [0.662100017070770, 0.912100017070770, 0.662100017070770],
- [0.500000000000000, 0.691100001335144, 0.500000000000000],
+ [0.867512464523315, 0.999994516372681, 0.683359920978546],
+ [0.789115190505981, 0.907470583915710, 0.624598741531372],
+ [0.622879266738892, 0.711280286312103, 0.500000000000000],
],
[
- [0.726599991321564, 1.000000000000000, 0.726599991321564],
- [0.662100017070770, 0.912100017070770, 0.662100017070770],
- [0.500000000000000, 0.691100001335144, 0.500000000000000],
+ [0.867512464523315, 0.999994516372681, 0.683359920978546],
+ [0.789115190505981, 0.907470583915710, 0.624598741531372],
+ [0.622879266738892, 0.711280286312103, 0.500000000000000],
],
]
],
diff --git a/tests/augmentation/test_conventions_intensity_ops.py b/tests/augmentation/test_conventions_intensity_ops.py
index c7762475f..6e0f64d87 100644
--- a/tests/augmentation/test_conventions_intensity_ops.py
+++ b/tests/augmentation/test_conventions_intensity_ops.py
@@ -1219,18 +1219,26 @@ class TestIlluminationAndNormalizeConventions(BaseTester):
flat = per_sample._params["gradient"].flatten(1).float().abs()
assert len(set(flat.argmax(1).tolist())) > 1, "the strongest-gradient location is not drawn per sample"
- # Issue #4811: ``center`` is rounded to a whole pixel, half to even, so ``center=0.5`` peaks one column
- # past the middle of a 7-wide image but on it for a 9-wide one. A fix that maps ``center`` to the pixel
- # centre flips the 7-wide leg.
- # Snippet used to generate expected:
- # a = K.RandomGaussianIllumination(gain=(0.5, 0.5), sigma=(0.2, 0.2), center=(0.5, 0.5), sign=(1., 1.), p=1.)
- # torch.manual_seed(0); a(torch.zeros(1, 1, w, w)); print(int(a._params["gradient"][0, 0].sum(0).argmax()))
- @pytest.mark.parametrize(("width", "peak"), [(7, 4), (9, 4)])
- def test_wart_random_gaussian_illumination_center_rounds_half_to_even_4811(self, device, dtype, width, peak):
- aug = K.RandomGaussianIllumination(gain=(0.5, 0.5), sigma=(0.2, 0.2), center=(0.5, 0.5), sign=(1.0, 1.0), p=1.0)
+ # Issue #4811: ``center`` maps to the pixel-centre position ``center * L - 0.5`` without rounding, so the
+ # column and row profiles are symmetric about it: the listed pair of indices sits the same distance on either
+ # side.
+ # Before the fix ``center=0.5`` peaked one column past the middle of a 3-, 7- or 11-wide image (rounding
+ # half to even), and ``center=0.25`` sat half a pixel off on a 6-wide axis. The 9- and 8-wide legs held before.
+ @pytest.mark.parametrize(
+ ("width", "center", "pair"),
+ [(3, 0.5, (0, 2)), (7, 0.5, (2, 4)), (9, 0.5, (3, 5)), (8, 0.5, (3, 4)), (8, 0.25, (1, 2)), (6, 0.25, (0, 2))],
+ )
+ def test_convention_random_gaussian_illumination_center_is_pixel_centre_4811(
+ self, device, dtype, width, center, pair
+ ):
+ aug = K.RandomGaussianIllumination(
+ gain=(0.5, 0.5), sigma=(0.2, 0.2), center=(center, center), sign=(1.0, 1.0), p=1.0
+ )
torch.manual_seed(_FORWARD_SEED)
aug(torch.zeros(1, 1, width, width, device=device, dtype=dtype))
- assert int(aug._params["gradient"][0, 0].float().sum(0).argmax()) == peak
+ field = aug._params["gradient"][0, 0].float()
+ for profile in (field.sum(0), field.sum(1)):
+ self.assert_close(profile[pair[0]], profile[pair[1]])
# The three classes round-trip through pickle, deepcopy and torch.save (#4435), and the copy
# reproduces the original's output under the same seed.