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 theBimages of a(B, C, H, W)tensorn_rowper row, in batch order, and returns one(C, H', W')image withpaddingzero pixels between neighbors and none around the border. It does not rescale or convert the images.- Convention:
n_rowis the number of columns, as torchvision’snrow. Zero images complete the last row, andn_row > Bstill givesn_rowcolumns.The channels are kept, and
Rrows ofn_rowimages withp = paddinggive(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:
- Returns:
The combined image grid.
- Return type:
- Raises:
TypeError – if
tensoris not atorch.Tensor.ValueError – if
tensoris not four dimensional,n_rowis not positive orpaddingis negative.
See also
combine_tensor_patches(): reassembles one image from its own sliding-window patches, averaging where they overlap.
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)returnsg(input, *args, **kwargs), which callsfoninputreshaped 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_imageon a function written for batches.- Convention:
falways 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 intoB. The result keepsf’s ownC,HandW, except that an(H, W)input getsf’s first channel only;fmust keep the batch size.A zero channel, height or width raises
ValueErrorbeforefis called. Zero leading dimensions, such as an empty batch, are passed tof, so an empty batch gives an empty output.
- Raises:
TypeError – if
inputis not a tensor.ValueError – if
inputhas fewer than two dimensions or a zero channel, height or width.
See also
perform_keep_shape_video(): the same for(B, C, D, H, W)volumes.
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)returnsg(input, *args, **kwargs), which callsfoninputreshaped to(B, C, D, H, W)with the other arguments and gives the result the input’s leading dimensions back.- Convention:
See the Convention block of
perform_keep_shape_image(), withfreceiving(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
inputis not a tensor.ValueError – if
inputhas fewer than three dimensions or a zero channel, depth, height or width.
See also
perform_keep_shape_image(): the same for(B, C, H, W)images.