Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

fix(enhance): build the CLAHE slow-path bin centres and bandwidth in the promoted tile dtype (#6383)

Prateekshit Jaiswal37afdb3c4df9
diff --git a/changelog.d/6383.fixed.md b/changelog.d/6383.fixed.md
new file mode 100644
index 000000000..d26b623b9
--- /dev/null
+++ b/changelog.d/6383.fixed.md
@@ -0,0 +1 @@
+`equalize_clahe(..., slow_and_differentiable=True)` builds its soft-histogram bin centres and bandwidth in float64 for a float64 image and in float32 for every other dtype. In 0.9.0 they were built in the default dtype: a float64 image was equalized with float32 centres, up to about 7e-5 relative away from a float64 computation, and a float64 default dtype changed the result for every input dtype, by up to 3e-2 for a bfloat16 image. The fast path is unchanged, and float32, float16 and bfloat16 results under the float32 default are bit-identical to 0.9.0.
diff --git a/kornia/enhance/equalization.py b/kornia/enhance/equalization.py
index ee6522c56..5f367dc75 100644
--- a/kornia/enhance/equalization.py
+++ b/kornia/enhance/equalization.py
@@ -239,9 +239,12 @@ def _compute_luts(
# In float16 the clipped total of a 256 x 256 tile overflows to inf, and bfloat16 rounds counts above 256.
histos = _tiles_histc(tiles, num_bins)
else:
- bins: torch.Tensor = torch.linspace(0, 1, num_bins, device=tiles.device)
+ # The centres and bandwidth follow float64 tiles; every other dtype stays float32 because histogram
+ # evaluates half-precision kernels in float32.
+ centre_dtype = torch.promote_types(tiles.dtype, torch.float32)
+ bins: torch.Tensor = torch.linspace(0, 1, num_bins, device=tiles.device, dtype=centre_dtype)
# histogram already returns (T, num_bins); squeeze() dropped the tile axis for a single tile
- histos = histogram(tiles, bins, torch.tensor(0.001))
+ histos = histogram(tiles, bins, torch.tensor(0.001, dtype=centre_dtype))
histos *= pixels
if isinstance(clip, torch.Tensor):
diff --git a/tests/enhance/test_equalization.py b/tests/enhance/test_equalization.py
index 5b56921fb..00a4d8abd 100644
--- a/tests/enhance/test_equalization.py
+++ b/tests/enhance/test_equalization.py
@@ -677,6 +677,43 @@ class TestEqualization(BaseTester):
flipped = enhance.equalize_clahe(img.flip(dim), 2.0, (3, 3), slow_and_differentiable)
self.assert_close(flipped, out.flip(dim))
+ def test_clahe_slow_path_does_not_depend_on_the_default_dtype_6366(self, device, dtype):
+ # Differentiable CLAHE takes its bin centres and 0.001 bandwidth from the tile dtype, so
+ # torch.set_default_dtype(torch.float64) must not change the output (#6366). The image is the
+ # 2x3x32x40 seed-10 case from that report, cast to the parametrized dtype.
+ if device.type == "mps":
+ pytest.skip("MPS has no float64")
+ x = torch.rand(2, 3, 32, 40, generator=torch.Generator().manual_seed(10), dtype=torch.float64).to(device, dtype)
+ out = enhance.equalize_clahe(x, slow_and_differentiable=True)
+ previous = torch.get_default_dtype()
+ torch.set_default_dtype(torch.float64)
+ try:
+ expected = enhance.equalize_clahe(x, slow_and_differentiable=True)
+ finally:
+ torch.set_default_dtype(previous)
+ self.assert_close(out, expected, rtol=0, atol=0)
+
+ def test_clahe_slow_path_centres_in_promoted_tile_dtype_6366(self, device, dtype, monkeypatch):
+ # Float64 tiles get float64 centres and bandwidth; every other dtype keeps the float32 ones it had, so its
+ # output is unchanged (#6366). The test above cannot see a dtype that ignores the default dtype but is wrong.
+ from kornia.enhance import equalization
+
+ calls = []
+ histogram = equalization.histogram
+
+ def spy(x, bins, bandwidth, *args, **kwargs):
+ calls.append((bins, bandwidth))
+ return histogram(x, bins, bandwidth, *args, **kwargs)
+
+ monkeypatch.setattr(equalization, "histogram", spy)
+ img = torch.linspace(0, 1, 256, device=device, dtype=dtype).view(1, 1, 16, 16)
+ enhance.equalize_clahe(img, slow_and_differentiable=True)
+ expected = torch.promote_types(dtype, torch.float32)
+ ((bins, bandwidth),) = calls
+ assert bins.dtype == expected
+ assert bandwidth.dtype == expected
+ self.assert_close(bins, torch.linspace(0, 1, 256, device=device, dtype=expected), rtol=0, atol=0)
+
def test_clahe_odd_tiles_gradcheck_5812(self, device):
torch.manual_seed(4)
img = torch.rand(1, 1, 9, 15, device=device, dtype=torch.float64)