Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

fix(contrib): let diamond_square generate sides below 3 px (#4488)

Dipak Chaudharid8896610ed38
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 4a40d8b03..a8e6fa585 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -407,6 +407,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Bug fixes
+* `kornia.contrib.diamond_square` generates fractals with a spatial side below 3 px. A 1 px side raised
+ `ValueError: math domain error` and a 2 px side a `TypeError` from a float in the `torch.rand` size, so
+ `RandomPlasmaBrightness`, `RandomPlasmaContrast` and `RandomPlasmaShadow` failed on such images. A small
+ side is now drawn on the 3 px grid and sliced; outputs for sides of 3 px and more are bit-identical.
+ (#4472, #4488)
+
* `RandomBoxBlur`'s `normalized` argument is documented as what it does. It was described as "if True, L1 norm
of the kernel is set to 1", but it was forwarded positionally into `kornia.filters.box_blur`'s `separable`
parameter: it chooses between the separable and the single 2D pass, and the kernel is L1-normalized either
diff --git a/kornia/contrib/diamond_square.py b/kornia/contrib/diamond_square.py
index b95da88c3..81930fa09 100644
--- a/kornia/contrib/diamond_square.py
+++ b/kornia/contrib/diamond_square.py
@@ -206,9 +206,11 @@ def diamond_square(
for x in output_size[:-2]:
num_samples *= x
- # compute the image seed
- p2_width: float = 2 ** math.ceil(math.log2(width - 1)) + 1
- p2_height: float = 2 ** math.ceil(math.log2(height - 1)) + 1
+ # compute the image seed. The smallest grid the algorithm builds is 3 px per side (two corners
+ # and a midpoint), so a smaller side is generated at 3 px and sliced down below: a 1 or 2 px
+ # side is just its corner draws. Sizes from 3 px up are unaffected.
+ p2_width: int = 2 ** math.ceil(math.log2(max(width, 3) - 1)) + 1
+ p2_height: int = 2 ** math.ceil(math.log2(max(height, 3) - 1)) + 1
recursion_depth: int = int(min(math.log2(p2_width - 1) - 1, math.log2(p2_height - 1) - 1))
seed_width: int = (p2_width - 1) // 2**recursion_depth + 1
seed_height: int = (p2_height - 1) // 2**recursion_depth + 1
diff --git a/tests/contrib/test_diamond_square.py b/tests/contrib/test_diamond_square.py
index 3ea3e6f46..74a6f2e94 100644
--- a/tests/contrib/test_diamond_square.py
+++ b/tests/contrib/test_diamond_square.py
@@ -15,6 +15,7 @@
# limitations under the License.
#
+import pytest
import torch
import kornia
@@ -46,3 +47,18 @@ class TestDiamondSquare(BaseTester):
)
self.assert_close(out.min(), expected_min)
self.assert_close(out.max(), expected_max)
+
+ @pytest.mark.parametrize("spatial_size", [(1, 1), (2, 2), (2, 3), (3, 2), (1, 5), (2, 17)])
+ def test_spatial_sizes_below_three(self, spatial_size, device, dtype):
+ # Sides under 3 px used to fail with `math domain error` or a float in the `rand` size
+ # (kornia#4472).
+ torch.manual_seed(0)
+ output_size = (2, 3, *spatial_size)
+ out = kornia.contrib.diamond_square(output_size, device=device, dtype=dtype)
+ assert out.shape == output_size
+ assert torch.isfinite(out).all()
+
+ def test_plasma_augmentation_on_a_two_pixel_image(self, device, dtype):
+ img = torch.rand(1, 1, 2, 2, device=device, dtype=dtype)
+ out = kornia.augmentation.RandomPlasmaBrightness(p=1.0)(img)
+ assert out.shape == img.shape