Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

Filter 2D->2d, 3D->3d (#1069)

Duc Nguyen76a64344b049
diff --git a/kornia/__init__.py b/kornia/__init__.py
index 8854e7aba..3fc6058c9 100644
--- a/kornia/__init__.py
+++ b/kornia/__init__.py
@@ -80,8 +80,8 @@ from kornia.filters import (
box_blur,
median_blur,
motion_blur,
- filter2D,
- filter3D,
+ filter2d,
+ filter3d,
unsharp_mask,
)
from kornia.losses import (
diff --git a/kornia/filters/__init__.py b/kornia/filters/__init__.py
index c99166529..f3177cc29 100644
--- a/kornia/filters/__init__.py
+++ b/kornia/filters/__init__.py
@@ -7,7 +7,7 @@ from .blur import BoxBlur, box_blur
from .blur_pool import MaxBlurPool2D, BlurPool2D, max_blur_pool2d, blur_pool2d
from .median import MedianBlur, median_blur
from .motion import MotionBlur, MotionBlur3D, motion_blur, motion_blur3d
-from .filter import filter2D, filter3D
+from .filter import filter2d, filter3d, filter2D, filter3D
from .unsharp import unsharp_mask, UnsharpMask
from .kernels import (
gaussian,
@@ -51,6 +51,8 @@ __all__ = [
"median_blur",
"motion_blur",
"motion_blur3d",
+ "filter2d",
+ "filter3d",
"filter2D",
"filter3D",
"GaussianBlur2d",
diff --git a/kornia/filters/__tmp__.py b/kornia/filters/__tmp__.py
new file mode 100644
index 000000000..a7c216248
--- /dev/null
+++ b/kornia/filters/__tmp__.py
@@ -0,0 +1,22 @@
+from typing import Callable
+
+import warnings
+from functools import wraps
+
+warnings.simplefilter('always', DeprecationWarning)
+
+
+def __deprecation_warning(name: str, replacement: str):
+ warnings.warn(
+ f"`{name}` will be renamed to `{replacement}` in the future versions. " f"Please use `{replacement}` instead.",
+ category=DeprecationWarning,
+ )
+
+
+def _deprecation_wrapper(new_func: Callable, old_func: str) -> Callable:
+ @wraps(new_func)
+ def wrapper(*args, **kwargs):
+ __deprecation_warning(old_func, new_func.__name__)
+ return new_func(*args, **kwargs)
+
+ return wrapper
diff --git a/kornia/filters/blur.py b/kornia/filters/blur.py
index 74fa25965..d454b6aa1 100644
--- a/kornia/filters/blur.py
+++ b/kornia/filters/blur.py
@@ -43,7 +43,7 @@ def box_blur(
kernel: torch.Tensor = get_box_kernel2d(kernel_size)
if normalized:
kernel = normalize_kernel2d(kernel)
- return kornia.filter2D(input, kernel, border_type)
+ return kornia.filter2d(input, kernel, border_type)
class BoxBlur(nn.Module):
diff --git a/kornia/filters/filter.py b/kornia/filters/filter.py
index bf610c19e..d83baeeb6 100644
--- a/kornia/filters/filter.py
+++ b/kornia/filters/filter.py
@@ -5,6 +5,7 @@ import torch.nn as nn
import torch.nn.functional as F
from kornia.filters.kernels import normalize_kernel2d
+from kornia.filters.__tmp__ import _deprecation_wrapper
def _compute_padding(kernel_size: List[int]) -> List[int]:
@@ -29,7 +30,7 @@ def _compute_padding(kernel_size: List[int]) -> List[int]:
return out_padding
-def filter2D(
+def filter2d(
input: torch.Tensor, kernel: torch.Tensor, border_type: str = 'reflect', normalized: bool = False
) -> torch.Tensor:
r"""Convolve a tensor with a 2d kernel.
@@ -61,7 +62,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)
tensor([[[[0., 0., 0., 0., 0.],
[0., 5., 5., 5., 0.],
[0., 5., 5., 5., 0.],
@@ -107,7 +108,7 @@ def filter2D(
return output.view(b, c, h, w)
-def filter3D(
+def filter3d(
input: torch.Tensor, kernel: torch.Tensor, border_type: str = 'replicate', normalized: bool = False
) -> torch.Tensor:
r"""Convolve a tensor with a 3d kernel.
@@ -150,7 +151,7 @@ def filter3D(
... [0., 0., 0., 0., 0.]]
... ]]])
>>> kernel = torch.ones(1, 3, 3, 3)
- >>> filter3D(input, kernel)
+ >>> filter3d(input, kernel)
tensor([[[[[0., 0., 0., 0., 0.],
[0., 5., 5., 5., 0.],
[0., 5., 5., 5., 0.],
@@ -207,3 +208,8 @@ def filter3D(
output = F.conv3d(input_pad, tmp_kernel, groups=tmp_kernel.size(0), padding=0, stride=1)
return output.view(b, c, d, h, w)
+
+
+# for backward compatibility.
+filter2D = _deprecation_wrapper(filter2d, 'filter2D')
+filter3D = _deprecation_wrapper(filter3d, 'filter3D')
diff --git a/kornia/filters/gaussian.py b/kornia/filters/gaussian.py
index 29e4b72f8..41e7ce52a 100644
--- a/kornia/filters/gaussian.py
+++ b/kornia/filters/gaussian.py
@@ -34,7 +34,7 @@ def gaussian_blur2d(
"""
kernel: torch.Tensor = torch.unsqueeze(get_gaussian_kernel2d(kernel_size, sigma), dim=0)
- return kornia.filter2D(input, kernel, border_type)
+ return kornia.filter2d(input, kernel, border_type)
class GaussianBlur2d(nn.Module):
diff --git a/kornia/filters/laplacian.py b/kornia/filters/laplacian.py
index 25a524bfc..2c3d00b6b 100644
--- a/kornia/filters/laplacian.py
+++ b/kornia/filters/laplacian.py
@@ -38,7 +38,7 @@ def laplacian(
if normalized:
kernel = normalize_kernel2d(kernel)
- return kornia.filter2D(input, kernel, border_type)
+ return kornia.filter2d(input, kernel, border_type)
class Laplacian(nn.Module):
diff --git a/kornia/filters/motion.py b/kornia/filters/motion.py
index c067ce8bf..bd495b54d 100644
--- a/kornia/filters/motion.py
+++ b/kornia/filters/motion.py
@@ -146,7 +146,7 @@ def motion_blur(
"""
assert border_type in ["constant", "reflect", "replicate", "circular"]
kernel: torch.Tensor = get_motion_kernel2d(kernel_size, angle, direction, mode)
- return kornia.filter2D(input, kernel, border_type)
+ return kornia.filter2d(input, kernel, border_type)
def motion_blur3d(
@@ -190,4 +190,4 @@ def motion_blur3d(
"""
assert border_type in ["constant", "reflect", "replicate", "circular"]
kernel: torch.Tensor = get_motion_kernel3d(kernel_size, angle, direction, mode)
- return kornia.filter3D(input, kernel, border_type)
+ return kornia.filter3d(input, kernel, border_type)
diff --git a/kornia/geometry/transform/elastic_transform.py b/kornia/geometry/transform/elastic_transform.py
index 0ba526a36..81d26bbf3 100644
--- a/kornia/geometry/transform/elastic_transform.py
+++ b/kornia/geometry/transform/elastic_transform.py
@@ -78,8 +78,8 @@ def elastic_transform2d(
disp_x: torch.Tensor = noise[:, :1]
disp_y: torch.Tensor = noise[:, 1:]
- disp_x = kornia.filters.filter2D(disp_x, kernel=kernel_y, border_type='constant') * alpha[0]
- disp_y = kornia.filters.filter2D(disp_y, kernel=kernel_x, border_type='constant') * alpha[1]
+ disp_x = kornia.filters.filter2d(disp_x, kernel=kernel_y, border_type='constant') * alpha[0]
+ disp_y = kornia.filters.filter2d(disp_y, kernel=kernel_x, border_type='constant') * alpha[1]
# stack and normalize displacement
disp = torch.cat([disp_x, disp_y], dim=1).permute(0, 2, 3, 1)
diff --git a/kornia/geometry/transform/pyramid.py b/kornia/geometry/transform/pyramid.py
index ac92cecc0..9eb1a5f87 100644
--- a/kornia/geometry/transform/pyramid.py
+++ b/kornia/geometry/transform/pyramid.py
@@ -6,7 +6,7 @@ import torch
import torch.nn as nn
import torch.nn.functional as F
-from kornia.filters import gaussian_blur2d, filter2D
+from kornia.filters import gaussian_blur2d, filter2d
__all__ = ["PyrDown", "PyrUp", "ScalePyramid", "pyrdown", "pyrup", "build_pyramid"]
@@ -253,7 +253,7 @@ def pyrdown(input: torch.Tensor, border_type: str = 'reflect', align_corners: bo
kernel: torch.Tensor = _get_pyramid_gaussian_kernel()
b, c, height, width = input.shape
# blur image
- x_blur: torch.Tensor = filter2D(input, kernel, border_type)
+ x_blur: torch.Tensor = filter2d(input, kernel, border_type)
# downsample.
out: torch.Tensor = F.interpolate(
@@ -294,7 +294,7 @@ def pyrup(input: torch.Tensor, border_type: str = 'reflect', align_corners: bool
)
# blurs upsampled tensor
- x_blur: torch.Tensor = filter2D(x_up, kernel, border_type)
+ x_blur: torch.Tensor = filter2d(x_up, kernel, border_type)
return x_blur
diff --git a/kornia/losses/ssim.py b/kornia/losses/ssim.py
index e4c6e9500..1269be2d6 100644
--- a/kornia/losses/ssim.py
+++ b/kornia/losses/ssim.py
@@ -4,7 +4,7 @@ import torch
import torch.nn as nn
import torch.nn.functional as F
-from kornia.filters import get_gaussian_kernel2d, filter2D
+from kornia.filters import get_gaussian_kernel2d, filter2d
def ssim(
@@ -68,17 +68,17 @@ def ssim(
C2: float = (0.03 * max_val) ** 2
# compute local mean per channel
- mu1: torch.Tensor = filter2D(img1, kernel)
- mu2: torch.Tensor = filter2D(img2, kernel)
+ mu1: torch.Tensor = filter2d(img1, kernel)
+ mu2: torch.Tensor = filter2d(img2, kernel)
mu1_sq = mu1 ** 2
mu2_sq = mu2 ** 2
mu1_mu2 = mu1 * mu2
# compute local sigma per channel
- sigma1_sq = filter2D(img1 ** 2, kernel) - mu1_sq
- sigma2_sq = filter2D(img2 ** 2, kernel) - mu2_sq
- sigma12 = filter2D(img1 * img2, kernel) - mu1_mu2
+ sigma1_sq = filter2d(img1 ** 2, kernel) - mu1_sq
+ sigma2_sq = filter2d(img2 ** 2, kernel) - mu2_sq
+ sigma12 = filter2d(img1 * img2, kernel) - mu1_mu2
# compute the similarity index map
num: torch.Tensor = (2.0 * mu1_mu2 + C1) * (2.0 * sigma12 + C2)
diff --git a/test/filters/test_filters.py b/test/filters/test_filters.py
index f21e84c1b..264df04fb 100644
--- a/test/filters/test_filters.py
+++ b/test/filters/test_filters.py
@@ -11,14 +11,14 @@ class TestFilter2D:
def test_smoke(self, device, dtype):
kernel = torch.rand(1, 3, 3, device=device, dtype=dtype)
input = torch.ones(1, 1, 7, 8, device=device, dtype=dtype)
- assert kornia.filter2D(input, kernel).shape == input.shape
+ assert kornia.filter2d(input, kernel).shape == input.shape
@pytest.mark.parametrize("batch_size", [2, 3, 6, 8])
def test_batch(self, batch_size, device, dtype):
B: int = batch_size
kernel = torch.rand(1, 3, 3, device=device, dtype=dtype)
input = torch.ones(B, 3, 7, 8, device=device, dtype=dtype)
- assert kornia.filter2D(input, kernel).shape == input.shape
+ assert kornia.filter2d(input, kernel).shape == input.shape
def test_mean_filter(self, device, dtype):
kernel = torch.ones(1, 3, 3, device=device, dtype=dtype)
@@ -53,7 +53,7 @@ class TestFilter2D:
dtype=dtype,
)
- actual = kornia.filter2D(input, kernel)
+ actual = kornia.filter2d(input, kernel)
assert_allclose(actual, expected)
def test_mean_filter_2batch_2ch(self, device, dtype):
@@ -90,7 +90,7 @@ class TestFilter2D:
dtype=dtype,
).expand(2, 2, -1, -1)
- actual = kornia.filter2D(input, kernel)
+ actual = kornia.filter2d(input, kernel)
assert_allclose(actual, expected)
def test_normalized_mean_filter(self, device, dtype):
@@ -128,7 +128,7 @@ class TestFilter2D:
dtype=dtype,
).expand(2, 2, -1, -1)
- actual = kornia.filter2D(input, kernel, normalized=True)
+ actual = kornia.filter2d(input, kernel, normalized=True)
tol_val: float = utils._get_precision_by_name(device, 'xla', 1e-1, 1e-4)
assert_allclose(actual, expected, rtol=tol_val, atol=tol_val)
@@ -167,7 +167,7 @@ class TestFilter2D:
dtype=dtype,
)
- actual = kornia.filter2D(input, kernel)
+ actual = kornia.filter2d(input, kernel)
assert_allclose(actual, expected)
def test_noncontiguous(self, device, dtype):
@@ -175,7 +175,7 @@ class TestFilter2D:
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)
expected = actual
assert_allclose(actual, actual)
@@ -186,10 +186,10 @@ class TestFilter2D:
# evaluate function gradient
input = utils.tensor_to_gradcheck_var(input) # to var
kernel = utils.tensor_to_gradcheck_var(kernel) # to var
- assert gradcheck(kornia.filter2D, (input, kernel), raise_exception=True)
+ assert gradcheck(kornia.filter2d, (input, kernel), raise_exception=True)
def test_jit(self, device, dtype):
- op = kornia.filter2D
+ op = kornia.filter2d
op_script = torch.jit.script(op)
kernel = torch.rand(1, 3, 3, device=device, dtype=dtype)
@@ -203,14 +203,14 @@ class TestFilter3D:
def test_smoke(self, device, dtype):
kernel = torch.rand(1, 3, 3, 3).to(device)
input = torch.ones(1, 1, 6, 7, 8).to(device)
- assert kornia.filter3D(input, kernel).shape == input.shape
+ assert kornia.filter3d(input, kernel).shape == input.shape
@pytest.mark.parametrize("batch_size", [2, 3, 6, 8])
def test_batch(self, batch_size, device, dtype):
B: int = batch_size
kernel = torch.rand(1, 3, 3, 3, device=device, dtype=dtype)
input = torch.ones(B, 3, 6, 7, 8, device=device, dtype=dtype)
- assert kornia.filter3D(input, kernel).shape == input.shape
+ assert kornia.filter3d(input, kernel).shape == input.shape
def test_mean_filter(self, device, dtype):
kernel = torch.ones(1, 3, 3, 3, device=device, dtype=dtype)
@@ -278,7 +278,7 @@ class TestFilter3D:
dtype=dtype,
)
- actual = kornia.filter3D(input, kernel)
+ actual = kornia.filter3d(input, kernel)
assert_allclose(actual, expected)
def test_mean_filter_2batch_2ch(self, device, dtype):
@@ -349,7 +349,7 @@ class TestFilter3D:
)
expected = expected.expand(2, 2, -1, -1, -1)
- actual = kornia.filter3D(input, kernel)
+ actual = kornia.filter3d(input, kernel)
assert_allclose(actual, expected)
def test_normalized_mean_filter(self, device, dtype):
@@ -421,7 +421,7 @@ class TestFilter3D:
)
expected = expected.expand(2, 2, -1, -1, -1)
- actual = kornia.filter3D(input, kernel, normalized=True)
+ actual = kornia.filter3d(input, kernel, normalized=True)
tol_val: float = utils._get_precision_by_name(device, 'xla', 1e-1, 1e-4)
assert_allclose(actual, expected, rtol=tol_val, atol=tol_val)
@@ -492,7 +492,7 @@ class TestFilter3D:
dtype=dtype,
)
- actual = kornia.filter3D(input, kernel)
+ actual = kornia.filter3d(input, kernel)
assert_allclose(actual, expected)
def test_noncontiguous(self, device, dtype):
@@ -500,7 +500,7 @@ class TestFilter3D:
inp = torch.rand(3, 5, 5, 5, device=device, dtype=dtype).expand(batch_size, -1, -1, -1, -1)
kernel = torch.ones(1, 2, 2, 2, device=device, dtype=dtype)
- actual = kornia.filter3D(inp, kernel)
+ actual = kornia.filter3d(inp, kernel)
expected = actual
assert_allclose(actual, expected)
@@ -511,10 +511,10 @@ class TestFilter3D:
# evaluate function gradient
input = utils.tensor_to_gradcheck_var(input) # to var
kernel = utils.tensor_to_gradcheck_var(kernel) # to var
- assert gradcheck(kornia.filter3D, (input, kernel), raise_exception=True)
+ assert gradcheck(kornia.filter3d, (input, kernel), raise_exception=True)
def test_jit(self, device, dtype):
- op = kornia.filter3D
+ op = kornia.filter3d
op_script = torch.jit.script(op)
kernel = torch.rand(1, 1, 3, 3, device=device, dtype=dtype)