Utilities#

kornia.image.make_grid(tensor, n_row=None, padding=2)[source]#

Tile a batch of images into one image, row by row, with zero padding between.

grid = make_grid(tensor, n_row) places the B images of a (B, C, H, W) tensor n_row per row, in batch order, and returns one (C, H', W') image with padding zero pixels between neighbors and none around the border. It does not rescale or convert the images.

Convention:
  • n_row is the number of columns, as torchvision’s nrow. Zero images complete the last row, and n_row > B still gives n_row columns.

  • The channels are kept, and R rows of n_row images with p = padding give (C, R * (H + p) - p, n_row * (W + p) - p).

  • The grid is a new tensor in the input’s dtype and on its device; \(B = 0\) gives a zero-height grid.

  • Files and arrays compares it with torchvision’s make_grid.

Parameters:
  • tensor (Tensor) – A batched tensor of shape (B, C, H, W).

  • n_row (Optional[int], optional) – Number of images displayed in each row of the grid. If None, \(\lceil \sqrt{B} \rceil\). Default: None

  • padding (int, optional) – The amount of padding to add between images; must be non-negative. Default: 2

Returns:

The combined image grid.

Return type:

torch.Tensor

Raises:
  • TypeError – if tensor is not a torch.Tensor.

  • ValueError – if tensor is not four dimensional, n_row is not positive or padding is negative.

See also

Example

>>> make_grid(torch.rand(5, 3, 4, 6), n_row=2).shape
torch.Size([3, 16, 14])
kornia.image.perform_keep_shape_image(f)[source]#

Decorate a (B, C, H, W) function to accept any number of leading dimensions.

g = perform_keep_shape_image(f) returns g(input, *args, **kwargs), which calls f on input reshaped to (B, C, H, W) with the other arguments and gives the result the input’s leading dimensions back. Use it as @perform_keep_shape_image on a function written for batches.

Return type:

Callable[..., Tensor]

Convention:
  • f always receives a (B, C, H, W) tensor: an (H, W) input becomes (1, 1, H, W), a (C, H, W) input (1, C, H, W), and the leading dimensions of a longer input are flattened into B. The result keeps f’s own C, H and W, except that an (H, W) input gets f’s first channel only; f must keep the batch size.

  • A zero channel, height or width raises ValueError before f is called. Zero leading dimensions, such as an empty batch, are passed to f, so an empty batch gives an empty output.

Raises:
  • TypeError – if input is not a tensor.

  • ValueError – if input has fewer than two dimensions or a zero channel, height or width.

See also

Example

>>> @perform_keep_shape_image
... def double(x):
...     return 2 * x
>>> double(torch.ones(4, 5)).shape
torch.Size([4, 5])
kornia.image.perform_keep_shape_video(f)[source]#

Decorate a (B, C, D, H, W) function to accept any number of leading dimensions.

g = perform_keep_shape_video(f) returns g(input, *args, **kwargs), which calls f on input reshaped to (B, C, D, H, W) with the other arguments and gives the result the input’s leading dimensions back.

Return type:

Callable[..., Tensor]

Convention:

See the Convention block of perform_keep_shape_image(), with f receiving (B, C, D, H, W): a (D, H, W) input becomes (1, 1, D, H, W) and a (C, D, H, W) input (1, C, D, H, W).

Raises:
  • TypeError – if input is not a tensor.

  • ValueError – if input has fewer than three dimensions or a zero channel, depth, height or width.

See also