Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

fix(filters): return a contiguous tensor from get_box_kernel1d and get_box_kernel2d (#5357)

εŒΊζ’“ηa8e1a0bb8728
diff --git a/changelog.d/5357.fixed.md b/changelog.d/5357.fixed.md
new file mode 100644
index 000000000..b7ce802f5
--- /dev/null
+++ b/changelog.d/5357.fixed.md
@@ -0,0 +1,4 @@
+`get_box_kernel1d` and `get_box_kernel2d` now return an ordinary contiguous tensor, so an in-place edit changes only
+the taps it addresses (#5160). Previously they returned a stride-0 view of one value: writing one tap rewrote every
+tap, and writing more than one raised torch's "more than one element of the written-to tensor refers to a single
+memory location" error. The kernel values and the `box_blur` results are unchanged.
diff --git a/kornia/filters/kernels.py b/kornia/filters/kernels.py
index 10394a183..bc85e4049 100644
--- a/kornia/filters/kernels.py
+++ b/kornia/filters/kernels.py
@@ -402,10 +402,9 @@ def get_box_kernel1d(
Convention:
- For a floating ``dtype`` every tap is ``1 / kernel_size``; an even ``kernel_size`` is accepted.
+ - The kernel is a contiguous tensor, so an in-place edit changes only the taps it addresses.
- Known defects:
- - the kernel is a stride-0 view of a single value, so writing one tap in place changes every tap
- (`#5160 <https://github.com/kornia/kornia/issues/5160>`_).
- an integer ``dtype`` truncates ``1 / kernel_size``, so every tap is 0 once ``kernel_size`` is above 1
(`#5155 <https://github.com/kornia/kornia/issues/5155>`_).
@@ -419,8 +418,7 @@ def get_box_kernel1d(
:math:`\frac{1}{\text{kernel\_size}}` for a floating ``dtype``.
"""
- scale = torch.tensor(1.0 / kernel_size, device=device, dtype=dtype)
- return scale.expand(1, kernel_size)
+ return torch.full((1, kernel_size), 1.0 / kernel_size, device=device, dtype=dtype)
def get_box_kernel2d(
@@ -443,8 +441,7 @@ def get_box_kernel2d(
"""
ky, kx = _unpack_2d_ks(kernel_size)
- scale = torch.tensor(1.0 / (kx * ky), device=device, dtype=dtype)
- return scale.expand(1, ky, kx)
+ return torch.full((1, ky, kx), 1.0 / (kx * ky), device=device, dtype=dtype)
def get_binary_kernel2d(
diff --git a/tests/filters/test_filters.py b/tests/filters/test_filters.py
index b18cc7ab8..9a0706fba 100644
--- a/tests/filters/test_filters.py
+++ b/tests/filters/test_filters.py
@@ -2236,11 +2236,20 @@ class TestConventionsKernels(BaseTester):
self.assert_close(from_float[0], on_cpu_f32[index])
@pytest.mark.parametrize("ndim", [1, 2])
- def test_wart_box_kernel_is_a_stride_zero_view_5160(self, ndim, device, dtype):
- """get_box_kernel1d/2d return an expanded view of one scalar, so writing one tap rewrites all (#5160)."""
+ def test_convention_box_kernel_is_a_contiguous_tensor_5160(self, ndim, device, dtype):
+ """get_box_kernel1d/2d return a contiguous tensor, so an in-place edit changes only its own taps (#5160)."""
if ndim == 1:
kernel = get_box_kernel1d(3, device=device, dtype=dtype)
+ expected = torch.full((1, 3), 1.0 / 3.0, device=device, dtype=dtype)
else:
kernel = get_box_kernel2d((3, 4), device=device, dtype=dtype)
+ expected = torch.full((1, 3, 4), 1.0 / 12.0, device=device, dtype=dtype)
+ assert kernel.is_contiguous()
+ self.assert_close(kernel, expected)
+ # zero the first tap, then the last column: one tap and several taps at once
kernel[(0,) * kernel.dim()] = 0.0
- assert bool((kernel == 0).all())
+ kernel[..., -1] = 0.0
+ expected[(0,) * kernel.dim()] = 0.0
+ expected[..., -1] = 0.0
+ self.assert_close(kernel, expected)
+ assert int((kernel == 0).sum()) == (2 if ndim == 1 else 4)