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.

vision_lite.h 9.8 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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_VISION_LITE_H_
  17. #define MINDSPORE_CCSRC_MINDDATA_DATASET_INCLUDE_VISION_LITE_H_
  18. #include <map>
  19. #include <memory>
  20. #include <string>
  21. #include <utility>
  22. #include <vector>
  23. #include "include/api/status.h"
  24. #include "minddata/dataset/include/constants.h"
  25. #include "minddata/dataset/include/transforms.h"
  26. namespace mindspore {
  27. namespace dataset {
  28. // Transform operations for performing computer vision.
  29. namespace vision {
  30. // Forward Declarations
  31. class RotateOperation;
  32. /// \brief Affine TensorTransform.
  33. /// \notes Apply affine transform on input image.
  34. class Affine : public TensorTransform {
  35. public:
  36. /// \brief Constructor.
  37. /// \param[in] degrees The degrees to rotate the image by
  38. /// \param[in] translation The value representing vertical and horizontal translation (default = {0.0, 0.0})
  39. /// The first value represent the x axis translation while the second represents y axis translation.
  40. /// \param[in] scale The scaling factor for the image (default = 0.0)
  41. /// \param[in] shear A float vector of size 2, representing the shear degrees (default = {0.0, 0.0})
  42. /// \param[in] interpolation An enum for the mode of interpolation
  43. /// \param[in] fill_value A vector representing the value to fill the area outside the transform
  44. /// in the output image. If 1 value is provided, it is used for all RGB channels.
  45. /// If 3 values are provided, it is used to fill R, G, B channels respectively.
  46. explicit Affine(float_t degrees, const std::vector<float> &translation = {0.0, 0.0}, float scale = 0.0,
  47. const std::vector<float> &shear = {0.0, 0.0},
  48. InterpolationMode interpolation = InterpolationMode::kNearestNeighbour,
  49. const std::vector<uint8_t> &fill_value = {0, 0, 0});
  50. /// \brief Destructor.
  51. ~Affine() = default;
  52. /// \brief Function to convert TensorTransform object into a TensorOperation object.
  53. /// \return Shared pointer to TensorOperation object.
  54. std::shared_ptr<TensorOperation> Parse() override;
  55. private:
  56. struct Data;
  57. std::shared_ptr<Data> data_;
  58. };
  59. /// \brief CenterCrop TensorTransform.
  60. /// \notes Crops the input image at the center to the given size.
  61. class CenterCrop : public TensorTransform {
  62. public:
  63. /// \brief Constructor.
  64. /// \param[in] size A vector representing the output size of the cropped image.
  65. /// If size is a single value, a square crop of size (size, size) is returned.
  66. /// If size has 2 values, it should be (height, width).
  67. explicit CenterCrop(std::vector<int32_t> size);
  68. /// \brief Destructor.
  69. ~CenterCrop() = default;
  70. protected:
  71. /// \brief Function to convert TensorTransform object into a TensorOperation object.
  72. /// \return Shared pointer to TensorOperation object.
  73. std::shared_ptr<TensorOperation> Parse() override;
  74. std::shared_ptr<TensorOperation> Parse(const MapTargetDevice &env) override;
  75. private:
  76. struct Data;
  77. std::shared_ptr<Data> data_;
  78. };
  79. /// \brief Crop TensorTransform.
  80. /// \notes Crop an image based on location and crop size
  81. class Crop : public TensorTransform {
  82. public:
  83. /// \brief Constructor.
  84. /// \param[in] coordinates Starting location of crop. Must be a vector of two values, in the form of {x_coor, y_coor}
  85. /// \param[in] size Size of the cropped area.
  86. /// If size is a single value, a square crop of size (size, size) is returned.
  87. /// If size has 2 values, it should be (height, width).
  88. Crop(std::vector<int32_t> coordinates, std::vector<int32_t> size);
  89. /// \brief Destructor.
  90. ~Crop() = default;
  91. protected:
  92. /// \brief Function to convert TensorTransform object into a TensorOperation object.
  93. /// \return Shared pointer to TensorOperation object.
  94. std::shared_ptr<TensorOperation> Parse() override;
  95. private:
  96. struct Data;
  97. std::shared_ptr<Data> data_;
  98. };
  99. /// \brief Decode TensorTransform.
  100. /// \notes Decode the input image in RGB mode.
  101. class Decode : public TensorTransform {
  102. public:
  103. /// \brief Constructor.
  104. /// \param[in] rgb A boolean of whether to decode in RGB mode or not.
  105. explicit Decode(bool rgb = true);
  106. /// \brief Destructor.
  107. ~Decode() = default;
  108. protected:
  109. /// \brief Function to convert TensorTransform object into a TensorOperation object.
  110. /// \return Shared pointer to TensorOperation object.
  111. std::shared_ptr<TensorOperation> Parse() override;
  112. std::shared_ptr<TensorOperation> Parse(const MapTargetDevice &env) override;
  113. private:
  114. struct Data;
  115. std::shared_ptr<Data> data_;
  116. };
  117. /// \brief Normalize TensorTransform.
  118. /// \notes Normalize the input image with respect to mean and standard deviation.
  119. class Normalize : public TensorTransform {
  120. public:
  121. /// \brief Constructor.
  122. /// \param[in] mean A vector of mean values for each channel, w.r.t channel order.
  123. /// The mean values must be in range [0.0, 255.0].
  124. /// \param[in] std A vector of standard deviations for each channel, w.r.t. channel order.
  125. /// The standard deviation values must be in range (0.0, 255.0]
  126. Normalize(std::vector<float> mean, std::vector<float> std);
  127. /// \brief Destructor.
  128. ~Normalize() = default;
  129. protected:
  130. /// \brief Function to convert TensorTransform object into a TensorOperation object.
  131. /// \return Shared pointer to TensorOperation object.
  132. std::shared_ptr<TensorOperation> Parse() override;
  133. std::shared_ptr<TensorOperation> Parse(const MapTargetDevice &env) override;
  134. private:
  135. struct Data;
  136. std::shared_ptr<Data> data_;
  137. };
  138. /// \brief RandomAffine TensorTransform.
  139. /// \notes Applies a Random Affine transformation on input image in RGB or Greyscale mode.
  140. class RandomAffine : public TensorTransform {
  141. public:
  142. /// \brief Constructor.
  143. /// \param[in] degrees A float vector of size 2, representing the starting and ending degree
  144. /// \param[in] translate_range A float vector of size 2 or 4, representing percentages of translation on x and y axes.
  145. /// if size is 2, (min_dx, max_dx, 0, 0)
  146. /// if size is 4, (min_dx, max_dx, min_dy, max_dy)
  147. /// all values are in range [-1, 1]
  148. /// \param[in] scale_range A float vector of size 2, representing the starting and ending scales in the range.
  149. /// \param[in] shear_ranges A float vector of size 2 or 4, representing the starting and ending shear degrees
  150. /// vertically and horizontally.
  151. /// if size is 2, (min_shear_x, max_shear_x, 0, 0)
  152. /// if size is 4, (min_shear_x, max_shear_x, min_shear_y, max_shear_y)
  153. /// \param[in] interpolation An enum for the mode of interpolation
  154. /// \param[in] fill_value A vector representing the value to fill the area outside the transform
  155. /// in the output image. If 1 value is provided, it is used for all RGB channels.
  156. /// If 3 values are provided, it is used to fill R, G, B channels respectively.
  157. explicit RandomAffine(const std::vector<float_t> &degrees,
  158. const std::vector<float_t> &translate_range = {0.0, 0.0, 0.0, 0.0},
  159. const std::vector<float_t> &scale_range = {1.0, 1.0},
  160. const std::vector<float_t> &shear_ranges = {0.0, 0.0, 0.0, 0.0},
  161. InterpolationMode interpolation = InterpolationMode::kNearestNeighbour,
  162. const std::vector<uint8_t> &fill_value = {0, 0, 0});
  163. /// \brief Destructor.
  164. ~RandomAffine() = default;
  165. /// \brief Function to convert TensorTransform object into a TensorOperation object.
  166. /// \return Shared pointer to TensorOperation object.
  167. std::shared_ptr<TensorOperation> Parse() override;
  168. private:
  169. struct Data;
  170. std::shared_ptr<Data> data_;
  171. };
  172. /// \brief Resize TensorTransform.
  173. /// \notes Resize the input image to the given size.
  174. class Resize : public TensorTransform {
  175. public:
  176. /// \brief Constructor.
  177. /// \param[in] size A vector representing the output size of the resized image.
  178. /// If size is a single value, the image will be resized to this value with
  179. /// the same image aspect ratio. If size has 2 values, it should be (height, width).
  180. /// \param[in] interpolation An enum for the mode of interpolation
  181. explicit Resize(std::vector<int32_t> size, InterpolationMode interpolation = InterpolationMode::kLinear);
  182. /// \brief Destructor.
  183. ~Resize() = default;
  184. protected:
  185. /// \brief Function to convert TensorTransform object into a TensorOperation object.
  186. /// \return Shared pointer to TensorOperation object.
  187. std::shared_ptr<TensorOperation> Parse() override;
  188. std::shared_ptr<TensorOperation> Parse(const MapTargetDevice &env) override;
  189. private:
  190. struct Data;
  191. std::shared_ptr<Data> data_;
  192. };
  193. /// \brief Rotate TensorTransform.
  194. /// \notes Rotate the input image using a specified angle id.
  195. class Rotate : public TensorTransform {
  196. public:
  197. /// \brief Constructor.
  198. Rotate();
  199. /// \brief Destructor.
  200. ~Rotate() = default;
  201. protected:
  202. /// \brief Function to convert TensorTransform object into a TensorOperation object.
  203. /// \return Shared pointer to TensorOperation object.
  204. std::shared_ptr<TensorOperation> Parse() override;
  205. private:
  206. std::shared_ptr<RotateOperation> op_;
  207. };
  208. } // namespace vision
  209. } // namespace dataset
  210. } // namespace mindspore
  211. #endif // MINDSPORE_CCSRC_MINDDATA_DATASET_INCLUDE_VISION_LITE_H_