Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

rename sobel_edges -> spatial_gradient

edgarribadeef40f76b8b
diff --git a/docs/source/filtering.rst b/docs/source/filtering.rst
index cfd032532..d70247876 100644
--- a/docs/source/filtering.rst
+++ b/docs/source/filtering.rst
@@ -17,7 +17,7 @@ The functions in this sections perform various image filtering operations.
.. autofunction:: pyrdown
.. autofunction:: pyrup
.. autofunction:: sobel
-.. autofunction:: sobel_edges
+.. autofunction:: spatial_gradients
.. autoclass:: BoxBlur
.. autoclass:: MedianBlur
@@ -26,4 +26,4 @@ The functions in this sections perform various image filtering operations.
.. autoclass:: PyrDown
.. autoclass:: PyrUp
.. autoclass:: Sobel
-.. autoclass:: SobelEdges
+.. autoclass:: spatial_gradients
diff --git a/test/test_sobel.py b/test/test_sobel.py
index a369088aa..99d9c666f 100644
--- a/test/test_sobel.py
+++ b/test/test_sobel.py
@@ -8,15 +8,15 @@ from torch.autograd import gradcheck
import utils # test utils
-class TestSobelEdges:
+class TestSpatialGradient:
def test_shape(self):
inp = torch.zeros(1, 3, 4, 4)
- sobel = tgm.image.SobelEdges()
+ sobel = tgm.image.SpatialGradient()
assert sobel(inp).shape == (1, 3, 2, 4, 4)
def test_shape_batch(self):
inp = torch.zeros(2, 6, 4, 4)
- sobel = tgm.image.SobelEdges()
+ sobel = tgm.image.SpatialGradient()
assert sobel(inp).shape == (2, 6, 2, 4, 4)
def test_edges(self):
@@ -36,22 +36,23 @@ class TestSobelEdges:
[3., 0., -3.],
]]]])
- edges = tgm.image.sobel_edges(inp)
+ edges = tgm.image.spatial_gradient(inp)
assert_allclose(edges, expected)
def test_gradcheck(self):
batch_size, channels, height, width = 1, 2, 5, 4
img = torch.rand(batch_size, channels, height, width)
img = utils.tensor_to_gradcheck_var(img) # to var
- assert gradcheck(tgm.image.sobel_edges, (img,), raise_exception=True)
+ assert gradcheck(tgm.image.spatial_gradient, (img,),
+ raise_exception=True)
def test_jit(self):
@torch.jit.script
def op_script(input):
- return tgm.image.sobel_edges(input)
+ return tgm.image.spatial_gradient(input)
img = torch.rand(2, 3, 4, 5)
actual = op_script(img)
- expected = tgm.image.sobel_edges(img)
+ expected = tgm.image.spatial_gradient(img)
assert_allclose(actual, expected)
diff --git a/torchgeometry/image/__init__.py b/torchgeometry/image/__init__.py
index 9c948bafe..f39b71fde 100644
--- a/torchgeometry/image/__init__.py
+++ b/torchgeometry/image/__init__.py
@@ -5,6 +5,6 @@ from .laplacian import get_laplacian_kernel, get_laplacian_kernel2d
from .laplacian import Laplacian, laplacian
from .color import rgb_to_grayscale, RgbToGrayscale
from .pyramid import pyrdown, pyrup, PyrDown, PyrUp
-from .sobel import SobelEdges, Sobel, sobel, sobel_edges
+from .sobel import SpatialGradient, Sobel, sobel, spatial_gradient
from .blur import BoxBlur, box_blur
from .median import MedianBlur, median_blur
diff --git a/torchgeometry/image/sobel.py b/torchgeometry/image/sobel.py
index 98815e302..aa32e0262 100644
--- a/torchgeometry/image/sobel.py
+++ b/torchgeometry/image/sobel.py
@@ -3,10 +3,10 @@ import torch.nn as nn
import torch.nn.functional as F
__all__ = [
- "SobelEdges",
+ "SpatialGradient",
"Sobel",
"sobel",
- "sobel_edges",
+ "spatial_gradient",
]
@@ -19,8 +19,9 @@ def _get_sobel_kernel_3x3() -> torch.Tensor:
])
-class SobelEdges(nn.Module):
- r"""Computes the Sobel edge maps per channel.
+class SpatialGradient(nn.Module):
+ r"""Computes the first order image derivative in both x and y using a Sobel
+ operator.
Args:
input (torch.Tensor): the input tensor with shape of BxCxHxW.
@@ -31,14 +32,12 @@ class SobelEdges(nn.Module):
Examples:
>>> input = torch.rand(1, 3, 4, 4)
- >>> output = tgm.image.SobelEdges()(input) # 1x3x2x4x4
+ >>> output = tgm.image.SpatialGradient()(input) # 1x3x2x4x4
"""
def __init__(self) -> None:
- super(SobelEdges, self).__init__()
+ super(SpatialGradient, self).__init__()
self.kernel: torch.Tensor = self.get_sobel_kernel()
- # NOTE: this wil change if we make the kernel size variable
- self.padding: int = 2
@staticmethod
def get_sobel_kernel() -> torch.Tensor:
@@ -88,7 +87,7 @@ class Sobel(nn.Module):
raise ValueError("Invalid input shape, we expect BxCxHxW. Got: {}"
.format(input.shape))
# comput the x/y gradients
- edges: torch.Tensor = sobel_edges(input)
+ edges: torch.Tensor = spatial_gradient(input)
# unpack the edges
gx: torch.Tensor = edges[:, :, 0]
@@ -102,12 +101,13 @@ class Sobel(nn.Module):
# functiona api
-def sobel_edges(input: torch.Tensor) -> torch.Tensor:
- r"""Computes the Sobel edge maps per channel.
+def spatial_gradient(input: torch.Tensor) -> torch.Tensor:
+ r"""Computes the first order image derivative in both x and y using a Sobel
+ operator.
See :class:`~torchgeometry.image.SobelEdges` for details.
"""
- return SobelEdges()(input)
+ return SpatialGradient()(input)
def sobel(input: torch.Tensor) -> torch.Tensor: