Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

split modules: image -> color, filters, transform

edgarribae7fae94bb4ff
diff --git a/docs/source/color.rst b/docs/source/color.rst
index 9cab3db4e..1ceb360bc 100644
--- a/docs/source/color.rst
+++ b/docs/source/color.rst
@@ -1,18 +1,18 @@
-Image Color Space Conversions
------------------------------
+torchgeometry.color
+-------------------
-.. currentmodule:: torchgeometry.image
+.. currentmodule:: torchgeometry.color
The functions in this section perform various color space conversions.
-.. autoclass:: RgbToGrayscale
+.. autofunction:: normalize
.. autofunction:: rgb_to_grayscale
-.. autoclass:: RgbToHsv
.. autofunction:: rgb_to_hsv
-.. autoclass:: RgbToBgr
.. autofunction:: rgb_to_bgr
-.. autoclass:: BgrToRgb
.. autofunction:: bgr_to_rgb
.. autoclass:: Normalize
-.. autofunction:: normalize
+.. autoclass:: RgbToGrayscale
+.. autoclass:: RgbToHsv
+.. autoclass:: RgbToBgr
+.. autoclass:: BgrToRgb
diff --git a/docs/source/filtering.rst b/docs/source/filters.rst
similarity index 73%
rename from docs/source/filtering.rst
rename to docs/source/filters.rst
index 1b7efad79..dcd55e254 100644
--- a/docs/source/filtering.rst
+++ b/docs/source/filters.rst
@@ -1,7 +1,7 @@
-Image Filtering
----------------
+torchgeometry.filters
+---------------------
-.. currentmodule:: torchgeometry.image
+.. currentmodule:: torchgeometry.filters
The functions in this sections perform various image filtering operations.
@@ -14,8 +14,6 @@ The functions in this sections perform various image filtering operations.
.. autofunction:: median_blur
.. autofunction:: gaussian_blur
.. autofunction:: laplacian
-.. autofunction:: pyrdown
-.. autofunction:: pyrup
.. autofunction:: sobel
.. autofunction:: spatial_gradient
@@ -23,7 +21,5 @@ The functions in this sections perform various image filtering operations.
.. autoclass:: MedianBlur
.. autoclass:: GaussianBlur
.. autoclass:: Laplacian
-.. autoclass:: PyrDown
-.. autoclass:: PyrUp
.. autoclass:: Sobel
-.. autoclass:: spatial_gradient
+.. autoclass:: SpatialGradient
diff --git a/docs/source/image.rst b/docs/source/image.rst
deleted file mode 100644
index c93c0a5aa..000000000
--- a/docs/source/image.rst
+++ /dev/null
@@ -1,8 +0,0 @@
-torchgeometry.image
-===================
-
-.. toctree::
- :maxdepth: 2
-
- filtering
- color
diff --git a/docs/source/index.rst b/docs/source/index.rst
index e5c45a3ca..67e6aa392 100644
--- a/docs/source/index.rst
+++ b/docs/source/index.rst
@@ -21,8 +21,10 @@ TGM focuses on Image and tensor warping functions such as:
:caption: Package Reference
core
- image
+ color
feature
+ filters
+ transform
losses
metrics
contrib
diff --git a/docs/source/transform.rst b/docs/source/transform.rst
new file mode 100644
index 000000000..ed434335c
--- /dev/null
+++ b/docs/source/transform.rst
@@ -0,0 +1,10 @@
+torchgeometry.transform
+-----------------------
+
+.. currentmodule:: torchgeometry.transform
+
+.. autofunction:: pyrdown
+.. autofunction:: pyrup
+
+.. autoclass:: PyrDown
+.. autoclass:: PyrUp
diff --git a/mypy_files.txt b/mypy_files.txt
index 26f7a9a13..331316af0 100644
--- a/mypy_files.txt
+++ b/mypy_files.txt
@@ -4,11 +4,14 @@ torchgeometry/core/pinhole.py
torchgeometry/core/imgwarp.py
torchgeometry/core/crop.py
torchgeometry/core/affine.py
-torchgeometry/image/gaussian.py
-torchgeometry/image/color.py
-torchgeometry/image/laplacian.py
-torchgeometry/image/pyramid.py
-torchgeometry/image/sobel.py
+torchgeometry/transform/pyramid.py
+torchgeometry/feature/harris.py
+torchgeometry/filters/gaussian.py
+torchgeometry/filters/laplacian.py
+torchgeometry/filters/sobel.py
+torchgeometry/filters/blur.py
+torchgeometry/filters/median.py
+torchgeometry/color/_color.py
torchgeometry/losses/ssim.py
torchgeometry/losses/dice.py
torchgeometry/losses/focal.py
diff --git a/test/test_blur.py b/test/filters/test_blur.py
similarity index 82%
rename from test/test_blur.py
rename to test/filters/test_blur.py
index 000961953..f483b5041 100644
--- a/test/test_blur.py
+++ b/test/filters/test_blur.py
@@ -12,12 +12,12 @@ import utils # test utils
class TestBoxBlur:
def test_shape(self):
inp = torch.zeros(1, 3, 4, 4)
- blur = tgm.image.BoxBlur((3, 3))
+ blur = tgm.filters.BoxBlur((3, 3))
assert blur(inp).shape == (1, 3, 4, 4)
def test_shape_batch(self):
inp = torch.zeros(2, 6, 4, 4)
- blur = tgm.image.BoxBlur((3, 3))
+ blur = tgm.filters.BoxBlur((3, 3))
assert blur(inp).shape == (2, 6, 4, 4)
def test_kernel_3x3(self):
@@ -30,7 +30,7 @@ class TestBoxBlur:
]]])
kernel_size = (3, 3)
- actual = tgm.image.box_blur(inp, kernel_size)
+ actual = tgm.filters.box_blur(inp, kernel_size)
assert_allclose(actual[0, 0, 1, 1:4], torch.tensor(1.))
def test_kernel_5x5(self):
@@ -43,7 +43,7 @@ class TestBoxBlur:
]]])
kernel_size = (5, 5)
- actual = tgm.image.box_blur(inp, kernel_size)
+ actual = tgm.filters.box_blur(inp, kernel_size)
assert_allclose(actual[0, 0, 1, 2], torch.tensor(1.))
def test_kernel_5x5_batch(self):
@@ -57,23 +57,23 @@ class TestBoxBlur:
]]]).repeat(batch_size, 1, 1, 1)
kernel_size = (5, 5)
- actual = tgm.image.box_blur(inp, kernel_size)
+ actual = tgm.filters.box_blur(inp, kernel_size)
assert_allclose(actual[0, 0, 1, 2], torch.tensor(1.))
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.box_blur, (img, (3, 3),),
+ assert gradcheck(tgm.filters.box_blur, (img, (3, 3),),
raise_exception=True)
def test_jit(self):
@torch.jit.script
def op_script(input: torch.Tensor,
kernel_size: Tuple[int, int]) -> torch.Tensor:
- return tgm.image.box_blur(input, kernel_size)
+ return tgm.filters.box_blur(input, kernel_size)
kernel_size = (3, 3)
img = torch.rand(2, 3, 4, 5)
actual = op_script(img, kernel_size)
- expected = tgm.image.box_blur(img, kernel_size)
+ expected = tgm.filters.box_blur(img, kernel_size)
assert_allclose(actual, expected)
diff --git a/test/filters/test_filters.py b/test/filters/test_filters.py
new file mode 100644
index 000000000..b7e67de9d
--- /dev/null
+++ b/test/filters/test_filters.py
@@ -0,0 +1,106 @@
+import pytest
+import torch
+from torch.autograd import gradcheck
+from torch.testing import assert_allclose
+from common import device_type
+
+import torchgeometry.filters as filters
+import utils
+
+
[email protected]("window_size", [5, 11])
[email protected]("sigma", [1.5, 5.0])
+def test_get_gaussian_kernel(window_size, sigma):
+ kernel = filters.get_gaussian_kernel(window_size, sigma)
+ assert kernel.shape == (window_size,)
+ assert kernel.sum().item() == pytest.approx(1.0)
+
+
[email protected]("ksize_x", [5, 11])
[email protected]("ksize_y", [3, 7])
[email protected]("sigma", [1.5, 2.1])
+def test_get_gaussian_kernel2d(ksize_x, ksize_y, sigma):
+ kernel = filters.get_gaussian_kernel2d(
+ (ksize_x, ksize_y), (sigma, sigma))
+ assert kernel.shape == (ksize_x, ksize_y)
+ assert kernel.sum().item() == pytest.approx(1.0)
+
+
+class TestGaussianBlur:
+ @pytest.mark.parametrize("batch_shape",
+ [(1, 4, 8, 15), (2, 3, 11, 7)])
+ def test_gaussian_blur(self, batch_shape, device_type):
+ kernel_size = (5, 7)
+ sigma = (1.5, 2.1)
+
+ input = torch.rand(batch_shape).to(torch.device(device_type))
+ gauss = filters.GaussianBlur(kernel_size, sigma)
+ assert gauss(input).shape == batch_shape
+
+ def test_gradcheck(self):
+ # test parameters
+ batch_shape = (2, 3, 11, 7)
+ kernel_size = (5, 3)
+ sigma = (1.5, 2.1)
+
+ # evaluate function gradient
+ input = torch.rand(batch_shape)
+ input = utils.tensor_to_gradcheck_var(input) # to var
+ assert gradcheck(filters.gaussian_blur, (input, kernel_size, sigma,),
+ raise_exception=True)
+
+ def test_jit(self):
+ @torch.jit.script
+ def op_script(img):
+
+ return filters.gaussian_blur(img, (5, 5), (1.2, 1.2))
+
+ batch_size, channels, height, width = 2, 3, 64, 64
+ img = torch.ones(batch_size, channels, height, width)
+ expected = filters.GaussianBlur((5, 5), (1.2, 1.2))(img)
+ actual = op_script(img)
+ assert_allclose(actual, expected)
+
+
[email protected]("window_size", [5])
+def test_get_laplacian_kernel(window_size):
+ kernel = filters.get_laplacian_kernel(window_size)
+ assert kernel.shape == (window_size,)
+ assert kernel.sum().item() == pytest.approx(0.0)
+
+
[email protected]("window_size", [7])
+def test_get_laplacian_kernel2d(window_size):
+ kernel = filters.get_laplacian_kernel2d(window_size)
+ assert kernel.shape == (window_size, window_size)
+ assert kernel.sum().item() == pytest.approx(0.0)
+ expected = torch.tensor([[1., 1., 1., 1., 1., 1., 1.],
+ [1., 1., 1., 1., 1., 1., 1.],
+ [1., 1., 1., 1., 1., 1., 1.],
+ [1., 1., 1., -48., 1., 1., 1.],
+ [1., 1., 1., 1., 1., 1., 1.],
+ [1., 1., 1., 1., 1., 1., 1.],
+ [1., 1., 1., 1., 1., 1., 1.]])
+ assert_allclose(expected, kernel)
+
+
+class TestLaplacian:
+ @pytest.mark.parametrize("batch_shape",
+ [(1, 4, 8, 15), (2, 3, 11, 7)])
+ def test_laplacian(self, batch_shape, device_type):
+ kernel_size = 5
+
+ input = torch.rand(batch_shape).to(torch.device(device_type))
+ laplace = filters.Laplacian(kernel_size)
+ assert laplace(input).shape == batch_shape
+
+ def test_gradcheck(self):
+ # test parameters
+ batch_shape = (2, 3, 11, 7)
+ kernel_size = 9
+
+ # evaluate function gradient
+ input = torch.rand(batch_shape)
+ input = utils.tensor_to_gradcheck_var(input)
+ assert gradcheck(filters.laplacian, (input, kernel_size,),
+ raise_exception=True)
diff --git a/test/test_median.py b/test/filters/test_median.py
similarity index 82%
rename from test/test_median.py
rename to test/filters/test_median.py
index 69dd40cde..24ddebb5a 100644
--- a/test/test_median.py
+++ b/test/filters/test_median.py
@@ -12,12 +12,12 @@ import utils # test utils
class TestMedianBlur:
def test_shape(self):
inp = torch.zeros(1, 3, 4, 4)
- median = tgm.image.MedianBlur((3, 3))
+ median = tgm.filters.MedianBlur((3, 3))
assert median(inp).shape == (1, 3, 4, 4)
def test_shape_batch(self):
inp = torch.zeros(2, 6, 4, 4)
- blur = tgm.image.BoxBlur((3, 3))
+ blur = tgm.filters.BoxBlur((3, 3))
assert blur(inp).shape == (2, 6, 4, 4)
def test_kernel_3x3(self):
@@ -36,7 +36,7 @@ class TestMedianBlur:
]]).repeat(2, 1, 1, 1)
kernel_size = (3, 3)
- actual = tgm.image.median_blur(inp, kernel_size)
+ actual = tgm.filters.median_blur(inp, kernel_size)
assert_allclose(actual[0, 0, 2, 2], torch.tensor(3.))
assert_allclose(actual[0, 1, 1, 1], torch.tensor(14.))
@@ -44,16 +44,16 @@ class TestMedianBlur:
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.median_blur, (img, (5, 3),),
+ assert gradcheck(tgm.filters.median_blur, (img, (5, 3),),
raise_exception=True)
def test_jit(self):
@torch.jit.script
def op_script(input: torch.Tensor,
kernel_size: Tuple[int, int]) -> torch.Tensor:
- return tgm.image.median_blur(input, kernel_size)
+ return tgm.filters.median_blur(input, kernel_size)
kernel_size = (3, 5)
img = torch.rand(2, 3, 4, 5)
actual = op_script(img, kernel_size)
- expected = tgm.image.median_blur(img, kernel_size)
+ expected = tgm.filters.median_blur(img, kernel_size)
assert_allclose(actual, expected)
diff --git a/test/test_sobel.py b/test/filters/test_sobel.py
similarity index 79%
rename from test/test_sobel.py
rename to test/filters/test_sobel.py
index 1339a84ee..070e97247 100644
--- a/test/test_sobel.py
+++ b/test/filters/test_sobel.py
@@ -11,12 +11,12 @@ import utils # test utils
class TestSpatialGradient:
def test_shape(self):
inp = torch.zeros(1, 3, 4, 4)
- sobel = tgm.image.SpatialGradient()
+ sobel = tgm.filters.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.SpatialGradient()
+ sobel = tgm.filters.SpatialGradient()
assert sobel(inp).shape == (2, 6, 2, 4, 4)
def test_edges(self):
@@ -36,35 +36,35 @@ class TestSpatialGradient:
[-3., -4., -3.],
]]]])
- edges = tgm.image.spatial_gradient(inp)
+ edges = tgm.filters.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.spatial_gradient, (img,),
+ assert gradcheck(tgm.filters.spatial_gradient, (img,),
raise_exception=True)
def test_jit(self):
@torch.jit.script
def op_script(input):
- return tgm.image.spatial_gradient(input)
+ return tgm.filters.spatial_gradient(input)
img = torch.rand(2, 3, 4, 5)
actual = op_script(img)
- expected = tgm.image.spatial_gradient(img)
+ expected = tgm.filters.spatial_gradient(img)
assert_allclose(actual, expected)
class TestSobel:
def test_shape(self):
inp = torch.zeros(1, 3, 4, 4)
- sobel = tgm.image.Sobel()
+ sobel = tgm.filters.Sobel()
assert sobel(inp).shape == (1, 3, 4, 4)
def test_shape_batch(self):
inp = torch.zeros(3, 2, 4, 4)
- sobel = tgm.image.Sobel()
+ sobel = tgm.filters.Sobel()
assert sobel(inp).shape == (3, 2, 4, 4)
def test_magnitude(self):
@@ -80,20 +80,20 @@ class TestSobel:
[4.2426, 4.00, 4.2426],
]]])
- edges = tgm.image.sobel(inp)
+ edges = tgm.filters.sobel(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, (img,), raise_exception=True)
+ assert gradcheck(tgm.filters.sobel, (img,), raise_exception=True)
def test_jit(self):
@torch.jit.script
def op_script(input):
- return tgm.image.sobel(input)
+ return tgm.filters.sobel(input)
img = torch.rand(2, 3, 4, 5)
actual = op_script(img)
- expected = tgm.image.sobel(img)
+ expected = tgm.filters.sobel(img)
assert_allclose(actual, expected)
diff --git a/test/test_image.py b/test/test_color.py
similarity index 69%
rename from test/test_image.py
rename to test/test_color.py
index 22b8b0c1f..8bebda844 100644
--- a/test/test_image.py
+++ b/test/test_color.py
@@ -4,70 +4,16 @@ from torch.autograd import gradcheck
from torch.testing import assert_allclose
from common import device_type
-import torchgeometry.image as image
+import torchgeometry.color as color
import utils
[email protected]("window_size", [5, 11])
[email protected]("sigma", [1.5, 5.0])
-def test_get_gaussian_kernel(window_size, sigma):
- kernel = image.get_gaussian_kernel(window_size, sigma)
- assert kernel.shape == (window_size,)
- assert kernel.sum().item() == pytest.approx(1.0)
-
-
[email protected]("ksize_x", [5, 11])
[email protected]("ksize_y", [3, 7])
[email protected]("sigma", [1.5, 2.1])
-def test_get_gaussian_kernel2d(ksize_x, ksize_y, sigma):
- kernel = image.get_gaussian_kernel2d(
- (ksize_x, ksize_y), (sigma, sigma))
- assert kernel.shape == (ksize_x, ksize_y)
- assert kernel.sum().item() == pytest.approx(1.0)
-
-
-class TestGaussianBlur:
- @pytest.mark.parametrize("batch_shape",
- [(1, 4, 8, 15), (2, 3, 11, 7)])
- def test_gaussian_blur(self, batch_shape, device_type):
- kernel_size = (5, 7)
- sigma = (1.5, 2.1)
-
- input = torch.rand(batch_shape).to(torch.device(device_type))
- gauss = image.GaussianBlur(kernel_size, sigma)
- assert gauss(input).shape == batch_shape
-
- def test_gradcheck(self):
- # test parameters
- batch_shape = (2, 3, 11, 7)
- kernel_size = (5, 3)
- sigma = (1.5, 2.1)
-
- # evaluate function gradient
- input = torch.rand(batch_shape)
- input = utils.tensor_to_gradcheck_var(input) # to var
- assert gradcheck(image.gaussian_blur, (input, kernel_size, sigma,),
- raise_exception=True)
-
- def test_jit(self):
- @torch.jit.script
- def op_script(img):
-
- return image.gaussian_blur(img, (5, 5), (1.2, 1.2))
-
- batch_size, channels, height, width = 2, 3, 64, 64
- img = torch.ones(batch_size, channels, height, width)
- expected = image.GaussianBlur((5, 5), (1.2, 1.2))(img)
- actual = op_script(img)
- assert_allclose(actual, expected)
-
-
class TestNormalize:
def test_smoke(self):
mean = [0.5]
std = [0.1]
repr = 'Normalize(mean=[0.5], std=[0.1])'
- assert str(image.Normalize(mean, std)) == repr
+ assert str(color.Normalize(mean, std)) == repr
def test_normalize(self):
@@ -79,7 +25,7 @@ class TestNormalize:
# expected output
expected = torch.tensor([0.25]).repeat(1, 2, 2).view_as(data)
- f = image.Normalize(mean, std)
+ f = color.Normalize(mean, std)
assert_allclose(f(data), expected)
def test_broadcast_normalize(self):
@@ -94,7 +40,7 @@ class TestNormalize:
# expected output
expected = torch.tensor([1.25, 1, 0.5]).repeat(2, 1, 1).view_as(data)
- f = image.Normalize(mean, std)
+ f = color.Normalize(mean, std)
assert_allclose(f(data), expected)
def test_batch_normalize(self):
@@ -109,7 +55,7 @@ class TestNormalize:
# expected output
expected = torch.tensor([1.25, 1, 0.5]).repeat(2, 1, 1).view_as(data)
- f = image.Normalize(mean, std)
+ f = color.Normalize(mean, std)
assert_allclose(f(data), expected)
def test_jit(self):
@@ -117,7 +63,7 @@ class TestNormalize:
def op_script(data: torch.Tensor, mean: torch.Tensor,
std: torch.Tensor) -> torch.Tensor:
- return image.normalize(data, mean, std)
+ return color.normalize(data, mean, std)
data = torch.ones(2, 3, 1, 1)
data += 2
@@ -126,7 +72,7 @@ class TestNormalize:
std = torch.tensor([2.0, 2.0, 2.0]).repeat(2, 1)
actual = op_script(data, mean, std)
- expected = image.normalize(data, mean, std)
+ expected = color.normalize(data, mean, std)
assert_allclose(actual, expected)
def test_gradcheck(self):
@@ -139,51 +85,7 @@ class TestNormalize:
data = utils.tensor_to_gradcheck_var(data) # to var
- assert gradcheck(image.Normalize(mean, std), (data,),
- raise_exception=True)
-
-
[email protected]("window_size", [5])
-def test_get_laplacian_kernel(window_size):
- kernel = image.get_laplacian_kernel(window_size)
- assert kernel.shape == (window_size,)
- assert kernel.sum().item() == pytest.approx(0.0)
-
-
[email protected]("window_size", [7])
-def test_get_laplacian_kernel2d(window_size):
- kernel = image.get_laplacian_kernel2d(window_size)
- assert kernel.shape == (window_size, window_size)
- assert kernel.sum().item() == pytest.approx(0.0)
- expected = torch.tensor([[1., 1., 1., 1., 1., 1., 1.],
- [1., 1., 1., 1., 1., 1., 1.],
- [1., 1., 1., 1., 1., 1., 1.],
- [1., 1., 1., -48., 1., 1., 1.],
- [1., 1., 1., 1., 1., 1., 1.],
- [1., 1., 1., 1., 1., 1., 1.],
- [1., 1., 1., 1., 1., 1., 1.]])
- assert_allclose(expected, kernel)
-
-
-class TestLaplacian:
- @pytest.mark.parametrize("batch_shape",
- [(1, 4, 8, 15), (2, 3, 11, 7)])
- def test_laplacian(self, batch_shape, device_type):
- kernel_size = 5
-
- input = torch.rand(batch_shape).to(torch.device(device_type))
- laplace = image.Laplacian(kernel_size)
- assert laplace(input).shape == batch_shape
-
- def test_gradcheck(self):
- # test parameters
- batch_shape = (2, 3, 11, 7)
- kernel_size = 9
-
- # evaluate function gradient
- input = torch.rand(batch_shape)
- input = utils.tensor_to_gradcheck_var(input)
- assert gradcheck(image.laplacian, (input, kernel_size,),
+ assert gradcheck(color.Normalize(mean, std), (data,),
raise_exception=True)
@@ -209,7 +111,7 @@ class TestRgbToHsv:
[[21.0000, 22.0000],
[22.0000, 22.0000]]])
- f = image.RgbToHsv()
+ f = color.RgbToHsv()
assert_allclose(f(data), expected, atol=1e-4, rtol=1e-5)
def test_batch_rgb_to_hsv(self):
@@ -231,7 +133,7 @@ class TestRgbToHsv:
[[21.0000, 22.0000],
[22.0000, 22.0000]]]) # 3x2x2
- f = image.RgbToHsv()
+ f = color.RgbToHsv()
data = data.repeat(2, 1, 1, 1) # 2x3x2x2
print(data.shape)
expected = expected.repeat(2, 1, 1, 1) # 2x3x2x2
@@ -252,14 +154,14 @@ class TestRgbToHsv:
data = utils.tensor_to_gradcheck_var(data) # to var
- assert gradcheck(image.RgbToHsv(), (data,),
+ assert gradcheck(color.RgbToHsv(), (data,),
raise_exception=True)
def test_jit(self):
@torch.jit.script
def op_script(data: torch.Tensor) -> torch.Tensor:
- return image.rgb_to_hsv(data)
+ return color.rgb_to_hsv(data)
data = torch.tensor([[[[21., 22.],
[22., 22.]],
@@ -270,7 +172,7 @@ class TestRgbToHsv:
[8., 8.]]]]) # 3x2x2
actual = op_script(data)
- expected = image.rgb_to_hsv(data)
+ expected = color.rgb_to_hsv(data)
assert_allclose(actual, expected)
@@ -297,7 +199,7 @@ class TestBgrToRgb:
[[1., 1.],
[1., 1.]]]) # 3x2x2
- f = image.BgrToRgb()
+ f = color.BgrToRgb()
assert_allclose(f(data), expected)
def test_batch_bgr_to_rgb(self):
@@ -339,7 +241,7 @@ class TestBgrToRgb:
[[1., 1.],
[1., 1.]]]]) # 2x3x2x2
- f = image.BgrToRgb()
+ f = color.BgrToRgb()
out = f(data)
assert_allclose(out, expected)
@@ -357,14 +259,14 @@ class TestBgrToRgb:
data = utils.tensor_to_gradcheck_var(data) # to var
- assert gradcheck(image.BgrToRgb(), (data,),
+ assert gradcheck(color.BgrToRgb(), (data,),
raise_exception=True)
def test_jit(self):
@torch.jit.script
def op_script(data: torch.Tensor) -> torch.Tensor:
- return image.bgr_to_rgb(data)
+ return color.bgr_to_rgb(data)
data = torch.tensor([[[1., 1.],
[1., 1.]],
@@ -376,7 +278,7 @@ class TestBgrToRgb:
[3., 3.]]]) # 3x2x2
actual = op_script(data)
- expected = image.bgr_to_rgb(data)
+ expected = color.bgr_to_rgb(data)
assert_allclose(actual, expected)
@@ -403,7 +305,7 @@ class TestRgbToBgr:
[[1., 1.],
[1., 1.]]]) # 3x2x2
- f = image.RgbToBgr()
+ f = color.RgbToBgr()
assert_allclose(f(data), expected)
def test_gradcheck(self):
@@ -420,14 +322,14 @@ class TestRgbToBgr:
data = utils.tensor_to_gradcheck_var(data) # to var
- assert gradcheck(image.RgbToBgr(), (data,),
+ assert gradcheck(color.RgbToBgr(), (data,),
raise_exception=True)
def test_jit(self):
@torch.jit.script
def op_script(data: torch.Tensor) -> torch.Tensor:
- return image.rgb_to_bgr(data)
+ return color.rgb_to_bgr(data)
data = torch.tensor([[[1., 1.],
[1., 1.]],
@@ -439,7 +341,7 @@ class TestRgbToBgr:
[3., 3.]]]) # 3x2x2
actual = op_script(data)
- expected = image.rgb_to_bgr(data)
+ expected = color.rgb_to_bgr(data)
assert_allclose(actual, expected)
def test_batch_rgb_to_bgr(self):
@@ -481,7 +383,7 @@ class TestRgbToBgr:
[[1., 1.],
[1., 1.]]]]) # 2x3x2x2
- f = image.RgbToBgr()
+ f = color.RgbToBgr()
out = f(data)
assert_allclose(out, expected)
@@ -490,23 +392,23 @@ class TestRgbToGrayscale:
def test_rgb_to_grayscale(self):
channels, height, width = 3, 4, 5
img = torch.ones(channels, height, width)
- assert image.RgbToGrayscale()(img).shape == (1, height, width)
+ assert color.RgbToGrayscale()(img).shape == (1, height, width)
def test_rgb_to_grayscale_batch(self):
batch_size, channels, height, width = 2, 3, 4, 5
img = torch.ones(batch_size, channels, height, width)
- assert image.RgbToGrayscale()(img).shape == \
+ assert color.RgbToGrayscale()(img).shape == \
(batch_size, 1, height, width)
def test_gradcheck(self):
batch_size, channels, height, width = 2, 3, 4, 5
img = torch.ones(batch_size, channels, height, width)
img = utils.tensor_to_gradcheck_var(img) # to var
- assert gradcheck(image.rgb_to_grayscale, (img,), raise_exception=True)
+ assert gradcheck(color.rgb_to_grayscale, (img,), raise_exception=True)
def test_jit(self):
batch_size, channels, height, width = 2, 3, 64, 64
img = torch.ones(batch_size, channels, height, width)
- gray = image.RgbToGrayscale()
- gray_traced = torch.jit.trace(image.RgbToGrayscale(), img)
+ gray = color.RgbToGrayscale()
+ gray_traced = torch.jit.trace(color.RgbToGrayscale(), img)
assert_allclose(gray(img), gray_traced(img))
diff --git a/test/test_pyramid.py b/test/transform/test_pyramid.py
similarity index 75%
rename from test/test_pyramid.py
rename to test/transform/test_pyramid.py
index 162263bdb..08961a136 100644
--- a/test/test_pyramid.py
+++ b/test/transform/test_pyramid.py
@@ -11,12 +11,12 @@ import utils # test utils
class TestPyrUp:
def test_shape(self):
inp = torch.zeros(1, 2, 4, 4)
- pyr = tgm.image.PyrUp()
+ pyr = tgm.transform.PyrUp()
assert pyr(inp).shape == (1, 2, 8, 8)
def test_shape_batch(self):
inp = torch.zeros(2, 2, 4, 4)
- pyr = tgm.image.PyrUp()
+ pyr = tgm.transform.PyrUp()
assert pyr(inp).shape == (2, 2, 8, 8)
def test_gradcheck(self):
@@ -24,27 +24,27 @@ class TestPyrUp:
img = torch.rand(batch_size, channels, height, width)
img = utils.tensor_to_gradcheck_var(img) # to var
- assert gradcheck(tgm.image.pyrup, (img,), raise_exception=True)
+ assert gradcheck(tgm.transform.pyrup, (img,), raise_exception=True)
def test_jit(self):
@torch.jit.script
def op_script(input):
- return tgm.image.pyrup(input)
+ return tgm.transform.pyrup(input)
img = torch.rand(2, 3, 4, 5)
actual = op_script(img)
- expected = tgm.image.pyrup(img)
+ expected = tgm.transform.pyrup(img)
assert_allclose(actual, expected)
class TestPyrDown:
def test_shape(self):
inp = torch.zeros(1, 2, 4, 4)
- pyr = tgm.image.PyrDown()
+ pyr = tgm.transform.PyrDown()
assert pyr(inp).shape == (1, 2, 2, 2)
def test_shape_batch(self):
inp = torch.zeros(2, 2, 4, 4)
- pyr = tgm.image.PyrDown()
+ pyr = tgm.transform.PyrDown()
assert pyr(inp).shape == (2, 2, 2, 2)
def test_gradcheck(self):
@@ -52,13 +52,13 @@ class TestPyrDown:
img = torch.rand(batch_size, channels, height, width)
img = utils.tensor_to_gradcheck_var(img) # to var
- assert gradcheck(tgm.image.pyrdown, (img,), raise_exception=True)
+ assert gradcheck(tgm.transform.pyrdown, (img,), raise_exception=True)
def test_jit(self):
@torch.jit.script
def op_script(input):
- return tgm.image.pyrdown(input)
+ return tgm.transform.pyrdown(input)
img = torch.rand(2, 3, 4, 5)
actual = op_script(img)
- expected = tgm.image.pyrdown(img)
+ expected = tgm.transform.pyrdown(img)
assert_allclose(actual, expected)
diff --git a/torchgeometry/__init__.py b/torchgeometry/__init__.py
index 1c2f87655..fe2dae3ca 100644
--- a/torchgeometry/__init__.py
+++ b/torchgeometry/__init__.py
@@ -7,12 +7,14 @@ if sys.version_info < (3, 6, 0):
from .version import __version__
from torchgeometry import core
-from torchgeometry import image
from torchgeometry import losses
from torchgeometry import contrib
from torchgeometry import utils
from torchgeometry import metrics
+from torchgeometry import color
from torchgeometry import feature
+from torchgeometry import filters
+from torchgeometry import transform
# Exposes ``torchgeometry.core`` package to top level
from .core.homography_warper import HomographyWarper, homography_warp
diff --git a/torchgeometry/color/__init__.py b/torchgeometry/color/__init__.py
new file mode 100644
index 000000000..f47cab985
--- /dev/null
+++ b/torchgeometry/color/__init__.py
@@ -0,0 +1,18 @@
+from ._color import rgb_to_grayscale, RgbToGrayscale
+from ._color import BgrToRgb, bgr_to_rgb
+from ._color import RgbToBgr, rgb_to_bgr
+from ._color import RgbToHsv, rgb_to_hsv
+from ._color import Normalize, normalize
+
+__all__ = [
+ "rgb_to_grayscale",
+ "RgbToGrayscale",
+ "BgrToRgb",
+ "bgr_to_rgb",
+ "RgbToBgr",
+ "rgb_to_bgr",
+ "RgbToHsv",
+ "rgb_to_hsv",
+ "Normalize",
+ "normalize"
+]
diff --git a/torchgeometry/image/color.py b/torchgeometry/color/_color.py
similarity index 92%
rename from torchgeometry/image/color.py
rename to torchgeometry/color/_color.py
index 9781868aa..c11a7d70c 100644
--- a/torchgeometry/image/color.py
+++ b/torchgeometry/color/_color.py
@@ -3,8 +3,8 @@ import torch.nn as nn
class RgbToHsv(nn.Module):
+ r"""Convert image from RGB to HSV.
- """Convert image from RGB to HSV.
The image data is assumed to be in the range of (0, 1).
args:
@@ -27,7 +27,7 @@ class RgbToHsv(nn.Module):
def rgb_to_hsv(image):
- """Convert an RGB image to HSV.
+ r"""Convert an RGB image to HSV.
Args:
input (torch.Tensor): RGB Image to be converted to HSV.
@@ -78,8 +78,8 @@ def rgb_to_hsv(image):
class RgbToBgr(nn.Module):
+ r"""Convert image from RGB to BGR.
- """Convert image from RGB to BGR.
The image data is assumed to be in the range of (0, 1).
args:
@@ -102,7 +102,7 @@ class RgbToBgr(nn.Module):
def rgb_to_bgr(image: torch.Tensor) -> torch.Tensor:
- """Convert a RGB image to BGR.
+ r"""Convert a RGB image to BGR.
Args:
input (torch.Tensor): RGB Image to be converted to BGR.
@@ -115,8 +115,8 @@ def rgb_to_bgr(image: torch.Tensor) -> torch.Tensor:
class BgrToRgb(nn.Module):
+ r"""Convert image from BGR to RGB.
- """convert image from BGR to RGB.
The image data is assumed to be in the range of (0, 1).
args:
@@ -139,7 +139,7 @@ class BgrToRgb(nn.Module):
def bgr_to_rgb(image: torch.Tensor) -> torch.Tensor:
- """Convert a BGR image to RGB.
+ r"""Convert a BGR image to RGB.
Args:
input (torch.Tensor): BGR Image to be converted to RGB.
@@ -194,13 +194,7 @@ class RgbToGrayscale(nn.Module):
def rgb_to_grayscale(input: torch.Tensor) -> torch.Tensor:
r"""Convert an RGB image to grayscale.
- See :class:`~torchgeometry.image.RgbToGrayscale` for details.
-
- Args:
- input (torch.Tensor): Image to be converted to grayscale.
-
- Returns:
- torch.Tensor: Grayscale version of the image.
+ See :class:`~torchgeometry.color.RgbToGrayscale` for details.
"""
if not torch.is_tensor(input):
raise TypeError("Input type is not a torch.Tensor. Got {}".format(
@@ -217,8 +211,7 @@ def rgb_to_grayscale(input: torch.Tensor) -> torch.Tensor:
class Normalize(nn.Module):
-
- """Normalize a tensor image or a batch of tensor images
+ r"""Normalize a tensor image or a batch of tensor images
with mean and standard deviation. Input must be a tensor of shape (C, H, W)
or a batch of tensors (*, C, H, W).
Given mean: ``(M1,...,Mn)`` and std: ``(S1,..,Sn)`` for ``n`` channels,
@@ -247,7 +240,7 @@ class Normalize(nn.Module):
def normalize(data: torch.Tensor, mean: torch.Tensor,
std: torch.Tensor) -> torch.Tensor:
- """Normalise the image with channel-wise mean and standard deviation.
+ r"""Normalise the image with channel-wise mean and standard deviation.
Args:
data (torch.Tensor): The image tensor to be normalised.
diff --git a/torchgeometry/contrib/max_blur_pool.py b/torchgeometry/contrib/max_blur_pool.py
index d8e683aba..16df59910 100644
--- a/torchgeometry/contrib/max_blur_pool.py
+++ b/torchgeometry/contrib/max_blur_pool.py
@@ -3,7 +3,8 @@ from typing import Tuple
import torch
import torch.nn as nn
import torch.nn.functional as F
-import torchgeometry.image as F_image
+
+from torchgeometry.transform import pyrdown
__all__ = [
"MaxBlurPool2d",
@@ -57,7 +58,7 @@ class MaxBlurPool2d(nn.Module):
padding=self.padding, stride=1)
# blur and downsample
- x_down: torch.Tensor = F_image.pyrdown(x_max)
+ x_down: torch.Tensor = pyrdown(x_max)
return x_down
diff --git a/torchgeometry/feature/harris.py b/torchgeometry/feature/harris.py
index 8cf8a9c43..456075dd4 100644
--- a/torchgeometry/feature/harris.py
+++ b/torchgeometry/feature/harris.py
@@ -4,7 +4,7 @@ import torch
import torch.nn as nn
import torch.nn.functional as F
-from torchgeometry.image import spatial_gradient, gaussian_blur
+from torchgeometry.filters import spatial_gradient, gaussian_blur
class NonMaximaSuppression2d(nn.Module):
diff --git a/torchgeometry/filters/__init__.py b/torchgeometry/filters/__init__.py
new file mode 100644
index 000000000..bca1aa3e6
--- /dev/null
+++ b/torchgeometry/filters/__init__.py
@@ -0,0 +1,27 @@
+from .gaussian import get_gaussian_kernel, get_gaussian_kernel2d
+from .gaussian import GaussianBlur, gaussian_blur
+from .laplacian import get_laplacian_kernel, get_laplacian_kernel2d
+from .laplacian import Laplacian, laplacian
+from .sobel import SpatialGradient, spatial_gradient
+from .sobel import Sobel, sobel
+from .blur import BoxBlur, box_blur
+from .median import MedianBlur, median_blur
+
+__all__ = [
+ "get_gaussian_kernel",
+ "get_gaussian_kernel2d",
+ "get_laplacian_kernel",
+ "get_laplacian_kernel2d",
+ "gaussian_blur",
+ "laplacian",
+ "sobel",
+ "spatial_gradient",
+ "box_blur",
+ "median_blur",
+ "GaussianBlur",
+ "Laplacian",
+ "SpatialGradient",
+ "Sobel",
+ "BoxBlur",
+ "MedianBlur",
+]
diff --git a/torchgeometry/image/blur.py b/torchgeometry/filters/blur.py
similarity index 96%
rename from torchgeometry/image/blur.py
rename to torchgeometry/filters/blur.py
index d2137f01a..083ccaea2 100644
--- a/torchgeometry/image/blur.py
+++ b/torchgeometry/filters/blur.py
@@ -46,7 +46,7 @@ class BoxBlur(nn.Module):
Example:
>>> input = torch.rand(2, 4, 5, 7)
- >>> blur = tgm.image.BoxBlur((3, 3))
+ >>> blur = tgm.filters.BoxBlur((3, 3))
>>> output = blur(input) # 2x4x5x7
"""
@@ -76,6 +76,6 @@ class BoxBlur(nn.Module):
def box_blur(input: torch.Tensor, kernel_size: Tuple[int, int]) -> torch.Tensor:
r"""Blurs an image using the box filter.
- See :class:`~torchgeometry.image.BoxBlur` for details.
+ See :class:`~torchgeometry.filters.BoxBlur` for details.
"""
return BoxBlur(kernel_size)(input)
diff --git a/torchgeometry/image/gaussian.py b/torchgeometry/filters/gaussian.py
similarity index 89%
rename from torchgeometry/image/gaussian.py
rename to torchgeometry/filters/gaussian.py
index 357989387..aa9dc478f 100644
--- a/torchgeometry/image/gaussian.py
+++ b/torchgeometry/filters/gaussian.py
@@ -105,7 +105,7 @@ class GaussianBlur(nn.Module):
Examples::
>>> input = torch.rand(2, 4, 5, 5)
- >>> gauss = tgm.image.GaussianBlur((3, 3), (1.5, 1.5))
+ >>> gauss = tgm.filters.GaussianBlur((3, 3), (1.5, 1.5))
>>> output = gauss(input) # 2x4x5x5
"""
@@ -153,24 +153,6 @@ def gaussian_blur(input: torch.Tensor,
float]) -> torch.Tensor:
r"""Function that blurs a tensor using a Gaussian filter.
- The operator smooths the given tensor with a gaussian kernel by convolving
- it to each channel. It suports batched operation.
-
- Arguments:
- input (torch.Tensor): the input tensor.
- kernel_size (Tuple[int, int]): the size of the kernel.
- sigma (Tuple[float, float]): the standard deviation of the kernel.
-
- Returns:
- torch.Tensor: the blurred tensor.
-
- Shape:
- - Input: :math:`(B, C, H, W)`
- - Output: :math:`(B, C, H, W)`
-
- Examples::
-
- >>> input = torch.rand(2, 4, 5, 5)
- >>> output = tgm.image.gaussian_blur(input, (3, 3), (1.5, 1.5))
+ See :class:`~torchgeometry.filters.GaussianBlur` for details.
"""
return GaussianBlur(kernel_size, sigma)(input)
diff --git a/torchgeometry/image/laplacian.py b/torchgeometry/filters/laplacian.py
similarity index 83%
rename from torchgeometry/image/laplacian.py
rename to torchgeometry/filters/laplacian.py
index 4890994c2..4d13ef963 100644
--- a/torchgeometry/image/laplacian.py
+++ b/torchgeometry/filters/laplacian.py
@@ -17,7 +17,7 @@ def laplacian_1d(window_size) -> torch.Tensor:
def get_laplacian_kernel(kernel_size: int) -> torch.Tensor:
- r"""Function that returns the coefficients of a 1D Laplacian filter
+ r"""Function that returns the coefficients of a 1D Laplacian filter.
Args:
kernel_size (int): filter size. It should be odd and positive.
@@ -26,7 +26,7 @@ def get_laplacian_kernel(kernel_size: int) -> torch.Tensor:
Tensor (float): 1D tensor with laplacian filter coefficients.
Shape:
- - Output: math:`(ksize, 0)`
+ - Output: math:`(\text{kernel_size})`
Examples::
>>> tgm.image.get_laplacian_kernel(3)
@@ -48,27 +48,27 @@ def get_laplacian_kernel2d(kernel_size: int) -> torch.Tensor:
r"""Function that returns Gaussian filter matrix coefficients.
Args:
- ksize (int): filter size should be odd.
+ kernel_size (int): filter size should be odd.
Returns:
Tensor: 2D tensor with laplacian filter matrix coefficients.
Shape:
- - Output: :math:`(ksize, ksize)`
+ - Output: :math:`(\text{kernel_size}_x, \text{kernel_size}_y)`
Examples::
>>> tgm.image.get_laplacian_kernel2d(3)
tensor([[ 1., 1., 1.],
- [ 1., -8., 1.],
- [ 1., 1., 1.]])
+ [ 1., -8., 1.],
+ [ 1., 1., 1.]])
>>> tgm.image.get_laplacian_kernel2d(5)
tensor([[ 1., 1., 1., 1., 1.],
- [ 1., 1., 1., 1., 1.],
- [ 1., 1., -24., 1., 1.],
- [ 1., 1., 1., 1., 1.],
- [ 1., 1., 1., 1., 1.]])
+ [ 1., 1., 1., 1., 1.],
+ [ 1., 1., -24., 1., 1.],
+ [ 1., 1., 1., 1., 1.],
+ [ 1., 1., 1., 1., 1.]])
"""
if not isinstance(kernel_size, int) or kernel_size % 2 == 0 or \
@@ -102,7 +102,7 @@ class Laplacian(nn.Module):
Examples::
>>> input = torch.rand(2, 4, 5, 5)
- >>> laplace = tgm.image.Laplacian(5)
+ >>> laplace = tgm.filters.Laplacian(5)
>>> output = laplace(input) # 2x4x5x5
"""
@@ -152,23 +152,6 @@ class Laplacian(nn.Module):
def laplacian(src: torch.Tensor, kernel_size: int) -> torch.Tensor:
r"""Function that returns a tensor using a Laplacian filter.
- The operator smooths the given tensor with a laplacian kernel by convolving
- it to each channel. It suports batched operation.
-
- Arguments:
- src (torch.Tensor): the input tensor.
- kernel_size (int): the size of the kernel.
-
- Returns:
- Tensor: the tensor.
-
- Shape:
- - Input: :math:`(B, C, H, W)`
- - Output: :math:`(B, C, H, W)`
-
- Examples::
-
- >>> input = torch.rand(2, 4, 5, 5)
- >>> output = tgm.image.laplacian(input, (3, 3), (1.5, 1.5))
+ See :class:`~torchgeometry.filters.Laplacian` for details.
"""
return Laplacian(kernel_size)(src)
diff --git a/torchgeometry/image/median.py b/torchgeometry/filters/median.py
similarity index 95%
rename from torchgeometry/image/median.py
rename to torchgeometry/filters/median.py
index bdf4ab975..f6f7394e0 100644
--- a/torchgeometry/image/median.py
+++ b/torchgeometry/filters/median.py
@@ -37,7 +37,7 @@ class MedianBlur(nn.Module):
Example:
>>> input = torch.rand(2, 4, 5, 7)
- >>> blur = tgm.image.MedianBlur((3, 3))
+ >>> blur = tgm.filters.MedianBlur((3, 3))
>>> output = blur(input) # 2x4x5x7
"""
@@ -75,6 +75,6 @@ def median_blur(input: torch.Tensor,
kernel_size: Tuple[int, int]) -> torch.Tensor:
r"""Blurs an image using the median filter.
- See :class:`~torchgeometry.image.MedianBlur` for details.
+ See :class:`~torchgeometry.filters.MedianBlur` for details.
"""
return MedianBlur(kernel_size)(input)
diff --git a/torchgeometry/image/sobel.py b/torchgeometry/filters/sobel.py
similarity index 89%
rename from torchgeometry/image/sobel.py
rename to torchgeometry/filters/sobel.py
index 50964e5e0..eeb4f284d 100644
--- a/torchgeometry/image/sobel.py
+++ b/torchgeometry/filters/sobel.py
@@ -32,7 +32,7 @@ class SpatialGradient(nn.Module):
Examples:
>>> input = torch.rand(1, 3, 4, 4)
- >>> output = tgm.image.SpatialGradient()(input) # 1x3x2x4x4
+ >>> output = tgm.filters.SpatialGradient()(input) # 1x3x2x4x4
"""
def __init__(self) -> None:
@@ -66,8 +66,7 @@ class Sobel(nn.Module):
r"""Computes the Sobel operator and returns the magnitude per channel.
Return:
- torch.Tensor: the sobel edge gradient maginitudes map. The output
- tensor shape is BxCxHxW.
+ torch.Tensor: the sobel edge gradient maginitudes map.
Shape:
- Input: :math:`(B, C, H, W)`
@@ -75,7 +74,7 @@ class Sobel(nn.Module):
Examples:
>>> input = torch.rand(1, 3, 4, 4)
- >>> output = tgm.image.Sobel()(input) # 1x3x4x4
+ >>> output = tgm.filters.Sobel()(input) # 1x3x4x4
"""
def __init__(self) -> None:
@@ -105,9 +104,9 @@ class Sobel(nn.Module):
def spatial_gradient(input: torch.Tensor) -> torch.Tensor:
r"""Computes the first order image derivative in both x and y using a Sobel
- operator.
+ operator.
- See :class:`~torchgeometry.image.SpatialGradient` for details.
+ See :class:`~torchgeometry.filters.SpatialGradient` for details.
"""
return SpatialGradient()(input)
@@ -115,6 +114,6 @@ def spatial_gradient(input: torch.Tensor) -> torch.Tensor:
def sobel(input: torch.Tensor) -> torch.Tensor:
r"""Computes the Sobel operator and returns the magnitude per channel.
- See :class:`~torchgeometry.image.Sobel` for details.
+ See :class:`~torchgeometry.filters.Sobel` for details.
"""
return Sobel()(input)
diff --git a/torchgeometry/image/__init__.py b/torchgeometry/image/__init__.py
deleted file mode 100644
index 7211dd17c..000000000
--- a/torchgeometry/image/__init__.py
+++ /dev/null
@@ -1,13 +0,0 @@
-from .gaussian import get_gaussian_kernel, get_gaussian_kernel2d
-from .gaussian import GaussianBlur, gaussian_blur
-from .laplacian import get_laplacian_kernel, get_laplacian_kernel2d
-from .laplacian import Laplacian, laplacian
-from .color import rgb_to_grayscale, RgbToGrayscale
-from .color import BgrToRgb, bgr_to_rgb
-from .color import RgbToBgr, rgb_to_bgr
-from .color import RgbToHsv, rgb_to_hsv
-from .color import Normalize, normalize
-from .pyramid import pyrdown, pyrup, PyrDown, PyrUp
-from .sobel import SpatialGradient, Sobel, sobel, spatial_gradient
-from .blur import BoxBlur, box_blur
-from .median import MedianBlur, median_blur
diff --git a/torchgeometry/losses/ssim.py b/torchgeometry/losses/ssim.py
index b1548748b..ea4c10463 100644
--- a/torchgeometry/losses/ssim.py
+++ b/torchgeometry/losses/ssim.py
@@ -4,7 +4,7 @@ import torch
import torch.nn as nn
import torch.nn.functional as F
-from torchgeometry.image import get_gaussian_kernel2d
+from torchgeometry.filters import get_gaussian_kernel2d
class SSIM(nn.Module):
diff --git a/torchgeometry/transform/__init__.py b/torchgeometry/transform/__init__.py
new file mode 100644
index 000000000..c2be9260f
--- /dev/null
+++ b/torchgeometry/transform/__init__.py
@@ -0,0 +1,8 @@
+from .pyramid import pyrdown, pyrup, PyrDown, PyrUp
+
+__all__ = [
+ "PyrDown",
+ "PyrUp",
+ "pyrdown",
+ "pyrup",
+]
diff --git a/torchgeometry/image/pyramid.py b/torchgeometry/transform/pyramid.py
similarity index 84%
rename from torchgeometry/image/pyramid.py
rename to torchgeometry/transform/pyramid.py
index d1ba17b79..eb9981a9a 100644
--- a/torchgeometry/image/pyramid.py
+++ b/torchgeometry/transform/pyramid.py
@@ -2,13 +2,6 @@ import torch
import torch.nn as nn
import torch.nn.functional as F
-__all__ = [
- "PyrDown",
- "PyrUp",
- "pyrdown",
- "pyrup",
-]
-
def _get_pyramid_gaussian_kernel() -> torch.Tensor:
"""Utility function that return a pre-computed gaussian kernel."""
@@ -25,15 +18,18 @@ class PyrDown(nn.Module):
r"""Blurs a tensor and downsamples it.
Args:
- input (torch.Tensor): the tensor to be downsampled. The tensor must be
- in the shape of BxCxHxW.
+ input (torch.Tensor): the tensor to be downsampled.
Return:
torch.Tensor: the downsampled tensor.
+ Shape:
+ - Input: :math:`(B, C, H, W)`
+ - Output: :math:`(B, C, H / 2, W / 2)`
+
Examples:
>>> input = torch.rand(1, 2, 4, 4)
- >>> output = tgm.image.PyrDown()(input) # 1x2x2x2
+ >>> output = tgm.transform.PyrDown()(input) # 1x2x2x2
"""
def __init__(self) -> None:
@@ -65,15 +61,18 @@ class PyrUp(nn.Module):
r"""Upsamples a tensor and then blurs it.
Args:
- input (torch.Tensor): the tensor to be upsampled. The tensor must be in
- the shape of BxCxHxW.
+ input (torch.Tensor): the tensor to be upsampled.
Return:
torch.Tensor: the upsampled tensor.
+ Shape:
+ - Input: :math:`(B, C, H, W)`
+ - Output: :math:`(B, C, H * 2, W * 2)`
+
Examples:
>>> input = torch.rand(1, 2, 4, 4)
- >>> output = tgm.image.PyrUp()(input) # 1x2x8x8
+ >>> output = tgm.transform.PyrUp()(input) # 1x2x8x8
"""
def __init__(self):
@@ -108,7 +107,7 @@ class PyrUp(nn.Module):
def pyrdown(input: torch.Tensor) -> torch.Tensor:
r"""Blurs a tensor and downsamples it.
- See :class:`~torchgeometry.image.PyrDown` for details.
+ See :class:`~torchgeometry.transform.PyrDown` for details.
"""
return PyrDown()(input)
@@ -116,6 +115,6 @@ def pyrdown(input: torch.Tensor) -> torch.Tensor:
def pyrup(input: torch.Tensor) -> torch.Tensor:
r"""Upsamples a tensor and then blurs it.
- See :class:`~torchgeometry.image.PyrUp` for details.
+ See :class:`~torchgeometry.transform.PyrUp` for details.
"""
return PyrUp()(input)