kornia.utils#

Draw#

kornia.utils.draw_line(image, p1, p2, color)[source]#

Draw a single line into an image.

Parameters
  • image (Tensor) – the input image to where to draw the lines with shape :math`(C,H,W)`.

  • p1 (Tensor) – the start point [x y] of the line with shape (2).

  • p2 (Tensor) – the end point [x y] of the line with shape (2).

  • color (Tensor) – the color of the line with shape :math`(C)` where :math`C` is the number of channels of the image.

Return type

Tensor

Returns

the image with containing the line.

Examples

>>> image = torch.zeros(1, 8, 8)
>>> draw_line(image, torch.tensor([6, 4]), torch.tensor([1, 4]), torch.tensor([255]))
tensor([[[  0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.],
         [  0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.],
         [  0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.],
         [  0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.],
         [  0., 255., 255., 255., 255., 255., 255.,   0.],
         [  0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.],
         [  0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.],
         [  0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.]]])
kornia.utils.draw_rectangle(image, rectangle, color=None, fill=None)[source]#

Draw N rectangles on a batch of image tensors.

Parameters
  • image (Tensor) – is tensor of BxCxHxW.

  • rectangle (Tensor) – represents number of rectangles to draw in BxNx4 N is the number of boxes to draw per batch index[x1, y1, x2, y2] 4 is in (top_left.x, top_left.y, bot_right.x, bot_right.y).

  • color (Optional[Tensor], optional) – a size 1, size 3, BxNx1, or BxNx3 tensor. If C is 3, and color is 1 channel it will be broadcasted. Default: None

  • fill (Optional[bool], optional) – is a flag used to fill the boxes with color if True. Default: None

Return type

Tensor

Returns

This operation modifies image inplace but also returns the drawn tensor for convenience with same shape the of the input BxCxHxW.

Example

>>> img = torch.rand(2, 3, 10, 12)
>>> rect = torch.tensor([[[0, 0, 4, 4]], [[4, 4, 10, 10]]])
>>> out = draw_rectangle(img, rect)
kornia.utils.draw_convex_polygon(images, polygons, colors)[source]#

Draws convex polygons on a batch of image tensors.

Parameters
  • images (Tensor) – is tensor of BxCxHxW.

  • polygons (Union[Tensor, List[Tensor]]) – represents polygons as points, either BxNx2 or List of variable length polygons. N is the number of points. 2 is (x, y).

  • color – a B x 3 tensor or 3 tensor with color to fill in.

Return type

Tensor

Returns

This operation modifies image inplace but also returns the drawn tensor for convenience with same shape the of the input BxCxHxW.

Note

This function assumes a coordinate system (0, h - 1), (0, w - 1) in the image, with (0, 0) being the center of the top-left pixel and (w - 1, h - 1) being the center of the bottom-right coordinate.

Example

>>> img = torch.rand(1, 3, 12, 16)
>>> poly = torch.tensor([[[4, 4], [12, 4], [12, 8], [4, 8]]])
>>> color = torch.tensor([[0.5, 0.5, 0.5]])
>>> out = draw_convex_polygon(img, poly, color)

Image#

kornia.utils.tensor_to_image(tensor, keepdim=False)[source]#

Converts a PyTorch tensor image to a numpy image.

In case the tensor is in the GPU, it will be copied back to CPU.

Parameters
  • tensor (Tensor) – image of the form \((H, W)\), \((C, H, W)\) or \((B, C, H, W)\).

  • keepdim (bool, optional) – If False squeeze the input image to match the shape \((H, W, C)\) or \((H, W)\). Default: False

Return type

ndarray

Returns

image of the form \((H, W)\), \((H, W, C)\) or \((B, H, W, C)\).

Example

>>> img = torch.ones(1, 3, 3)
>>> tensor_to_image(img).shape
(3, 3)
>>> img = torch.ones(3, 4, 4)
>>> tensor_to_image(img).shape
(4, 4, 3)
kornia.utils.image_to_tensor(image, keepdim=True)[source]#

Convert a numpy image to a PyTorch 4d tensor image.

Parameters
  • image (ndarray) – image of the form \((H, W, C)\), \((H, W)\) or \((B, H, W, C)\).

  • keepdim (bool, optional) – If False unsqueeze the input image to match the shape \((B, H, W, C)\). Default: True

Return type

Tensor

Returns

tensor of the form \((B, C, H, W)\) if keepdim is False,

\((C, H, W)\) otherwise.

Example

>>> img = np.ones((3, 3))
>>> image_to_tensor(img).shape
torch.Size([1, 3, 3])
>>> img = np.ones((4, 4, 1))
>>> image_to_tensor(img).shape
torch.Size([1, 4, 4])
>>> img = np.ones((4, 4, 3))
>>> image_to_tensor(img, keepdim=False).shape
torch.Size([1, 3, 4, 4])
kornia.utils.image_list_to_tensor(images)[source]#

Converts a list of numpy images to a PyTorch 4d tensor image.

Parameters
  • images (List[ndarray]) – list of images, each of the form \((H, W, C)\).

  • consistent (Image shapes must be) –

Return type

Tensor

Returns

tensor of the form \((B, C, H, W)\).

Example

>>> imgs = [np.ones((4, 4, 1)), np.zeros((4, 4, 1))]
>>> image_list_to_tensor(imgs).shape
torch.Size([2, 1, 4, 4])

Grid#

