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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132
  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/adjust_gamma_ir.h"
  23. #include "minddata/dataset/kernels/ir/vision/affine_ir.h"
  24. #include "minddata/dataset/kernels/ir/vision/auto_augment_ir.h"
  25. #include "minddata/dataset/kernels/ir/vision/auto_contrast_ir.h"
  26. #include "minddata/dataset/kernels/ir/vision/bounding_box_augment_ir.h"
  27. #include "minddata/dataset/kernels/ir/vision/center_crop_ir.h"
  28. #include "minddata/dataset/kernels/ir/vision/convert_color_ir.h"
  29. #include "minddata/dataset/kernels/ir/vision/crop_ir.h"
  30. #include "minddata/dataset/kernels/ir/vision/cutmix_batch_ir.h"
  31. #include "minddata/dataset/kernels/ir/vision/cutout_ir.h"
  32. #include "minddata/dataset/kernels/ir/vision/decode_ir.h"
  33. #include "minddata/dataset/kernels/ir/vision/equalize_ir.h"
  34. #include "minddata/dataset/kernels/ir/vision/gaussian_blur_ir.h"
  35. #include "minddata/dataset/kernels/ir/vision/horizontal_flip_ir.h"
  36. #include "minddata/dataset/kernels/ir/vision/hwc_to_chw_ir.h"
  37. #include "minddata/dataset/kernels/ir/vision/invert_ir.h"
  38. #include "minddata/dataset/kernels/ir/vision/mixup_batch_ir.h"
  39. #include "minddata/dataset/kernels/ir/vision/normalize_ir.h"
  40. #include "minddata/dataset/kernels/ir/vision/normalize_pad_ir.h"
  41. #include "minddata/dataset/kernels/ir/vision/pad_ir.h"
  42. #include "minddata/dataset/kernels/ir/vision/random_adjust_sharpness_ir.h"
  43. #include "minddata/dataset/kernels/ir/vision/random_affine_ir.h"
  44. #include "minddata/dataset/kernels/ir/vision/random_auto_contrast_ir.h"
  45. #include "minddata/dataset/kernels/ir/vision/random_color_adjust_ir.h"
  46. #include "minddata/dataset/kernels/ir/vision/random_color_ir.h"
  47. #include "minddata/dataset/kernels/ir/vision/random_crop_decode_resize_ir.h"
  48. #include "minddata/dataset/kernels/ir/vision/random_crop_ir.h"
  49. #include "minddata/dataset/kernels/ir/vision/random_crop_with_bbox_ir.h"
  50. #include "minddata/dataset/kernels/ir/vision/random_equalize_ir.h"
  51. #include "minddata/dataset/kernels/ir/vision/random_horizontal_flip_ir.h"
  52. #include "minddata/dataset/kernels/ir/vision/random_horizontal_flip_with_bbox_ir.h"
  53. #include "minddata/dataset/kernels/ir/vision/random_invert_ir.h"
  54. #include "minddata/dataset/kernels/ir/vision/random_lighting_ir.h"
  55. #include "minddata/dataset/kernels/ir/vision/random_posterize_ir.h"
  56. #include "minddata/dataset/kernels/ir/vision/random_resize_ir.h"
  57. #include "minddata/dataset/kernels/ir/vision/random_resize_with_bbox_ir.h"
  58. #include "minddata/dataset/kernels/ir/vision/random_resized_crop_ir.h"
  59. #include "minddata/dataset/kernels/ir/vision/random_resized_crop_with_bbox_ir.h"
  60. #include "minddata/dataset/kernels/ir/vision/random_rotation_ir.h"
  61. #include "minddata/dataset/kernels/ir/vision/random_select_subpolicy_ir.h"
  62. #include "minddata/dataset/kernels/ir/vision/random_sharpness_ir.h"
  63. #include "minddata/dataset/kernels/ir/vision/random_solarize_ir.h"
  64. #include "minddata/dataset/kernels/ir/vision/random_vertical_flip_ir.h"
  65. #include "minddata/dataset/kernels/ir/vision/random_vertical_flip_with_bbox_ir.h"
  66. #include "minddata/dataset/kernels/ir/vision/rescale_ir.h"
  67. #include "minddata/dataset/kernels/ir/vision/resize_ir.h"
  68. #include "minddata/dataset/kernels/ir/vision/resize_preserve_ar_ir.h"
  69. #include "minddata/dataset/kernels/ir/vision/resize_with_bbox_ir.h"
  70. #include "minddata/dataset/kernels/ir/vision/rgb_to_bgr_ir.h"
  71. #include "minddata/dataset/kernels/ir/vision/rgb_to_gray_ir.h"
  72. #include "minddata/dataset/kernels/ir/vision/rgba_to_bgr_ir.h"
  73. #include "minddata/dataset/kernels/ir/vision/rgba_to_rgb_ir.h"
  74. #include "minddata/dataset/kernels/ir/vision/rotate_ir.h"
  75. #include "minddata/dataset/kernels/ir/vision/slice_patches_ir.h"
  76. #include "minddata/dataset/kernels/ir/vision/softdvpp_decode_random_crop_resize_jpeg_ir.h"
  77. #include "minddata/dataset/kernels/ir/vision/softdvpp_decode_resize_jpeg_ir.h"
  78. #include "minddata/dataset/kernels/ir/vision/swap_red_blue_ir.h"
  79. #include "minddata/dataset/kernels/ir/vision/uniform_aug_ir.h"
  80. #include "minddata/dataset/kernels/ir/vision/vertical_flip_ir.h"
  81. #ifndef ENABLE_ANDROID
  82. #include "utils/log_adapter.h"
  83. #else
  84. #include "mindspore/lite/src/common/log_adapter.h"
  85. #endif
  86. #include "minddata/dataset/kernels/ir/validators.h"
  87. // Kernel image headers (in alphabetical order)
  88. namespace mindspore {
  89. namespace dataset {
  90. // Transform operations for computer vision.
  91. namespace vision {
  92. // CONSTRUCTORS FOR API CLASSES TO CREATE VISION TENSOR TRANSFORM OPERATIONS
  93. // (In alphabetical order)
  94. // Affine Transform Operation.
  95. struct Affine::Data {
  96. Data(float_t degrees, const std::vector<float> &translation, float scale, const std::vector<float> &shear,
  97. InterpolationMode interpolation, const std::vector<uint8_t> &fill_value)
  98. : degrees_(degrees),
  99. translation_(translation),
  100. scale_(scale),
  101. shear_(shear),
  102. interpolation_(interpolation),
  103. fill_value_(fill_value) {}
  104. float degrees_;
  105. std::vector<float> translation_;
  106. float scale_;
  107. std::vector<float> shear_;
  108. InterpolationMode interpolation_;
  109. std::vector<uint8_t> fill_value_;
  110. };
  111. Affine::Affine(float_t degrees, const std::vector<float> &translation, float scale, const std::vector<float> &shear,
  112. InterpolationMode interpolation, const std::vector<uint8_t> &fill_value)
  113. : data_(std::make_shared<Data>(degrees, translation, scale, shear, interpolation, fill_value)) {}
  114. std::shared_ptr<TensorOperation> Affine::Parse() {
  115. return std::make_shared<AffineOperation>(data_->degrees_, data_->translation_, data_->scale_, data_->shear_,
  116. data_->interpolation_, data_->fill_value_);
  117. }
  118. #ifndef ENABLE_ANDROID
  119. // AdjustGamma Transform Operation.
  120. struct AdjustGamma::Data {
  121. Data(float gamma, float gain) : gamma_(gamma), gain_(gain) {}
  122. float gamma_;
  123. float gain_;
  124. };
  125. AdjustGamma::AdjustGamma(float gamma, float gain) : data_(std::make_shared<Data>(gamma, gain)) {}
  126. std::shared_ptr<TensorOperation> AdjustGamma::Parse() {
  127. return std::make_shared<AdjustGammaOperation>(data_->gamma_, data_->gain_);
  128. }
  129. // AutoAugment Transform Operation.
  130. struct AutoAugment::Data {
  131. Data(AutoAugmentPolicy policy, InterpolationMode interpolation, const std::vector<uint8_t> &fill_value)
  132. : policy_(policy), interpolation_(interpolation), fill_value_(fill_value) {}
  133. AutoAugmentPolicy policy_;
  134. InterpolationMode interpolation_;
  135. std::vector<uint8_t> fill_value_;
  136. };
  137. AutoAugment::AutoAugment(AutoAugmentPolicy policy, InterpolationMode interpolation,
  138. const std::vector<uint8_t> &fill_value)
  139. : data_(std::make_shared<Data>(policy, interpolation, fill_value)) {}
  140. std::shared_ptr<TensorOperation> AutoAugment::Parse() {
  141. return std::make_shared<AutoAugmentOperation>(data_->policy_, data_->interpolation_, data_->fill_value_);
  142. }
  143. // AutoContrast Transform Operation.
  144. struct AutoContrast::Data {
  145. Data(float cutoff, const std::vector<uint32_t> &ignore) : cutoff_(cutoff), ignore_(ignore) {}
  146. float cutoff_;
  147. std::vector<uint32_t> ignore_;
  148. };
  149. AutoContrast::AutoContrast(float cutoff, std::vector<uint32_t> ignore)
  150. : data_(std::make_shared<Data>(cutoff, ignore)) {}
  151. std::shared_ptr<TensorOperation> AutoContrast::Parse() {
  152. return std::make_shared<AutoContrastOperation>(data_->cutoff_, data_->ignore_);
  153. }
  154. // BoundingBoxAugment Transform Operation.
  155. struct BoundingBoxAugment::Data {
  156. std::shared_ptr<TensorOperation> transform_;
  157. float ratio_;
  158. };
  159. BoundingBoxAugment::BoundingBoxAugment(TensorTransform *transform, float ratio) : data_(std::make_shared<Data>()) {
  160. data_->transform_ = transform ? transform->Parse() : nullptr;
  161. data_->ratio_ = ratio;
  162. }
  163. BoundingBoxAugment::BoundingBoxAugment(const std::shared_ptr<TensorTransform> &transform, float ratio)
  164. : data_(std::make_shared<Data>()) {
  165. data_->transform_ = transform ? transform->Parse() : nullptr;
  166. data_->ratio_ = ratio;
  167. }
  168. BoundingBoxAugment::BoundingBoxAugment(const std::reference_wrapper<TensorTransform> transform, float ratio)
  169. : data_(std::make_shared<Data>()) {
  170. data_->transform_ = transform.get().Parse();
  171. data_->ratio_ = ratio;
  172. }
  173. std::shared_ptr<TensorOperation> BoundingBoxAugment::Parse() {
  174. return std::make_shared<BoundingBoxAugmentOperation>(data_->transform_, data_->ratio_);
  175. }
  176. #endif // not ENABLE_ANDROID
  177. // CenterCrop Transform Operation.
  178. struct CenterCrop::Data {
  179. explicit Data(const std::vector<int32_t> &size) : size_(size) {}
  180. std::vector<int32_t> size_;
  181. };
  182. CenterCrop::CenterCrop(std::vector<int32_t> size) : data_(std::make_shared<Data>(size)) {}
  183. std::shared_ptr<TensorOperation> CenterCrop::Parse() { return std::make_shared<CenterCropOperation>(data_->size_); }
  184. std::shared_ptr<TensorOperation> CenterCrop::Parse(const MapTargetDevice &env) {
  185. if (env == MapTargetDevice::kAscend310) {
  186. #ifdef ENABLE_ACL
  187. std::vector<uint32_t> usize_;
  188. usize_.reserve(data_->size_.size());
  189. std::transform(data_->size_.begin(), data_->size_.end(), std::back_inserter(usize_),
  190. [](int32_t i) { return (uint32_t)i; });
  191. return std::make_shared<DvppCropJpegOperation>(usize_);
  192. #endif // ENABLE_ACL
  193. } else if (env == MapTargetDevice::kCpu) {
  194. return std::make_shared<CenterCropOperation>(data_->size_);
  195. }
  196. MS_LOG(ERROR) << "Unsupported MapTargetDevice, only supported kCpu and kAscend310.";
  197. return nullptr;
  198. }
  199. #ifndef ENABLE_ANDROID
  200. // ConvertColor Transform Operation.
  201. struct ConvertColor::Data {
  202. explicit Data(ConvertMode convert_mode) : convert_mode_(convert_mode) {}
  203. ConvertMode convert_mode_;
  204. };
  205. ConvertColor::ConvertColor(ConvertMode convert_mode) : data_(std::make_shared<Data>(convert_mode)) {}
  206. std::shared_ptr<TensorOperation> ConvertColor::Parse() {
  207. return std::make_shared<ConvertColorOperation>(data_->convert_mode_);
  208. }
  209. #endif // not ENABLE_ANDROID
  210. // Crop Transform Operation.
  211. struct Crop::Data {
  212. Data(const std::vector<int32_t> &coordinates, const std::vector<int32_t> &size)
  213. : coordinates_(coordinates), size_(size) {}
  214. std::vector<int32_t> coordinates_;
  215. std::vector<int32_t> size_;
  216. };
  217. Crop::Crop(std::vector<int32_t> coordinates, std::vector<int32_t> size)
  218. : data_(std::make_shared<Data>(coordinates, size)) {}
  219. std::shared_ptr<TensorOperation> Crop::Parse() {
  220. return std::make_shared<CropOperation>(data_->coordinates_, data_->size_);
  221. }
  222. #ifndef ENABLE_ANDROID
  223. // CutMixBatch Transform Operation.
  224. struct CutMixBatch::Data {
  225. Data(ImageBatchFormat image_batch_format, float alpha, float prob)
  226. : image_batch_format_(image_batch_format), alpha_(alpha), prob_(prob) {}
  227. float alpha_;
  228. float prob_;
  229. ImageBatchFormat image_batch_format_;
  230. };
  231. CutMixBatch::CutMixBatch(ImageBatchFormat image_batch_format, float alpha, float prob)
  232. : data_(std::make_shared<Data>(image_batch_format, alpha, prob)) {}
  233. std::shared_ptr<TensorOperation> CutMixBatch::Parse() {
  234. return std::make_shared<CutMixBatchOperation>(data_->image_batch_format_, data_->alpha_, data_->prob_);
  235. }
  236. // CutOutOp.
  237. struct CutOut::Data {
  238. Data(int32_t length, int32_t num_patches) : length_(length), num_patches_(num_patches) {}
  239. int32_t length_;
  240. int32_t num_patches_;
  241. };
  242. CutOut::CutOut(int32_t length, int32_t num_patches) : data_(std::make_shared<Data>(length, num_patches)) {}
  243. std::shared_ptr<TensorOperation> CutOut::Parse() {
  244. return std::make_shared<CutOutOperation>(data_->length_, data_->num_patches_);
  245. }
  246. #endif // not ENABLE_ANDROID
  247. // Decode Transform Operation.
  248. struct Decode::Data {
  249. explicit Data(bool rgb) : rgb_(rgb) {}
  250. bool rgb_;
  251. };
  252. Decode::Decode(bool rgb) : data_(std::make_shared<Data>(rgb)) {}
  253. std::shared_ptr<TensorOperation> Decode::Parse() { return std::make_shared<DecodeOperation>(data_->rgb_); }
  254. std::shared_ptr<TensorOperation> Decode::Parse(const MapTargetDevice &env) {
  255. if (env == MapTargetDevice::kAscend310) {
  256. #ifdef ENABLE_ACL
  257. return std::make_shared<DvppDecodeJpegOperation>();
  258. #endif // ENABLE_ACL
  259. } else if (env == MapTargetDevice::kCpu) {
  260. return std::make_shared<DecodeOperation>(data_->rgb_);
  261. }
  262. MS_LOG(ERROR) << "Unsupported MapTargetDevice, only supported kCpu and kAscend310.";
  263. return nullptr;
  264. }
  265. #ifdef ENABLE_ACL
  266. // DvppDecodeResize Transform Operation.
  267. struct DvppDecodeResizeJpeg::Data {
  268. explicit Data(const std::vector<uint32_t> &resize) : resize_(resize) {}
  269. std::vector<uint32_t> resize_;
  270. };
  271. DvppDecodeResizeJpeg::DvppDecodeResizeJpeg(std::vector<uint32_t> resize) : data_(std::make_shared<Data>(resize)) {}
  272. std::shared_ptr<TensorOperation> DvppDecodeResizeJpeg::Parse() {
  273. return std::make_shared<DvppDecodeResizeOperation>(data_->resize_);
  274. }
  275. std::shared_ptr<TensorOperation> DvppDecodeResizeJpeg::Parse(const MapTargetDevice &env) {
  276. if (env == MapTargetDevice::kAscend310) {
  277. return std::make_shared<DvppDecodeResizeOperation>(data_->resize_);
  278. }
  279. MS_LOG(ERROR) << "Unsupported MapTargetDevice, only supported kAscend310.";
  280. return nullptr;
  281. }
  282. // DvppDecodeResizeCrop Transform Operation.
  283. struct DvppDecodeResizeCropJpeg::Data {
  284. Data(const std::vector<uint32_t> &crop, const std::vector<uint32_t> &resize) : crop_(crop), resize_(resize) {}
  285. std::vector<uint32_t> crop_;
  286. std::vector<uint32_t> resize_;
  287. };
  288. DvppDecodeResizeCropJpeg::DvppDecodeResizeCropJpeg(std::vector<uint32_t> crop, std::vector<uint32_t> resize)
  289. : data_(std::make_shared<Data>(crop, resize)) {}
  290. std::shared_ptr<TensorOperation> DvppDecodeResizeCropJpeg::Parse() {
  291. return std::make_shared<DvppDecodeResizeCropOperation>(data_->crop_, data_->resize_);
  292. }
  293. std::shared_ptr<TensorOperation> DvppDecodeResizeCropJpeg::Parse(const MapTargetDevice &env) {
  294. if (env == MapTargetDevice::kAscend310) {
  295. return std::make_shared<DvppDecodeResizeCropOperation>(data_->crop_, data_->resize_);
  296. }
  297. MS_LOG(ERROR) << "Unsupported MapTargetDevice, only supported kAscend310.";
  298. return nullptr;
  299. }
  300. // DvppDecodePng Transform Operation.
  301. DvppDecodePng::DvppDecodePng() {}
  302. std::shared_ptr<TensorOperation> DvppDecodePng::Parse() { return std::make_shared<DvppDecodePngOperation>(); }
  303. std::shared_ptr<TensorOperation> DvppDecodePng::Parse(const MapTargetDevice &env) {
  304. if (env == MapTargetDevice::kAscend310) {
  305. return std::make_shared<DvppDecodePngOperation>();
  306. }
  307. MS_LOG(ERROR) << "Unsupported MapTargetDevice, only supported kAscend310.";
  308. return nullptr;
  309. }
  310. #endif // ENABLE_ACL
  311. #ifndef ENABLE_ANDROID
  312. // Equalize Transform Operation.
  313. Equalize::Equalize() {}
  314. std::shared_ptr<TensorOperation> Equalize::Parse() { return std::make_shared<EqualizeOperation>(); }
  315. #endif // not ENABLE_ANDROID
  316. // GaussianBlur Transform Operation.
  317. struct GaussianBlur::Data {
  318. Data(const std::vector<int32_t> &kernel_size, const std::vector<float> &sigma)
  319. : kernel_size_(kernel_size), sigma_(sigma) {}
  320. std::vector<int32_t> kernel_size_;
  321. std::vector<float> sigma_;
  322. };
  323. GaussianBlur::GaussianBlur(const std::vector<int32_t> &kernel_size, const std::vector<float> &sigma)
  324. : data_(std::make_shared<Data>(kernel_size, sigma)) {}
  325. std::shared_ptr<TensorOperation> GaussianBlur::Parse() {
  326. return std::make_shared<GaussianBlurOperation>(data_->kernel_size_, data_->sigma_);
  327. }
  328. #ifndef ENABLE_ANDROID
  329. // HorizontalFlip Transform Operation.
  330. HorizontalFlip::HorizontalFlip() {}
  331. std::shared_ptr<TensorOperation> HorizontalFlip::Parse() { return std::make_shared<HorizontalFlipOperation>(); }
  332. // HwcToChw Transform Operation.
  333. HWC2CHW::HWC2CHW() {}
  334. std::shared_ptr<TensorOperation> HWC2CHW::Parse() { return std::make_shared<HwcToChwOperation>(); }
  335. // Invert Transform Operation.
  336. Invert::Invert() {}
  337. std::shared_ptr<TensorOperation> Invert::Parse() { return std::make_shared<InvertOperation>(); }
  338. // MixUpBatch Transform Operation.
  339. struct MixUpBatch::Data {
  340. explicit Data(float alpha) : alpha_(alpha) {}
  341. float alpha_;
  342. };
  343. MixUpBatch::MixUpBatch(float alpha) : data_(std::make_shared<Data>(alpha)) {}
  344. std::shared_ptr<TensorOperation> MixUpBatch::Parse() { return std::make_shared<MixUpBatchOperation>(data_->alpha_); }
  345. #endif // not ENABLE_ANDROID
  346. // Normalize Transform Operation.
  347. struct Normalize::Data {
  348. Data(const std::vector<float> &mean, const std::vector<float> &std) : mean_(mean), std_(std) {}
  349. std::vector<float> mean_;
  350. std::vector<float> std_;
  351. };
  352. Normalize::Normalize(std::vector<float> mean, std::vector<float> std) : data_(std::make_shared<Data>(mean, std)) {}
  353. std::shared_ptr<TensorOperation> Normalize::Parse() {
  354. return std::make_shared<NormalizeOperation>(data_->mean_, data_->std_);
  355. }
  356. std::shared_ptr<TensorOperation> Normalize::Parse(const MapTargetDevice &env) {
  357. if (env == MapTargetDevice::kAscend310) {
  358. #ifdef ENABLE_ACL
  359. return std::make_shared<DvppNormalizeOperation>(data_->mean_, data_->std_);
  360. #endif // ENABLE_ACL
  361. } else if (env == MapTargetDevice::kCpu) {
  362. return std::make_shared<NormalizeOperation>(data_->mean_, data_->std_);
  363. }
  364. MS_LOG(ERROR) << "Unsupported MapTargetDevice, only supported kCpu and kAscend310.";
  365. return nullptr;
  366. }
  367. #ifndef ENABLE_ANDROID
  368. // NormalizePad Transform Operation.
  369. struct NormalizePad::Data {
  370. Data(const std::vector<float> &mean, const std::vector<float> &std, const std::string &dtype)
  371. : mean_(mean), std_(std), dtype_(dtype) {}
  372. std::vector<float> mean_;
  373. std::vector<float> std_;
  374. std::string dtype_;
  375. };
  376. NormalizePad::NormalizePad(const std::vector<float> &mean, const std::vector<float> &std,
  377. const std::vector<char> &dtype)
  378. : data_(std::make_shared<Data>(mean, std, CharToString(dtype))) {}
  379. std::shared_ptr<TensorOperation> NormalizePad::Parse() {
  380. return std::make_shared<NormalizePadOperation>(data_->mean_, data_->std_, data_->dtype_);
  381. }
  382. // Pad Transform Operation.
  383. struct Pad::Data {
  384. Data(const std::vector<int32_t> &padding, const std::vector<uint8_t> &fill_value, BorderType padding_mode)
  385. : padding_(padding), fill_value_(fill_value), padding_mode_(padding_mode) {}
  386. std::vector<int32_t> padding_;
  387. std::vector<uint8_t> fill_value_;
  388. BorderType padding_mode_;
  389. };
  390. Pad::Pad(std::vector<int32_t> padding, std::vector<uint8_t> fill_value, BorderType padding_mode)
  391. : data_(std::make_shared<Data>(padding, fill_value, padding_mode)) {}
  392. std::shared_ptr<TensorOperation> Pad::Parse() {
  393. return std::make_shared<PadOperation>(data_->padding_, data_->fill_value_, data_->padding_mode_);
  394. }
  395. // RandomAdjustSharpness Transform Operation.
  396. struct RandomAdjustSharpness::Data {
  397. Data(float degree, float prob) : degree_(degree), probability_(prob) {}
  398. float degree_;
  399. float probability_;
  400. };
  401. RandomAdjustSharpness::RandomAdjustSharpness(float degree, float prob) : data_(std::make_shared<Data>(degree, prob)) {}
  402. std::shared_ptr<TensorOperation> RandomAdjustSharpness::Parse() {
  403. return std::make_shared<RandomAdjustSharpnessOperation>(data_->degree_, data_->probability_);
  404. }
  405. #endif // not ENABLE_ANDROID
  406. // RandomAffine Transform Operation.
  407. struct RandomAffine::Data {
  408. Data(const std::vector<float_t> &degrees, const std::vector<float_t> &translate_range,
  409. const std::vector<float_t> &scale_range, const std::vector<float_t> &shear_ranges,
  410. InterpolationMode interpolation, const std::vector<uint8_t> &fill_value)
  411. : degrees_(degrees),
  412. translate_range_(translate_range),
  413. scale_range_(scale_range),
  414. shear_ranges_(shear_ranges),
  415. interpolation_(interpolation),
  416. fill_value_(fill_value) {}
  417. std::vector<float_t> degrees_; // min_degree, max_degree
  418. std::vector<float_t> translate_range_; // maximum x translation percentage, maximum y translation percentage
  419. std::vector<float_t> scale_range_; // min_scale, max_scale
  420. std::vector<float_t> shear_ranges_; // min_x_shear, max_x_shear, min_y_shear, max_y_shear
  421. InterpolationMode interpolation_;
  422. std::vector<uint8_t> fill_value_;
  423. };
  424. RandomAffine::RandomAffine(const std::vector<float_t> &degrees, const std::vector<float_t> &translate_range,
  425. const std::vector<float_t> &scale_range, const std::vector<float_t> &shear_ranges,
  426. InterpolationMode interpolation, const std::vector<uint8_t> &fill_value)
  427. : data_(std::make_shared<Data>(degrees, translate_range, scale_range, shear_ranges, interpolation, fill_value)) {}
  428. std::shared_ptr<TensorOperation> RandomAffine::Parse() {
  429. return std::make_shared<RandomAffineOperation>(data_->degrees_, data_->translate_range_, data_->scale_range_,
  430. data_->shear_ranges_, data_->interpolation_, data_->fill_value_);
  431. }
  432. #ifndef ENABLE_ANDROID
  433. // RandomAutoContrast Transform Operation.
  434. struct RandomAutoContrast::Data {
  435. Data(float cutoff, const std::vector<uint32_t> &ignore, float prob)
  436. : cutoff_(cutoff), ignore_(ignore), probability_(prob) {}
  437. float cutoff_;
  438. std::vector<uint32_t> ignore_;
  439. float probability_;
  440. };
  441. RandomAutoContrast::RandomAutoContrast(float cutoff, std::vector<uint32_t> ignore, float prob)
  442. : data_(std::make_shared<Data>(cutoff, ignore, prob)) {}
  443. std::shared_ptr<TensorOperation> RandomAutoContrast::Parse() {
  444. return std::make_shared<RandomAutoContrastOperation>(data_->cutoff_, data_->ignore_, data_->probability_);
  445. }
  446. // RandomColor Transform Operation.
  447. struct RandomColor::Data {
  448. Data(float t_lb, float t_ub) : t_lb_(t_lb), t_ub_(t_ub) {}
  449. float t_lb_;
  450. float t_ub_;
  451. };
  452. RandomColor::RandomColor(float t_lb, float t_ub) : data_(std::make_shared<Data>(t_lb, t_ub)) {}
  453. std::shared_ptr<TensorOperation> RandomColor::Parse() {
  454. return std::make_shared<RandomColorOperation>(data_->t_lb_, data_->t_ub_);
  455. }
  456. // RandomColorAdjust Transform Operation.
  457. struct RandomColorAdjust::Data {
  458. Data(const std::vector<float> &brightness, const std::vector<float> &contrast, const std::vector<float> &saturation,
  459. const std::vector<float> &hue)
  460. : brightness_(brightness), contrast_(contrast), saturation_(saturation), hue_(hue) {}
  461. std::vector<float> brightness_;
  462. std::vector<float> contrast_;
  463. std::vector<float> saturation_;
  464. std::vector<float> hue_;
  465. };
  466. RandomColorAdjust::RandomColorAdjust(std::vector<float> brightness, std::vector<float> contrast,
  467. std::vector<float> saturation, std::vector<float> hue)
  468. : data_(std::make_shared<Data>(brightness, contrast, saturation, hue)) {}
  469. std::shared_ptr<TensorOperation> RandomColorAdjust::Parse() {
  470. return std::make_shared<RandomColorAdjustOperation>(data_->brightness_, data_->contrast_, data_->saturation_,
  471. data_->hue_);
  472. }
  473. // RandomCrop Transform Operation.
  474. struct RandomCrop::Data {
  475. Data(const std::vector<int32_t> &size, const std::vector<int32_t> &padding, bool pad_if_needed,
  476. const std::vector<uint8_t> &fill_value, BorderType padding_mode)
  477. : size_(size),
  478. padding_(padding),
  479. pad_if_needed_(pad_if_needed),
  480. fill_value_(fill_value),
  481. padding_mode_(padding_mode) {}
  482. std::vector<int32_t> size_;
  483. std::vector<int32_t> padding_;
  484. bool pad_if_needed_;
  485. std::vector<uint8_t> fill_value_;
  486. BorderType padding_mode_;
  487. };
  488. RandomCrop::RandomCrop(std::vector<int32_t> size, std::vector<int32_t> padding, bool pad_if_needed,
  489. std::vector<uint8_t> fill_value, BorderType padding_mode)
  490. : data_(std::make_shared<Data>(size, padding, pad_if_needed, fill_value, padding_mode)) {}
  491. std::shared_ptr<TensorOperation> RandomCrop::Parse() {
  492. return std::make_shared<RandomCropOperation>(data_->size_, data_->padding_, data_->pad_if_needed_, data_->fill_value_,
  493. data_->padding_mode_);
  494. }
  495. // RandomCropDecodeResize Transform Operation.
  496. struct RandomCropDecodeResize::Data {
  497. Data(const std::vector<int32_t> &size, const std::vector<float> &scale, const std::vector<float> &ratio,
  498. InterpolationMode interpolation, int32_t max_attempts)
  499. : size_(size), scale_(scale), ratio_(ratio), interpolation_(interpolation), max_attempts_(max_attempts) {}
  500. std::vector<int32_t> size_;
  501. std::vector<float> scale_;
  502. std::vector<float> ratio_;
  503. InterpolationMode interpolation_;
  504. int32_t max_attempts_;
  505. };
  506. RandomCropDecodeResize::RandomCropDecodeResize(std::vector<int32_t> size, std::vector<float> scale,
  507. std::vector<float> ratio, InterpolationMode interpolation,
  508. int32_t max_attempts)
  509. : data_(std::make_shared<Data>(size, scale, ratio, interpolation, max_attempts)) {}
  510. std::shared_ptr<TensorOperation> RandomCropDecodeResize::Parse() {
  511. return std::make_shared<RandomCropDecodeResizeOperation>(data_->size_, data_->scale_, data_->ratio_,
  512. data_->interpolation_, data_->max_attempts_);
  513. }
  514. // RandomCropWithBBox Transform Operation.
  515. struct RandomCropWithBBox::Data {
  516. Data(const std::vector<int32_t> &size, const std::vector<int32_t> &padding, bool pad_if_needed,
  517. const std::vector<uint8_t> &fill_value, BorderType padding_mode)
  518. : size_(size),
  519. padding_(padding),
  520. pad_if_needed_(pad_if_needed),
  521. fill_value_(fill_value),
  522. padding_mode_(padding_mode) {}
  523. std::vector<int32_t> size_;
  524. std::vector<int32_t> padding_;
  525. bool pad_if_needed_;
  526. std::vector<uint8_t> fill_value_;
  527. BorderType padding_mode_;
  528. };
  529. RandomCropWithBBox::RandomCropWithBBox(std::vector<int32_t> size, std::vector<int32_t> padding, bool pad_if_needed,
  530. std::vector<uint8_t> fill_value, BorderType padding_mode)
  531. : data_(std::make_shared<Data>(size, padding, pad_if_needed, fill_value, padding_mode)) {}
  532. std::shared_ptr<TensorOperation> RandomCropWithBBox::Parse() {
  533. return std::make_shared<RandomCropWithBBoxOperation>(data_->size_, data_->padding_, data_->pad_if_needed_,
  534. data_->fill_value_, data_->padding_mode_);
  535. }
  536. // RandomEqualize Transform Operation.
  537. struct RandomEqualize::Data {
  538. explicit Data(float prob) : probability_(prob) {}
  539. float probability_;
  540. };
  541. RandomEqualize::RandomEqualize(float prob) : data_(std::make_shared<Data>(prob)) {}
  542. std::shared_ptr<TensorOperation> RandomEqualize::Parse() {
  543. return std::make_shared<RandomEqualizeOperation>(data_->probability_);
  544. }
  545. // RandomHorizontalFlip.
  546. struct RandomHorizontalFlip::Data {
  547. explicit Data(float prob) : probability_(prob) {}
  548. float probability_;
  549. };
  550. RandomHorizontalFlip::RandomHorizontalFlip(float prob) : data_(std::make_shared<Data>(prob)) {}
  551. std::shared_ptr<TensorOperation> RandomHorizontalFlip::Parse() {
  552. return std::make_shared<RandomHorizontalFlipOperation>(data_->probability_);
  553. }
  554. // RandomHorizontalFlipWithBBox
  555. struct RandomHorizontalFlipWithBBox::Data {
  556. explicit Data(float prob) : probability_(prob) {}
  557. float probability_;
  558. };
  559. RandomHorizontalFlipWithBBox::RandomHorizontalFlipWithBBox(float prob) : data_(std::make_shared<Data>(prob)) {}
  560. std::shared_ptr<TensorOperation> RandomHorizontalFlipWithBBox::Parse() {
  561. return std::make_shared<RandomHorizontalFlipWithBBoxOperation>(data_->probability_);
  562. }
  563. // RandomInvert Operation.
  564. struct RandomInvert::Data {
  565. explicit Data(float prob) : probability_(prob) {}
  566. float probability_;
  567. };
  568. RandomInvert::RandomInvert(float prob) : data_(std::make_shared<Data>(prob)) {}
  569. std::shared_ptr<TensorOperation> RandomInvert::Parse() {
  570. return std::make_shared<RandomInvertOperation>(data_->probability_);
  571. }
  572. // RandomLighting Transform Operation.
  573. struct RandomLighting::Data {
  574. explicit Data(float alpha) : alpha_(alpha) {}
  575. float alpha_;
  576. };
  577. RandomLighting::RandomLighting(float alpha) : data_(std::make_shared<Data>(alpha)) {}
  578. std::shared_ptr<TensorOperation> RandomLighting::Parse() {
  579. return std::make_shared<RandomLightingOperation>(data_->alpha_);
  580. }
  581. // RandomPosterize Transform Operation.
  582. struct RandomPosterize::Data {
  583. explicit Data(const std::vector<uint8_t> &bit_range) : bit_range_(bit_range) {}
  584. std::vector<uint8_t> bit_range_;
  585. };
  586. RandomPosterize::RandomPosterize(const std::vector<uint8_t> &bit_range) : data_(std::make_shared<Data>(bit_range)) {}
  587. std::shared_ptr<TensorOperation> RandomPosterize::Parse() {
  588. return std::make_shared<RandomPosterizeOperation>(data_->bit_range_);
  589. }
  590. // RandomResize Transform Operation.
  591. struct RandomResize::Data {
  592. explicit Data(const std::vector<int32_t> &size) : size_(size) {}
  593. std::vector<int32_t> size_;
  594. };
  595. RandomResize::RandomResize(std::vector<int32_t> size) : data_(std::make_shared<Data>(size)) {}
  596. std::shared_ptr<TensorOperation> RandomResize::Parse() { return std::make_shared<RandomResizeOperation>(data_->size_); }
  597. // RandomResizeWithBBox Transform Operation.
  598. struct RandomResizeWithBBox::Data {
  599. explicit Data(const std::vector<int32_t> &size) : size_(size) {}
  600. std::vector<int32_t> size_;
  601. };
  602. RandomResizeWithBBox::RandomResizeWithBBox(std::vector<int32_t> size) : data_(std::make_shared<Data>(size)) {}
  603. std::shared_ptr<TensorOperation> RandomResizeWithBBox::Parse() {
  604. return std::make_shared<RandomResizeWithBBoxOperation>(data_->size_);
  605. }
  606. // RandomResizedCrop Transform Operation.
  607. struct RandomResizedCrop::Data {
  608. Data(const std::vector<int32_t> &size, const std::vector<float> &scale, const std::vector<float> &ratio,
  609. InterpolationMode interpolation, int32_t max_attempts)
  610. : size_(size), scale_(scale), ratio_(ratio), interpolation_(interpolation), max_attempts_(max_attempts) {}
  611. std::vector<int32_t> size_;
  612. std::vector<float> scale_;
  613. std::vector<float> ratio_;
  614. InterpolationMode interpolation_;
  615. int32_t max_attempts_;
  616. };
  617. RandomResizedCrop::RandomResizedCrop(std::vector<int32_t> size, std::vector<float> scale, std::vector<float> ratio,
  618. InterpolationMode interpolation, int32_t max_attempts)
  619. : data_(std::make_shared<Data>(size, scale, ratio, interpolation, max_attempts)) {}
  620. std::shared_ptr<TensorOperation> RandomResizedCrop::Parse() {
  621. return std::make_shared<RandomResizedCropOperation>(data_->size_, data_->scale_, data_->ratio_, data_->interpolation_,
  622. data_->max_attempts_);
  623. }
  624. // RandomResizedCrop Transform Operation.
  625. struct RandomResizedCropWithBBox::Data {
  626. Data(const std::vector<int32_t> &size, const std::vector<float> &scale, const std::vector<float> &ratio,
  627. InterpolationMode interpolation, int32_t max_attempts)
  628. : size_(size), scale_(scale), ratio_(ratio), interpolation_(interpolation), max_attempts_(max_attempts) {}
  629. std::vector<int32_t> size_;
  630. std::vector<float> scale_;
  631. std::vector<float> ratio_;
  632. InterpolationMode interpolation_;
  633. int32_t max_attempts_;
  634. };
  635. RandomResizedCropWithBBox::RandomResizedCropWithBBox(std::vector<int32_t> size, std::vector<float> scale,
  636. std::vector<float> ratio, InterpolationMode interpolation,
  637. int32_t max_attempts)
  638. : data_(std::make_shared<Data>(size, scale, ratio, interpolation, max_attempts)) {}
  639. std::shared_ptr<TensorOperation> RandomResizedCropWithBBox::Parse() {
  640. return std::make_shared<RandomResizedCropWithBBoxOperation>(data_->size_, data_->scale_, data_->ratio_,
  641. data_->interpolation_, data_->max_attempts_);
  642. }
  643. // RandomRotation Transform Operation.
  644. struct RandomRotation::Data {
  645. Data(const std::vector<float> &degrees, InterpolationMode resample, bool expand, const std::vector<float> &center,
  646. const std::vector<uint8_t> &fill_value)
  647. : degrees_(degrees), interpolation_mode_(resample), center_(center), expand_(expand), fill_value_(fill_value) {}
  648. std::vector<float> degrees_;
  649. InterpolationMode interpolation_mode_;
  650. std::vector<float> center_;
  651. bool expand_;
  652. std::vector<uint8_t> fill_value_;
  653. };
  654. RandomRotation::RandomRotation(std::vector<float> degrees, InterpolationMode resample, bool expand,
  655. std::vector<float> center, std::vector<uint8_t> fill_value)
  656. : data_(std::make_shared<Data>(degrees, resample, expand, center, fill_value)) {}
  657. std::shared_ptr<TensorOperation> RandomRotation::Parse() {
  658. return std::make_shared<RandomRotationOperation>(data_->degrees_, data_->interpolation_mode_, data_->expand_,
  659. data_->center_, data_->fill_value_);
  660. }
  661. // RandomSelectSubpolicy Transform Operation.
  662. struct RandomSelectSubpolicy::Data {
  663. std::vector<std::vector<std::pair<std::shared_ptr<TensorOperation>, double>>> policy_;
  664. };
  665. RandomSelectSubpolicy::RandomSelectSubpolicy(
  666. const std::vector<std::vector<std::pair<TensorTransform *, double>>> &policy)
  667. : data_(std::make_shared<Data>()) {
  668. for (uint32_t i = 0; i < policy.size(); i++) {
  669. std::vector<std::pair<std::shared_ptr<TensorOperation>, double>> subpolicy;
  670. for (uint32_t j = 0; j < policy[i].size(); j++) {
  671. TensorTransform *op = policy[i][j].first;
  672. std::shared_ptr<TensorOperation> operation = (op ? op->Parse() : nullptr);
  673. double prob = policy[i][j].second;
  674. subpolicy.emplace_back(std::move(std::make_pair(operation, prob)));
  675. }
  676. data_->policy_.emplace_back(subpolicy);
  677. }
  678. }
  679. RandomSelectSubpolicy::RandomSelectSubpolicy(
  680. const std::vector<std::vector<std::pair<std::shared_ptr<TensorTransform>, double>>> &policy)
  681. : data_(std::make_shared<Data>()) {
  682. for (uint32_t i = 0; i < policy.size(); i++) {
  683. std::vector<std::pair<std::shared_ptr<TensorOperation>, double>> subpolicy;
  684. for (uint32_t j = 0; j < policy[i].size(); j++) {
  685. std::shared_ptr<TensorTransform> op = policy[i][j].first;
  686. std::shared_ptr<TensorOperation> operation = (op ? op->Parse() : nullptr);
  687. double prob = policy[i][j].second;
  688. subpolicy.emplace_back(std::move(std::make_pair(operation, prob)));
  689. }
  690. data_->policy_.emplace_back(subpolicy);
  691. }
  692. }
  693. RandomSelectSubpolicy::RandomSelectSubpolicy(
  694. const std::vector<std::vector<std::pair<std::reference_wrapper<TensorTransform>, double>>> &policy)
  695. : data_(std::make_shared<Data>()) {
  696. for (int32_t i = 0; i < policy.size(); i++) {
  697. std::vector<std::pair<std::shared_ptr<TensorOperation>, double>> subpolicy;
  698. for (int32_t j = 0; j < policy[i].size(); j++) {
  699. TensorTransform &op = policy[i][j].first;
  700. std::shared_ptr<TensorOperation> operation = op.Parse();
  701. double prob = policy[i][j].second;
  702. subpolicy.emplace_back(std::move(std::make_pair(operation, prob)));
  703. }
  704. data_->policy_.emplace_back(subpolicy);
  705. }
  706. }
  707. std::shared_ptr<TensorOperation> RandomSelectSubpolicy::Parse() {
  708. return std::make_shared<RandomSelectSubpolicyOperation>(data_->policy_);
  709. }
  710. // RandomSharpness Transform Operation.
  711. struct RandomSharpness::Data {
  712. explicit Data(const std::vector<float> &degrees) : degrees_(degrees) {}
  713. std::vector<float> degrees_;
  714. };
  715. RandomSharpness::RandomSharpness(std::vector<float> degrees) : data_(std::make_shared<Data>(degrees)) {}
  716. std::shared_ptr<TensorOperation> RandomSharpness::Parse() {
  717. return std::make_shared<RandomSharpnessOperation>(data_->degrees_);
  718. }
  719. // RandomSolarize Transform Operation.
  720. struct RandomSolarize::Data {
  721. explicit Data(const std::vector<uint8_t> &threshold) : threshold_(threshold) {}
  722. std::vector<uint8_t> threshold_;
  723. };
  724. RandomSolarize::RandomSolarize(std::vector<uint8_t> threshold) : data_(std::make_shared<Data>(threshold)) {}
  725. std::shared_ptr<TensorOperation> RandomSolarize::Parse() {
  726. return std::make_shared<RandomSolarizeOperation>(data_->threshold_);
  727. }
  728. // RandomVerticalFlip Transform Operation.
  729. struct RandomVerticalFlip::Data {
  730. explicit Data(float prob) : probability_(prob) {}
  731. float probability_;
  732. };
  733. RandomVerticalFlip::RandomVerticalFlip(float prob) : data_(std::make_shared<Data>(prob)) {}
  734. std::shared_ptr<TensorOperation> RandomVerticalFlip::Parse() {
  735. return std::make_shared<RandomVerticalFlipOperation>(data_->probability_);
  736. }
  737. // RandomVerticalFlipWithBBox Transform Operation.
  738. struct RandomVerticalFlipWithBBox::Data {
  739. explicit Data(float prob) : probability_(prob) {}
  740. float probability_;
  741. };
  742. RandomVerticalFlipWithBBox::RandomVerticalFlipWithBBox(float prob) : data_(std::make_shared<Data>(prob)) {}
  743. std::shared_ptr<TensorOperation> RandomVerticalFlipWithBBox::Parse() {
  744. return std::make_shared<RandomVerticalFlipWithBBoxOperation>(data_->probability_);
  745. }
  746. // Rescale Transform Operation.
  747. struct Rescale::Data {
  748. Data(float rescale, float shift) : rescale_(rescale), shift_(shift) {}
  749. float rescale_;
  750. float shift_;
  751. };
  752. Rescale::Rescale(float rescale, float shift) : data_(std::make_shared<Data>(rescale, shift)) {}
  753. std::shared_ptr<TensorOperation> Rescale::Parse() {
  754. return std::make_shared<RescaleOperation>(data_->rescale_, data_->shift_);
  755. }
  756. #endif // not ENABLE_ANDROID
  757. // Resize Transform Operation.
  758. struct Resize::Data {
  759. Data(const std::vector<int32_t> &size, InterpolationMode interpolation)
  760. : size_(size), interpolation_(interpolation) {}
  761. std::vector<int32_t> size_;
  762. InterpolationMode interpolation_;
  763. };
  764. Resize::Resize(std::vector<int32_t> size, InterpolationMode interpolation)
  765. : data_(std::make_shared<Data>(size, interpolation)) {}
  766. std::shared_ptr<TensorOperation> Resize::Parse() {
  767. return std::make_shared<ResizeOperation>(data_->size_, data_->interpolation_);
  768. }
  769. std::shared_ptr<TensorOperation> Resize::Parse(const MapTargetDevice &env) {
  770. if (env == MapTargetDevice::kAscend310) {
  771. #ifdef ENABLE_ACL
  772. std::vector<uint32_t> usize_;
  773. usize_.reserve(data_->size_.size());
  774. std::transform(data_->size_.begin(), data_->size_.end(), std::back_inserter(usize_),
  775. [](int32_t i) { return (uint32_t)i; });
  776. return std::make_shared<DvppResizeJpegOperation>(usize_);
  777. #endif // ENABLE_ACL
  778. } else if (env == MapTargetDevice::kCpu) {
  779. return std::make_shared<ResizeOperation>(data_->size_, data_->interpolation_);
  780. }
  781. MS_LOG(ERROR) << "Unsupported MapTargetDevice, only supported kCpu and kAscend310.";
  782. return nullptr;
  783. }
  784. // ResizePreserveAR Transform Operation.
  785. struct ResizePreserveAR::Data {
  786. Data(int32_t height, int32_t width, int32_t img_orientation)
  787. : height_(height), width_(width), img_orientation_(img_orientation) {}
  788. int32_t height_;
  789. int32_t width_;
  790. int32_t img_orientation_;
  791. };
  792. ResizePreserveAR::ResizePreserveAR(int32_t height, int32_t width, int32_t img_orientation)
  793. : data_(std::make_shared<Data>(height, width, img_orientation)) {}
  794. std::shared_ptr<TensorOperation> ResizePreserveAR::Parse() {
  795. return std::make_shared<ResizePreserveAROperation>(data_->height_, data_->width_, data_->img_orientation_);
  796. }
  797. // Rotate Transform Operation.
  798. struct Rotate::Data {
  799. Data(const float &degrees, InterpolationMode resample, bool expand, const std::vector<float> &center,
  800. const std::vector<uint8_t> &fill_value)
  801. : degrees_(degrees), interpolation_mode_(resample), center_(center), expand_(expand), fill_value_(fill_value) {}
  802. explicit Data(const FixRotationAngle &angle_id) : angle_id_(angle_id), lite_impl_(true) {}
  803. FixRotationAngle angle_id_{FixRotationAngle::k0Degree};
  804. bool lite_impl_{false};
  805. float degrees_{0};
  806. InterpolationMode interpolation_mode_{InterpolationMode::kNearestNeighbour};
  807. std::vector<float> center_{{}};
  808. bool expand_{false};
  809. std::vector<uint8_t> fill_value_{0, 0, 0};
  810. };
  811. Rotate::Rotate(FixRotationAngle angle_id) : data_(std::make_shared<Data>(angle_id)) {}
  812. Rotate::Rotate(float degrees, InterpolationMode resample, bool expand, std::vector<float> center,
  813. std::vector<uint8_t> fill_value)
  814. : data_(std::make_shared<Data>(degrees, resample, expand, center, fill_value)) {}
  815. std::shared_ptr<TensorOperation> Rotate::Parse() {
  816. #ifndef ENABLE_ANDROID
  817. if (!data_->lite_impl_) {
  818. return std::make_shared<RotateOperation>(data_->degrees_, data_->interpolation_mode_, data_->expand_,
  819. data_->center_, data_->fill_value_);
  820. }
  821. #else
  822. if (data_->lite_impl_) {
  823. return std::make_shared<RotateOperation>(data_->angle_id_);
  824. }
  825. #endif // not ENABLE_ANDROID
  826. std::string platform = data_->lite_impl_ ? "Cloud" : "Android";
  827. MS_LOG(ERROR) << "This Rotate API is not supported for " + platform + ", use another Rotate API.";
  828. return nullptr;
  829. }
  830. #ifndef ENABLE_ANDROID
  831. // ResizeWithBBox Transform Operation.
  832. struct ResizeWithBBox::Data {
  833. Data(const std::vector<int32_t> &size, InterpolationMode interpolation)
  834. : size_(size), interpolation_(interpolation) {}
  835. std::vector<int32_t> size_;
  836. InterpolationMode interpolation_;
  837. };
  838. ResizeWithBBox::ResizeWithBBox(std::vector<int32_t> size, InterpolationMode interpolation)
  839. : data_(std::make_shared<Data>(size, interpolation)) {}
  840. std::shared_ptr<TensorOperation> ResizeWithBBox::Parse() {
  841. return std::make_shared<ResizeWithBBoxOperation>(data_->size_, data_->interpolation_);
  842. }
  843. #endif // not ENABLE_ANDROID
  844. // RGB2BGR Transform Operation.
  845. std::shared_ptr<TensorOperation> RGB2BGR::Parse() { return std::make_shared<RgbToBgrOperation>(); }
  846. // RGB2GRAY Transform Operation.
  847. std::shared_ptr<TensorOperation> RGB2GRAY::Parse() { return std::make_shared<RgbToGrayOperation>(); }
  848. #ifndef ENABLE_ANDROID
  849. // RgbaToBgr Transform Operation.
  850. RGBA2BGR::RGBA2BGR() {}
  851. std::shared_ptr<TensorOperation> RGBA2BGR::Parse() { return std::make_shared<RgbaToBgrOperation>(); }
  852. // RgbaToRgb Transform Operation.
  853. RGBA2RGB::RGBA2RGB() {}
  854. std::shared_ptr<TensorOperation> RGBA2RGB::Parse() { return std::make_shared<RgbaToRgbOperation>(); }
  855. // SlicePatches Transform Operation.
  856. struct SlicePatches::Data {
  857. Data(int32_t num_height, int32_t num_width, SliceMode slice_mode, uint8_t fill_value)
  858. : num_height_(num_height), num_width_(num_width), slice_mode_(slice_mode), fill_value_(fill_value) {}
  859. int32_t num_height_;
  860. int32_t num_width_;
  861. SliceMode slice_mode_;
  862. uint8_t fill_value_;
  863. };
  864. SlicePatches::SlicePatches(int32_t num_height, int32_t num_width, SliceMode slice_mode, uint8_t fill_value)
  865. : data_(std::make_shared<Data>(num_height, num_width, slice_mode, fill_value)) {}
  866. std::shared_ptr<TensorOperation> SlicePatches::Parse() {
  867. return std::make_shared<SlicePatchesOperation>(data_->num_height_, data_->num_width_, data_->slice_mode_,
  868. data_->fill_value_);
  869. }
  870. // SoftDvppDecodeRandomCropResizeJpeg Transform Operation.
  871. struct SoftDvppDecodeRandomCropResizeJpeg::Data {
  872. Data(const std::vector<int32_t> &size, const std::vector<float> &scale, const std::vector<float> &ratio,
  873. int32_t max_attempts)
  874. : size_(size), scale_(scale), ratio_(ratio), max_attempts_(max_attempts) {}
  875. std::vector<int32_t> size_;
  876. std::vector<float> scale_;
  877. std::vector<float> ratio_;
  878. int32_t max_attempts_;
  879. };
  880. SoftDvppDecodeRandomCropResizeJpeg::SoftDvppDecodeRandomCropResizeJpeg(std::vector<int32_t> size,
  881. std::vector<float> scale,
  882. std::vector<float> ratio, int32_t max_attempts)
  883. : data_(std::make_shared<Data>(size, scale, ratio, max_attempts)) {}
  884. std::shared_ptr<TensorOperation> SoftDvppDecodeRandomCropResizeJpeg::Parse() {
  885. return std::make_shared<SoftDvppDecodeRandomCropResizeJpegOperation>(data_->size_, data_->scale_, data_->ratio_,
  886. data_->max_attempts_);
  887. }
  888. // SoftDvppDecodeResizeJpeg Transform Operation.
  889. struct SoftDvppDecodeResizeJpeg::Data {
  890. explicit Data(const std::vector<int32_t> &size) : size_(size) {}
  891. std::vector<int32_t> size_;
  892. };
  893. SoftDvppDecodeResizeJpeg::SoftDvppDecodeResizeJpeg(std::vector<int32_t> size) : data_(std::make_shared<Data>(size)) {}
  894. std::shared_ptr<TensorOperation> SoftDvppDecodeResizeJpeg::Parse() {
  895. return std::make_shared<SoftDvppDecodeResizeJpegOperation>(data_->size_);
  896. }
  897. // SwapRedBlue Transform Operation.
  898. SwapRedBlue::SwapRedBlue() {}
  899. std::shared_ptr<TensorOperation> SwapRedBlue::Parse() { return std::make_shared<SwapRedBlueOperation>(); }
  900. // UniformAug Transform Operation.
  901. struct UniformAugment::Data {
  902. std::vector<std::shared_ptr<TensorOperation>> transforms_;
  903. int32_t num_ops_;
  904. };
  905. UniformAugment::UniformAugment(const std::vector<TensorTransform *> &transforms, int32_t num_ops)
  906. : data_(std::make_shared<Data>()) {
  907. (void)std::transform(
  908. transforms.begin(), transforms.end(), std::back_inserter(data_->transforms_),
  909. [](TensorTransform *const op) -> std::shared_ptr<TensorOperation> { return op ? op->Parse() : nullptr; });
  910. data_->num_ops_ = num_ops;
  911. }
  912. UniformAugment::UniformAugment(const std::vector<std::shared_ptr<TensorTransform>> &transforms, int32_t num_ops)
  913. : data_(std::make_shared<Data>()) {
  914. (void)std::transform(transforms.begin(), transforms.end(), std::back_inserter(data_->transforms_),
  915. [](const std::shared_ptr<TensorTransform> op) -> std::shared_ptr<TensorOperation> {
  916. return op ? op->Parse() : nullptr;
  917. });
  918. data_->num_ops_ = num_ops;
  919. }
  920. UniformAugment::UniformAugment(const std::vector<std::reference_wrapper<TensorTransform>> &transforms, int32_t num_ops)
  921. : data_(std::make_shared<Data>()) {
  922. (void)std::transform(transforms.begin(), transforms.end(), std::back_inserter(data_->transforms_),
  923. [](TensorTransform &op) -> std::shared_ptr<TensorOperation> { return op.Parse(); });
  924. data_->num_ops_ = num_ops;
  925. }
  926. std::shared_ptr<TensorOperation> UniformAugment::Parse() {
  927. return std::make_shared<UniformAugOperation>(data_->transforms_, data_->num_ops_);
  928. }
  929. // VerticalFlip Transform Operation.
  930. VerticalFlip::VerticalFlip() {}
  931. std::shared_ptr<TensorOperation> VerticalFlip::Parse() { return std::make_shared<VerticalFlipOperation>(); }
  932. #endif // not ENABLE_ANDROID
  933. } // namespace vision
  934. } // namespace dataset
  935. } // namespace mindspore