Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

fix(filters)!: default the motion-blur border to reflect (2-D) and replicate (3-D) (#5246)

Dmytro Mishkin8f7e643b7e7c
diff --git a/changelog.d/5246.breaking.md b/changelog.d/5246.breaking.md
new file mode 100644
index 000000000..a6269d3a8
--- /dev/null
+++ b/changelog.d/5246.breaking.md
@@ -0,0 +1,13 @@
+`motion_blur` and `MotionBlur` now default to `border_type='reflect'`, and `motion_blur3d` and `MotionBlur3D` to
+`'replicate'`; previously all four zero-padded (`'constant'`). The zero padding pulled pixels whose kernel reaches past
+an edge toward `0`: a constant image came back as low as `0.6` at the border for `kernel_size=5` at angle 0 in 2-D and
+`0.667` for `kernel_size=3` at angle `(0, 0, 0)` in 3-D, and lower at other angles and directions (measured down to
+`0.16` in 2-D, for `kernel_size=7` at angle 120, and `0.2` in 3-D). A constant image now stays constant, as it does
+under `filter2d`, `gaussian_blur2d`, `box_blur` and `filter3d`. Pass `border_type='constant'` to keep the old output.
+`'reflect'` needs each spatial axis longer than `kernel_size // 2`, so a 2-D image with an axis of `kernel_size // 2`
+pixels or fewer now raises at the default border instead of being zero-padded; the 3-D default is `'replicate'` so that
+thin volumes (`D = 1`) still work. On torch releases without a float16 CPU `reflection_pad2d` / `replication_pad3d`
+kernel (2.5.1 among them), float16 CPU input to these four filters now raises at the default, where it used to run with
+zero padding; pass `border_type='constant'` (the old behaviour) or `'circular'` (which needs each axis at least
+`kernel_size // 2` long) there. `RandomMotionBlur` and `RandomMotionBlur3D` keep their own `'constant'` default, so
+their output does not change.
diff --git a/kornia/augmentation/_2d/intensity/motion_blur.py b/kornia/augmentation/_2d/intensity/motion_blur.py
index b58f36aff..fce4bd163 100644
--- a/kornia/augmentation/_2d/intensity/motion_blur.py
+++ b/kornia/augmentation/_2d/intensity/motion_blur.py
@@ -65,7 +65,9 @@ class RandomMotionBlur(IntensityAugmentationBase2D):
``-1`` and ``+1`` pile it at opposite ends. The rotation then resamples the kernel with ``resample``:
``"nearest"`` can drop or duplicate taps, so the line's length and end weights change with ``angle``, and
``"bilinear"`` or ``"bicubic"`` also spread weight off the line.
- - the defaults ``border_type="constant"`` and ``resample="nearest"`` are the function's own defaults.
+ - the default ``resample="nearest"`` is the function's own default. The default ``border_type="constant"``
+ is not: the class zero-pads by its own default, while :func:`kornia.filters.motion_blur` defaults to
+ ``"reflect"``. Pass ``border_type="reflect"`` to match the function.
- a ranged ``kernel_size`` is drawn once per call and repeated into ``_params["ksize_factor"]`` with
shape ``(B,)``, so every sample uses that size even with ``same_on_batch=False``; angle and direction
are drawn per sample unless ``same_on_batch=True``. A tuple range draws each odd size inside it with
diff --git a/kornia/filters/motion.py b/kornia/filters/motion.py
index 8f68c3b84..81fbc148d 100644
--- a/kornia/filters/motion.py
+++ b/kornia/filters/motion.py
@@ -41,7 +41,11 @@ class MotionBlur(nn.Module):
while higher values towards 1.0 will point the motion blur forward. A value of 0.0 leads to a
uniformly (but still angled) motion blur.
border_type: the padding mode to be applied before convolving. The expected modes are:
- ``'constant'``, ``'reflect'``, ``'replicate'`` or ``'circular'``.
+ ``'constant'``, ``'reflect'``, ``'replicate'`` or ``'circular'``. Default: ``'reflect'``, which leaves
+ a constant image constant like :func:`~kornia.filters.box_blur` and
+ :func:`~kornia.filters.gaussian_blur2d`, but needs each spatial axis longer than ``kernel_size // 2``.
+ ``'constant'`` zero-pads, so pixels whose kernel reaches past an edge (at most
+ ``kernel_size // 2`` from it) are pulled toward ``0``.
mode: interpolation mode for rotating the kernel. ``'bilinear'`` or ``'nearest'``.
Returns:
@@ -59,7 +63,7 @@ class MotionBlur(nn.Module):
"""
def __init__(
- self, kernel_size: int, angle: float, direction: float, border_type: str = "constant", mode: str = "nearest"
+ self, kernel_size: int, angle: float, direction: float, border_type: str = "reflect", mode: str = "nearest"
) -> None:
super().__init__()
self.kernel_size = kernel_size
@@ -106,7 +110,11 @@ class MotionBlur3D(nn.Module):
while higher values towards 1.0 will point the motion blur forward. A value of 0.0 leads to a
uniformly (but still angled) motion blur.
border_type: the padding mode to be applied before convolving. The expected modes are:
- ``'constant'``, ``'reflect'``, ``'replicate'`` or ``'circular'``.
+ ``'constant'``, ``'reflect'``, ``'replicate'`` or ``'circular'``. Default: ``'replicate'``, which
+ leaves a constant volume constant like :func:`~kornia.filters.filter3d`, at any volume size
+ (``'reflect'`` needs each axis longer than ``kernel_size // 2``, so it raises on a thin volume such as
+ ``D = 1``). ``'constant'`` zero-pads, so voxels whose kernel reaches past a face
+ (at most ``kernel_size // 2`` from it) are pulled toward ``0``.
mode: interpolation mode for rotating the kernel. ``'bilinear'`` or ``'nearest'``.
Returns:
@@ -132,7 +140,7 @@ class MotionBlur3D(nn.Module):
kernel_size: int,
angle: float | tuple[float, float, float] | torch.Tensor,
direction: float | torch.Tensor,
- border_type: str = "constant",
+ border_type: str = "replicate",
mode: str = "nearest",
) -> None:
super().__init__()
@@ -181,7 +189,7 @@ def motion_blur(
kernel_size: int,
angle: float | torch.Tensor,
direction: float | torch.Tensor,
- border_type: str = "constant",
+ border_type: str = "reflect",
mode: str = "nearest",
) -> torch.Tensor:
r"""Perform motion blur on torch.Tensor images.
@@ -199,7 +207,11 @@ def motion_blur(
uniformly (but still angled) motion blur.
If torch.Tensor, it must be :math:`(B,)`.
border_type: the padding mode to be applied before convolving. The expected modes are:
- ``'constant'``, ``'reflect'``, ``'replicate'`` or ``'circular'``. Default: ``'constant'``.
+ ``'constant'``, ``'reflect'``, ``'replicate'`` or ``'circular'``. Default: ``'reflect'``, which leaves
+ a constant image constant like :func:`~kornia.filters.box_blur` and
+ :func:`~kornia.filters.gaussian_blur2d`, but needs each spatial axis longer than ``kernel_size // 2``.
+ ``'constant'`` zero-pads, so pixels whose kernel reaches past an edge (at most
+ ``kernel_size // 2`` from it) are pulled toward ``0``.
mode: interpolation mode for rotating the kernel. ``'bilinear'`` or ``'nearest'``.
Return:
@@ -226,7 +238,7 @@ def motion_blur3d(
kernel_size: int,
angle: tuple[float, float, float] | torch.Tensor,
direction: float | torch.Tensor,
- border_type: str = "constant",
+ border_type: str = "replicate",
mode: str = "nearest",
) -> torch.Tensor:
r"""Perform motion blur on 3D volumes (5D torch.Tensor).
@@ -242,7 +254,11 @@ def motion_blur3d(
uniformly (but still angled) motion blur.
If torch.Tensor, it must be :math:`(B,)`.
border_type: the padding mode to be applied before convolving. The expected modes are:
- ``'constant'``, ``'reflect'``, ``'replicate'`` or ``'circular'``. Default: ``'constant'``.
+ ``'constant'``, ``'reflect'``, ``'replicate'`` or ``'circular'``. Default: ``'replicate'``, which
+ leaves a constant volume constant like :func:`~kornia.filters.filter3d`, at any volume size
+ (``'reflect'`` needs each axis longer than ``kernel_size // 2``, so it raises on a thin volume such as
+ ``D = 1``). ``'constant'`` zero-pads, so voxels whose kernel reaches past a face
+ (at most ``kernel_size // 2`` from it) are pulled toward ``0``.
mode: interpolation mode for rotating the kernel. ``'bilinear'`` or ``'nearest'``.
Return:
diff --git a/testing/base.py b/testing/base.py
index cb915aa7c..9e7cb07c4 100644
--- a/testing/base.py
+++ b/testing/base.py
@@ -292,6 +292,20 @@ def supports_replicate_padding(device: torch.device, dtype: torch.dtype) -> bool
return _supports_kernel_probe(_replicate_padding_op, device.type, dtype)
+def _replicate_padding_3d_op(device_type: str, dtype: torch.dtype) -> None:
+ F.pad(_probe_zeros(device_type, dtype, 1, 1, 2, 2, 2), (1, 1, 1, 1, 1, 1), mode="replicate")
+
+
+def supports_replicate_padding_3d(device: torch.device, dtype: torch.dtype) -> bool:
+ """Whether this device has a 3D ``mode="replicate"`` pad kernel (``replication_pad3d``) for ``dtype``.
+
+ :func:`kornia.filters.filter3d` and :func:`kornia.filters.motion_blur3d` pad that way by default.
+ torch 2.5.1 has no float16 CPU ``replication_pad3d`` (bfloat16 is fine). Probed at runtime and cached per
+ (device type, dtype), so it auto-enables once PyTorch fills the kernel in.
+ """
+ return _supports_kernel_probe(_replicate_padding_3d_op, device.type, dtype)
+
+
def _conv2d_op(device_type: str, dtype: torch.dtype) -> None:
F.conv2d(_probe_zeros(device_type, dtype, 1, 1, 3, 3), _probe_zeros(device_type, dtype, 1, 1, 2, 2))
diff --git a/tests/augmentation/test_conventions_intensity_ops.py b/tests/augmentation/test_conventions_intensity_ops.py
index eaf9cd74b..42fdb480e 100644
--- a/tests/augmentation/test_conventions_intensity_ops.py
+++ b/tests/augmentation/test_conventions_intensity_ops.py
@@ -319,7 +319,7 @@ class TestBlurConventions(BaseTester):
self.assert_close(taps[0.0], image.new_tensor([0.1, 0.2, 0.3, 0.4]))
self.assert_close(taps[45.0], image.new_tensor([1.0, 2.0, 3.0]) / 6.0)
- # The class keeps kornia.filters.motion_blur's defaults.
+ # The class's resample="nearest" is motion_blur's mode="nearest", but it zero-pads by its own default, not reflect.
@pytest.mark.device_agnostic
def test_convention_random_motion_blur_defaults_are_constant_and_nearest(self):
aug = K.RandomMotionBlur(3, (45.0, 45.0), (0.0, 0.0), p=1.0)
diff --git a/tests/augmentation/test_motionblur.py b/tests/augmentation/test_motionblur.py
index f85db7a5a..99f3fd40d 100644
--- a/tests/augmentation/test_motionblur.py
+++ b/tests/augmentation/test_motionblur.py
@@ -90,7 +90,11 @@ class TestRandomMotionBlur(BaseTester):
self.assert_close(aug._params[key], value, rtol=0, atol=0)
self.assert_close(aug(image, params=params), output, rtol=0, atol=0)
blurred = motion_blur(
- image, int(params["ksize_factor"][params["idx"][0]]), params["angle_factor"], params["direction_factor"]
+ image,
+ int(params["ksize_factor"][params["idx"][0]]),
+ params["angle_factor"],
+ params["direction_factor"],
+ border_type=aug.flags["border_type"].name.lower(),
)
expected = torch.where(params["batch_prob"].to(device=device).view(-1, 1, 1, 1) > 0.5, blurred, image)
self.assert_close(output, expected)
@@ -102,7 +106,8 @@ class TestRandomMotionBlur(BaseTester):
params = aug.forward_parameters(image.shape)
params["ksize_factor"] = torch.tensor([3, 5, 7], dtype=torch.int32)
params["idx"] = torch.tensor([1])
- expected = motion_blur(image, 5, params["angle_factor"], params["direction_factor"])
+ border_type = aug.flags["border_type"].name.lower()
+ expected = motion_blur(image, 5, params["angle_factor"], params["direction_factor"], border_type=border_type)
self.assert_close(aug(image, params=params), expected)
@pytest.mark.slow
@@ -124,6 +129,17 @@ class TestRandomMotionBlur(BaseTester):
fast_mode=False,
)
+ # The class zero-pads by its own default, although ``motion_blur`` now defaults to "reflect".
+ # Snippet used to generate expected:
+ # import torch, kornia.augmentation as K
+ # print(K.RandomMotionBlur(5, (0.0, 0.0), (0.0, 0.0), p=1.0)(torch.ones(1, 1, 5, 7))[0, 0, 2].tolist())
+ # -> [0.6, 0.8, 1.0, 1.0, 1.0, 0.8, 0.6]
+ def test_default_border_still_zero_pads(self, device, dtype):
+ image = torch.ones(1, 1, 5, 7, device=device, dtype=dtype)
+ row = torch.tensor([0.6, 0.8, 1.0, 1.0, 1.0, 0.8, 0.6], device=device, dtype=dtype)
+
+ self.assert_close(RandomMotionBlur(5, (0.0, 0.0), (0.0, 0.0), p=1.0)(image), row.expand(1, 1, 5, 7))
+
def test_dynamo(self, device, dtype, torch_optimizer):
# A fixed (int) kernel size avoids the data-dependent kernel-size selection and
# is torch.compile fullgraph-safe.
@@ -190,6 +206,18 @@ class TestRandomMotionBlur3D(BaseTester):
assert output.shape == input.shape
+ # The class zero-pads by its own default, although ``motion_blur3d`` now defaults to "replicate".
+ # Snippet used to generate expected:
+ # import torch, kornia.augmentation as K
+ # print(K.RandomMotionBlur3D(3, (0.0, 0.0, 0.0), 0.0, p=1.0)(torch.ones(1, 1, 5, 6, 7))[0, 0, 2, 2].tolist())
+ # -> [0.6667, 1.0, 1.0, 1.0, 1.0, 1.0, 0.6667]
+ def test_default_border_still_zero_pads(self, device, dtype):
+ volume = torch.ones(1, 1, 5, 6, 7, device=device, dtype=dtype)
+ row = torch.tensor([2.0 / 3.0, 1.0, 1.0, 1.0, 1.0, 1.0, 2.0 / 3.0], device=device, dtype=dtype)
+
+ actual = RandomMotionBlur3D(3, (0.0, 0.0, 0.0), 0.0, p=1.0)(volume)
+ self.assert_close(actual, row.expand(1, 1, 5, 6, 7))
+
@pytest.mark.slow
def test_gradcheck(self, device):
torch.manual_seed(0) # for random reproductibility
diff --git a/tests/filters/test_motion.py b/tests/filters/test_motion.py
index 208f161c2..ebb60584e 100644
--- a/tests/filters/test_motion.py
+++ b/tests/filters/test_motion.py
@@ -15,19 +15,51 @@
# limitations under the License.
#
+import inspect
+
import pytest
import torch
from kornia.filters import (
MotionBlur,
MotionBlur3D,
+ box_blur,
+ filter2d,
+ filter3d,
+ gaussian_blur2d,
get_motion_kernel2d,
get_motion_kernel3d,
motion_blur,
motion_blur3d,
)
-from testing.base import BaseTester, supports_bilinear_3d_grid_sample, supports_nearest_3d_grid_sample
+from testing.base import (
+ BaseTester,
+ supports_bilinear_3d_grid_sample,
+ supports_nearest_3d_grid_sample,
+ supports_reflect_padding,
+ supports_replicate_padding_3d,
+)
+
+
+def _default_border_type(op) -> str:
+ return inspect.signature(op).parameters["border_type"].default
+
+
+# The motion filters share their sibling's default border: the 2-D ones that of ``filter2d``, ``gaussian_blur2d`` and
+# ``box_blur`` ("reflect"), the 3-D ones that of ``filter3d`` ("replicate").
+ "op, siblings",
+ [
+ (motion_blur, (filter2d, gaussian_blur2d, box_blur)),
+ (MotionBlur, (filter2d, gaussian_blur2d, box_blur)),
+ (motion_blur3d, (filter3d,)),
+ (MotionBlur3D, (filter3d,)),
+ ],
+)
+def test_default_border_type_matches_the_sibling_filters(op, siblings):
+ assert {_default_border_type(op)} == {_default_border_type(sibling) for sibling in siblings}
class TestMotionBlur(BaseTester):
@@ -51,6 +83,8 @@ class TestMotionBlur(BaseTester):
@pytest.mark.parametrize("shape", [(1, 4, 8, 15), (2, 3, 11, 7)])
def test_cardinality(self, shape, device, dtype):
+ if not supports_reflect_padding(device, dtype):
+ pytest.skip("reflection_pad2d is unavailable for this device/dtype")
ksize = 5
angle = 200.0
direction = 0.3
@@ -103,6 +137,8 @@ class TestMotionBlur(BaseTester):
get_motion_kernel2d(3, angle, direction)
def test_noncontiguous(self, device, dtype):
+ if not supports_reflect_padding(device, dtype):
+ pytest.skip("reflection_pad2d is unavailable for this device/dtype")
batch_size = 3
inp = torch.rand(3, 5, 5, device=device, dtype=dtype).expand(batch_size, -1, -1, -1)
@@ -122,6 +158,8 @@ class TestMotionBlur(BaseTester):
self.gradcheck(motion_blur, (sample, ksize, angle, direction, "replicate"), nondet_tol=1e-8)
def test_module(self, device, dtype):
+ if not supports_reflect_padding(device, dtype):
+ pytest.skip("reflection_pad2d is unavailable for this device/dtype")
params = [3, 20.0, 0.5]
op = motion_blur
op_module = MotionBlur(*params)
@@ -129,6 +167,57 @@ class TestMotionBlur(BaseTester):
self.assert_close(op(img, *params), op_module(img))
+ # A blur of a constant image is that constant. ``(1, 1, 3, 9)`` and ``(1, 1, 9, 3)`` are shorter than the
+ # 5-tap kernel along one axis.
+ @pytest.mark.parametrize("shape", [(1, 1, 5, 7), (2, 3, 8, 6), (1, 1, 3, 9), (1, 1, 9, 3)])
+ @pytest.mark.parametrize("angle", [0.0, 45.0, 90.0, 30.0])
+ @pytest.mark.parametrize("direction", [0.0, 0.7])
+ def test_constant_image_stays_constant_at_the_default_border(self, shape, angle, direction, device, dtype):
+ if not supports_reflect_padding(device, dtype):
+ pytest.skip("reflection_pad2d is unavailable for this device/dtype")
+ image = torch.full(shape, 0.75, device=device, dtype=dtype)
+
+ self.assert_close(motion_blur(image, 5, angle, direction), image)
+ self.assert_close(MotionBlur(5, angle, direction)(image), image)
+
+ # "reflect" needs each spatial axis longer than the kernel radius (``kernel_size // 2 = 2`` here), as it does for
+ # ``filter2d``, ``gaussian_blur2d`` and ``box_blur`` at their defaults; "constant" and "replicate" do not.
+ @pytest.mark.parametrize("shape", [(1, 1, 2, 9), (1, 1, 9, 2), (1, 1, 1, 9)])
+ def test_default_border_raises_on_an_axis_of_at_most_the_kernel_radius(self, shape, device, dtype):
+ if not supports_reflect_padding(device, dtype):
+ pytest.skip("reflection_pad2d is unavailable for this device/dtype")
+ image = torch.ones(shape, device=device, dtype=dtype)
+
+ with pytest.raises(RuntimeError, match="Padding size should be less than"):
+ motion_blur(image, 5, 0.0, 0.0)
+ with pytest.raises(RuntimeError, match="Padding size should be less than"):
+ MotionBlur(5, 0.0, 0.0)(image)
+ assert motion_blur(image, 5, 0.0, 0.0, border_type="constant").shape == shape
+
+ # Snippet used to generate expected:
+ # import torch, kornia.filters as KF
+ # print(KF.motion_blur(torch.ones(1, 1, 5, 7), 5, 0.0, 0.0, "constant")[0, 0, 2].tolist())
+ # -> [0.6, 0.8, 1.0, 1.0, 1.0, 0.8, 0.6] (zero padding darkens every pixel the kernel reaches past the edge)
+ def test_explicit_constant_border_zero_pads(self, device, dtype):
+ image = torch.ones(1, 1, 5, 7, device=device, dtype=dtype)
+ row = torch.tensor([0.6, 0.8, 1.0, 1.0, 1.0, 0.8, 0.6], device=device, dtype=dtype)
+ expected = row.expand(1, 1, 5, 7)
+
+ self.assert_close(motion_blur(image, 5, 0.0, 0.0, border_type="constant"), expected)
+ self.assert_close(MotionBlur(5, 0.0, 0.0, border_type="constant")(image), expected)
+
+ def test_function_and_module_agree_at_their_defaults(self, device, dtype):
+ if not supports_reflect_padding(device, dtype):
+ pytest.skip("reflection_pad2d is unavailable for this device/dtype")
+ torch.manual_seed(0)
+ image = torch.rand(2, 3, 9, 11, device=device, dtype=dtype)
+ params = (5, 35.0, 0.5)
+
+ from_function = motion_blur(image, *params)
+ self.assert_close(MotionBlur(*params)(image), from_function)
+ # the shared default is "reflect"
+ self.assert_close(from_function, motion_blur(image, *params, border_type="reflect"))
+
@pytest.mark.skip(reason="After the op be optimized the results are not the same")
@pytest.mark.parametrize("batch_size", [1, 2])
def test_dynamo(self, batch_size, device, dtype, torch_optimizer):
@@ -169,6 +258,8 @@ class TestMotionBlur3D(BaseTester):
@pytest.mark.parametrize("shape", [(1, 4, 1, 8, 15), (2, 3, 1, 11, 7)])
def test_cardinality(self, shape, device, dtype):
+ if not supports_replicate_padding_3d(device, dtype):
+ pytest.skip("replication_pad3d is unavailable for this device/dtype")
ksize = 5
angle = (200.0, 15.0, 120.0)
direction = 0.3
@@ -206,6 +297,8 @@ class TestMotionBlur3D(BaseTester):
self.assert_close(actual.sum(), expected.sum())
def test_noncontiguous(self, device, dtype):
+ if not supports_replicate_padding_3d(device, dtype):
+ pytest.skip("replication_pad3d is unavailable for this device/dtype")
batch_size = 3
inp = torch.rand(3, 1, 5, 5, device=device, dtype=dtype).expand(batch_size, -1, -1, -1, -1)
@@ -225,6 +318,8 @@ class TestMotionBlur3D(BaseTester):
self.gradcheck(motion_blur3d, (sample, ksize, angle, direction, "replicate"), nondet_tol=1e-8)
def test_module(self, device, dtype):
+ if not supports_replicate_padding_3d(device, dtype):
+ pytest.skip("replication_pad3d is unavailable for this device/dtype")
params = [3, (0.0, 360.0, 150.0), 0.5]
op = motion_blur3d
op_module = MotionBlur3D(*params)
@@ -241,3 +336,43 @@ class TestMotionBlur3D(BaseTester):
op_optimized = torch_optimizer(op)
self.assert_close(op(data), op_optimized(data))
+
+ # A blur of a constant volume is that constant, thin volumes included: ``D = 1`` is why the default is
+ # "replicate" and not "reflect", which needs each axis longer than the kernel radius. "replicate" has no such
+ # limit, down to a single voxel, so unlike the 2-D default there is no size at which this one raises.
+ @pytest.mark.parametrize(
+ "shape", [(1, 1, 5, 6, 7), (2, 2, 3, 4, 5), (1, 1, 1, 5, 7), (2, 3, 1, 8, 9), (1, 1, 1, 1, 1)]
+ )
+ @pytest.mark.parametrize("angle", [(0.0, 0.0, 0.0), (45.0, 0.0, 0.0), (0.0, 90.0, 0.0), (30.0, 60.0, 120.0)])
+ @pytest.mark.parametrize("direction", [0.0, 0.7])
+ def test_constant_volume_stays_constant_at_the_default_border(self, shape, angle, direction, device, dtype):
+ if not supports_replicate_padding_3d(device, dtype):
+ pytest.skip("replication_pad3d is unavailable for this device/dtype")
+ volume = torch.full(shape, 0.75, device=device, dtype=dtype)
+
+ self.assert_close(motion_blur3d(volume, 3, angle, direction), volume)
+ self.assert_close(MotionBlur3D(3, angle, direction)(volume), volume)
+
+ # Snippet used to generate expected:
+ # import torch, kornia.filters as KF
+ # print(KF.motion_blur3d(torch.ones(1, 1, 5, 6, 7), 3, (0.0, 0.0, 0.0), 0.0, "constant")[0, 0, 2, 2].tolist())
+ # -> [0.6667, 1.0, 1.0, 1.0, 1.0, 1.0, 0.6667] (the k = 3 kernel at angle 0 runs along the last axis)
+ def test_explicit_constant_border_zero_pads(self, device, dtype):
+ volume = torch.ones(1, 1, 5, 6, 7, device=device, dtype=dtype)
+ row = torch.tensor([2.0 / 3.0, 1.0, 1.0, 1.0, 1.0, 1.0, 2.0 / 3.0], device=device, dtype=dtype)
+ expected = row.expand(1, 1, 5, 6, 7)
+
+ self.assert_close(motion_blur3d(volume, 3, (0.0, 0.0, 0.0), 0.0, border_type="constant"), expected)
+ self.assert_close(MotionBlur3D(3, (0.0, 0.0, 0.0), 0.0, border_type="constant")(volume), expected)
+
+ def test_function_and_module_agree_at_their_defaults(self, device, dtype):
+ if not supports_replicate_padding_3d(device, dtype):
+ pytest.skip("replication_pad3d is unavailable for this device/dtype")
+ torch.manual_seed(0)
+ volume = torch.rand(2, 2, 4, 6, 7, device=device, dtype=dtype)
+ params = (3, (30.0, 10.0, 20.0), 0.5)
+
+ from_function = motion_blur3d(volume, *params)
+ self.assert_close(MotionBlur3D(*params)(volume), from_function)
+ # the shared default is "replicate"
+ self.assert_close(from_function, motion_blur3d(volume, *params, border_type="replicate"))