kornia.utils.create_meshgrid(height, width, normalized_coordinates=True, device=torch.device('cpu'), dtype=torch.float32)[source]#

Generate a coordinate grid for an image.

When the flag normalized_coordinates is set to True, the grid is normalized to be in the range \([-1,1]\) to be consistent with the pytorch function torch.nn.functional.grid_sample().

Parameters
  • height (int) – the image height (rows).

  • width (int) – the image width (cols).

  • normalized_coordinates (bool, optional) – whether to normalize coordinates in the range \([-1,1]\) in order to be consistent with the PyTorch function torch.nn.functional.grid_sample(). Default: True

  • device (Optional[device], optional) – the device on which the grid will be generated. Default: torch.device('cpu')

  • dtype (dtype, optional) – the data type of the generated grid. Default: torch.float32

Return type

Tensor

Returns

grid tensor with shape \((1, H, W, 2)\).

Example

>>> create_meshgrid(2, 2)
tensor([[[[-1., -1.],
          [ 1., -1.]],

         [[-1.,  1.],
          [ 1.,  1.]]]])
>>> create_meshgrid(2, 2, normalized_coordinates=False)
tensor([[[[0., 0.],
          [1., 0.]],

         [[0., 1.],
          [1., 1.]]]])
kornia.utils.create_meshgrid3d(depth, height, width, normalized_coordinates=True, device=torch.device('cpu'), dtype=torch.float32)[source]#

Generate a coordinate grid for an image.

When the flag normalized_coordinates is set to True, the grid is normalized to be in the range \([-1,1]\) to be consistent with the pytorch function torch.nn.functional.grid_sample().

Parameters
  • depth (int) – the image depth (channels).

  • height (int) – the image height (rows).

  • width (int) – the image width (cols).

  • normalized_coordinates (bool, optional) – whether to normalize coordinates in the range \([-1,1]\) in order to be consistent with the PyTorch function torch.nn.functional.grid_sample(). Default: True

  • device (Optional[device], optional) – the device on which the grid will be generated. Default: torch.device('cpu')

  • dtype (dtype, optional) – the data type of the generated grid. Default: torch.float32

Return type

Tensor

Returns

grid tensor with shape \((1, D, H, W, 3)\).

Pointcloud#

kornia.utils.save_pointcloud_ply(filename, pointcloud)[source]#

Utility function to save to disk a pointcloud in PLY format.

Parameters
  • filename (str) – the path to save the pointcloud.

  • pointcloud (Tensor) – tensor containing the pointcloud to save. The tensor must be in the shape of \((*, 3)\) where the last component is assumed to be a 3d point coordinate \((X, Y, Z)\).

Return type

None

kornia.utils.load_pointcloud_ply(filename, header_size=8)[source]#

Utility function to load from disk a pointcloud in PLY format.

Parameters
  • filename (str) – the path to the pointcloud.

  • header_size (int, optional) – the size of the ply file header that will be skipped during loading. Default: 8

Return type

Tensor

Returns

tensor containing the loaded point with shape \((*, 3)\) where \(*\) represents the number of points.

Memory#

kornia.utils.one_hot(labels, num_classes, device=None, dtype=None, eps=1e-06)[source]#

Convert an integer label x-D tensor to a one-hot (x+1)-D tensor.

Parameters
  • labels (Tensor) – tensor with labels of shape \((N, *)\), where N is batch size. Each value is an integer representing correct classification.

  • num_classes (int) – number of classes in labels.

  • device (Optional[device], optional) – the desired device of returned tensor. Default: None

  • dtype (Optional[dtype], optional) – the desired data type of returned tensor. Default: None

Return type

Tensor

Returns

the labels in one hot tensor of shape \((N, C, *)\),

Examples

>>> labels = torch.LongTensor([[[0, 1], [2, 0]]])
>>> one_hot(labels, num_classes=3)
tensor([[[[1.0000e+00, 1.0000e-06],
          [1.0000e-06, 1.0000e+00]],

         [[1.0000e-06, 1.0000e+00],
          [1.0000e-06, 1.0000e-06]],

         [[1.0000e-06, 1.0000e-06],
          [1.0000e+00, 1.0000e-06]]]])
kornia.utils.batched_forward(model, data, device, batch_size=128, **kwargs)[source]#

Convenience function, which allows to run the forward in micro-batches.

When the just model.forward(data) does not fit into device memory, e.g. on laptop GPU. In the end, it transfers the output to the device of the input data tensor. E.g. running HardNet on 8000x1x32x32 tensor.

Parameters
  • model (Module) – Any torch model, which outputs a single tensor as an output.

  • data (Tensor) – Input data of Bx(Any) shape.

  • device (device) – which device should we run on.

  • batch_size (int, optional) – “micro-batch” size. Default: 128

  • **kwargs – any other arguments, which accepts model.

Return type

Tensor

Returns

output of the model.

Example

>>> patches = torch.rand(8000, 1, 32, 32)
>>> sift = kornia.feature.SIFTDescriptor(32)
>>> desc_batched = batched_forward(sift, patches, torch.device('cpu'), 128)
>>> desc = sift(patches)
>>> assert torch.allclose(desc, desc_batched)

Device#

kornia.utils.get_cuda_device_if_available(index=0)[source]#

Tries to get cuda device, if fail, returns cpu.

Parameters

index (int, optional) – cuda device index Default: 0

Return type

device

Returns

torch.device