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

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