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.

transforms.h 8.7 kB

5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /**
  2. * Copyright 2020-2021 Huawei Technologies Co., Ltd
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #ifndef MINDSPORE_CCSRC_MINDDATA_DATASET_INCLUDE_TRANSFORMS_H_
  17. #define MINDSPORE_CCSRC_MINDDATA_DATASET_INCLUDE_TRANSFORMS_H_
  18. #include <map>
  19. #include <memory>
  20. #include <string>
  21. #include <vector>
  22. #include "include/api/dual_abi_helper.h"
  23. #include "include/api/status.h"
  24. #include "minddata/dataset/include/constants.h"
  25. namespace mindspore {
  26. namespace dataset {
  27. class TensorOperation;
  28. // We need the following two groups of forward declaration to friend the class in class TensorTransform.
  29. namespace transforms {
  30. class Compose;
  31. class RandomApply;
  32. class RandomChoice;
  33. } // namespace transforms
  34. namespace vision {
  35. class BoundingBoxAugment;
  36. class RandomSelectSubpolicy;
  37. class UniformAugment;
  38. } // namespace vision
  39. // Abstract class to represent a tensor transform operation in the data pipeline.
  40. /// \class TensorTransform transforms.h
  41. /// \brief A base class to represent a tensor transform operation in the data pipeline.
  42. class TensorTransform : public std::enable_shared_from_this<TensorTransform> {
  43. friend class Dataset;
  44. friend class Execute;
  45. friend class transforms::Compose;
  46. friend class transforms::RandomApply;
  47. friend class transforms::RandomChoice;
  48. friend class vision::BoundingBoxAugment;
  49. friend class vision::RandomSelectSubpolicy;
  50. friend class vision::UniformAugment;
  51. public:
  52. /// \brief Constructor
  53. TensorTransform() {}
  54. /// \brief Destructor
  55. ~TensorTransform() = default;
  56. protected:
  57. /// \brief Pure virtual function to convert a TensorTransform class into a IR TensorOperation object.
  58. /// \return shared pointer to the newly created TensorOperation.
  59. virtual std::shared_ptr<TensorOperation> Parse() = 0;
  60. /// \brief Virtual function to convert a TensorTransform class into a IR TensorOperation object.
  61. /// \param[in] env A string to determine the running environment
  62. /// \return shared pointer to the newly created TensorOperation.
  63. virtual std::shared_ptr<TensorOperation> Parse(const MapTargetDevice &env) { return nullptr; }
  64. };
  65. // Transform operations for performing data transformation.
  66. namespace transforms {
  67. /// \brief Compose Op.
  68. /// \notes Compose a list of transforms into a single transform.
  69. class Compose : public TensorTransform {
  70. public:
  71. /// \brief Constructor.
  72. /// \param[in] transforms A vector of raw pointers to TensorTransform objects to be applied.
  73. explicit Compose(const std::vector<TensorTransform *> &transforms);
  74. /// \brief Constructor.
  75. /// \param[in] transforms A vector of shared pointers to TensorTransform objects to be applied.
  76. explicit Compose(const std::vector<std::shared_ptr<TensorTransform>> &transforms);
  77. /// \brief Constructor.
  78. /// \param[in] transforms A vector of TensorTransform objects to be applied.
  79. explicit Compose(const std::vector<std::reference_wrapper<TensorTransform>> &transforms);
  80. /// \brief Destructor
  81. ~Compose() = default;
  82. protected:
  83. /// \brief Function to convert TensorTransform object into a TensorOperation object.
  84. /// \return Shared pointer to TensorOperation object.
  85. std::shared_ptr<TensorOperation> Parse() override;
  86. private:
  87. struct Data;
  88. std::shared_ptr<Data> data_;
  89. };
  90. /// \brief Duplicate Op.
  91. /// \notes Duplicate the input tensor to a new output tensor.
  92. /// The input tensor is carried over to the output list.
  93. class Duplicate : public TensorTransform {
  94. public:
  95. /// \brief Constructor.
  96. Duplicate();
  97. /// \brief Destructor
  98. ~Duplicate() = default;
  99. protected:
  100. /// \brief Function to convert TensorTransform object into a TensorOperation object.
  101. /// \return Shared pointer to TensorOperation object.
  102. std::shared_ptr<TensorOperation> Parse() override;
  103. };
  104. /// \brief OneHot Op.
  105. /// \notes Convert the labels into OneHot format.
  106. class OneHot : public TensorTransform {
  107. public:
  108. /// \brief Constructor.
  109. /// \param[in] num_classes number of classes.
  110. explicit OneHot(int32_t num_classes);
  111. /// \brief Destructor
  112. ~OneHot() = default;
  113. protected:
  114. /// \brief Function to convert TensorTransform object into a TensorOperation object.
  115. /// \return Shared pointer to TensorOperation object.
  116. std::shared_ptr<TensorOperation> Parse() override;
  117. private:
  118. struct Data;
  119. std::shared_ptr<Data> data_;
  120. };
  121. /// \brief RandomApply Op.
  122. /// \notes Randomly perform a series of transforms with a given probability.
  123. class RandomApply : public TensorTransform {
  124. public:
  125. /// \brief Constructor.
  126. /// \param[in] transforms A vector of raw pointers to TensorTransform objects to be applied.
  127. /// \param[in] prob The probability to apply the transformation list (default=0.5)
  128. explicit RandomApply(const std::vector<TensorTransform *> &transforms, double prob = 0.5);
  129. /// \brief Constructor.
  130. /// \param[in] transforms A vector of shared pointers to TensorTransform objects to be applied.
  131. /// \param[in] prob The probability to apply the transformation list (default=0.5)
  132. explicit RandomApply(const std::vector<std::shared_ptr<TensorTransform>> &transforms, double prob = 0.5);
  133. /// \brief Constructor.
  134. /// \param[in] transforms A vector of TensorTransform objects to be applied.
  135. /// \param[in] prob The probability to apply the transformation list (default=0.5)
  136. explicit RandomApply(const std::vector<std::reference_wrapper<TensorTransform>> &transforms, double prob = 0.5);
  137. /// \brief Destructor
  138. ~RandomApply() = default;
  139. protected:
  140. /// \brief Function to convert TensorTransform object into a TensorOperation object.
  141. /// \return Shared pointer to TensorOperation object.
  142. std::shared_ptr<TensorOperation> Parse() override;
  143. private:
  144. struct Data;
  145. std::shared_ptr<Data> data_;
  146. };
  147. /// \brief RandomChoice Op.
  148. /// \notes Randomly selects one transform from a list of transforms to perform operation.
  149. class RandomChoice : public TensorTransform {
  150. public:
  151. /// \brief Constructor.
  152. /// \param[in] transforms A vector of raw pointers to TensorTransform objects to be applied.
  153. explicit RandomChoice(const std::vector<TensorTransform *> &transforms);
  154. /// \brief Constructor.
  155. /// \param[in] transforms A vector of shared pointers to TensorTransform objects to be applied.
  156. explicit RandomChoice(const std::vector<std::shared_ptr<TensorTransform>> &transforms);
  157. /// \brief Constructor.
  158. /// \param[in] transforms A vector of TensorTransform objects to be applied.
  159. explicit RandomChoice(const std::vector<std::reference_wrapper<TensorTransform>> &transforms);
  160. /// \brief Destructor
  161. ~RandomChoice() = default;
  162. protected:
  163. /// \brief Function to convert TensorTransform object into a TensorOperation object.
  164. /// \return Shared pointer to TensorOperation object.
  165. std::shared_ptr<TensorOperation> Parse() override;
  166. private:
  167. struct Data;
  168. std::shared_ptr<Data> data_;
  169. };
  170. /// \brief TypeCast Op.
  171. /// \notes Tensor operation to cast to a given MindSpore data type.
  172. class TypeCast : public TensorTransform {
  173. public:
  174. /// \brief Constructor.
  175. /// \param[in] data_type mindspore.dtype to be cast to.
  176. explicit TypeCast(std::string data_type) : TypeCast(StringToChar(data_type)) {}
  177. explicit TypeCast(const std::vector<char> &data_type);
  178. /// \brief Destructor
  179. ~TypeCast() = default;
  180. protected:
  181. /// \brief Function to convert TensorTransform object into a TensorOperation object.
  182. /// \return Shared pointer to TensorOperation object.
  183. std::shared_ptr<TensorOperation> Parse() override;
  184. private:
  185. struct Data;
  186. std::shared_ptr<Data> data_;
  187. };
  188. /// \brief Unique Op.
  189. /// \notes Return an output tensor containing all the unique elements of the input tensor in
  190. /// the same order that they occur in the input tensor.
  191. class Unique : public TensorTransform {
  192. public:
  193. /// \brief Constructor.
  194. Unique();
  195. /// \brief Destructor
  196. ~Unique() = default;
  197. protected:
  198. /// \brief Function to convert TensorTransform object into a TensorOperation object.
  199. /// \return Shared pointer to TensorOperation object.
  200. std::shared_ptr<TensorOperation> Parse() override;
  201. };
  202. } // namespace transforms
  203. } // namespace dataset
  204. } // namespace mindspore
  205. #endif // MINDSPORE_CCSRC_MINDDATA_DATASET_INCLUDE_TRANSFORMS_H_