π Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
fix(enhance): keep CLAHE tile histogram counts out of half precision (#6018)
Prateekshit Jaiswal9986cb43d901
diff --git a/changelog.d/6018.fixed.md b/changelog.d/6018.fixed.mdnew file mode 100644index 000000000..e8e951e45--- /dev/null+++ b/changelog.d/6018.fixed.md@@ -0,0 +1 @@+`equalize_clahe` and `RandomClahe` now count, clip and accumulate the tile histograms of float16 and bfloat16 images in float32 on the default (fast) path, and cast only the finished lookup tables back. Before, with clipping enabled, a float16 image whose tiles hold about 65520 or more pixels, such as 2048 x 2048 with the default 8 x 8 grid, came back entirely NaN. Once the per-bin cap `clip_limit * tile_pixels // 256` passed 65504, `equalize_clahe` raised an overflow error on a float16 image instead (and aborted the process on MPS); at the default `clip_limit=40.0` that is any tile above about 419,000 pixels, such as the 8 x 8 grid of a 7680 x 4320 image. Smaller half-precision tiles got lookup tables one or more gray levels off in float16 and several in bfloat16. The lookup tables now equal those of a float32 image with the same pixel values. float32 and float64 results, and the `slow_and_differentiable` path, are unchanged.diff --git a/kornia/enhance/equalization.py b/kornia/enhance/equalization.pyindex 65245d16a..012f659ef 100644--- a/kornia/enhance/equalization.py+++ b/kornia/enhance/equalization.py@@ -168,7 +168,7 @@ def _tiles_histc(tiles: torch.Tensor, bins: int) -> torch.Tensor:bins: number of bins.Returns:- Per-tile counts in the dtype of ``tiles``. (T, bins)+ Per-tile counts in float32, or float64 for float64 ``tiles``. (T, bins)"""num_tiles = tiles.shape[0]@@ -180,7 +180,7 @@ def _tiles_histc(tiles: torch.Tensor, bins: int) -> torch.Tensor:idx = torch.where(in_range, idx, bins)counts = torch.zeros(num_tiles, bins + 1, dtype=x.dtype, device=x.device)counts.scatter_add_(1, idx, torch.ones_like(x))- return counts[:, :bins].to(tiles.dtype)+ return counts[:, :bins]def _spread_residual(residual: torch.Tensor, num_bins: int) -> torch.Tensor:@@ -232,6 +232,8 @@ def _compute_luts(pixels: int = th * twtiles: torch.Tensor = tiles_x_im.view(-1, pixels) # test with view # T x (THxTW)if not diff:+ # Counted, clipped and accumulated in float32 (float64 for float64 tiles); only the finished LUT is cast back.+ # 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)@@ -270,7 +272,8 @@ def _compute_luts(luts: torch.Tensor = torch.cumsum(histos, 1) * lut_scaleluts = luts.clamp(0, num_bins - 1)if not diff:- luts = luts.round() # match OpenCV's round-to-nearest LUT quantization+ # match OpenCV's round-to-nearest LUT quantization; the integers 0..255 are exact in every float dtype+ luts = luts.round().to(tiles_x_im.dtype)return luts.view((b, gh, gw, c, num_bins))diff --git a/tests/enhance/test_equalization.py b/tests/enhance/test_equalization.pyindex 8543aa849..6ff44877d 100644--- a/tests/enhance/test_equalization.py+++ b/tests/enhance/test_equalization.py@@ -493,8 +493,7 @@ class TestEqualization(BaseTester):# the residual 32 goes, as in OpenCV, to every 256 // 32 = 8th bin from bin 0. The LUT is# round(cumsum * 255 / 1024): bin 100 maps to round(535 * 255 / 1024) = 133 and bin 200 to# round(908 * 255 / 1024) = 226, where writing the residual into bins 0..31 would give 138 and 228.- if dtype in (torch.float16, torch.bfloat16):- pytest.skip("the cumulative counts of a 1024-pixel tile are not exact below float32")+ # The counts stay in float32 for half-precision tiles too, so this is exact in every dtype.values = torch.cat([torch.zeros(342), torch.full((341,), 100 / 256), torch.full((341,), 200 / 256)])tiles = values.to(device, dtype).view(1, 1, 1, 1, 32, 32)clip = torch.tensor([40.0], device=device, dtype=dtype) if clip_as_tensor else 40.0@@ -544,19 +543,6 @@ class TestEqualization(BaseTester):],dtype=torch.float64,)- if dtype == torch.float16:- # LUT values at 143.4375 round to 144 in float16, instead of 143.- # Propagate that one-level difference through the centre-anchored weights.- expected[0, 7] += 40- expected[1, 8] += 24- expected[2, 2] += 7- expected[2, 3] += 21- expected[2, 8] += 21- expected[2, 9] += 7- expected[3, 0] += 24- expected[3, 4] += 25- expected[5, 9] += 1- expected[6, 5] += 8expected = expected.div(64 * 255).to(dtype).to(device)[None, None]# Both orientations: the two border regions are indexed by the grid size of different axes.self.assert_close(enhance.equalize_clahe(img, 40.0, (2, 3)), expected)@@ -573,6 +559,42 @@ class TestEqualization(BaseTester):out_t = enhance.equalize_clahe(img.transpose(-2, -1), 40.0, grid_size[::-1], slow_and_differentiable)self.assert_close(out_t, out.transpose(-2, -1))+ # The fast path counts, clips and accumulates each tile's histogram in float32 (float64 for float64 images) and+ # casts only the finished LUT back, so a half-precision tile gets the LUT a float32 tile with the same pixels gets.+ # Counting in float16 overflowed the clipped total of a 256 x 256 tile to inf, the tile the default 8 x 8 grid+ # gives on a 2048 x 2048 image, and rounded counts above 2048; bfloat16 rounded counts above 256.+ @pytest.mark.parametrize("clip_as_tensor", [False, True])+ @pytest.mark.parametrize(+ ("size", "grid", "tile"), [((512, 512), (2, 2), (256, 256)), ((197, 100), (1, 1), (197, 100))]+ )+ def test_clahe_luts_count_outside_half_precision(self, size, grid, tile, clip_as_tensor, device, dtype):+ torch.manual_seed(5374)+ img = torch.rand(1, 2, *size, device=device, dtype=dtype)+ tiles, _ = _compute_tiles(img, grid)+ assert tuple(tiles.shape[-2:]) == tile+ clip = torch.tensor([40.0], device=device) if clip_as_tensor else 40.0+ luts = enhance.equalization._compute_luts(tiles, clip=clip)+ count_dtype = torch.float64 if dtype == torch.float64 else torch.float32+ expected = enhance.equalization._compute_luts(tiles.to(count_dtype), clip=clip)+ assert luts.dtype == dtype+ assert torch.equal(luts.to(count_dtype), expected)++ # One 65536-pixel tile: in float16 the default clip limit turned every output pixel into NaN, and a clip limit whose+ # per-bin cap clip * 65536 // 256 exceeds 65504 raised an overflow error on CPU (and aborted the process on MPS).+ @pytest.mark.parametrize("clip_limit", [40.0, 300.0])+ @pytest.mark.parametrize("slow_and_differentiable", [False, True])+ def test_clahe_half_precision_tile_of_65536_pixels(self, slow_and_differentiable, clip_limit, device, dtype):+ torch.manual_seed(5374)+ img = torch.rand(1, 1, 256, 256, device=device, dtype=dtype)+ out = enhance.equalize_clahe(img, clip_limit, (1, 1), slow_and_differentiable)+ assert out.dtype == dtype+ assert torch.isfinite(out).all()+ expected = enhance.equalize_clahe(img.cpu().double(), clip_limit, (1, 1), slow_and_differentiable)+ # What remains in half precision is the pixel lookup and the blend, which stay in the input dtype as they do for+ # equalize: at most two gray levels for this uniform image.+ atol = 2.5 / 255 if dtype in (torch.float16, torch.bfloat16) else None+ self.assert_close(out.cpu().double(), expected, rtol=0 if atol else None, atol=atol)+@pytest.mark.parametrize(("size", "grid", "expected_tile", "expected_padded"),[