Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

improve extract_tensor_patches documentation

edgarribaac9a969e383b
diff --git a/docs/source/contrib.rst b/docs/source/contrib.rst
index 1c9c5f30c..b767378f8 100644
--- a/docs/source/contrib.rst
+++ b/docs/source/contrib.rst
@@ -4,5 +4,7 @@ torchgeometry.contrib
.. currentmodule:: torchgeometry.contrib
.. autofunction:: spatial_soft_argmax2d
+.. autofunction:: extract_tensor_patches
.. autoclass:: SpatialSoftArgmax2d
+.. autoclass:: ExtractTensorPatches
diff --git a/torchgeometry/contrib/extract_patches.py b/torchgeometry/contrib/extract_patches.py
index e0509496a..73a2b5693 100644
--- a/torchgeometry/contrib/extract_patches.py
+++ b/torchgeometry/contrib/extract_patches.py
@@ -9,6 +9,63 @@ from torch.nn.modules.utils import _pair
class ExtractTensorPatches(nn.Module):
r"""Module that extract patches from tensors and stack them.
+
+ Applies a 2D convolution over an input tensor to extract patches and stack
+ them in the depth axis of the output tensor. The function applies a
+ Depthwise Convolution by applying the same kernel for all the input planes.
+
+ In the simplest case, the output value of the operator with input size
+ :math:`(B, C, H, W)` is :math:`(B, N, C, H_{out}, W_{out})`.
+
+ where
+ - :math:`B` is the batch size.
+ - :math:`N` denotes the total number of extracted patches stacked in
+ - :math:`C` denotes the number of input channels.
+ - :math:`H`, :math:`W` the input height and width of the input in pixels.
+ - :math:`H_{out}`, :math:`W_{out}` denote to denote to the patch size
+ defined in the function signature.
+ left-right and top-bottom order.
+
+ * :attr:`window_size` is the size of the sliding window and controls the
+ shape of the output tensor and defines the shape of the output patch.
+ * :attr:`stride` controls the stride to apply to the sliding window and
+ regulates the overlapping between the extracted patches.
+ * :attr:`padding` controls the amount of implicit zeros-paddings on both
+ sizes at each dimension.
+
+ The parameters :attr:`window_size`, :attr:`stride` and :attr:`padding` can
+ be either:
+
+ - a single ``int`` -- in which case the same value is used for the
+ height and width dimension.
+ - a ``tuple`` of two ints -- in which case, the first `int` is used for
+ the height dimension, and the second `int` for the width dimension.
+
+ Arguments:
+ window_size (Union[int, Tuple[int, int]]): the size of the convolving
+ kernel and the output patch size.
+ stride (Optional[Union[int, Tuple[int, int]]]): stride of the
+ convolution. Default is 1.
+ padding (Optional[Union[int, Tuple[int, int]]]): Zero-padding added to
+ both side of the input. Default is 0.
+
+ Shape:
+ - Input: :math:`(B, C, H, W)`
+ - Output: :math:`(B, N, C, H_{out}, W_{out})`
+
+ Returns:
+ torch.Tensor: the tensor with the extracted patches.
+
+ Examples:
+ >>> input = torch.arange(9.).view(1, 1, 3, 3)
+ >>> patches = tgm.contrib.extract_tensor_patches(input, (2, 3))
+ >>> input
+ tensor([[[[0., 1., 2.],
+ [3., 4., 5.],
+ [6., 7., 8.]]]])
+ >>> patches[:, -1]
+ tensor([[[[3.0000, 4.0000, 5.0000],
+ [6.0000, 7.0000, 8.0000]]]])
"""
def __init__(