π Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
add nondet_tol to ssim gradcheck (#889)
Edgar Riba1d9a46e15cea
diff --git a/kornia/losses/ssim.py b/kornia/losses/ssim.pyindex e9c061d10..c4721f013 100644--- a/kornia/losses/ssim.py+++ b/kornia/losses/ssim.py@@ -77,13 +77,13 @@ def ssim(img1: torch.Tensor, img2: torch.Tensor, window_size: int,mu1: torch.Tensor = filter2D(img1, kernel)mu2: torch.Tensor = filter2D(img2, kernel)- mu1_sq = mu1.pow(2)- mu2_sq = mu2.pow(2)+ mu1_sq = mu1 ** 2+ mu2_sq = mu2 ** 2mu1_mu2 = mu1 * mu2# compute local sigma per channel- sigma1_sq = filter2D(img1 * img1, kernel) - mu1_sq- sigma2_sq = filter2D(img2 * img2, kernel) - mu2_sq+ sigma1_sq = filter2D(img1 ** 2, kernel) - mu1_sq+ sigma2_sq = filter2D(img2 ** 2, kernel) - mu2_sqsigma12 = filter2D(img1 * img2, kernel) - mu1_mu2# compute the similarity index mapdiff --git a/test/test_losses.py b/test/test_losses.pyindex 548c1fb76..20a99fb49 100644--- a/test/test_losses.py+++ b/test/test_losses.py@@ -388,13 +388,16 @@ class TestSSIMLoss:def test_gradcheck(self, device, dtype):# input datawindow_size = 3- img1 = torch.rand(1, 1, 10, 16, device=device, dtype=dtype)- img2 = torch.rand(1, 1, 10, 16, device=device, dtype=dtype)+ img1 = torch.rand(1, 1, 5, 4, device=device, dtype=dtype)+ img2 = torch.rand(1, 1, 5, 4, device=device, dtype=dtype)# evaluate function gradientimg1 = utils.tensor_to_gradcheck_var(img1) # to var- img2 = utils.tensor_to_gradcheck_var(img2, requires_grad=False) # to var- assert gradcheck(kornia.losses.ssim_loss, (img1, img2, window_size), raise_exception=True)+ img2 = utils.tensor_to_gradcheck_var(img2) # to var++ # TODO: review method since it needs `nondet_tol` in cuda sometimes.+ assert gradcheck(kornia.losses.ssim_loss,+ (img1, img2, window_size), raise_exception=True, nondet_tol=1e-8)class TestDivergenceLoss: