π Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
fix(losses): validate target tensor and non-negative parameters in tversky_loss (#6229)
Ali Tharwat28b9706fb091
diff --git a/changelog.d/6229.fixed.md b/changelog.d/6229.fixed.mdnew file mode 100644index 000000000..3ee313090--- /dev/null+++ b/changelog.d/6229.fixed.md@@ -0,0 +1 @@+`tversky_loss` and `TverskyLoss` raise `TypeError` when `target` is not a tensor; it used to be an `AttributeError`, or a misleading device-mismatch `ValueError` for a NumPy array. They raise `ValueError` when `alpha`, `beta` or `eps` is a Python `int` or `float` that is negative, infinite or NaN (`TverskyLoss` at construction): a NaN weight returned a NaN loss, a negative one returned a meaningless loss (`alpha=-0.5` could give a loss below 0), and an infinite one returned 1. Tensor-valued `alpha`, `beta` and `eps`, including one weight per class, are not checked and work as before.diff --git a/kornia/losses/tversky.py b/kornia/losses/tversky.pyindex a7c77d3de..b9e7d2588 100644--- a/kornia/losses/tversky.py+++ b/kornia/losses/tversky.py@@ -17,6 +17,7 @@from __future__ import annotations+import sysfrom typing import Optionalimport torch@@ -29,6 +30,19 @@ from kornia.losses.one_hot import one_hot# based on:# https://github.com/kevinzakka/pytorch-goodies/blob/master/losses.py+# A module constant: torch.compile on torch 2.5 cannot read sys.float_info inside a traced function.+_FLOAT_MAX: float = sys.float_info.max+++def _check_parameters(alpha: float, beta: float, eps: float) -> None:+ # Only Python numbers are checked: a tensor weight (a 0-d device tensor, or one weight per class) worked before,+ # and reading its value here would cost a host sync on every call and break a fullgraph compile. Comparisons, not+ # math.isfinite: torch.compile traces a float that changes between calls symbolically, and math.isfinite on it+ # breaks the graph. NaN fails both comparisons; a bound of `< math.inf` would be dropped from the compiled guards.+ for name, value in (("alpha", alpha), ("beta", beta), ("eps", eps)):+ if isinstance(value, (int, float)) and not (0.0 <= value <= _FLOAT_MAX):+ raise ValueError(f"{name} must be non-negative and finite. Got: {value}")+def tversky_loss(pred: torch.Tensor,@@ -95,6 +109,9 @@ def tversky_loss(if not isinstance(pred, torch.Tensor):raise TypeError(f"pred type is not a torch.Tensor. Got {type(pred)}")+ if not isinstance(target, torch.Tensor):+ raise TypeError(f"target type is not a torch.Tensor. Got {type(target)}")+if not len(pred.shape) == 4:raise ValueError(f"Invalid pred shape, we expect BxNxHxW. Got: {pred.shape}")@@ -107,6 +124,8 @@ def tversky_loss(if not (pred.shape[0] == target.shape[0] and pred.shape[2:] == target.shape[1:]):raise ValueError(f"Expected target size {torch.Size((pred.shape[0], *pred.shape[2:]))}, got {target.shape}")+ _check_parameters(alpha, beta, eps)+# Keep the ratio's backward pass in float32 through softmax for half inputs.reduction_dtype = torch.float32 if pred.dtype in (torch.float16, torch.bfloat16) else pred.dtypepred_soft = F.softmax(pred, dim=1, dtype=reduction_dtype)@@ -181,6 +200,7 @@ class TverskyLoss(nn.Module):def __init__(self, alpha: float, beta: float, eps: float = 1e-8, ignore_index: Optional[int] = -100) -> None:super().__init__()+ _check_parameters(alpha, beta, eps)self.alpha: float = alphaself.beta: float = betaself.eps: float = epsdiff --git a/tests/losses/test_tversky.py b/tests/losses/test_tversky.pyindex 953c364ec..391af6edf 100644--- a/tests/losses/test_tversky.py+++ b/tests/losses/test_tversky.py@@ -214,6 +214,10 @@ class TestTverskyLoss(BaseTester):criterion("not a tensor", torch.rand(1))assert "pred type is not a torch.Tensor. Got" in str(errinfo)+ with pytest.raises(TypeError) as errinfo:+ criterion(torch.rand(1, 1, 1, 1), "not a tensor")+ assert "target type is not a torch.Tensor. Got" in str(errinfo)+with pytest.raises(ValueError) as errinfo:criterion(torch.rand(1), torch.rand(1))assert "Invalid pred shape, we expect BxNxHxW. Got:" in str(errinfo)@@ -233,10 +237,68 @@ class TestTverskyLoss(BaseTester):with pytest.raises(ValueError, match=r"Expected target size torch.Size\(\[1, 4, 6\]\)"):criterion(torch.rand(1, 3, 4, 6), torch.randint(0, 3, (2, 4, 6)))+ pred = torch.rand(2, 3, 4, 4)+ target = torch.randint(0, 3, (2, 4, 4))+ with pytest.raises(ValueError, match="alpha must be non-negative"):+ kornia.losses.tversky_loss(pred, target, alpha=-0.1, beta=0.5)++ with pytest.raises(ValueError, match="beta must be non-negative"):+ kornia.losses.tversky_loss(pred, target, alpha=0.5, beta=-0.1)++ with pytest.raises(ValueError, match="eps must be non-negative"):+ kornia.losses.tversky_loss(pred, target, alpha=0.5, beta=0.5, eps=-1e-6)++ with pytest.raises(ValueError, match="alpha must be non-negative"):+ kornia.losses.TverskyLoss(alpha=-0.1, beta=0.5)++ with pytest.raises(ValueError, match="beta must be non-negative"):+ kornia.losses.TverskyLoss(alpha=0.5, beta=-0.1)++ with pytest.raises(ValueError, match="eps must be non-negative"):+ kornia.losses.TverskyLoss(alpha=0.5, beta=0.5, eps=-1e-6)++ for invalid_val in (float("nan"), float("inf")):+ with pytest.raises(ValueError, match="alpha must be non-negative"):+ kornia.losses.tversky_loss(pred, target, alpha=invalid_val, beta=0.5)+ with pytest.raises(ValueError, match="alpha must be non-negative"):+ kornia.losses.TverskyLoss(alpha=invalid_val, beta=0.5)++ with pytest.raises(ValueError, match="beta must be non-negative"):+ kornia.losses.tversky_loss(pred, target, alpha=0.5, beta=invalid_val)+ with pytest.raises(ValueError, match="beta must be non-negative"):+ kornia.losses.TverskyLoss(alpha=0.5, beta=invalid_val)++ with pytest.raises(ValueError, match="eps must be non-negative"):+ kornia.losses.tversky_loss(pred, target, alpha=0.5, beta=0.5, eps=invalid_val)+ with pytest.raises(ValueError, match="eps must be non-negative"):+ kornia.losses.TverskyLoss(alpha=0.5, beta=0.5, eps=invalid_val)++ # Constructor boundary tests: 0.0 is accepted for alpha, beta, and eps+ assert kornia.losses.TverskyLoss(alpha=0.0, beta=0.5).alpha == 0.0+ assert kornia.losses.TverskyLoss(alpha=0.5, beta=0.0).beta == 0.0+ assert kornia.losses.TverskyLoss(alpha=0.5, beta=0.5, eps=0.0).eps == 0.0+# A target with a channel axis, (B, 1, H, W), is not (B, H, W) either.with pytest.raises(ValueError, match=r"Expected target size torch.Size\(\[1, 4, 6\]\)"):criterion(torch.rand(1, 3, 4, 6), torch.randint(0, 3, (1, 1, 4, 6)))+ def test_tensor_weights(self, device, dtype):+ pred = torch.rand(2, 3, 4, 4, device=device, dtype=dtype)+ target = torch.randint(0, 3, (2, 4, 4), device=device)+ expected = kornia.losses.tversky_loss(pred, target, alpha=0.5, beta=0.5)+ self.assert_close(+ kornia.losses.tversky_loss(pred, target, alpha=torch.tensor(0.5, device=device, dtype=dtype), beta=0.5),+ expected,+ rtol=0,+ atol=0,+ )+ self.assert_close(+ kornia.losses.tversky_loss(pred, target, alpha=torch.full((3,), 0.5, device=device, dtype=dtype), beta=0.5),+ expected,+ rtol=0,+ atol=0,+ )+@pytest.mark.parametrize("ignore_index", [-100, None])def test_all_zeros(self, device, dtype, ignore_index):num_classes = 3@@ -297,6 +359,21 @@ class TestTverskyLoss(BaseTester):expected = op(logits, labels, *params)self.assert_close(actual, expected)+ def test_dynamo_float_weights_change_between_calls(self, device, dtype, torch_optimizer):+ """The weight check keeps the graph whole when float weights change between calls and are traced as symbols."""+ from torch._dynamo.testing import CompileCounter++ logits = torch.rand(2, 3, 4, 4, device=device, dtype=dtype)+ labels = torch.randint(0, 3, (2, 4, 4), device=device)+ counter = CompileCounter()+ op_optimized = torch_optimizer(kornia.losses.tversky_loss, backend=counter, fullgraph=True, dynamic=True)+ for params in ((0.5, 0.5, 1e-8), (0.3, 0.7, 1e-6), (0.7, 0.2, 1e-5)):+ self.assert_close(+ op_optimized(logits, labels, *params), kornia.losses.tversky_loss(logits, labels, *params)+ )+ if not torch._dynamo.config.specialize_float:+ assert counter.frame_count == 1+def test_module(self, device, dtype):num_classes = 3params = (0.3, 0.7, 0.25, -100)