Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

implement multiple window_size in extract_tensor_patches

edgarribad864f0a6be9a
diff --git a/test/test_contrib.py b/test/test_contrib.py
index 179e9b9a9..11c1b7b3a 100644
--- a/test/test_contrib.py
+++ b/test/test_contrib.py
@@ -13,7 +13,7 @@ class TestExtractTensorPatches:
m = tgm.contrib.ExtractTensorPatches(3)
assert m(input).shape == (1, 4, 1, 3, 3)
- def _test_b1_ch1_h4w4_ws3_4n(self):
+ def _test_b1_ch1_h4w4_ws3(self):
input = torch.arange(16.).view(1, 1, 4, 4)
m = tgm.contrib.ExtractTensorPatches(3)
patches = m(input)
@@ -23,7 +23,7 @@ class TestExtractTensorPatches:
assert utils.check_equal_torch(input[0, :, 1:, :3], patches[0, 2])
assert utils.check_equal_torch(input[0, :, 1:, 1:], patches[0, 3])
- def _test_b1_ch2_h4w4_ws3_4n(self):
+ def _test_b1_ch2_h4w4_ws3(self):
input = torch.arange(16.).view(1, 1, 4, 4)
input = input.expand(-1, 2, -1, -1) # copy all channels
m = tgm.contrib.ExtractTensorPatches(3)
@@ -34,20 +34,15 @@ class TestExtractTensorPatches:
assert utils.check_equal_torch(input[0, :, 1:, :3], patches[0, 2])
assert utils.check_equal_torch(input[0, :, 1:, 1:], patches[0, 3])
- def _test_b1_ch1_h4w4_ws2_4n(self):
+ def _test_b1_ch1_h4w4_ws2(self):
input = torch.arange(16.).view(1, 1, 4, 4)
m = tgm.contrib.ExtractTensorPatches(2)
patches = m(input)
assert patches.shape == (1, 9, 1, 2, 2)
- assert utils.check_equal_torch(input[0, :, 0:2, 0:2], patches[0, 0])
assert utils.check_equal_torch(input[0, :, 0:2, 1:3], patches[0, 1])
assert utils.check_equal_torch(input[0, :, 0:2, 2:4], patches[0, 2])
- assert utils.check_equal_torch(input[0, :, 1:3, 0:2], patches[0, 3])
assert utils.check_equal_torch(input[0, :, 1:3, 1:3], patches[0, 4])
- assert utils.check_equal_torch(input[0, :, 1:3, 2:4], patches[0, 5])
- assert utils.check_equal_torch(input[0, :, 2:4, 0:2], patches[0, 6])
assert utils.check_equal_torch(input[0, :, 2:4, 1:3], patches[0, 7])
- assert utils.check_equal_torch(input[0, :, 2:4, 2:4], patches[0, 8])
def _test_b1_ch1_h4w4_ws2_stride2(self):
input = torch.arange(16.).view(1, 1, 4, 4)
@@ -64,11 +59,9 @@ class TestExtractTensorPatches:
m = tgm.contrib.ExtractTensorPatches(2, stride=(2, 1))
patches = m(input)
assert patches.shape == (1, 6, 1, 2, 2)
- assert utils.check_equal_torch(input[0, :, 0:2, 0:2], patches[0, 0])
assert utils.check_equal_torch(input[0, :, 0:2, 1:3], patches[0, 1])
assert utils.check_equal_torch(input[0, :, 0:2, 2:4], patches[0, 2])
assert utils.check_equal_torch(input[0, :, 2:4, 0:2], patches[0, 3])
- assert utils.check_equal_torch(input[0, :, 2:4, 1:3], patches[0, 4])
assert utils.check_equal_torch(input[0, :, 2:4, 2:4], patches[0, 5])
def _test_b1_ch1_h3w3_ws2_stride1_padding1(self):
@@ -95,6 +88,14 @@ class TestExtractTensorPatches:
assert utils.check_equal_torch(
input[i, :, 1:3, 1:3], patches[i, 10])
+ def _test_b1_ch1_h3w3_ws23(self):
+ input = torch.arange(9.).view(1, 1, 3, 3)
+ m = tgm.contrib.ExtractTensorPatches((2, 3))
+ patches = m(input)
+ assert patches.shape == (1, 2, 1, 2, 3)
+ assert utils.check_equal_torch(input[0, :, 0:2, 0:3], patches[0, 0])
+ assert utils.check_equal_torch(input[0, :, 1:3, 0:3], patches[0, 1])
+
# TODO: implement me
def _test_jit(self):
pass
@@ -107,9 +108,10 @@ class TestExtractTensorPatches:
def test_run_all(self):
self._test_smoke()
- self._test_b1_ch1_h4w4_ws3_4n()
- self._test_b1_ch2_h4w4_ws3_4n()
- self._test_b1_ch1_h4w4_ws2_4n()
+ self._test_b1_ch1_h4w4_ws3()
+ self._test_b1_ch2_h4w4_ws3()
+ self._test_b1_ch1_h4w4_ws2()
+ self._test_b1_ch1_h3w3_ws23()
self._test_b1_ch1_h4w4_ws2_stride2()
self._test_b1_ch1_h4w4_ws2_stride21()
self._test_b1_ch1_h3w3_ws2_stride1_padding1()
diff --git a/torchgeometry/contrib/extract_patches.py b/torchgeometry/contrib/extract_patches.py
index 3aba5c299..e0509496a 100644
--- a/torchgeometry/contrib/extract_patches.py
+++ b/torchgeometry/contrib/extract_patches.py
@@ -1,9 +1,11 @@
-from typing import Optional
+from typing import Optional, Union, Tuple
import torch
import torch.nn as nn
import torch.nn.functional as F
+from torch.nn.modules.utils import _pair
+
class ExtractTensorPatches(nn.Module):
r"""Module that extract patches from tensors and stack them.
@@ -11,25 +13,30 @@ class ExtractTensorPatches(nn.Module):
def __init__(
self,
- window_size: int,
- stride: int = 1,
- padding: int = 0) -> None:
+ window_size: Union[int, Tuple[int, int]],
+ stride: Optional[Union[int, Tuple[int, int]]] = 1,
+ padding: Optional[Union[int, Tuple[int, int]]] = 0) -> None:
super(ExtractTensorPatches, self).__init__()
- self.window_size: int = window_size
- self.stride: int = stride
- self.padding: int = padding
+ self.window_size: Tuple[int, int] = _pair(window_size)
+ self.stride: Tuple[int, int] = _pair(stride)
+ self.padding: Tuple[int, int] = _pair(padding)
self.eps: float = 1e-6
# create base kernel
self.kernel: torch.Tensor = self.create_kernel(self.window_size)
@staticmethod
- def create_kernel(window_size: int, eps: float = 1e-6) -> torch.Tensor:
- window_range = window_size * window_size
- kernel = torch.zeros(window_range, window_range) + eps
+ def create_kernel(
+ window_size: Tuple[int, int],
+ eps: float = 1e-6) -> torch.Tensor:
+ r"""Creates a binary kernel to extract the patches. If the window size
+ is HxW will create a (H*W)xHxW kernel.
+ """
+ window_range: int = window_size[0] * window_size[1]
+ kernel: torch.Tensor = torch.zeros(window_range, window_range) + eps
for i in range(window_range):
kernel[i, i] += 1.0
- return kernel.view(window_range, 1, window_size, window_size)
+ return kernel.view(window_range, 1, window_size[0], window_size[1])
def forward(self, input: torch.Tensor) -> torch.Tensor:
if not torch.is_tensor(input):
@@ -53,7 +60,7 @@ class ExtractTensorPatches(nn.Module):
# reshape the output tensor
output: torch.Tensor = output_tmp.view(
- batch_size, channels, self.window_size, self.window_size, -1)
+ batch_size, channels, self.window_size[0], self.window_size[1], -1)
return output.permute(0, 4, 1, 2, 3) # BxNxCxhxw
@@ -64,7 +71,9 @@ class ExtractTensorPatches(nn.Module):
def extract_tensor_patches(
input: torch.Tensor,
- window_size: int, stride: int = 1, padding: int = 0) -> torch.Tensor:
+ window_size: Union[int, Tuple[int, int]],
+ stride: Optional[Union[int, Tuple[int, int]]] = 1,
+ padding: Optional[Union[int, Tuple[int, int]]] = 0) -> torch.Tensor:
r"""Function that extract patches from tensors and stack them.
See :class:`~torchgeometry.contrib.ExtractTensorPatches` for details.