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, ) or (B, 2).p2 (
Tensor
) – the end point [x y] of the line with shape (2, ) or (B, 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:
- 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:
- 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:
- Return type:
- 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:
- Return type:
- 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:
- Return type:
- Returns:
- tensor of the form \((B, C, H, W)\) if keepdim is
False
, \((C, H, W)\) otherwise.
- tensor of the form \((B, C, H, W)\) if keepdim is
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:
- Return type:
- 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=None, dtype=None)[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 functiontorch.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 functiontorch.nn.functional.grid_sample()
. Default:True
device (
Optional
[device
], optional) – the device on which the grid will be generated. Default:None
dtype (
Optional
[dtype
], optional) – the data type of the generated grid. Default:None
- Return type:
- 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=None, dtype=None)[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 functiontorch.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 functiontorch.nn.functional.grid_sample()
. Default:True
device (
Optional
[device
], optional) – the device on which the grid will be generated. Default:None
dtype (
Optional
[dtype
], optional) – the data type of the generated grid. Default:None
- Return type:
- 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.
Memory#
- kornia.utils.one_hot(labels, num_classes, device, dtype, eps=1e-6)[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 (
device
) – the desired device of returned tensor.dtype (
dtype
) – the desired data type of returned tensor.
- Return type:
- 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, device=torch.device('cpu'), dtype=torch.int64) 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 (
Union
[str
,device
,None
]) – which device should we run on.batch_size (
int
, optional) – “micro-batch” size. Default:128
**kwargs (
Dict
[str
,Any
]) – any other arguments, which accepts model.
- Return type:
- 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.
- kornia.utils.get_mps_device_if_available()[source]#
Tries to get mps device, if fail, returns cpu.
- Return type:
- Returns:
torch.device
Automatic Mixed Precision#
- kornia.utils.is_autocast_enabled(both=True)[source]#
Check if torch autocast is enabled.
- Parameters:
both (
bool
, optional) – if True will consider autocast region for both types of devices Default:True
- Return type:
- Returns:
Return a Bool, will always return False for a torch without support, otherwise will be: if both is True torch.is_autocast_enabled() or torch.is_autocast_cpu_enabled(). If both is False will return just torch.is_autocast_enabled().