π Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
fix(filters): reshape the input of filter2d and filter3d so a per-sample kernel accepts any memory layout (#5301)
εΊζ’ηe0eda1275302
diff --git a/changelog.d/5301.fixed.md b/changelog.d/5301.fixed.mdnew file mode 100644index 000000000..773a25ec9--- /dev/null+++ b/changelog.d/5301.fixed.md@@ -0,0 +1,6 @@+`filter2d` and `filter3d` with one kernel per sample now accept an input whose batch and channel axes cannot be merged+without a copy (#5292). Previously a channels-last input raised torch's "view size is not compatible with input+tensor's size and stride" when the batch and channel axes were merged for the grouped convolution, and with+`padding='valid'` so did a `filter2d` input whose batch and channel axes are swapped in memory or that is expanded+along the batch. A shared kernel and a contiguous input were unaffected, as was `fft_conv`. The error reached+`gaussian_blur2d` with a per-sample `sigma` on a channels-last image. The output values do not change.diff --git a/kornia/filters/filter.py b/kornia/filters/filter.pyindex f09cfd1f3..0ab1353a0 100644--- a/kornia/filters/filter.py+++ b/kornia/filters/filter.py@@ -167,7 +167,7 @@ def filter2d(# kernel and input tensor reshape to align element-wise or batch-wise paramstmp_kernel = tmp_kernel.reshape(-1, 1, height, width)- input = input.view(-1, tmp_kernel.size(0), input.size(-2), input.size(-1))+ input = input.reshape(-1, tmp_kernel.size(0), input.size(-2), input.size(-1))# convolve the tensor with the kernel.output = F.conv2d(input, tmp_kernel, groups=tmp_kernel.size(0), padding=0, stride=1)@@ -350,7 +350,7 @@ def filter3d(# kernel and input tensor reshape to align element-wise or batch-wise paramstmp_kernel = tmp_kernel.reshape(-1, 1, depth, height, width)- input_pad = input_pad.view(-1, tmp_kernel.size(0), input_pad.size(-3), input_pad.size(-2), input_pad.size(-1))+ input_pad = input_pad.reshape(-1, tmp_kernel.size(0), input_pad.size(-3), input_pad.size(-2), input_pad.size(-1))# convolve the tensor with the kernel.output = F.conv3d(input_pad, tmp_kernel, groups=tmp_kernel.size(0), padding=0, stride=1)diff --git a/tests/filters/test_filters.py b/tests/filters/test_filters.pyindex b197f4f2f..b18cc7ab8 100644--- a/tests/filters/test_filters.py+++ b/tests/filters/test_filters.py@@ -393,6 +393,25 @@ class TestFilter2D(BaseTester):actual = filter2d(inp, kernel, padding=padding)assert actual.is_contiguous()+ @pytest.mark.parametrize("padding", ["same", "valid"])+ @pytest.mark.parametrize("layout", ["channels_last", "batch_transposed", "expanded_batch"])+ def test_per_sample_kernel_on_a_non_contiguous_input(self, layout, padding, device, dtype):+ """One kernel per sample merges the batch and channel axes, whatever the input strides are."""+ kernel = torch.rand(2, 3, 4, device=device, dtype=dtype)+ if layout == "channels_last":+ inp = torch.rand(2, 3, 6, 7, device=device, dtype=dtype).contiguous(memory_format=torch.channels_last)+ elif layout == "batch_transposed":+ inp = torch.rand(3, 2, 6, 7, device=device, dtype=dtype).transpose(0, 1)+ else:+ inp = torch.rand(1, 3, 6, 7, device=device, dtype=dtype).expand(2, -1, -1, -1)+ assert not inp.is_contiguous()++ actual = filter2d(inp, kernel, padding=padding)+ self.assert_close(actual, filter2d(inp.contiguous(), kernel, padding=padding))+ for i in range(2): # sample i is filtered with kernel i+ expected = filter2d(inp[i : i + 1].contiguous(), kernel[i : i + 1], padding=padding)+ self.assert_close(actual[i : i + 1], expected)+@pytest.mark.parametrize("padding", ["same", "valid"])def test_separable(self, padding, device, dtype):batch_size = 3@@ -763,6 +782,23 @@ class TestFilter3D(BaseTester):actual = filter3d(inp, kernel)assert actual.is_contiguous()+ @pytest.mark.parametrize("layout", ["channels_last_3d", "batch_transposed", "expanded_batch"])+ def test_per_sample_kernel_on_a_non_contiguous_input(self, layout, device, dtype):+ """One kernel per sample merges the batch and channel axes, whatever the input strides are."""+ kernel = torch.rand(2, 2, 3, 4, device=device, dtype=dtype)+ if layout == "channels_last_3d":+ inp = torch.rand(2, 3, 4, 6, 7, device=device, dtype=dtype).contiguous(memory_format=torch.channels_last_3d)+ elif layout == "batch_transposed":+ inp = torch.rand(3, 2, 4, 6, 7, device=device, dtype=dtype).transpose(0, 1)+ else:+ inp = torch.rand(1, 3, 4, 6, 7, device=device, dtype=dtype).expand(2, -1, -1, -1, -1)+ assert not inp.is_contiguous()++ actual = filter3d(inp, kernel)+ self.assert_close(actual, filter3d(inp.contiguous(), kernel))+ for i in range(2): # sample i is filtered with kernel i+ self.assert_close(actual[i : i + 1], filter3d(inp[i : i + 1].contiguous(), kernel[i : i + 1]))+@pytest.mark.parametrize("kernel_batch", [1, 2])@pytest.mark.parametrize("normalized", [True, False])@pytest.mark.parametrize("behaviour", ["corr", "conv"])