π Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
resolve
Dmytro Mishkinb68ff0cf9592
diff --git a/kornia/feature/affine_shape.py b/kornia/feature/affine_shape.pyindex 1efacaa53..d71144d5c 100644--- a/kornia/feature/affine_shape.py+++ b/kornia/feature/affine_shape.py@@ -51,6 +51,7 @@ class PatchAffineShapeEstimator(nn.Module):"input shape should be must be [Bx1x{}x{}]. ""Got {}".format(self.patch_size, self.patch_size, patch.size()))self.weighting = self.weighting.to(patch.dtype).to(patch.device)+<<<<<<< HEADgrads: torch.Tensor = self.gradient(patch) * self.weighting# unpack the edgesgx: torch.Tensor = grads[:, :, 0]@@ -67,10 +68,20 @@ class PatchAffineShapeEstimator(nn.Module):ellipse_shape = ellipse_shape * (1.0 - bad_mask) + circular_shape * bad_mask # type: ignore# normalizationellipse_shape = ellipse_shape / ellipse_shape.max(dim=2, keepdim=True)[0]+=======+ grads: torch.Tensor = self.gradient(patch)+ # unpack the edges+ gx: torch.Tensor = grads[:, :, 0]+ gy: torch.Tensor = grads[:, :, 1]+ ellipse_shape = torch.cat([gx.pow(2).mean(dim=2).mean(dim=2, keepdim=True),+ (gx * gy).mean(dim=2).mean(dim=2, keepdim=True),+ gy.pow(2).mean(dim=2).mean(dim=2, keepdim=True)], dim=2)+>>>>>>> resolvereturn ellipse_shapeclass LAFAffineShapeEstimator(nn.Module):+<<<<<<< HEAD"""Module, which extracts patches using input images and local affine frames (LAFs),then runs PatchAffineShapeEstimator on patches to estimate LAFs shape.Then original LAF shape is replaced with estimated one. The original LAF orientation is not preserved,@@ -79,11 +90,16 @@ class LAFAffineShapeEstimator(nn.Module):patch_size: int, default = 32"""def __init__(self,patch_size: int = 32) -> None:+=======+ def __init__(self,+ patch_size: int = 32):+>>>>>>> resolvesuper(LAFAffineShapeEstimator, self).__init__()self.patch_size = patch_sizeself.affine_shape_detector = PatchAffineShapeEstimator(self.patch_size)return+<<<<<<< HEADdef __repr__(self):return self.__class__.__name__ + '('\'patch_size=' + str(self.patch_size) + ')'@@ -94,6 +110,9 @@ class LAFAffineShapeEstimator(nn.Module):img: 4d tensor, shape [Bx1xHxW]Returns:laf_out: 4d tensor, shape [BxNx2x3] """+=======+ def forward(self, laf: torch.Tensor, img: torch.Tensor) -> torch.Tensor: # type: ignore+>>>>>>> resolveraise_error_if_laf_is_not_valid(laf)img_message: str = "Invalid img shape, we expect BxCxHxW. Got: {}".format(img.shape)if not torch.is_tensor(img):@@ -105,10 +124,17 @@ class LAFAffineShapeEstimator(nn.Module):raise ValueError("Batch size of laf and img should be the same. Got {}, {}".format(img.size(0), laf.size(0)))B, N = laf.shape[:2]+<<<<<<< HEADPS: int = self.patch_sizepatches: torch.Tensor = extract_patches_from_pyramid(img,make_upright(laf),PS, True).view(-1, 1, PS, PS)+=======+ patches: torch.Tensor = extract_patches_from_pyramid(img, laf, PS=self.patch_size).view(-1,+ 1,+ self.patch_size,+ self.patch_size)+>>>>>>> resolveellipse_shape: torch.Tensor = self.affine_shape_detector(patches)ellipses = torch.cat([laf.view(-1, 2, 3)[..., 2].unsqueeze(1), ellipse_shape], dim=2).view(B, N, 5)scale_orig = get_laf_scale(laf)diff --git a/kornia/feature/scale_space_detector.py b/kornia/feature/scale_space_detector.pyindex a73cc2104..b25519478 100644--- a/kornia/feature/scale_space_detector.py+++ b/kornia/feature/scale_space_detector.py@@ -65,15 +65,11 @@ class ScaleSpaceDetector(nn.Module):mr_size: float, default 6.0. Multiplier for local feature scale compared to the detection scale. 6.0 ismatching OpenCV 12.0 convention for SIFT.- scale_pyr_module: nn.Module, which generates scale pyramid. See :class:`~kornia.geometry.ScalePyramid`- for details. Default is ScalePyramid(3, 1.6, 10)+ scale_pyr_module: nn.Module, which generates scale pyramid. See :class:`~kornia.geometry.ScalePyramid` for details. Default is ScalePyramid(3, 1.6, 10)resp_module: nn.Module, which calculates 'cornerness' of the pixel. Default is BlobHessian(),- nms_module: nn.Module, which outputs per-patch coordinates of the responce maxima.- See :class:`~kornia.geometry.ConvSoftArgmax3d` for details.- ori_module: nn.Module for local feature orientation estimation.- See :class:`~kornia.feature.LAFOrienter` for details. Default is PassLAF, which does nothing- aff_module: nn.Module for local feature affine shape estimation.- See :class:`~kornia.feature.LAFAffineShapeEstimator` for details. Default is PassLAF, which does nothing+ nms_module: nn.Module, which outputs per-patch coordinates of the responce maxima. See :class:`~kornia.geometry.ConvSoftArgmax3d` for details.+ ori_module: nn.Module for local feature orientation estimation. See :class:`~kornia.feature.LAFOrienter` for details. Default is PassLAF, which does nothing+ aff_module: nn.Module for local feature affine shape estimation. See :class:`~kornia.feature.LAFAffineShapeEstimator` for details. Default is PassLAF, which does nothing"""def __init__(self,num_features: int = 500,diff --git a/test/feature/test_affine_shape_estimator.py b/test/feature/test_affine_shape_estimator.pyindex aac46e050..0a79650a3 100644--- a/test/feature/test_affine_shape_estimator.py+++ b/test/feature/test_affine_shape_estimator.py@@ -66,6 +66,7 @@ class TestLAFAffineShapeEstimator:inp[:, :, 15:-15, 9:-9] = 1laf = torch.tensor([[[[20., 0., 16.], [0., 20., 16.]]]])new_laf = aff(laf, inp)+<<<<<<< HEADexpected = torch.tensor([[[[36.643, 0., 16.], [0., 10.916, 16.]]]])assert_allclose(new_laf, expected, atol=1e-4, rtol=1e-4)@@ -77,3 +78,16 @@ class TestLAFAffineShapeEstimator:laf = utils.tensor_to_gradcheck_var(laf) # to varassert gradcheck(LAFAffineShapeEstimator(11), (laf, patches),raise_exception=True, rtol=1e-3, atol=1e-3)+=======+ expected = torch.tensor([[[[36.246, 0., 16.], [0., 11.036, 16.]]]])+ assert_allclose(new_laf, expected, atol=1e-4, rtol=1e-4)++ def test_gradcheck(self):+ batch_size, channels, height, width = 1, 1, 21, 21+ patches = torch.rand(batch_size, channels, height, width).float()+ patches = utils.tensor_to_gradcheck_var(patches) # to var+ laf = torch.ones(batch_size, 4, 2, 3).float()+ laf = utils.tensor_to_gradcheck_var(laf) # to var+ assert gradcheck(LAFAffineShapeEstimator(8), (laf, patches),+ raise_exception=True, rtol=1e-4, atol=1e-4)+>>>>>>> resolvediff --git a/test/feature/test_laf.py b/test/feature/test_laf.pyindex 6a8a952ca..da2170fd7 100644--- a/test/feature/test_laf.py+++ b/test/feature/test_laf.py@@ -279,7 +279,7 @@ class TestExtractPatchesPyr:assert patches.shape == (5, 4, 3, PS, PS)# TODO: check what to do to improve timing- @pytest.mark.skip("The test takes too long to finish.")+ # @pytest.mark.skip("The test takes too long to finish.")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)