Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

fix(losses): validate tensor types, devices and parameters in hausdorff loss (#6234)

Ali Tharwat3e1850653f78
diff --git a/changelog.d/6234.fixed.md b/changelog.d/6234.fixed.md
new file mode 100644
index 000000000..4a56fc94b
--- /dev/null
+++ b/changelog.d/6234.fixed.md
@@ -0,0 +1 @@
+`HausdorffERLoss` and `HausdorffERLoss3D` raise `TypeError` for a `pred` or `target` that is not a tensor (was `AttributeError`) and `DeviceError`, a `ValueError`, for tensors on different devices (was torch's `RuntimeError`). Their constructors raise `ValueError` for an `alpha` that is NaN or `+inf` (the loss was NaN) and for a negative `k` (the loss was 0), and `TypeError` for a boolean `k` (counted as 0 or 1 erosions) or a `k` that is not an integer (`TypeError` in `forward` before); an unknown `reduction` raises the same `NotImplementedError` in the constructor instead of in `forward`. A `k` given as a NumPy integer or an integer tensor, `k=0`, a negative `alpha` and `alpha=-inf` still work.
diff --git a/kornia/losses/hausdorff.py b/kornia/losses/hausdorff.py
index 0e00360a0..d24f00fd5 100644
--- a/kornia/losses/hausdorff.py
+++ b/kornia/losses/hausdorff.py
@@ -17,11 +17,15 @@
from __future__ import annotations
+import math
+import operator
from typing import Callable
import torch
from torch import nn
+from kornia.core.check import KORNIA_CHECK_SAME_DEVICE
+from kornia.core.ops import _is_numpy_bool
from kornia.core.utils import is_compiling
@@ -50,6 +54,22 @@ class _HausdorffERLossBase(nn.Module):
def __init__(self, alpha: float = 2.0, k: int = 10, reduction: str = "mean") -> None:
super().__init__()
+ # NaN and +inf weights make the loss NaN. -inf weights only the first erosion (the limit of the weights), and
+ # an alpha tensor of shape (B, 1, 1, 1) gives each sample its own exponent, so neither is rejected.
+ if not torch.as_tensor(alpha < math.inf).all():
+ raise ValueError(f"alpha must not be NaN or +inf. Got {alpha}.")
+ # operator.index reads a bool, a boolean tensor and, on NumPy 1.x, a NumPy bool as 0 or 1.
+ if isinstance(k, bool) or (isinstance(k, torch.Tensor) and k.dtype == torch.bool) or _is_numpy_bool(k):
+ raise TypeError("k must be an integer. Got bool.")
+ try:
+ # NumPy integers and integer 0-d tensors are integers too.
+ k = operator.index(k)
+ except TypeError:
+ raise TypeError(f"k must be an integer. Got {type(k).__name__}.") from None
+ if k < 0:
+ raise ValueError(f"k must be non-negative. Got {k}.")
+ if reduction not in ("mean", "sum", "none"):
+ raise NotImplementedError(f"reduction `{reduction}` has not been implemented yet.")
self.alpha = alpha
self.k = k
self.reduction = reduction
@@ -107,6 +127,8 @@ class _HausdorffERLossBase(nn.Module):
Estimated Hausdorff Loss.
"""
+ KORNIA_CHECK_SAME_DEVICE(pred, target)
+
if not (pred.shape[2:] == target.shape[2:] and pred.size(0) == target.size(0) and target.size(1) == 1):
raise ValueError(
"Prediction and target need to be of same size, and target should not be one-hot."
@@ -224,6 +246,10 @@ class HausdorffERLoss(_HausdorffERLossBase):
Estimated Hausdorff 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 pred.dim() != 4:
raise ValueError(f"Only 2D images supported. Got {pred.dim()}.")
@@ -303,6 +329,10 @@ class HausdorffERLoss3D(_HausdorffERLossBase):
Estimated Hausdorff 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 pred.dim() != 5:
raise ValueError(f"Only 3D images supported. Got {pred.dim()}.")
diff --git a/tests/losses/test_hd.py b/tests/losses/test_hd.py
index ca4ecfe36..063061d9d 100644
--- a/tests/losses/test_hd.py
+++ b/tests/losses/test_hd.py
@@ -15,6 +15,7 @@
# limitations under the License.
#
+import numpy as np
import pytest
import torch
@@ -45,6 +46,40 @@ class TestHausdorffLoss(BaseTester):
loss(logits, labels)
+ @pytest.mark.parametrize("loss_cls", [kornia.losses.HausdorffERLoss, kornia.losses.HausdorffERLoss3D])
+ def test_input_type_validation(self, loss_cls):
+ with pytest.raises(TypeError) as errinf:
+ loss_cls()(None, torch.zeros(1, 1, 1, 1, dtype=torch.long))
+ assert "pred type is not a torch.Tensor" in str(errinf)
+
+ with pytest.raises(TypeError) as errinf:
+ loss_cls()(torch.zeros(1, 2, 1, 1), [1, 2, 3])
+ assert "target type is not a torch.Tensor" in str(errinf)
+
+ def test_integer_like_k_negative_alpha_and_zero_k(self, device, dtype):
+ # The loss sums k erosions weighted by (i + 1) ** alpha: any integer type of k, k = 0 (an empty sum), alpha < 0,
+ # alpha = -inf (only the first erosion counts) and a per-sample alpha tensor are valid.
+ hd = kornia.losses.HausdorffERLoss
+ # Two offset squares: unlike near-uniform softmax maps, every erosion up to k = 4 adds to the loss.
+ labels = torch.zeros(2, 1, 16, 16, dtype=torch.long, device=device)
+ labels[..., 4:12, 4:12] = 1
+ fg = torch.zeros(2, 1, 16, 16, device=device, dtype=dtype)
+ fg[..., 7:15, 7:15] = 1
+ pred = torch.cat([1 - fg, fg], 1)
+
+ expected = hd(k=3)(pred, labels)
+ assert hd(k=4)(pred, labels) > expected > hd(k=2)(pred, labels) > 0
+ for k in (np.int64(3), np.int32(3), torch.tensor(3), torch.tensor(3, dtype=torch.int32)):
+ self.assert_close(hd(k=k)(pred, labels), expected, rtol=0, atol=0)
+ self.assert_close(hd(k=0)(pred, labels), torch.zeros_like(expected), rtol=0, atol=0)
+ assert 0 < hd(k=3, alpha=-1.0)(pred, labels) < expected
+ self.assert_close(hd(alpha=float("-inf"))(pred, labels), hd(k=1)(pred, labels), rtol=0, atol=0)
+
+ alpha = torch.tensor([2.0, -1.0], device=device, dtype=dtype).view(2, 1, 1, 1)
+ per_sample = hd(k=3, alpha=alpha, reduction="none")(pred, labels)
+ self.assert_close(per_sample[:, :1], hd(k=3, alpha=2.0, reduction="none")(pred, labels)[:, :1])
+ self.assert_close(per_sample[:, 1:], hd(k=3, alpha=-1.0, reduction="none")(pred, labels)[:, 1:])
+
@pytest.mark.parametrize("reduction", ["mean", "none", "sum"])
@pytest.mark.parametrize(
"hd,shape", [(kornia.losses.HausdorffERLoss, (4, 4)), (kornia.losses.HausdorffERLoss3D, (3, 4, 4))]
@@ -75,6 +110,40 @@ class TestHausdorffLoss(BaseTester):
hd()(pred, target)
def test_exception_2d(self):
+
+ with pytest.raises(ValueError) as errinf:
+ kornia.losses.HausdorffERLoss()(
+ torch.zeros(1, 2, 1, 1), torch.zeros(1, 1, 1, 1, dtype=torch.long, device="meta")
+ )
+ assert "Device mismatch" in str(errinf)
+
+ with pytest.raises(TypeError) as errinf:
+ kornia.losses.HausdorffERLoss(k=1.5)
+ assert "k must be an integer" in str(errinf)
+
+ with pytest.raises(TypeError) as errinf:
+ kornia.losses.HausdorffERLoss(k="10")
+ assert "k must be an integer" in str(errinf)
+
+ for k in (True, False, np.bool_(True), torch.tensor(True), torch.tensor(False)):
+ with pytest.raises(TypeError) as errinf:
+ kornia.losses.HausdorffERLoss(k=k)
+ assert "k must be an integer" in str(errinf)
+
+ for k in (-1, torch.tensor(-1)):
+ with pytest.raises(ValueError) as errinf:
+ kornia.losses.HausdorffERLoss(k=k)
+ assert "k must be non-negative" in str(errinf)
+
+ for alpha in (float("nan"), float("inf"), torch.tensor(float("nan")), torch.tensor([2.0, float("inf")])):
+ with pytest.raises(ValueError) as errinf:
+ kornia.losses.HausdorffERLoss(alpha=alpha)
+ assert "alpha must not be NaN or +inf" in str(errinf)
+
+ with pytest.raises(NotImplementedError) as errinf:
+ kornia.losses.HausdorffERLoss(reduction="invalid")
+ assert "reduction `invalid` has not been implemented yet" in str(errinf)
+
with pytest.raises(ValueError) as errinf:
kornia.losses.HausdorffERLoss()((torch.rand(1, 2, 1) > 0.5) * 1, (torch.rand(1, 1, 1, 2) > 0.5) * 1)
assert "Only 2D images supported. Got " in str(errinf)