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 10 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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. float degrees_;
  57. std::vector<float> translation_;
  58. float scale_;
  59. std::vector<float> shear_;
  60. InterpolationMode interpolation_;
  61. std::vector<uint8_t> fill_value_;
  62. };
  63. /// \brief CenterCrop TensorTransform.
  64. /// \notes Crops the input image at the center to the given size.
  65. class CenterCrop : public TensorTransform {
  66. public:
  67. /// \brief Constructor.
  68. /// \param[in] size A vector representing the output size of the cropped image.
  69. /// If size is a single value, a square crop of size (size, size) is returned.
  70. /// If size has 2 values, it should be (height, width).
  71. explicit CenterCrop(std::vector<int32_t> size);
  72. /// \brief Destructor.
  73. ~CenterCrop() = default;
  74. protected:
  75. /// \brief Function to convert TensorTransform object into a TensorOperation object.
  76. /// \return Shared pointer to TensorOperation object.
  77. std::shared_ptr<TensorOperation> Parse() override;
  78. std::shared_ptr<TensorOperation> Parse(const MapTargetDevice &env) override;
  79. private:
  80. std::vector<int32_t> size_;
  81. };
  82. /// \brief Crop TensorTransform.
  83. /// \notes Crop an image based on location and crop size
  84. class Crop : public TensorTransform {
  85. public:
  86. /// \brief Constructor.
  87. /// \param[in] coordinates Starting location of crop. Must be a vector of two values, in the form of {x_coor, y_coor}
  88. /// \param[in] size Size of the cropped area.
  89. /// If size is a single value, a square crop of size (size, size) is returned.
  90. /// If size has 2 values, it should be (height, width).
  91. Crop(std::vector<int32_t> coordinates, std::vector<int32_t> size);
  92. /// \brief Destructor.
  93. ~Crop() = default;
  94. protected:
  95. /// \brief Function to convert TensorTransform object into a TensorOperation object.
  96. /// \return Shared pointer to TensorOperation object.
  97. std::shared_ptr<TensorOperation> Parse() override;
  98. private:
  99. std::vector<int32_t> coordinates_;
  100. std::vector<int32_t> size_;
  101. };
  102. /// \brief Decode TensorTransform.
  103. /// \notes Decode the input image in RGB mode.
  104. class Decode : public TensorTransform {
  105. public:
  106. /// \brief Constructor.
  107. /// \param[in] rgb A boolean of whether to decode in RGB mode or not.
  108. explicit Decode(bool rgb = true);
  109. /// \brief Destructor.
  110. ~Decode() = default;
  111. protected:
  112. /// \brief Function to convert TensorTransform object into a TensorOperation object.
  113. /// \return Shared pointer to TensorOperation object.
  114. std::shared_ptr<TensorOperation> Parse() override;
  115. std::shared_ptr<TensorOperation> Parse(const MapTargetDevice &env) override;
  116. private:
  117. bool rgb_;
  118. };
  119. /// \brief Normalize TensorTransform.
  120. /// \notes Normalize the input image with respect to mean and standard deviation.
  121. class Normalize : public TensorTransform {
  122. public:
  123. /// \brief Constructor.
  124. /// \param[in] mean A vector of mean values for each channel, w.r.t channel order.
  125. /// The mean values must be in range [0.0, 255.0].
  126. /// \param[in] std A vector of standard deviations for each channel, w.r.t. channel order.
  127. /// The standard deviation values must be in range (0.0, 255.0]
  128. Normalize(std::vector<float> mean, std::vector<float> std);
  129. /// \brief Destructor.
  130. ~Normalize() = default;
  131. protected:
  132. /// \brief Function to convert TensorTransform object into a TensorOperation object.
  133. /// \return Shared pointer to TensorOperation object.
  134. std::shared_ptr<TensorOperation> Parse() override;
  135. std::shared_ptr<TensorOperation> Parse(const MapTargetDevice &env) override;
  136. private:
  137. std::vector<float> mean_;
  138. std::vector<float> std_;
  139. };
  140. /// \brief RandomAffine TensorTransform.
  141. /// \notes Applies a Random Affine transformation on input image in RGB or Greyscale mode.
  142. class RandomAffine : public TensorTransform {
  143. public:
  144. /// \brief Constructor.
  145. /// \param[in] degrees A float vector of size 2, representing the starting and ending degree
  146. /// \param[in] translate_range A float vector of size 2 or 4, representing percentages of translation on x and y axes.
  147. /// if size is 2, (min_dx, max_dx, 0, 0)
  148. /// if size is 4, (min_dx, max_dx, min_dy, max_dy)
  149. /// all values are in range [-1, 1]
  150. /// \param[in] scale_range A float vector of size 2, representing the starting and ending scales in the range.
  151. /// \param[in] shear_ranges A float vector of size 2 or 4, representing the starting and ending shear degrees
  152. /// vertically and horizontally.
  153. /// if size is 2, (min_shear_x, max_shear_x, 0, 0)
  154. /// if size is 4, (min_shear_x, max_shear_x, min_shear_y, max_shear_y)
  155. /// \param[in] interpolation An enum for the mode of interpolation
  156. /// \param[in] fill_value A vector representing the value to fill the area outside the transform
  157. /// in the output image. If 1 value is provided, it is used for all RGB channels.
  158. /// If 3 values are provided, it is used to fill R, G, B channels respectively.
  159. explicit RandomAffine(const std::vector<float_t> &degrees,
  160. const std::vector<float_t> &translate_range = {0.0, 0.0, 0.0, 0.0},
  161. const std::vector<float_t> &scale_range = {1.0, 1.0},
  162. const std::vector<float_t> &shear_ranges = {0.0, 0.0, 0.0, 0.0},
  163. InterpolationMode interpolation = InterpolationMode::kNearestNeighbour,
  164. const std::vector<uint8_t> &fill_value = {0, 0, 0});
  165. /// \brief Destructor.
  166. ~RandomAffine() = default;
  167. /// \brief Function to convert TensorTransform object into a TensorOperation object.
  168. /// \return Shared pointer to TensorOperation object.
  169. std::shared_ptr<TensorOperation> Parse() override;
  170. private:
  171. std::vector<float_t> degrees_; // min_degree, max_degree
  172. std::vector<float_t> translate_range_; // maximum x translation percentage, maximum y translation percentage
  173. std::vector<float_t> scale_range_; // min_scale, max_scale
  174. std::vector<float_t> shear_ranges_; // min_x_shear, max_x_shear, min_y_shear, max_y_shear
  175. InterpolationMode interpolation_;
  176. std::vector<uint8_t> fill_value_;
  177. };
  178. /// \brief Resize TensorTransform.
  179. /// \notes Resize the input image to the given size.
  180. class Resize : public TensorTransform {
  181. public:
  182. /// \brief Constructor.
  183. /// \param[in] size A vector representing the output size of the resized image.
  184. /// If size is a single value, the image will be resized to this value with
  185. /// the same image aspect ratio. If size has 2 values, it should be (height, width).
  186. /// \param[in] interpolation An enum for the mode of interpolation
  187. explicit Resize(std::vector<int32_t> size, InterpolationMode interpolation = InterpolationMode::kLinear);
  188. /// \brief Destructor.
  189. ~Resize() = default;
  190. protected:
  191. /// \brief Function to convert TensorTransform object into a TensorOperation object.
  192. /// \return Shared pointer to TensorOperation object.
  193. std::shared_ptr<TensorOperation> Parse() override;
  194. std::shared_ptr<TensorOperation> Parse(const MapTargetDevice &env) override;
  195. private:
  196. std::vector<int32_t> size_;
  197. InterpolationMode interpolation_;
  198. };
  199. /// \brief Rotate TensorTransform.
  200. /// \notes Rotate the input image using a specified angle id.
  201. class Rotate : public TensorTransform {
  202. public:
  203. /// \brief Constructor.
  204. Rotate();
  205. /// \brief Destructor.
  206. ~Rotate() = default;
  207. protected:
  208. /// \brief Function to convert TensorTransform object into a TensorOperation object.
  209. /// \return Shared pointer to TensorOperation object.
  210. std::shared_ptr<TensorOperation> Parse() override;
  211. private:
  212. std::shared_ptr<RotateOperation> op_;
  213. };
  214. } // namespace vision
  215. } // namespace dataset
  216. } // namespace mindspore
  217. #endif // MINDSPORE_CCSRC_MINDDATA_DATASET_INCLUDE_VISION_LITE_H_