π Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
fix(enhance): preserve single-bin axis in image_histogram2d (#5366)
Mateeh Ullah667f49c64c2d
diff --git a/changelog.d/5366.breaking.md b/changelog.d/5366.breaking.mdnew file mode 100644index 000000000..3442130a2--- /dev/null+++ b/changelog.d/5366.breaking.md@@ -0,0 +1,4 @@+`image_histogram2d` now keeps the bin axis for rank-2 `(H, W)` input with a single bin (`n_bins=1` or one+explicit center): the histogram and the PDF have shape `(1,)`, the documented `(bins,)`. They used to be 0-d+tensors, because the synthetic batch and channel axes were removed with an unrestricted `squeeze()` that also+dropped the length-one bin axis. Rank-3 and rank-4 input and rank-2 input with more than one bin are unchanged.diff --git a/kornia/enhance/histogram.py b/kornia/enhance/histogram.pyindex ef1b3bda7..7dc987c77 100644--- a/kornia/enhance/histogram.py+++ b/kornia/enhance/histogram.py@@ -241,10 +241,6 @@ def image_histogram2d(centers lie at min + (i + 0.5) * bandwidth; values outside the supplied range contributeaccording to the selected kernel rather than being clipped into an endpoint bin.- .. warning::- Rank-2 input with a single bin or explicit center returns 0-d histogram and PDF tensors- instead of length-one vectors (`#5363 <https://github.com/kornia/kornia/issues/5363>`_).-The calculation uses triangular kernel density estimation.Args:@@ -271,7 +267,7 @@ def image_histogram2d(:math:`(B, C, bins)`.Computed probability densities of shape :math:`(bins)`, :math:`(C, bins)`,:math:`(B, C, bins)`, if return_pdf is ``True``. torch.Tensor of torch.zeros with shape- of the histogram otherwise. For rank-2 input with one bin, both returned tensors are 0-d.+ of the histogram otherwise."""if image is not None and not isinstance(image, torch.Tensor):@@ -335,15 +331,15 @@ def image_histogram2d(normalization = torch.sum(hist, dim=-1, keepdim=True) + epspdf = hist / normalizationif image.dim() == 2:- hist = hist.squeeze()- pdf = pdf.squeeze()+ hist = hist[0, 0]+ pdf = pdf[0, 0]elif image.dim() == 3:hist = hist.squeeze(0)pdf = pdf.squeeze(0)return hist, pdfif image.dim() == 2:- hist = hist.squeeze()+ hist = hist[0, 0]elif image.dim() == 3:hist = hist.squeeze(0)diff --git a/tests/enhance/test_conventions_enhance.py b/tests/enhance/test_conventions_enhance.pyindex 11b3d75d5..42be0d931 100644--- a/tests/enhance/test_conventions_enhance.py+++ b/tests/enhance/test_conventions_enhance.py@@ -87,8 +87,7 @@ class TestEnhanceConventions(BaseTester):@pytest.mark.parametrize("explicit_center", [False, True])@pytest.mark.parametrize("return_pdf", [False, True])- @pytest.mark.xfail(strict=True, reason="#5363: rank-2 image histogram squeezes away a single bin axis")- def test_wart_image_histogram_rank2_single_bin_preserves_bin_axis_5363(+ def test_convention_image_histogram_rank2_single_bin_preserves_bin_axis_5363(self, device, dtype, explicit_center, return_pdf):image = torch.tensor([[0.25, 0.75]], device=device, dtype=dtype)diff --git a/tests/enhance/test_histogram.py b/tests/enhance/test_histogram.pyindex 252b9ffc2..6279bd5ec 100644--- a/tests/enhance/test_histogram.py+++ b/tests/enhance/test_histogram.py@@ -35,6 +35,20 @@ class TestImageHistogram2d(BaseTester):assert hist.shape == (32,)assert pdf.shape == (32,)+ @pytest.mark.parametrize("return_pdf", [False, True])+ def test_rank2_single_bin_preserves_bin_axis(self, device, dtype, return_pdf):+ sample = torch.tensor([[0.25, 0.75]], device=device, dtype=dtype)+ hist, pdf = TestImageHistogram2d.fcn(sample, 0.0, 1.0, 1, return_pdf=return_pdf)++ assert hist.shape == (1,)+ assert pdf.shape == (1,)++ hist_explicit, pdf_explicit = TestImageHistogram2d.fcn(+ sample, 0.0, 1.0, centers=torch.tensor([0.5], device=device, dtype=dtype), return_pdf=return_pdf+ )+ assert hist_explicit.shape == (1,)+ assert pdf_explicit.shape == (1,)+@pytest.mark.parametrize("kernel", ["triangular", "gaussian", "uniform", "epanechnikov"])def test_shape_channels(self, device, dtype, kernel):sample = torch.ones(3, 16, 16, device=device, dtype=dtype)