Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

Add more border_type options for filter2D (#1375)

Sravan Chittupallia66610d5402f
diff --git a/kornia/filters/filter.py b/kornia/filters/filter.py
index 15d2f2e53..051469630 100644
--- a/kornia/filters/filter.py
+++ b/kornia/filters/filter.py
@@ -31,7 +31,8 @@ def _compute_padding(kernel_size: List[int]) -> List[int]:
def filter2d(
- input: torch.Tensor, kernel: torch.Tensor, border_type: str = 'reflect', normalized: bool = False
+ input: torch.Tensor, kernel: torch.Tensor, border_type: str = 'reflect', normalized: bool = False,
+ padding: str = 'same'
) -> torch.Tensor:
r"""Convolve a tensor with a 2d kernel.
@@ -49,6 +50,8 @@ def filter2d(
The expected modes are: ``'constant'``, ``'reflect'``,
``'replicate'`` or ``'circular'``.
normalized: If True, kernel will be L1 normalized.
+ padding: This defines the type of padding.
+ 2 modes available ``'same'`` or ``'valid'``.
Return:
torch.Tensor: the convolved tensor of same size and numbers of channels
@@ -62,7 +65,7 @@ def filter2d(
... [0., 0., 0., 0., 0.],
... [0., 0., 0., 0., 0.],]]])
>>> kernel = torch.ones(1, 3, 3)
- >>> filter2d(input, kernel)
+ >>> filter2d(input, kernel, padding='same')
tensor([[[[0., 0., 0., 0., 0.],
[0., 5., 5., 5., 0.],
[0., 5., 5., 5., 0.],
@@ -78,6 +81,12 @@ def filter2d(
if not isinstance(border_type, str):
raise TypeError(f"Input border_type is not string. Got {type(kernel)}")
+ if not isinstance(padding, str):
+ raise TypeError(f"Input padding is not string. Got {type(padding)}")
+
+ if padding not in ['valid', 'same']:
+ raise ValueError(f"Invalid padding mode, we expect 'valid' or 'same'. Got: {padding}")
+
if not len(input.shape) == 4:
raise ValueError(f"Invalid input shape, we expect BxCxHxW. Got: {input.shape}")
@@ -93,19 +102,26 @@ def filter2d(
tmp_kernel = tmp_kernel.expand(-1, c, -1, -1)
- # pad the input tensor
height, width = tmp_kernel.shape[-2:]
- padding_shape: List[int] = _compute_padding([height, width])
- input_pad: torch.Tensor = F.pad(input, padding_shape, mode=border_type)
+
+ # pad the input tensor
+ if padding == 'same':
+ padding_shape: List[int] = _compute_padding([height, width])
+ input = F.pad(input, padding_shape, mode=border_type)
# kernel and input tensor reshape to align element-wise or batch-wise params
tmp_kernel = tmp_kernel.reshape(-1, 1, height, width)
- input_pad = input_pad.view(-1, tmp_kernel.size(0), input_pad.size(-2), input_pad.size(-1))
+ input = input.view(-1, tmp_kernel.size(0), input.size(-2), input.size(-1))
# convolve the tensor with the kernel.
- output = F.conv2d(input_pad, tmp_kernel, groups=tmp_kernel.size(0), padding=0, stride=1)
+ output = F.conv2d(input, tmp_kernel, groups=tmp_kernel.size(0), padding=0, stride=1)
+
+ if padding == 'same':
+ out = output.view(b, c, h, w)
+ else:
+ out = output.view(b, c, h - height + 1, w - width + 1)
- return output.view(b, c, h, w)
+ return out
def filter3d(
diff --git a/test/filters/test_filters.py b/test/filters/test_filters.py
index 23bdb3565..715830012 100644
--- a/test/filters/test_filters.py
+++ b/test/filters/test_filters.py
@@ -8,19 +8,36 @@ from kornia.testing import assert_close
class TestFilter2D:
- def test_smoke(self, device, dtype):
+ @pytest.mark.parametrize("padding", ["same", "valid"])
+ def test_smoke(self, padding, device, dtype):
kernel = torch.rand(1, 3, 3, device=device, dtype=dtype)
+ _, height, width = kernel.shape
input = torch.ones(1, 1, 7, 8, device=device, dtype=dtype)
- assert kornia.filter2d(input, kernel).shape == input.shape
+ b, c, h, w = input.shape
+ if padding == 'same':
+ out = kornia.filter2d(input, kernel, padding=padding)
+ assert out.shape == (b, c, h, w)
+ else:
+ out = kornia.filter2d(input, kernel, padding=padding)
+ assert out.shape == (b, c, h - height + 1, w - width + 1)
@pytest.mark.parametrize("batch_size", [2, 3, 6, 8])
- def test_batch(self, batch_size, device, dtype):
+ @pytest.mark.parametrize("padding", ["same", "valid"])
+ def test_batch(self, batch_size, padding, device, dtype):
B: int = batch_size
kernel = torch.rand(1, 3, 3, device=device, dtype=dtype)
+ _, height, width = kernel.shape
input = torch.ones(B, 3, 7, 8, device=device, dtype=dtype)
- assert kornia.filter2d(input, kernel).shape == input.shape
-
- def test_mean_filter(self, device, dtype):
+ b, c, h, w = input.shape
+ if padding == 'same':
+ out = kornia.filter2d(input, kernel, padding=padding)
+ assert out.shape == (b, c, h, w)
+ else:
+ out = kornia.filter2d(input, kernel, padding=padding)
+ assert out.shape == (b, c, h - height + 1, w - width + 1)
+
+ @pytest.mark.parametrize("padding", ["same", "valid"])
+ def test_mean_filter(self, padding, device, dtype):
kernel = torch.ones(1, 3, 3, device=device, dtype=dtype)
input = torch.tensor(
[
@@ -37,7 +54,7 @@ class TestFilter2D:
device=device,
dtype=dtype,
)
- expected = torch.tensor(
+ expected_same = torch.tensor(
[
[
[
@@ -53,10 +70,28 @@ class TestFilter2D:
dtype=dtype,
)
- actual = kornia.filter2d(input, kernel)
- assert_close(actual, expected)
+ expected_valid = torch.tensor(
+ [
+ [
+ [
+ [5.0, 5.0, 5.0],
+ [5.0, 5.0, 5.0],
+ [5.0, 5.0, 5.0],
+ ]
+ ]
+ ],
+ device=device,
+ dtype=dtype,
+ )
- def test_mean_filter_2batch_2ch(self, device, dtype):
+ actual = kornia.filter2d(input, kernel, padding=padding)
+ if padding == 'same':
+ assert_close(actual, expected_same)
+ else:
+ assert_close(actual, expected_valid)
+
+ @pytest.mark.parametrize("padding", ["same", "valid"])
+ def test_mean_filter_2batch_2ch(self, padding, device, dtype):
kernel = torch.ones(1, 3, 3, device=device, dtype=dtype)
input = torch.tensor(
[
@@ -74,7 +109,7 @@ class TestFilter2D:
dtype=dtype,
).expand(2, 2, -1, -1)
- expected = torch.tensor(
+ expected_same = torch.tensor(
[
[
[
@@ -90,10 +125,28 @@ class TestFilter2D:
dtype=dtype,
).expand(2, 2, -1, -1)
- actual = kornia.filter2d(input, kernel)
- assert_close(actual, expected)
+ expected_valid = torch.tensor(
+ [
+ [
+ [
+ [5.0, 5.0, 5.0],
+ [5.0, 5.0, 5.0],
+ [5.0, 5.0, 5.0],
+ ]
+ ]
+ ],
+ device=device,
+ dtype=dtype,
+ ).expand(2, 2, -1, -1)
- def test_normalized_mean_filter(self, device, dtype):
+ actual = kornia.filter2d(input, kernel, padding=padding)
+ if padding == 'same':
+ assert_close(actual, expected_same)
+ else:
+ assert_close(actual, expected_valid)
+
+ @pytest.mark.parametrize("padding", ["same", "valid"])
+ def test_normalized_mean_filter(self, padding, device, dtype):
kernel = torch.ones(1, 3, 3).to(device)
input = torch.tensor(
[
@@ -112,7 +165,7 @@ class TestFilter2D:
).expand(2, 2, -1, -1)
nv: float = 5.0 / 9 # normalization value
- expected = torch.tensor(
+ expected_same = torch.tensor(
[
[
[
@@ -128,12 +181,30 @@ class TestFilter2D:
dtype=dtype,
).expand(2, 2, -1, -1)
- actual = kornia.filter2d(input, kernel, normalized=True)
+ expected_valid = torch.tensor(
+ [
+ [
+ [
+ [nv, nv, nv],
+ [nv, nv, nv],
+ [nv, nv, nv],
+ ]
+ ]
+ ],
+ device=device,
+ dtype=dtype,
+ ).expand(2, 2, -1, -1)
+
+ actual = kornia.filter2d(input, kernel, normalized=True, padding=padding)
tol_val: float = utils._get_precision_by_name(device, 'xla', 1e-1, 1e-4)
- assert_close(actual, expected, rtol=tol_val, atol=tol_val)
+ if padding == 'same':
+ assert_close(actual, expected_same, rtol=tol_val, atol=tol_val)
+ else:
+ assert_close(actual, expected_valid, rtol=tol_val, atol=tol_val)
- def test_even_sized_filter(self, device, dtype):
+ @pytest.mark.parametrize("padding", ["same", "valid"])
+ def test_even_sized_filter(self, padding, device, dtype):
kernel = torch.ones(1, 2, 2, device=device, dtype=dtype)
input = torch.tensor(
[
@@ -151,7 +222,7 @@ class TestFilter2D:
dtype=dtype,
)
- expected = torch.tensor(
+ expected_same = torch.tensor(
[
[
[
@@ -167,15 +238,34 @@ class TestFilter2D:
dtype=dtype,
)
- actual = kornia.filter2d(input, kernel)
- assert_close(actual, expected)
+ expected_valid = torch.tensor(
+ [
+ [
+ [
+ [0.0, 0.0, 0.0, 0.0],
+ [0.0, 5.0, 5.0, 0.0],
+ [0.0, 5.0, 5.0, 0.0],
+ [0.0, 0.0, 0.0, 0.0],
+ ]
+ ]
+ ],
+ device=device,
+ dtype=dtype,
+ )
- def test_noncontiguous(self, device, dtype):
+ actual = kornia.filter2d(input, kernel, padding=padding)
+ if padding == 'same':
+ assert_close(actual, expected_same)
+ else:
+ assert_close(actual, expected_valid)
+
+ @pytest.mark.parametrize("padding", ["same", "valid"])
+ def test_noncontiguous(self, padding, device, dtype):
batch_size = 3
inp = torch.rand(3, 5, 5, device=device, dtype=dtype).expand(batch_size, -1, -1, -1)
kernel = torch.ones(1, 2, 2, device=device, dtype=dtype)
- actual = kornia.filter2d(inp, kernel)
+ actual = kornia.filter2d(inp, kernel, padding=padding)
assert_close(actual, actual)
def test_gradcheck(self, device):
@@ -187,14 +277,15 @@ class TestFilter2D:
kernel = utils.tensor_to_gradcheck_var(kernel) # to var
assert gradcheck(kornia.filter2d, (input, kernel), raise_exception=True)
- def test_jit(self, device, dtype):
+ @pytest.mark.parametrize("padding", ["same", "valid"])
+ def test_jit(self, padding, device, dtype):
op = kornia.filter2d
op_script = torch.jit.script(op)
kernel = torch.rand(1, 3, 3, device=device, dtype=dtype)
input = torch.ones(1, 1, 7, 8, device=device, dtype=dtype)
- expected = op(input, kernel)
- actual = op_script(input, kernel)
+ expected = op(input, kernel, padding=padding)
+ actual = op_script(input, kernel, padding=padding)
assert_close(actual, expected)