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.cc 38 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908
  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. #include "minddata/dataset/include/dataset/vision.h"
  17. #ifdef ENABLE_ACL
  18. #include "minddata/dataset/include/dataset/vision_ascend.h"
  19. #include "minddata/dataset/kernels/ir/vision/ascend_vision_ir.h"
  20. #endif
  21. #include "minddata/dataset/include/dataset/transforms.h"
  22. #include "minddata/dataset/kernels/ir/vision/affine_ir.h"
  23. #include "minddata/dataset/kernels/ir/vision/auto_contrast_ir.h"
  24. #include "minddata/dataset/kernels/ir/vision/bounding_box_augment_ir.h"
  25. #include "minddata/dataset/kernels/ir/vision/center_crop_ir.h"
  26. #include "minddata/dataset/kernels/ir/vision/crop_ir.h"
  27. #include "minddata/dataset/kernels/ir/vision/cutmix_batch_ir.h"
  28. #include "minddata/dataset/kernels/ir/vision/cutout_ir.h"
  29. #include "minddata/dataset/kernels/ir/vision/decode_ir.h"
  30. #include "minddata/dataset/kernels/ir/vision/equalize_ir.h"
  31. #include "minddata/dataset/kernels/ir/vision/hwc_to_chw_ir.h"
  32. #include "minddata/dataset/kernels/ir/vision/invert_ir.h"
  33. #include "minddata/dataset/kernels/ir/vision/mixup_batch_ir.h"
  34. #include "minddata/dataset/kernels/ir/vision/normalize_ir.h"
  35. #include "minddata/dataset/kernels/ir/vision/normalize_pad_ir.h"
  36. #include "minddata/dataset/kernels/ir/vision/pad_ir.h"
  37. #include "minddata/dataset/kernels/ir/vision/random_affine_ir.h"
  38. #include "minddata/dataset/kernels/ir/vision/random_color_adjust_ir.h"
  39. #include "minddata/dataset/kernels/ir/vision/random_color_ir.h"
  40. #include "minddata/dataset/kernels/ir/vision/random_crop_decode_resize_ir.h"
  41. #include "minddata/dataset/kernels/ir/vision/random_crop_ir.h"
  42. #include "minddata/dataset/kernels/ir/vision/random_crop_with_bbox_ir.h"
  43. #include "minddata/dataset/kernels/ir/vision/random_horizontal_flip_ir.h"
  44. #include "minddata/dataset/kernels/ir/vision/random_horizontal_flip_with_bbox_ir.h"
  45. #include "minddata/dataset/kernels/ir/vision/random_posterize_ir.h"
  46. #include "minddata/dataset/kernels/ir/vision/random_resized_crop_ir.h"
  47. #include "minddata/dataset/kernels/ir/vision/random_resized_crop_with_bbox_ir.h"
  48. #include "minddata/dataset/kernels/ir/vision/random_resize_ir.h"
  49. #include "minddata/dataset/kernels/ir/vision/random_resize_with_bbox_ir.h"
  50. #include "minddata/dataset/kernels/ir/vision/random_rotation_ir.h"
  51. #include "minddata/dataset/kernels/ir/vision/random_select_subpolicy_ir.h"
  52. #include "minddata/dataset/kernels/ir/vision/random_sharpness_ir.h"
  53. #include "minddata/dataset/kernels/ir/vision/random_solarize_ir.h"
  54. #include "minddata/dataset/kernels/ir/vision/random_vertical_flip_ir.h"
  55. #include "minddata/dataset/kernels/ir/vision/random_vertical_flip_with_bbox_ir.h"
  56. #include "minddata/dataset/kernels/ir/vision/rescale_ir.h"
  57. #include "minddata/dataset/kernels/ir/vision/resize_ir.h"
  58. #include "minddata/dataset/kernels/ir/vision/resize_preserve_ar_ir.h"
  59. #include "minddata/dataset/kernels/ir/vision/resize_with_bbox_ir.h"
  60. #include "minddata/dataset/kernels/ir/vision/rgba_to_bgr_ir.h"
  61. #include "minddata/dataset/kernels/ir/vision/rgba_to_rgb_ir.h"
  62. #include "minddata/dataset/kernels/ir/vision/rgb_to_gray_ir.h"
  63. #include "minddata/dataset/kernels/ir/vision/rotate_ir.h"
  64. #include "minddata/dataset/kernels/ir/vision/softdvpp_decode_random_crop_resize_jpeg_ir.h"
  65. #include "minddata/dataset/kernels/ir/vision/softdvpp_decode_resize_jpeg_ir.h"
  66. #include "minddata/dataset/kernels/ir/vision/swap_red_blue_ir.h"
  67. #include "minddata/dataset/kernels/ir/vision/uniform_aug_ir.h"
  68. #ifndef ENABLE_ANDROID
  69. #include "utils/log_adapter.h"
  70. #else
  71. #include "mindspore/lite/src/common/log_adapter.h"
  72. #endif
  73. #include "minddata/dataset/kernels/ir/validators.h"
  74. // Kernel image headers (in alphabetical order)
  75. namespace mindspore {
  76. namespace dataset {
  77. // Transform operations for computer vision.
  78. namespace vision {
  79. // CONSTRUCTORS FOR API CLASSES TO CREATE VISION TENSOR TRANSFORM OPERATIONS
  80. // (In alphabetical order)
  81. // Affine Transform Operation.
  82. struct Affine::Data {
  83. Data(float_t degrees, const std::vector<float> &translation, float scale, const std::vector<float> &shear,
  84. InterpolationMode interpolation, const std::vector<uint8_t> &fill_value)
  85. : degrees_(degrees),
  86. translation_(translation),
  87. scale_(scale),
  88. shear_(shear),
  89. interpolation_(interpolation),
  90. fill_value_(fill_value) {}
  91. float degrees_;
  92. std::vector<float> translation_;
  93. float scale_;
  94. std::vector<float> shear_;
  95. InterpolationMode interpolation_;
  96. std::vector<uint8_t> fill_value_;
  97. };
  98. Affine::Affine(float_t degrees, const std::vector<float> &translation, float scale, const std::vector<float> &shear,
  99. InterpolationMode interpolation, const std::vector<uint8_t> &fill_value)
  100. : data_(std::make_shared<Data>(degrees, translation, scale, shear, interpolation, fill_value)) {}
  101. std::shared_ptr<TensorOperation> Affine::Parse() {
  102. return std::make_shared<AffineOperation>(data_->degrees_, data_->translation_, data_->scale_, data_->shear_,
  103. data_->interpolation_, data_->fill_value_);
  104. }
  105. #ifndef ENABLE_ANDROID
  106. // AutoContrast Transform Operation.
  107. struct AutoContrast::Data {
  108. Data(float cutoff, const std::vector<uint32_t> &ignore) : cutoff_(cutoff), ignore_(ignore) {}
  109. float cutoff_;
  110. std::vector<uint32_t> ignore_;
  111. };
  112. AutoContrast::AutoContrast(float cutoff, std::vector<uint32_t> ignore)
  113. : data_(std::make_shared<Data>(cutoff, ignore)) {}
  114. std::shared_ptr<TensorOperation> AutoContrast::Parse() {
  115. return std::make_shared<AutoContrastOperation>(data_->cutoff_, data_->ignore_);
  116. }
  117. // BoundingBoxAugment Transform Operation.
  118. struct BoundingBoxAugment::Data {
  119. std::shared_ptr<TensorOperation> transform_;
  120. float ratio_;
  121. };
  122. BoundingBoxAugment::BoundingBoxAugment(TensorTransform *transform, float ratio) : data_(std::make_shared<Data>()) {
  123. data_->transform_ = transform ? transform->Parse() : nullptr;
  124. data_->ratio_ = ratio;
  125. }
  126. BoundingBoxAugment::BoundingBoxAugment(const std::shared_ptr<TensorTransform> &transform, float ratio)
  127. : data_(std::make_shared<Data>()) {
  128. data_->transform_ = transform ? transform->Parse() : nullptr;
  129. data_->ratio_ = ratio;
  130. }
  131. BoundingBoxAugment::BoundingBoxAugment(const std::reference_wrapper<TensorTransform> transform, float ratio)
  132. : data_(std::make_shared<Data>()) {
  133. data_->transform_ = transform.get().Parse();
  134. data_->ratio_ = ratio;
  135. }
  136. std::shared_ptr<TensorOperation> BoundingBoxAugment::Parse() {
  137. return std::make_shared<BoundingBoxAugmentOperation>(data_->transform_, data_->ratio_);
  138. }
  139. #endif // not ENABLE_ANDROID
  140. // CenterCrop Transform Operation.
  141. struct CenterCrop::Data {
  142. explicit Data(const std::vector<int32_t> &size) : size_(size) {}
  143. std::vector<int32_t> size_;
  144. };
  145. CenterCrop::CenterCrop(std::vector<int32_t> size) : data_(std::make_shared<Data>(size)) {}
  146. std::shared_ptr<TensorOperation> CenterCrop::Parse() { return std::make_shared<CenterCropOperation>(data_->size_); }
  147. std::shared_ptr<TensorOperation> CenterCrop::Parse(const MapTargetDevice &env) {
  148. if (env == MapTargetDevice::kAscend310) {
  149. #ifdef ENABLE_ACL
  150. std::vector<uint32_t> usize_;
  151. usize_.reserve(data_->size_.size());
  152. std::transform(data_->size_.begin(), data_->size_.end(), std::back_inserter(usize_),
  153. [](int32_t i) { return (uint32_t)i; });
  154. return std::make_shared<DvppCropJpegOperation>(usize_);
  155. #endif // ENABLE_ACL
  156. }
  157. return std::make_shared<CenterCropOperation>(data_->size_);
  158. }
  159. // RGB2GRAY Transform Operation.
  160. std::shared_ptr<TensorOperation> RGB2GRAY::Parse() { return std::make_shared<RgbToGrayOperation>(); }
  161. // Crop Transform Operation.
  162. struct Crop::Data {
  163. Data(const std::vector<int32_t> &coordinates, const std::vector<int32_t> &size)
  164. : coordinates_(coordinates), size_(size) {}
  165. std::vector<int32_t> coordinates_;
  166. std::vector<int32_t> size_;
  167. };
  168. Crop::Crop(std::vector<int32_t> coordinates, std::vector<int32_t> size)
  169. : data_(std::make_shared<Data>(coordinates, size)) {}
  170. std::shared_ptr<TensorOperation> Crop::Parse() {
  171. return std::make_shared<CropOperation>(data_->coordinates_, data_->size_);
  172. }
  173. #ifndef ENABLE_ANDROID
  174. // CutMixBatch Transform Operation.
  175. struct CutMixBatch::Data {
  176. Data(ImageBatchFormat image_batch_format, float alpha, float prob)
  177. : image_batch_format_(image_batch_format), alpha_(alpha), prob_(prob) {}
  178. float alpha_;
  179. float prob_;
  180. ImageBatchFormat image_batch_format_;
  181. };
  182. CutMixBatch::CutMixBatch(ImageBatchFormat image_batch_format, float alpha, float prob)
  183. : data_(std::make_shared<Data>(image_batch_format, alpha, prob)) {}
  184. std::shared_ptr<TensorOperation> CutMixBatch::Parse() {
  185. return std::make_shared<CutMixBatchOperation>(data_->image_batch_format_, data_->alpha_, data_->prob_);
  186. }
  187. // CutOutOp.
  188. struct CutOut::Data {
  189. Data(int32_t length, int32_t num_patches) : length_(length), num_patches_(num_patches) {}
  190. int32_t length_;
  191. int32_t num_patches_;
  192. };
  193. CutOut::CutOut(int32_t length, int32_t num_patches) : data_(std::make_shared<Data>(length, num_patches)) {}
  194. std::shared_ptr<TensorOperation> CutOut::Parse() {
  195. return std::make_shared<CutOutOperation>(data_->length_, data_->num_patches_);
  196. }
  197. #endif // not ENABLE_ANDROID
  198. // Decode Transform Operation.
  199. struct Decode::Data {
  200. explicit Data(bool rgb) : rgb_(rgb) {}
  201. bool rgb_;
  202. };
  203. Decode::Decode(bool rgb) : data_(std::make_shared<Data>(rgb)) {}
  204. std::shared_ptr<TensorOperation> Decode::Parse() { return std::make_shared<DecodeOperation>(data_->rgb_); }
  205. std::shared_ptr<TensorOperation> Decode::Parse(const MapTargetDevice &env) {
  206. if (env == MapTargetDevice::kAscend310) {
  207. #ifdef ENABLE_ACL
  208. return std::make_shared<DvppDecodeJpegOperation>();
  209. #endif // ENABLE_ACL
  210. }
  211. return std::make_shared<DecodeOperation>(data_->rgb_);
  212. }
  213. #ifdef ENABLE_ACL
  214. // DvppDecodeResize Transform Operation.
  215. struct DvppDecodeResizeJpeg::Data {
  216. explicit Data(const std::vector<uint32_t> &resize) : resize_(resize) {}
  217. std::vector<uint32_t> resize_;
  218. };
  219. DvppDecodeResizeJpeg::DvppDecodeResizeJpeg(std::vector<uint32_t> resize) : data_(std::make_shared<Data>(resize)) {}
  220. std::shared_ptr<TensorOperation> DvppDecodeResizeJpeg::Parse() {
  221. return std::make_shared<DvppDecodeResizeOperation>(data_->resize_);
  222. }
  223. std::shared_ptr<TensorOperation> DvppDecodeResizeJpeg::Parse(const MapTargetDevice &env) {
  224. return std::make_shared<DvppDecodeResizeOperation>(data_->resize_);
  225. }
  226. // DvppDecodeResizeCrop Transform Operation.
  227. struct DvppDecodeResizeCropJpeg::Data {
  228. Data(const std::vector<uint32_t> &crop, const std::vector<uint32_t> &resize) : crop_(crop), resize_(resize) {}
  229. std::vector<uint32_t> crop_;
  230. std::vector<uint32_t> resize_;
  231. };
  232. DvppDecodeResizeCropJpeg::DvppDecodeResizeCropJpeg(std::vector<uint32_t> crop, std::vector<uint32_t> resize)
  233. : data_(std::make_shared<Data>(crop, resize)) {}
  234. std::shared_ptr<TensorOperation> DvppDecodeResizeCropJpeg::Parse() {
  235. return std::make_shared<DvppDecodeResizeCropOperation>(data_->crop_, data_->resize_);
  236. }
  237. std::shared_ptr<TensorOperation> DvppDecodeResizeCropJpeg::Parse(const MapTargetDevice &env) {
  238. return std::make_shared<DvppDecodeResizeCropOperation>(data_->crop_, data_->resize_);
  239. }
  240. // DvppDecodePng Transform Operation.
  241. DvppDecodePng::DvppDecodePng() {}
  242. std::shared_ptr<TensorOperation> DvppDecodePng::Parse() { return std::make_shared<DvppDecodePngOperation>(); }
  243. std::shared_ptr<TensorOperation> DvppDecodePng::Parse(const MapTargetDevice &env) {
  244. return std::make_shared<DvppDecodePngOperation>();
  245. }
  246. #endif // ENABLE_ACL
  247. #ifndef ENABLE_ANDROID
  248. // Equalize Transform Operation.
  249. Equalize::Equalize() {}
  250. std::shared_ptr<TensorOperation> Equalize::Parse() { return std::make_shared<EqualizeOperation>(); }
  251. // HwcToChw Transform Operation.
  252. HWC2CHW::HWC2CHW() {}
  253. std::shared_ptr<TensorOperation> HWC2CHW::Parse() { return std::make_shared<HwcToChwOperation>(); }
  254. // Invert Transform Operation.
  255. Invert::Invert() {}
  256. std::shared_ptr<TensorOperation> Invert::Parse() { return std::make_shared<InvertOperation>(); }
  257. // MixUpBatch Transform Operation.
  258. struct MixUpBatch::Data {
  259. explicit Data(float alpha) : alpha_(alpha) {}
  260. float alpha_;
  261. };
  262. MixUpBatch::MixUpBatch(float alpha) : data_(std::make_shared<Data>(alpha)) {}
  263. std::shared_ptr<TensorOperation> MixUpBatch::Parse() { return std::make_shared<MixUpBatchOperation>(data_->alpha_); }
  264. #endif // not ENABLE_ANDROID
  265. // Normalize Transform Operation.
  266. struct Normalize::Data {
  267. Data(const std::vector<float> &mean, const std::vector<float> &std) : mean_(mean), std_(std) {}
  268. std::vector<float> mean_;
  269. std::vector<float> std_;
  270. };
  271. Normalize::Normalize(std::vector<float> mean, std::vector<float> std) : data_(std::make_shared<Data>(mean, std)) {}
  272. std::shared_ptr<TensorOperation> Normalize::Parse() {
  273. return std::make_shared<NormalizeOperation>(data_->mean_, data_->std_);
  274. }
  275. std::shared_ptr<TensorOperation> Normalize::Parse(const MapTargetDevice &env) {
  276. if (env == MapTargetDevice::kAscend310) {
  277. #ifdef ENABLE_ACL
  278. return std::make_shared<DvppNormalizeOperation>(data_->mean_, data_->std_);
  279. #endif // ENABLE_ACL
  280. }
  281. return std::make_shared<NormalizeOperation>(data_->mean_, data_->std_);
  282. }
  283. #ifndef ENABLE_ANDROID
  284. // NormalizePad Transform Operation.
  285. struct NormalizePad::Data {
  286. Data(const std::vector<float> &mean, const std::vector<float> &std, const std::string &dtype)
  287. : mean_(mean), std_(std), dtype_(dtype) {}
  288. std::vector<float> mean_;
  289. std::vector<float> std_;
  290. std::string dtype_;
  291. };
  292. NormalizePad::NormalizePad(const std::vector<float> &mean, const std::vector<float> &std,
  293. const std::vector<char> &dtype)
  294. : data_(std::make_shared<Data>(mean, std, CharToString(dtype))) {}
  295. std::shared_ptr<TensorOperation> NormalizePad::Parse() {
  296. return std::make_shared<NormalizePadOperation>(data_->mean_, data_->std_, data_->dtype_);
  297. }
  298. // Pad Transform Operation.
  299. struct Pad::Data {
  300. Data(const std::vector<int32_t> &padding, const std::vector<uint8_t> &fill_value, BorderType padding_mode)
  301. : padding_(padding), fill_value_(fill_value), padding_mode_(padding_mode) {}
  302. std::vector<int32_t> padding_;
  303. std::vector<uint8_t> fill_value_;
  304. BorderType padding_mode_;
  305. };
  306. Pad::Pad(std::vector<int32_t> padding, std::vector<uint8_t> fill_value, BorderType padding_mode)
  307. : data_(std::make_shared<Data>(padding, fill_value, padding_mode)) {}
  308. std::shared_ptr<TensorOperation> Pad::Parse() {
  309. return std::make_shared<PadOperation>(data_->padding_, data_->fill_value_, data_->padding_mode_);
  310. }
  311. #endif // not ENABLE_ANDROID
  312. // RandomAffine Transform Operation.
  313. struct RandomAffine::Data {
  314. Data(const std::vector<float_t> &degrees, const std::vector<float_t> &translate_range,
  315. const std::vector<float_t> &scale_range, const std::vector<float_t> &shear_ranges,
  316. InterpolationMode interpolation, const std::vector<uint8_t> &fill_value)
  317. : degrees_(degrees),
  318. translate_range_(translate_range),
  319. scale_range_(scale_range),
  320. shear_ranges_(shear_ranges),
  321. interpolation_(interpolation),
  322. fill_value_(fill_value) {}
  323. std::vector<float_t> degrees_; // min_degree, max_degree
  324. std::vector<float_t> translate_range_; // maximum x translation percentage, maximum y translation percentage
  325. std::vector<float_t> scale_range_; // min_scale, max_scale
  326. std::vector<float_t> shear_ranges_; // min_x_shear, max_x_shear, min_y_shear, max_y_shear
  327. InterpolationMode interpolation_;
  328. std::vector<uint8_t> fill_value_;
  329. };
  330. RandomAffine::RandomAffine(const std::vector<float_t> &degrees, const std::vector<float_t> &translate_range,
  331. const std::vector<float_t> &scale_range, const std::vector<float_t> &shear_ranges,
  332. InterpolationMode interpolation, const std::vector<uint8_t> &fill_value)
  333. : data_(std::make_shared<Data>(degrees, translate_range, scale_range, shear_ranges, interpolation, fill_value)) {}
  334. std::shared_ptr<TensorOperation> RandomAffine::Parse() {
  335. return std::make_shared<RandomAffineOperation>(data_->degrees_, data_->translate_range_, data_->scale_range_,
  336. data_->shear_ranges_, data_->interpolation_, data_->fill_value_);
  337. }
  338. #ifndef ENABLE_ANDROID
  339. // RandomColor Transform Operation.
  340. struct RandomColor::Data {
  341. Data(float t_lb, float t_ub) : t_lb_(t_lb), t_ub_(t_ub) {}
  342. float t_lb_;
  343. float t_ub_;
  344. };
  345. RandomColor::RandomColor(float t_lb, float t_ub) : data_(std::make_shared<Data>(t_lb, t_ub)) {}
  346. std::shared_ptr<TensorOperation> RandomColor::Parse() {
  347. return std::make_shared<RandomColorOperation>(data_->t_lb_, data_->t_ub_);
  348. }
  349. // RandomColorAdjust Transform Operation.
  350. struct RandomColorAdjust::Data {
  351. Data(const std::vector<float> &brightness, const std::vector<float> &contrast, const std::vector<float> &saturation,
  352. const std::vector<float> &hue)
  353. : brightness_(brightness), contrast_(contrast), saturation_(saturation), hue_(hue) {}
  354. std::vector<float> brightness_;
  355. std::vector<float> contrast_;
  356. std::vector<float> saturation_;
  357. std::vector<float> hue_;
  358. };
  359. RandomColorAdjust::RandomColorAdjust(std::vector<float> brightness, std::vector<float> contrast,
  360. std::vector<float> saturation, std::vector<float> hue)
  361. : data_(std::make_shared<Data>(brightness, contrast, saturation, hue)) {}
  362. std::shared_ptr<TensorOperation> RandomColorAdjust::Parse() {
  363. return std::make_shared<RandomColorAdjustOperation>(data_->brightness_, data_->contrast_, data_->saturation_,
  364. data_->hue_);
  365. }
  366. // RandomCrop Transform Operation.
  367. struct RandomCrop::Data {
  368. Data(const std::vector<int32_t> &size, const std::vector<int32_t> &padding, bool pad_if_needed,
  369. const std::vector<uint8_t> &fill_value, BorderType padding_mode)
  370. : size_(size),
  371. padding_(padding),
  372. pad_if_needed_(pad_if_needed),
  373. fill_value_(fill_value),
  374. padding_mode_(padding_mode) {}
  375. std::vector<int32_t> size_;
  376. std::vector<int32_t> padding_;
  377. bool pad_if_needed_;
  378. std::vector<uint8_t> fill_value_;
  379. BorderType padding_mode_;
  380. };
  381. RandomCrop::RandomCrop(std::vector<int32_t> size, std::vector<int32_t> padding, bool pad_if_needed,
  382. std::vector<uint8_t> fill_value, BorderType padding_mode)
  383. : data_(std::make_shared<Data>(size, padding, pad_if_needed, fill_value, padding_mode)) {}
  384. std::shared_ptr<TensorOperation> RandomCrop::Parse() {
  385. return std::make_shared<RandomCropOperation>(data_->size_, data_->padding_, data_->pad_if_needed_, data_->fill_value_,
  386. data_->padding_mode_);
  387. }
  388. // RandomCropDecodeResize Transform Operation.
  389. struct RandomCropDecodeResize::Data {
  390. Data(const std::vector<int32_t> &size, const std::vector<float> &scale, const std::vector<float> &ratio,
  391. InterpolationMode interpolation, int32_t max_attempts)
  392. : size_(size), scale_(scale), ratio_(ratio), interpolation_(interpolation), max_attempts_(max_attempts) {}
  393. std::vector<int32_t> size_;
  394. std::vector<float> scale_;
  395. std::vector<float> ratio_;
  396. InterpolationMode interpolation_;
  397. int32_t max_attempts_;
  398. };
  399. RandomCropDecodeResize::RandomCropDecodeResize(std::vector<int32_t> size, std::vector<float> scale,
  400. std::vector<float> ratio, InterpolationMode interpolation,
  401. int32_t max_attempts)
  402. : data_(std::make_shared<Data>(size, scale, ratio, interpolation, max_attempts)) {}
  403. std::shared_ptr<TensorOperation> RandomCropDecodeResize::Parse() {
  404. return std::make_shared<RandomCropDecodeResizeOperation>(data_->size_, data_->scale_, data_->ratio_,
  405. data_->interpolation_, data_->max_attempts_);
  406. }
  407. // RandomCropWithBBox Transform Operation.
  408. struct RandomCropWithBBox::Data {
  409. Data(const std::vector<int32_t> &size, const std::vector<int32_t> &padding, bool pad_if_needed,
  410. const std::vector<uint8_t> &fill_value, BorderType padding_mode)
  411. : size_(size),
  412. padding_(padding),
  413. pad_if_needed_(pad_if_needed),
  414. fill_value_(fill_value),
  415. padding_mode_(padding_mode) {}
  416. std::vector<int32_t> size_;
  417. std::vector<int32_t> padding_;
  418. bool pad_if_needed_;
  419. std::vector<uint8_t> fill_value_;
  420. BorderType padding_mode_;
  421. };
  422. RandomCropWithBBox::RandomCropWithBBox(std::vector<int32_t> size, std::vector<int32_t> padding, bool pad_if_needed,
  423. std::vector<uint8_t> fill_value, BorderType padding_mode)
  424. : data_(std::make_shared<Data>(size, padding, pad_if_needed, fill_value, padding_mode)) {}
  425. std::shared_ptr<TensorOperation> RandomCropWithBBox::Parse() {
  426. return std::make_shared<RandomCropWithBBoxOperation>(data_->size_, data_->padding_, data_->pad_if_needed_,
  427. data_->fill_value_, data_->padding_mode_);
  428. }
  429. // RandomHorizontalFlip.
  430. struct RandomHorizontalFlip::Data {
  431. explicit Data(float prob) : probability_(prob) {}
  432. float probability_;
  433. };
  434. RandomHorizontalFlip::RandomHorizontalFlip(float prob) : data_(std::make_shared<Data>(prob)) {}
  435. std::shared_ptr<TensorOperation> RandomHorizontalFlip::Parse() {
  436. return std::make_shared<RandomHorizontalFlipOperation>(data_->probability_);
  437. }
  438. // RandomHorizontalFlipWithBBox
  439. struct RandomHorizontalFlipWithBBox::Data {
  440. explicit Data(float prob) : probability_(prob) {}
  441. float probability_;
  442. };
  443. RandomHorizontalFlipWithBBox::RandomHorizontalFlipWithBBox(float prob) : data_(std::make_shared<Data>(prob)) {}
  444. std::shared_ptr<TensorOperation> RandomHorizontalFlipWithBBox::Parse() {
  445. return std::make_shared<RandomHorizontalFlipWithBBoxOperation>(data_->probability_);
  446. }
  447. // RandomPosterize Transform Operation.
  448. struct RandomPosterize::Data {
  449. explicit Data(const std::vector<uint8_t> &bit_range) : bit_range_(bit_range) {}
  450. std::vector<uint8_t> bit_range_;
  451. };
  452. RandomPosterize::RandomPosterize(const std::vector<uint8_t> &bit_range) : data_(std::make_shared<Data>(bit_range)) {}
  453. std::shared_ptr<TensorOperation> RandomPosterize::Parse() {
  454. return std::make_shared<RandomPosterizeOperation>(data_->bit_range_);
  455. }
  456. // RandomResize Transform Operation.
  457. struct RandomResize::Data {
  458. explicit Data(const std::vector<int32_t> &size) : size_(size) {}
  459. std::vector<int32_t> size_;
  460. };
  461. RandomResize::RandomResize(std::vector<int32_t> size) : data_(std::make_shared<Data>(size)) {}
  462. std::shared_ptr<TensorOperation> RandomResize::Parse() { return std::make_shared<RandomResizeOperation>(data_->size_); }
  463. // RandomResizeWithBBox Transform Operation.
  464. struct RandomResizeWithBBox::Data {
  465. explicit Data(const std::vector<int32_t> &size) : size_(size) {}
  466. std::vector<int32_t> size_;
  467. };
  468. RandomResizeWithBBox::RandomResizeWithBBox(std::vector<int32_t> size) : data_(std::make_shared<Data>(size)) {}
  469. std::shared_ptr<TensorOperation> RandomResizeWithBBox::Parse() {
  470. return std::make_shared<RandomResizeWithBBoxOperation>(data_->size_);
  471. }
  472. // RandomResizedCrop Transform Operation.
  473. struct RandomResizedCrop::Data {
  474. Data(const std::vector<int32_t> &size, const std::vector<float> &scale, const std::vector<float> &ratio,
  475. InterpolationMode interpolation, int32_t max_attempts)
  476. : size_(size), scale_(scale), ratio_(ratio), interpolation_(interpolation), max_attempts_(max_attempts) {}
  477. std::vector<int32_t> size_;
  478. std::vector<float> scale_;
  479. std::vector<float> ratio_;
  480. InterpolationMode interpolation_;
  481. int32_t max_attempts_;
  482. };
  483. RandomResizedCrop::RandomResizedCrop(std::vector<int32_t> size, std::vector<float> scale, std::vector<float> ratio,
  484. InterpolationMode interpolation, int32_t max_attempts)
  485. : data_(std::make_shared<Data>(size, scale, ratio, interpolation, max_attempts)) {}
  486. std::shared_ptr<TensorOperation> RandomResizedCrop::Parse() {
  487. return std::make_shared<RandomResizedCropOperation>(data_->size_, data_->scale_, data_->ratio_, data_->interpolation_,
  488. data_->max_attempts_);
  489. }
  490. // RandomResizedCrop Transform Operation.
  491. struct RandomResizedCropWithBBox::Data {
  492. Data(const std::vector<int32_t> &size, const std::vector<float> &scale, const std::vector<float> &ratio,
  493. InterpolationMode interpolation, int32_t max_attempts)
  494. : size_(size), scale_(scale), ratio_(ratio), interpolation_(interpolation), max_attempts_(max_attempts) {}
  495. std::vector<int32_t> size_;
  496. std::vector<float> scale_;
  497. std::vector<float> ratio_;
  498. InterpolationMode interpolation_;
  499. int32_t max_attempts_;
  500. };
  501. RandomResizedCropWithBBox::RandomResizedCropWithBBox(std::vector<int32_t> size, std::vector<float> scale,
  502. std::vector<float> ratio, InterpolationMode interpolation,
  503. int32_t max_attempts)
  504. : data_(std::make_shared<Data>(size, scale, ratio, interpolation, max_attempts)) {}
  505. std::shared_ptr<TensorOperation> RandomResizedCropWithBBox::Parse() {
  506. return std::make_shared<RandomResizedCropWithBBoxOperation>(data_->size_, data_->scale_, data_->ratio_,
  507. data_->interpolation_, data_->max_attempts_);
  508. }
  509. // RandomRotation Transform Operation.
  510. struct RandomRotation::Data {
  511. Data(const std::vector<float> &degrees, InterpolationMode interpolation_mode, bool expand,
  512. const std::vector<float> &center, const std::vector<uint8_t> &fill_value)
  513. : degrees_(degrees),
  514. interpolation_mode_(interpolation_mode),
  515. expand_(expand),
  516. center_(center),
  517. fill_value_(fill_value) {}
  518. std::vector<float> degrees_;
  519. InterpolationMode interpolation_mode_;
  520. std::vector<float> center_;
  521. bool expand_;
  522. std::vector<uint8_t> fill_value_;
  523. };
  524. RandomRotation::RandomRotation(std::vector<float> degrees, InterpolationMode interpolation_mode, bool expand,
  525. std::vector<float> center, std::vector<uint8_t> fill_value)
  526. : data_(std::make_shared<Data>(degrees, interpolation_mode, expand, center, fill_value)) {}
  527. std::shared_ptr<TensorOperation> RandomRotation::Parse() {
  528. return std::make_shared<RandomRotationOperation>(data_->degrees_, data_->interpolation_mode_, data_->expand_,
  529. data_->center_, data_->fill_value_);
  530. }
  531. // RandomSelectSubpolicy Transform Operation.
  532. struct RandomSelectSubpolicy::Data {
  533. std::vector<std::vector<std::pair<std::shared_ptr<TensorOperation>, double>>> policy_;
  534. };
  535. RandomSelectSubpolicy::RandomSelectSubpolicy(
  536. const std::vector<std::vector<std::pair<TensorTransform *, double>>> &policy)
  537. : data_(std::make_shared<Data>()) {
  538. for (int32_t i = 0; i < policy.size(); i++) {
  539. std::vector<std::pair<std::shared_ptr<TensorOperation>, double>> subpolicy;
  540. for (int32_t j = 0; j < policy[i].size(); j++) {
  541. TensorTransform *op = policy[i][j].first;
  542. std::shared_ptr<TensorOperation> operation = (op ? op->Parse() : nullptr);
  543. double prob = policy[i][j].second;
  544. subpolicy.emplace_back(std::move(std::make_pair(operation, prob)));
  545. }
  546. data_->policy_.emplace_back(subpolicy);
  547. }
  548. }
  549. RandomSelectSubpolicy::RandomSelectSubpolicy(
  550. const std::vector<std::vector<std::pair<std::shared_ptr<TensorTransform>, double>>> &policy)
  551. : data_(std::make_shared<Data>()) {
  552. for (int32_t i = 0; i < policy.size(); i++) {
  553. std::vector<std::pair<std::shared_ptr<TensorOperation>, double>> subpolicy;
  554. for (int32_t j = 0; j < policy[i].size(); j++) {
  555. std::shared_ptr<TensorTransform> op = policy[i][j].first;
  556. std::shared_ptr<TensorOperation> operation = (op ? op->Parse() : nullptr);
  557. double prob = policy[i][j].second;
  558. subpolicy.emplace_back(std::move(std::make_pair(operation, prob)));
  559. }
  560. data_->policy_.emplace_back(subpolicy);
  561. }
  562. }
  563. RandomSelectSubpolicy::RandomSelectSubpolicy(
  564. const std::vector<std::vector<std::pair<std::reference_wrapper<TensorTransform>, double>>> &policy)
  565. : data_(std::make_shared<Data>()) {
  566. for (int32_t i = 0; i < policy.size(); i++) {
  567. std::vector<std::pair<std::shared_ptr<TensorOperation>, double>> subpolicy;
  568. for (int32_t j = 0; j < policy[i].size(); j++) {
  569. TensorTransform &op = policy[i][j].first;
  570. std::shared_ptr<TensorOperation> operation = op.Parse();
  571. double prob = policy[i][j].second;
  572. subpolicy.emplace_back(std::move(std::make_pair(operation, prob)));
  573. }
  574. data_->policy_.emplace_back(subpolicy);
  575. }
  576. }
  577. std::shared_ptr<TensorOperation> RandomSelectSubpolicy::Parse() {
  578. return std::make_shared<RandomSelectSubpolicyOperation>(data_->policy_);
  579. }
  580. // RandomSharpness Transform Operation.
  581. struct RandomSharpness::Data {
  582. explicit Data(const std::vector<float> &degrees) : degrees_(degrees) {}
  583. std::vector<float> degrees_;
  584. };
  585. RandomSharpness::RandomSharpness(std::vector<float> degrees) : data_(std::make_shared<Data>(degrees)) {}
  586. std::shared_ptr<TensorOperation> RandomSharpness::Parse() {
  587. return std::make_shared<RandomSharpnessOperation>(data_->degrees_);
  588. }
  589. // RandomSolarize Transform Operation.
  590. struct RandomSolarize::Data {
  591. explicit Data(const std::vector<uint8_t> &threshold) : threshold_(threshold) {}
  592. std::vector<uint8_t> threshold_;
  593. };
  594. RandomSolarize::RandomSolarize(std::vector<uint8_t> threshold) : data_(std::make_shared<Data>(threshold)) {}
  595. std::shared_ptr<TensorOperation> RandomSolarize::Parse() {
  596. return std::make_shared<RandomSolarizeOperation>(data_->threshold_);
  597. }
  598. // RandomVerticalFlip Transform Operation.
  599. struct RandomVerticalFlip::Data {
  600. explicit Data(float prob) : probability_(prob) {}
  601. float probability_;
  602. };
  603. RandomVerticalFlip::RandomVerticalFlip(float prob) : data_(std::make_shared<Data>(prob)) {}
  604. std::shared_ptr<TensorOperation> RandomVerticalFlip::Parse() {
  605. return std::make_shared<RandomVerticalFlipOperation>(data_->probability_);
  606. }
  607. // RandomVerticalFlipWithBBox Transform Operation.
  608. struct RandomVerticalFlipWithBBox::Data {
  609. explicit Data(float prob) : probability_(prob) {}
  610. float probability_;
  611. };
  612. RandomVerticalFlipWithBBox::RandomVerticalFlipWithBBox(float prob) : data_(std::make_shared<Data>(prob)) {}
  613. std::shared_ptr<TensorOperation> RandomVerticalFlipWithBBox::Parse() {
  614. return std::make_shared<RandomVerticalFlipWithBBoxOperation>(data_->probability_);
  615. }
  616. // Rescale Transform Operation.
  617. struct Rescale::Data {
  618. Data(float rescale, float shift) : rescale_(rescale), shift_(shift) {}
  619. float rescale_;
  620. float shift_;
  621. };
  622. Rescale::Rescale(float rescale, float shift) : data_(std::make_shared<Data>(rescale, shift)) {}
  623. std::shared_ptr<TensorOperation> Rescale::Parse() {
  624. return std::make_shared<RescaleOperation>(data_->rescale_, data_->shift_);
  625. }
  626. #endif // not ENABLE_ANDROID
  627. // Resize Transform Operation.
  628. struct Resize::Data {
  629. Data(const std::vector<int32_t> &size, InterpolationMode interpolation)
  630. : size_(size), interpolation_(interpolation) {}
  631. std::vector<int32_t> size_;
  632. InterpolationMode interpolation_;
  633. };
  634. Resize::Resize(std::vector<int32_t> size, InterpolationMode interpolation)
  635. : data_(std::make_shared<Data>(size, interpolation)) {}
  636. std::shared_ptr<TensorOperation> Resize::Parse() {
  637. return std::make_shared<ResizeOperation>(data_->size_, data_->interpolation_);
  638. }
  639. std::shared_ptr<TensorOperation> Resize::Parse(const MapTargetDevice &env) {
  640. if (env == MapTargetDevice::kAscend310) {
  641. #ifdef ENABLE_ACL
  642. std::vector<uint32_t> usize_;
  643. usize_.reserve(data_->size_.size());
  644. std::transform(data_->size_.begin(), data_->size_.end(), std::back_inserter(usize_),
  645. [](int32_t i) { return (uint32_t)i; });
  646. return std::make_shared<DvppResizeJpegOperation>(usize_);
  647. #endif // ENABLE_ACL
  648. }
  649. return std::make_shared<ResizeOperation>(data_->size_, data_->interpolation_);
  650. }
  651. // ResizePreserveAR Transform Operation.
  652. struct ResizePreserveAR::Data {
  653. Data(int32_t height, int32_t width, int32_t img_orientation)
  654. : height_(height), width_(width), img_orientation_(img_orientation) {}
  655. int32_t height_;
  656. int32_t width_;
  657. int32_t img_orientation_;
  658. };
  659. ResizePreserveAR::ResizePreserveAR(int32_t height, int32_t width, int32_t img_orientation)
  660. : data_(std::make_shared<Data>(height, width, img_orientation)) {}
  661. std::shared_ptr<TensorOperation> ResizePreserveAR::Parse() {
  662. return std::make_shared<ResizePreserveAROperation>(data_->height_, data_->width_, data_->img_orientation_);
  663. }
  664. #ifdef ENABLE_ANDROID
  665. // Rotate Transform Operation.
  666. Rotate::Rotate() {}
  667. std::shared_ptr<TensorOperation> Rotate::Parse() { return std::make_shared<RotateOperation>(); }
  668. #endif // ENABLE_ANDROID
  669. #ifndef ENABLE_ANDROID
  670. // ResizeWithBBox Transform Operation.
  671. struct ResizeWithBBox::Data {
  672. Data(const std::vector<int32_t> &size, InterpolationMode interpolation)
  673. : size_(size), interpolation_(interpolation) {}
  674. std::vector<int32_t> size_;
  675. InterpolationMode interpolation_;
  676. };
  677. ResizeWithBBox::ResizeWithBBox(std::vector<int32_t> size, InterpolationMode interpolation)
  678. : data_(std::make_shared<Data>(size, interpolation)) {}
  679. std::shared_ptr<TensorOperation> ResizeWithBBox::Parse() {
  680. return std::make_shared<ResizeWithBBoxOperation>(data_->size_, data_->interpolation_);
  681. }
  682. // RgbaToBgr Transform Operation.
  683. RGBA2BGR::RGBA2BGR() {}
  684. std::shared_ptr<TensorOperation> RGBA2BGR::Parse() { return std::make_shared<RgbaToBgrOperation>(); }
  685. // RgbaToRgb Transform Operation.
  686. RGBA2RGB::RGBA2RGB() {}
  687. std::shared_ptr<TensorOperation> RGBA2RGB::Parse() { return std::make_shared<RgbaToRgbOperation>(); }
  688. // SoftDvppDecodeRandomCropResizeJpeg Transform Operation.
  689. struct SoftDvppDecodeRandomCropResizeJpeg::Data {
  690. Data(const std::vector<int32_t> &size, const std::vector<float> &scale, const std::vector<float> &ratio,
  691. int32_t max_attempts)
  692. : size_(size), scale_(scale), ratio_(ratio), max_attempts_(max_attempts) {}
  693. std::vector<int32_t> size_;
  694. std::vector<float> scale_;
  695. std::vector<float> ratio_;
  696. int32_t max_attempts_;
  697. };
  698. SoftDvppDecodeRandomCropResizeJpeg::SoftDvppDecodeRandomCropResizeJpeg(std::vector<int32_t> size,
  699. std::vector<float> scale,
  700. std::vector<float> ratio, int32_t max_attempts)
  701. : data_(std::make_shared<Data>(size, scale, ratio, max_attempts)) {}
  702. std::shared_ptr<TensorOperation> SoftDvppDecodeRandomCropResizeJpeg::Parse() {
  703. return std::make_shared<SoftDvppDecodeRandomCropResizeJpegOperation>(data_->size_, data_->scale_, data_->ratio_,
  704. data_->max_attempts_);
  705. }
  706. // SoftDvppDecodeResizeJpeg Transform Operation.
  707. struct SoftDvppDecodeResizeJpeg::Data {
  708. explicit Data(const std::vector<int32_t> &size) : size_(size) {}
  709. std::vector<int32_t> size_;
  710. };
  711. SoftDvppDecodeResizeJpeg::SoftDvppDecodeResizeJpeg(std::vector<int32_t> size) : data_(std::make_shared<Data>(size)) {}
  712. std::shared_ptr<TensorOperation> SoftDvppDecodeResizeJpeg::Parse() {
  713. return std::make_shared<SoftDvppDecodeResizeJpegOperation>(data_->size_);
  714. }
  715. // SwapRedBlue Transform Operation.
  716. SwapRedBlue::SwapRedBlue() {}
  717. std::shared_ptr<TensorOperation> SwapRedBlue::Parse() { return std::make_shared<SwapRedBlueOperation>(); }
  718. // UniformAug Transform Operation.
  719. struct UniformAugment::Data {
  720. std::vector<std::shared_ptr<TensorOperation>> transforms_;
  721. int32_t num_ops_;
  722. };
  723. UniformAugment::UniformAugment(const std::vector<TensorTransform *> &transforms, int32_t num_ops)
  724. : data_(std::make_shared<Data>()) {
  725. (void)std::transform(
  726. transforms.begin(), transforms.end(), std::back_inserter(data_->transforms_),
  727. [](TensorTransform *const op) -> std::shared_ptr<TensorOperation> { return op ? op->Parse() : nullptr; });
  728. data_->num_ops_ = num_ops;
  729. }
  730. UniformAugment::UniformAugment(const std::vector<std::shared_ptr<TensorTransform>> &transforms, int32_t num_ops)
  731. : data_(std::make_shared<Data>()) {
  732. (void)std::transform(
  733. transforms.begin(), transforms.end(), std::back_inserter(data_->transforms_),
  734. [](std::shared_ptr<TensorTransform> op) -> std::shared_ptr<TensorOperation> { return op ? op->Parse() : nullptr; });
  735. data_->num_ops_ = num_ops;
  736. }
  737. UniformAugment::UniformAugment(const std::vector<std::reference_wrapper<TensorTransform>> &transforms, int32_t num_ops)
  738. : data_(std::make_shared<Data>()) {
  739. (void)std::transform(transforms.begin(), transforms.end(), std::back_inserter(data_->transforms_),
  740. [](TensorTransform &op) -> std::shared_ptr<TensorOperation> { return op.Parse(); });
  741. data_->num_ops_ = num_ops;
  742. }
  743. std::shared_ptr<TensorOperation> UniformAugment::Parse() {
  744. return std::make_shared<UniformAugOperation>(data_->transforms_, data_->num_ops_);
  745. }
  746. #endif // not ENABLE_ANDROID
  747. } // namespace vision
  748. } // namespace dataset
  749. } // namespace mindspore