• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright 2020-2024 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 
17 #include "minddata/dataset/include/dataset/vision.h"
18 #if defined(WITH_BACKEND) || defined(ENABLE_ACL) || defined(ENABLE_DVPP)
19 #include "minddata/dataset/include/dataset/vision_ascend.h"
20 #endif
21 #include "minddata/dataset/kernels/ir/vision/ascend_vision_ir.h"
22 
23 #include "minddata/dataset/kernels/ir/vision/adjust_brightness_ir.h"
24 #include "minddata/dataset/kernels/ir/vision/adjust_contrast_ir.h"
25 #include "minddata/dataset/kernels/ir/vision/adjust_gamma_ir.h"
26 #include "minddata/dataset/kernels/ir/vision/adjust_hue_ir.h"
27 #include "minddata/dataset/kernels/ir/vision/adjust_saturation_ir.h"
28 #include "minddata/dataset/kernels/ir/vision/adjust_sharpness_ir.h"
29 #include "minddata/dataset/kernels/ir/vision/affine_ir.h"
30 #include "minddata/dataset/kernels/ir/vision/auto_augment_ir.h"
31 #include "minddata/dataset/kernels/ir/vision/auto_contrast_ir.h"
32 #include "minddata/dataset/kernels/ir/vision/bounding_box_augment_ir.h"
33 #include "minddata/dataset/kernels/ir/vision/center_crop_ir.h"
34 #include "minddata/dataset/kernels/ir/vision/convert_color_ir.h"
35 #include "minddata/dataset/kernels/ir/vision/crop_ir.h"
36 #include "minddata/dataset/kernels/ir/vision/cutmix_batch_ir.h"
37 #include "minddata/dataset/kernels/ir/vision/cutout_ir.h"
38 #include "minddata/dataset/kernels/ir/vision/decode_ir.h"
39 #include "minddata/dataset/kernels/ir/vision/decode_video_ir.h"
40 #include "minddata/dataset/kernels/ir/vision/equalize_ir.h"
41 #include "minddata/dataset/kernels/ir/vision/erase_ir.h"
42 #include "minddata/dataset/kernels/ir/vision/gaussian_blur_ir.h"
43 #include "minddata/dataset/kernels/ir/vision/horizontal_flip_ir.h"
44 #include "minddata/dataset/kernels/ir/vision/hwc_to_chw_ir.h"
45 #include "minddata/dataset/kernels/ir/vision/invert_ir.h"
46 #include "minddata/dataset/kernels/ir/vision/mixup_batch_ir.h"
47 #include "minddata/dataset/kernels/ir/vision/normalize_ir.h"
48 #include "minddata/dataset/kernels/ir/vision/normalize_pad_ir.h"
49 #include "minddata/dataset/kernels/ir/vision/pad_to_size_ir.h"
50 #include "minddata/dataset/kernels/ir/vision/perspective_ir.h"
51 #include "minddata/dataset/kernels/ir/vision/posterize_ir.h"
52 #include "minddata/dataset/kernels/ir/vision/rand_augment_ir.h"
53 #include "minddata/dataset/kernels/ir/vision/random_adjust_sharpness_ir.h"
54 #include "minddata/dataset/kernels/ir/vision/random_affine_ir.h"
55 #include "minddata/dataset/kernels/ir/vision/random_auto_contrast_ir.h"
56 #include "minddata/dataset/kernels/ir/vision/random_color_adjust_ir.h"
57 #include "minddata/dataset/kernels/ir/vision/random_color_ir.h"
58 #include "minddata/dataset/kernels/ir/vision/random_crop_decode_resize_ir.h"
59 #include "minddata/dataset/kernels/ir/vision/random_crop_ir.h"
60 #include "minddata/dataset/kernels/ir/vision/random_crop_with_bbox_ir.h"
61 #include "minddata/dataset/kernels/ir/vision/random_equalize_ir.h"
62 #include "minddata/dataset/kernels/ir/vision/random_horizontal_flip_ir.h"
63 #include "minddata/dataset/kernels/ir/vision/random_horizontal_flip_with_bbox_ir.h"
64 #include "minddata/dataset/kernels/ir/vision/random_invert_ir.h"
65 #include "minddata/dataset/kernels/ir/vision/random_lighting_ir.h"
66 #include "minddata/dataset/kernels/ir/vision/random_posterize_ir.h"
67 #include "minddata/dataset/kernels/ir/vision/random_resize_ir.h"
68 #include "minddata/dataset/kernels/ir/vision/random_resize_with_bbox_ir.h"
69 #include "minddata/dataset/kernels/ir/vision/random_resized_crop_ir.h"
70 #include "minddata/dataset/kernels/ir/vision/random_resized_crop_with_bbox_ir.h"
71 #include "minddata/dataset/kernels/ir/vision/random_rotation_ir.h"
72 #include "minddata/dataset/kernels/ir/vision/random_select_subpolicy_ir.h"
73 #include "minddata/dataset/kernels/ir/vision/random_sharpness_ir.h"
74 #include "minddata/dataset/kernels/ir/vision/random_solarize_ir.h"
75 #include "minddata/dataset/kernels/ir/vision/random_vertical_flip_ir.h"
76 #include "minddata/dataset/kernels/ir/vision/random_vertical_flip_with_bbox_ir.h"
77 #include "minddata/dataset/kernels/ir/vision/resize_ir.h"
78 #include "minddata/dataset/kernels/ir/vision/resize_preserve_ar_ir.h"
79 #include "minddata/dataset/kernels/ir/vision/resize_with_bbox_ir.h"
80 #include "minddata/dataset/kernels/ir/vision/resized_crop_ir.h"
81 #include "minddata/dataset/kernels/ir/vision/rgb_to_bgr_ir.h"
82 #include "minddata/dataset/kernels/ir/vision/rgb_to_gray_ir.h"
83 #include "minddata/dataset/kernels/ir/vision/rgba_to_bgr_ir.h"
84 #include "minddata/dataset/kernels/ir/vision/rgba_to_rgb_ir.h"
85 #include "minddata/dataset/kernels/ir/vision/rotate_ir.h"
86 #include "minddata/dataset/kernels/ir/vision/slice_patches_ir.h"
87 #include "minddata/dataset/kernels/ir/vision/solarize_ir.h"
88 #include "minddata/dataset/kernels/ir/vision/to_tensor_ir.h"
89 #include "minddata/dataset/kernels/ir/vision/trivial_augment_wide_ir.h"
90 #include "minddata/dataset/kernels/ir/vision/uniform_aug_ir.h"
91 #include "minddata/dataset/kernels/ir/vision/vertical_flip_ir.h"
92 #include "minddata/dataset/util/log_adapter.h"
93 
94 #if !defined(ENABLE_ANDROID) || defined(ENABLE_MINDDATA_PYTHON)
95 #include "minddata/dataset/kernels/ir/vision/pad_ir.h"
96 #include "minddata/dataset/kernels/ir/vision/rescale_ir.h"
97 #include "minddata/dataset/kernels/ir/vision/swap_red_blue_ir.h"
98 #endif
99 
100 #ifndef ENABLE_ANDROID
101 #include "minddata/dataset/kernels/image/image_utils.h"
102 #include "minddata/dataset/kernels/image/video_utils.h"
103 #endif
104 #include "minddata/dataset/kernels/ir/validators.h"
105 
106 // Typecast between mindspore::DataType and dataset::DataType
107 #include "minddata/dataset/core/type_id.h"
108 #include "mindspore/core/ir/dtype/type_id.h"
109 
110 namespace mindspore {
111 namespace dataset {
112 // Transform operations for computer vision.
113 namespace vision {
114 // CONSTRUCTORS FOR API CLASSES TO CREATE VISION TENSOR TRANSFORM OPERATIONS
115 // (In alphabetical order)
116 
117 // Affine Transform Operation.
118 struct Affine::Data {
Datamindspore::dataset::vision::Affine::Data119   Data(float_t degrees, const std::vector<float> &translation, float scale, const std::vector<float> &shear,
120        InterpolationMode interpolation, const std::vector<uint8_t> &fill_value)
121       : degrees_(degrees),
122         translation_(translation),
123         scale_(scale),
124         shear_(shear),
125         interpolation_(interpolation),
126         fill_value_(fill_value) {}
127   float degrees_;
128   std::vector<float> translation_;
129   float scale_;
130   std::vector<float> shear_;
131   InterpolationMode interpolation_;
132   std::vector<uint8_t> fill_value_;
133 };
134 
Affine(float_t degrees,const std::vector<float> & translation,float scale,const std::vector<float> & shear,InterpolationMode interpolation,const std::vector<uint8_t> & fill_value)135 Affine::Affine(float_t degrees, const std::vector<float> &translation, float scale, const std::vector<float> &shear,
136                InterpolationMode interpolation, const std::vector<uint8_t> &fill_value)
137     : data_(std::make_shared<Data>(degrees, translation, scale, shear, interpolation, fill_value)) {}
138 
Parse()139 std::shared_ptr<TensorOperation> Affine::Parse() {
140   return std::make_shared<AffineOperation>(data_->degrees_, data_->translation_, data_->scale_, data_->shear_,
141                                            data_->interpolation_, data_->fill_value_);
142 }
143 
144 #ifndef ENABLE_ANDROID
145 // AdjustBrightness Transform Operation.
146 struct AdjustBrightness::Data {
Datamindspore::dataset::vision::AdjustBrightness::Data147   explicit Data(float brightness_factor) : brightness_factor_(brightness_factor) {}
148   float brightness_factor_;
149 };
150 
AdjustBrightness(float brightness_factor)151 AdjustBrightness::AdjustBrightness(float brightness_factor) : data_(std::make_shared<Data>(brightness_factor)) {}
152 
Parse()153 std::shared_ptr<TensorOperation> AdjustBrightness::Parse() {
154   return std::make_shared<AdjustBrightnessOperation>(data_->brightness_factor_);
155 }
156 
157 // AdjustContrast Transform Operation.
158 struct AdjustContrast::Data {
Datamindspore::dataset::vision::AdjustContrast::Data159   explicit Data(float contrast_factor) : contrast_factor_(contrast_factor) {}
160   float contrast_factor_;
161 };
162 
AdjustContrast(float contrast_factor)163 AdjustContrast::AdjustContrast(float contrast_factor) : data_(std::make_shared<Data>(contrast_factor)) {}
164 
Parse()165 std::shared_ptr<TensorOperation> AdjustContrast::Parse() {
166   return std::make_shared<AdjustContrastOperation>(data_->contrast_factor_);
167 }
168 
169 // AdjustGamma Transform Operation.
170 struct AdjustGamma::Data {
Datamindspore::dataset::vision::AdjustGamma::Data171   Data(float gamma, float gain) : gamma_(gamma), gain_(gain) {}
172   float gamma_;
173   float gain_;
174 };
175 
AdjustGamma(float gamma,float gain)176 AdjustGamma::AdjustGamma(float gamma, float gain) : data_(std::make_shared<Data>(gamma, gain)) {}
177 
Parse()178 std::shared_ptr<TensorOperation> AdjustGamma::Parse() {
179   return std::make_shared<AdjustGammaOperation>(data_->gamma_, data_->gain_);
180 }
181 
182 // AdjustHue Transform Operation.
183 struct AdjustHue::Data {
Datamindspore::dataset::vision::AdjustHue::Data184   explicit Data(float hue_factor) : hue_factor_(hue_factor) {}
185   float hue_factor_;
186 };
187 
AdjustHue(float hue_factor)188 AdjustHue::AdjustHue(float hue_factor) : data_(std::make_shared<Data>(hue_factor)) {}
189 
Parse()190 std::shared_ptr<TensorOperation> AdjustHue::Parse() { return std::make_shared<AdjustHueOperation>(data_->hue_factor_); }
191 
192 // AdjustSaturation Transform Operation.
193 struct AdjustSaturation::Data {
Datamindspore::dataset::vision::AdjustSaturation::Data194   explicit Data(float saturation_factor) : saturation_factor_(saturation_factor) {}
195   float saturation_factor_;
196 };
197 
AdjustSaturation(float saturation_factor)198 AdjustSaturation::AdjustSaturation(float saturation_factor) : data_(std::make_shared<Data>(saturation_factor)) {}
199 
Parse()200 std::shared_ptr<TensorOperation> AdjustSaturation::Parse() {
201   return std::make_shared<AdjustSaturationOperation>(data_->saturation_factor_);
202 }
203 
204 // AdjustSharpness Transform Operation.
205 struct AdjustSharpness::Data {
Datamindspore::dataset::vision::AdjustSharpness::Data206   explicit Data(float sharpness_factor) : sharpness_factor_(sharpness_factor) {}
207   float sharpness_factor_;
208 };
209 
AdjustSharpness(float sharpness_factor)210 AdjustSharpness::AdjustSharpness(float sharpness_factor) : data_(std::make_shared<Data>(sharpness_factor)) {}
211 
Parse()212 std::shared_ptr<TensorOperation> AdjustSharpness::Parse() {
213   return std::make_shared<AdjustSharpnessOperation>(data_->sharpness_factor_);
214 }
215 
216 // AutoAugment Transform Operation.
217 struct AutoAugment::Data {
Datamindspore::dataset::vision::AutoAugment::Data218   Data(AutoAugmentPolicy policy, InterpolationMode interpolation, const std::vector<uint8_t> &fill_value)
219       : policy_(policy), interpolation_(interpolation), fill_value_(fill_value) {}
220   AutoAugmentPolicy policy_;
221   InterpolationMode interpolation_;
222   std::vector<uint8_t> fill_value_;
223 };
224 
AutoAugment(AutoAugmentPolicy policy,InterpolationMode interpolation,const std::vector<uint8_t> & fill_value)225 AutoAugment::AutoAugment(AutoAugmentPolicy policy, InterpolationMode interpolation,
226                          const std::vector<uint8_t> &fill_value)
227     : data_(std::make_shared<Data>(policy, interpolation, fill_value)) {}
228 
Parse()229 std::shared_ptr<TensorOperation> AutoAugment::Parse() {
230   return std::make_shared<AutoAugmentOperation>(data_->policy_, data_->interpolation_, data_->fill_value_);
231 }
232 
233 // AutoContrast Transform Operation.
234 struct AutoContrast::Data {
Datamindspore::dataset::vision::AutoContrast::Data235   Data(float cutoff, const std::vector<uint32_t> &ignore) : cutoff_(cutoff), ignore_(ignore) {}
236   float cutoff_;
237   std::vector<uint32_t> ignore_;
238 };
239 
AutoContrast(float cutoff,const std::vector<uint32_t> & ignore)240 AutoContrast::AutoContrast(float cutoff, const std::vector<uint32_t> &ignore)
241     : data_(std::make_shared<Data>(cutoff, ignore)) {}
242 
Parse()243 std::shared_ptr<TensorOperation> AutoContrast::Parse() {
244   return std::make_shared<AutoContrastOperation>(data_->cutoff_, data_->ignore_);
245 }
246 
247 // BoundingBoxAugment Transform Operation.
248 struct BoundingBoxAugment::Data {
249   std::shared_ptr<TensorOperation> transform_;
250   float ratio_;
251 };
252 
BoundingBoxAugment(TensorTransform * transform,float ratio)253 BoundingBoxAugment::BoundingBoxAugment(TensorTransform *transform, float ratio) : data_(std::make_shared<Data>()) {
254   data_->transform_ = transform ? transform->Parse() : nullptr;
255   data_->ratio_ = ratio;
256 }
257 
BoundingBoxAugment(const std::shared_ptr<TensorTransform> & transform,float ratio)258 BoundingBoxAugment::BoundingBoxAugment(const std::shared_ptr<TensorTransform> &transform, float ratio)
259     : data_(std::make_shared<Data>()) {
260   data_->transform_ = transform ? transform->Parse() : nullptr;
261   data_->ratio_ = ratio;
262 }
263 
BoundingBoxAugment(const std::reference_wrapper<TensorTransform> & transform,float ratio)264 BoundingBoxAugment::BoundingBoxAugment(const std::reference_wrapper<TensorTransform> &transform, float ratio)
265     : data_(std::make_shared<Data>()) {
266   data_->transform_ = transform.get().Parse();
267   data_->ratio_ = ratio;
268 }
269 
Parse()270 std::shared_ptr<TensorOperation> BoundingBoxAugment::Parse() {
271   return std::make_shared<BoundingBoxAugmentOperation>(data_->transform_, data_->ratio_);
272 }
273 #endif  // not ENABLE_ANDROID
274 
275 // CenterCrop Transform Operation.
276 struct CenterCrop::Data {
Datamindspore::dataset::vision::CenterCrop::Data277   explicit Data(const std::vector<int32_t> &size) : size_(size) {}
278   std::vector<int32_t> size_;
279 };
280 
CenterCrop(const std::vector<int32_t> & size)281 CenterCrop::CenterCrop(const std::vector<int32_t> &size) : data_(std::make_shared<Data>(size)) {}
282 
Parse()283 std::shared_ptr<TensorOperation> CenterCrop::Parse() { return std::make_shared<CenterCropOperation>(data_->size_); }
284 
Parse(const MapTargetDevice & env)285 std::shared_ptr<TensorOperation> CenterCrop::Parse(const MapTargetDevice &env) {
286   if (env == MapTargetDevice::kAscend310) {
287 #if defined(WITH_BACKEND) || defined(ENABLE_ACL) || defined(ENABLE_DVPP)
288     std::vector<uint32_t> usize_;
289     usize_.reserve(data_->size_.size());
290     std::transform(data_->size_.begin(), data_->size_.end(), std::back_inserter(usize_),
291                    [](int32_t i) { return (uint32_t)i; });
292     return std::make_shared<DvppCropJpegOperation>(usize_);
293 #endif
294   } else if (env == MapTargetDevice::kCpu) {
295     return std::make_shared<CenterCropOperation>(data_->size_);
296   }
297   MS_LOG(ERROR) << "Unsupported MapTargetDevice, only supported kCpu and kAscend310.";
298   return nullptr;
299 }
300 
301 #ifndef ENABLE_ANDROID
302 // ConvertColor Transform Operation.
303 struct ConvertColor::Data {
Datamindspore::dataset::vision::ConvertColor::Data304   explicit Data(ConvertMode convert_mode) : convert_mode_(convert_mode) {}
305   ConvertMode convert_mode_;
306 };
307 
ConvertColor(ConvertMode convert_mode)308 ConvertColor::ConvertColor(ConvertMode convert_mode) : data_(std::make_shared<Data>(convert_mode)) {}
309 
Parse()310 std::shared_ptr<TensorOperation> ConvertColor::Parse() {
311   return std::make_shared<ConvertColorOperation>(data_->convert_mode_);
312 }
313 #endif  // not ENABLE_ANDROID
314 
315 // Crop Transform Operation.
316 struct Crop::Data {
Datamindspore::dataset::vision::Crop::Data317   Data(const std::vector<int32_t> &coordinates, const std::vector<int32_t> &size)
318       : coordinates_(coordinates), size_(size) {}
319   std::vector<int32_t> coordinates_;
320   std::vector<int32_t> size_;
321 };
322 
Crop(const std::vector<int32_t> & coordinates,const std::vector<int32_t> & size)323 Crop::Crop(const std::vector<int32_t> &coordinates, const std::vector<int32_t> &size)
324     : data_(std::make_shared<Data>(coordinates, size)) {}
325 
Parse()326 std::shared_ptr<TensorOperation> Crop::Parse() {
327   return std::make_shared<CropOperation>(data_->coordinates_, data_->size_);
328 }
329 
330 #ifndef ENABLE_ANDROID
331 // CutMixBatch Transform Operation.
332 struct CutMixBatch::Data {
Datamindspore::dataset::vision::CutMixBatch::Data333   Data(ImageBatchFormat image_batch_format, float alpha, float prob)
334       : image_batch_format_(image_batch_format), alpha_(alpha), prob_(prob) {}
335   float alpha_;
336   float prob_;
337   ImageBatchFormat image_batch_format_;
338 };
339 
CutMixBatch(ImageBatchFormat image_batch_format,float alpha,float prob)340 CutMixBatch::CutMixBatch(ImageBatchFormat image_batch_format, float alpha, float prob)
341     : data_(std::make_shared<Data>(image_batch_format, alpha, prob)) {}
342 
Parse()343 std::shared_ptr<TensorOperation> CutMixBatch::Parse() {
344   return std::make_shared<CutMixBatchOperation>(data_->image_batch_format_, data_->alpha_, data_->prob_);
345 }
346 
347 // CutOutOp.
348 struct CutOut::Data {
Datamindspore::dataset::vision::CutOut::Data349   Data(int32_t length, int32_t num_patches, bool is_hwc)
350       : length_(length), num_patches_(num_patches), is_hwc_(is_hwc) {}
351   int32_t length_;
352   int32_t num_patches_;
353   bool is_hwc_;
354 };
355 
CutOut(int32_t length,int32_t num_patches,bool is_hwc)356 CutOut::CutOut(int32_t length, int32_t num_patches, bool is_hwc)
357     : data_(std::make_shared<Data>(length, num_patches, is_hwc)) {}
358 
Parse()359 std::shared_ptr<TensorOperation> CutOut::Parse() {
360   return std::make_shared<CutOutOperation>(data_->length_, data_->num_patches_, data_->is_hwc_);
361 }
362 #endif  // not ENABLE_ANDROID
363 
364 // Decode Transform Operation.
365 struct Decode::Data {
Datamindspore::dataset::vision::Decode::Data366   explicit Data(bool rgb) : rgb_(rgb) {}
367   bool rgb_;
368 };
369 
Decode(bool rgb)370 Decode::Decode(bool rgb) : data_(std::make_shared<Data>(rgb)) {}
371 
Parse()372 std::shared_ptr<TensorOperation> Decode::Parse() { return std::make_shared<DecodeOperation>(data_->rgb_); }
373 
Parse(const MapTargetDevice & env)374 std::shared_ptr<TensorOperation> Decode::Parse(const MapTargetDevice &env) {
375   if (env == MapTargetDevice::kAscend310) {
376 #if defined(WITH_BACKEND) || defined(ENABLE_ACL) || defined(ENABLE_DVPP)
377     return std::make_shared<DvppDecodeJpegOperation>();
378 #endif
379   } else if (env == MapTargetDevice::kCpu) {
380     return std::make_shared<DecodeOperation>(data_->rgb_);
381   }
382   MS_LOG(ERROR) << "Unsupported MapTargetDevice, only supported kCpu and kAscend310.";
383   return nullptr;
384 }
385 
386 #ifndef ENABLE_ANDROID
387 // DecodeVideo Transform Operation.
DecodeVideo()388 DecodeVideo::DecodeVideo() {}
389 
Parse()390 std::shared_ptr<TensorOperation> DecodeVideo::Parse() { return std::make_shared<DecodeVideoOperation>(); }
391 #endif  // not ENABLE_ANDROID
392 
393 #if defined(WITH_BACKEND) || defined(ENABLE_ACL) || defined(ENABLE_DVPP)
394 // DvppDecodeVideo Transform Operation.
395 struct DvppDecodeVideo::Data {
Datamindspore::dataset::vision::DvppDecodeVideo::Data396   Data(const std::vector<uint32_t> &size, VdecStreamFormat type, VdecOutputFormat out_format, const std::string &output)
397       : size_(size), format_(out_format), en_type_(type), output_(output) {}
398 
399   std::vector<uint32_t> size_;
400   VdecOutputFormat format_;
401   VdecStreamFormat en_type_;
402   std::string output_;
403 };
404 
DvppDecodeVideo(const std::vector<uint32_t> & size,VdecStreamFormat type,VdecOutputFormat out_format,const std::vector<char> & output)405 DvppDecodeVideo::DvppDecodeVideo(const std::vector<uint32_t> &size, VdecStreamFormat type, VdecOutputFormat out_format,
406                                  const std::vector<char> &output)
407     : data_(std::make_shared<Data>(size, type, out_format, CharToString(output))) {}
408 
Parse()409 std::shared_ptr<TensorOperation> DvppDecodeVideo::Parse() {
410   return std::make_shared<DvppDecodeVideoOperation>(data_->size_, data_->en_type_, data_->format_, data_->output_);
411 }
412 
Parse(const MapTargetDevice & env)413 std::shared_ptr<TensorOperation> DvppDecodeVideo::Parse(const MapTargetDevice &env) {
414   if (env == MapTargetDevice::kAscend310) {
415     return std::make_shared<DvppDecodeVideoOperation>(data_->size_, data_->en_type_, data_->format_, data_->output_);
416   }
417   MS_LOG(ERROR) << "Unsupported MapTargetDevice, only kAscend310 is supported.";
418   return nullptr;
419 }
420 
421 // DvppDecodeResize Transform Operation.
422 struct DvppDecodeResizeJpeg::Data {
Datamindspore::dataset::vision::DvppDecodeResizeJpeg::Data423   explicit Data(const std::vector<uint32_t> &resize) : resize_(resize) {}
424   std::vector<uint32_t> resize_;
425 };
426 
DvppDecodeResizeJpeg(const std::vector<uint32_t> & resize)427 DvppDecodeResizeJpeg::DvppDecodeResizeJpeg(const std::vector<uint32_t> &resize)
428     : data_(std::make_shared<Data>(resize)) {}
429 
Parse()430 std::shared_ptr<TensorOperation> DvppDecodeResizeJpeg::Parse() {
431   return std::make_shared<DvppDecodeResizeOperation>(data_->resize_);
432 }
433 
Parse(const MapTargetDevice & env)434 std::shared_ptr<TensorOperation> DvppDecodeResizeJpeg::Parse(const MapTargetDevice &env) {
435   if (env == MapTargetDevice::kAscend310) {
436     return std::make_shared<DvppDecodeResizeOperation>(data_->resize_);
437   }
438   MS_LOG(ERROR) << "Unsupported MapTargetDevice, only supported kAscend310.";
439   return nullptr;
440 }
441 
442 // DvppDecodeResizeCrop Transform Operation.
443 struct DvppDecodeResizeCropJpeg::Data {
Datamindspore::dataset::vision::DvppDecodeResizeCropJpeg::Data444   Data(const std::vector<uint32_t> &crop, const std::vector<uint32_t> &resize) : crop_(crop), resize_(resize) {}
445   std::vector<uint32_t> crop_;
446   std::vector<uint32_t> resize_;
447 };
448 
DvppDecodeResizeCropJpeg(const std::vector<uint32_t> & crop,const std::vector<uint32_t> & resize)449 DvppDecodeResizeCropJpeg::DvppDecodeResizeCropJpeg(const std::vector<uint32_t> &crop,
450                                                    const std::vector<uint32_t> &resize)
451     : data_(std::make_shared<Data>(crop, resize)) {}
452 
Parse()453 std::shared_ptr<TensorOperation> DvppDecodeResizeCropJpeg::Parse() {
454   return std::make_shared<DvppDecodeResizeCropOperation>(data_->crop_, data_->resize_);
455 }
456 
Parse(const MapTargetDevice & env)457 std::shared_ptr<TensorOperation> DvppDecodeResizeCropJpeg::Parse(const MapTargetDevice &env) {
458   if (env == MapTargetDevice::kAscend310) {
459 #if defined(WITH_BACKEND) || defined(ENABLE_ACL)
460     return std::make_shared<DvppDecodeResizeCropOperation>(data_->crop_, data_->resize_);
461 #endif
462   }
463   MS_LOG(ERROR) << "Unsupported MapTargetDevice, only supported kAscend310.";
464   return nullptr;
465 }
466 
467 // DvppDecodePng Transform Operation.
DvppDecodePng()468 DvppDecodePng::DvppDecodePng() {}
469 
Parse()470 std::shared_ptr<TensorOperation> DvppDecodePng::Parse() { return std::make_shared<DvppDecodePngOperation>(); }
471 
Parse(const MapTargetDevice & env)472 std::shared_ptr<TensorOperation> DvppDecodePng::Parse(const MapTargetDevice &env) {
473   if (env == MapTargetDevice::kAscend310) {
474     return std::make_shared<DvppDecodePngOperation>();
475   }
476   MS_LOG(ERROR) << "Unsupported MapTargetDevice, only supported kAscend310.";
477   return nullptr;
478 }
479 #endif
480 #ifndef ENABLE_ANDROID
481 
482 // EncodeJpeg Function.
EncodeJpeg(const mindspore::MSTensor & image,mindspore::MSTensor * output,int quality)483 Status EncodeJpeg(const mindspore::MSTensor &image, mindspore::MSTensor *output, int quality) {
484   RETURN_UNEXPECTED_IF_NULL(output);
485   std::shared_ptr<dataset::Tensor> input;
486   RETURN_IF_NOT_OK(Tensor::CreateFromMSTensor(image, &input));
487   std::shared_ptr<dataset::Tensor> de_tensor;
488   RETURN_IF_NOT_OK(mindspore::dataset::EncodeJpeg(input, &de_tensor, quality));
489   CHECK_FAIL_RETURN_UNEXPECTED(de_tensor->HasData(),
490                                "EncodeJpeg: get an empty tensor with shape " + de_tensor->shape().ToString());
491   *output = mindspore::MSTensor(std::make_shared<DETensor>(de_tensor));
492   return Status::OK();
493 }
494 
495 // EncodePng Function.
EncodePng(const mindspore::MSTensor & image,mindspore::MSTensor * output,int compression_level)496 Status EncodePng(const mindspore::MSTensor &image, mindspore::MSTensor *output, int compression_level) {
497   RETURN_UNEXPECTED_IF_NULL(output);
498   std::shared_ptr<dataset::Tensor> input;
499   RETURN_IF_NOT_OK(Tensor::CreateFromMSTensor(image, &input));
500   TensorPtr de_tensor;
501   RETURN_IF_NOT_OK(mindspore::dataset::EncodePng(input, &de_tensor, compression_level));
502   CHECK_FAIL_RETURN_UNEXPECTED(de_tensor->HasData(),
503                                "EncodePng: get an empty tensor with shape " + de_tensor->shape().ToString());
504   *output = mindspore::MSTensor(std::make_shared<DETensor>(de_tensor));
505   return Status::OK();
506 }
507 
508 // Equalize Transform Operation.
509 Equalize::Equalize() = default;
510 
Parse()511 std::shared_ptr<TensorOperation> Equalize::Parse() { return std::make_shared<EqualizeOperation>(); }
512 
513 // Erase Operation.
514 struct Erase::Data {
Datamindspore::dataset::vision::Erase::Data515   Data(int32_t top, int32_t left, int32_t height, int32_t width, const std::vector<float> &value, bool inplace)
516       : top_(top), left_(left), height_(height), width_(width), value_(value), inplace_(inplace) {}
517   int32_t top_;
518   int32_t left_;
519   int32_t height_;
520   int32_t width_;
521   std::vector<float> value_;
522   bool inplace_;
523 };
524 
Erase(int32_t top,int32_t left,int32_t height,int32_t width,const std::vector<float> & value,bool inplace)525 Erase::Erase(int32_t top, int32_t left, int32_t height, int32_t width, const std::vector<float> &value, bool inplace)
526     : data_(std::make_shared<Data>(top, left, height, width, value, inplace)) {}
527 
Parse()528 std::shared_ptr<TensorOperation> Erase::Parse() {
529   return std::make_shared<EraseOperation>(data_->top_, data_->left_, data_->height_, data_->width_, data_->value_,
530                                           data_->inplace_);
531 }
532 #endif  // not ENABLE_ANDROID
533 
534 // GaussianBlur Transform Operation.
535 struct GaussianBlur::Data {
Datamindspore::dataset::vision::GaussianBlur::Data536   Data(const std::vector<int32_t> &kernel_size, const std::vector<float> &sigma)
537       : kernel_size_(kernel_size), sigma_(sigma) {}
538   std::vector<int32_t> kernel_size_;
539   std::vector<float> sigma_;
540 };
541 
GaussianBlur(const std::vector<int32_t> & kernel_size,const std::vector<float> & sigma)542 GaussianBlur::GaussianBlur(const std::vector<int32_t> &kernel_size, const std::vector<float> &sigma)
543     : data_(std::make_shared<Data>(kernel_size, sigma)) {}
544 
Parse()545 std::shared_ptr<TensorOperation> GaussianBlur::Parse() {
546   return std::make_shared<GaussianBlurOperation>(data_->kernel_size_, data_->sigma_);
547 }
548 
549 #ifndef ENABLE_ANDROID
550 // GetImageNumChannels Function.
GetImageNumChannels(const mindspore::MSTensor & image,dsize_t * channels)551 Status GetImageNumChannels(const mindspore::MSTensor &image, dsize_t *channels) {
552   RETURN_UNEXPECTED_IF_NULL(channels);
553   std::shared_ptr<dataset::Tensor> input;
554   Status rc = Tensor::CreateFromMSTensor(image, &input);
555   if (rc.IsError()) {
556     RETURN_STATUS_UNEXPECTED("GetImageNumChannels: failed to create image tensor.");
557   }
558   return ImageNumChannels(input, channels);
559 }
560 
561 // GetImageSize Function.
GetImageSize(const mindspore::MSTensor & image,std::vector<dsize_t> * size)562 Status GetImageSize(const mindspore::MSTensor &image, std::vector<dsize_t> *size) {
563   RETURN_UNEXPECTED_IF_NULL(size);
564   std::shared_ptr<Tensor> input;
565   Status rc = Tensor::CreateFromMSTensor(image, &input);
566   if (rc.IsError()) {
567     RETURN_STATUS_UNEXPECTED("GetImageSize: failed to create image tensor.");
568   }
569   return ImageSize(input, size);
570 }
571 
572 // HorizontalFlip Transform Operation.
573 HorizontalFlip::HorizontalFlip() = default;
574 
Parse()575 std::shared_ptr<TensorOperation> HorizontalFlip::Parse() { return std::make_shared<HorizontalFlipOperation>(); }
576 #endif  // not ENABLE_ANDROID
577 
578 // HwcToChw Transform Operation.
579 HWC2CHW::HWC2CHW() = default;
580 
Parse()581 std::shared_ptr<TensorOperation> HWC2CHW::Parse() { return std::make_shared<HwcToChwOperation>(); }
582 
583 #ifndef ENABLE_ANDROID
584 // Invert Transform Operation.
585 Invert::Invert() = default;
586 
Parse()587 std::shared_ptr<TensorOperation> Invert::Parse() { return std::make_shared<InvertOperation>(); }
588 
589 // MixUpBatch Transform Operation.
590 struct MixUpBatch::Data {
Datamindspore::dataset::vision::MixUpBatch::Data591   explicit Data(float alpha) : alpha_(alpha) {}
592   float alpha_;
593 };
594 
MixUpBatch(float alpha)595 MixUpBatch::MixUpBatch(float alpha) : data_(std::make_shared<Data>(alpha)) {}
596 
Parse()597 std::shared_ptr<TensorOperation> MixUpBatch::Parse() { return std::make_shared<MixUpBatchOperation>(data_->alpha_); }
598 #endif  // not ENABLE_ANDROID
599 
600 // Normalize Transform Operation.
601 struct Normalize::Data {
Datamindspore::dataset::vision::Normalize::Data602   Data(const std::vector<float> &mean, const std::vector<float> &std, bool is_hwc)
603       : mean_(mean), std_(std), is_hwc_(is_hwc) {}
604   std::vector<float> mean_;
605   std::vector<float> std_;
606   bool is_hwc_;
607 };
608 
Normalize(const std::vector<float> & mean,const std::vector<float> & std,bool is_hwc)609 Normalize::Normalize(const std::vector<float> &mean, const std::vector<float> &std, bool is_hwc)
610     : data_(std::make_shared<Data>(mean, std, is_hwc)) {}
611 
Parse()612 std::shared_ptr<TensorOperation> Normalize::Parse() {
613   return std::make_shared<NormalizeOperation>(data_->mean_, data_->std_, data_->is_hwc_);
614 }
615 
Parse(const MapTargetDevice & env)616 std::shared_ptr<TensorOperation> Normalize::Parse(const MapTargetDevice &env) {
617 #ifdef ENABLE_ANDROID
618   if (data_->is_hwc_ == false) {
619     MS_LOG(ERROR) << "Normalize op on Lite does not support 'is_hwc' = false.";
620     return nullptr;
621   }
622 #endif
623   if (env == MapTargetDevice::kAscend310) {
624 #if defined(WITH_BACKEND) || defined(ENABLE_ACL) || defined(ENABLE_DVPP)
625     return std::make_shared<DvppNormalizeOperation>(data_->mean_, data_->std_);
626 #endif
627   } else if (env == MapTargetDevice::kCpu) {
628     return std::make_shared<NormalizeOperation>(data_->mean_, data_->std_, data_->is_hwc_);
629   }
630   MS_LOG(ERROR) << "Unsupported MapTargetDevice, only supported kCpu and kAscend310.";
631   return nullptr;
632 }
633 
634 #ifndef ENABLE_ANDROID
635 // NormalizePad Transform Operation.
636 struct NormalizePad::Data {
Datamindspore::dataset::vision::NormalizePad::Data637   Data(const std::vector<float> &mean, const std::vector<float> &std, const std::string &dtype, bool is_hwc)
638       : mean_(mean), std_(std), dtype_(dtype), is_hwc_(is_hwc) {}
639   std::vector<float> mean_;
640   std::vector<float> std_;
641   std::string dtype_;
642   bool is_hwc_;
643 };
644 
NormalizePad(const std::vector<float> & mean,const std::vector<float> & std,const std::vector<char> & dtype,bool is_hwc)645 NormalizePad::NormalizePad(const std::vector<float> &mean, const std::vector<float> &std,
646                            const std::vector<char> &dtype, bool is_hwc)
647     : data_(std::make_shared<Data>(mean, std, CharToString(dtype), is_hwc)) {}
648 
Parse()649 std::shared_ptr<TensorOperation> NormalizePad::Parse() {
650   return std::make_shared<NormalizePadOperation>(data_->mean_, data_->std_, data_->dtype_, data_->is_hwc_);
651 }
652 #endif  // not ENABLE_ANDROID
653 
654 // Pad Transform Operation.
655 struct Pad::Data {
Datamindspore::dataset::vision::Pad::Data656   Data(const std::vector<int32_t> &padding, const std::vector<uint8_t> &fill_value, BorderType padding_mode)
657       : padding_(padding), fill_value_(fill_value), padding_mode_(padding_mode) {}
658   std::vector<int32_t> padding_;
659   std::vector<uint8_t> fill_value_;
660   BorderType padding_mode_;
661 };
662 
Pad(const std::vector<int32_t> & padding,const std::vector<uint8_t> & fill_value,BorderType padding_mode)663 Pad::Pad(const std::vector<int32_t> &padding, const std::vector<uint8_t> &fill_value, BorderType padding_mode)
664     : data_(std::make_shared<Data>(padding, fill_value, padding_mode)) {}
665 
Parse()666 std::shared_ptr<TensorOperation> Pad::Parse() {
667 #if !defined(ENABLE_ANDROID) || defined(ENABLE_MINDDATA_PYTHON)
668   return std::make_shared<PadOperation>(data_->padding_, data_->fill_value_, data_->padding_mode_);
669 #else
670   MS_LOG(ERROR) << "Unsupported Pad.";
671   return nullptr;
672 #endif
673 }
674 
675 #ifndef ENABLE_ANDROID
676 // PadToSize Transform Operation.
677 struct PadToSize::Data {
Datamindspore::dataset::vision::PadToSize::Data678   Data(const std::vector<int32_t> &size, const std::vector<int32_t> &offset, const std::vector<uint8_t> &fill_value,
679        BorderType padding_mode)
680       : size_(size), offset_(offset), fill_value_(fill_value), padding_mode_(padding_mode) {}
681   std::vector<int32_t> size_;
682   std::vector<int32_t> offset_;
683   std::vector<uint8_t> fill_value_;
684   BorderType padding_mode_;
685 };
686 
PadToSize(const std::vector<int32_t> & size,const std::vector<int32_t> & offset,const std::vector<uint8_t> & fill_value,BorderType padding_mode)687 PadToSize::PadToSize(const std::vector<int32_t> &size, const std::vector<int32_t> &offset,
688                      const std::vector<uint8_t> &fill_value, BorderType padding_mode)
689     : data_(std::make_shared<Data>(size, offset, fill_value, padding_mode)) {}
690 
Parse()691 std::shared_ptr<TensorOperation> PadToSize::Parse() {
692   return std::make_shared<PadToSizeOperation>(data_->size_, data_->offset_, data_->fill_value_, data_->padding_mode_);
693 }
694 
695 // Perspective Transform Operation.
696 struct Perspective::Data {
Datamindspore::dataset::vision::Perspective::Data697   Data(const std::vector<std::vector<int32_t>> &start_points, const std::vector<std::vector<int32_t>> &end_points,
698        InterpolationMode interpolation)
699       : start_points_(start_points), end_points_(end_points), interpolation_(interpolation) {}
700   std::vector<std::vector<int32_t>> start_points_;
701   std::vector<std::vector<int32_t>> end_points_;
702   InterpolationMode interpolation_;
703 };
704 
Perspective(const std::vector<std::vector<int32_t>> & start_points,const std::vector<std::vector<int32_t>> & end_points,InterpolationMode interpolation)705 Perspective::Perspective(const std::vector<std::vector<int32_t>> &start_points,
706                          const std::vector<std::vector<int32_t>> &end_points, InterpolationMode interpolation)
707     : data_(std::make_shared<Data>(start_points, end_points, interpolation)) {}
708 
Parse()709 std::shared_ptr<TensorOperation> Perspective::Parse() {
710   return std::make_shared<PerspectiveOperation>(data_->start_points_, data_->end_points_, data_->interpolation_);
711 }
712 
713 // Posterize Transform Operation
714 struct Posterize::Data {
Datamindspore::dataset::vision::Posterize::Data715   explicit Data(uint8_t bits) : bits_(bits) {}
716   uint8_t bits_;
717 };
718 
Posterize(uint8_t bits)719 Posterize::Posterize(uint8_t bits) : data_(std::make_shared<Data>(bits)) {}
720 
Parse()721 std::shared_ptr<TensorOperation> Posterize::Parse() { return std::make_shared<PosterizeOperation>(data_->bits_); }
722 
723 // RandAugment Transform Operation
724 struct RandAugment::Data {
Datamindspore::dataset::vision::RandAugment::Data725   Data(int32_t num_ops, int32_t magnitude, int32_t num_magnitude_bins, InterpolationMode interpolation,
726        const std::vector<uint8_t> &fill_value)
727       : num_ops_(num_ops),
728         magnitude_(magnitude),
729         num_magnitude_bins_(num_magnitude_bins),
730         interpolation_(interpolation),
731         fill_value_(fill_value) {}
732   int32_t num_ops_;
733   int32_t magnitude_;
734   int32_t num_magnitude_bins_;
735   InterpolationMode interpolation_;
736   std::vector<uint8_t> fill_value_;
737 };
738 
RandAugment(int32_t num_ops,int32_t magnitude,int32_t num_magnitude_bins,InterpolationMode interpolation,const std::vector<uint8_t> & fill_value)739 RandAugment::RandAugment(int32_t num_ops, int32_t magnitude, int32_t num_magnitude_bins,
740                          InterpolationMode interpolation, const std::vector<uint8_t> &fill_value)
741     : data_(std::make_shared<Data>(num_ops, magnitude, num_magnitude_bins, interpolation, fill_value)) {}
742 
Parse()743 std::shared_ptr<TensorOperation> RandAugment::Parse() {
744   return std::make_shared<RandAugmentOperation>(data_->num_ops_, data_->magnitude_, data_->num_magnitude_bins_,
745                                                 data_->interpolation_, data_->fill_value_);
746 }
747 
748 // RandomAdjustSharpness Transform Operation.
749 struct RandomAdjustSharpness::Data {
Datamindspore::dataset::vision::RandomAdjustSharpness::Data750   Data(float degree, float prob) : degree_(degree), probability_(prob) {}
751   float degree_;
752   float probability_;
753 };
754 
RandomAdjustSharpness(float degree,float prob)755 RandomAdjustSharpness::RandomAdjustSharpness(float degree, float prob) : data_(std::make_shared<Data>(degree, prob)) {}
756 
Parse()757 std::shared_ptr<TensorOperation> RandomAdjustSharpness::Parse() {
758   return std::make_shared<RandomAdjustSharpnessOperation>(data_->degree_, data_->probability_);
759 }
760 #endif  // not ENABLE_ANDROID
761 
762 // RandomAffine Transform Operation.
763 struct RandomAffine::Data {
Datamindspore::dataset::vision::RandomAffine::Data764   Data(const std::vector<float_t> &degrees, const std::vector<float_t> &translate_range,
765        const std::vector<float_t> &scale_range, const std::vector<float_t> &shear_ranges,
766        InterpolationMode interpolation, const std::vector<uint8_t> &fill_value)
767       : degrees_(degrees),
768         translate_range_(translate_range),
769         scale_range_(scale_range),
770         shear_ranges_(shear_ranges),
771         interpolation_(interpolation),
772         fill_value_(fill_value) {}
773   std::vector<float_t> degrees_;          // min_degree, max_degree
774   std::vector<float_t> translate_range_;  // maximum x translation percentage, maximum y translation percentage
775   std::vector<float_t> scale_range_;      // min_scale, max_scale
776   std::vector<float_t> shear_ranges_;     // min_x_shear, max_x_shear, min_y_shear, max_y_shear
777   InterpolationMode interpolation_;
778   std::vector<uint8_t> fill_value_;
779 };
780 
RandomAffine(const std::vector<float_t> & degrees,const std::vector<float_t> & translate_range,const std::vector<float_t> & scale_range,const std::vector<float_t> & shear_ranges,InterpolationMode interpolation,const std::vector<uint8_t> & fill_value)781 RandomAffine::RandomAffine(const std::vector<float_t> &degrees, const std::vector<float_t> &translate_range,
782                            const std::vector<float_t> &scale_range, const std::vector<float_t> &shear_ranges,
783                            InterpolationMode interpolation, const std::vector<uint8_t> &fill_value)
784     : data_(std::make_shared<Data>(degrees, translate_range, scale_range, shear_ranges, interpolation, fill_value)) {}
785 
Parse()786 std::shared_ptr<TensorOperation> RandomAffine::Parse() {
787   return std::make_shared<RandomAffineOperation>(data_->degrees_, data_->translate_range_, data_->scale_range_,
788                                                  data_->shear_ranges_, data_->interpolation_, data_->fill_value_);
789 }
790 
791 #ifndef ENABLE_ANDROID
792 // RandomAutoContrast Transform Operation.
793 struct RandomAutoContrast::Data {
Datamindspore::dataset::vision::RandomAutoContrast::Data794   Data(float cutoff, const std::vector<uint32_t> &ignore, float prob)
795       : cutoff_(cutoff), ignore_(ignore), probability_(prob) {}
796   float cutoff_;
797   std::vector<uint32_t> ignore_;
798   float probability_;
799 };
800 
RandomAutoContrast(float cutoff,const std::vector<uint32_t> & ignore,float prob)801 RandomAutoContrast::RandomAutoContrast(float cutoff, const std::vector<uint32_t> &ignore, float prob)
802     : data_(std::make_shared<Data>(cutoff, ignore, prob)) {}
803 
Parse()804 std::shared_ptr<TensorOperation> RandomAutoContrast::Parse() {
805   return std::make_shared<RandomAutoContrastOperation>(data_->cutoff_, data_->ignore_, data_->probability_);
806 }
807 
808 // RandomColor Transform Operation.
809 struct RandomColor::Data {
Datamindspore::dataset::vision::RandomColor::Data810   Data(float t_lb, float t_ub) : t_lb_(t_lb), t_ub_(t_ub) {}
811   float t_lb_;
812   float t_ub_;
813 };
814 
RandomColor(float t_lb,float t_ub)815 RandomColor::RandomColor(float t_lb, float t_ub) : data_(std::make_shared<Data>(t_lb, t_ub)) {}
816 
Parse()817 std::shared_ptr<TensorOperation> RandomColor::Parse() {
818   return std::make_shared<RandomColorOperation>(data_->t_lb_, data_->t_ub_);
819 }
820 
821 // RandomColorAdjust Transform Operation.
822 struct RandomColorAdjust::Data {
Datamindspore::dataset::vision::RandomColorAdjust::Data823   Data(const std::vector<float> &brightness, const std::vector<float> &contrast, const std::vector<float> &saturation,
824        const std::vector<float> &hue)
825       : brightness_(brightness), contrast_(contrast), saturation_(saturation), hue_(hue) {}
826   std::vector<float> brightness_;
827   std::vector<float> contrast_;
828   std::vector<float> saturation_;
829   std::vector<float> hue_;
830 };
831 
RandomColorAdjust(const std::vector<float> & brightness,const std::vector<float> & contrast,const std::vector<float> & saturation,const std::vector<float> & hue)832 RandomColorAdjust::RandomColorAdjust(const std::vector<float> &brightness, const std::vector<float> &contrast,
833                                      const std::vector<float> &saturation, const std::vector<float> &hue)
834     : data_(std::make_shared<Data>(brightness, contrast, saturation, hue)) {}
835 
Parse()836 std::shared_ptr<TensorOperation> RandomColorAdjust::Parse() {
837   return std::make_shared<RandomColorAdjustOperation>(data_->brightness_, data_->contrast_, data_->saturation_,
838                                                       data_->hue_);
839 }
840 
841 // RandomCrop Transform Operation.
842 struct RandomCrop::Data {
Datamindspore::dataset::vision::RandomCrop::Data843   Data(const std::vector<int32_t> &size, const std::vector<int32_t> &padding, bool pad_if_needed,
844        const std::vector<uint8_t> &fill_value, BorderType padding_mode)
845       : size_(size),
846         padding_(padding),
847         pad_if_needed_(pad_if_needed),
848         fill_value_(fill_value),
849         padding_mode_(padding_mode) {}
850   std::vector<int32_t> size_;
851   std::vector<int32_t> padding_;
852   bool pad_if_needed_;
853   std::vector<uint8_t> fill_value_;
854   BorderType padding_mode_;
855 };
856 
RandomCrop(const std::vector<int32_t> & size,const std::vector<int32_t> & padding,bool pad_if_needed,const std::vector<uint8_t> & fill_value,BorderType padding_mode)857 RandomCrop::RandomCrop(const std::vector<int32_t> &size, const std::vector<int32_t> &padding, bool pad_if_needed,
858                        const std::vector<uint8_t> &fill_value, BorderType padding_mode)
859     : data_(std::make_shared<Data>(size, padding, pad_if_needed, fill_value, padding_mode)) {}
860 
Parse()861 std::shared_ptr<TensorOperation> RandomCrop::Parse() {
862   return std::make_shared<RandomCropOperation>(data_->size_, data_->padding_, data_->pad_if_needed_, data_->fill_value_,
863                                                data_->padding_mode_);
864 }
865 
866 // RandomCropDecodeResize Transform Operation.
867 struct RandomCropDecodeResize::Data {
Datamindspore::dataset::vision::RandomCropDecodeResize::Data868   Data(const std::vector<int32_t> &size, const std::vector<float> &scale, const std::vector<float> &ratio,
869        InterpolationMode interpolation, int32_t max_attempts)
870       : size_(size), scale_(scale), ratio_(ratio), interpolation_(interpolation), max_attempts_(max_attempts) {}
871   std::vector<int32_t> size_;
872   std::vector<float> scale_;
873   std::vector<float> ratio_;
874   InterpolationMode interpolation_;
875   int32_t max_attempts_;
876 };
877 
RandomCropDecodeResize(const std::vector<int32_t> & size,const std::vector<float> & scale,const std::vector<float> & ratio,InterpolationMode interpolation,int32_t max_attempts)878 RandomCropDecodeResize::RandomCropDecodeResize(const std::vector<int32_t> &size, const std::vector<float> &scale,
879                                                const std::vector<float> &ratio, InterpolationMode interpolation,
880                                                int32_t max_attempts)
881     : data_(std::make_shared<Data>(size, scale, ratio, interpolation, max_attempts)) {}
882 
Parse()883 std::shared_ptr<TensorOperation> RandomCropDecodeResize::Parse() {
884   return std::make_shared<RandomCropDecodeResizeOperation>(data_->size_, data_->scale_, data_->ratio_,
885                                                            data_->interpolation_, data_->max_attempts_);
886 }
887 
888 // RandomCropWithBBox Transform Operation.
889 struct RandomCropWithBBox::Data {
Datamindspore::dataset::vision::RandomCropWithBBox::Data890   Data(const std::vector<int32_t> &size, const std::vector<int32_t> &padding, bool pad_if_needed,
891        const std::vector<uint8_t> &fill_value, BorderType padding_mode)
892       : size_(size),
893         padding_(padding),
894         pad_if_needed_(pad_if_needed),
895         fill_value_(fill_value),
896         padding_mode_(padding_mode) {}
897   std::vector<int32_t> size_;
898   std::vector<int32_t> padding_;
899   bool pad_if_needed_;
900   std::vector<uint8_t> fill_value_;
901   BorderType padding_mode_;
902 };
903 
RandomCropWithBBox(const std::vector<int32_t> & size,const std::vector<int32_t> & padding,bool pad_if_needed,const std::vector<uint8_t> & fill_value,BorderType padding_mode)904 RandomCropWithBBox::RandomCropWithBBox(const std::vector<int32_t> &size, const std::vector<int32_t> &padding,
905                                        bool pad_if_needed, const std::vector<uint8_t> &fill_value,
906                                        BorderType padding_mode)
907     : data_(std::make_shared<Data>(size, padding, pad_if_needed, fill_value, padding_mode)) {}
908 
Parse()909 std::shared_ptr<TensorOperation> RandomCropWithBBox::Parse() {
910   return std::make_shared<RandomCropWithBBoxOperation>(data_->size_, data_->padding_, data_->pad_if_needed_,
911                                                        data_->fill_value_, data_->padding_mode_);
912 }
913 
914 // RandomEqualize Transform Operation.
915 struct RandomEqualize::Data {
Datamindspore::dataset::vision::RandomEqualize::Data916   explicit Data(float prob) : probability_(prob) {}
917   float probability_;
918 };
919 
RandomEqualize(float prob)920 RandomEqualize::RandomEqualize(float prob) : data_(std::make_shared<Data>(prob)) {}
921 
Parse()922 std::shared_ptr<TensorOperation> RandomEqualize::Parse() {
923   return std::make_shared<RandomEqualizeOperation>(data_->probability_);
924 }
925 
926 // RandomHorizontalFlip.
927 struct RandomHorizontalFlip::Data {
Datamindspore::dataset::vision::RandomHorizontalFlip::Data928   explicit Data(float prob) : probability_(prob) {}
929   float probability_;
930 };
931 
RandomHorizontalFlip(float prob)932 RandomHorizontalFlip::RandomHorizontalFlip(float prob) : data_(std::make_shared<Data>(prob)) {}
933 
Parse()934 std::shared_ptr<TensorOperation> RandomHorizontalFlip::Parse() {
935   return std::make_shared<RandomHorizontalFlipOperation>(data_->probability_);
936 }
937 
938 // RandomHorizontalFlipWithBBox
939 struct RandomHorizontalFlipWithBBox::Data {
Datamindspore::dataset::vision::RandomHorizontalFlipWithBBox::Data940   explicit Data(float prob) : probability_(prob) {}
941   float probability_;
942 };
943 
RandomHorizontalFlipWithBBox(float prob)944 RandomHorizontalFlipWithBBox::RandomHorizontalFlipWithBBox(float prob) : data_(std::make_shared<Data>(prob)) {}
945 
Parse()946 std::shared_ptr<TensorOperation> RandomHorizontalFlipWithBBox::Parse() {
947   return std::make_shared<RandomHorizontalFlipWithBBoxOperation>(data_->probability_);
948 }
949 
950 // RandomInvert Operation.
951 struct RandomInvert::Data {
Datamindspore::dataset::vision::RandomInvert::Data952   explicit Data(float prob) : probability_(prob) {}
953   float probability_;
954 };
955 
RandomInvert(float prob)956 RandomInvert::RandomInvert(float prob) : data_(std::make_shared<Data>(prob)) {}
957 
Parse()958 std::shared_ptr<TensorOperation> RandomInvert::Parse() {
959   return std::make_shared<RandomInvertOperation>(data_->probability_);
960 }
961 
962 // RandomLighting Transform Operation.
963 struct RandomLighting::Data {
Datamindspore::dataset::vision::RandomLighting::Data964   explicit Data(float alpha) : alpha_(alpha) {}
965   float alpha_;
966 };
967 
RandomLighting(float alpha)968 RandomLighting::RandomLighting(float alpha) : data_(std::make_shared<Data>(alpha)) {}
969 
Parse()970 std::shared_ptr<TensorOperation> RandomLighting::Parse() {
971   return std::make_shared<RandomLightingOperation>(data_->alpha_);
972 }
973 
974 // RandomPosterize Transform Operation.
975 struct RandomPosterize::Data {
Datamindspore::dataset::vision::RandomPosterize::Data976   explicit Data(const std::vector<uint8_t> &bit_range) : bit_range_(bit_range) {}
977   std::vector<uint8_t> bit_range_;
978 };
979 
RandomPosterize(const std::vector<uint8_t> & bit_range)980 RandomPosterize::RandomPosterize(const std::vector<uint8_t> &bit_range) : data_(std::make_shared<Data>(bit_range)) {}
981 
Parse()982 std::shared_ptr<TensorOperation> RandomPosterize::Parse() {
983   return std::make_shared<RandomPosterizeOperation>(data_->bit_range_);
984 }
985 
986 // RandomResize Transform Operation.
987 struct RandomResize::Data {
Datamindspore::dataset::vision::RandomResize::Data988   explicit Data(const std::vector<int32_t> &size) : size_(size) {}
989   std::vector<int32_t> size_;
990 };
991 
RandomResize(const std::vector<int32_t> & size)992 RandomResize::RandomResize(const std::vector<int32_t> &size) : data_(std::make_shared<Data>(size)) {}
993 
Parse()994 std::shared_ptr<TensorOperation> RandomResize::Parse() { return std::make_shared<RandomResizeOperation>(data_->size_); }
995 
996 // RandomResizeWithBBox Transform Operation.
997 struct RandomResizeWithBBox::Data {
Datamindspore::dataset::vision::RandomResizeWithBBox::Data998   explicit Data(const std::vector<int32_t> &size) : size_(size) {}
999   std::vector<int32_t> size_;
1000 };
1001 
RandomResizeWithBBox(const std::vector<int32_t> & size)1002 RandomResizeWithBBox::RandomResizeWithBBox(const std::vector<int32_t> &size) : data_(std::make_shared<Data>(size)) {}
1003 
Parse()1004 std::shared_ptr<TensorOperation> RandomResizeWithBBox::Parse() {
1005   return std::make_shared<RandomResizeWithBBoxOperation>(data_->size_);
1006 }
1007 
1008 // RandomResizedCrop Transform Operation.
1009 struct RandomResizedCrop::Data {
Datamindspore::dataset::vision::RandomResizedCrop::Data1010   Data(const std::vector<int32_t> &size, const std::vector<float> &scale, const std::vector<float> &ratio,
1011        InterpolationMode interpolation, int32_t max_attempts)
1012       : size_(size), scale_(scale), ratio_(ratio), interpolation_(interpolation), max_attempts_(max_attempts) {}
1013   std::vector<int32_t> size_;
1014   std::vector<float> scale_;
1015   std::vector<float> ratio_;
1016   InterpolationMode interpolation_;
1017   int32_t max_attempts_;
1018 };
1019 
RandomResizedCrop(const std::vector<int32_t> & size,const std::vector<float> & scale,const std::vector<float> & ratio,InterpolationMode interpolation,int32_t max_attempts)1020 RandomResizedCrop::RandomResizedCrop(const std::vector<int32_t> &size, const std::vector<float> &scale,
1021                                      const std::vector<float> &ratio, InterpolationMode interpolation,
1022                                      int32_t max_attempts)
1023     : data_(std::make_shared<Data>(size, scale, ratio, interpolation, max_attempts)) {}
1024 
Parse()1025 std::shared_ptr<TensorOperation> RandomResizedCrop::Parse() {
1026   return std::make_shared<RandomResizedCropOperation>(data_->size_, data_->scale_, data_->ratio_, data_->interpolation_,
1027                                                       data_->max_attempts_);
1028 }
1029 
1030 // RandomResizedCrop Transform Operation.
1031 struct RandomResizedCropWithBBox::Data {
Datamindspore::dataset::vision::RandomResizedCropWithBBox::Data1032   Data(const std::vector<int32_t> &size, const std::vector<float> &scale, const std::vector<float> &ratio,
1033        InterpolationMode interpolation, int32_t max_attempts)
1034       : size_(size), scale_(scale), ratio_(ratio), interpolation_(interpolation), max_attempts_(max_attempts) {}
1035   std::vector<int32_t> size_;
1036   std::vector<float> scale_;
1037   std::vector<float> ratio_;
1038   InterpolationMode interpolation_;
1039   int32_t max_attempts_;
1040 };
1041 
RandomResizedCropWithBBox(const std::vector<int32_t> & size,const std::vector<float> & scale,const std::vector<float> & ratio,InterpolationMode interpolation,int32_t max_attempts)1042 RandomResizedCropWithBBox::RandomResizedCropWithBBox(const std::vector<int32_t> &size, const std::vector<float> &scale,
1043                                                      const std::vector<float> &ratio, InterpolationMode interpolation,
1044                                                      int32_t max_attempts)
1045     : data_(std::make_shared<Data>(size, scale, ratio, interpolation, max_attempts)) {}
1046 
Parse()1047 std::shared_ptr<TensorOperation> RandomResizedCropWithBBox::Parse() {
1048   return std::make_shared<RandomResizedCropWithBBoxOperation>(data_->size_, data_->scale_, data_->ratio_,
1049                                                               data_->interpolation_, data_->max_attempts_);
1050 }
1051 
1052 // RandomRotation Transform Operation.
1053 struct RandomRotation::Data {
Datamindspore::dataset::vision::RandomRotation::Data1054   Data(const std::vector<float> &degrees, InterpolationMode resample, bool expand, const std::vector<float> &center,
1055        const std::vector<uint8_t> &fill_value)
1056       : degrees_(degrees), interpolation_mode_(resample), center_(center), expand_(expand), fill_value_(fill_value) {}
1057   std::vector<float> degrees_;
1058   InterpolationMode interpolation_mode_;
1059   std::vector<float> center_;
1060   bool expand_;
1061   std::vector<uint8_t> fill_value_;
1062 };
1063 
RandomRotation(const std::vector<float> & degrees,InterpolationMode resample,bool expand,const std::vector<float> & center,const std::vector<uint8_t> & fill_value)1064 RandomRotation::RandomRotation(const std::vector<float> &degrees, InterpolationMode resample, bool expand,
1065                                const std::vector<float> &center, const std::vector<uint8_t> &fill_value)
1066     : data_(std::make_shared<Data>(degrees, resample, expand, center, fill_value)) {}
1067 
Parse()1068 std::shared_ptr<TensorOperation> RandomRotation::Parse() {
1069   return std::make_shared<RandomRotationOperation>(data_->degrees_, data_->interpolation_mode_, data_->expand_,
1070                                                    data_->center_, data_->fill_value_);
1071 }
1072 
1073 // RandomSelectSubpolicy Transform Operation.
1074 struct RandomSelectSubpolicy::Data {
1075   std::vector<std::vector<std::pair<std::shared_ptr<TensorOperation>, double>>> policy_;
1076 };
1077 
RandomSelectSubpolicy(const std::vector<std::vector<std::pair<TensorTransform *,double>>> & policy)1078 RandomSelectSubpolicy::RandomSelectSubpolicy(
1079   const std::vector<std::vector<std::pair<TensorTransform *, double>>> &policy)
1080     : data_(std::make_shared<Data>()) {
1081   for (uint32_t i = 0; i < policy.size(); i++) {
1082     std::vector<std::pair<std::shared_ptr<TensorOperation>, double>> subpolicy;
1083 
1084     for (uint32_t j = 0; j < policy[i].size(); j++) {
1085       TensorTransform *op = policy[i][j].first;
1086       std::shared_ptr<TensorOperation> operation = (op ? op->Parse() : nullptr);
1087       double prob = policy[i][j].second;
1088       subpolicy.emplace_back(std::move(std::make_pair(operation, prob)));
1089     }
1090     data_->policy_.emplace_back(subpolicy);
1091   }
1092 }
1093 
RandomSelectSubpolicy(const std::vector<std::vector<std::pair<std::shared_ptr<TensorTransform>,double>>> & policy)1094 RandomSelectSubpolicy::RandomSelectSubpolicy(
1095   const std::vector<std::vector<std::pair<std::shared_ptr<TensorTransform>, double>>> &policy)
1096     : data_(std::make_shared<Data>()) {
1097   for (uint32_t i = 0; i < policy.size(); i++) {
1098     std::vector<std::pair<std::shared_ptr<TensorOperation>, double>> subpolicy;
1099 
1100     for (uint32_t j = 0; j < policy[i].size(); j++) {
1101       std::shared_ptr<TensorTransform> op = policy[i][j].first;
1102       std::shared_ptr<TensorOperation> operation = (op ? op->Parse() : nullptr);
1103       double prob = policy[i][j].second;
1104       subpolicy.emplace_back(std::move(std::make_pair(operation, prob)));
1105     }
1106     data_->policy_.emplace_back(subpolicy);
1107   }
1108 }
1109 
RandomSelectSubpolicy(const std::vector<std::vector<std::pair<std::reference_wrapper<TensorTransform>,double>>> & policy)1110 RandomSelectSubpolicy::RandomSelectSubpolicy(
1111   const std::vector<std::vector<std::pair<std::reference_wrapper<TensorTransform>, double>>> &policy)
1112     : data_(std::make_shared<Data>()) {
1113   for (int32_t i = 0; i < policy.size(); i++) {
1114     std::vector<std::pair<std::shared_ptr<TensorOperation>, double>> subpolicy;
1115 
1116     for (int32_t j = 0; j < policy[i].size(); j++) {
1117       TensorTransform &op = policy[i][j].first;
1118       std::shared_ptr<TensorOperation> operation = op.Parse();
1119       double prob = policy[i][j].second;
1120       subpolicy.emplace_back(std::move(std::make_pair(operation, prob)));
1121     }
1122     data_->policy_.emplace_back(subpolicy);
1123   }
1124 }
1125 
Parse()1126 std::shared_ptr<TensorOperation> RandomSelectSubpolicy::Parse() {
1127   return std::make_shared<RandomSelectSubpolicyOperation>(data_->policy_);
1128 }
1129 
1130 // RandomSharpness Transform Operation.
1131 struct RandomSharpness::Data {
Datamindspore::dataset::vision::RandomSharpness::Data1132   explicit Data(const std::vector<float> &degrees) : degrees_(degrees) {}
1133   std::vector<float> degrees_;
1134 };
1135 
RandomSharpness(const std::vector<float> & degrees)1136 RandomSharpness::RandomSharpness(const std::vector<float> &degrees) : data_(std::make_shared<Data>(degrees)) {}
1137 
Parse()1138 std::shared_ptr<TensorOperation> RandomSharpness::Parse() {
1139   return std::make_shared<RandomSharpnessOperation>(data_->degrees_);
1140 }
1141 
1142 // RandomSolarize Transform Operation.
1143 struct RandomSolarize::Data {
Datamindspore::dataset::vision::RandomSolarize::Data1144   explicit Data(const std::vector<uint8_t> &threshold) : threshold_(threshold) {}
1145   std::vector<uint8_t> threshold_;
1146 };
1147 
RandomSolarize(const std::vector<uint8_t> & threshold)1148 RandomSolarize::RandomSolarize(const std::vector<uint8_t> &threshold) : data_(std::make_shared<Data>(threshold)) {}
1149 
Parse()1150 std::shared_ptr<TensorOperation> RandomSolarize::Parse() {
1151   return std::make_shared<RandomSolarizeOperation>(data_->threshold_);
1152 }
1153 
1154 // RandomVerticalFlip Transform Operation.
1155 struct RandomVerticalFlip::Data {
Datamindspore::dataset::vision::RandomVerticalFlip::Data1156   explicit Data(float prob) : probability_(prob) {}
1157   float probability_;
1158 };
1159 
RandomVerticalFlip(float prob)1160 RandomVerticalFlip::RandomVerticalFlip(float prob) : data_(std::make_shared<Data>(prob)) {}
1161 
Parse()1162 std::shared_ptr<TensorOperation> RandomVerticalFlip::Parse() {
1163   return std::make_shared<RandomVerticalFlipOperation>(data_->probability_);
1164 }
1165 
1166 // RandomVerticalFlipWithBBox Transform Operation.
1167 struct RandomVerticalFlipWithBBox::Data {
Datamindspore::dataset::vision::RandomVerticalFlipWithBBox::Data1168   explicit Data(float prob) : probability_(prob) {}
1169   float probability_;
1170 };
1171 
RandomVerticalFlipWithBBox(float prob)1172 RandomVerticalFlipWithBBox::RandomVerticalFlipWithBBox(float prob) : data_(std::make_shared<Data>(prob)) {}
1173 
Parse()1174 std::shared_ptr<TensorOperation> RandomVerticalFlipWithBBox::Parse() {
1175   return std::make_shared<RandomVerticalFlipWithBBoxOperation>(data_->probability_);
1176 }
1177 
1178 // ReadFile Function.
ReadFile(const std::string & filename,mindspore::MSTensor * output)1179 Status ReadFile(const std::string &filename, mindspore::MSTensor *output) {
1180   RETURN_UNEXPECTED_IF_NULL(output);
1181 
1182   std::shared_ptr<Tensor> de_tensor;
1183   RETURN_IF_NOT_OK(mindspore::dataset::ReadFile(filename, &de_tensor));
1184   CHECK_FAIL_RETURN_UNEXPECTED(de_tensor->HasData(),
1185                                "ReadFile: Get an empty tensor with shape " + de_tensor->shape().ToString());
1186   *output = mindspore::MSTensor(std::make_shared<DETensor>(de_tensor));
1187 
1188   return Status::OK();
1189 }
1190 
1191 // ReadImage Function.
ReadImage(const std::string & filename,mindspore::MSTensor * output,ImageReadMode mode)1192 Status ReadImage(const std::string &filename, mindspore::MSTensor *output, ImageReadMode mode) {
1193   RETURN_UNEXPECTED_IF_NULL(output);
1194 
1195   std::shared_ptr<Tensor> de_tensor;
1196   RETURN_IF_NOT_OK(mindspore::dataset::ReadImage(filename, &de_tensor, mode));
1197   CHECK_FAIL_RETURN_UNEXPECTED(de_tensor->HasData(),
1198                                "ReadImage: get an empty tensor with shape " + de_tensor->shape().ToString());
1199   *output = mindspore::MSTensor(std::make_shared<DETensor>(de_tensor));
1200   return Status::OK();
1201 }
1202 
1203 // ReadVideo Function.
ReadVideo(const std::string & filename,mindspore::MSTensor * video_output,mindspore::MSTensor * audio_output,std::map<std::string,std::string> * metadata_output,float start_pts,float end_pts,const std::string & pts_unit)1204 Status ReadVideo(const std::string &filename, mindspore::MSTensor *video_output, mindspore::MSTensor *audio_output,
1205                  std::map<std::string, std::string> *metadata_output, float start_pts, float end_pts,
1206                  const std::string &pts_unit) {
1207   RETURN_UNEXPECTED_IF_NULL(video_output);
1208   RETURN_UNEXPECTED_IF_NULL(audio_output);
1209   RETURN_UNEXPECTED_IF_NULL(metadata_output);
1210 
1211   std::shared_ptr<Tensor> de_video_output;
1212   std::shared_ptr<Tensor> de_audio_output;
1213   RETURN_IF_NOT_OK(mindspore::dataset::ReadVideo(filename, &de_video_output, &de_audio_output, metadata_output,
1214                                                  start_pts, end_pts, pts_unit));
1215   *video_output = mindspore::MSTensor(std::make_shared<DETensor>(de_video_output));
1216   *audio_output = mindspore::MSTensor(std::make_shared<DETensor>(de_audio_output));
1217   return Status::OK();
1218 }
1219 
1220 // ReadVideoTimestamps.
ReadVideoTimestamps(const std::string & filename,std::tuple<std::vector<float>,float> * output,const std::string & pts_unit)1221 Status ReadVideoTimestamps(const std::string &filename, std::tuple<std::vector<float>, float> *output,
1222                            const std::string &pts_unit) {
1223   RETURN_UNEXPECTED_IF_NULL(output);
1224   std::vector<int64_t> pts_int64_vector;
1225   float video_fps;
1226   float time_base;
1227 
1228   RETURN_IF_NOT_OK(
1229     mindspore::dataset::ReadVideoTimestamps(filename, &pts_int64_vector, &video_fps, &time_base, pts_unit));
1230 
1231   std::vector<float> pts_float_vector;
1232 
1233   if (pts_unit == "pts") {
1234     for (int64_t pts_int64 : pts_int64_vector) {
1235       pts_float_vector.push_back(static_cast<float>(pts_int64));
1236     }
1237   }
1238   if (pts_unit == "sec") {
1239     for (int64_t pts_int64 : pts_int64_vector) {
1240       pts_float_vector.push_back(static_cast<float>(pts_int64 * time_base));
1241     }
1242   }
1243   *output = std::make_tuple(pts_float_vector, video_fps);
1244   return Status::OK();
1245 }
1246 #endif  // not ENABLE_ANDROID
1247 
1248 // Rescale Transform Operation.
1249 struct Rescale::Data {
Datamindspore::dataset::vision::Rescale::Data1250   Data(float rescale, float shift) : rescale_(rescale), shift_(shift) {}
1251   float rescale_;
1252   float shift_;
1253 };
1254 
Rescale(float rescale,float shift)1255 Rescale::Rescale(float rescale, float shift) : data_(std::make_shared<Data>(rescale, shift)) {}
1256 
Parse()1257 std::shared_ptr<TensorOperation> Rescale::Parse() {
1258 #if !defined(ENABLE_ANDROID) || defined(ENABLE_MINDDATA_PYTHON)
1259   return std::make_shared<RescaleOperation>(data_->rescale_, data_->shift_);
1260 #else
1261   MS_LOG(ERROR) << "Unsupported Rescale.";
1262   return nullptr;
1263 #endif
1264 }
1265 
1266 // Resize Transform Operation.
1267 struct Resize::Data {
Datamindspore::dataset::vision::Resize::Data1268   Data(const std::vector<int32_t> &size, InterpolationMode interpolation)
1269       : size_(size), interpolation_(interpolation) {}
1270   std::vector<int32_t> size_;
1271   InterpolationMode interpolation_;
1272 };
1273 
Resize(const std::vector<int32_t> & size,InterpolationMode interpolation)1274 Resize::Resize(const std::vector<int32_t> &size, InterpolationMode interpolation)
1275     : data_(std::make_shared<Data>(size, interpolation)) {}
1276 
Parse()1277 std::shared_ptr<TensorOperation> Resize::Parse() {
1278   return std::make_shared<ResizeOperation>(data_->size_, data_->interpolation_);
1279 }
1280 
Parse(const MapTargetDevice & env)1281 std::shared_ptr<TensorOperation> Resize::Parse(const MapTargetDevice &env) {
1282   if (env == MapTargetDevice::kAscend310) {
1283 #if defined(WITH_BACKEND) || defined(ENABLE_ACL) || defined(ENABLE_DVPP)
1284     std::vector<uint32_t> usize_;
1285     usize_.reserve(data_->size_.size());
1286     std::transform(data_->size_.begin(), data_->size_.end(), std::back_inserter(usize_),
1287                    [](int32_t i) { return (uint32_t)i; });
1288     return std::make_shared<DvppResizeJpegOperation>(usize_);
1289 #endif
1290   } else if (env == MapTargetDevice::kCpu) {
1291     return std::make_shared<ResizeOperation>(data_->size_, data_->interpolation_);
1292   }
1293   MS_LOG(ERROR) << "Unsupported MapTargetDevice, only supported kCpu and kAscend310.";
1294   return nullptr;
1295 }
1296 
1297 #ifndef ENABLE_ANDROID
1298 // ResizedCrop Transform Operation.
1299 struct ResizedCrop::Data {
Datamindspore::dataset::vision::ResizedCrop::Data1300   Data(int32_t top, int32_t left, int32_t height, int32_t width, const std::vector<int32_t> &size,
1301        InterpolationMode interpolation)
1302       : top_(top), left_(left), height_(height), width_(width), size_(size), interpolation_(interpolation) {}
1303   int32_t top_;
1304   int32_t left_;
1305   int32_t height_;
1306   int32_t width_;
1307   std::vector<int32_t> size_;
1308   InterpolationMode interpolation_;
1309 };
1310 
ResizedCrop(int32_t top,int32_t left,int32_t height,int32_t width,const std::vector<int32_t> & size,InterpolationMode interpolation)1311 ResizedCrop::ResizedCrop(int32_t top, int32_t left, int32_t height, int32_t width, const std::vector<int32_t> &size,
1312                          InterpolationMode interpolation)
1313     : data_(std::make_shared<Data>(top, left, height, width, size, interpolation)) {}
1314 
Parse()1315 std::shared_ptr<TensorOperation> ResizedCrop::Parse() {
1316   return std::make_shared<ResizedCropOperation>(data_->top_, data_->left_, data_->height_, data_->width_, data_->size_,
1317                                                 data_->interpolation_);
1318 }
1319 #endif  // not ENABLE_ANDROID
1320 
1321 // ResizePreserveAR Transform Operation.
1322 struct ResizePreserveAR::Data {
Datamindspore::dataset::vision::ResizePreserveAR::Data1323   Data(int32_t height, int32_t width, int32_t img_orientation)
1324       : height_(height), width_(width), img_orientation_(img_orientation) {}
1325   int32_t height_;
1326   int32_t width_;
1327   int32_t img_orientation_;
1328 };
1329 
ResizePreserveAR(int32_t height,int32_t width,int32_t img_orientation)1330 ResizePreserveAR::ResizePreserveAR(int32_t height, int32_t width, int32_t img_orientation)
1331     : data_(std::make_shared<Data>(height, width, img_orientation)) {}
1332 
Parse()1333 std::shared_ptr<TensorOperation> ResizePreserveAR::Parse() {
1334   return std::make_shared<ResizePreserveAROperation>(data_->height_, data_->width_, data_->img_orientation_);
1335 }
1336 
1337 #ifndef ENABLE_ANDROID
1338 // ResizeWithBBox Transform Operation.
1339 struct ResizeWithBBox::Data {
Datamindspore::dataset::vision::ResizeWithBBox::Data1340   Data(const std::vector<int32_t> &size, InterpolationMode interpolation)
1341       : size_(size), interpolation_(interpolation) {}
1342   std::vector<int32_t> size_;
1343   InterpolationMode interpolation_;
1344 };
1345 
ResizeWithBBox(const std::vector<int32_t> & size,InterpolationMode interpolation)1346 ResizeWithBBox::ResizeWithBBox(const std::vector<int32_t> &size, InterpolationMode interpolation)
1347     : data_(std::make_shared<Data>(size, interpolation)) {}
1348 
Parse()1349 std::shared_ptr<TensorOperation> ResizeWithBBox::Parse() {
1350   return std::make_shared<ResizeWithBBoxOperation>(data_->size_, data_->interpolation_);
1351 }
1352 #endif  // not ENABLE_ANDROID
1353 
1354 // RGB2BGR Transform Operation.
Parse()1355 std::shared_ptr<TensorOperation> RGB2BGR::Parse() { return std::make_shared<RgbToBgrOperation>(); }
1356 
1357 // RGB2GRAY Transform Operation.
Parse()1358 std::shared_ptr<TensorOperation> RGB2GRAY::Parse() { return std::make_shared<RgbToGrayOperation>(); }
1359 
1360 // Rotate Transform Operation.
1361 struct Rotate::Data {
Datamindspore::dataset::vision::Rotate::Data1362   Data(const float &degrees, InterpolationMode resample, bool expand, const std::vector<float> &center,
1363        const std::vector<uint8_t> &fill_value)
1364       : degrees_(degrees), interpolation_mode_(resample), center_(center), expand_(expand), fill_value_(fill_value) {}
Datamindspore::dataset::vision::Rotate::Data1365   explicit Data(const FixRotationAngle &angle_id) : angle_id_(angle_id), lite_impl_(true) {}
1366   FixRotationAngle angle_id_{FixRotationAngle::k0Degree};
1367   bool lite_impl_{false};
1368   float degrees_{0};
1369   InterpolationMode interpolation_mode_{InterpolationMode::kNearestNeighbour};
1370   std::vector<float> center_{{}};
1371   bool expand_{false};
1372   std::vector<uint8_t> fill_value_{0, 0, 0};
1373 };
1374 
Rotate(FixRotationAngle angle_id)1375 Rotate::Rotate(FixRotationAngle angle_id) : data_(std::make_shared<Data>(angle_id)) {}
1376 
Rotate(float degrees,InterpolationMode resample,bool expand,const std::vector<float> & center,const std::vector<uint8_t> & fill_value)1377 Rotate::Rotate(float degrees, InterpolationMode resample, bool expand, const std::vector<float> &center,
1378                const std::vector<uint8_t> &fill_value)
1379     : data_(std::make_shared<Data>(degrees, resample, expand, center, fill_value)) {}
1380 
Parse()1381 std::shared_ptr<TensorOperation> Rotate::Parse() {
1382 #ifndef ENABLE_ANDROID
1383   if (!data_->lite_impl_) {
1384     return std::make_shared<RotateOperation>(data_->degrees_, data_->interpolation_mode_, data_->expand_,
1385                                              data_->center_, data_->fill_value_);
1386   }
1387 #else
1388   if (data_->lite_impl_) {
1389     return std::make_shared<RotateOperation>(data_->angle_id_);
1390   }
1391 #endif  // not ENABLE_ANDROID
1392   std::string platform = data_->lite_impl_ ? "Cloud" : "Android";
1393   MS_LOG(ERROR) << "This Rotate API is not supported for " + platform + ", use another Rotate API.";
1394   return nullptr;
1395 }
1396 
1397 #ifndef ENABLE_ANDROID
1398 // RgbaToBgr Transform Operation.
1399 RGBA2BGR::RGBA2BGR() = default;
1400 
Parse()1401 std::shared_ptr<TensorOperation> RGBA2BGR::Parse() { return std::make_shared<RgbaToBgrOperation>(); }
1402 
1403 // RgbaToRgb Transform Operation.
1404 RGBA2RGB::RGBA2RGB() = default;
1405 
Parse()1406 std::shared_ptr<TensorOperation> RGBA2RGB::Parse() { return std::make_shared<RgbaToRgbOperation>(); }
1407 
1408 // SlicePatches Transform Operation.
1409 struct SlicePatches::Data {
Datamindspore::dataset::vision::SlicePatches::Data1410   Data(int32_t num_height, int32_t num_width, SliceMode slice_mode, uint8_t fill_value)
1411       : num_height_(num_height), num_width_(num_width), slice_mode_(slice_mode), fill_value_(fill_value) {}
1412   int32_t num_height_;
1413   int32_t num_width_;
1414   SliceMode slice_mode_;
1415   uint8_t fill_value_;
1416 };
1417 
SlicePatches(int32_t num_height,int32_t num_width,SliceMode slice_mode,uint8_t fill_value)1418 SlicePatches::SlicePatches(int32_t num_height, int32_t num_width, SliceMode slice_mode, uint8_t fill_value)
1419     : data_(std::make_shared<Data>(num_height, num_width, slice_mode, fill_value)) {}
1420 
Parse()1421 std::shared_ptr<TensorOperation> SlicePatches::Parse() {
1422   return std::make_shared<SlicePatchesOperation>(data_->num_height_, data_->num_width_, data_->slice_mode_,
1423                                                  data_->fill_value_);
1424 }
1425 
1426 // Solarize Transform Operation.
1427 struct Solarize::Data {
Datamindspore::dataset::vision::Solarize::Data1428   explicit Data(const std::vector<float> &threshold) : threshold_(threshold) {}
1429   std::vector<float> threshold_;
1430 };
1431 
Solarize(const std::vector<float> & threshold)1432 Solarize::Solarize(const std::vector<float> &threshold) : data_(std::make_shared<Data>(threshold)) {}
1433 
Parse()1434 std::shared_ptr<TensorOperation> Solarize::Parse() { return std::make_shared<SolarizeOperation>(data_->threshold_); }
1435 #endif  // not ENABLE_ANDROID
1436 
1437 // SwapRedBlue Transform Operation.
1438 SwapRedBlue::SwapRedBlue() = default;
Parse()1439 std::shared_ptr<TensorOperation> SwapRedBlue::Parse() {
1440 #if !defined(ENABLE_ANDROID) || defined(ENABLE_MINDDATA_PYTHON)
1441   return std::make_shared<SwapRedBlueOperation>();
1442 #else
1443   MS_LOG(ERROR) << "Unsupported SwapRedBlue.";
1444   return nullptr;
1445 #endif
1446 }
1447 
1448 #ifndef ENABLE_ANDROID
1449 // ToTensor Transform Operation.
1450 struct ToTensor::Data {
Datamindspore::dataset::vision::ToTensor::Data1451   explicit Data(const std::string &output_type) : output_type_(DataType(output_type)) {}
Datamindspore::dataset::vision::ToTensor::Data1452   explicit Data(const DataType::Type &output_type) : output_type_(output_type) {}
Datamindspore::dataset::vision::ToTensor::Data1453   explicit Data(const mindspore::DataType &output_type)
1454       : output_type_(dataset::MSTypeToDEType(static_cast<TypeId>(output_type))) {}
1455   DataType output_type_{};
1456 };
1457 
ToTensor()1458 ToTensor::ToTensor() : data_(std::make_shared<Data>(DataType::Type::DE_FLOAT32)) {}
ToTensor(std::string output_type)1459 ToTensor::ToTensor(std::string output_type) : data_(std::make_shared<Data>(output_type)) {}
ToTensor(mindspore::DataType output_type)1460 ToTensor::ToTensor(mindspore::DataType output_type) : data_(std::make_shared<Data>(output_type)) {}
1461 
Parse()1462 std::shared_ptr<TensorOperation> ToTensor::Parse() { return std::make_shared<ToTensorOperation>(data_->output_type_); }
1463 
1464 // TrivialAugmentWide Transform Operation.
1465 struct TrivialAugmentWide::Data {
Datamindspore::dataset::vision::TrivialAugmentWide::Data1466   Data(int32_t num_magnitude_bins, InterpolationMode interpolation, const std::vector<uint8_t> &fill_value)
1467       : num_magnitude_bins_(num_magnitude_bins), interpolation_(interpolation), fill_value_(fill_value) {}
1468   int32_t num_magnitude_bins_;
1469   InterpolationMode interpolation_;
1470   std::vector<uint8_t> fill_value_;
1471 };
1472 
TrivialAugmentWide(int32_t num_magnitude_bins,InterpolationMode interpolation,const std::vector<uint8_t> & fill_value)1473 TrivialAugmentWide::TrivialAugmentWide(int32_t num_magnitude_bins, InterpolationMode interpolation,
1474                                        const std::vector<uint8_t> &fill_value)
1475     : data_(std::make_shared<Data>(num_magnitude_bins, interpolation, fill_value)) {}
1476 
Parse()1477 std::shared_ptr<TensorOperation> TrivialAugmentWide::Parse() {
1478   return std::make_shared<TrivialAugmentWideOperation>(data_->num_magnitude_bins_, data_->interpolation_,
1479                                                        data_->fill_value_);
1480 }
1481 
1482 // UniformAug Transform Operation.
1483 struct UniformAugment::Data {
1484   std::vector<std::shared_ptr<TensorOperation>> transforms_;
1485   int32_t num_ops_;
1486 };
1487 
UniformAugment(const std::vector<TensorTransform * > & transforms,int32_t num_ops)1488 UniformAugment::UniformAugment(const std::vector<TensorTransform *> &transforms, int32_t num_ops)
1489     : data_(std::make_shared<Data>()) {
1490   (void)std::transform(
1491     transforms.begin(), transforms.end(), std::back_inserter(data_->transforms_),
1492     [](TensorTransform *const op) -> std::shared_ptr<TensorOperation> { return op ? op->Parse() : nullptr; });
1493   data_->num_ops_ = num_ops;
1494 }
1495 
UniformAugment(const std::vector<std::shared_ptr<TensorTransform>> & transforms,int32_t num_ops)1496 UniformAugment::UniformAugment(const std::vector<std::shared_ptr<TensorTransform>> &transforms, int32_t num_ops)
1497     : data_(std::make_shared<Data>()) {
1498   (void)std::transform(transforms.begin(), transforms.end(), std::back_inserter(data_->transforms_),
1499                        [](const std::shared_ptr<TensorTransform> &op) -> std::shared_ptr<TensorOperation> {
1500                          return op ? op->Parse() : nullptr;
1501                        });
1502   data_->num_ops_ = num_ops;
1503 }
1504 
UniformAugment(const std::vector<std::reference_wrapper<TensorTransform>> & transforms,int32_t num_ops)1505 UniformAugment::UniformAugment(const std::vector<std::reference_wrapper<TensorTransform>> &transforms, int32_t num_ops)
1506     : data_(std::make_shared<Data>()) {
1507   (void)std::transform(transforms.begin(), transforms.end(), std::back_inserter(data_->transforms_),
1508                        [](TensorTransform &op) -> std::shared_ptr<TensorOperation> { return op.Parse(); });
1509   data_->num_ops_ = num_ops;
1510 }
1511 
Parse()1512 std::shared_ptr<TensorOperation> UniformAugment::Parse() {
1513   return std::make_shared<UniformAugOperation>(data_->transforms_, data_->num_ops_);
1514 }
1515 
1516 // VerticalFlip Transform Operation.
1517 VerticalFlip::VerticalFlip() = default;
1518 
Parse()1519 std::shared_ptr<TensorOperation> VerticalFlip::Parse() { return std::make_shared<VerticalFlipOperation>(); }
1520 
1521 // WriteFile Function.
WriteFile(const std::string & filename,const mindspore::MSTensor & data)1522 Status WriteFile(const std::string &filename, const mindspore::MSTensor &data) {
1523   std::shared_ptr<dataset::Tensor> de_tensor;
1524   RETURN_IF_NOT_OK(Tensor::CreateFromMSTensor(data, &de_tensor));
1525   RETURN_IF_NOT_OK(mindspore::dataset::WriteFile(filename, de_tensor));
1526   return Status::OK();
1527 }
1528 
1529 // WriteJpeg Function.
WriteJpeg(const std::string & filename,const mindspore::MSTensor & image,int quality)1530 Status WriteJpeg(const std::string &filename, const mindspore::MSTensor &image, int quality) {
1531   std::shared_ptr<dataset::Tensor> image_de_tensor;
1532   RETURN_IF_NOT_OK(Tensor::CreateFromMSTensor(image, &image_de_tensor));
1533   RETURN_IF_NOT_OK(mindspore::dataset::WriteJpeg(filename, image_de_tensor, quality));
1534   return Status::OK();
1535 }
1536 
1537 // WritePNG Function.
WritePng(const std::string & filename,const mindspore::MSTensor & image,int compression_level)1538 Status WritePng(const std::string &filename, const mindspore::MSTensor &image, int compression_level) {
1539   std::shared_ptr<dataset::Tensor> image_de_tensor;
1540   RETURN_IF_NOT_OK(Tensor::CreateFromMSTensor(image, &image_de_tensor));
1541   RETURN_IF_NOT_OK(mindspore::dataset::WritePng(filename, image_de_tensor, compression_level));
1542   return Status::OK();
1543 }
1544 #endif  // not ENABLE_ANDROID
1545 }  // namespace vision
1546 }  // namespace dataset
1547 }  // namespace mindspore
1548