Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

fix(losses): validate the target dtype and label range of HausdorffERLoss3D like HausdorffERLoss (#5562)

εŒΊζ’“η00b24383c2a7
diff --git a/changelog.d/5562.breaking.md b/changelog.d/5562.breaking.md
new file mode 100644
index 000000000..d5a129518
--- /dev/null
+++ b/changelog.d/5562.breaking.md
@@ -0,0 +1,6 @@
+`HausdorffERLoss3D` now validates its target as `HausdorffERLoss` does: `target` must be `torch.long` with every label
+in `[0, C)`, otherwise it raises `ValueError` naming the accepted range (#5545). The 3-D loss used to check only that
+no label exceeded `C`. It returned a loss for float targets and for integer targets other than `torch.long`, such as
+`int32` or `uint8` (the right loss when every label was a whole number in `[0, C)`), and it silently dropped voxels
+labelled `-1`, `C` or a fractional value from every class map. These targets now raise. A label above `C` used to
+raise `ValueError("Invalid target value.")` and now raises the range message.
diff --git a/kornia/losses/hausdorff.py b/kornia/losses/hausdorff.py
index 720eb301f..10de9091a 100644
--- a/kornia/losses/hausdorff.py
+++ b/kornia/losses/hausdorff.py
@@ -113,9 +113,13 @@ class _HausdorffERLossBase(nn.Module):
f"Got {pred.shape} and {target.shape}."
)
+ if target.dtype != torch.long:
+ raise ValueError(f"Expect long type target value in range [0, {pred.size(1)}). Got {target.dtype}.")
# The range check reads the data, which graph capture cannot do; skip it under export.
- if not is_exporting() and pred.size(1) < target.max().item():
- raise ValueError("Invalid target value.")
+ if not is_exporting() and not (target.max() < pred.size(1) and target.min() >= 0):
+ raise ValueError(
+ f"Expect long type target value in range [0, {pred.size(1)}). ({target.min()}, {target.max()})"
+ )
out = torch.stack(
[
@@ -208,13 +212,6 @@ class HausdorffERLoss(_HausdorffERLossBase):
if pred.dim() != 4:
raise ValueError(f"Only 2D images supported. Got {pred.dim()}.")
- if target.dtype != torch.long:
- raise ValueError(f"Expect long type target value in range (0, {pred.size(1)}). Got {target.dtype}.")
- # The range check reads the data, which graph capture cannot do; skip it under export.
- if not is_exporting() and not (target.max() < pred.size(1) and target.min() >= 0):
- raise ValueError(
- f"Expect long type target value in range (0, {pred.size(1)}). ({target.min()}, {target.max()})"
- )
return super().forward(pred, target)
diff --git a/tests/losses/test_hd.py b/tests/losses/test_hd.py
index e11f0e661..38bc4cdf8 100644
--- a/tests/losses/test_hd.py
+++ b/tests/losses/test_hd.py
@@ -56,11 +56,17 @@ class TestHausdorffLoss(BaseTester):
kornia.losses.HausdorffERLoss3D()((torch.rand(1, 2, 1) > 0.5) * 1, (torch.rand(1, 1, 1, 2) > 0.5) * 1)
assert "Only 3D images supported. Got " in str(errinf)
+ # The 3-D loss validates its target like the 2-D one: long dtype and labels in [0, C) (#5545).
with pytest.raises(ValueError) as errinf:
kornia.losses.HausdorffERLoss3D()(
- (torch.rand(1, 2, 1, 1, 1) > 0.5) * 1, torch.tensor([[[[[5]]]]], dtype=torch.float32)
+ (torch.rand(1, 2, 1, 1, 1) > 0.5) * 1, torch.tensor([[[[[1]]]]], dtype=torch.float32)
)
- assert "Invalid target value" in str(errinf)
+ assert "Expect long type target value in range [0, 2). Got torch.float32." in str(errinf)
+
+ for label in (-1, 2, 5):
+ with pytest.raises(ValueError) as errinf:
+ kornia.losses.HausdorffERLoss3D()(torch.rand(1, 2, 1, 1, 1), torch.tensor([[[[[label]]]]]))
+ assert f"Expect long type target value in range [0, 2). ({label}, {label})" in str(errinf)
def test_numeric(self, device, dtype, cudnn_tf32_follows_option):
# `cudnn_tf32_follows_option` keeps CUDA's float32 `conv2d` out of TF32: rounding its inputs to 10 mantissa bits