π Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
Refactorization of the morphology package (#1034)
Manza128bbefba71d33
diff --git a/.gitignore b/.gitignoreindex 57751d550..2ff7c6da2 100644--- a/.gitignore+++ b/.gitignore@@ -2,6 +2,7 @@ build/dist/*version.pykornia.egg-info/+.idea*/**/__pycache__*/**/*.pyc*/**/*~diff --git a/kornia/morphology/__init__.py b/kornia/morphology/__init__.pyindex 7f5cd6aac..5c8dbe328 100644--- a/kornia/morphology/__init__.py+++ b/kornia/morphology/__init__.py@@ -1,3 +1 @@-from .basic_operators import *-from .open_close import *from .morphology import *diff --git a/kornia/morphology/basic_operators.py b/kornia/morphology/basic_operators.pydeleted file mode 100644index aef1ce94c..000000000--- a/kornia/morphology/basic_operators.py+++ /dev/null@@ -1,92 +0,0 @@-from typing import Tuple, Union, List--import torch-import torch.nn as nn-import torch.nn.functional as F---# _se_to_mask-def _se_to_mask(se: torch.Tensor) -> torch.Tensor:- se_h, se_w = se.size()- se_flat = se.view(-1)- num_feats = se_h * se_w- out = torch.zeros(num_feats, 1, se_h, se_w, dtype=se.dtype, device=se.device)- for i in range(num_feats):- y = i // se_w- x = i % se_w- out[i, 0, y, x] = (se_flat[i] >= 0).float()- return out---def dilation(tensor: torch.Tensor, kernel: torch.Tensor) -> torch.Tensor:- r"""Returns the dilated image applying the same kernel in each channel.-- The kernel must have 2 dimensions, each one defined by an odd number.- Dilation is equivalent to eroding the background thus dilation(x, k) == -erosion(-x, k).-- Args:- tensor (torch.Tensor): Image with shape :math:`(B, C, H, W)`.- kernel (torch.Tensor): Structuring element with shape :math:`(k_x, k_y)`.-- Returns:- torch.Tensor: Dilated image with shape :math:`(B, C, H, W)`.-- Example:- >>> tensor = torch.rand(1, 3, 5, 5)- >>> kernel = torch.ones(3, 3)- >>> dilated_img = dilation(tensor, kernel)- """- return -erosion(-tensor, kernel)---# erosion-def erosion(tensor: torch.Tensor, kernel: torch.Tensor) -> torch.Tensor:- r"""Returns the eroded image applying the same kernel in each channel.-- The kernel must have 2 dimensions, each one defined by an odd number.-- Args:- tensor (torch.Tensor): Image with shape :math:`(B, C, H, W)`.- kernel (torch.Tensor): Structuring element with shape :math:`(k_x, k_y)`.-- Returns:- torch.Tensor: Eroded image with shape :math:`(B, C, H, W)`.-- Example:- >>> tensor = torch.rand(1, 3, 5, 5)- >>> kernel = torch.ones(5, 5)- >>> output = erosion(tensor, kernel)- """-- if not isinstance(tensor, torch.Tensor):- raise TypeError("Input type is not a torch.Tensor. Got {}".format(- type(tensor)))-- if len(tensor.shape) != 4:- raise ValueError("Input size must have 4 dimensions. Got {}".format(- tensor.dim()))-- if not isinstance(kernel, torch.Tensor):- raise TypeError("Kernel type is not a torch.Tensor. Got {}".format(- type(kernel)))-- if len(kernel.shape) != 2:- raise ValueError("Kernel size must have 2 dimensions. Got {}".format(- kernel.dim()))-- # prepare kernel- se_e: torch.Tensor = kernel - 1.- kernel_e: torch.Tensor = _se_to_mask(se_e)-- # pad- se_h, se_w = kernel.shape- pad_e: List[int] = [se_w // 2, se_w // 2, se_h // 2, se_h // 2]-- output: torch.Tensor = tensor.view(- tensor.shape[0] * tensor.shape[1], 1, tensor.shape[2], tensor.shape[3])- output = F.pad(output, pad_e, mode='constant', value=1.)- output = F.conv2d(output, kernel_e) - se_e.view(1, -1, 1, 1)- # TODO: upgrade to: `output = torch.amin(output, dim=1)` after dropping pytorch 1.6 support- output = torch.min(output, dim=1)[0]-- return output.view_as(tensor)diff --git a/kornia/morphology/morphology.py b/kornia/morphology/morphology.pyindex 335db242f..9d6b13c7f 100644--- a/kornia/morphology/morphology.py+++ b/kornia/morphology/morphology.py@@ -1,29 +1,82 @@import torch-import torch.nn as nnimport torch.nn.functional as F-from kornia.morphology.basic_operators import dilation, erosion-from kornia.morphology.open_close import open, close+from typing import List, Optional-# morphological gradient-def gradient(tensor: torch.Tensor, kernel: torch.Tensor) -> torch.Tensor:- r"""Returns the morphological gradient of an image.+# Dilation+def dilation(tensor: torch.Tensor, kernel: torch.Tensor, origin: Optional[List[int]] = None) -> torch.Tensor:+ r"""Returns the dilated image applying the same kernel in each channel.- That means, (dilation - erosion) applying the same kernel in each channel.- The kernel must have 2 dimensions, each one defined by an odd number.+ The kernel must have 2 dimensions.Args:- tensor (torch.Tensor): Image with shape :math:`(B, C, H, W)`.- kernel (torch.Tensor): Structuring element with shape :math:`(k_x, k_y)`.+ tensor (torch.Tensor): Image with shape :math:`(B, C, H, W)`.+ kernel (torch.Tensor): Structuring element with shape :math:`(k_x, k_y)`.+ origin (List[int], Tuple[int, int]): Origin of the structuring element. Default is None and uses the center of+ the structuring element as origin (rounding towards zero).Returns:- torch.Tensor: Dilated image with shape :math:`(B, C, H, W)`.+ torch.Tensor: Dilated image with shape :math:`(B, C, H, W)`.Example:>>> tensor = torch.rand(1, 3, 5, 5)>>> kernel = torch.ones(3, 3)- >>> gradient_img = gradient(tensor, kernel)+ >>> dilated_img = dilation(tensor, kernel)+ """++ if not isinstance(tensor, torch.Tensor):+ raise TypeError("Input type is not a torch.Tensor. Got {}".format(+ type(tensor)))++ if len(tensor.shape) != 4:+ raise ValueError("Input size must have 4 dimensions. Got {}".format(+ tensor.dim()))++ if not isinstance(kernel, torch.Tensor):+ raise TypeError("Kernel type is not a torch.Tensor. Got {}".format(+ type(kernel)))++ if len(kernel.shape) != 2:+ raise ValueError("Kernel size must have 2 dimensions. Got {}".format(+ kernel.dim()))++ # origin+ se_h, se_w = kernel.shape+ if origin is None:+ origin = [se_h // 2, se_w // 2]++ # pad+ pad_e: List[int] = [origin[1], se_w - origin[1] - 1, origin[0], se_h - origin[0] - 1]+ output: torch.Tensor = F.pad(tensor, pad_e, mode='constant', value=0.)++ # computation+ output = output.unfold(2, se_h, 1).unfold(3, se_w, 1)+ output, _ = torch.max(output + kernel.flip((0, 1)), 4)+ output, _ = torch.max(output, 4)++ return output+++# Erosion+def erosion(tensor: torch.Tensor, kernel: torch.Tensor, origin: Optional[List[int]] = None) -> torch.Tensor:+ r"""Returns the eroded image applying the same kernel in each channel.++ The kernel must have 2 dimensions.++ Args:+ tensor (torch.Tensor): Image with shape :math:`(B, C, H, W)`.+ kernel (torch.Tensor): Structuring element with shape :math:`(k_x, k_y)`.+ origin (List[int], Tuple[int, int]): Origin of the structuring element. Default is None and uses the center of+ the structuring element as origin (rounding towards zero).++ Returns:+ torch.Tensor: Eroded image with shape :math:`(B, C, H, W)`.++ Example:+ >>> tensor = torch.rand(1, 3, 5, 5)+ >>> kernel = torch.ones(5, 5)+ >>> output = erosion(tensor, kernel)"""if not isinstance(tensor, torch.Tensor):@@ -42,21 +95,142 @@ def gradient(tensor: torch.Tensor, kernel: torch.Tensor) -> torch.Tensor:raise ValueError("Kernel size must have 2 dimensions. Got {}".format(kernel.dim()))- return dilation(tensor, kernel) - erosion(tensor, kernel)+ # origin+ se_h, se_w = kernel.shape+ if origin is None:+ origin = [se_h // 2, se_w // 2]++ # pad+ pad_e: List[int] = [origin[1], se_w - origin[1] - 1, origin[0], se_h - origin[0] - 1]+ output: torch.Tensor = F.pad(tensor, pad_e, mode='constant', value=1.)++ # computation+ output = output.unfold(2, se_h, 1).unfold(3, se_w, 1)+ output, _ = torch.min(output - kernel, 4)+ output, _ = torch.min(output, 4)++ return output+++# Opening+def opening(tensor: torch.Tensor, kernel: torch.Tensor, origin: Optional[List[int]] = None) -> torch.Tensor:+ r"""Returns the opened image, (that means, dilation after an erosion) applying the same kernel in each channel.++ The kernel must have 2 dimensions.++ Args:+ tensor (torch.Tensor): Image with shape :math:`(B, C, H, W)`.+ kernel (torch.Tensor): Structuring element with shape :math:`(k_x, k_y)`.+ origin (List[int], Tuple[int, int]): Origin of the structuring element. Default is None and uses the center of+ the structuring element as origin (rounding towards zero).++ Returns:+ torch.Tensor: Opened image with shape :math:`(B, C, H, W)`.++ Example:+ >>> tensor = torch.rand(1, 3, 5, 5)+ >>> kernel = torch.ones(3, 3)+ >>> opened_img = opening(tensor, kernel)+ """++ if not isinstance(tensor, torch.Tensor):+ raise TypeError("Input type is not a torch.Tensor. Got {}".format(+ type(tensor)))++ if len(tensor.shape) != 4:+ raise ValueError("Input size must have 4 dimensions. Got {}".format(+ tensor.dim()))++ if not isinstance(kernel, torch.Tensor):+ raise TypeError("Kernel type is not a torch.Tensor. Got {}".format(+ type(kernel)))++ if len(kernel.shape) != 2:+ raise ValueError("Kernel size must have 2 dimensions. Got {}".format(+ kernel.dim()))++ return dilation(erosion(tensor, kernel, origin=origin), kernel, origin=origin)+++# Closing+def closing(tensor: torch.Tensor, kernel: torch.Tensor, origin: Optional[List[int]] = None) -> torch.Tensor:+ r"""Returns the closed image, (that means, erosion after a dilation) applying the same kernel in each channel.++ The kernel must have 2 dimensions.++ Args:+ tensor (torch.Tensor): Image with shape :math:`(B, C, H, W)`.+ kernel (torch.Tensor): Structuring element with shape :math:`(k_x, k_y)`.+ origin (List[int], Tuple[int, int]): Origin of the structuring element. Default is None and uses the center of+ the structuring element as origin (rounding towards zero).++ Returns:+ torch.Tensor: Closed image with shape :math:`(B, C, H, W)`.++ Example:+ >>> tensor = torch.rand(1, 3, 5, 5)+ >>> kernel = torch.ones(3, 3)+ >>> closed_img = closing(tensor, kernel)+ """++ if not isinstance(tensor, torch.Tensor):+ raise TypeError("Input type is not a torch.Tensor. Got {}".format(+ type(tensor)))++ if len(tensor.shape) != 4:+ raise ValueError("Input size must have 4 dimensions. Got {}".format(+ tensor.dim()))++ if not isinstance(kernel, torch.Tensor):+ raise TypeError("Kernel type is not a torch.Tensor. Got {}".format(+ type(kernel)))++ if len(kernel.shape) != 2:+ raise ValueError("Kernel size must have 2 dimensions. Got {}".format(+ kernel.dim()))++ return erosion(dilation(tensor, kernel, origin=origin), kernel, origin=origin)+++# Morphological Gradient+def gradient(tensor: torch.Tensor, kernel: torch.Tensor, origin: Optional[List[int]] = None) -> torch.Tensor:+ r"""Returns the morphological gradient of an image.++ That means, (dilation - erosion) applying the same kernel in each channel.+ The kernel must have 2 dimensions.++ Args:+ tensor (torch.Tensor): Image with shape :math:`(B, C, H, W)`.+ kernel (torch.Tensor): Structuring element with shape :math:`(k_x, k_y)`.+ origin (List[int], Tuple[int, int]): Origin of the structuring element. Default is None and uses the center of+ the structuring element as origin (rounding towards zero).++ Returns:+ torch.Tensor: Gradient image with shape :math:`(B, C, H, W)`.++ Example:+ >>> tensor = torch.rand(1, 3, 5, 5)+ >>> kernel = torch.ones(3, 3)+ >>> gradient_img = gradient(tensor, kernel)+ """++ return dilation(tensor, kernel, origin=origin) - erosion(tensor, kernel, origin=origin)-# top_hat-def top_hat(tensor: torch.Tensor, kernel: torch.Tensor) -> torch.Tensor:+# Top Hat+def top_hat(tensor: torch.Tensor, kernel: torch.Tensor, origin: Optional[List[int]] = None) -> torch.Tensor:r"""Returns the top hat tranformation of an image.That means, (image - opened_image) applying the same kernel in each channel.- The kernel must have 2 dimensions, each one defined by an odd number.+ The kernel must have 2 dimensions.- See :class:`~kornia.morphology.open` for details.+ See :opening for details.Args:- tensor (torch.Tensor): Image with shape :math:`(B, C, H, W)`.- kernel (torch.Tensor): Structuring element with shape :math:`(k_x, k_y)`.+ tensor (torch.Tensor): Image with shape :math:`(B, C, H, W)`.+ kernel (torch.Tensor): Structuring element with shape :math:`(k_x, k_y)`.+ origin (List[int], Tuple[int, int]): Origin of the structuring element. Default is None and uses the center of+ the structuring element as origin (rounding towards zero).Returns:torch.Tensor: Top hat transformated image with shape :math:`(B, C, H, W)`.@@ -83,21 +257,23 @@ def top_hat(tensor: torch.Tensor, kernel: torch.Tensor) -> torch.Tensor:raise ValueError("Kernel size must have 2 dimensions. Got {}".format(kernel.dim()))- return tensor - open(tensor, kernel)+ return tensor - opening(tensor, kernel, origin=origin)-# black_hat-def black_hat(tensor: torch.Tensor, kernel: torch.Tensor) -> torch.Tensor:- r"""Returns the black hat tranformation of an image.+# Bottom Hat+def bottom_hat(tensor: torch.Tensor, kernel: torch.Tensor, origin: Optional[List[int]] = None) -> torch.Tensor:+ r"""Returns the bottom hat tranformation of an image.That means, (closed_image - image) applying the same kernel in each channel.- The kernel must have 2 dimensions, each one defined by an odd number.+ The kernel must have 2 dimensions.- See :class:`~kornia.morphology.close` for details.+ See :closing for details.Args:- tensor (torch.Tensor): Image with shape :math:`(B, C, H, W)`.- kernel (torch.Tensor): Structuring element with shape :math:`(k_x, k_y)`.+ tensor (torch.Tensor): Image with shape :math:`(B, C, H, W)`.+ kernel (torch.Tensor): Structuring element with shape :math:`(k_x, k_y)`.+ origin (List[int], Tuple[int, int]): Origin of the structuring element. Default is None and uses the center of+ the structuring element as origin (rounding towards zero).Returns:torch.Tensor: Top hat transformated image with shape :math:`(B, C, H, W)`.@@ -105,7 +281,7 @@ def black_hat(tensor: torch.Tensor, kernel: torch.Tensor) -> torch.Tensor:Example:>>> tensor = torch.rand(1, 3, 5, 5)>>> kernel = torch.ones(3, 3)- >>> black_hat_img = black_hat(tensor, kernel)+ >>> bottom_hat_img = bottom_hat(tensor, kernel)"""if not isinstance(tensor, torch.Tensor):@@ -124,4 +300,4 @@ def black_hat(tensor: torch.Tensor, kernel: torch.Tensor) -> torch.Tensor:raise ValueError("Kernel size must have 2 dimensions. Got {}".format(kernel.dim()))- return close(tensor, kernel) - tensor+ return closing(tensor, kernel, origin=origin) - tensordiff --git a/kornia/morphology/open_close.py b/kornia/morphology/open_close.pydeleted file mode 100644index 240c1fd49..000000000--- a/kornia/morphology/open_close.py+++ /dev/null@@ -1,81 +0,0 @@-import torch-import torch.nn as nn-import torch.nn.functional as F--from kornia.morphology.basic_operators import dilation, erosion---# open-def open(tensor: torch.Tensor, kernel: torch.Tensor) -> torch.Tensor:- r"""Returns the opened image, (that means, dilation after an erosion) applying the same kernel in each channel.-- The kernel must have 2 dimensions, each one defined by an odd number.-- Args:- tensor (torch.Tensor): Image with shape :math:`(B, C, H, W)`.- kernel (torch.Tensor): Structuring element with shape :math:`(k_x, k_y)`.-- Returns:- torch.Tensor: Dilated image with shape :math:`(B, C, H, W)`.-- Example:- >>> tensor = torch.rand(1, 3, 5, 5)- >>> kernel = torch.ones(3, 3)- >>> opened_img = open(tensor, kernel)- """-- if not isinstance(tensor, torch.Tensor):- raise TypeError("Input type is not a torch.Tensor. Got {}".format(- type(tensor)))-- if len(tensor.shape) != 4:- raise ValueError("Input size must have 4 dimensions. Got {}".format(- tensor.dim()))-- if not isinstance(kernel, torch.Tensor):- raise TypeError("Kernel type is not a torch.Tensor. Got {}".format(- type(kernel)))-- if len(kernel.shape) != 2:- raise ValueError("Kernel size must have 2 dimensions. Got {}".format(- kernel.dim()))-- return dilation(erosion(tensor, kernel), kernel)---# close-def close(tensor: torch.Tensor, kernel: torch.Tensor) -> torch.Tensor:- r"""Returns the closed image, (that means, erosion after a dilation) applying the same kernel in each channel.-- The kernel must have 2 dimensions, each one defined by an odd number.-- Args:- tensor (torch.Tensor): Image with shape :math:`(B, C, H, W)`.- kernel (torch.Tensor): Structuring element with shape :math:`(k_x, k_y)`.-- Returns:- torch.Tensor: Dilated image with shape :math:`(B, C, H, W)`.-- Example:- >>> tensor = torch.rand(1, 3, 5, 5)- >>> kernel = torch.ones(3, 3)- >>> closed_img = close(tensor, kernel)- """-- if not isinstance(tensor, torch.Tensor):- raise TypeError("Input type is not a torch.Tensor. Got {}".format(- type(tensor)))-- if len(tensor.shape) != 4:- raise ValueError("Input size must have 4 dimensions. Got {}".format(- tensor.dim()))-- if not isinstance(kernel, torch.Tensor):- raise TypeError("Kernel type is not a torch.Tensor. Got {}".format(- type(kernel)))-- if len(kernel.shape) != 2:- raise ValueError("Kernel size must have 2 dimensions. Got {}".format(- kernel.dim()))-- return erosion(dilation(tensor, kernel), kernel)diff --git a/test/morphology/test_black_hat.py b/test/morphology/test_bottom_hat.pysimilarity index 74%rename from test/morphology/test_black_hat.pyrename to test/morphology/test_bottom_hat.pyindex 5b0ec0cda..904dc3ebc 100644--- a/test/morphology/test_black_hat.py+++ b/test/morphology/test_bottom_hat.py@@ -1,17 +1,15 @@import pytestimport torch-from kornia.morphology.basic_operators import _se_to_mask-from kornia.morphology.morphology import black_hat-import kornia.testing as utils # test utils+from kornia.morphology.morphology import bottom_hatfrom torch.autograd import gradcheckfrom torch.testing import assert_allclose-class TestBlackHat():+class TestBlackHat:def test_smoke(self, device, dtype):kernel = torch.rand(3, 3, device=device, dtype=dtype)- assert _se_to_mask(kernel) is not None+ assert kernel is not None@pytest.mark.parametrize("shape", [(1, 3, 4, 4), (2, 3, 2, 4), (3, 3, 4, 1), (3, 2, 5, 5)])@@ -20,43 +18,43 @@ class TestBlackHat():def test_cardinality(self, device, dtype, shape, kernel):img = torch.ones(shape, device=device, dtype=dtype)krnl = torch.ones(kernel, device=device, dtype=dtype)- assert black_hat(img, krnl).shape == shape+ assert bottom_hat(img, krnl).shape == shapedef test_value(self, device, dtype):input = torch.tensor([[0.5, 1., 0.3], [0.7, 0.3, 0.8], [0.4, 0.9, 0.2]],device=device, dtype=dtype)[None, None, :, :]- kernel = torch.tensor([[0., 1., 0.], [1., 1., 1.], [0., 1., 0.]], device=device, dtype=dtype)+ kernel = torch.tensor([[-1., 0., -1.], [0., 0., 0.], [-1., 0., -1.]], device=device, dtype=dtype)expected = torch.tensor([[0.2, 0., 0.5], [0., 0.4, 0.], [0.3, 0., 0.6]],device=device, dtype=dtype)[None, None, :, :]- assert_allclose(black_hat(input, kernel), expected)+ assert_allclose(bottom_hat(input, kernel), expected)def test_exception(self, device, dtype):input = torch.ones(1, 1, 3, 4, device=device, dtype=dtype)kernel = torch.ones(3, 3, device=device, dtype=dtype)with pytest.raises(TypeError):- assert black_hat([0.], kernel)+ assert bottom_hat([0.], kernel)with pytest.raises(TypeError):- assert black_hat(input, [0.])+ assert bottom_hat(input, [0.])with pytest.raises(ValueError):test = torch.ones(2, 3, 4, device=device, dtype=dtype)- assert black_hat(test, kernel)+ assert bottom_hat(test, kernel)with pytest.raises(ValueError):test = torch.ones(2, 3, 4, device=device, dtype=dtype)- assert black_hat(input, test)+ assert bottom_hat(input, test)@pytest.mark.graddef test_gradcheck(self, device, dtype):input = torch.rand(2, 3, 4, 4, requires_grad=True, device=device, dtype=torch.float64)kernel = torch.rand(3, 3, requires_grad=True, device=device, dtype=torch.float64)- assert gradcheck(black_hat, (input, kernel), raise_exception=True)+ assert gradcheck(bottom_hat, (input, kernel), raise_exception=True)@pytest.mark.jitdef test_jit(self, device, dtype):- op = black_hat+ op = bottom_hatop_script = torch.jit.script(op)input = torch.rand(1, 2, 7, 7, device=device, dtype=dtype)diff --git a/test/morphology/test_close.py b/test/morphology/test_closing.pysimilarity index 57%rename from test/morphology/test_close.pyrename to test/morphology/test_closing.pyindex 24557cf08..24c956782 100644--- a/test/morphology/test_close.py+++ b/test/morphology/test_closing.py@@ -1,17 +1,15 @@import pytestimport torch-from kornia.morphology.basic_operators import _se_to_mask-from kornia.morphology.open_close import close-import kornia.testing as utils # test utils+from kornia.morphology.morphology import closingfrom torch.autograd import gradcheckfrom torch.testing import assert_allclose-class TestClose():+class TestClosing:def test_smoke(self, device, dtype):kernel = torch.rand(3, 3, device=device, dtype=dtype)- assert _se_to_mask(kernel) is not None+ assert kernel is not None@pytest.mark.parametrize("shape", [(1, 3, 4, 4), (2, 3, 2, 4), (3, 3, 4, 1), (3, 2, 5, 5)])@@ -20,49 +18,49 @@ class TestClose():def test_cardinality(self, device, dtype, shape, kernel):img = torch.ones(shape, device=device, dtype=dtype)krnl = torch.ones(kernel, device=device, dtype=dtype)- assert close(img, krnl).shape == shape+ assert closing(img, krnl).shape == shapedef test_value(self, device, dtype):- input = torch.tensor([[0.5, 1., 0.3], [0.7, 0.3, 0.8], [0.4, 0.9, 0.2]],- device=device, dtype=dtype)[None, None, :, :]- kernel = torch.tensor([[0., 1., 0.], [1., 1., 1.], [0., 1., 0.]], device=device, dtype=dtype)+ tensor = torch.tensor([[0.5, 1., 0.3], [0.7, 0.3, 0.8], [0.4, 0.9, 0.2]],+ device=device, dtype=dtype)[None, None, :, :]+ kernel = torch.tensor([[-1., 0., -1.], [0., 0., 0.], [-1., 0., -1.]], device=device, dtype=dtype)expected = torch.tensor([[0.7, 1., 0.8], [0.7, 0.7, 0.8], [0.7, 0.9, 0.8]],device=device, dtype=dtype)[None, None, :, :]- assert_allclose(close(input, kernel), expected)+ assert_allclose(closing(tensor, kernel), expected)def test_exception(self, device, dtype):- input = torch.ones(1, 1, 3, 4, device=device, dtype=dtype)+ tensor = torch.ones(1, 1, 3, 4, device=device, dtype=dtype)kernel = torch.ones(3, 3, device=device, dtype=dtype)with pytest.raises(TypeError):- assert close([0.], kernel)+ assert closing([0.], kernel)with pytest.raises(TypeError):- assert close(input, [0.])+ assert closing(tensor, [0.])with pytest.raises(ValueError):test = torch.ones(2, 3, 4, device=device, dtype=dtype)- assert close(test, kernel)+ assert closing(test, kernel)with pytest.raises(ValueError):test = torch.ones(2, 3, 4, device=device, dtype=dtype)- assert close(input, test)+ assert closing(tensor, test)@pytest.mark.graddef test_gradcheck(self, device, dtype):- input = torch.rand(2, 3, 4, 4, requires_grad=True, device=device, dtype=torch.float64)+ tensor = torch.rand(2, 3, 4, 4, requires_grad=True, device=device, dtype=torch.float64)kernel = torch.rand(3, 3, requires_grad=True, device=device, dtype=torch.float64)- assert gradcheck(close, (input, kernel), raise_exception=True)+ assert gradcheck(closing, (tensor, kernel), raise_exception=True)@pytest.mark.jitdef test_jit(self, device, dtype):- op = close+ op = closingop_script = torch.jit.script(op)- input = torch.rand(1, 2, 7, 7, device=device, dtype=dtype)+ tensor = torch.rand(1, 2, 7, 7, device=device, dtype=dtype)kernel = torch.ones(3, 3, device=device, dtype=dtype)- actual = op_script(input, kernel)- expected = op(input, kernel)+ actual = op_script(tensor, kernel)+ expected = op(tensor, kernel)assert_allclose(actual, expected)diff --git a/test/morphology/test_dilation.py b/test/morphology/test_dilation.pyindex 8fe7994ee..3f2653494 100644--- a/test/morphology/test_dilation.py+++ b/test/morphology/test_dilation.py@@ -1,16 +1,15 @@import pytestimport torch-from kornia.morphology.basic_operators import _se_to_mask, dilation-import kornia.testing as utils # test utils+from kornia.morphology.morphology import dilationfrom torch.autograd import gradcheckfrom torch.testing import assert_allclose-class TestDilate():+class TestDilate:def test_smoke(self, device, dtype):kernel = torch.rand(3, 3, device=device, dtype=dtype)- assert _se_to_mask(kernel) is not None+ assert kernel is not None@pytest.mark.parametrize("shape", [(1, 3, 4, 4), (2, 3, 2, 4), (3, 3, 4, 1), (3, 2, 5, 5)])@@ -22,22 +21,22 @@ class TestDilate():assert dilation(img, krnl).shape == shapedef test_value(self, device, dtype):- input = torch.tensor([[0.5, 1., 0.3], [0.7, 0.3, 0.8], [0.4, 0.9, 0.2]],- device=device, dtype=dtype)[None, None, :, :]- kernel = torch.tensor([[0., 1., 0.], [1., 1., 1.], [0., 1., 0.]], device=device, dtype=dtype)+ tensor = torch.tensor([[0.5, 1., 0.3], [0.7, 0.3, 0.8], [0.4, 0.9, 0.2]],+ device=device, dtype=dtype)[None, None, :, :]+ kernel = torch.tensor([[-1., 0., -1.], [0., 0., 0.], [-1., 0., -1.]], device=device, dtype=dtype)expected = torch.tensor([[1., 1., 1.], [0.7, 1., 0.8], [0.9, 0.9, 0.9]],device=device, dtype=dtype)[None, None, :, :]- assert_allclose(dilation(input, kernel), expected)+ assert_allclose(dilation(tensor, kernel), expected)def test_exception(self, device, dtype):- input = torch.ones(1, 1, 3, 4, device=device, dtype=dtype)+ tensor = torch.ones(1, 1, 3, 4, device=device, dtype=dtype)kernel = torch.ones(3, 3, device=device, dtype=dtype)with pytest.raises(TypeError):assert dilation([0.], kernel)with pytest.raises(TypeError):- assert dilation(input, [0.])+ assert dilation(tensor, [0.])with pytest.raises(ValueError):test = torch.ones(2, 3, 4, device=device, dtype=dtype)@@ -45,23 +44,23 @@ class TestDilate():with pytest.raises(ValueError):test = torch.ones(2, 3, 4, device=device, dtype=dtype)- assert dilation(input, test)+ assert dilation(tensor, test)@pytest.mark.graddef test_gradcheck(self, device, dtype):- input = torch.rand(2, 3, 4, 4, requires_grad=True, device=device, dtype=torch.float64)+ tensor = torch.rand(2, 3, 4, 4, requires_grad=True, device=device, dtype=torch.float64)kernel = torch.rand(3, 3, requires_grad=True, device=device, dtype=torch.float64)- assert gradcheck(dilation, (input, kernel), raise_exception=True)+ assert gradcheck(dilation, (tensor, kernel), raise_exception=True)@pytest.mark.jitdef test_jit(self, device, dtype):op = dilationop_script = torch.jit.script(op)- input = torch.rand(1, 2, 7, 7, device=device, dtype=dtype)+ tensor = torch.rand(1, 2, 7, 7, device=device, dtype=dtype)kernel = torch.ones(3, 3, device=device, dtype=dtype)- actual = op_script(input, kernel)- expected = op(input, kernel)+ actual = op_script(tensor, kernel)+ expected = op(tensor, kernel)assert_allclose(actual, expected)diff --git a/test/morphology/test_erosion.py b/test/morphology/test_erosion.pyindex a8c739759..af810e397 100644--- a/test/morphology/test_erosion.py+++ b/test/morphology/test_erosion.py@@ -1,16 +1,15 @@import pytestimport torch-from kornia.morphology.basic_operators import _se_to_mask, erosion-import kornia.testing as utils # test utils+from kornia.morphology.morphology import erosionfrom torch.autograd import gradcheckfrom torch.testing import assert_allclose-class TestErode():+class TestErode:def test_smoke(self, device, dtype):kernel = torch.rand(3, 3, device=device, dtype=dtype)- assert _se_to_mask(kernel) is not None+ assert kernel is not None@pytest.mark.parametrize("shape", [(1, 3, 4, 4), (2, 3, 2, 4), (3, 3, 4, 1), (3, 2, 5, 5)])@@ -22,22 +21,22 @@ class TestErode():assert erosion(img, krnl).shape == shapedef test_value(self, device, dtype):- input = torch.tensor([[0.5, 1., 0.3], [0.7, 0.3, 0.8], [0.4, 0.9, 0.2]],- device=device, dtype=dtype)[None, None, :, :]- kernel = torch.tensor([[0., 1., 0.], [1., 1., 1.], [0., 1., 0.]], device=device, dtype=dtype)+ tensor = torch.tensor([[0.5, 1., 0.3], [0.7, 0.3, 0.8], [0.4, 0.9, 0.2]],+ device=device, dtype=dtype)[None, None, :, :]+ kernel = torch.tensor([[-1., 0., -1.], [0., 0., 0.], [-1., 0., -1.]], device=device, dtype=dtype)expected = torch.tensor([[0.5, 0.3, 0.3], [0.3, 0.3, 0.2], [0.4, 0.2, 0.2]],device=device, dtype=dtype)[None, None, :, :]- assert_allclose(erosion(input, kernel), expected)+ assert_allclose(erosion(tensor, kernel), expected)def test_exception(self, device, dtype):- input = torch.ones(1, 1, 3, 4, device=device, dtype=dtype)+ tensor = torch.ones(1, 1, 3, 4, device=device, dtype=dtype)kernel = torch.ones(3, 3, device=device, dtype=dtype)with pytest.raises(TypeError):assert erosion([0.], kernel)with pytest.raises(TypeError):- assert erosion(input, [0.])+ assert erosion(tensor, [0.])with pytest.raises(ValueError):test = torch.ones(2, 3, 4, device=device, dtype=dtype)@@ -45,23 +44,23 @@ class TestErode():with pytest.raises(ValueError):test = torch.ones(2, 3, 4, device=device, dtype=dtype)- assert erosion(input, test)+ assert erosion(tensor, test)@pytest.mark.graddef test_gradcheck(self, device, dtype):- input = torch.rand(2, 3, 4, 4, requires_grad=True, device=device, dtype=torch.float64)+ tensor = torch.rand(2, 3, 4, 4, requires_grad=True, device=device, dtype=torch.float64)kernel = torch.rand(3, 3, requires_grad=True, device=device, dtype=torch.float64)- assert gradcheck(erosion, (input, kernel), raise_exception=True)+ assert gradcheck(erosion, (tensor, kernel), raise_exception=True)@pytest.mark.jitdef test_jit(self, device, dtype):op = erosionop_script = torch.jit.script(op)- input = torch.rand(1, 2, 7, 7, device=device, dtype=dtype)+ tensor = torch.rand(1, 2, 7, 7, device=device, dtype=dtype)kernel = torch.ones(3, 3, device=device, dtype=dtype)- actual = op_script(input, kernel)- expected = op(input, kernel)+ actual = op_script(tensor, kernel)+ expected = op(tensor, kernel)assert_allclose(actual, expected)diff --git a/test/morphology/test_gradient.py b/test/morphology/test_gradient.pyindex 03ff39db1..24dc1f136 100644--- a/test/morphology/test_gradient.py+++ b/test/morphology/test_gradient.py@@ -1,17 +1,15 @@import pytestimport torch-from kornia.morphology.basic_operators import _se_to_maskfrom kornia.morphology.morphology import gradient-import kornia.testing as utils # test utilsfrom torch.autograd import gradcheckfrom torch.testing import assert_allclose-class TestGradient():+class TestGradient:def test_smoke(self, device, dtype):kernel = torch.rand(3, 3, device=device, dtype=dtype)- assert _se_to_mask(kernel) is not None+ assert kernel is not None@pytest.mark.parametrize("shape", [(1, 3, 4, 4), (2, 3, 2, 4), (3, 3, 4, 1), (3, 2, 5, 5)])@@ -23,22 +21,22 @@ class TestGradient():assert gradient(img, krnl).shape == shapedef test_value(self, device, dtype):- input = torch.tensor([[0.5, 1., 0.3], [0.7, 0.3, 0.8], [0.4, 0.9, 0.2]],- device=device, dtype=dtype)[None, None, :, :]- kernel = torch.tensor([[0., 1., 0.], [1., 1., 1.], [0., 1., 0.]], device=device, dtype=dtype)+ tensor = torch.tensor([[0.5, 1., 0.3], [0.7, 0.3, 0.8], [0.4, 0.9, 0.2]],+ device=device, dtype=dtype)[None, None, :, :]+ kernel = torch.tensor([[-1., 0., -1.], [0., 0., 0.], [-1., 0., -1.]], device=device, dtype=dtype)expected = torch.tensor([[0.5, 0.7, 0.7], [0.4, 0.7, 0.6], [0.5, 0.7, 0.7]],device=device, dtype=dtype)[None, None, :, :]- assert_allclose(gradient(input, kernel), expected)+ assert_allclose(gradient(tensor, kernel), expected)def test_exception(self, device, dtype):- input = torch.ones(1, 1, 3, 4, device=device, dtype=dtype)+ tensor = torch.ones(1, 1, 3, 4, device=device, dtype=dtype)kernel = torch.ones(3, 3, device=device, dtype=dtype)with pytest.raises(TypeError):assert gradient([0.], kernel)with pytest.raises(TypeError):- assert gradient(input, [0.])+ assert gradient(tensor, [0.])with pytest.raises(ValueError):test = torch.ones(2, 3, 4, device=device, dtype=dtype)@@ -46,23 +44,23 @@ class TestGradient():with pytest.raises(ValueError):test = torch.ones(2, 3, 4, device=device, dtype=dtype)- assert gradient(input, test)+ assert gradient(tensor, test)@pytest.mark.graddef test_gradcheck(self, device, dtype):- input = torch.rand(2, 3, 4, 4, requires_grad=True, device=device, dtype=torch.float64)+ tensor = torch.rand(2, 3, 4, 4, requires_grad=True, device=device, dtype=torch.float64)kernel = torch.rand(3, 3, requires_grad=True, device=device, dtype=torch.float64)- assert gradcheck(gradient, (input, kernel), raise_exception=True)+ assert gradcheck(gradient, (tensor, kernel), raise_exception=True)@pytest.mark.jitdef test_jit(self, device, dtype):op = gradientop_script = torch.jit.script(op)- input = torch.rand(1, 2, 7, 7, device=device, dtype=dtype)+ tensor = torch.rand(1, 2, 7, 7, device=device, dtype=dtype)kernel = torch.ones(3, 3, device=device, dtype=dtype)- actual = op_script(input, kernel)- expected = op(input, kernel)+ actual = op_script(tensor, kernel)+ expected = op(tensor, kernel)assert_allclose(actual, expected)diff --git a/test/morphology/test_open.py b/test/morphology/test_opening.pysimilarity index 57%rename from test/morphology/test_open.pyrename to test/morphology/test_opening.pyindex c0ae81470..d7b9aff8d 100644--- a/test/morphology/test_open.py+++ b/test/morphology/test_opening.py@@ -1,17 +1,15 @@import pytestimport torch-from kornia.morphology.basic_operators import _se_to_mask-from kornia.morphology.open_close import open-import kornia.testing as utils # test utils+from kornia.morphology.morphology import openingfrom torch.autograd import gradcheckfrom torch.testing import assert_allclose-class TestOpen():+class TestOpening:def test_smoke(self, device, dtype):kernel = torch.rand(3, 3, device=device, dtype=dtype)- assert _se_to_mask(kernel) is not None+ assert kernel is not None@pytest.mark.parametrize("shape", [(1, 3, 4, 4), (2, 3, 2, 4), (3, 3, 4, 1), (3, 2, 5, 5)])@@ -20,49 +18,49 @@ class TestOpen():def test_cardinality(self, device, dtype, shape, kernel):img = torch.ones(shape, device=device, dtype=dtype)krnl = torch.ones(kernel, device=device, dtype=dtype)- assert open(img, krnl).shape == shape+ assert opening(img, krnl).shape == shapedef test_value(self, device, dtype):- input = torch.tensor([[0.5, 1., 0.3], [0.7, 0.3, 0.8], [0.4, 0.9, 0.2]],- device=device, dtype=dtype)[None, None, :, :]- kernel = torch.tensor([[0., 1., 0.], [1., 1., 1.], [0., 1., 0.]], device=device, dtype=dtype)+ tensor = torch.tensor([[0.5, 1., 0.3], [0.7, 0.3, 0.8], [0.4, 0.9, 0.2]],+ device=device, dtype=dtype)[None, None, :, :]+ kernel = torch.tensor([[-1., 0., -1.], [0., 0., 0.], [-1., 0., -1.]], device=device, dtype=dtype)expected = torch.tensor([[0.5, 0.5, 0.3], [0.5, 0.3, 0.3], [0.4, 0.4, 0.2]],device=device, dtype=dtype)[None, None, :, :]- assert_allclose(open(input, kernel), expected)+ assert_allclose(opening(tensor, kernel), expected)def test_exception(self, device, dtype):- input = torch.ones(1, 1, 3, 4, device=device, dtype=dtype)+ tensor = torch.ones(1, 1, 3, 4, device=device, dtype=dtype)kernel = torch.ones(3, 3, device=device, dtype=dtype)with pytest.raises(TypeError):- assert open([0.], kernel)+ assert opening([0.], kernel)with pytest.raises(TypeError):- assert open(input, [0.])+ assert opening(tensor, [0.])with pytest.raises(ValueError):test = torch.ones(2, 3, 4, device=device, dtype=dtype)- assert open(test, kernel)+ assert opening(test, kernel)with pytest.raises(ValueError):test = torch.ones(2, 3, 4, device=device, dtype=dtype)- assert open(input, test)+ assert opening(tensor, test)@pytest.mark.graddef test_gradcheck(self, device, dtype):- input = torch.rand(2, 3, 4, 4, requires_grad=True, device=device, dtype=torch.float64)+ tensor = torch.rand(2, 3, 4, 4, requires_grad=True, device=device, dtype=torch.float64)kernel = torch.rand(3, 3, requires_grad=True, device=device, dtype=torch.float64)- assert gradcheck(open, (input, kernel), raise_exception=True)+ assert gradcheck(opening, (tensor, kernel), raise_exception=True)@pytest.mark.jitdef test_jit(self, device, dtype):- op = open+ op = openingop_script = torch.jit.script(op)- input = torch.rand(1, 2, 7, 7, device=device, dtype=dtype)+ tensor = torch.rand(1, 2, 7, 7, device=device, dtype=dtype)kernel = torch.ones(3, 3, device=device, dtype=dtype)- actual = op_script(input, kernel)- expected = op(input, kernel)+ actual = op_script(tensor, kernel)+ expected = op(tensor, kernel)assert_allclose(actual, expected)diff --git a/test/morphology/test_top_hat.py b/test/morphology/test_top_hat.pyindex 4baadc74b..0ef9e2309 100644--- a/test/morphology/test_top_hat.py+++ b/test/morphology/test_top_hat.py@@ -1,17 +1,15 @@import pytestimport torch-from kornia.morphology.basic_operators import _se_to_maskfrom kornia.morphology.morphology import top_hat-import kornia.testing as utils # test utilsfrom torch.autograd import gradcheckfrom torch.testing import assert_allclose-class TestTopHat():+class TestTopHat:def test_smoke(self, device, dtype):kernel = torch.rand(3, 3, device=device, dtype=dtype)- assert _se_to_mask(kernel) is not None+ assert kernel is not None@pytest.mark.parametrize("shape", [(1, 3, 4, 4), (2, 3, 2, 4), (3, 3, 4, 1), (3, 2, 5, 5)])@@ -25,7 +23,7 @@ class TestTopHat():def test_value(self, device, dtype):input = torch.tensor([[0.5, 1., 0.3], [0.7, 0.3, 0.8], [0.4, 0.9, 0.2]],device=device, dtype=dtype)[None, None, :, :]- kernel = torch.tensor([[0., 1., 0.], [1., 1., 1.], [0., 1., 0.]], device=device, dtype=dtype)+ kernel = torch.tensor([[-1., 0., -1.], [0., 0., 0.], [-1., 0., -1.]], device=device, dtype=dtype)expected = torch.tensor([[0., 0.5, 0.], [0.2, 0., 0.5], [0., 0.5, 0.]],device=device, dtype=dtype)[None, None, :, :]assert_allclose(top_hat(input, kernel), expected)