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 26 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
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674
  1. /**
  2. * Copyright 2020 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_API_TRANSFORMS_H_
  17. #define MINDSPORE_CCSRC_MINDDATA_DATASET_API_TRANSFORMS_H_
  18. #include <vector>
  19. #include <memory>
  20. #include "minddata/dataset/core/constants.h"
  21. namespace mindspore {
  22. namespace dataset {
  23. class TensorOp;
  24. namespace api {
  25. // Abstract class to represent a dataset in the data pipeline.
  26. class TensorOperation : public std::enable_shared_from_this<TensorOperation> {
  27. public:
  28. /// \brief Constructor
  29. TensorOperation();
  30. /// \brief Destructor
  31. ~TensorOperation() = default;
  32. /// \brief Pure virtual function to convert a TensorOperation class into a runtime TensorOp object.
  33. /// \return shared pointer to the newly created TensorOp.
  34. virtual std::shared_ptr<TensorOp> Build() = 0;
  35. virtual bool ValidateParams() = 0;
  36. };
  37. // Transform operations for performing computer vision.
  38. namespace vision {
  39. // Transform Op classes (in alphabetical order)
  40. class CenterCropOperation;
  41. class CropOperation;
  42. class CutMixBatchOperation;
  43. class CutOutOperation;
  44. class DecodeOperation;
  45. class HwcToChwOperation;
  46. class MixUpBatchOperation;
  47. class NormalizeOperation;
  48. class OneHotOperation;
  49. class PadOperation;
  50. class RandomAffineOperation;
  51. class RandomColorOperation;
  52. class RandomColorAdjustOperation;
  53. class RandomCropOperation;
  54. class RandomHorizontalFlipOperation;
  55. class RandomPosterizeOperation;
  56. class RandomRotationOperation;
  57. class RandomSharpnessOperation;
  58. class RandomSolarizeOperation;
  59. class RandomVerticalFlipOperation;
  60. class ResizeOperation;
  61. class RgbaToBgrOperation;
  62. class RgbaToRgbOperation;
  63. class SwapRedBlueOperation;
  64. class UniformAugOperation;
  65. /// \brief Function to create a CenterCrop TensorOperation.
  66. /// \notes Crops the input image at the center to the given size.
  67. /// \param[in] size - a vector representing the output size of the cropped image.
  68. /// If size is a single value, a square crop of size (size, size) is returned.
  69. /// If size has 2 values, it should be (height, width).
  70. /// \return Shared pointer to the current TensorOperation.
  71. std::shared_ptr<CenterCropOperation> CenterCrop(std::vector<int32_t> size);
  72. /// \brief Function to create a Crop TensorOp
  73. /// \notes Crop an image based on location and crop size
  74. /// \param[in] coordinates Starting location of crop. Must be a vector of two values, in the form of {x_coor, y_coor}
  75. /// \param[in] size Size of the cropped area. Must be a vector of two values, in the form of {height, width}
  76. /// \return Shared pointer to the current TensorOp
  77. std::shared_ptr<CropOperation> Crop(std::vector<int32_t> coordinates, std::vector<int32_t> size);
  78. /// \brief Function to apply CutMix on a batch of images
  79. /// \notes Masks a random section of each image with the corresponding part of another randomly selected image in
  80. /// that batch
  81. /// \param[in] image_batch_format The format of the batch
  82. /// \param[in] alpha The hyperparameter of beta distribution (default = 1.0)
  83. /// \param[in] prob The probability by which CutMix is applied to each image (default = 1.0)
  84. /// \return Shared pointer to the current TensorOp
  85. std::shared_ptr<CutMixBatchOperation> CutMixBatch(ImageBatchFormat image_batch_format, float alpha = 1.0,
  86. float prob = 1.0);
  87. /// \brief Function to create a CutOut TensorOp
  88. /// \notes Randomly cut (mask) out a given number of square patches from the input image
  89. /// \param[in] length Integer representing the side length of each square patch
  90. /// \param[in] num_patches Integer representing the number of patches to be cut out of an image
  91. /// \return Shared pointer to the current TensorOp
  92. std::shared_ptr<CutOutOperation> CutOut(int32_t length, int32_t num_patches = 1);
  93. /// \brief Function to create a Decode TensorOperation.
  94. /// \notes Decode the input image in RGB mode.
  95. /// \param[in] rgb - a boolean of whether to decode in RGB mode or not.
  96. /// \return Shared pointer to the current TensorOperation.
  97. std::shared_ptr<DecodeOperation> Decode(bool rgb = true);
  98. /// \brief Function to create a HwcToChw TensorOperation.
  99. /// \notes Transpose the input image; shape (H, W, C) to shape (C, H, W).
  100. /// \return Shared pointer to the current TensorOperation.
  101. std::shared_ptr<HwcToChwOperation> HWC2CHW();
  102. /// \brief Function to create a MixUpBatch TensorOperation.
  103. /// \notes Apply MixUp transformation on an input batch of images and labels. The labels must be in one-hot format and
  104. /// Batch must be called before calling this function.
  105. /// \param[in] alpha hyperparameter of beta distribution (default = 1.0)
  106. /// \return Shared pointer to the current TensorOperation.
  107. std::shared_ptr<MixUpBatchOperation> MixUpBatch(float alpha = 1);
  108. /// \brief Function to create a Normalize TensorOperation.
  109. /// \notes Normalize the input image with respect to mean and standard deviation.
  110. /// \param[in] mean A vector of mean values for each channel, w.r.t channel order.
  111. /// \param[in] std A vector of standard deviations for each channel, w.r.t. channel order.
  112. /// \return Shared pointer to the current TensorOperation.
  113. std::shared_ptr<NormalizeOperation> Normalize(std::vector<float> mean, std::vector<float> std);
  114. /// \brief Function to create a OneHot TensorOperation.
  115. /// \notes Convert the labels into OneHot format.
  116. /// \param[in] num_classes number of classes.
  117. /// \return Shared pointer to the current TensorOperation.
  118. std::shared_ptr<OneHotOperation> OneHot(int32_t num_classes);
  119. /// \brief Function to create a Pad TensorOp
  120. /// \notes Pads the image according to padding parameters
  121. /// \param[in] padding A vector representing the number of pixels to pad the image
  122. /// If vector has one value, it pads all sides of the image with that value
  123. /// If vector has two values, it pads left and right with the first and
  124. /// top and bottom with the second value
  125. /// If vector has four values, it pads left, top, right, and bottom with
  126. /// those values respectively
  127. /// \param[in] fill_value A vector representing the pixel intensity of the borders if the padding_mode is
  128. /// BorderType.kConstant. If 3 values are provided,
  129. /// it is used to fill R, G, B channels respectively
  130. /// \param[in] padding_mode The method of padding (default=BorderType.kConstant)
  131. /// Can be any of
  132. /// [BorderType.kConstant, BorderType.kEdge, BorderType.kReflect, BorderType.kSymmetric]
  133. /// - BorderType.kConstant, means it fills the border with constant values
  134. /// - BorderType.kEdge, means it pads with the last value on the edge
  135. /// - BorderType.kReflect, means it reflects the values on the edge omitting the last value of edge
  136. /// - BorderType.kSymmetric, means it reflects the values on the edge repeating the last value of edge
  137. /// \return Shared pointer to the current TensorOp
  138. std::shared_ptr<PadOperation> Pad(std::vector<int32_t> padding, std::vector<uint8_t> fill_value = {0},
  139. BorderType padding_mode = BorderType::kConstant);
  140. /// \brief Function to create a RandomAffine TensorOperation.
  141. /// \notes Applies a Random Affine transformation on input image in RGB or Greyscale mode.
  142. /// \param[in] degrees A float vector size 2, representing the starting and ending degree
  143. /// \param[in] translate_range A float vector size 2, representing percentages of translation on x and y axes.
  144. /// \param[in] scale_range A float vector size 2, representing the starting and ending scales in the range.
  145. /// \param[in] shear_ranges A float vector size 4, representing the starting and ending shear degrees vertically and
  146. /// horizontally.
  147. /// \param[in] interpolation An enum for the mode of interpolation
  148. /// \param[in] fill_value A uint8_t vector size 3, representing the pixel intensity of the borders, it is used to
  149. /// fill R, G, B channels respectively.
  150. /// \return Shared pointer to the current TensorOperation.
  151. std::shared_ptr<RandomAffineOperation> RandomAffine(
  152. const std::vector<float_t> &degrees, const std::vector<float_t> &translate_range = {0.0, 0.0},
  153. const std::vector<float_t> &scale_range = {1.0, 1.0}, const std::vector<float_t> &shear_ranges = {0.0, 0.0, 0.0, 0.0},
  154. InterpolationMode interpolation = InterpolationMode::kNearestNeighbour,
  155. const std::vector<uint8_t> &fill_value = {0, 0, 0});
  156. /// \brief Blends an image with its grayscale version with random weights
  157. /// t and 1 - t generated from a given range. If the range is trivial
  158. /// then the weights are determinate and t equals the bound of the interval
  159. /// \param[in] t_lb lower bound on the range of random weights
  160. /// \param[in] t_lb upper bound on the range of random weights
  161. /// \return Shared pointer to the current TensorOp
  162. std::shared_ptr<RandomColorOperation> RandomColor(float t_lb, float t_ub);
  163. /// \brief Randomly adjust the brightness, contrast, saturation, and hue of the input image
  164. /// \param[in] brightness Brightness adjustment factor. Must be a vector of one or two values
  165. /// if it's a vector of two values it needs to be in the form of [min, max]. Default value is {1, 1}
  166. /// \param[in] contrast Contrast adjustment factor. Must be a vector of one or two values
  167. /// if it's a vector of two values it needs to be in the form of [min, max]. Default value is {1, 1}
  168. /// \param[in] saturation Saturation adjustment factor. Must be a vector of one or two values
  169. /// if it's a vector of two values it needs to be in the form of [min, max]. Default value is {1, 1}
  170. /// \param[in] hue Brightness adjustment factor. Must be a vector of one or two values
  171. /// if it's a vector of two values it must be in the form of [min, max] where -0.5 <= min <= max <= 0.5
  172. /// Default value is {0, 0}
  173. /// \return Shared pointer to the current TensorOp
  174. std::shared_ptr<RandomColorAdjustOperation> RandomColorAdjust(std::vector<float> brightness = {1.0, 1.0},
  175. std::vector<float> contrast = {1.0, 1.0},
  176. std::vector<float> saturation = {1.0, 1.0},
  177. std::vector<float> hue = {0.0, 0.0});
  178. /// \brief Function to create a RandomCrop TensorOperation.
  179. /// \notes Crop the input image at a random location.
  180. /// \param[in] size - a vector representing the output size of the cropped image.
  181. /// If size is a single value, a square crop of size (size, size) is returned.
  182. /// If size has 2 values, it should be (height, width).
  183. /// \param[in] padding - a vector with the value of pixels to pad the image. If 4 values are provided,
  184. /// it pads the left, top, right and bottom respectively.
  185. /// \param[in] pad_if_needed - a boolean whether to pad the image if either side is smaller than
  186. /// the given output size.
  187. /// \param[in] fill_value - a vector representing the pixel intensity of the borders, it is used to
  188. /// fill R, G, B channels respectively.
  189. /// \return Shared pointer to the current TensorOperation.
  190. std::shared_ptr<RandomCropOperation> RandomCrop(std::vector<int32_t> size, std::vector<int32_t> padding = {0, 0, 0, 0},
  191. bool pad_if_needed = false, std::vector<uint8_t> fill_value = {0, 0, 0},
  192. BorderType padding_mode = BorderType::kConstant);
  193. /// \brief Function to create a RandomHorizontalFlip TensorOperation.
  194. /// \notes Tensor operation to perform random horizontal flip.
  195. /// \param[in] prob - float representing the probability of flip.
  196. /// \return Shared pointer to the current TensorOperation.
  197. std::shared_ptr<RandomHorizontalFlipOperation> RandomHorizontalFlip(float prob = 0.5);
  198. /// \brief Function to create a RandomPosterize TensorOperation.
  199. /// \notes Tensor operation to perform random posterize.
  200. /// \param[in] min_bit - uint8_t representing the minimum bit in range. (Default=8)
  201. /// \param[in] max_bit - uint8_t representing the maximum bit in range. (Default=8)
  202. /// \return Shared pointer to the current TensorOperation.
  203. std::shared_ptr<RandomPosterizeOperation> RandomPosterize(uint8_t min_bit = 8, uint8_t max_bit = 8);
  204. /// \brief Function to create a RandomRotation TensorOp
  205. /// \notes Rotates the image according to parameters
  206. /// \param[in] degrees A float vector size 2, representing the starting and ending degree
  207. /// \param[in] resample An enum for the mode of interpolation
  208. /// \param[in] expand A boolean representing whether the image is expanded after rotation
  209. /// \param[in] center A float vector size 2, representing the x and y center of rotation.
  210. /// \param[in] fill_value A uint8_t vector size 3, representing the rgb value of the fill color
  211. /// \return Shared pointer to the current TensorOp
  212. std::shared_ptr<RandomRotationOperation> RandomRotation(
  213. std::vector<float> degrees, InterpolationMode resample = InterpolationMode::kNearestNeighbour, bool expand = false,
  214. std::vector<float> center = {-1, -1}, std::vector<uint8_t> fill_value = {0, 0, 0});
  215. /// \brief Function to create a RandomSharpness TensorOperation.
  216. /// \notes Tensor operation to perform random sharpness.
  217. /// \param[in] start_degree - float representing the start of the range to uniformly sample the factor from it.
  218. /// \param[in] end_degree - float representing the end of the range.
  219. /// \return Shared pointer to the current TensorOperation.
  220. std::shared_ptr<RandomSharpnessOperation> RandomSharpness(std::vector<float> degrees = {0.1, 1.9});
  221. /// \brief Function to create a RandomSolarize TensorOperation.
  222. /// \notes Invert pixels within specified range. If min=max, then it inverts all pixel above that threshold
  223. /// \param[in] threshold - a vector with two elements specifying the pixel range to invert.
  224. /// \return Shared pointer to the current TensorOperation.
  225. std::shared_ptr<RandomSolarizeOperation> RandomSolarize(std::vector<uint8_t> threshold = {0, 255});
  226. /// \brief Function to create a RandomVerticalFlip TensorOperation.
  227. /// \notes Tensor operation to perform random vertical flip.
  228. /// \param[in] prob - float representing the probability of flip.
  229. /// \return Shared pointer to the current TensorOperation.
  230. std::shared_ptr<RandomVerticalFlipOperation> RandomVerticalFlip(float prob = 0.5);
  231. /// \brief Function to create a RgbaToBgr TensorOperation.
  232. /// \notes Changes the input 4 channel RGBA tensor to 3 channel BGR.
  233. /// \return Shared pointer to the current TensorOperation.
  234. std::shared_ptr<RgbaToBgrOperation> RGBA2BGR();
  235. /// \brief Function to create a RgbaToRgb TensorOperation.
  236. /// \notes Changes the input 4 channel RGBA tensor to 3 channel RGB.
  237. /// \return Shared pointer to the current TensorOperation.
  238. std::shared_ptr<RgbaToRgbOperation> RGBA2RGB();
  239. /// \brief Function to create a Resize TensorOperation.
  240. /// \notes Resize the input image to the given size.
  241. /// \param[in] size - a vector representing the output size of the resized image.
  242. /// If size is a single value, the image will be resized to this value with
  243. /// the same image aspect ratio. If size has 2 values, it should be (height, width).
  244. /// \param[in] interpolation An enum for the mode of interpolation
  245. /// \return Shared pointer to the current TensorOperation.
  246. std::shared_ptr<ResizeOperation> Resize(std::vector<int32_t> size,
  247. InterpolationMode interpolation = InterpolationMode::kLinear);
  248. /// \brief Function to create a SwapRedBlue TensorOp
  249. /// \notes Swaps the red and blue channels in image
  250. /// \return Shared pointer to the current TensorOp
  251. std::shared_ptr<SwapRedBlueOperation> SwapRedBlue();
  252. /// \brief Function to create a UniformAugment TensorOperation.
  253. /// \notes Tensor operation to perform randomly selected augmentation.
  254. /// \param[in] transforms - a vector of TensorOperation transforms.
  255. /// \param[in] num_ops - integer representing the number of OPs to be selected and applied.
  256. /// \return Shared pointer to the current TensorOperation.
  257. std::shared_ptr<UniformAugOperation> UniformAugment(std::vector<std::shared_ptr<TensorOperation>> transforms,
  258. int32_t num_ops = 2);
  259. /* ####################################### Derived TensorOperation classes ################################# */
  260. class CenterCropOperation : public TensorOperation {
  261. public:
  262. explicit CenterCropOperation(std::vector<int32_t> size);
  263. ~CenterCropOperation() = default;
  264. std::shared_ptr<TensorOp> Build() override;
  265. bool ValidateParams() override;
  266. private:
  267. std::vector<int32_t> size_;
  268. };
  269. class CropOperation : public TensorOperation {
  270. public:
  271. CropOperation(std::vector<int32_t> coordinates, std::vector<int32_t> size);
  272. ~CropOperation() = default;
  273. std::shared_ptr<TensorOp> Build() override;
  274. bool ValidateParams() override;
  275. private:
  276. std::vector<int32_t> coordinates_;
  277. std::vector<int32_t> size_;
  278. };
  279. class CutMixBatchOperation : public TensorOperation {
  280. public:
  281. explicit CutMixBatchOperation(ImageBatchFormat image_batch_format, float alpha = 1.0, float prob = 1.0);
  282. ~CutMixBatchOperation() = default;
  283. std::shared_ptr<TensorOp> Build() override;
  284. bool ValidateParams() override;
  285. private:
  286. float alpha_;
  287. float prob_;
  288. ImageBatchFormat image_batch_format_;
  289. };
  290. class CutOutOperation : public TensorOperation {
  291. public:
  292. explicit CutOutOperation(int32_t length, int32_t num_patches = 1);
  293. ~CutOutOperation() = default;
  294. std::shared_ptr<TensorOp> Build() override;
  295. bool ValidateParams() override;
  296. private:
  297. int32_t length_;
  298. int32_t num_patches_;
  299. ImageBatchFormat image_batch_format_;
  300. };
  301. class DecodeOperation : public TensorOperation {
  302. public:
  303. explicit DecodeOperation(bool rgb = true);
  304. ~DecodeOperation() = default;
  305. std::shared_ptr<TensorOp> Build() override;
  306. bool ValidateParams() override;
  307. private:
  308. bool rgb_;
  309. };
  310. class HwcToChwOperation : public TensorOperation {
  311. public:
  312. ~HwcToChwOperation() = default;
  313. std::shared_ptr<TensorOp> Build() override;
  314. bool ValidateParams() override;
  315. };
  316. class MixUpBatchOperation : public TensorOperation {
  317. public:
  318. explicit MixUpBatchOperation(float alpha = 1);
  319. ~MixUpBatchOperation() = default;
  320. std::shared_ptr<TensorOp> Build() override;
  321. bool ValidateParams() override;
  322. private:
  323. float alpha_;
  324. };
  325. class NormalizeOperation : public TensorOperation {
  326. public:
  327. NormalizeOperation(std::vector<float> mean, std::vector<float> std);
  328. ~NormalizeOperation() = default;
  329. std::shared_ptr<TensorOp> Build() override;
  330. bool ValidateParams() override;
  331. private:
  332. std::vector<float> mean_;
  333. std::vector<float> std_;
  334. };
  335. class OneHotOperation : public TensorOperation {
  336. public:
  337. explicit OneHotOperation(int32_t num_classes_);
  338. ~OneHotOperation() = default;
  339. std::shared_ptr<TensorOp> Build() override;
  340. bool ValidateParams() override;
  341. private:
  342. float num_classes_;
  343. };
  344. class PadOperation : public TensorOperation {
  345. public:
  346. PadOperation(std::vector<int32_t> padding, std::vector<uint8_t> fill_value = {0},
  347. BorderType padding_mode = BorderType::kConstant);
  348. ~PadOperation() = default;
  349. std::shared_ptr<TensorOp> Build() override;
  350. bool ValidateParams() override;
  351. private:
  352. std::vector<int32_t> padding_;
  353. std::vector<uint8_t> fill_value_;
  354. BorderType padding_mode_;
  355. };
  356. class RandomAffineOperation : public TensorOperation {
  357. public:
  358. RandomAffineOperation(const std::vector<float_t> &degrees, const std::vector<float_t> &translate_range = {0.0, 0.0},
  359. const std::vector<float_t> &scale_range = {1.0, 1.0},
  360. const std::vector<float_t> &shear_ranges = {0.0, 0.0, 0.0, 0.0},
  361. InterpolationMode interpolation = InterpolationMode::kNearestNeighbour,
  362. const std::vector<uint8_t> &fill_value = {0, 0, 0});
  363. ~RandomAffineOperation() = default;
  364. std::shared_ptr<TensorOp> Build() override;
  365. bool ValidateParams() override;
  366. private:
  367. std::vector<float_t> degrees_; // min_degree, max_degree
  368. std::vector<float_t> translate_range_; // maximum x translation percentage, maximum y translation percentage
  369. std::vector<float_t> scale_range_; // min_scale, max_scale
  370. std::vector<float_t> shear_ranges_; // min_x_shear, max_x_shear, min_y_shear, max_y_shear
  371. InterpolationMode interpolation_;
  372. std::vector<uint8_t> fill_value_;
  373. };
  374. class RandomColorOperation : public TensorOperation {
  375. public:
  376. RandomColorOperation(float t_lb, float t_ub);
  377. ~RandomColorOperation() = default;
  378. std::shared_ptr<TensorOp> Build() override;
  379. bool ValidateParams() override;
  380. private:
  381. float t_lb_;
  382. float t_ub_;
  383. };
  384. class RandomColorAdjustOperation : public TensorOperation {
  385. public:
  386. RandomColorAdjustOperation(std::vector<float> brightness = {1.0, 1.0}, std::vector<float> contrast = {1.0, 1.0},
  387. std::vector<float> saturation = {1.0, 1.0}, std::vector<float> hue = {0.0, 0.0});
  388. ~RandomColorAdjustOperation() = default;
  389. std::shared_ptr<TensorOp> Build() override;
  390. bool ValidateParams() override;
  391. private:
  392. std::vector<float> brightness_;
  393. std::vector<float> contrast_;
  394. std::vector<float> saturation_;
  395. std::vector<float> hue_;
  396. };
  397. class RandomCropOperation : public TensorOperation {
  398. public:
  399. RandomCropOperation(std::vector<int32_t> size, std::vector<int32_t> padding = {0, 0, 0, 0},
  400. bool pad_if_needed = false, std::vector<uint8_t> fill_value = {0, 0, 0},
  401. BorderType padding_mode = BorderType::kConstant);
  402. ~RandomCropOperation() = default;
  403. std::shared_ptr<TensorOp> Build() override;
  404. bool ValidateParams() override;
  405. private:
  406. std::vector<int32_t> size_;
  407. std::vector<int32_t> padding_;
  408. bool pad_if_needed_;
  409. std::vector<uint8_t> fill_value_;
  410. BorderType padding_mode_;
  411. };
  412. class RandomHorizontalFlipOperation : public TensorOperation {
  413. public:
  414. explicit RandomHorizontalFlipOperation(float probability = 0.5);
  415. ~RandomHorizontalFlipOperation() = default;
  416. std::shared_ptr<TensorOp> Build() override;
  417. bool ValidateParams() override;
  418. private:
  419. float probability_;
  420. };
  421. class RandomPosterizeOperation : public TensorOperation {
  422. public:
  423. explicit RandomPosterizeOperation(uint8_t min_bit = 8, uint8_t max_bit = 8);
  424. ~RandomPosterizeOperation() = default;
  425. std::shared_ptr<TensorOp> Build() override;
  426. bool ValidateParams() override;
  427. private:
  428. uint8_t min_bit_;
  429. uint8_t max_bit_;
  430. };
  431. class RandomRotationOperation : public TensorOperation {
  432. public:
  433. RandomRotationOperation(std::vector<float> degrees, InterpolationMode interpolation_mode, bool expand,
  434. std::vector<float> center, std::vector<uint8_t> fill_value);
  435. ~RandomRotationOperation() = default;
  436. std::shared_ptr<TensorOp> Build() override;
  437. bool ValidateParams() override;
  438. private:
  439. std::vector<float> degrees_;
  440. InterpolationMode interpolation_mode_;
  441. std::vector<float> center_;
  442. bool expand_;
  443. std::vector<uint8_t> fill_value_;
  444. };
  445. class RandomSharpnessOperation : public TensorOperation {
  446. public:
  447. explicit RandomSharpnessOperation(std::vector<float> degrees = {0.1, 1.9});
  448. ~RandomSharpnessOperation() = default;
  449. std::shared_ptr<TensorOp> Build() override;
  450. bool ValidateParams() override;
  451. private:
  452. std::vector<float> degrees_;
  453. };
  454. class RandomVerticalFlipOperation : public TensorOperation {
  455. public:
  456. explicit RandomVerticalFlipOperation(float probability = 0.5);
  457. ~RandomVerticalFlipOperation() = default;
  458. std::shared_ptr<TensorOp> Build() override;
  459. bool ValidateParams() override;
  460. private:
  461. float probability_;
  462. };
  463. class ResizeOperation : public TensorOperation {
  464. public:
  465. explicit ResizeOperation(std::vector<int32_t> size,
  466. InterpolationMode interpolation_mode = InterpolationMode::kLinear);
  467. ~ResizeOperation() = default;
  468. std::shared_ptr<TensorOp> Build() override;
  469. bool ValidateParams() override;
  470. private:
  471. std::vector<int32_t> size_;
  472. InterpolationMode interpolation_;
  473. };
  474. class RgbaToBgrOperation : public TensorOperation {
  475. public:
  476. RgbaToBgrOperation();
  477. ~RgbaToBgrOperation() = default;
  478. std::shared_ptr<TensorOp> Build() override;
  479. bool ValidateParams() override;
  480. };
  481. class RgbaToRgbOperation : public TensorOperation {
  482. public:
  483. RgbaToRgbOperation();
  484. ~RgbaToRgbOperation() = default;
  485. std::shared_ptr<TensorOp> Build() override;
  486. bool ValidateParams() override;
  487. };
  488. class UniformAugOperation : public TensorOperation {
  489. public:
  490. explicit UniformAugOperation(std::vector<std::shared_ptr<TensorOperation>> transforms, int32_t num_ops = 2);
  491. ~UniformAugOperation() = default;
  492. std::shared_ptr<TensorOp> Build() override;
  493. bool ValidateParams() override;
  494. private:
  495. std::vector<std::shared_ptr<TensorOperation>> transforms_;
  496. int32_t num_ops_;
  497. };
  498. class SwapRedBlueOperation : public TensorOperation {
  499. public:
  500. SwapRedBlueOperation();
  501. ~SwapRedBlueOperation() = default;
  502. std::shared_ptr<TensorOp> Build() override;
  503. bool ValidateParams() override;
  504. };
  505. class RandomSolarizeOperation : public TensorOperation {
  506. public:
  507. explicit RandomSolarizeOperation(std::vector<uint8_t> threshold);
  508. ~RandomSolarizeOperation() = default;
  509. std::shared_ptr<TensorOp> Build() override;
  510. bool ValidateParams() override;
  511. private:
  512. std::vector<uint8_t> threshold_;
  513. };
  514. } // namespace vision
  515. } // namespace api
  516. } // namespace dataset
  517. } // namespace mindspore
  518. #endif // MINDSPORE_CCSRC_MINDDATA_DATASET_API_TRANSFORMS_H_