Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

clean LAF commit

Dmytro Mishkin9bf681c4e598
diff --git a/kornia/feature/__init__.py b/kornia/feature/__init__.py
index fdf963ba1..b92342691 100644
--- a/kornia/feature/__init__.py
+++ b/kornia/feature/__init__.py
@@ -1,10 +1,30 @@
from .harris import CornerHarris, corner_harris
from .nms import NonMaximaSuppression2d, non_maxima_suppression2d
+from .laf import (extract_patches_from_pyramid,
+ extract_patches_simple,
+ visualize_LAF,
+ normalize_LAF,
+ denormalize_LAF,
+ LAF2pts,
+ ell2LAF,
+ make_upright,
+ get_laf_scale,
+ angle_to_rotation_matrix)
from .siftdesc import SIFTDescriptor
__all__ = [
"non_maxima_suppression2d",
"corner_harris",
"NonMaximaSuppression2d",
"CornerHarris",
+ "extract_patches_from_pyramid",
+ "extract_patches_simple",
+ "visualize_LAF",
+ "normalize_LAF",
+ "denormalize_LAF",
+ "LAF2pts",
+ "ell2LAF",
+ "make_upright",
+ "get_laf_scale",
+ "angle_to_rotation_matrix"
"SIFTDescriptor"
]
diff --git a/kornia/feature/laf.py b/kornia/feature/laf.py
new file mode 100644
index 000000000..d16108308
--- /dev/null
+++ b/kornia/feature/laf.py
@@ -0,0 +1,364 @@
+# flake8: noqa E127
+# flake8: noqa E128
+from typing import Tuple
+
+import kornia
+import torch
+import math
+import numpy as np
+import torch
+import torch.nn as nn
+import torch.nn.functional as F
+import matplotlib.pyplot as plt
+
+
+def angle_to_rotation_matrix(angle: torch.Tensor,
+ do_deg2rad: bool = False) -> torch.Tensor:
+ """
+ Creates a rotation matrix out of angles
+ Args:
+ angle: (torch.Tensor): tensor of angles, any shape.
+ do_deg2rad: (bool): if we should convert to radians first
+
+ Returns:
+ torch.Tensor: tensor of Nx2x2 rotation matrices.
+
+ Shape:
+ - Input: :math:`(N)`
+ - Output: :math:`(N, 2, 2)`
+
+ Example:
+ >>> input = torch.rand(1, 3) # Nx3
+ >>> output = kornia.angle_to_rotation_matrix(input) # Nx3x2x2
+ """
+ if do_deg2rad:
+ ang = kornia.deg2rad(angle)
+ else:
+ ang = angle
+ n_dims = len(ang.size())
+ cos_a = torch.cos(ang).unsqueeze(-1).unsqueeze(-1)
+ sin_a = torch.sin(ang).unsqueeze(-1).unsqueeze(-1)
+ A1_ang = torch.cat([ cos_a, sin_a], dim=n_dims + 1)
+ A2_ang = torch.cat([-sin_a, cos_a], dim=n_dims + 1)
+ return torch.cat([A1_ang, A2_ang], dim=n_dims)
+
+
+def get_laf_scale(A: torch.Tensor) -> torch.Tensor:
+ """
+ Returns a scale of the LAFs
+ Args:
+ LAF: (torch.Tensor): tensor [BxNx2x3] or [BxNx2x2].
+
+
+ Returns:
+ torch.Tensor: tensor BxNx1x1 .
+
+ Shape:
+ - Input: :math: `(B, N, 2, 3)`
+ - Output: :math: `(B, N, 1, 1)`
+
+ Example:
+ >>> input = torch.ones(1, 5, 2, 3) # BxNx2x3
+ >>> output = kornia.get_laf_scale(input) # BxNx1x1
+ """
+ n_dims = len(A.size())
+ eps = 1e-10
+ if (n_dims != 4):
+ raise TypeError(
+ "LAF shape should be must be [BxNx2x3]. "
+ "Got {}".format(A.size())
+ )
+ out = A[:, :, 0:1, 0:1] * A[:, :, 1:2, 1:2] -\
+ A[:, :, 1:2, 0:1] * A[:, :, 0:1, 1:2] + eps # noqa: E127
+ return out.abs().sqrt()
+
+
+def make_upright(A: torch.Tensor) -> torch.Tensor: # noqa: E128
+ """
+ Rectifies the affine matrix, so that it becomes upright
+ Args:
+ A: (torch.Tensor): tensor of LAFs.
+
+ Returns:
+ torch.Tensor: tensor of same shape.
+
+ Shape:
+ - Input: :math:`(B, N, 2, 3)`
+ - Output: :math:`(B, N, 2, 3)`
+ Example:
+ >>> input = torch.ones(1, 5, 2, 3) # BxNx2x3
+ >>> output = kornia.make_upright(input) # BxNx2x3
+ """
+ n_dims = len(A.size())
+ if (n_dims != 4):
+ raise TypeError(
+ "LAF shape should be must be [BxNx2x3]. "
+ "Got {}".format(A.size())
+ )
+ det = get_laf_scale(A)
+ eps = 1e-10
+ b2a2 = torch.sqrt(A[:, :, 0:1, 1:2]**2 + A[:, :, 0:1, 0:1]**2)
+ A1_ell = torch.cat([(b2a2 / det).contiguous(), # type: ignore
+ torch.zeros_like(det)], dim=3) # type: ignore
+ A2_ell = torch.cat([((A[:, :, 1:2, 1:2] * A[:, :, 0:1, 1:2] +
+ A[:, :, 1:2, 0:1] * A[:, :, 0:1, 0:1]) / (b2a2 * det)),
+ (det / b2a2).contiguous()], dim=3) # type: ignore
+ return torch.cat([torch.cat([A1_ell, A2_ell], dim=2),
+ A[:, :, :, 2:3]], dim=3)
+
+
+def ell2LAF(ells: torch.Tensor) -> torch.Tensor:
+ """
+ Converts ellipse regions to LAF format
+ Args:
+ A: (torch.Tensor): tensor of ellipses in Oxford format [x y a b c].
+
+ Returns:
+ LAF: (torch.Tensor) tensor of ellipses in LAF format.
+
+ Shape:
+ - Input: :math:`(B, N, 5)`
+ - Output: :math:`(B, N, 2, 3)`
+ Example:
+ >>> input = torch.ones(1, 10, 5) # BxNx5
+ >>> output = kornia.ell2LAF(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()))
+ ell_shape = torch.cat([torch.cat([ells[:, :, 2:3], ells[:, :, 3:4]], dim=2).unsqueeze(2),
+ torch.cat([ells[:, :, 3:4], ells[:, :, 4:5]], dim=2).unsqueeze(2)],
+ dim=2).view(-1, 2, 2)
+ out = torch.matrix_power(torch.cholesky(ell_shape, False), -1).view(B, N, 2, 2)
+ out = torch.cat([out, ells[:, :, :2].view(B, N, 2, 1)], dim=3)
+ return out
+
+
+def LAF2pts(LAF: torch.Tensor, n_pts: int = 50) -> torch.Tensor:
+ """
+ Converts LAFs to boundary points of the regions + center.
+ Used for local features visualization, see visualize_LAF function
+ Args:
+ LAF: (torch.Tensor).
+ n_pts: number of points to output
+
+ Returns:
+ pts: (torch.Tensor) tensor of boundary points
+
+ Shape:
+ - Input: :math:`(B, N, 2, 3)`
+ - Output: :math:`(B, N, n_pts, 2)`
+ """
+ n_dims = len(LAF.size())
+ if (n_dims != 4):
+ raise TypeError(
+ "LAF shape should be must be [Nx2x3] or [BxNx2x3]. "
+ "Got {}".format(LAF.size())
+ )
+ B, N, _, _ = LAF.size()
+ pts = torch.cat([torch.sin(torch.linspace(0, 2 * math.pi, n_pts - 1)).unsqueeze(-1),
+ torch.cos(torch.linspace(0, 2 * math.pi, n_pts - 1)).unsqueeze(-1),
+ torch.ones(n_pts - 1, 1)], dim=1)
+ # Add origin to draw also the orientation
+ pts = torch.cat([torch.tensor([0, 0, 1.]).view(1, 3),
+ pts],
+ dim=0).unsqueeze(0).expand(B * N, n_pts, 3)
+ pts = pts.to(LAF.device).to(LAF.dtype)
+ aux = torch.tensor([0, 0, 1.]).view(1, 1, 3).expand(B * N, 1, 3)
+ HLAF = torch.cat([LAF.view(-1, 2, 3), aux.to(LAF.device).to(LAF.dtype)], dim=1)
+ pts_h = torch.bmm(HLAF, pts.permute(0, 2, 1)).permute(0, 2, 1)
+ return kornia.convert_points_from_homogeneous(
+ pts_h.view(B, N, n_pts, 3))
+
+
+def visualize_LAF(img: torch.Tensor, # pragma: no cover
+ LAF: torch.Tensor,
+ img_idx: int = 0,
+ color: str = 'r'):
+ """
+ Draws affine regions (LAF)
+ """
+ pts = LAF2pts(LAF[img_idx:img_idx + 1])[0]
+ pts_np = pts.detach().permute(1, 0, 2).cpu().numpy()
+ plt.figure()
+ plt.imshow(kornia.utils.tensor_to_image(img[img_idx]))
+ plt.plot(pts_np[:, :, 0], pts_np[:, :, 1], color)
+ plt.show()
+ return
+
+
+def denormalize_LAF(LAF: torch.Tensor, images: torch.Tensor) -> torch.Tensor:
+ """
+ De-normalizes LAFs from scale to image scale
+ B,N,H,W = images.size()
+ MIN_SIZE = min(H,W)
+ [a11 a21 x]
+ [a21 a22 y]
+ becomes
+ [a11*MIN_SIZE a21*MIN_SIZE x*W]
+ [a21*MIN_SIZE a22*MIN_SIZE y*H]
+ Args:
+ LAF: (torch.Tensor).
+ images: (torch.Tensor) images, LAFs are detected in
+
+ Returns:
+ LAF: (torch.Tensor).
+
+ Shape:
+ - Input: :math:`(B, N, 2, 3)`
+ - Output: :math:`(B, N, 2, 3)`
+ """
+ n_dims = len(LAF.size())
+ if (n_dims != 4):
+ raise TypeError(
+ "LAF shape should be must be [Nx2x3] or [BxNx2x3]. "
+ "Got {}".format(LAF.size())
+ )
+ n, ch, h, w = images.size()
+ w = float(w)
+ h = float(h)
+ min_size = min(h, w)
+ coef = torch.ones(1, 1, 2, 3).to(LAF.dtype) * min_size
+ coef[0, 0, 0, 2] = w
+ coef[0, 0, 1, 2] = h
+ coef.to(LAF.device)
+ return coef.expand_as(LAF) * LAF
+
+
+def normalize_LAF(LAF: torch.Tensor, images: torch.Tensor) -> torch.Tensor:
+ """
+ Normalizes LAFs to [0,1] scale.
+ B,N,H,W = images.size()
+ MIN_SIZE = min(H,W)
+ [a11 a21 x]
+ [a21 a22 y]
+ becomes
+ [a11/MIN_SIZE a21/MIN_SIZE x/W]
+ [a21/MIN_SIZE a22/MIN_SIZE y/H]
+ Args:
+ LAF: (torch.Tensor).
+ images: (torch.Tensor) images, LAFs are detected in
+
+ Returns:
+ LAF: (torch.Tensor).
+
+ Shape:
+ - Input: :math:`(B, N, 2, 3)`
+ - Output: :math:`(B, N, 2, 3)`
+ """
+ n_dims = len(LAF.size())
+ if (n_dims != 4):
+ raise TypeError(
+ "LAF shape should be must be [BxNx2x3]. "
+ "Got {}".format(LAF.size()))
+ n, ch, h, w = images.size()
+ w = float(w)
+ h = float(h)
+ min_size = min(h, w)
+ coef = torch.ones(1, 1, 2, 3).to(LAF.dtype) / min_size
+ coef[0, 0, 0, 2] = 1.0 / w
+ coef[0, 0, 1, 2] = 1.0 / h
+ coef.to(LAF.device)
+ return coef.expand_as(LAF) * LAF
+
+
+def generate_patch_grid_from_normalized_LAF(img: torch.Tensor,
+ LAF: torch.Tensor,
+ PS: int = 32) -> torch.Tensor:
+ """
+ Helper function for affine grid generation
+ """
+ n_dims = len(LAF.size())
+ if (n_dims != 4):
+ raise TypeError(
+ "LAF shape should be must be [BxNx2x3]. "
+ "Got {}".format(LAF.size()))
+ B, N, _, _ = LAF.size()
+ num, ch, h, w = img.size()
+ # norm, then renorm is needed for allowing detection on one resulution
+ # and extraction at arbitrary other
+ LAF_renorm = denormalize_LAF(LAF, img)
+
+ grid = F.affine_grid(LAF_renorm.view(B * N, 2, 3),
+ [B * N, ch, PS, PS])
+ grid[:, :, :, 0] = 2.0 * grid[:, :, :, 0].clone() / float(w) - 1.0
+ grid[:, :, :, 1] = 2.0 * grid[:, :, :, 1].clone() / float(h) - 1.0
+ return grid
+
+
+def extract_patches_simple(img: torch.Tensor,
+ LAF: torch.Tensor,
+ PS: int = 32) -> torch.Tensor:
+ """
+ Extract patches defined by LAFs from image tensor.
+ No smoothing applied, huge aliasing (better use extract_patches_from_pyramid)
+ Args:
+ LAF: (torch.Tensor).
+ images: (torch.Tensor) images, LAFs are detected in
+ PS: (int) patch size, default = 32
+
+ Returns:
+ LAF: (torch.Tensor), :math:`(B, N, CH, PS,PS)`
+ """
+ num, ch, h, w = img.size()
+ B, N, _, _ = LAF.size()
+ out = []
+ # for loop temporarily, to be refactored
+ for i in range(B):
+ grid = generate_patch_grid_from_normalized_LAF(
+ img[i:i + 1], LAF[i:i + 1], PS)
+ out.append(F.grid_sample(
+ img[i:i + 1].expand(grid.size(0), ch, h, w),
+ grid,
+ padding_mode="border")
+ )
+ return torch.cat(out, dim=0).view(B, N, ch, PS, PS)
+
+
+def extract_patches_from_pyramid(img: torch.Tensor,
+ LAF: torch.Tensor,
+ PS: int = 32) -> torch.Tensor:
+ """
+ Extract patches defined by LAFs from image tensor.
+ Patches are extracted from appropriate pyramid level
+ Args:
+ LAF: (torch.Tensor).
+ images: (torch.Tensor) images, LAFs are detected in
+ PS: (int) patch size, default = 32
+
+ Returns:
+ LAF: (torch.Tensor), :math:`(B, N, CH, PS,PS)`
+ """
+ B, N, _, _ = LAF.size()
+ num, ch, h, w = img.size()
+ scale = 2.0 * get_laf_scale(denormalize_LAF(LAF, img)) / float(PS)
+ pyr_idx = (scale.log2() + 0.5).relu().long()
+ cur_img = img
+ cur_pyr_level = int(0)
+ out = torch.zeros(B, N, ch, PS, PS).to(LAF.dtype)
+ while min(cur_img.size(2), cur_img.size(3)) > PS:
+ num, ch, h, w = cur_img.size()
+ # for loop temporarily, to be refactored
+ for i in range(B):
+ scale_mask = (pyr_idx[i] == cur_pyr_level).squeeze()
+ if (scale_mask.float().sum()) == 0:
+ continue
+ scale_mask = scale_mask.byte().view(-1)
+ grid = generate_patch_grid_from_normalized_LAF(
+ cur_img[i:i + 1],
+ LAF[i:i + 1, scale_mask, :, :],
+ PS)
+ out[i, scale_mask, :, :, :] = out[i, scale_mask, :, :,:].clone() * 0\
+ + F.grid_sample(cur_img[i:i + 1].expand(grid.size(0),ch, h, w),
+ grid,
+ padding_mode="border")
+ cur_img = kornia.pyrdown(cur_img)
+ cur_pyr_level += 1
+ return out
diff --git a/test/feature/test_laf.py b/test/feature/test_laf.py
new file mode 100644
index 000000000..3252e972c
--- /dev/null
+++ b/test/feature/test_laf.py
@@ -0,0 +1,252 @@
+import pytest
+
+import kornia as kornia
+import kornia.testing as utils # test utils
+
+import torch
+from torch.testing import assert_allclose
+from torch.autograd import gradcheck
+import math
+
+
+class TestAngleToRotationMatrix:
+ def test_shape(self):
+ inp = torch.ones(1, 3, 4, 4)
+ rotmat = kornia.feature.angle_to_rotation_matrix(inp)
+ assert rotmat.shape == (1, 3, 4, 4, 2, 2)
+
+ def test_angles(self):
+ inp = torch.tensor([0, math.pi / 2.0])
+
+ expected = torch.tensor([[[1.0, 0.], [0., 1.0]],
+ [[0, 1.0], [-1.0, 0]]])
+ rotmat = kornia.feature.angle_to_rotation_matrix(inp)
+ assert_allclose(rotmat, expected)
+
+ def test_gradcheck(self):
+ batch_size, channels, height, width = 1, 2, 5, 4
+ img = torch.rand(batch_size, channels, height, width)
+ img = utils.tensor_to_gradcheck_var(img) # to var
+ assert gradcheck(kornia.feature.angle_to_rotation_matrix,
+ (img,),
+ raise_exception=True)
+
+
+class TestGetLAFScale:
+ def test_shape(self):
+ inp = torch.ones(1, 3, 2, 3)
+ rotmat = kornia.feature.get_laf_scale(inp)
+ assert rotmat.shape == (1, 3, 1, 1)
+
+ def test_scale(self):
+ inp = torch.tensor([[5., 1, 0], [1, 1, 0]]).float()
+ inp = inp.view(1, 1, 2, 3)
+ expected = torch.tensor([[[[2]]]]).float()
+ rotmat = kornia.feature.get_laf_scale(inp)
+ assert_allclose(rotmat, expected)
+
+ def test_gradcheck(self):
+ batch_size, channels, height, width = 1, 2, 2, 3
+ img = torch.rand(batch_size, channels, height, width)
+ img = utils.tensor_to_gradcheck_var(img) # to var
+ assert gradcheck(kornia.feature.get_laf_scale,
+ (img,),
+ raise_exception=True)
+
+
+class TestMakeUpright:
+ def test_shape(self):
+ inp = torch.ones(5, 3, 2, 3)
+ rotmat = kornia.feature.make_upright(inp)
+ assert rotmat.shape == (5, 3, 2, 3)
+
+ def test_do_nothing(self):
+ inp = torch.tensor([[1, 0, 0], [0, 1, 0]]).float()
+ inp = inp.view(1, 1, 2, 3)
+ expected = torch.tensor([[1, 0, 0], [0, 1, 0]]).float()
+ laf = kornia.feature.make_upright(inp)
+ assert_allclose(laf, expected)
+
+ def test_check_zeros(self):
+ inp = torch.rand(4, 5, 2, 3)
+ laf = kornia.feature.make_upright(inp)
+ must_be_zeros = laf[:, :, 0, 1]
+ assert_allclose(must_be_zeros, torch.zeros_like(must_be_zeros))
+
+ def test_gradcheck(self):
+ batch_size, channels, height, width = 14, 2, 2, 3
+ img = torch.rand(batch_size, channels, height, width)
+ img = utils.tensor_to_gradcheck_var(img) # to var
+ assert gradcheck(kornia.feature.make_upright,
+ (img,),
+ raise_exception=True)
+
+
+class TestELL2LAF:
+ def test_shape(self):
+ inp = torch.ones(5, 3, 5)
+ inp[:, :, 3] = 0
+ rotmat = kornia.feature.ell2LAF(inp)
+ assert rotmat.shape == (5, 3, 2, 3)
+
+ def test_conversion(self):
+ inp = torch.tensor([[10, -20, 0.01, 0, 0.01]]).float()
+ inp = inp.view(1, 1, 5)
+ expected = torch.tensor([[10, 0, 10.], [0, 10, -20]]).float()
+ expected = expected.view(1, 1, 2, 3)
+ laf = kornia.feature.ell2LAF(inp)
+ assert_allclose(laf, expected)
+
+ def test_gradcheck(self):
+ batch_size, channels, height = 1, 2, 5
+ img = torch.rand(batch_size, channels, height).abs()
+ img[:, :, 2] = img[:, :, 3].abs() + 0.3
+ img[:, :, 4] += 1.
+ # assure it is positive definite
+ img = utils.tensor_to_gradcheck_var(img) # to var
+ assert gradcheck(kornia.feature.ell2LAF,
+ (img,),
+ raise_exception=True)
+
+
+class TestNormalizeLAF:
+ def test_shape(self):
+ inp = torch.rand(5, 3, 2, 3)
+ img = torch.rand(5, 3, 10, 10)
+ assert inp.shape == kornia.feature.normalize_LAF(inp, img).shape
+
+ def test_conversion(self):
+ w, h = 10, 5
+ laf = torch.tensor([[1, 0, 1], [0, 1, 1]]).float()
+ laf = laf.view(1, 1, 2, 3)
+ img = torch.rand(1, 3, h, w)
+ expected = torch.tensor([[0.2, 0, 0.1], [0, 0.2, 0.2]]).float()
+ lafn = kornia.feature.normalize_LAF(laf, img)
+ assert_allclose(lafn, expected)
+
+ def test_gradcheck(self):
+ batch_size, channels, height, width = 1, 2, 2, 3
+
+ laf = torch.rand(batch_size, channels, height, width)
+ img = torch.rand(batch_size, 3, 10, 32)
+ img = utils.tensor_to_gradcheck_var(img) # to var
+ laf = utils.tensor_to_gradcheck_var(laf) # to var
+ assert gradcheck(kornia.feature.normalize_LAF,
+ (laf, img,),
+ raise_exception=True)
+
+
+class TestLAF2pts:
+ def test_shape(self):
+ inp = torch.rand(5, 3, 2, 3)
+ n_pts = 13
+ assert kornia.feature.LAF2pts(inp, n_pts).shape == (5, 3, n_pts, 2)
+
+ def test_conversion(self):
+ laf = torch.tensor([[1, 0, 1], [0, 1, 1]]).float()
+ laf = laf.view(1, 1, 2, 3)
+ n_pts = 6
+ expected = torch.tensor([[[[1, 1],
+ [1, 2],
+ [2, 1],
+ [1, 0],
+ [0, 1],
+ [1, 2]]]]).float()
+ pts = kornia.feature.LAF2pts(laf, n_pts)
+ assert_allclose(pts, expected)
+
+ def test_gradcheck(self):
+ batch_size, channels, height, width = 3, 2, 2, 3
+ laf = torch.rand(batch_size, channels, height, width)
+ laf = utils.tensor_to_gradcheck_var(laf) # to var
+ assert gradcheck(kornia.feature.LAF2pts,
+ (laf),
+ raise_exception=True)
+
+
+class TestDenormalizeLAF:
+ def test_shape(self):
+ inp = torch.rand(5, 3, 2, 3)
+ img = torch.rand(5, 3, 10, 10)
+ assert inp.shape == kornia.feature.denormalize_LAF(inp, img).shape
+
+ def test_conversion(self):
+ w, h = 10, 5
+ expected = torch.tensor([[1, 0, 1], [0, 1, 1]]).float()
+ expected = expected.view(1, 1, 2, 3)
+ img = torch.rand(1, 3, h, w)
+ lafn = torch.tensor([[0.2, 0, 0.1], [0, 0.2, 0.2]]).float()
+ laf = kornia.feature.denormalize_LAF(lafn.view(1, 1, 2, 3), img)
+ assert_allclose(laf, expected)
+
+ def test_gradcheck(self):
+ batch_size, channels, height, width = 1, 2, 2, 3
+
+ laf = torch.rand(batch_size, channels, height, width)
+ img = torch.rand(batch_size, 3, 10, 32)
+ img = utils.tensor_to_gradcheck_var(img) # to var
+ laf = utils.tensor_to_gradcheck_var(laf) # to var
+ assert gradcheck(kornia.feature.denormalize_LAF,
+ (laf, img,),
+ raise_exception=True)
+
+
+class TestGenPatchGrid:
+ def test_shape(self):
+ laf = torch.rand(5, 3, 2, 3)
+ img = torch.rand(5, 3, 10, 10)
+ PS = 3
+ from kornia.feature.laf import generate_patch_grid_from_normalized_LAF
+ grid = generate_patch_grid_from_normalized_LAF(img, laf, PS)
+ assert grid.shape == (15, 3, 3, 2)
+
+ def test_gradcheck(self):
+ laf = torch.rand(5, 3, 2, 3)
+ img = torch.rand(5, 3, 10, 10)
+ PS = 3
+ from kornia.feature.laf import generate_patch_grid_from_normalized_LAF
+ img = utils.tensor_to_gradcheck_var(img) # to var
+ laf = utils.tensor_to_gradcheck_var(laf) # to var
+ assert gradcheck(generate_patch_grid_from_normalized_LAF,
+ (img, laf, PS,),
+ raise_exception=True)
+
+
+class TestExtractPatchesSimple:
+ def test_shape(self):
+ laf = torch.rand(5, 4, 2, 3)
+ img = torch.rand(5, 3, 100, 30)
+ PS = 10
+ patches = kornia.feature.extract_patches_simple(img, laf, PS)
+ assert patches.shape == (5, 4, 3, PS, PS)
+
+ def test_gradcheck(self):
+ nlaf = torch.tensor([[0.1, 0.001, 0.5], [0, 0.1, 0.5]]).float()
+ nlaf = nlaf.view(1, 1, 2, 3)
+ img = torch.rand(1, 3, 100, 120)
+ PS = 11
+ img = utils.tensor_to_gradcheck_var(img) # to var
+ nlaf = utils.tensor_to_gradcheck_var(nlaf) # to var
+ assert gradcheck(kornia.feature.extract_patches_simple,
+ (img, nlaf, PS,),
+ raise_exception=True)
+
+
+class TestExtractPatchesPyr:
+ def test_shape(self):
+ laf = torch.rand(5, 4, 2, 3)
+ img = torch.rand(5, 3, 100, 30)
+ PS = 10
+ patches = kornia.feature.extract_patches_from_pyramid(img, laf, PS)
+ assert patches.shape == (5, 4, 3, PS, PS)
+
+ def test_gradcheck(self):
+ nlaf = torch.tensor([[0.1, 0.001, 0.5], [0, 0.1, 0.5]]).float()
+ nlaf = nlaf.view(1, 1, 2, 3)
+ img = torch.rand(1, 3, 100, 120)
+ PS = 11
+ img = utils.tensor_to_gradcheck_var(img) # to var
+ nlaf = utils.tensor_to_gradcheck_var(nlaf) # to var
+ assert gradcheck(kornia.feature.extract_patches_from_pyramid,
+ (img, nlaf, PS,),
+ raise_exception=True)