You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

c_transforms.py 7.9 kB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. # Copyright 2019 Huawei Technologies Co., Ltd
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. # ==============================================================================
  15. """
  16. This module c_transforms provides common operations, including OneHotOp and TypeCast.
  17. """
  18. from enum import IntEnum
  19. import numpy as np
  20. import mindspore.common.dtype as mstype
  21. import mindspore._c_dataengine as cde
  22. from .validators import check_num_classes, check_de_type, check_fill_value, check_slice_op, check_mask_op, \
  23. check_pad_end, check_concat_type
  24. from ..core.datatypes import mstype_to_detype
  25. class OneHot(cde.OneHotOp):
  26. """
  27. Tensor operation to apply one hot encoding.
  28. Args:
  29. num_classes (int): Number of classes of the label.
  30. """
  31. @check_num_classes
  32. def __init__(self, num_classes):
  33. self.num_classes = num_classes
  34. super().__init__(num_classes)
  35. class Fill(cde.FillOp):
  36. """
  37. Tensor operation to create a tensor filled with passed scalar value.
  38. The output tensor will have the same shape and type as the input tensor.
  39. Args:
  40. fill_value (python types (str, bytes, int, float, or bool)) : scalar value
  41. to fill created tensor with.
  42. """
  43. @check_fill_value
  44. def __init__(self, fill_value):
  45. super().__init__(cde.Tensor(np.array(fill_value)))
  46. class TypeCast(cde.TypeCastOp):
  47. """
  48. Tensor operation to cast to a given MindSpore data type.
  49. Args:
  50. data_type (mindspore.dtype): mindspore.dtype to be casted to.
  51. """
  52. @check_de_type
  53. def __init__(self, data_type):
  54. data_type = mstype_to_detype(data_type)
  55. self.data_type = str(data_type)
  56. super().__init__(data_type)
  57. class Slice(cde.SliceOp):
  58. """
  59. Slice operation to extract a tensor out using the given n slices.
  60. The functionality of Slice is similar to NumPy indexing feature.
  61. (Currently only rank-1 tensors are supported).
  62. Args:
  63. *slices(Variable length argument list, supported types are, int, list(int), slice, None or Ellipses):
  64. Maximum `n` number of arguments to slice a tensor of rank `n`.
  65. One object in slices can be one of:
  66. 1. :py:obj:`int`: Slice this index only. Negative index is supported.
  67. 2. :py:obj:`list(int)`: Slice these indices ion the list only. Negative indices are supdeported.
  68. 3. :py:obj:`slice`: Slice the generated indices from the slice object. Similar to `start:stop:step`.
  69. 4. :py:obj:`None`: Slice the whole dimension. Similar to `:` in python indexing.
  70. 5. :py:obj:`Ellipses`: Slice all dimensions between the two slices. Similar to `...` in python indexing.
  71. Examples:
  72. >>> # Data before
  73. >>> # | col |
  74. >>> # +---------+
  75. >>> # | [1,2,3] |
  76. >>> # +---------|
  77. >>> data = data.map(operations=Slice(slice(1,3))) # slice indices 1 and 2 only
  78. >>> # Data after
  79. >>> # | col |
  80. >>> # +---------+
  81. >>> # | [2,3] |
  82. >>> # +---------|
  83. """
  84. @check_slice_op
  85. def __init__(self, *slices):
  86. dim0 = slices[0]
  87. if isinstance(dim0, int):
  88. dim0 = [dim0]
  89. elif dim0 is None:
  90. dim0 = True
  91. elif isinstance(dim0, slice):
  92. dim0 = (dim0.start, dim0.stop, dim0.step)
  93. elif dim0 is Ellipsis:
  94. dim0 = True
  95. super().__init__(dim0)
  96. class Relational(IntEnum):
  97. EQ = 0
  98. NE = 1
  99. GT = 2
  100. GE = 3
  101. LT = 4
  102. LE = 5
  103. DE_C_RELATIONAL = {Relational.EQ: cde.RelationalOp.EQ,
  104. Relational.NE: cde.RelationalOp.NE,
  105. Relational.GT: cde.RelationalOp.GT,
  106. Relational.GE: cde.RelationalOp.GE,
  107. Relational.LT: cde.RelationalOp.LT,
  108. Relational.LE: cde.RelationalOp.LE}
  109. class Mask(cde.MaskOp):
  110. """
  111. Mask content of the input tensor with the given predicate.
  112. Any element of the tensor that matches the predicate will be evaluated to True, otherwise False.
  113. Args:
  114. operator (Relational): One of the relational operator EQ, NE LT, GT, LE or GE
  115. constant (python types (str, int, float, or bool): constant to be compared to.
  116. Constant will be casted to the type of the input tensor
  117. dtype (optional, mindspore.dtype): type of the generated mask. Default to bool
  118. Examples:
  119. >>> # Data before
  120. >>> # | col1 |
  121. >>> # +---------+
  122. >>> # | [1,2,3] |
  123. >>> # +---------+
  124. >>> data = data.map(operations=Mask(Relational.EQ, 2))
  125. >>> # Data after
  126. >>> # | col1 |
  127. >>> # +--------------------+
  128. >>> # | [False,True,False] |
  129. >>> # +--------------------+
  130. """
  131. @check_mask_op
  132. def __init__(self, operator, constant, dtype=mstype.bool_):
  133. dtype = mstype_to_detype(dtype)
  134. constant = cde.Tensor(np.array(constant))
  135. super().__init__(DE_C_RELATIONAL[operator], constant, dtype)
  136. class PadEnd(cde.PadEndOp):
  137. """
  138. Pad input tensor according to `pad_shape`, need to have same rank.
  139. Args:
  140. pad_shape (list(int)): list on integers representing the shape needed. Dimensions that set to `None` will
  141. not be padded (i.e., original dim will be used). Shorter dimensions will truncate the values.
  142. pad_value (python types (str, bytes, int, float, or bool), optional): value used to pad. Default to 0 or empty
  143. string in case of Tensors of strings.
  144. Examples:
  145. >>> # Data before
  146. >>> # | col |
  147. >>> # +---------+
  148. >>> # | [1,2,3] |
  149. >>> # +---------|
  150. >>> data = data.map(operations=PadEnd(pad_shape=[4], pad_value=10))
  151. >>> # Data after
  152. >>> # | col |
  153. >>> # +------------+
  154. >>> # | [1,2,3,10] |
  155. >>> # +------------|
  156. """
  157. @check_pad_end
  158. def __init__(self, pad_shape, pad_value=None):
  159. if pad_value is not None:
  160. pad_value = cde.Tensor(np.array(pad_value))
  161. super().__init__(cde.TensorShape(pad_shape), pad_value)
  162. class Concatenate(cde.ConcatenateOp):
  163. """
  164. Tensor operation to prepend and append to a tensor.
  165. Args:
  166. axis (int, optional): axis to concatenate the tensors along (Default=0).
  167. prepend (np.array, optional): numpy array to be prepended to the already concatenated tensors (Default=None).
  168. append (np.array, optional): numpy array to be appended to the already concatenated tensors (Default=None).
  169. """
  170. @check_concat_type
  171. def __init__(self, axis=0, prepend=None, append=None):
  172. if prepend is not None:
  173. prepend = cde.Tensor(np.array(prepend))
  174. if append is not None:
  175. append = cde.Tensor(np.array(append))
  176. super().__init__(axis, prepend, append)
  177. class Duplicate(cde.DuplicateOp):
  178. """
  179. Duplicate the input tensor to a new output tensor. The input tensor is carried over to the output list.
  180. Examples:
  181. >>> # Data before
  182. >>> # | x |
  183. >>> # +---------+
  184. >>> # | [1,2,3] |
  185. >>> # +---------+
  186. >>> data = data.map(input_columns=["x"], operations=Duplicate(),
  187. >>> output_columns=["x", "y"], columns_order=["x", "y"])
  188. >>> # Data after
  189. >>> # | x | y |
  190. >>> # +---------+---------+
  191. >>> # | [1,2,3] | [1,2,3] |
  192. >>> # +---------+---------+
  193. """