Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

fix(image): honor images per row in make_grid (#5745)

NIKHIL1a14d1641f13
diff --git a/changelog.d/5745.breaking.md b/changelog.d/5745.breaking.md
new file mode 100644
index 000000000..a143fe24a
--- /dev/null
+++ b/changelog.d/5745.breaking.md
@@ -0,0 +1,7 @@
+`make_grid`, `ImageModule.show` and `ImageModule.save` now read `n_row` as the number of images per row, as their
+docstrings say (the meaning of `nrow` in `torchvision.utils.make_grid`). Previously `n_row` set the number of rows, so
+every grid whose row and column counts differ was laid out the other way round: 6 images with `n_row=2` used to give
+2 rows of 3 and now give 3 rows of 2. The default `n_row=None` (the square root of the batch size, rounded up) is now
+the number of columns, so 2 images used to be stacked vertically and are now side by side, and 5 or 6 images used to
+be 3 rows of 2 and are now 2 rows of 3. Images still fill the grid row by row in batch order. To keep the old
+placement of `B` images in `R` rows, pass `n_row=math.ceil(B / R)`. (#5745)
diff --git a/kornia/image/utils.py b/kornia/image/utils.py
index eb47fe4ae..ca9d3ecae 100644
--- a/kornia/image/utils.py
+++ b/kornia/image/utils.py
@@ -257,7 +257,8 @@ def make_grid(tensor: torch.Tensor, n_row: Optional[int] = None, padding: int =
Args:
tensor: A batched tensor of shape (B, C, H, W).
- n_row: Number of images displayed in each row of the grid.
+ n_row: Number of images displayed in each row of the grid. If ``None``, use the square root
+ of the batch size rounded up. Images are placed in batch order, filling each row before the next.
padding: The amount of padding to add between images.
Returns:
@@ -270,7 +271,8 @@ def make_grid(tensor: torch.Tensor, n_row: Optional[int] = None, padding: int =
B, C, H, W = tensor.shape
if n_row is None:
n_row = int(torch.sqrt(torch.tensor(B, dtype=torch.float32)).ceil().item())
- n_col = (B + n_row - 1) // n_row
+ n_col = n_row
+ n_row = (B + n_col - 1) // n_col
padded_H = H + padding
padded_W = W + padding
diff --git a/tests/image/test_image_utils.py b/tests/image/test_image_utils.py
index 31450756b..a9712a403 100644
--- a/tests/image/test_image_utils.py
+++ b/tests/image/test_image_utils.py
@@ -23,7 +23,7 @@ import torch
import kornia
-from testing.base import assert_close
+from testing.base import BaseTester, assert_close
@pytest.mark.parametrize(
@@ -205,3 +205,36 @@ def test_perform_keep_shape_video_non_contiguous(device, dtype):
assert result.shape == expected.shape
assert_close(result, expected)
+
+
+class TestMakeGrid(BaseTester):
+ @pytest.mark.parametrize(
+ "batch_size,n_row,rows,columns",
+ [(6, 2, 3, 2), (5, 2, 3, 2), (5, None, 2, 3), (4, 2, 2, 2), (3, 1, 3, 1), (3, 3, 1, 3), (1, 1, 1, 1)],
+ )
+ @pytest.mark.parametrize("padding", [0, 2])
+ @pytest.mark.parametrize("channels", [1, 3])
+ def test_layout(self, device, dtype, batch_size, n_row, rows, columns, padding, channels):
+ height, width = 2, 3
+ tensor = torch.arange(batch_size * channels * height * width, device=device, dtype=dtype)
+ tensor = tensor.reshape(batch_size, channels, width, height).transpose(-1, -2)
+ assert not tensor.is_contiguous()
+
+ expected = tensor.new_zeros(
+ channels, rows * (height + padding) - padding, columns * (width + padding) - padding
+ )
+ for index in range(batch_size):
+ row, column = divmod(index, columns)
+ y, x = row * (height + padding), column * (width + padding)
+ expected[:, y : y + height, x : x + width] = tensor[index]
+
+ actual = kornia.image.make_grid(tensor, n_row=n_row, padding=padding)
+
+ assert actual.shape == expected.shape
+ assert actual.device == tensor.device
+ assert actual.dtype == tensor.dtype
+ self.assert_close(actual, expected)
+
+ def test_gradcheck(self, device):
+ tensor = torch.rand(3, 1, 2, 2, device=device, dtype=torch.float64)
+ self.gradcheck(kornia.image.make_grid, (tensor, 2, 1))