Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

Double image option for scale pyramid (#351)

Dmytro Mishkinf036e07c000f
diff --git a/kornia/geometry/transform/pyramid.py b/kornia/geometry/transform/pyramid.py
index d4b8cd81d..be838c914 100644
--- a/kornia/geometry/transform/pyramid.py
+++ b/kornia/geometry/transform/pyramid.py
@@ -119,6 +119,7 @@ class ScalePyramid(nn.Module):
n_levels (int): number of the levels in octave.
init_sigma (float): initial blur level.
min_size (int): the minimum size of the octave in pixels. Default is 5
+ double_image (bool): add 2x upscaled image as 1st level of pyramid. OpenCV SIFT does this. Default is False
Returns:
Tuple(List(Tensors), List(Tensors), List(Tensors)):
1st output: images
@@ -139,13 +140,15 @@ class ScalePyramid(nn.Module):
def __init__(self,
n_levels: int = 3,
init_sigma: float = 1.6,
- min_size: int = 5):
+ min_size: int = 5,
+ double_image: bool = False):
super(ScalePyramid, self).__init__()
self.n_levels = n_levels
self.init_sigma = init_sigma
self.min_size = min_size
self.border = min_size // 2 - 1
self.sigma_step = 2 ** (1. / float(self.n_levels))
+ self.double_image = double_image
return
def __repr__(self) -> str:
@@ -154,7 +157,8 @@ class ScalePyramid(nn.Module):
'init_sigma=' + str(self.init_sigma) + ', ' + \
'min_size=' + str(self.min_size) + ', ' + \
'border=' + str(self.border) + ', ' + \
- 'sigma_step=' + str(self.sigma_step) + ')'
+ 'sigma_step=' + str(self.sigma_step) + \
+ 'double_image=' + str(self.double_image) + ')'
def get_kernel_size(self, sigma: float):
ksize = int(2.0 * 3.0 * sigma + 1.0)
@@ -167,6 +171,9 @@ class ScalePyramid(nn.Module):
bs, ch, h, w = x.size()
pixel_distance = 1.0
cur_sigma = 0.5
+ if self.double_image:
+ x = F.interpolate(x, scale_factor=2.0, mode='bilinear', align_corners=False)
+ pixel_distance = 0.5
if self.init_sigma > cur_sigma:
sigma = math.sqrt(self.init_sigma**2 - cur_sigma**2)
cur_sigma = self.init_sigma
diff --git a/test/geometry/transform/test_pyramid.py b/test/geometry/transform/test_pyramid.py
index cc307baed..8737dd6b7 100644
--- a/test/geometry/transform/test_pyramid.py
+++ b/test/geometry/transform/test_pyramid.py
@@ -91,6 +91,12 @@ class TestScalePyramid:
sp, sigmas, pd = SP(inp)
assert sp[0].shape == (3, 1, 2, 6, 6)
+ def test_shape_batch_double(self, device):
+ inp = torch.zeros(3, 2, 6, 6).to(device)
+ SP = kornia.geometry.ScalePyramid(n_levels=1, double_image=True)
+ sp, sigmas, pd = SP(inp)
+ assert sp[0].shape == (3, 1, 2, 12, 12)
+
def test_n_levels_shape(self, device):
inp = torch.zeros(1, 1, 6, 6).to(device)
SP = kornia.geometry.ScalePyramid(n_levels=5)