one_hot.py 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. # LICENSE HEADER MANAGED BY add-license-header
  2. #
  3. # Copyright 2018 Kornia Team
  4. #
  5. # Licensed under the Apache License, Version 2.0 (the "License");
  6. # you may not use this file except in compliance with the License.
  7. # You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. #
  17. import torch
  18. from kornia.core import Tensor
  19. def one_hot(labels: Tensor, num_classes: int, device: torch.device, dtype: torch.dtype, eps: float = 1e-6) -> Tensor:
  20. r"""Convert an integer label x-D tensor to a one-hot (x+1)-D tensor.
  21. Args:
  22. labels: tensor with labels of shape :math:`(N, *)`, where N is batch size.
  23. Each value is an integer representing correct classification.
  24. num_classes: number of classes in labels.
  25. device: the desired device of returned tensor.
  26. dtype: the desired data type of returned tensor.
  27. eps: epsilon for numerical stability.
  28. Returns:
  29. the labels in one hot tensor of shape :math:`(N, C, *)`,
  30. Examples:
  31. >>> labels = torch.LongTensor([[[0, 1], [2, 0]]])
  32. >>> one_hot(labels, num_classes=3, device=torch.device('cpu'), dtype=torch.float32)
  33. tensor([[[[1.0000e+00, 1.0000e-06],
  34. [1.0000e-06, 1.0000e+00]],
  35. <BLANKLINE>
  36. [[1.0000e-06, 1.0000e+00],
  37. [1.0000e-06, 1.0000e-06]],
  38. <BLANKLINE>
  39. [[1.0000e-06, 1.0000e-06],
  40. [1.0000e+00, 1.0000e-06]]]])
  41. """
  42. if not isinstance(labels, Tensor):
  43. raise TypeError(f"Input labels type is not a Tensor. Got {type(labels)}")
  44. if not labels.dtype == torch.int64:
  45. raise ValueError(f"labels must be of the same dtype torch.int64. Got: {labels.dtype}")
  46. if num_classes < 1:
  47. raise ValueError(f"The number of classes must be bigger than one. Got: {num_classes}")
  48. shape = labels.shape
  49. one_hot = torch.full((shape[0], num_classes) + shape[1:], eps, device=device, dtype=dtype)
  50. return one_hot.scatter_(1, labels.unsqueeze(1), 1.0)