Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

implement PyrDown and PyrUp

edgarriba32511a901df6
diff --git a/test/test_pyramid.py b/test/test_pyramid.py
new file mode 100644
index 000000000..162263bdb
--- /dev/null
+++ b/test/test_pyramid.py
@@ -0,0 +1,64 @@
+import pytest
+
+import torch
+import torchgeometry as tgm
+from torch.testing import assert_allclose
+from torch.autograd import gradcheck
+
+import utils # test utils
+
+
+class TestPyrUp:
+ def test_shape(self):
+ inp = torch.zeros(1, 2, 4, 4)
+ pyr = tgm.image.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()
+ assert pyr(inp).shape == (2, 2, 8, 8)
+
+ 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.pyrup, (img,), raise_exception=True)
+
+ def test_jit(self):
+ @torch.jit.script
+ def op_script(input):
+ return tgm.image.pyrup(input)
+ img = torch.rand(2, 3, 4, 5)
+ actual = op_script(img)
+ expected = tgm.image.pyrup(img)
+ assert_allclose(actual, expected)
+
+
+class TestPyrDown:
+ def test_shape(self):
+ inp = torch.zeros(1, 2, 4, 4)
+ pyr = tgm.image.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()
+ assert pyr(inp).shape == (2, 2, 2, 2)
+
+ 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.pyrdown, (img,), raise_exception=True)
+
+ def test_jit(self):
+ @torch.jit.script
+ def op_script(input):
+ return tgm.image.pyrdown(input)
+ img = torch.rand(2, 3, 4, 5)
+ actual = op_script(img)
+ expected = tgm.image.pyrdown(img)
+ assert_allclose(actual, expected)
diff --git a/torchgeometry/image/__init__.py b/torchgeometry/image/__init__.py
index 9d0486c4f..129b3e39c 100644
--- a/torchgeometry/image/__init__.py
+++ b/torchgeometry/image/__init__.py
@@ -4,3 +4,4 @@ from .normalization import Normalize, normalize
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
diff --git a/torchgeometry/image/pyramid.py b/torchgeometry/image/pyramid.py
new file mode 100644
index 000000000..d4fa13efb
--- /dev/null
+++ b/torchgeometry/image/pyramid.py
@@ -0,0 +1,101 @@
+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."""
+ return torch.tensor([
+ [1., 4., 6., 4., 1.],
+ [4., 16., 24., 16., 4.],
+ [6., 24., 36., 24., 6.],
+ [4., 16., 24., 16., 4.],
+ [1., 4., 6., 4., 1.]
+ ]) / 256.
+
+
+class PyrDown(nn.Module):
+ r"""Blurs a tensor and downsamples it.
+
+ Args:
+ x (torch.Tensor): the tensor to be downsampled. The tensor must be in
+ the shape of BxCxHxW.
+
+ Return:
+ torch.Tensor: the downsampled tensor.
+ """
+
+ def __init__(self) -> None:
+ super(PyrDown, self).__init__()
+ self.kernel: torch.Tensor = _get_pyramid_gaussian_kernel()
+
+ def forward(self, x: torch.Tensor) -> torch.Tensor: # type: ignore
+ # prepare kernel
+ b, c, h, w = x.shape
+ tmp_kernel: torch.Tensor = self.kernel.to(x.device).to(x.dtype)
+ kernel: torch.Tensor = tmp_kernel.repeat(c, 1, 1, 1)
+
+ # blur image
+ x_blur: torch.Tensor = F.conv2d(
+ x, kernel, padding=2, stride=1, groups=c)
+
+ # reject even rows and columns.
+ out: torch.Tensor = x_blur[..., ::2, ::2]
+ return out
+
+
+class PyrUp(nn.Module):
+ r"""Upsamples a tensor and then blurs it.
+
+ Args:
+ x (torch.Tensor): the tensor to be upsampled. The tensor must be in
+ the shape of BxCxHxW.
+
+ Return:
+ torch.Tensor: the upsampled tensor.
+ """
+
+ def __init__(self):
+ super(PyrUp, self).__init__()
+ self.kernel: torch.Tensor = _get_pyramid_gaussian_kernel()
+
+ def forward(self, x: torch.Tensor) -> torch.Tensor: # type: ignore
+ # prepare kernel
+ b, c, height, width = x.shape
+ tmp_kernel: torch.Tensor = self.kernel.to(x.device).to(x.dtype)
+ kernel: torch.Tensor = tmp_kernel.repeat(c, 1, 1, 1)
+
+ # upsample tensor
+ x_up: torch.Tensor = F.interpolate(x, size=(height * 2, width * 2),
+ mode='bilinear', align_corners=True)
+
+ # blurs upsampled tensor
+ x_blur: torch.Tensor = F.conv2d(
+ x_up, kernel, padding=2, stride=1, groups=c)
+ return x_blur
+
+
+# functiona api
+
+
+def pyrdown(x: torch.Tensor) -> torch.Tensor:
+ r"""Blurs a tensor and downsamples it.
+
+ See :class:`~torchgeometry.image.PyrDown` for details.
+ """
+ return PyrDown()(x)
+
+
+def pyrup(x: torch.Tensor) -> torch.Tensor:
+ r"""Upsamples a tensor and then blurs it.
+
+ See :class:`~torchgeometry.image.PyrUp` for details.
+ """
+ return PyrUp()(x)