Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

fix(augmentation): pass RandomBoxBlur's flag to box_blur by keyword and document it (#4486)

Dipak Chaudhari1f692e33afb9
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 2478b1728..4a40d8b03 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -407,6 +407,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Bug fixes
+* `RandomBoxBlur`'s `normalized` argument is documented as what it does. It was described as "if True, L1 norm
+ of the kernel is set to 1", but it was forwarded positionally into `kornia.filters.box_blur`'s `separable`
+ parameter: it chooses between the separable and the single 2D pass, and the kernel is L1-normalized either
+ way, so `normalized=False` returns window means, not sums. The call now passes `border_type` and
+ `separable` by keyword; outputs are unchanged. (#4433, #4486)
+
* `So3.exp`, `So3.log`, `Se3.exp`, `Se3.log` and `Se2.exp` return finite gradients at the identity,
and `So3.log` also at a half turn, instead of `nan` on every dtype. The forward values are unchanged;
`torch.where` was differentiating the singular branch it does not select. (#4405)
diff --git a/kornia/augmentation/_2d/intensity/box_blur.py b/kornia/augmentation/_2d/intensity/box_blur.py
index 436561259..6aa8bcd8a 100644
--- a/kornia/augmentation/_2d/intensity/box_blur.py
+++ b/kornia/augmentation/_2d/intensity/box_blur.py
@@ -32,7 +32,9 @@ class RandomBoxBlur(IntensityAugmentationBase2D):
kernel_size: the blurring kernel size.
border_type: the padding mode to be applied before convolving.
The expected modes are: ``constant``, ``reflect``, ``replicate`` or ``circular``.
- normalized: if True, L1 norm of the kernel is set to 1.
+ normalized: selects the implementation of :func:`kornia.filters.box_blur`: ``True`` computes the blur
+ as two 1D passes (``separable=True``), ``False`` as one 2D pass. The kernel is L1-normalized either
+ way, so both return the mean of each window, never its sum, and differ only by float rounding.
same_on_batch: apply the same transformation across the batch.
p: probability of applying the transformation.
keepdim: whether to keep the output shape the same as input (True) or broadcast it
@@ -69,4 +71,4 @@ class RandomBoxBlur(IntensityAugmentationBase2D):
def apply_transform(
self, input: Tensor, params: Dict[str, Tensor], flags: Dict[str, Any], transform: Optional[Tensor] = None
) -> Tensor:
- return box_blur(input, flags["kernel_size"], flags["border_type"], flags["normalized"])
+ return box_blur(input, flags["kernel_size"], border_type=flags["border_type"], separable=flags["normalized"])
diff --git a/tests/augmentation/test_augmentation.py b/tests/augmentation/test_augmentation.py
index b6c918779..914eafa98 100644
--- a/tests/augmentation/test_augmentation.py
+++ b/tests/augmentation/test_augmentation.py
@@ -4778,12 +4778,31 @@ class TestRandomElasticTransform(BaseTester):
assert labels_transformed[to_apply].ne(labels[to_apply]).any()
-class TestRandomBoxBlur:
+class TestRandomBoxBlur(BaseTester):
def test_smoke(self, device, dtype):
img = torch.rand(1, 1, 2, 2, device=device, dtype=dtype)
aug = RandomBoxBlur(p=1.0)
assert img.shape == aug(img).shape
+ @pytest.mark.parametrize("normalized", [True, False])
+ def test_normalized_selects_the_separable_path(self, normalized, device, dtype):
+ from kornia.filters import box_blur
+
+ torch.manual_seed(0)
+ img = torch.rand(2, 3, 6, 8, device=device, dtype=dtype)
+ out = RandomBoxBlur((3, 3), normalized=normalized, p=1.0)(img.clone())
+
+ expected = box_blur(img, (3, 3), border_type="reflect", separable=normalized)
+ torch.testing.assert_close(out, expected, rtol=0.0, atol=0.0)
+
+ @pytest.mark.parametrize("normalized", [True, False])
+ def test_output_is_the_window_mean_for_either_setting(self, normalized, device, dtype):
+ torch.manual_seed(0)
+ img = torch.rand(1, 1, 6, 8, device=device, dtype=dtype)
+ out = RandomBoxBlur((3, 3), normalized=normalized, p=1.0)(img.clone())
+
+ self.assert_close(out[0, 0, 2, 2], img[0, 0, 1:4, 1:4].mean())
+
class TestPadTo(BaseTester):
def test_smoke(self, device, dtype):