Utilities#

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

Convert a batched tensor to one image with padding in between.

Convention:
  • n_row is the number of images in each row, that is the number of columns, as torchvision’s nrow; it defaults to \(\lceil \sqrt{B} \rceil\). The images fill each row in batch order, zero images complete the last row, and n_row > B still gives n_row columns.

  • padding zeros go between the images only, with no outer border, and the channels are kept: R rows of n_row images give \((C, R (H + p) - p, n_{row} (W + p) - p)\) for \(p\) = padding.

  • 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, use the square root of the batch size rounded up. Images are placed in batch order, filling each row before the next. 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.

kornia.image.perform_keep_shape_image(f)[source]#

Apply f to an image of arbitrary leading dimensions (*, C, H, W).

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 output gets the input’s leading dimensions back around 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.

  • An empty input raises ValueError before f is called.

kornia.image.perform_keep_shape_video(f)[source]#

Apply f to an image of arbitrary leading dimensions (*, C, D, H, W).

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).