Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

add type hints and improve docs to homography warper

edgarribac3cfb019c433
diff --git a/torchgeometry/homography_warper.py b/torchgeometry/homography_warper.py
index b134e59cd..574f63893 100644
--- a/torchgeometry/homography_warper.py
+++ b/torchgeometry/homography_warper.py
@@ -1,3 +1,5 @@
+from typing import Optional, Tuple
+
import torch
import torch.nn as nn
import torch.nn.functional as F
@@ -15,7 +17,7 @@ __all__ = [
# layer api
class HomographyWarper(nn.Module):
- r"""Warps patches by homographies.
+ r"""Warps image patches or tensors by homographies.
.. math::
@@ -24,44 +26,62 @@ class HomographyWarper(nn.Module):
Args:
height (int): The height of the image to warp.
width (int): The width of the image to warp.
- padding_mode (string): Either 'zeros' to replace out of bounds with
- zeros or 'border' to choose the closest
- border data.
+ padding_mode (str): Either 'zeros' to replace out of bounds with
+ zeros or 'border' to choose the closest
+ border data.
+ normalized_coordinates (Optional[bool]): wether to use a grid with
+ normalized coordinates.
"""
- def __init__(self, height, width, padding_mode='zeros',
- normalized_coordinates=True):
+ def __init__(
+ self,
+ height: int,
+ width: int,
+ padding_mode: Optional[str] = 'zeros',
+ normalized_coordinates: Optional[bool] = True) -> None:
super(HomographyWarper, self).__init__()
- self.width = width
- self.height = height
- self.padding_mode = padding_mode
+ self.width: int = width
+ self.height: int = height
+ self.padding_mode: Optional[str] = padding_mode
+ self.normalized_coordinates: Optional[bool] = normalized_coordinates
# create base grid to compute the flow
- self.grid = create_meshgrid(
+ self.grid: torch.Tensor = create_meshgrid(
height, width, normalized_coordinates=normalized_coordinates)
- def warp_grid(self, H):
- r"""
- :param H: Homography or homographies (stacked) to transform all points
- in the grid.
- :returns: Tensor[1, Height, Width, 2] containing transformed points in
- normalized images space.
+ def warp_grid(self, dst_homo_src: torch.Tensor) -> torch.Tensor:
+ r"""Computes the grid to warp the coordinates grid by an homography.
+
+ Args:
+ dst_homo_src (torch.Tensor): Homography or homographies (stacked) to
+ transform all points in the grid. Shape of the
+ homography has to be :math:`(N, 3, 3)`.
+
+ Returns:
+ torch.Tensor: the transformed grid of shape :math:`(N, H, W, 2)`.
"""
- batch_size = H.shape[0] # expand grid to match the input batch size
- grid = self.grid.repeat(batch_size, 1, 1, 1) # NxHxWx2
- if len(H.shape) == 3: # local homography case
- H = H.view(batch_size, 1, 3, 3) # NxHxWx3x3
+ batch_size: int = dst_homo_src.shape[0]
+ device: torch.device = dst_homo_src.device
+ dtype: torch.dtype = dst_homo_src.dtype
+ # expand grid to match the input batch size
+ grid: torch.Tensor = self.grid.expand(batch_size, -1, -1, -1) # NxHxWx2
+ if len(dst_homo_src.shape) == 3: # local homography case
+ dst_homo_src = dst_homo_src.view(batch_size, 1, 3, 3) # NxHxWx3x3
# perform the actual grid transformation,
# the grid is copied to input device and casted to the same type
- flow = transform_points(H, grid.to(H.device).type_as(H)) # NxHxWx2
- return flow.view(batch_size, self.height, self.width, 2) # NxHxWx2
-
- def forward(self, patch_src, dst_homo_src):
+ flow: torch.Tensor = transform_points(
+ dst_homo_src, grid.to(device).to(dtype)) # NxHxWx2
+ return flow.view(batch_size, self.height, self.width, 2) # NxHxWx2
+
+ def forward(
+ self,
+ patch_src: torch.Tensor,
+ dst_homo_src: torch.Tensor) -> torch.Tensor:
r"""Warps an image or tensor from source into reference frame.
Args:
patch_src (torch.Tensor): The image or tensor to warp.
- Should be from source.
+ Should be from source.
dst_homo_src (torch.Tensor): The homography or stack of homographies
from source to destination. The homography assumes normalized
coordinates [-1, 1].
@@ -91,27 +111,28 @@ class HomographyWarper(nn.Module):
# functional api
-def homography_warp(patch, dst_H_src, dsize, padding_mode='zeros'):
- r"""
- .. note:: Functional API for :class:`torgeometry.HomographyWarper`
+def homography_warp(patch_src: torch.Tensor,
+ dst_H_src: torch.Tensor,
+ dsize: Tuple[int,
+ int],
+ padding_mode: Optional[str] = 'zeros'):
+ r"""Function that warps image patchs or tensors by homographies.
- Warps patches by homographies.
+ See :class:`~torchgeometry.HomographyWarper` for details.
Args:
- patch (Tensor): The image or tensor to warp. Should be from source.
- dst_homo_src (Tensor): The homography or stack of homographies from
- source to destination.
- dsize (tuple): The height and width of the image to warp.
- padding_mode (string): Either 'zeros' to replace out of bounds with
- zeros or 'border' to choose the closest border
- data.
+ patch_src (torch.Tensor): The image or tensor to warp. Should be from
+ source of shape :math:`(N, C, H, W)`.
+ dst_homo_src (torch.Tensor): The homography or stack of homographies
+ from source to destination of shape
+ :math:`(N, 3, 3)`.
+ dsize (Tuple[int, int]): The height and width of the image to warp.
+ padding_mode (Optional[string]): Either 'zeros' to replace out of
+ bounds with zeros or 'border' to
+ choose the closest border data.
Return:
- Tensor: Patch sampled at locations from source to destination.
-
- Shape:
- - Input: :math:`(N, C, H, W)` and :math:`(N, 3, 3)`
- - Output: :math:`(N, C, H, W)`
+ torch.Tensor: Patch sampled at locations from source to destination.
Example:
>>> input = torch.rand(1, 3, 32, 32)
@@ -120,4 +141,4 @@ def homography_warp(patch, dst_H_src, dsize, padding_mode='zeros'):
"""
height, width = dsize
warper = HomographyWarper(height, width, padding_mode)
- return warper(patch, dst_H_src)
+ return warper(patch_src, dst_H_src)