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

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