π Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
test(losses): seed the HausdorffERLoss numeric test and pin the value at that seed (#5581)
εΊζ’η00d230c53ae0
diff --git a/tests/losses/test_hd.py b/tests/losses/test_hd.pyindex 5cb66518b..e11f0e661 100644--- a/tests/losses/test_hd.py+++ b/tests/losses/test_hd.py@@ -62,20 +62,47 @@ class TestHausdorffLoss(BaseTester):)assert "Invalid target value" in str(errinf)- def test_numeric(self, device, dtype):- if dtype == torch.float64:- pytest.xfail("Sometimes failing on float64")+ 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+ # moves the value by 3e-5 to 8e-5, above the float32 tolerance.num_classes = 3shape = (50, 50)hd = kornia.losses.HausdorffERLoss- logits = torch.rand(2, num_classes, *shape, dtype=dtype, device=device)- labels = (torch.rand(2, 1, *shape, dtype=dtype, device=device) * (num_classes - 1)).long()+ generator = torch.Generator().manual_seed(0)+ logits = torch.rand(2, num_classes, *shape, generator=generator).to(device, dtype)+ labels = (torch.rand(2, 1, *shape, generator=generator) * (num_classes - 1)).long().to(device)loss = hd(k=10)- expected = torch.tensor(0.025, device=device, dtype=dtype)+ # The value at this seed. Unseeded draws spread from 0.0205 to 0.0316 around the old constant 0.025 (#5577).+ expected = torch.tensor(0.0265066, device=device, dtype=dtype)actual = loss(logits, labels)- self.assert_close(actual, expected, rtol=0.005, atol=0.005)+ self.assert_close(actual, expected)++ def test_numeric_every_erosion_counts(self, device, dtype, cudnn_tf32_follows_option):+ # Random inputs as in test_numeric erode to zero after two steps, so that value is the same for any k >= 2.+ # These mismatches are 10 to 20 pixels wide and stay nonzero through all ten erosions: k = 9 gives 11.567 and+ # k = 11 gives 13.223.+ if dtype not in (torch.float32, torch.float64):+ pytest.skip("the weighted sum of ten erosions is 0.3 % (float16) to 1.2 % (bfloat16) off in half precision")+ target = torch.zeros(2, 1, 32, 32, dtype=torch.long, device=device)+ target[0, :, 6:26, 6:26] = 1 # a 20 x 20 object+ fg = torch.zeros(2, 32, 32, device=device, dtype=dtype)+ fg[0, 6:26, 6:16] = 0.9 # predicted over the left half of the object only+ fg[1, 4:20, 10:26] = 0.8 # a 16 x 16 false positive+ pred = torch.stack([1 - fg, fg], 1)++ # The reference algorithm (PatRyg99/HausdorffLoss, HausdorffERLoss.perform_erosion) in float64 with SciPy, for+ # each class c and image b, with cross = [[0, 1, 0], [1, 1, 1], [0, 1, 0]]; the loss is the mean of `eroded`+ # over classes, images and pixels:+ # bound, eroded = (pred[b, c] - (target[b, 0] == c)) ** 2, 0+ # for k in range(10):+ # e = np.maximum(scipy.ndimage.convolve(bound, 0.2 * cross, mode="constant") - 0.5, 0)+ # e = (e - e.min()) / np.ptp(e) if np.ptp(e) else e+ # eroded += e * (k + 1) ** 2+ # bound = e+ expected = torch.tensor(12.540735900430166, device=device, dtype=dtype)+ self.assert_close(kornia.losses.HausdorffERLoss(k=10)(pred, target), expected)def test_numeric_3d(self, device, dtype):num_classes = 3