π Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
batch and test fixed
Dmytro Mishkinee73f550dee0
diff --git a/kornia/feature/affine_shape.py b/kornia/feature/affine_shape.pyindex d71144d5c..1efacaa53 100644--- a/kornia/feature/affine_shape.py+++ b/kornia/feature/affine_shape.py@@ -51,7 +51,6 @@ 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]@@ -68,20 +67,10 @@ 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,@@ -90,16 +79,11 @@ 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) + ')'@@ -110,9 +94,6 @@ 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):@@ -124,17 +105,10 @@ 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/test/feature/test_affine_shape_estimator.py b/test/feature/test_affine_shape_estimator.pyindex 0a79650a3..aac46e050 100644--- a/test/feature/test_affine_shape_estimator.py+++ b/test/feature/test_affine_shape_estimator.py@@ -66,7 +66,6 @@ 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)@@ -78,16 +77,3 @@ 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 da2170fd7..6a8a952ca 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)