π Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
test lint
Dmytro Mishkin5b44a5483d7a
diff --git a/test/filters/test_filters.py b/test/filters/test_filters.pyindex 3bc156d14..396bce4b1 100644--- a/test/filters/test_filters.py+++ b/test/filters/test_filters.py@@ -21,21 +21,19 @@ def test_get_gaussian_kernel(window_size, sigma):@pytest.mark.parametrize("ksize_y", [3, 7])@pytest.mark.parametrize("sigma", [1.5, 2.1])def test_get_gaussian_kernel2d(ksize_x, ksize_y, sigma):- kernel = kornia.get_gaussian_kernel2d(- (ksize_x, ksize_y), (sigma, sigma))+ kernel = kornia.get_gaussian_kernel2d((ksize_x, ksize_y), (sigma, sigma))assert kernel.shape == (ksize_x, ksize_y)assert kernel.sum().item() == pytest.approx(1.0)class TestGaussianBlur:- @pytest.mark.parametrize("batch_shape",- [(1, 4, 8, 15), (2, 3, 11, 7)])+ @pytest.mark.parametrize("batch_shape", [(1, 4, 8, 15), (2, 3, 11, 7)])def test_gaussian_blur(self, batch_shape, device_type):kernel_size = (5, 7)sigma = (1.5, 2.1)input = torch.rand(batch_shape).to(torch.device(device_type))- gauss = kornia.filters.GaussianBlur2d(kernel_size, sigma, 'replicate')+ gauss = kornia.filters.GaussianBlur2d(kernel_size, sigma, "replicate")assert gauss(input).shape == batch_shapedef test_gradcheck(self):@@ -47,18 +45,23 @@ class TestGaussianBlur:# evaluate function gradientinput = torch.rand(batch_shape)input = utils.tensor_to_gradcheck_var(input) # to var- assert gradcheck(kornia.gaussian_blur2d, (input, kernel_size, sigma,'replicate'),- raise_exception=True)+ assert gradcheck(+ kornia.gaussian_blur2d,+ (input, kernel_size, sigma, "replicate"),+ raise_exception=True,+ )def test_jit(self):@torch.jit.scriptdef op_script(img):- return kornia.gaussian_blur2d(img, (5, 5), (1.2, 1.2), 'replicate')+ return kornia.gaussian_blur2d(img, (5, 5), (1.2, 1.2), "replicate")batch_size, channels, height, width = 2, 3, 64, 64img = torch.ones(batch_size, channels, height, width)- expected = kornia.filters.GaussianBlur2d((5, 5), (1.2, 1.2), 'replicate')(img)+ expected = kornia.filters.GaussianBlur2d(+ (5, 5), (1.2, 1.2), "replicate"+ )(img)actual = op_script(img)assert_allclose(actual, expected)@@ -75,19 +78,22 @@ def test_get_laplacian_kernel2d(window_size):kernel = kornia.get_laplacian_kernel2d(window_size)assert kernel.shape == (window_size, window_size)assert kernel.sum().item() == pytest.approx(0.0)- expected = torch.tensor([[1., 1., 1., 1., 1., 1., 1.],- [1., 1., 1., 1., 1., 1., 1.],- [1., 1., 1., 1., 1., 1., 1.],- [1., 1., 1., -48., 1., 1., 1.],- [1., 1., 1., 1., 1., 1., 1.],- [1., 1., 1., 1., 1., 1., 1.],- [1., 1., 1., 1., 1., 1., 1.]])+ expected = torch.tensor(+ [+ [1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0],+ [1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0],+ [1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0],+ [1.0, 1.0, 1.0, -48.0, 1.0, 1.0, 1.0],+ [1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0],+ [1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0],+ [1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0],+ ]+ )assert_allclose(expected, kernel)class TestLaplacian:- @pytest.mark.parametrize("batch_shape",- [(1, 4, 8, 15), (2, 3, 11, 7)])+ @pytest.mark.parametrize("batch_shape", [(1, 4, 8, 15), (2, 3, 11, 7)])def test_laplacian(self, batch_shape, device_type):kernel_size = 5@@ -103,5 +109,6 @@ class TestLaplacian:# evaluate function gradientinput = torch.rand(batch_shape)input = utils.tensor_to_gradcheck_var(input)- assert gradcheck(kornia.laplacian, (input, kernel_size,),- raise_exception=True)+ assert gradcheck(+ kornia.laplacian, (input, kernel_size), raise_exception=True+ )diff --git a/test/test_losses.py b/test/test_losses.pyindex 45be01517..03595eb31 100644--- a/test/test_losses.py+++ b/test/test_losses.py@@ -16,14 +16,8 @@ class TestFocalLoss:labels = labels.long()assert kornia.losses.focal_loss(- logits,- labels,- alpha=0.5,- gamma=2.0,- reduction='none').shape == (- 2,- 3,- 2)+ logits, labels, alpha=0.5, gamma=2.0, reduction="none"+ ).shape == (2, 3, 2)def _test_smoke_sum(self):num_classes = 3@@ -31,12 +25,11 @@ class TestFocalLoss:labels = torch.rand(2, 3, 2) * num_classeslabels = labels.long()- assert kornia.losses.focal_loss(- logits,- labels,- alpha=0.5,- gamma=2.0,- reduction='sum').shape == ()+ assert (+ kornia.losses.focal_loss(+ logits, labels, alpha=0.5, gamma=2.0, reduction="sum"+ ).shape == ()+ )def _test_smoke_mean(self):num_classes = 3@@ -44,12 +37,11 @@ class TestFocalLoss:labels = torch.rand(2, 3, 2) * num_classeslabels = labels.long()- assert kornia.losses.focal_loss(- logits,- labels,- alpha=0.5,- gamma=2.0,- reduction='mean').shape == ()+ assert (+ kornia.losses.focal_loss(+ logits, labels, alpha=0.5, gamma=2.0, reduction="mean"+ ).shape == ()+ )# TODO: implement medef _test_jit(self):@@ -63,8 +55,11 @@ class TestFocalLoss:labels = labels.long()logits = utils.tensor_to_gradcheck_var(logits) # to var- assert gradcheck(kornia.losses.focal_loss,- (logits, labels, alpha, gamma), raise_exception=True)+ assert gradcheck(+ kornia.losses.focal_loss,+ (logits, labels, alpha, gamma),+ raise_exception=True,+ )def test_run_all(self):self._test_smoke_none()@@ -107,8 +102,11 @@ class TestTverskyLoss:labels = labels.long()logits = utils.tensor_to_gradcheck_var(logits) # to var- assert gradcheck(kornia.losses.tversky_loss,- (logits, labels, alpha, beta), raise_exception=True)+ assert gradcheck(+ kornia.losses.tversky_loss,+ (logits, labels, alpha, beta),+ raise_exception=True,+ )def test_run_all(self):self._test_smoke()@@ -149,8 +147,9 @@ class TestDiceLoss:labels = labels.long()logits = utils.tensor_to_gradcheck_var(logits) # to var- assert gradcheck(kornia.losses.dice_loss,- (logits, labels,), raise_exception=True)+ assert gradcheck(+ kornia.losses.dice_loss, (logits, labels), raise_exception=True+ )def test_run_all(self):self._test_smoke()@@ -179,11 +178,13 @@ class TestDepthSmoothnessLoss:depth = self.depth.clone()depth = utils.tensor_to_gradcheck_var(depth) # to varimage = utils.tensor_to_gradcheck_var(image) # to var- assert gradcheck(kornia.losses.inverse_depth_smoothness_loss,- (depth, image,), raise_exception=True)+ assert gradcheck(+ kornia.losses.inverse_depth_smoothness_loss,+ (depth, image),+ raise_exception=True,+ )- @pytest.mark.parametrize("batch_shape",- [(1, 1, 10, 16), (2, 4, 8, 15), ])+ @pytest.mark.parametrize("batch_shape", [(1, 1, 10, 16), (2, 4, 8, 15)])def test_run_all(self, batch_shape, device_type):self.image = torch.rand(batch_shape).to(torch.device(device_type))self.depth = torch.rand(batch_shape).to(torch.device(device_type))@@ -193,9 +194,8 @@ class TestDepthSmoothnessLoss:@pytest.mark.parametrize("window_size", [5, 11])[email protected]("reduction_type", ['none', 'mean', 'sum'])[email protected]("batch_shape",- [(1, 1, 10, 16), (2, 4, 8, 15), ])[email protected]("reduction_type", ["none", "mean", "sum"])[email protected]("batch_shape", [(1, 1, 10, 16), (2, 4, 8, 15)])def test_ssim(batch_shape, device_type, window_size, reduction_type):# input datadevice = torch.device(device_type)@@ -205,7 +205,7 @@ def test_ssim(batch_shape, device_type, window_size, reduction_type):ssim = kornia.losses.SSIM(window_size, reduction_type)ssim_loss_val = ssim(img1, img2)- if reduction_type == 'none':+ if reduction_type == "none":assert ssim_loss_val.shape == batch_shapeelse:assert ssim_loss_val.dim() == 0@@ -215,10 +215,11 @@ def test_ssim(batch_shape, device_type, window_size, reduction_type):# functionalassert utils.check_equal_torch(- ssim_loss_val, kornia.losses.ssim(- img1, img2, window_size, reduction_type))+ ssim_loss_val,+ kornia.losses.ssim(img1, img2, window_size, reduction_type),+ )# evaluate function gradientimg1 = utils.tensor_to_gradcheck_var(img1) # to varimg2 = utils.tensor_to_gradcheck_var(img2, requires_grad=False) # to var- assert gradcheck(ssim, (img1, img2,), raise_exception=True)+ assert gradcheck(ssim, (img1, img2), raise_exception=True)