Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

fix(augmentation): name the minimum image size the blurs and sharpness need (#4645)

Dipak Chaudharib56b7138146b
diff --git a/changelog.d/4645.fixed.md b/changelog.d/4645.fixed.md
new file mode 100644
index 000000000..310534c06
--- /dev/null
+++ b/changelog.d/4645.fixed.md
@@ -0,0 +1 @@
+`RandomBoxBlur`, `RandomGaussianBlur` and `RandomSharpness` now raise a `ValueError` naming the class, the kernel, the border type and the input shape when the image is too small for the filter, instead of letting a raw torch padding error out. The sizes they accept are unchanged: the minimum is derived from the wider of `filter2d`'s two pads, which for an even kernel extent is the rear one, so `RandomBoxBlur((4, 4), border_type="reflect")` reports the 3 pixels it needs rather than 2.
diff --git a/kornia/augmentation/_2d/intensity/box_blur.py b/kornia/augmentation/_2d/intensity/box_blur.py
index 3058d2290..615d00139 100644
--- a/kornia/augmentation/_2d/intensity/box_blur.py
+++ b/kornia/augmentation/_2d/intensity/box_blur.py
@@ -20,6 +20,7 @@ from typing import Any, Dict, Optional, Tuple, Union
from torch import Tensor
from kornia.augmentation._2d.intensity.base import IntensityAugmentationBase2D
+from kornia.augmentation.utils import _check_filter_min_size
from kornia.constants import BorderType
from kornia.filters import box_blur
@@ -61,12 +62,13 @@ class RandomBoxBlur(IntensityAugmentationBase2D):
weights; ``border_type="constant"`` pads with zeros, which pulls a border pixel toward ``0``: below
the input's minimum for a positive image, and above its maximum for a negative one.
- .. warning::
- At the default ``border_type="reflect"``, an image with a spatial axis no longer than half the kernel's extent
- along that axis raises a raw torch ``RuntimeError`` about the padding rather than a kornia error naming the
- class or the shape. ``"constant"`` and ``"replicate"`` run on the same image; ``"circular"`` raises a padding
- error of its own, also raw, once the kernel radius exceeds that axis. Tracked in `#4559
- <https://github.com/kornia/kornia/issues/4559>`_.
+ .. note::
+ The padding sets a minimum image size. An even kernel extent ``k`` is padded asymmetrically, with the
+ wider ``k // 2`` pad behind, so the bound is stated against that pad rather than the radius: at the default
+ ``border_type="reflect"`` each spatial axis must be longer than ``k // 2`` along it (``3`` pixels for a
+ ``4``-wide kernel), and ``"circular"`` needs at least ``k // 2``; both raise a ``ValueError`` naming the
+ class, the kernel and the input shape. ``"constant"`` and ``"replicate"`` invent their padding and run down
+ to a single pixel.
.. note::
This function internally uses :func:`kornia.filters.box_blur`.
@@ -104,10 +106,7 @@ 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"],
- # a per-call `border_type` override reaches `flags` unnormalized, so normalize here too
- border_type=BorderType.get(flags["border_type"]).name.lower(),
- separable=flags["normalized"],
- )
+ # a per-call `border_type` override reaches `flags` unnormalized, so normalize here too
+ border_type = BorderType.get(flags["border_type"]).name.lower()
+ _check_filter_min_size("RandomBoxBlur", input, flags["kernel_size"], border_type=border_type)
+ return box_blur(input, flags["kernel_size"], border_type=border_type, separable=flags["normalized"])
diff --git a/kornia/augmentation/_2d/intensity/gaussian_blur.py b/kornia/augmentation/_2d/intensity/gaussian_blur.py
index 5443ef06f..a0d0f2714 100644
--- a/kornia/augmentation/_2d/intensity/gaussian_blur.py
+++ b/kornia/augmentation/_2d/intensity/gaussian_blur.py
@@ -22,8 +22,10 @@ from torch import Tensor
from kornia.augmentation import random_generator as rg
from kornia.augmentation._2d.intensity.base import IntensityAugmentationBase2D
+from kornia.augmentation.utils import _check_filter_min_size
from kornia.constants import BorderType
from kornia.filters import gaussian_blur2d
+from kornia.filters.kernels import _check_kernel_size, _unpack_2d_ks
class RandomGaussianBlur(IntensityAugmentationBase2D):
@@ -63,12 +65,11 @@ class RandomGaussianBlur(IntensityAugmentationBase2D):
weights; ``border_type="constant"`` pads with zeros, which pulls a border pixel toward ``0``: below
the input's minimum for a positive image, and above its maximum for a negative one.
- .. warning::
- At the default ``border_type="reflect"``, an image with a spatial axis no longer than half the kernel's extent
- along that axis raises a raw torch ``RuntimeError`` about the padding rather than a kornia error naming the
- class or the shape. ``"constant"`` and ``"replicate"`` run on the same image; ``"circular"`` raises a padding
- error of its own, also raw, once the kernel radius exceeds that axis. Tracked in `#4559
- <https://github.com/kornia/kornia/issues/4559>`_.
+ .. note::
+ The padding sets a minimum image size. At the default ``border_type="reflect"`` each spatial axis must be
+ longer than the kernel's radius along it, and ``"circular"`` needs at least that radius; both raise a
+ ``ValueError`` naming the class, the kernel and the input shape. ``"constant"`` and ``"replicate"`` invent
+ their padding and run down to a single pixel.
.. note::
This function internally uses :func:`kornia.filters.gaussian_blur2d`.
@@ -120,6 +121,15 @@ class RandomGaussianBlur(IntensityAugmentationBase2D):
flags: Dict[str, Any],
transform: Optional[Tensor] = None,
) -> Tensor:
+ # An even entry is wrong at every image size, so raise gaussian_blur2d's own kernel error
+ # before the size guard below can blame the image for it.
+ _check_kernel_size(_unpack_2d_ks(self.flags["kernel_size"]), 0)
+ _check_filter_min_size(
+ "RandomGaussianBlur",
+ input,
+ self.flags["kernel_size"],
+ border_type=self.flags["border_type"].name.lower(),
+ )
sigma = params["sigma"].unsqueeze(-1).expand(-1, 2)
if self.same_on_batch:
# Every sample shares one sigma, so pass a single (1, 2) sigma: gaussian_blur2d then
diff --git a/kornia/augmentation/_2d/intensity/motion_blur.py b/kornia/augmentation/_2d/intensity/motion_blur.py
index 0f0f17a12..564dfabea 100644
--- a/kornia/augmentation/_2d/intensity/motion_blur.py
+++ b/kornia/augmentation/_2d/intensity/motion_blur.py
@@ -83,13 +83,12 @@ class RandomMotionBlur(IntensityAugmentationBase2D):
``border_type="reflect"`` the result stays between the input's extremes, up to rounding, at
``resample="nearest"`` or ``"bilinear"``; a ``"bicubic"`` rotation gives the kernel negative weights,
and the result can overshoot both extremes.
- - an image smaller than the kernel is accepted, down to ``1 x 1``, at ``border_type="constant"``
- and ``"replicate"``. ``"reflect"`` raises once a spatial axis is no longer than half the kernel
- size along it, as the two padding blurs do, and ``"circular"`` raises a padding error of its own once the
- kernel radius exceeds a spatial axis. Both are raw torch errors of the kind
- `#4559 <https://github.com/kornia/kornia/issues/4559>`_ tracks, although that issue is scoped to the
- three classes that raise at their *defaults* and names this one as accepting the same images --
- which it does, at the default ``border_type="constant"``.
+ - an image smaller than the kernel is accepted, down to ``1 x 1``, at the default
+ ``border_type="constant"`` and at ``"replicate"``. ``"reflect"`` raises once a spatial axis is
+ no longer than half the kernel size along it, and ``"circular"`` raises a padding error of its
+ own once the kernel radius exceeds a spatial axis. Unlike :class:`RandomBoxBlur`,
+ :class:`RandomGaussianBlur` and :class:`RandomSharpness`, which name the class and the shape,
+ both of these surface as raw torch errors.
Note:
Input torch.Tensor must be float and normalized into [0, 1] for the best differentiability support.
diff --git a/kornia/augmentation/_2d/intensity/sharpness.py b/kornia/augmentation/_2d/intensity/sharpness.py
index 852296f00..5d49fdeb0 100644
--- a/kornia/augmentation/_2d/intensity/sharpness.py
+++ b/kornia/augmentation/_2d/intensity/sharpness.py
@@ -21,6 +21,7 @@ import torch
from kornia.augmentation import random_generator as rg
from kornia.augmentation._2d.intensity.base import IntensityAugmentationBase2D
+from kornia.augmentation.utils import _check_filter_min_size
from kornia.enhance import sharpness
@@ -60,10 +61,10 @@ class RandomSharpness(IntensityAugmentationBase2D):
An input whose values are all negative comes back as an all-zero image. Tracked in
`#4430 <https://github.com/kornia/kornia/issues/4430>`_.
- .. warning::
- An image with a side smaller than the ``3 x 3`` smoothing kernel raises a raw torch
- ``RuntimeError`` about the padded input size rather than a kornia error naming the class.
- Tracked in `#4559 <https://github.com/kornia/kornia/issues/4559>`_.
+ .. note::
+ The smoothing kernel is a fixed ``3 x 3`` convolved with no padding, so both spatial sides
+ must be at least ``3`` pixels; a smaller image raises a ``ValueError`` naming the class and
+ the input shape.
.. note::
This function internally uses :func:`kornia.enhance.sharpness`.
@@ -104,5 +105,7 @@ class RandomSharpness(IntensityAugmentationBase2D):
flags: Dict[str, Any],
transform: Optional[torch.Tensor] = None,
) -> torch.Tensor:
+ # the smoothing kernel is a fixed 3 x 3 convolved without padding of its own
+ _check_filter_min_size("RandomSharpness", input, 3, border_type="valid")
factor = params["sharpness"]
return sharpness(input, factor)
diff --git a/kornia/augmentation/utils/__init__.py b/kornia/augmentation/utils/__init__.py
index 04c66ff0d..2d4332780 100644
--- a/kornia/augmentation/utils/__init__.py
+++ b/kornia/augmentation/utils/__init__.py
@@ -25,6 +25,7 @@ from kornia.augmentation.utils.helpers import (
_adapted_rsampling,
_adapted_sampling,
_adapted_uniform,
+ _check_filter_min_size,
_infer_batch_shape,
_infer_batch_shape3d,
_pad_with_fill,
@@ -57,6 +58,7 @@ __all__ = [
"_adapted_rsampling",
"_adapted_sampling",
"_adapted_uniform",
+ "_check_filter_min_size",
"_check_positive_int_or_traced",
"_common_param_check",
"_infer_batch_shape",
diff --git a/kornia/augmentation/utils/helpers.py b/kornia/augmentation/utils/helpers.py
index b3346740a..3e38cbed0 100644
--- a/kornia/augmentation/utils/helpers.py
+++ b/kornia/augmentation/utils/helpers.py
@@ -540,3 +540,42 @@ class MultiprocessWrapper:
kwargs = {key: val.clone() if isinstance(val, torch.Tensor) else val for key, val in kwargs.items()}
super().__init__(*args, **kwargs)
+
+
+#: How small an image each padding mode can filter, given the kernel extent ``k`` along that axis.
+#: ``filter2d`` pads an even kernel asymmetrically -- ``(k - 1) // 2`` in front and ``k // 2``
+#: behind -- so the constraint is against the wider of the two, ``k // 2``, not against the radius.
+#: ``reflect`` cannot mirror a pad as wide as the axis and ``circular`` cannot wrap one wider than
+#: it; ``"valid"`` is the no-padding case, where the kernel itself must fit. ``constant`` and
+#: ``replicate`` invent their padding and run on a 1-pixel axis.
+_MIN_FILTERED_SIZE: Dict[str, Callable[[int], int]] = {
+ "reflect": lambda extent: extent // 2 + 1,
+ "circular": lambda extent: extent // 2,
+ "valid": lambda extent: extent,
+}
+
+
+def _check_filter_min_size(
+ name: str,
+ input: torch.Tensor,
+ kernel_size: Union[int, Tuple[int, int], List[int]],
+ border_type: str = "reflect",
+) -> None:
+ """Refuse an image the filter's padding cannot handle, naming the class and the shape.
+
+ torch raises about "padding size" or "calculated padded input size" from two layers below,
+ which names neither the augmentation nor the image the caller passed.
+ """
+ size = (kernel_size, kernel_size) if isinstance(kernel_size, int) else tuple(kernel_size)
+ smallest = _MIN_FILTERED_SIZE.get(str(border_type).lower())
+ if smallest is None:
+ return
+ for axis, (extent, side) in enumerate(zip(size, input.shape[-2:])):
+ minimum = smallest(int(extent))
+ if side < minimum:
+ raise ValueError(
+ f"{name} cannot filter an image this small: kernel_size="
+ f"{tuple(int(s) for s in size)} with border_type={border_type!r} needs at least "
+ f"{minimum} pixel(s) along {'height' if axis == 0 else 'width'}, but the input is "
+ f"{tuple(int(s) for s in input.shape)}."
+ )
diff --git a/tests/augmentation/test_conventions_intensity_ops.py b/tests/augmentation/test_conventions_intensity_ops.py
index 6d54cb29a..6c095331e 100644
--- a/tests/augmentation/test_conventions_intensity_ops.py
+++ b/tests/augmentation/test_conventions_intensity_ops.py
@@ -286,6 +286,18 @@ class TestBlurConventions(BaseTester):
with pytest.raises(Exception, match="odd integer"):
_sync(K.RandomGaussianBlur((2, 2), (1.0, 1.0), p=1.0)(image).device)
+ # Row 6c-21b: the even-kernel error does not depend on the image size. 2x2 is below the reflect
+ # minimum a 4-extent kernel needs, so a size check that ran first would blame the image instead.
+ # Snippet used to generate expected:
+ # K.RandomGaussianBlur((4, 4), (0.1, 1.0), p=1.0)(torch.rand(1, 1, 2, 2)) (and a 3x4 kernel)
+ # executed 2026-09-22 (torch 2.5.0, cpu) -> `Kernel size must be an odd integer bigger than 0.
+ # Gotcha 4 on (4, 4)` and `... Gotcha 4 on (3, 4)`.
+ @pytest.mark.parametrize("kernel_size", [(4, 4), (3, 4)])
+ def test_convention_random_gaussian_blur_even_kernel_error_is_size_independent(self, device, dtype, kernel_size):
+ image = torch.rand(1, 1, 2, 2).to(device=device, dtype=dtype)
+ with pytest.raises(Exception, match=r"odd integer bigger than 0\. Gotcha 4"):
+ _sync(K.RandomGaussianBlur(kernel_size, (0.1, 1.0), p=1.0)(image).device)
+
# Row 6c-22: RandomMotionBlur's ``angle`` is counter-clockwise as the image is displayed. The
# fixture is a 7x9 impulse at (row 2, col 3), off both centre lines, so no literal below is its
# own transpose: +45 deg carries the blur from (row 3, col 2) up to (row 1, col 4), and -45 deg
@@ -490,26 +502,24 @@ class TestBlurConventions(BaseTester):
self.assert_close(out, make()(half_impulse) * 2.0)
self.assert_close(make()(-half_impulse), -make()(half_impulse))
- # Issue #4559: RandomBoxBlur, RandomGaussianBlur and RandomSharpness let a raw torch error out
- # when the image is smaller than the kernel along an axis, instead of raising a kornia error that
- # names the kernel and the shape. The two blurs reflect-pad, so they fail as soon as an axis is
- # no longer than the kernel radius (`k // 2`, one pixel for the 3x3 default); RandomSharpness
- # convolves without padding, so it needs the full 3x3.
- # RandomMedianBlur and RandomMotionBlur accept the same degenerate image, which is what makes the
- # split a defect rather than a package-wide rule. #4559 leaves two coherent outcomes open (raise
- # a named kornia error, or pad and run), so this is a wart pin on today's behavior, not a strict
- # xfail on a settled contract.
+ # Issue #4559, fixed: RandomBoxBlur, RandomGaussianBlur and RandomSharpness now raise a kornia
+ # `ValueError` naming the class, the kernel and the input shape, where they used to let a raw torch
+ # padding error out. The thresholds are unchanged, and they are what the message states: the two
+ # blurs reflect-pad, so an axis must be longer than the kernel radius (`k // 2`, one pixel for the
+ # 3x3 default); RandomSharpness convolves without padding, so it needs the full 3x3.
+ # RandomMedianBlur and RandomMotionBlur accept the same degenerate image, which is what made the
+ # split a defect rather than a package-wide rule, and they are unchanged.
# Snippet used to generate expected:
# for shape in ((2, 3, 1, 8), (2, 3, 2, 2), (2, 3, 3, 3)):
# x = torch.rand(*shape)
# torch.manual_seed(0); K.RandomBoxBlur(p=1.0)(x) # and the four other classes
- # executed 2026-09-15 (torch 2.14.0, cpu) -> on (2, 3, 1, 8) the two blurs raise `RuntimeError:
- # Argument #6: Padding size should be less than the corresponding input dimension, but got:
- # padding (1, 1) at dimension 2 of input [2, 3, 1, 8]` and RandomSharpness `RuntimeError:
- # Calculated padded input size per channel: (1 x 8). Kernel size: (3 x 3). Kernel size can't be
- # greater than actual input size`; on (2, 3, 2, 2) only RandomSharpness raises; on (2, 3, 3, 3)
- # all five run; RandomMedianBlur and RandomMotionBlur run on every shape.
- def test_wart_blur_and_sharpness_reject_images_smaller_than_kernel_4559(self, device, dtype):
+ # executed 2026-09-18 (torch 2.5.0, cpu) -> on (2, 3, 1, 8) the two blurs raise `ValueError:
+ # RandomBoxBlur cannot filter an image this small: kernel_size=(3, 3) with border_type='reflect'
+ # needs at least 2 pixel(s) along height, but the input is (2, 3, 1, 8)` and RandomSharpness the
+ # same shape of message with `border_type='valid'` and 3 pixels; on (2, 3, 2, 2) only
+ # RandomSharpness raises; on (2, 3, 3, 3) all five run; RandomMedianBlur and RandomMotionBlur run
+ # on every shape.
+ def test_convention_blur_and_sharpness_name_the_size_they_need_4559(self, device, dtype):
# The skip is scoped to the legs that actually reflect-pad. RandomSharpness fails through
# F.conv2d, and RandomMedianBlur and RandomMotionBlur run on a 1x8 image with no reflect padding
# at all, so skipping the whole test where reflection_pad2d is missing -- the torch 2.5.1 float16
@@ -527,10 +537,8 @@ class TestBlurConventions(BaseTester):
if not reflect_ok:
continue
torch.manual_seed(_FORWARD_SEED)
- # The message is torch's reflect-padding guard, identical on cpu and mps and in every
- # dtype (measured); a kornia-named error would not be a RuntimeError at all, since
- # BaseError derives straight from Exception.
- with pytest.raises(RuntimeError, match="Padding size should be less"):
+ # kornia's own error, raised before the filter runs, so it is device- and dtype-independent.
+ with pytest.raises(ValueError, match=rf"{name} cannot filter an image this small"):
_sync(make()(thin).device)
torch.manual_seed(_FORWARD_SEED)
assert make()(small).shape == small.shape, f"{name} should still accept a 2x2 image"
@@ -543,16 +551,19 @@ class TestBlurConventions(BaseTester):
}
narrow = torch.rand(2, 3, 20, 2).to(device=device, dtype=dtype)
short = torch.rand(2, 3, 3, 20).to(device=device, dtype=dtype)
- for make in rectangular.values():
+ for name, make in rectangular.items():
if not reflect_ok:
continue
torch.manual_seed(_FORWARD_SEED)
assert make()(narrow).shape == narrow.shape
torch.manual_seed(_FORWARD_SEED)
- with pytest.raises(RuntimeError, match="Padding size should be less"):
+ with pytest.raises(ValueError, match=rf"{name} cannot filter an image this small"):
_sync(make()(short).device)
+ # the message names the axis that is too short, not just the kernel
+ with pytest.raises(ValueError, match="along height"):
+ make()(short)
torch.manual_seed(_FORWARD_SEED)
- with pytest.raises(RuntimeError, match="Kernel size can't be greater"):
+ with pytest.raises(ValueError, match="RandomSharpness cannot filter an image this small"):
_sync(K.RandomSharpness(1.0, p=1.0)(small).device)
torch.manual_seed(_FORWARD_SEED)
assert K.RandomSharpness(1.0, p=1.0)(square).shape == square.shape
@@ -567,8 +578,8 @@ class TestBlurConventions(BaseTester):
# as the kernel radius (kernel_size // 2) exceeds the axis it pads -- which is a strictly smaller
# image than the reflect guard needs, and is why the docstrings cannot say the non-reflect border
# types run on every image. ``"constant"`` and ``"replicate"`` do run there. The message is
- # torch's, not kornia's, so this is the same wart as the pin above and #4559's two outcomes
- # (raise a named kornia error, or pad and run) leave it a wart pin rather than a strict xfail.
+ # still torch's for RandomMotionBlur, which #4559 leaves alone, while the two padding blurs now
+ # raise kornia's own error before reaching the pad -- at the same threshold, the radius.
# Snippet used to generate expected:
# torch.manual_seed(1234); one = torch.rand(2, 3, 1, 1); thin = torch.rand(2, 3, 3, 12)
# for bt in ("constant", "reflect", "replicate", "circular"):
@@ -579,7 +590,7 @@ class TestBlurConventions(BaseTester):
# value causes wrapping around more than once.`, while motion `k=3` on 1x1 (radius 1, axis 1) and
# the `"constant"`/`"replicate"` legs return the input shape. The boundary is the radius, not the
# kernel: `(9, 9)` circular runs at H=4 and raises at H=3; motion `k=5` runs at 2x2.
- def test_wart_circular_padding_rejects_images_smaller_than_kernel_radius_4559(self, device, dtype):
+ def test_convention_circular_padding_rejects_images_smaller_than_kernel_radius_4559(self, device, dtype):
if not supports_replicate_padding(device, dtype):
pytest.skip("replication_pad2d is unavailable for this device/dtype")
torch.manual_seed(_FIXTURE_SEED)
@@ -602,12 +613,59 @@ class TestBlurConventions(BaseTester):
}
for name, make in blurs.items():
torch.manual_seed(_FORWARD_SEED)
- with pytest.raises(RuntimeError, match="wrapping around"):
+ with pytest.raises(ValueError, match=rf"{name} cannot filter an image this small"):
_sync(make("circular")(thin).device)
for border in ("constant", "replicate"):
torch.manual_seed(_FORWARD_SEED)
assert make(border)(thin).shape == thin.shape, f"{name} at {border} should accept a 3-row image"
+ # Issue #4559, the case the first cut of the guard missed: RandomBoxBlur accepts EVEN kernel
+ # sizes, and `filter2d` pads those asymmetrically -- `(k - 1) // 2` in front, `k // 2` behind.
+ # Stating the minima against the radius `(k - 1) // 2` therefore under-reports by one pixel for
+ # every even extent, and those inputs walked straight past the guard into the raw torch errors
+ # it exists to replace. The constraint is the wider pad, `k // 2`.
+ # Snippet used to generate expected:
+ # for k in range(1, 9):
+ # front, rear = (k - 1) // 2, k - 1 - (k - 1) // 2
+ # for mode in ("reflect", "circular"):
+ # min(s for s in range(1, 12) if F.pad(torch.rand(1, 1, s, s), [front, rear] * 2, mode=mode))
+ # executed 2026-09-19 (torch 2.5.0, cpu) -> reflect needs `k // 2 + 1` and circular `k // 2` for
+ # every k in 1..8, i.e. 3 and 2 for a 4-wide kernel where the radius said 2 and 1.
+ @pytest.mark.parametrize(
+ "kernel, border, too_small, big_enough",
+ [
+ ((4, 4), "reflect", (1, 1, 2, 2), (1, 1, 3, 3)),
+ ((4, 4), "circular", (1, 1, 1, 1), (1, 1, 2, 2)),
+ ((2, 2), "reflect", (1, 1, 1, 1), (1, 1, 2, 2)),
+ ((6, 6), "reflect", (1, 1, 3, 3), (1, 1, 4, 4)),
+ ((6, 6), "circular", (1, 1, 2, 2), (1, 1, 3, 3)),
+ # the odd-extent circular boundary: `(k + 1) // 2` agrees with `k // 2` on every even row
+ # above, and only an odd kernel at its exact minimum tells them apart
+ ((5, 5), "circular", (1, 1, 1, 1), (1, 1, 2, 2)),
+ ],
+ )
+ def test_convention_even_kernels_use_the_wider_asymmetric_pad_4559(
+ self, kernel, border, too_small, big_enough, device, dtype
+ ):
+ if border == "reflect" and not supports_reflect_padding(device, dtype):
+ pytest.skip("reflection_pad2d is unavailable for this device/dtype")
+ torch.manual_seed(_FIXTURE_SEED)
+ small = torch.rand(*too_small).to(device=device, dtype=dtype)
+ large = torch.rand(*big_enough).to(device=device, dtype=dtype)
+
+ torch.manual_seed(_FORWARD_SEED)
+ with pytest.raises(ValueError, match="RandomBoxBlur cannot filter an image this small"):
+ _sync(K.RandomBoxBlur(kernel, border_type=border, p=1.0)(small).device)
+ # one pixel more and it is kornia's job to run, not to refuse
+ torch.manual_seed(_FORWARD_SEED)
+ assert K.RandomBoxBlur(kernel, border_type=border, p=1.0)(large).shape == large.shape
+ # the modes that invent their padding are untouched by the widening
+ for invented in ("constant", "replicate"):
+ if invented == "replicate" and not supports_replicate_padding(device, dtype):
+ continue
+ torch.manual_seed(_FORWARD_SEED)
+ assert K.RandomBoxBlur(kernel, border_type=invented, p=1.0)(small).shape == small.shape
+
# The constant-border claim is directional, and a positive fixture cannot tell "pulled toward 0" from
# "pulled below the minimum". On an all-negative image zero padding pushes the border ABOVE the
# maximum and nothing ever falls below the minimum, so both fixtures are checked.