Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

Feature LAF docs fix (#2245)

Dmytro Mishkin5accb2723fcd
diff --git a/kornia/feature/affine_shape.py b/kornia/feature/affine_shape.py
index ff6ca6eed..4e3d6e409 100644
--- a/kornia/feature/affine_shape.py
+++ b/kornia/feature/affine_shape.py
@@ -46,10 +46,14 @@ class PatchAffineShapeEstimator(nn.Module):
return f"{self.__class__.__name__}(patch_size={self.patch_size}, eps={self.eps})"
def forward(self, patch: torch.Tensor) -> torch.Tensor:
- """Args:
- patch: (torch.Tensor) shape [Bx1xHxW]
+ """
+ Args:
+ patch: :math:`(B, 1, H, W)`
+
Returns:
- torch.Tensor: ellipse_shape shape [Bx1x3]"""
+ torch.Tensor: ellipse_shape :math:`(B, 1, 3)`
+
+ """
KORNIA_CHECK_SHAPE(patch, ["B", "1", "H", "W"])
self.weighting = self.weighting.to(patch.dtype).to(patch.device)
grads: torch.Tensor = self.gradient(patch) * self.weighting
@@ -118,11 +122,12 @@ class LAFAffineShapeEstimator(nn.Module):
def forward(self, laf: torch.Tensor, img: torch.Tensor) -> torch.Tensor:
"""
Args:
- laf: (torch.Tensor) shape [BxNx2x3]
- img: (torch.Tensor) shape [Bx1xHxW]
+ LAF: :math:`(B, N, 2, 3)`
+ img: :math:`(B, 1, H, W)`
Returns:
- torch.Tensor: laf_out shape [BxNx2x3]"""
+ LAF_out: :math:`(B, N, 2, 3)`
+ """
KORNIA_CHECK_LAF(laf)
KORNIA_CHECK_SHAPE(img, ["B", "1", "H", "W"])
B, N = laf.shape[:2]
@@ -209,11 +214,11 @@ class LAFAffNetShapeEstimator(nn.Module):
def forward(self, laf: torch.Tensor, img: torch.Tensor) -> torch.Tensor:
"""
Args:
- laf: shape [BxNx2x3]
- img: shape [Bx1xHxW]
+ LAF: :math:`(B, N, 2, 3)`
+ img: :math:`(B, 1, H, W)`
Returns:
- laf_out shape [BxNx2x3]
+ LAF_out: :math:`(B, N, 2, 3)`
"""
KORNIA_CHECK_LAF(laf)
KORNIA_CHECK_SHAPE(img, ["B", "1", "H", "W"])
diff --git a/kornia/feature/laf.py b/kornia/feature/laf.py
index 7739b2054..414a69a24 100644
--- a/kornia/feature/laf.py
+++ b/kornia/feature/laf.py
@@ -15,14 +15,10 @@ def get_laf_scale(LAF: Tensor) -> Tensor:
"""Return a scale of the LAFs.
Args:
- LAF: tensor [BxNx2x3] or [BxNx2x2].
+ LAF: :math:`(B, N, 2, 3)`
Returns:
- tensor BxNx1x1.
-
- Shape:
- - Input: :math: `(B, N, 2, 3)`
- - Output: :math: `(B, N, 1, 1)`
+ scale :math:`(B, N, 1, 1)`
Example:
>>> input = torch.ones(1, 5, 2, 3) # BxNx2x3
@@ -38,14 +34,10 @@ def get_laf_center(LAF: Tensor) -> Tensor:
"""Return a center (keypoint) of the LAFs.
Args:
- LAF: tensor [BxNx2x3].
+ LAF: :math:`(B, N, 2, 3)`
Returns:
- tensor BxNx2.
-
- Shape:
- - Input: :math: `(B, N, 2, 3)`
- - Output: :math: `(B, N, 2)`
+ xy :math:`(B, N, 2)`
Example:
>>> input = torch.ones(1, 5, 2, 3) # BxNx2x3
@@ -60,14 +52,10 @@ def get_laf_orientation(LAF: Tensor) -> Tensor:
"""Return orientation of the LAFs, in degrees.
Args:
- LAF: (Tensor): tensor [BxNx2x3].
+ LAF: :math:`(B, N, 2, 3)`
Returns:
- Tensor: tensor BxNx1 .
-
- Shape:
- - Input: :math: `(B, N, 2, 3)`
- - Output: :math: `(B, N, 1)`
+ angle in degrees :math:`(B, N, 1)`
Example:
>>> input = torch.ones(1, 5, 2, 3) # BxNx2x3
@@ -82,15 +70,11 @@ def set_laf_orientation(LAF: Tensor, angles_degrees: Tensor) -> Tensor:
"""Change the orientation of the LAFs.
Args:
- LAF: tensor [BxNx2x3].
- angles: tensor BxNx1, in degrees.
+ LAF: :math:`(B, N, 2, 3)`
+ angles: :math:`(B, N, 1)` in degrees.
Returns:
- tensor [BxNx2x3].
-
- Shape:
- - Input: :math: `(B, N, 2, 3)`, `(B, N, 1)`
- - Output: :math: `(B, N, 2, 3)`
+ LAF oriented with angles :math:`(B, N, 2, 3)`
"""
KORNIA_CHECK_LAF(LAF)
B, N = LAF.shape[:2]
@@ -102,15 +86,17 @@ def set_laf_orientation(LAF: Tensor, angles_degrees: Tensor) -> Tensor:
def laf_from_center_scale_ori(xy: Tensor, scale: Optional[Tensor] = None, ori: Optional[Tensor] = None) -> Tensor:
- """Return orientation of the LAFs, in radians. Useful to create kornia LAFs from OpenCV keypoints.
+ """Creates a LAF from keypoint center, scale and orientation.
+
+ Useful to create kornia LAFs from OpenCV keypoints.
Args:
- xy: tensor [BxNx2].
- scale: tensor [BxNx1x1]. If not provided, scale = 1 is assumed
- ori: tensor [BxNx1]. If not provided orientation = 0 is assumed
+ xy: :math:`(B, N, 2)`.
+ scale: :math:`(B, N, 1, 1)`. If not provided, scale = 1.0 is assumed
+ angle in degrees: :math:`(B, N, 1)`. If not provided orientation = 0 is assumed
Returns:
- tensor BxNx2x3.
+ LAF :math:`(B, N, 2, 3)`
"""
KORNIA_CHECK_SHAPE(xy, ["B", "N", "2"])
device = xy.device
@@ -133,16 +119,11 @@ def scale_laf(laf: Tensor, scale_coef: Union[float, Tensor]) -> Tensor:
So the center, shape and orientation of the local feature stays the same, but the region area changes.
Args:
- laf: tensor [BxNx2x3] or [BxNx2x2].
+ LAF :math:`(B, N, 2, 3)`
scale_coef: broadcastable tensor or float.
Returns:
- tensor BxNx2x3.
-
- Shape:
- - Input: :math:`(B, N, 2, 3)`
- - Input: :math:`(B, N,)` or ()
- - Output: :math:`(B, N, 1, 1)`
+ LAF :math:`(B, N, 2, 3)`
Example:
>>> input = torch.ones(1, 5, 2, 3) # BxNx2x3
@@ -160,15 +141,11 @@ def make_upright(laf: Tensor, eps: float = 1e-9) -> Tensor:
"""Rectify the affine matrix, so that it becomes upright.
Args:
- laf: tensor of LAFs.
- eps : for safe division.
+ LAF :math:`(B, N, 2, 3)`
+ eps: for safe division.
Returns:
- tensor of same shape.
-
- Shape:
- - Input: :math:`(B, N, 2, 3)`
- - Output: :math:`(B, N, 2, 3)`
+ LAF :math:`(B, N, 2, 3)`
Example:
>>> input = torch.ones(1, 5, 2, 3) # BxNx2x3
@@ -201,25 +178,17 @@ def ellipse_to_laf(ells: Tensor) -> Tensor:
See also https://github.com/vlfeat/vlfeat/blob/master/toolbox/sift/vl_frame2oell.m
Args:
- ells: tensor of ellipses in Oxford format [x y a b c].
+ ells: tensor :math:`(B, N, 5)` of ellipses in Oxford format [x y a b c].
Returns:
- tensor of ellipses in LAF format.
-
- Shape:
- - Input: :math:`(B, N, 5)`
- - Output: :math:`(B, N, 2, 3)`
+ LAF :math:`(B, N, 2, 3)`
Example:
>>> input = torch.ones(1, 10, 5) # BxNx5
>>> output = ellipse_to_laf(input) # BxNx2x3
"""
- n_dims = len(ells.size())
- if n_dims != 3:
- raise TypeError("ellipse shape should be must be [BxNx5]. " "Got {}".format(ells.size()))
- B, N, dim = ells.size()
- if dim != 5:
- raise TypeError("ellipse shape should be must be [BxNx5]. " "Got {}".format(ells.size()))
+ KORNIA_CHECK_SHAPE(ells, ["B", "N", "5"])
+ B, N, _ = ells.shape
# Previous implementation was incorrectly using Cholesky decomp as matrix sqrt
# ell_shape = concatenate([concatenate([ells[..., 2:3], ells[..., 3:4]], dim=2).unsqueeze(2),
# concatenate([ells[..., 3:4], ells[..., 4:5]], dim=2).unsqueeze(2)], dim=2).view(-1, 2, 2)
@@ -247,15 +216,11 @@ def laf_to_boundary_points(LAF: Tensor, n_pts: int = 50) -> Tensor:
Used for local features visualization, see visualize_laf function.
Args:
- LAF:
+ LAF: :math:`(B, N, 2, 3)`
n_pts: number of points to output.
Returns:
- tensor of boundary points.
-
- Shape:
- - Input: :math:`(B, N, 2, 3)`
- - Output: :math:`(B, N, n_pts, 2)`
+ tensor of boundary points LAF: :math:`(B, N, n_pts, 2)`
"""
KORNIA_CHECK_LAF(LAF)
B, N, _, _ = LAF.size()
@@ -277,18 +242,14 @@ def laf_to_boundary_points(LAF: Tensor, n_pts: int = 50) -> Tensor:
def get_laf_pts_to_draw(LAF: Tensor, img_idx: int = 0) -> Tuple[List[int], List[int]]:
- """Return numpy array for drawing LAFs (local features).
+ """Returns list for drawing LAFs (local features).
Args:
- LAF:
- n_pts: number of boundary points to output.
+ LAF: :math:`(B, N, 2, 3)`
+ n_pts: number of points to output.
Returns:
- tensor of boundary points.
-
- Shape:
- - Input: :math:`(B, N, 2, 3)`
- - Output: :math:`(B, N, n_pts, 2)`
+ List of boundary points x, y`
Examples:
x, y = get_laf_pts_to_draw(LAF, img_idx)
@@ -316,22 +277,18 @@ def denormalize_laf(LAF: Tensor, images: Tensor) -> Tensor:
[a21*MIN_SIZE a22*MIN_SIZE y*H]
Args:
- LAF:
- images: images, LAFs are detected in.
+ LAF: :math:`(B, N, 2, 3)`
+ images: :math:`(B, CH, H, W)`
Returns:
- the denormalized lafs.
-
- Shape:
- - Input: :math:`(B, N, 2, 3)`
- - Output: :math:`(B, N, 2, 3)`
+ the denormalized LAF: :math:`(B, N, 2, 3)`, scale in pixels
"""
KORNIA_CHECK_LAF(LAF)
_, _, h, w = images.size()
wf = float(w)
hf = float(h)
min_size = min(hf, wf)
- coef = torch.ones(1, 1, 2, 3).to(LAF.dtype).to(LAF.device) * min_size
+ coef = torch.ones(1, 1, 2, 3, dtype=LAF.dtype, device=LAF.device) * min_size
coef[0, 0, 0, 2] = wf
coef[0, 0, 1, 2] = hf
return coef.expand_as(LAF) * LAF
@@ -348,22 +305,19 @@ def normalize_laf(LAF: Tensor, images: Tensor) -> Tensor:
[a21/MIN_SIZE a22/MIN_SIZE y/H]
Args:
- LAF: (Tensor).
- images: (Tensor) images, LAFs are detected in
+ LAF: :math:`(B, N, 2, 3)`
+ images: :math:`(B, CH, H, W)`
Returns:
- LAF: (Tensor).
+ the denormalized LAF: :math:`(B, N, 2, 3)`, scale in image percentage (0, 1)
- Shape:
- - Input: :math:`(B, N, 2, 3)`
- - Output: :math:`(B, N, 2, 3)`
"""
KORNIA_CHECK_LAF(LAF)
_, _, h, w = images.size()
wf = float(w)
hf = float(h)
min_size = min(hf, wf)
- coef = torch.ones(1, 1, 2, 3).to(LAF.dtype).to(LAF.device) / min_size
+ coef = torch.ones(1, 1, 2, 3, dtype=LAF.dtype, device=LAF.device) / min_size
coef[0, 0, 0, 2] = 1.0 / wf
coef[0, 0, 1, 2] = 1.0 / hf
return coef.expand_as(LAF) * LAF
@@ -378,7 +332,7 @@ def generate_patch_grid_from_normalized_LAF(img: Tensor, LAF: Tensor, PS: int =
PS: patch size to be extracted.
Returns:
- grid
+ grid :math:`(B*N, PS, PS, 2)`
"""
KORNIA_CHECK_LAF(LAF)
B, N, _, _ = LAF.size()
@@ -402,8 +356,8 @@ def extract_patches_simple(
No smoothing applied, huge aliasing (better use extract_patches_from_pyramid).
Args:
- img: images, LAFs are detected in.
- laf:
+ img: images, LAFs are detected in :math:`(B, CH, H, W)`.
+ laf: :math:`(B, N, 2, 3)`.
PS: patch size.
normalize_lafs_before_extraction: if True, lafs are normalized to image size.
@@ -437,8 +391,8 @@ def extract_patches_from_pyramid(
Patches are extracted from appropriate pyramid level.
Args:
- laf:
- images: images, LAFs are detected in.
+ img: images, LAFs are detected in :math:`(B, CH, H, W)`.
+ laf: :math:`(B, N, 2, 3)`.
PS: patch size.
normalize_lafs_before_extraction: if True, lafs are normalized to image size.
@@ -456,7 +410,7 @@ def extract_patches_from_pyramid(
pyr_idx = scale.log2().relu().long()
cur_img = img
cur_pyr_level = 0
- out = zeros(B, N, ch, PS, PS).to(nlaf.dtype).to(nlaf.device)
+ out = zeros(B, N, ch, PS, PS, dtype=nlaf.dtype, device=nlaf.device)
while min(cur_img.size(2), cur_img.size(3)) >= PS:
_, ch, h, w = cur_img.size()
# for loop temporarily, to be refactored
diff --git a/kornia/feature/orientation.py b/kornia/feature/orientation.py
index 20b9c966f..a4b0f5e56 100644
--- a/kornia/feature/orientation.py
+++ b/kornia/feature/orientation.py
@@ -23,11 +23,12 @@ class PassLAF(nn.Module):
def forward(self, laf: torch.Tensor, img: torch.Tensor) -> torch.Tensor:
"""
Args:
- laf: 4d tensor.
- img: the input image tensor.
+ laf: :math:`(B, N, 2, 3)`
+ img: :math:`(B, 1, H, W)`
- Return:
- torch.Tensor: unchanged laf from the input."""
+ Returns:
+ LAF, unchanged :math:`(B, N, 2, 3)`
+ """
return laf
@@ -63,14 +64,14 @@ class PatchDominantGradientOrientation(nn.Module):
)
def forward(self, patch: torch.Tensor) -> torch.Tensor:
- """Args:
- patch: shape [Bx1xHxW]
+ """
+ Args:
+ patch: :math:`(B, 1, H, W)`
+
Returns:
- torch.Tensor: angle shape [B]"""
- if not isinstance(patch, torch.Tensor):
- raise TypeError(f"Input type is not a torch.Tensor. Got {type(patch)}")
- if not len(patch.shape) == 4:
- raise ValueError(f"Invalid input shape, we expect Bx1xHxW. Got: {patch.shape}")
+ angle in radians: :math:`(B)`
+ """
+ KORNIA_CHECK_SHAPE(patch, ["B", "1", "H", "W"])
_, CH, W, H = patch.size()
if (W != self.patch_size) or (H != self.patch_size) or (CH != 1):
raise TypeError(
@@ -174,10 +175,13 @@ class OriNet(nn.Module):
return (x - mp.detach()) / (sp.detach() + eps)
def forward(self, patch: torch.Tensor) -> torch.Tensor:
- """Args:
- patch: (torch.Tensor) shape [Bx1xHxW]
+ """
+ Args:
+ patch: :math:`(B, 1, H, W)`
+
Returns:
- patch: (torch.Tensor) shape [B]"""
+ angle in radians: :math:`(B)`
+ """
xy = self.features(self._normalize_input(patch)).view(-1, 2)
angle = torch.atan2(xy[:, 0] + 1e-8, xy[:, 1] + self.eps)
return angle
@@ -212,11 +216,11 @@ class LAFOrienter(nn.Module):
def forward(self, laf: torch.Tensor, img: torch.Tensor) -> torch.Tensor:
"""
Args:
- laf: shape [BxNx2x3]
- img: shape [Bx1xHxW]
+ laf: :math:`(B, N, 2, 3)`
+ img: :math:`(B, 1, H, W)`
Returns:
- laf_out, shape [BxNx2x3]
+ LAF_out: :math:`(B, N, 2, 3)`
"""
KORNIA_CHECK_LAF(laf)
KORNIA_CHECK_SHAPE(img, ["B", "C", "H", "W"])