# kornia.utils¶

tensor_to_image(tensor: torch.Tensor) → numpy.array[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 (torch.Tensor) – image of the form $$(H, W)$$, math – (C, H, W), or math:(B, C, H, W). image of the form $$(H, W)$$, math:(H, W, C), or math:(B, H, W, C). numpy.ndarray
image_to_tensor(image: numpy.array) → torch.Tensor[source]

Converts a numpy image to a PyTorch tensor image.

Parameters: image (numpy.ndarray) – image of the form $$(H, W)$$, math – (H, W, C), or math:(B, H, W, C). tensor of the form $$(H, W)$$, math:(C, H, W), or math:(B, C, H, W). torch.Tensor
create_meshgrid(height: int, width: int, normalized_coordinates: Optional[bool] = True) → torch.Tensor[source]

Generates 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 grid_sample. http://pytorch.org/docs/master/nn.html#torch.nn.functional.grid_sample

Parameters: height (int) – the image height (rows). width (int) – the image width (cols). normalized_coordinates (Optional[bool]) – wether to normalize coordinates in the range [-1, 1] in order to be consistent with the PyTorch function grid_sample. returns a grid tensor with shape $$(1, H, W, 2)$$. torch.Tensor
save_pointcloud_ply(filename: str, pointcloud: torch.Tensor) → None[source]

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

Parameters: filename (str) – the path to save the pointcloud. pointcloud (torch.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)$$.
load_pointcloud_ply(filename: str, header_size: Optional[int] = 8) → torch.Tensor[source]

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

Parameters: filename (str) – the path to the pointcloud. header_size (Optional[int]) – the size of the ply file header that will be skipped during loading. Default is 8 lines. a tensor containing the loaded point with shape $$(*, 3)$$ where $$*$$ represents the number of points. torch.Tensor
one_hot(labels: torch.Tensor, num_classes: int, device: Optional[torch.device] = None, dtype: Optional[torch.dtype] = None, eps: Optional[float] = 1e-06) → torch.Tensor[source]

Converts an integer label 2D tensor to a one-hot 3D tensor.

Parameters: labels (torch.Tensor) – tensor with labels of shape $$(N, H, W)$$, where N is batch siz. Each value is an integer representing correct classification. num_classes (int) – number of classes in labels. device (Optional[torch.device]) – the desired device of returned tensor. Default: if None, uses the current device for the default tensor type (see torch.set_default_tensor_type()). device will be the CPU for CPU tensor types and the current CUDA device for CUDA tensor types. dtype (Optional[torch.dpython:type]) – the desired data type of returned tensor. Default: if None, infers data type from values. the labels in one hot tensor. torch.Tensor
Examples::
>>> labels = torch.LongTensor([[[0, 1], [2, 0]]])
>>> kornia.losses.one_hot(labels, num_classes=3)
tensor([[[[1., 0.],
[0., 1.]],
[[0., 1.],
[0., 0.]],
[[0., 0.],
[1., 0.]]]]