Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

fix(augmentation): name the minimum image size RandomMotionBlur needs (#4982)

Mohansree Vijayakumar5f4271d84f49
diff --git a/changelog.d/4982.fixed.md b/changelog.d/4982.fixed.md
new file mode 100644
index 000000000..8d7e65869
--- /dev/null
+++ b/changelog.d/4982.fixed.md
@@ -0,0 +1 @@
+`RandomMotionBlur` now raises a `ValueError` naming the class, the drawn kernel, the border type and the input shape when the image is too small for `border_type="reflect"` or `"circular"`, as `RandomBoxBlur` and `RandomGaussianBlur` already do, instead of letting a raw torch padding error out. The sizes it accepts are unchanged.
diff --git a/kornia/augmentation/_2d/intensity/motion_blur.py b/kornia/augmentation/_2d/intensity/motion_blur.py
index 9b7bec186..b58f36aff 100644
--- a/kornia/augmentation/_2d/intensity/motion_blur.py
+++ b/kornia/augmentation/_2d/intensity/motion_blur.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.constants import BorderType, Resample
from kornia.filters import motion_blur
@@ -77,12 +78,9 @@ class RandomMotionBlur(IntensityAugmentationBase2D):
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 the default
``border_type="constant"`` and at ``"replicate"``. ``"reflect"`` needs each spatial axis longer than
- the kernel radius along it, and ``"circular"`` at least that long.
-
- .. warning::
- Under ``"reflect"`` and ``"circular"`` an image too small for the drawn kernel raises a raw torch padding
- error, where :class:`RandomBoxBlur` and :class:`RandomGaussianBlur` raise a ``ValueError`` naming the
- class and the shape. Tracked in `#4784 <https://github.com/kornia/kornia/issues/4784>`_.
+ the kernel radius along it, and ``"circular"`` at least that long; below that both raise a
+ ``ValueError`` naming the class, the drawn kernel and the input shape, as :class:`RandomBoxBlur` and
+ :class:`RandomGaussianBlur` do.
Note:
This function accepts another transformation torch.Tensor (:math:`(B, 3, 3)`), then the
@@ -155,11 +153,13 @@ class RandomMotionBlur(IntensityAugmentationBase2D):
kernel_size_list: List[int] = params["ksize_factor"].tolist()
idx: int = cast(int, params["idx"][0])
kernel_size = kernel_size_list[idx]
+ border_type = flags["border_type"].name.lower()
+ _check_filter_min_size("RandomMotionBlur", input, kernel_size, border_type=border_type)
return motion_blur(
input,
kernel_size=kernel_size,
angle=params["angle_factor"],
direction=params["direction_factor"],
- border_type=flags["border_type"].name.lower(),
+ border_type=border_type,
mode=flags["resample"].name.lower(),
)
diff --git a/tests/augmentation/test_conventions_intensity_ops.py b/tests/augmentation/test_conventions_intensity_ops.py
index 6e0f64d87..d0ae8942b 100644
--- a/tests/augmentation/test_conventions_intensity_ops.py
+++ b/tests/augmentation/test_conventions_intensity_ops.py
@@ -507,13 +507,12 @@ class TestBlurConventions(BaseTester):
torch.manual_seed(_FORWARD_SEED)
assert make(border)(thin).shape == thin.shape, f"{name} at {border} should accept a 3-row image"
- # Wart (#4784, https://github.com/kornia/kornia/issues/4784): RandomMotionBlur has the same minimum
- # size under ``"reflect"`` and ``"circular"`` but lets torch's raw padding error out, where RandomBoxBlur
- # raises a named ValueError. A kornia check flips this pin; the controls below it do not move.
+ # RandomMotionBlur has the same minimum size as the padding blurs under ``"reflect"`` and ``"circular"``
+ # and raises the same named ValueError below it (#4784); the controls below it do not move.
@pytest.mark.parametrize(
("border", "kernel_size", "shape"), [("reflect", 3, (2, 3, 1, 8)), ("circular", 5, (2, 3, 1, 1))]
)
- def test_wart_random_motion_blur_small_image_raises_a_raw_padding_error_4784(
+ def test_convention_random_motion_blur_names_the_size_it_needs_4784(
self, device, dtype, border, kernel_size, shape
):
if border == "reflect" and not supports_reflect_padding(device, dtype):
@@ -522,9 +521,13 @@ class TestBlurConventions(BaseTester):
image = torch.rand(*shape).to(device=device, dtype=dtype)
torch.manual_seed(_FORWARD_SEED)
aug = K.RandomMotionBlur(kernel_size, (45.0, 45.0), (0.0, 0.0), border_type=border, p=1.0)
- with pytest.raises(RuntimeError) as raised:
+ with pytest.raises(ValueError, match="RandomMotionBlur cannot filter an image this small"):
_sync(aug(image).device)
- assert type(raised.value) is RuntimeError
+ # A ranged kernel_size is checked against the size drawn for the call.
+ torch.manual_seed(_FORWARD_SEED)
+ ranged = K.RandomMotionBlur((kernel_size, kernel_size), (45.0, 45.0), (0.0, 0.0), border_type=border, p=1.0)
+ with pytest.raises(ValueError, match="RandomMotionBlur cannot filter an image this small"):
+ _sync(ranged(image).device)
# The default constant border runs on the same image, and one radius less runs under circular.
torch.manual_seed(_FORWARD_SEED)
assert K.RandomMotionBlur(kernel_size, (45.0, 45.0), (0.0, 0.0), p=1.0)(image).shape == image.shape