π Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
fix(enhance): keep the tile axis for a single-tile CLAHE histogram (#4642)
Dipak Chaudhari971ecf494718
diff --git a/changelog.d/4642.fixed.md b/changelog.d/4642.fixed.mdnew file mode 100644index 000000000..6d9bb3eaf--- /dev/null+++ b/changelog.d/4642.fixed.md@@ -0,0 +1 @@+`equalize_clahe(..., slow_and_differentiable=True)` no longer raises `IndexError` for `grid_size=(1, 1)`, which is global histogram equalization with the clip limit applied. Grids with more than one tile are unchanged.diff --git a/kornia/enhance/equalization.py b/kornia/enhance/equalization.pyindex 474260843..cc9ee4599 100644--- a/kornia/enhance/equalization.py+++ b/kornia/enhance/equalization.py@@ -168,7 +168,8 @@ def _compute_luts(histos = torch.stack(list(map(_my_histc, tiles, [num_bins] * len(tiles))))else:bins: torch.Tensor = torch.linspace(0, 1, num_bins, device=tiles.device)- histos = histogram(tiles, bins, torch.tensor(0.001)).squeeze()+ # histogram already returns (T, num_bins); squeeze() dropped the tile axis for a single tile+ histos = histogram(tiles, bins, torch.tensor(0.001))histos *= pixelsif clip > 0.0:diff --git a/tests/enhance/test_equalization.py b/tests/enhance/test_equalization.pyindex b46c6bc20..02ad064fb 100644--- a/tests/enhance/test_equalization.py+++ b/tests/enhance/test_equalization.py@@ -102,6 +102,15 @@ class TestEqualization(BaseTester):with pytest.raises(TypeError):enhance.equalize_clahe([1, 2, 3])+ @pytest.mark.parametrize("grid_size", [(1, 1), (1, 2), (2, 1), (2, 2)])+ def test_single_tile_on_the_differentiable_path(self, grid_size, device, dtype):+ # A (1, 1) grid is global equalization with the clip limit applied, and the slow path raised an+ # IndexError on it because one tile lost its tile axis to squeeze().+ img = torch.rand(1, 1, 16, 16, device=device, dtype=dtype)+ out = enhance.equalize_clahe(img, 40.0, grid_size, slow_and_differentiable=True)+ assert out.shape == img.shape+ assert torch.isfinite(out).all()+@pytest.mark.parametrize("grid_size", [(2, 2), (2, 3), (3, 2)])def test_gradcheck(self, device, grid_size):torch.random.manual_seed(4)