Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

added convolution/correlation switch to filter2d (#2271)

Dmytro Mishkinee13c8ea519f
diff --git a/kornia/filters/filter.py b/kornia/filters/filter.py
index 755368999..0df46c217 100644
--- a/kornia/filters/filter.py
+++ b/kornia/filters/filter.py
@@ -9,6 +9,7 @@ from .kernels import normalize_kernel2d
_VALID_BORDERS = {'constant', 'reflect', 'replicate', 'circular'}
_VALID_PADDING = {'valid', 'same'}
+_VALID_BEHAVIOUR = {'conv', 'corr'}
def _compute_padding(kernel_size: list[int]) -> list[int]:
@@ -35,7 +36,12 @@ def _compute_padding(kernel_size: list[int]) -> list[int]:
def filter2d(
- input: Tensor, kernel: Tensor, border_type: str = 'reflect', normalized: bool = False, padding: str = 'same'
+ input: Tensor,
+ kernel: Tensor,
+ border_type: str = 'reflect',
+ normalized: bool = False,
+ padding: str = 'same',
+ behaviour: str = 'corr',
) -> Tensor:
r"""Convolve a tensor with a 2d kernel.
@@ -55,6 +61,9 @@ def filter2d(
normalized: If True, kernel will be L1 normalized.
padding: This defines the type of padding.
2 modes available ``'same'`` or ``'valid'``.
+ behaviour: defines the convolution mode -- correlation (default), using pytorch conv2d,
+ or true convolution (kernel is flipped). 2 modes available ``'corr'`` or ``'conv'``.
+
Return:
Tensor: the convolved tensor of same size and numbers of channels
@@ -88,10 +97,17 @@ def filter2d(
str(padding).lower() in _VALID_PADDING,
f'Invalid padding mode, gotcha {padding}. Expected one of {_VALID_PADDING}',
)
-
+ KORNIA_CHECK(
+ str(behaviour).lower() in _VALID_BEHAVIOUR,
+ f'Invalid padding mode, gotcha {behaviour}. Expected one of {_VALID_BEHAVIOUR}',
+ )
# prepare kernel
b, c, h, w = input.shape
- tmp_kernel = kernel[:, None, ...].to(device=input.device, dtype=input.dtype)
+ if str(behaviour).lower() == 'conv':
+ tmp_kernel = kernel.flip((-2, -1))[:, None, ...].to(device=input.device, dtype=input.dtype)
+ else:
+ tmp_kernel = kernel[:, None, ...].to(device=input.device, dtype=input.dtype)
+ # str(behaviour).lower() == 'conv':
if normalized:
tmp_kernel = normalize_kernel2d(tmp_kernel)
diff --git a/test/filters/test_filters.py b/test/filters/test_filters.py
index 017f33cf6..7ef94a372 100644
--- a/test/filters/test_filters.py
+++ b/test/filters/test_filters.py
@@ -34,6 +34,45 @@ class TestFilter2D(BaseTester):
else:
assert out.shape == (b, c, h - height + 1, w - width + 1)
+ def test_conv(self, device, dtype):
+ inp = torch.zeros(1, 1, 5, 5, device=device, dtype=dtype)
+ inp[..., 2, 2] = 1.0
+ kernel = torch.arange(1, 10).reshape(3, 3).to(device, dtype)[None]
+ corr_expected = torch.tensor(
+ [
+ [
+ [
+ [0.0, 0.0, 0.0, 0.0, 0.0],
+ [0.0, 9.0, 8.0, 7.0, 0.0],
+ [0.0, 6.0, 5.0, 4.0, 0.0],
+ [0.0, 3.0, 2.0, 1.0, 0.0],
+ [0.0, 0.0, 0.0, 0.0, 0.0],
+ ]
+ ]
+ ],
+ device=device,
+ dtype=dtype,
+ )
+ conv_expected = torch.tensor(
+ [
+ [
+ [
+ [0.0, 0.0, 0.0, 0.0, 0.0],
+ [0.0, 1.0, 2.0, 3.0, 0.0],
+ [0.0, 4.0, 5.0, 6.0, 0.0],
+ [0.0, 7.0, 8.0, 9.0, 0.0],
+ [0.0, 0.0, 0.0, 0.0, 0.0],
+ ]
+ ]
+ ],
+ device=device,
+ dtype=dtype,
+ )
+ out_corr = filter2d(inp, kernel, behaviour='corr')
+ assert_close(out_corr, corr_expected)
+ out_conv = filter2d(inp, kernel, behaviour='conv')
+ assert_close(out_conv, conv_expected)
+
def test_exception(self):
k = torch.ones(1, 1, 1)
inpt = torch.ones(1, 1, 1, 1)