π Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
add documentation for homography and depth warpers
edgarribad2c08a6188cf
diff --git a/torchgeometry/depth_warper.py b/torchgeometry/depth_warper.pyindex 28e3c9968..34c9e981f 100644--- a/torchgeometry/depth_warper.py+++ b/torchgeometry/depth_warper.py@@ -13,8 +13,28 @@ __all__ = [class DepthWarper(nn.Module):"""Warps a patch by inverse depth.- """+ .. math::+ H_{ref}^{i} = K_{i} * T_{ref}^{i} * K_{ref}^{-1}++ I_{ref} = \\omega(I_{i}, H_{ref}^{i}, \\xi_{ref})++ Args:+ pinholes (Tensor): The pinhole models for ith frame with shape `[Nx12]`.+ width (int): The width of the image to warp. Optional.+ height (int): The height of the image to warp. Optional.++ .. note::+ The pinhole model is represented in a single vector as follows:++ .. math::+ pinhole = (f_x, f_y, c_x, c_y, height, width, r_x, r_y, r_z, t_x, t_y, t_z)++ where:+ :math:`(r_x, r_y, r_z)` is the rotation vector in angle-axis convention.++ :math:`(t_x, t_y, t_z)` is the translation vector.+ """def __init__(self, pinholes, width=None, height=None):super(DepthWarper, self).__init__()# TODO: add type and value checkings@@ -115,16 +135,65 @@ accurate sampling of the depth cost volume, per camera.flows = flow.view(n, c, height, width) # Nx2xHxWreturn flows.permute(0, 2, 3, 1) # NxHxWx2- def forward(self, inv_depth_ref, data):+ def forward(self, inv_depth_ref, patch_i):+ """Warps an image or tensor from ith frame to reference given the inverse depth in the reference frame.++ Args:+ inv_depth_ref (Tensor): The inverse depth in the reference frame.+ patch_i (Tensor): The patch in the it frame.++ Return:+ Tensor: The warped data from ith frame to reference.++ Shape:+ - Input: :math:`(N, 1, H, W)` and :math:`(N, C, H, W)`+ - Output: :math:`(N, C, H, W)`++ Example:+ >>> # image in ith frame+ >>> img_i = torch.rand(1, 3, 32, 32) # NxCxHxW+ >>> # pinholes models for camera i and reference+ >>> pinholes_i = torch.Tensor([1, 12]) # Nx12+ >>> pinhole_ref = torch.Tensor([1, 12]), # Nx12+ >>> # create the depth warper and compute the homographies+ >>> warper = tgm.DepthWarper(pinholes_i)+ >>> warper.compute_homographies(pinhole_ref)+ >>> # warper the ith frame to reference by inverse depth in the reference+ >>> inv_depth_ref = torch.ones(1, 1, 32, 32) # Nx1xHxW+ >>> img_ref = warper(inv_depth_ref, img_i) # NxCxHxW+ """# TODO: add type and value checkings- return torch.nn.functional.grid_sample(data, self.warp(inv_depth_ref))+ return torch.nn.functional.grid_sample(patch_i, self.warp(inv_depth_ref))# functional apidef depth_warp(pinholes_i, pinhole_ref, inv_depth_ref, patch_i, \width=None, height=None):- """Functional API for :class:`torgeometry.DepthWarper`.+ """+ .. note::+ Functional API for :class:`torgeometry.DepthWarper`.++ Warps a patch by inverse depth.++ Args:+ pinholes_i (Tensor): The pinhole models for ith frame with shape `[Nx12]`.+ pinholes_ref (Tensor): The pinhole models for the reference frame with shape `[1x12]`.+ inv_depth_ref (Tensor): The inverse depth in the reference frame.+ patch_i (Tensor): The patch data in the ith frame.++ Return:+ Tensor: The warped data from ith frame to reference.++ Example:+ >>> # image in ith frame+ >>> img_i = torch.rand(1, 3, 32, 32) # NxCxHxW+ >>> # pinholes models for camera i and reference+ >>> pinholes_i = torch.Tensor([1, 12]) # Nx12+ >>> pinhole_ref = torch.Tensor([1, 12]), # Nx12+ >>> # warp the ith frame to reference by inverse depth in the reference+ >>> inv_depth_ref = torch.ones(1, 1, 32, 32) # Nx1xHxW+ >>> img_ref = tgm.depth_warp(pinholes_i, pinhole_ref, inv_depth_ref, img_i) # NxCxHxW"""warper = DepthWarper(pinholes_i, width=width, height=height)warper.compute_homographies(pinhole_ref)diff --git a/torchgeometry/homography_warper.py b/torchgeometry/homography_warper.pyindex e6b7bfcb2..f5000bdab 100644--- a/torchgeometry/homography_warper.py+++ b/torchgeometry/homography_warper.py@@ -30,18 +30,20 @@ def create_meshgrid(height, width, normalized_coordinates=True):# layer apiclass HomographyWarper(nn.Module):- '''- Warps patches by homographies.-height, width '''+ """Warps patches by homographies.- def __init__(self, height, width, points=None):- '''- :param width: The width of the image to warp.- :param height: The height of the image to warp.- :param points: Tensor[3, N] of homogeneous points in normalized image+ .. math::++ X_{dst} = H_{dst}^{src} * X_{src}++ Args:+ width (int): The width of the image to warp.+ height (int): The height of the image to warp.+ points (Tensor): Tensor[3, N] of homogeneous points in normalized imagespace [-1, 1] to sample. Optional parameter.- '''- nn.Module.__init__(self)+ """+ def __init__(self, height, width, points=None):+ super(HomographyWarper, self).__init__()if points is not None:assert points.size(0) == 3, "Points must be 3xN"self.width = points.size(1)@@ -100,22 +102,33 @@ height, width '''return F.grid_sample(image, grid, mode='bilinear', padding_mode=padding_mode)- def forward(self, patch, dst_H_src, padding_mode='zeros'):- """- Warps an image from dst into src's reference frame.- :param patch: Tensor[X,Y,Height,Width] The image to warp.- Should be from dst.- :param dst_H_src: Tensor[3,3] The homography from src to dst.- :param padding_mode: Either 'zeros' to replace out of bounds with zeros+ def forward(self, patch, dst_homo_src, padding_mode='zeros'):+ """Warps an image or tensor from source into reference frame.++ 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.+ padding_mode (string): Either 'zeros' to replace out of bounds with zerosor 'border' to choose the closest border data.- :returns: Tensor[X,Y,Height,Width] patch sampled at locations from src- warped to dst.++ 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)`++ Example:+ >>> input = torch.rand(1, 3, 32, 32)+ >>> homography = torch.eye(3).view(1, 3, 3)+ >>> warper = tgm.HomographyWarper(32, 32)+ >>> output = warper(input, homography) # NxCxHxW"""- if not dst_H_src.device == patch.device:+ if not dst_homo_src.device == patch.device:raise TypeError("Patch and homography must be on the same device. \Got patch.device: {} dst_H_src.device: {}."- .format(patch.device, dst_H_src.device))- return F.grid_sample(patch, self.warp_grid(dst_H_src), 'bilinear',+ .format(patch.device, dst_homo_src.device))+ return F.grid_sample(patch, self.warp_grid(dst_homo_src), 'bilinear',padding_mode=padding_mode)# functional api@@ -123,6 +136,29 @@ height, width '''def homography_warp(patch, dst_H_src, dsize, points=None,padding_mode='zeros'):"""Functional API for :class:`torgeometry.HomographyWarper`.++ Warps patches by homographies.++ 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.+ points (Tensor): Tensor[3, N] of homogeneous points in normalized image+ space [-1, 1] to sample. Optional parameter.+ padding_mode (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)`++ Example:+ >>> input = torch.rand(1, 3, 32, 32)+ >>> homography = torch.eye(3).view(1, 3, 3)+ >>> output = tgm.homography_warp(input, homography, (32, 32)) # NxCxHxW"""height, width = dsizewarper = HomographyWarper(height, width, points)