π Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
implement js_div_loss_2d, kl_div_loss_2d ida tests and add missing docs
edgarribaa494c8162a53
diff --git a/docs/source/losses.rst b/docs/source/losses.rstindex 62bda0299..76e866829 100644--- a/docs/source/losses.rst+++ b/docs/source/losses.rst@@ -10,6 +10,8 @@ kornia.losses.. autofunction:: focal_loss.. autofunction:: ssim.. autofunction:: inverse_depth_smoothness_loss+.. autofunction:: js_div_loss_2d+.. autofunction:: kl_div_loss_2d.. autoclass:: DiceLoss.. autoclass:: TverskyLossdiff --git a/kornia/__init__.py b/kornia/__init__.pyindex a9ea66650..7612df918 100644--- a/kornia/__init__.py+++ b/kornia/__init__.py@@ -60,7 +60,9 @@ from kornia.losses import (tversky_loss,inverse_depth_smoothness_loss,total_variation,- psnr_loss+ psnr_loss,+ kl_div_loss_2d,+ js_div_loss_2d,)from kornia.utils import (one_hot,diff --git a/kornia/losses/divergence.py b/kornia/losses/divergence.pyindex 776a0c86a..acde2c4a5 100644--- a/kornia/losses/divergence.py+++ b/kornia/losses/divergence.py@@ -22,6 +22,8 @@ def _js_div_2d(p, q):m = 0.5 * (p + q)return 0.5 * _kl_div_2d(p, m) + 0.5 * _kl_div_2d(q, m)+# TODO: add this to the main module+def _reduce_loss(losses, reduction):if reduction == 'none':diff --git a/test/test_losses.py b/test/test_losses.pyindex dda6b4db5..57aa2b17c 100644--- a/test/test_losses.py+++ b/test/test_losses.py@@ -214,9 +214,9 @@ class TestDivergenceLoss:(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_js_div_loss_2d(self, input, target, expected):- actual = kornia.losses.js_div_loss_2d(input, target).item()- assert actual == pytest.approx(expected, abs=1e-6)+ def test_js_div_loss_2d(self, device, input, target, expected):+ actual = kornia.losses.js_div_loss_2d(input.to(device), target.to(device)).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),@@ -224,9 +224,9 @@ class TestDivergenceLoss:(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_kl_div_loss_2d(self, input, target, expected):- actual = kornia.losses.kl_div_loss_2d(input, target).item()- assert actual == pytest.approx(expected, abs=1e-6)+ def test_kl_div_loss_2d(self, device, input, target, expected):+ actual = kornia.losses.kl_div_loss_2d(input.to(device), target.to(device)).item()+ assert_allclose(actual, expected)@pytest.mark.parametrize('input,target,expected', [(torch.full((1, 1, 2, 4), 0.125),@@ -242,9 +242,29 @@ class TestDivergenceLoss:torch.full((1, 7, 2, 4), 0.125),torch.full((1, 7), math.inf)),])- def test_kl_div_loss_2d_without_reduction(self, input, target, expected):- actual = kornia.losses.kl_div_loss_2d(input, target, reduction='none')- assert_allclose(actual, expected)+ def test_kl_div_loss_2d_without_reduction(self, device, input, target, expected):+ actual = kornia.losses.kl_div_loss_2d(input.to(device), target.to(device), reduction='none')+ assert_allclose(actual, expected.to(device))++ def test_gradcheck_kl(self, device):+ input = torch.rand(1, 1, 10, 16).to(device)+ target = torch.rand(1, 1, 10, 16).to(device)++ # evaluate function gradient+ input = utils.tensor_to_gradcheck_var(input) # to var+ target = utils.tensor_to_gradcheck_var(target) # to var+ assert gradcheck(kornia.losses.kl_div_loss_2d, (input, target),+ raise_exception=True)++ def test_gradcheck_js(self, device):+ input = torch.rand(1, 1, 10, 16).to(device)+ target = torch.rand(1, 1, 10, 16).to(device)++ # evaluate function gradient+ input = utils.tensor_to_gradcheck_var(input) # to var+ target = utils.tensor_to_gradcheck_var(target) # to var+ assert gradcheck(kornia.losses.js_div_loss_2d, (input, target),+ raise_exception=True)class TestTotalVariation: