π Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
Support non-contiguous input for divergence losses (#728)
Aiden Nibali343b02debf74
diff --git a/kornia/losses/divergence.py b/kornia/losses/divergence.pyindex acde2c4a5..6bb4decfa 100644--- a/kornia/losses/divergence.py+++ b/kornia/losses/divergence.py@@ -9,8 +9,8 @@ def _kl_div_2d(p, q):# D_KL(P || Q)batch, chans, height, width = p.shapeunsummed_kl = F.kl_div(- q.view(batch * chans, height * width).log(),- p.view(batch * chans, height * width),+ q.reshape(batch * chans, height * width).log(),+ p.reshape(batch * chans, height * width),reduction='none',)kl_values = unsummed_kl.sum(-1).view(batch, chans)diff --git a/test/test_losses.py b/test/test_losses.pyindex a56384dff..5890ee705 100644--- a/test/test_losses.py+++ b/test/test_losses.py@@ -256,6 +256,30 @@ class TestDivergenceLoss:actual = kornia.losses.kl_div_loss_2d(input.to(device), target.to(device), reduction='none')assert_allclose(actual, expected.to(device))+ @pytest.mark.parametrize('input,target,expected', [+ (torch.full((1, 1, 2, 4), 0.125), torch.full((1, 1, 2, 4), 0.125), 0.0),+ (torch.full((1, 7, 2, 4), 0.125), torch.full((1, 7, 2, 4), 0.125), 0.0),+ (torch.full((1, 7, 2, 4), 0.125), torch.zeros((1, 7, 2, 4)), 0.0),+ (torch.zeros((1, 7, 2, 4)), torch.full((1, 7, 2, 4), 0.125), math.inf),+ ])+ def test_noncontiguous_kl(self, device, input, target, expected):+ input = input.to(device).view(input.shape[::-1]).T+ target = target.to(device).view(target.shape[::-1]).T+ actual = kornia.losses.kl_div_loss_2d(input, target).item()+ assert_allclose(actual, expected)++ @pytest.mark.parametrize('input,target,expected', [+ (torch.full((1, 1, 2, 4), 0.125), torch.full((1, 1, 2, 4), 0.125), 0.0),+ (torch.full((1, 7, 2, 4), 0.125), torch.full((1, 7, 2, 4), 0.125), 0.0),+ (torch.full((1, 7, 2, 4), 0.125), torch.zeros((1, 7, 2, 4)), 0.346574),+ (torch.zeros((1, 7, 2, 4)), torch.full((1, 7, 2, 4), 0.125), 0.346574),+ ])+ def test_noncontiguous_js(self, device, input, target, expected):+ input = input.to(device).view(input.shape[::-1]).T+ target = target.to(device).view(target.shape[::-1]).T+ actual = kornia.losses.js_div_loss_2d(input, target).item()+ assert_allclose(actual, expected)+def test_gradcheck_kl(self, device):input = torch.rand(1, 1, 10, 16).to(device)target = torch.rand(1, 1, 10, 16).to(device)