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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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 final : 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 final : 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 RGB2GRAY TensorTransform.
  80. /// \notes Convert RGB image or color image to grayscale image
  81. class RGB2GRAY final : public TensorTransform {
  82. public:
  83. /// \brief Constructor.
  84. RGB2GRAY() = default;
  85. /// \brief Destructor.
  86. ~RGB2GRAY() = default;
  87. protected:
  88. /// \brief Function to convert TensorTransform object into a TensorOperation object.
  89. /// \return Shared pointer to TensorOperation object.
  90. std::shared_ptr<TensorOperation> Parse() override;
  91. };
  92. /// \brief Crop TensorTransform.
  93. /// \notes Crop an image based on location and crop size
  94. class Crop final : public TensorTransform {
  95. public:
  96. /// \brief Constructor.
  97. /// \param[in] coordinates Starting location of crop. Must be a vector of two values, in the form of {x_coor, y_coor}
  98. /// \param[in] size Size of the cropped area.
  99. /// If size is a single value, a square crop of size (size, size) is returned.
  100. /// If size has 2 values, it should be (height, width).
  101. Crop(std::vector<int32_t> coordinates, std::vector<int32_t> size);
  102. /// \brief Destructor.
  103. ~Crop() = default;
  104. protected:
  105. /// \brief Function to convert TensorTransform object into a TensorOperation object.
  106. /// \return Shared pointer to TensorOperation object.
  107. std::shared_ptr<TensorOperation> Parse() override;
  108. private:
  109. struct Data;
  110. std::shared_ptr<Data> data_;
  111. };
  112. /// \brief Decode TensorTransform.
  113. /// \notes Decode the input image in RGB mode.
  114. class Decode final : public TensorTransform {
  115. public:
  116. /// \brief Constructor.
  117. /// \param[in] rgb A boolean of whether to decode in RGB mode or not.
  118. explicit Decode(bool rgb = true);
  119. /// \brief Destructor.
  120. ~Decode() = default;
  121. protected:
  122. /// \brief Function to convert TensorTransform object into a TensorOperation object.
  123. /// \return Shared pointer to TensorOperation object.
  124. std::shared_ptr<TensorOperation> Parse() override;
  125. std::shared_ptr<TensorOperation> Parse(const MapTargetDevice &env) override;
  126. private:
  127. struct Data;
  128. std::shared_ptr<Data> data_;
  129. };
  130. /// \brief Normalize TensorTransform.
  131. /// \notes Normalize the input image with respect to mean and standard deviation.
  132. class Normalize final : public TensorTransform {
  133. public:
  134. /// \brief Constructor.
  135. /// \param[in] mean A vector of mean values for each channel, w.r.t channel order.
  136. /// The mean values must be in range [0.0, 255.0].
  137. /// \param[in] std A vector of standard deviations for each channel, w.r.t. channel order.
  138. /// The standard deviation values must be in range (0.0, 255.0]
  139. Normalize(std::vector<float> mean, std::vector<float> std);
  140. /// \brief Destructor.
  141. ~Normalize() = default;
  142. protected:
  143. /// \brief Function to convert TensorTransform object into a TensorOperation object.
  144. /// \return Shared pointer to TensorOperation object.
  145. std::shared_ptr<TensorOperation> Parse() override;
  146. std::shared_ptr<TensorOperation> Parse(const MapTargetDevice &env) override;
  147. private:
  148. struct Data;
  149. std::shared_ptr<Data> data_;
  150. };
  151. /// \brief RandomAffine TensorTransform.
  152. /// \notes Applies a Random Affine transformation on input image in RGB or Greyscale mode.
  153. class RandomAffine final : public TensorTransform {
  154. public:
  155. /// \brief Constructor.
  156. /// \param[in] degrees A float vector of size 2, representing the starting and ending degree
  157. /// \param[in] translate_range A float vector of size 2 or 4, representing percentages of translation on x and y axes.
  158. /// if size is 2, (min_dx, max_dx, 0, 0)
  159. /// if size is 4, (min_dx, max_dx, min_dy, max_dy)
  160. /// all values are in range [-1, 1]
  161. /// \param[in] scale_range A float vector of size 2, representing the starting and ending scales in the range.
  162. /// \param[in] shear_ranges A float vector of size 2 or 4, representing the starting and ending shear degrees
  163. /// vertically and horizontally.
  164. /// if size is 2, (min_shear_x, max_shear_x, 0, 0)
  165. /// if size is 4, (min_shear_x, max_shear_x, min_shear_y, max_shear_y)
  166. /// \param[in] interpolation An enum for the mode of interpolation
  167. /// \param[in] fill_value A vector representing the value to fill the area outside the transform
  168. /// in the output image. If 1 value is provided, it is used for all RGB channels.
  169. /// If 3 values are provided, it is used to fill R, G, B channels respectively.
  170. explicit RandomAffine(const std::vector<float_t> &degrees,
  171. const std::vector<float_t> &translate_range = {0.0, 0.0, 0.0, 0.0},
  172. const std::vector<float_t> &scale_range = {1.0, 1.0},
  173. const std::vector<float_t> &shear_ranges = {0.0, 0.0, 0.0, 0.0},
  174. InterpolationMode interpolation = InterpolationMode::kNearestNeighbour,
  175. const std::vector<uint8_t> &fill_value = {0, 0, 0});
  176. /// \brief Destructor.
  177. ~RandomAffine() = default;
  178. /// \brief Function to convert TensorTransform object into a TensorOperation object.
  179. /// \return Shared pointer to TensorOperation object.
  180. std::shared_ptr<TensorOperation> Parse() override;
  181. private:
  182. struct Data;
  183. std::shared_ptr<Data> data_;
  184. };
  185. /// \brief Resize TensorTransform.
  186. /// \notes Resize the input image to the given size.
  187. class Resize final : public TensorTransform {
  188. public:
  189. /// \brief Constructor.
  190. /// \param[in] size A vector representing the output size of the resized image.
  191. /// If size is a single value, the image will be resized to this value with
  192. /// the same image aspect ratio. If size has 2 values, it should be (height, width).
  193. /// \param[in] interpolation An enum for the mode of interpolation
  194. explicit Resize(std::vector<int32_t> size, InterpolationMode interpolation = InterpolationMode::kLinear);
  195. /// \brief Destructor.
  196. ~Resize() = 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. std::shared_ptr<TensorOperation> Parse(const MapTargetDevice &env) override;
  202. private:
  203. struct Data;
  204. std::shared_ptr<Data> data_;
  205. };
  206. /// \brief ResizePreserveAR TensorTransform.
  207. /// \notes Keep the original picture ratio and fill the rest.
  208. class ResizePreserveAR final : public TensorTransform {
  209. public:
  210. /// \brief Constructor.
  211. /// \param[in] height The height of image output value after resizing.
  212. /// \param[in] width The width of image output value after resizing.
  213. /// \param[in] img_orientation Angle method of image rotation.
  214. ResizePreserveAR(int32_t height, int32_t width, int32_t img_orientation = 0);
  215. /// \brief Destructor.
  216. ~ResizePreserveAR() = default;
  217. protected:
  218. /// \brief Function to convert TensorTransform object into a TensorOperation object.
  219. /// \return Shared pointer to TensorOperation object.
  220. std::shared_ptr<TensorOperation> Parse() override;
  221. private:
  222. struct Data;
  223. std::shared_ptr<Data> data_;
  224. };
  225. /// \brief Rotate TensorTransform.
  226. /// \notes Rotate the input image using a specified angle id.
  227. class Rotate final : public TensorTransform {
  228. public:
  229. /// \brief Constructor.
  230. Rotate();
  231. /// \brief Destructor.
  232. ~Rotate() = default;
  233. protected:
  234. /// \brief Function to convert TensorTransform object into a TensorOperation object.
  235. /// \return Shared pointer to TensorOperation object.
  236. std::shared_ptr<TensorOperation> Parse() override;
  237. private:
  238. std::shared_ptr<RotateOperation> op_;
  239. };
  240. } // namespace vision
  241. } // namespace dataset
  242. } // namespace mindspore
  243. #endif // MINDSPORE_CCSRC_MINDDATA_DATASET_INCLUDE_VISION_LITE_H_