π Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
Added version of spatial gradients w/o averaging for use with spatial pyramid
Dmytro Mishkin5b5157072374
diff --git a/kornia/filters/sobel.py b/kornia/filters/sobel.pyindex e1de52667..ad0cb0720 100644--- a/kornia/filters/sobel.py+++ b/kornia/filters/sobel.py@@ -12,6 +12,15 @@ def _get_sobel_kernel_3x3() -> torch.Tensor:])+def _get_separate_kernel_3x3() -> torch.Tensor:+ """Utility function that returns a sobel kernel of 3x3"""+ return torch.tensor([+ [-0., 0., 0.],+ [-1., 0., 1.],+ [-0., 0., 0.],+ ])++class SpatialGradient(nn.Module):r"""Computes the first order image derivative in both x and y using a Sobeloperator.@@ -28,9 +37,16 @@ class SpatialGradient(nn.Module):>>> output = kornia.filters.SpatialGradient()(input) # 1x3x2x4x4"""- def __init__(self) -> None:+ def __init__(self, mode: str = 'Sobel') -> None:super(SpatialGradient, self).__init__()- self.kernel: torch.Tensor = self.get_sobel_kernel()+ if mode not in ['Sobel', 'NoBlur']:+ raise TypeError("Mode should be either type is Sobel\+ or NoBlur. Got {}".format(mode))+ self.mode = mode+ if self.mode == 'Sobel':+ self.kernel: torch.Tensor = self.get_sobel_kernel()+ else:+ self.kernel = self.get_separate_kernel()@staticmethoddef get_sobel_kernel() -> torch.Tensor:@@ -38,6 +54,12 @@ class SpatialGradient(nn.Module):kernel_y: torch.Tensor = kernel_x.transpose(0, 1)return torch.stack([kernel_x, kernel_y])+ @staticmethod+ def get_separate_kernel() -> torch.Tensor:+ kernel_x: torch.Tensor = _get_separate_kernel_3x3()+ kernel_y: torch.Tensor = kernel_x.transpose(0, 1)+ return torch.stack([kernel_x, kernel_y])+def forward(self, input: torch.Tensor) -> torch.Tensor: # type: ignoreif not torch.is_tensor(input):raise TypeError("Input type is not a torch.Tensor. Got {}"@@ -98,13 +120,13 @@ class Sobel(nn.Module):# functiona api-def spatial_gradient(input: torch.Tensor) -> torch.Tensor:+def spatial_gradient(input: torch.Tensor, mode: str = 'Sobel') -> torch.Tensor:r"""Computes the first order image derivative in both x and y using a Sobeloperator.See :class:`~kornia.filters.SpatialGradient` for details."""- return SpatialGradient()(input)+ return SpatialGradient(mode)(input)def sobel(input: torch.Tensor) -> torch.Tensor:diff --git a/test/filters/test_sobel.py b/test/filters/test_sobel.pyindex d2e498523..2df2c0b7b 100644--- a/test/filters/test_sobel.py+++ b/test/filters/test_sobel.py@@ -45,6 +45,31 @@ class TestSpatialGradient:edges = kornia.filters.spatial_gradient(inp)assert_allclose(edges, expected)+ def test_edges_sep(self):+ inp = torch.tensor([[[+ [0., 0., 0., 0., 0.],+ [0., 0., 1., 0., 0.],+ [0., 1., 1., 1., 0.],+ [0., 0., 1., 0., 0.],+ [0., 0., 0., 0., 0.],+ ]]])++ expected = torch.tensor([[[[+ [0., 0., 0., 0., 0.],+ [0., 1., 0., -1., 0.],+ [1., 1., 0., -1., -1.],+ [0., 1., 0., -1., 0.],+ [0., 0., 0., 0., 0.]+ ], [+ [0., 0., 1., 0., 0.],+ [0., 1., 1., 1., 0.],+ [0., 0., 0., 0., 0.],+ [0., -1., -1., -1., 0.],+ [0., 0., -1., 0., 0.]+ ]]]])+ edges = kornia.filters.spatial_gradient(inp, 'NoBlur')+ assert_allclose(edges, expected)+def test_gradcheck(self):batch_size, channels, height, width = 1, 2, 5, 4img = torch.rand(batch_size, channels, height, width)