1 /**
2 * Copyright 2020-2021 Huawei Technologies Co., Ltd
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "minddata/dataset/include/dataset/vision.h"
18 #ifdef ENABLE_ACL
19 #include "minddata/dataset/include/dataset/vision_ascend.h"
20 #include "minddata/dataset/kernels/ir/vision/ascend_vision_ir.h"
21 #endif
22
23 #include "minddata/dataset/include/dataset/transforms.h"
24 #include "minddata/dataset/kernels/ir/vision/adjust_gamma_ir.h"
25 #include "minddata/dataset/kernels/ir/vision/affine_ir.h"
26 #include "minddata/dataset/kernels/ir/vision/auto_contrast_ir.h"
27 #include "minddata/dataset/kernels/ir/vision/bounding_box_augment_ir.h"
28 #include "minddata/dataset/kernels/ir/vision/center_crop_ir.h"
29 #include "minddata/dataset/kernels/ir/vision/convert_color_ir.h"
30 #include "minddata/dataset/kernels/ir/vision/crop_ir.h"
31 #include "minddata/dataset/kernels/ir/vision/cutmix_batch_ir.h"
32 #include "minddata/dataset/kernels/ir/vision/cutout_ir.h"
33 #include "minddata/dataset/kernels/ir/vision/decode_ir.h"
34 #include "minddata/dataset/kernels/ir/vision/equalize_ir.h"
35 #include "minddata/dataset/kernels/ir/vision/gaussian_blur_ir.h"
36 #include "minddata/dataset/kernels/ir/vision/horizontal_flip_ir.h"
37 #include "minddata/dataset/kernels/ir/vision/hwc_to_chw_ir.h"
38 #include "minddata/dataset/kernels/ir/vision/invert_ir.h"
39 #include "minddata/dataset/kernels/ir/vision/mixup_batch_ir.h"
40 #include "minddata/dataset/kernels/ir/vision/normalize_ir.h"
41 #include "minddata/dataset/kernels/ir/vision/normalize_pad_ir.h"
42 #include "minddata/dataset/kernels/ir/vision/pad_ir.h"
43 #include "minddata/dataset/kernels/ir/vision/random_affine_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_horizontal_flip_ir.h"
50 #include "minddata/dataset/kernels/ir/vision/random_horizontal_flip_with_bbox_ir.h"
51 #include "minddata/dataset/kernels/ir/vision/random_posterize_ir.h"
52 #include "minddata/dataset/kernels/ir/vision/random_resized_crop_ir.h"
53 #include "minddata/dataset/kernels/ir/vision/random_resized_crop_with_bbox_ir.h"
54 #include "minddata/dataset/kernels/ir/vision/random_resize_ir.h"
55 #include "minddata/dataset/kernels/ir/vision/random_resize_with_bbox_ir.h"
56 #include "minddata/dataset/kernels/ir/vision/random_rotation_ir.h"
57 #include "minddata/dataset/kernels/ir/vision/random_select_subpolicy_ir.h"
58 #include "minddata/dataset/kernels/ir/vision/random_sharpness_ir.h"
59 #include "minddata/dataset/kernels/ir/vision/random_solarize_ir.h"
60 #include "minddata/dataset/kernels/ir/vision/random_vertical_flip_ir.h"
61 #include "minddata/dataset/kernels/ir/vision/random_vertical_flip_with_bbox_ir.h"
62 #include "minddata/dataset/kernels/ir/vision/rescale_ir.h"
63 #include "minddata/dataset/kernels/ir/vision/resize_ir.h"
64 #include "minddata/dataset/kernels/ir/vision/resize_preserve_ar_ir.h"
65 #include "minddata/dataset/kernels/ir/vision/resize_with_bbox_ir.h"
66 #include "minddata/dataset/kernels/ir/vision/rgb_to_bgr_ir.h"
67 #include "minddata/dataset/kernels/ir/vision/rgb_to_gray_ir.h"
68 #include "minddata/dataset/kernels/ir/vision/rgba_to_bgr_ir.h"
69 #include "minddata/dataset/kernels/ir/vision/rgba_to_rgb_ir.h"
70 #include "minddata/dataset/kernels/ir/vision/rotate_ir.h"
71 #include "minddata/dataset/kernels/ir/vision/slice_patches_ir.h"
72 #include "minddata/dataset/kernels/ir/vision/softdvpp_decode_random_crop_resize_jpeg_ir.h"
73 #include "minddata/dataset/kernels/ir/vision/softdvpp_decode_resize_jpeg_ir.h"
74 #include "minddata/dataset/kernels/ir/vision/swap_red_blue_ir.h"
75 #include "minddata/dataset/kernels/ir/vision/uniform_aug_ir.h"
76 #include "minddata/dataset/kernels/ir/vision/vertical_flip_ir.h"
77
78 #ifndef ENABLE_ANDROID
79 #include "utils/log_adapter.h"
80 #else
81 #include "mindspore/lite/src/common/log_adapter.h"
82 #endif
83 #include "minddata/dataset/kernels/ir/validators.h"
84
85 // Kernel image headers (in alphabetical order)
86
87 namespace mindspore {
88 namespace dataset {
89
90 // Transform operations for computer vision.
91 namespace vision {
92 // CONSTRUCTORS FOR API CLASSES TO CREATE VISION TENSOR TRANSFORM OPERATIONS
93 // (In alphabetical order)
94
95 // Affine Transform Operation.
96 struct Affine::Data {
Datamindspore::dataset::vision::Affine::Data97 Data(float_t degrees, const std::vector<float> &translation, float scale, const std::vector<float> &shear,
98 InterpolationMode interpolation, const std::vector<uint8_t> &fill_value)
99 : degrees_(degrees),
100 translation_(translation),
101 scale_(scale),
102 shear_(shear),
103 interpolation_(interpolation),
104 fill_value_(fill_value) {}
105 float degrees_;
106 std::vector<float> translation_;
107 float scale_;
108 std::vector<float> shear_;
109 InterpolationMode interpolation_;
110 std::vector<uint8_t> fill_value_;
111 };
112
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)113 Affine::Affine(float_t degrees, const std::vector<float> &translation, float scale, const std::vector<float> &shear,
114 InterpolationMode interpolation, const std::vector<uint8_t> &fill_value)
115 : data_(std::make_shared<Data>(degrees, translation, scale, shear, interpolation, fill_value)) {}
116
Parse()117 std::shared_ptr<TensorOperation> Affine::Parse() {
118 return std::make_shared<AffineOperation>(data_->degrees_, data_->translation_, data_->scale_, data_->shear_,
119 data_->interpolation_, data_->fill_value_);
120 }
121
122 #ifndef ENABLE_ANDROID
123 // AdjustGamma Transform Operation.
124 struct AdjustGamma::Data {
Datamindspore::dataset::vision::AdjustGamma::Data125 Data(float gamma, float gain) : gamma_(gamma), gain_(gain) {}
126 float gamma_;
127 float gain_;
128 };
129
AdjustGamma(float gamma,float gain)130 AdjustGamma::AdjustGamma(float gamma, float gain) : data_(std::make_shared<Data>(gamma, gain)) {}
131
Parse()132 std::shared_ptr<TensorOperation> AdjustGamma::Parse() {
133 return std::make_shared<AdjustGammaOperation>(data_->gamma_, data_->gain_);
134 }
135
136 // AutoContrast Transform Operation.
137 struct AutoContrast::Data {
Datamindspore::dataset::vision::AutoContrast::Data138 Data(float cutoff, const std::vector<uint32_t> &ignore) : cutoff_(cutoff), ignore_(ignore) {}
139 float cutoff_;
140 std::vector<uint32_t> ignore_;
141 };
142
AutoContrast(float cutoff,std::vector<uint32_t> ignore)143 AutoContrast::AutoContrast(float cutoff, std::vector<uint32_t> ignore)
144 : data_(std::make_shared<Data>(cutoff, ignore)) {}
145
Parse()146 std::shared_ptr<TensorOperation> AutoContrast::Parse() {
147 return std::make_shared<AutoContrastOperation>(data_->cutoff_, data_->ignore_);
148 }
149
150 // BoundingBoxAugment Transform Operation.
151 struct BoundingBoxAugment::Data {
152 std::shared_ptr<TensorOperation> transform_;
153 float ratio_;
154 };
155
BoundingBoxAugment(TensorTransform * transform,float ratio)156 BoundingBoxAugment::BoundingBoxAugment(TensorTransform *transform, float ratio) : data_(std::make_shared<Data>()) {
157 data_->transform_ = transform ? transform->Parse() : nullptr;
158 data_->ratio_ = ratio;
159 }
160
BoundingBoxAugment(const std::shared_ptr<TensorTransform> & transform,float ratio)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(const std::reference_wrapper<TensorTransform> transform,float ratio)167 BoundingBoxAugment::BoundingBoxAugment(const std::reference_wrapper<TensorTransform> transform, float ratio)
168 : data_(std::make_shared<Data>()) {
169 data_->transform_ = transform.get().Parse();
170 data_->ratio_ = ratio;
171 }
172
Parse()173 std::shared_ptr<TensorOperation> BoundingBoxAugment::Parse() {
174 return std::make_shared<BoundingBoxAugmentOperation>(data_->transform_, data_->ratio_);
175 }
176 #endif // not ENABLE_ANDROID
177
178 // CenterCrop Transform Operation.
179 struct CenterCrop::Data {
Datamindspore::dataset::vision::CenterCrop::Data180 explicit Data(const std::vector<int32_t> &size) : size_(size) {}
181 std::vector<int32_t> size_;
182 };
183
CenterCrop(std::vector<int32_t> size)184 CenterCrop::CenterCrop(std::vector<int32_t> size) : data_(std::make_shared<Data>(size)) {}
185
Parse()186 std::shared_ptr<TensorOperation> CenterCrop::Parse() { return std::make_shared<CenterCropOperation>(data_->size_); }
187
Parse(const MapTargetDevice & env)188 std::shared_ptr<TensorOperation> CenterCrop::Parse(const MapTargetDevice &env) {
189 if (env == MapTargetDevice::kAscend310) {
190 #ifdef ENABLE_ACL
191 std::vector<uint32_t> usize_;
192 usize_.reserve(data_->size_.size());
193 std::transform(data_->size_.begin(), data_->size_.end(), std::back_inserter(usize_),
194 [](int32_t i) { return (uint32_t)i; });
195 return std::make_shared<DvppCropJpegOperation>(usize_);
196 #endif // ENABLE_ACL
197 } else if (env == MapTargetDevice::kCpu) {
198 return std::make_shared<CenterCropOperation>(data_->size_);
199 }
200 MS_LOG(ERROR) << "Unsupported MapTargetDevice, only supported kCpu and kAscend310.";
201 return nullptr;
202 }
203
204 #ifndef ENABLE_ANDROID
205 // ConvertColor Transform Operation.
206 struct ConvertColor::Data {
Datamindspore::dataset::vision::ConvertColor::Data207 explicit Data(ConvertMode convert_mode) : convert_mode_(convert_mode) {}
208 ConvertMode convert_mode_;
209 };
210
ConvertColor(ConvertMode convert_mode)211 ConvertColor::ConvertColor(ConvertMode convert_mode) : data_(std::make_shared<Data>(convert_mode)) {}
212
Parse()213 std::shared_ptr<TensorOperation> ConvertColor::Parse() {
214 return std::make_shared<ConvertColorOperation>(data_->convert_mode_);
215 }
216 #endif // not ENABLE_ANDROID
217
218 // Crop Transform Operation.
219 struct Crop::Data {
Datamindspore::dataset::vision::Crop::Data220 Data(const std::vector<int32_t> &coordinates, const std::vector<int32_t> &size)
221 : coordinates_(coordinates), size_(size) {}
222 std::vector<int32_t> coordinates_;
223 std::vector<int32_t> size_;
224 };
225
Crop(std::vector<int32_t> coordinates,std::vector<int32_t> size)226 Crop::Crop(std::vector<int32_t> coordinates, std::vector<int32_t> size)
227 : data_(std::make_shared<Data>(coordinates, size)) {}
228
Parse()229 std::shared_ptr<TensorOperation> Crop::Parse() {
230 return std::make_shared<CropOperation>(data_->coordinates_, data_->size_);
231 }
232
233 #ifndef ENABLE_ANDROID
234 // CutMixBatch Transform Operation.
235 struct CutMixBatch::Data {
Datamindspore::dataset::vision::CutMixBatch::Data236 Data(ImageBatchFormat image_batch_format, float alpha, float prob)
237 : image_batch_format_(image_batch_format), alpha_(alpha), prob_(prob) {}
238 float alpha_;
239 float prob_;
240 ImageBatchFormat image_batch_format_;
241 };
242
CutMixBatch(ImageBatchFormat image_batch_format,float alpha,float prob)243 CutMixBatch::CutMixBatch(ImageBatchFormat image_batch_format, float alpha, float prob)
244 : data_(std::make_shared<Data>(image_batch_format, alpha, prob)) {}
245
Parse()246 std::shared_ptr<TensorOperation> CutMixBatch::Parse() {
247 return std::make_shared<CutMixBatchOperation>(data_->image_batch_format_, data_->alpha_, data_->prob_);
248 }
249
250 // CutOutOp.
251 struct CutOut::Data {
Datamindspore::dataset::vision::CutOut::Data252 Data(int32_t length, int32_t num_patches) : length_(length), num_patches_(num_patches) {}
253 int32_t length_;
254 int32_t num_patches_;
255 };
256
CutOut(int32_t length,int32_t num_patches)257 CutOut::CutOut(int32_t length, int32_t num_patches) : data_(std::make_shared<Data>(length, num_patches)) {}
258
Parse()259 std::shared_ptr<TensorOperation> CutOut::Parse() {
260 return std::make_shared<CutOutOperation>(data_->length_, data_->num_patches_);
261 }
262 #endif // not ENABLE_ANDROID
263
264 // Decode Transform Operation.
265 struct Decode::Data {
Datamindspore::dataset::vision::Decode::Data266 explicit Data(bool rgb) : rgb_(rgb) {}
267 bool rgb_;
268 };
269
Decode(bool rgb)270 Decode::Decode(bool rgb) : data_(std::make_shared<Data>(rgb)) {}
271
Parse()272 std::shared_ptr<TensorOperation> Decode::Parse() { return std::make_shared<DecodeOperation>(data_->rgb_); }
273
Parse(const MapTargetDevice & env)274 std::shared_ptr<TensorOperation> Decode::Parse(const MapTargetDevice &env) {
275 if (env == MapTargetDevice::kAscend310) {
276 #ifdef ENABLE_ACL
277 return std::make_shared<DvppDecodeJpegOperation>();
278 #endif // ENABLE_ACL
279 } else if (env == MapTargetDevice::kCpu) {
280 return std::make_shared<DecodeOperation>(data_->rgb_);
281 }
282 MS_LOG(ERROR) << "Unsupported MapTargetDevice, only supported kCpu and kAscend310.";
283 return nullptr;
284 }
285
286 #ifdef ENABLE_ACL
287 // DvppDecodeResize Transform Operation.
288 struct DvppDecodeResizeJpeg::Data {
Datamindspore::dataset::vision::DvppDecodeResizeJpeg::Data289 explicit Data(const std::vector<uint32_t> &resize) : resize_(resize) {}
290 std::vector<uint32_t> resize_;
291 };
292
DvppDecodeResizeJpeg(std::vector<uint32_t> resize)293 DvppDecodeResizeJpeg::DvppDecodeResizeJpeg(std::vector<uint32_t> resize) : data_(std::make_shared<Data>(resize)) {}
294
Parse()295 std::shared_ptr<TensorOperation> DvppDecodeResizeJpeg::Parse() {
296 return std::make_shared<DvppDecodeResizeOperation>(data_->resize_);
297 }
298
Parse(const MapTargetDevice & env)299 std::shared_ptr<TensorOperation> DvppDecodeResizeJpeg::Parse(const MapTargetDevice &env) {
300 if (env == MapTargetDevice::kAscend310) {
301 return std::make_shared<DvppDecodeResizeOperation>(data_->resize_);
302 }
303 MS_LOG(ERROR) << "Unsupported MapTargetDevice, only supported kAscend310.";
304 return nullptr;
305 }
306
307 // DvppDecodeResizeCrop Transform Operation.
308 struct DvppDecodeResizeCropJpeg::Data {
Datamindspore::dataset::vision::DvppDecodeResizeCropJpeg::Data309 Data(const std::vector<uint32_t> &crop, const std::vector<uint32_t> &resize) : crop_(crop), resize_(resize) {}
310 std::vector<uint32_t> crop_;
311 std::vector<uint32_t> resize_;
312 };
313
DvppDecodeResizeCropJpeg(std::vector<uint32_t> crop,std::vector<uint32_t> resize)314 DvppDecodeResizeCropJpeg::DvppDecodeResizeCropJpeg(std::vector<uint32_t> crop, std::vector<uint32_t> resize)
315 : data_(std::make_shared<Data>(crop, resize)) {}
316
Parse()317 std::shared_ptr<TensorOperation> DvppDecodeResizeCropJpeg::Parse() {
318 return std::make_shared<DvppDecodeResizeCropOperation>(data_->crop_, data_->resize_);
319 }
320
Parse(const MapTargetDevice & env)321 std::shared_ptr<TensorOperation> DvppDecodeResizeCropJpeg::Parse(const MapTargetDevice &env) {
322 if (env == MapTargetDevice::kAscend310) {
323 return std::make_shared<DvppDecodeResizeCropOperation>(data_->crop_, data_->resize_);
324 }
325 MS_LOG(ERROR) << "Unsupported MapTargetDevice, only supported kAscend310.";
326 return nullptr;
327 }
328
329 // DvppDecodePng Transform Operation.
DvppDecodePng()330 DvppDecodePng::DvppDecodePng() {}
331
Parse()332 std::shared_ptr<TensorOperation> DvppDecodePng::Parse() { return std::make_shared<DvppDecodePngOperation>(); }
333
Parse(const MapTargetDevice & env)334 std::shared_ptr<TensorOperation> DvppDecodePng::Parse(const MapTargetDevice &env) {
335 if (env == MapTargetDevice::kAscend310) {
336 return std::make_shared<DvppDecodePngOperation>();
337 }
338 MS_LOG(ERROR) << "Unsupported MapTargetDevice, only supported kAscend310.";
339 return nullptr;
340 }
341 #endif // ENABLE_ACL
342
343 #ifndef ENABLE_ANDROID
344 // Equalize Transform Operation.
Equalize()345 Equalize::Equalize() {}
346
Parse()347 std::shared_ptr<TensorOperation> Equalize::Parse() { return std::make_shared<EqualizeOperation>(); }
348 #endif // not ENABLE_ANDROID
349
350 // GaussianBlur Transform Operation.
351 struct GaussianBlur::Data {
Datamindspore::dataset::vision::GaussianBlur::Data352 Data(const std::vector<int32_t> &kernel_size, const std::vector<float> &sigma)
353 : kernel_size_(kernel_size), sigma_(sigma) {}
354 std::vector<int32_t> kernel_size_;
355 std::vector<float> sigma_;
356 };
357
GaussianBlur(const std::vector<int32_t> & kernel_size,const std::vector<float> & sigma)358 GaussianBlur::GaussianBlur(const std::vector<int32_t> &kernel_size, const std::vector<float> &sigma)
359 : data_(std::make_shared<Data>(kernel_size, sigma)) {}
360
Parse()361 std::shared_ptr<TensorOperation> GaussianBlur::Parse() {
362 return std::make_shared<GaussianBlurOperation>(data_->kernel_size_, data_->sigma_);
363 }
364
365 #ifndef ENABLE_ANDROID
366 // HorizontalFlip Transform Operation.
HorizontalFlip()367 HorizontalFlip::HorizontalFlip() {}
368
Parse()369 std::shared_ptr<TensorOperation> HorizontalFlip::Parse() { return std::make_shared<HorizontalFlipOperation>(); }
370
371 // HwcToChw Transform Operation.
HWC2CHW()372 HWC2CHW::HWC2CHW() {}
373
Parse()374 std::shared_ptr<TensorOperation> HWC2CHW::Parse() { return std::make_shared<HwcToChwOperation>(); }
375
376 // Invert Transform Operation.
Invert()377 Invert::Invert() {}
378
Parse()379 std::shared_ptr<TensorOperation> Invert::Parse() { return std::make_shared<InvertOperation>(); }
380
381 // MixUpBatch Transform Operation.
382 struct MixUpBatch::Data {
Datamindspore::dataset::vision::MixUpBatch::Data383 explicit Data(float alpha) : alpha_(alpha) {}
384 float alpha_;
385 };
386
MixUpBatch(float alpha)387 MixUpBatch::MixUpBatch(float alpha) : data_(std::make_shared<Data>(alpha)) {}
388
Parse()389 std::shared_ptr<TensorOperation> MixUpBatch::Parse() { return std::make_shared<MixUpBatchOperation>(data_->alpha_); }
390 #endif // not ENABLE_ANDROID
391
392 // Normalize Transform Operation.
393 struct Normalize::Data {
Datamindspore::dataset::vision::Normalize::Data394 Data(const std::vector<float> &mean, const std::vector<float> &std) : mean_(mean), std_(std) {}
395 std::vector<float> mean_;
396 std::vector<float> std_;
397 };
398
Normalize(std::vector<float> mean,std::vector<float> std)399 Normalize::Normalize(std::vector<float> mean, std::vector<float> std) : data_(std::make_shared<Data>(mean, std)) {}
400
Parse()401 std::shared_ptr<TensorOperation> Normalize::Parse() {
402 return std::make_shared<NormalizeOperation>(data_->mean_, data_->std_);
403 }
404
Parse(const MapTargetDevice & env)405 std::shared_ptr<TensorOperation> Normalize::Parse(const MapTargetDevice &env) {
406 if (env == MapTargetDevice::kAscend310) {
407 #ifdef ENABLE_ACL
408 return std::make_shared<DvppNormalizeOperation>(data_->mean_, data_->std_);
409 #endif // ENABLE_ACL
410 } else if (env == MapTargetDevice::kCpu) {
411 return std::make_shared<NormalizeOperation>(data_->mean_, data_->std_);
412 }
413 MS_LOG(ERROR) << "Unsupported MapTargetDevice, only supported kCpu and kAscend310.";
414 return nullptr;
415 }
416
417 #ifndef ENABLE_ANDROID
418 // NormalizePad Transform Operation.
419 struct NormalizePad::Data {
Datamindspore::dataset::vision::NormalizePad::Data420 Data(const std::vector<float> &mean, const std::vector<float> &std, const std::string &dtype)
421 : mean_(mean), std_(std), dtype_(dtype) {}
422 std::vector<float> mean_;
423 std::vector<float> std_;
424 std::string dtype_;
425 };
426
NormalizePad(const std::vector<float> & mean,const std::vector<float> & std,const std::vector<char> & dtype)427 NormalizePad::NormalizePad(const std::vector<float> &mean, const std::vector<float> &std,
428 const std::vector<char> &dtype)
429 : data_(std::make_shared<Data>(mean, std, CharToString(dtype))) {}
430
Parse()431 std::shared_ptr<TensorOperation> NormalizePad::Parse() {
432 return std::make_shared<NormalizePadOperation>(data_->mean_, data_->std_, data_->dtype_);
433 }
434
435 // Pad Transform Operation.
436 struct Pad::Data {
Datamindspore::dataset::vision::Pad::Data437 Data(const std::vector<int32_t> &padding, const std::vector<uint8_t> &fill_value, BorderType padding_mode)
438 : padding_(padding), fill_value_(fill_value), padding_mode_(padding_mode) {}
439 std::vector<int32_t> padding_;
440 std::vector<uint8_t> fill_value_;
441 BorderType padding_mode_;
442 };
443
Pad(std::vector<int32_t> padding,std::vector<uint8_t> fill_value,BorderType padding_mode)444 Pad::Pad(std::vector<int32_t> padding, std::vector<uint8_t> fill_value, BorderType padding_mode)
445 : data_(std::make_shared<Data>(padding, fill_value, padding_mode)) {}
446
Parse()447 std::shared_ptr<TensorOperation> Pad::Parse() {
448 return std::make_shared<PadOperation>(data_->padding_, data_->fill_value_, data_->padding_mode_);
449 }
450 #endif // not ENABLE_ANDROID
451
452 // RandomAffine Transform Operation.
453 struct RandomAffine::Data {
Datamindspore::dataset::vision::RandomAffine::Data454 Data(const std::vector<float_t> °rees, const std::vector<float_t> &translate_range,
455 const std::vector<float_t> &scale_range, const std::vector<float_t> &shear_ranges,
456 InterpolationMode interpolation, const std::vector<uint8_t> &fill_value)
457 : degrees_(degrees),
458 translate_range_(translate_range),
459 scale_range_(scale_range),
460 shear_ranges_(shear_ranges),
461 interpolation_(interpolation),
462 fill_value_(fill_value) {}
463 std::vector<float_t> degrees_; // min_degree, max_degree
464 std::vector<float_t> translate_range_; // maximum x translation percentage, maximum y translation percentage
465 std::vector<float_t> scale_range_; // min_scale, max_scale
466 std::vector<float_t> shear_ranges_; // min_x_shear, max_x_shear, min_y_shear, max_y_shear
467 InterpolationMode interpolation_;
468 std::vector<uint8_t> fill_value_;
469 };
470
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)471 RandomAffine::RandomAffine(const std::vector<float_t> °rees, const std::vector<float_t> &translate_range,
472 const std::vector<float_t> &scale_range, const std::vector<float_t> &shear_ranges,
473 InterpolationMode interpolation, const std::vector<uint8_t> &fill_value)
474 : data_(std::make_shared<Data>(degrees, translate_range, scale_range, shear_ranges, interpolation, fill_value)) {}
475
Parse()476 std::shared_ptr<TensorOperation> RandomAffine::Parse() {
477 return std::make_shared<RandomAffineOperation>(data_->degrees_, data_->translate_range_, data_->scale_range_,
478 data_->shear_ranges_, data_->interpolation_, data_->fill_value_);
479 }
480
481 #ifndef ENABLE_ANDROID
482 // RandomColor Transform Operation.
483 struct RandomColor::Data {
Datamindspore::dataset::vision::RandomColor::Data484 Data(float t_lb, float t_ub) : t_lb_(t_lb), t_ub_(t_ub) {}
485 float t_lb_;
486 float t_ub_;
487 };
488
RandomColor(float t_lb,float t_ub)489 RandomColor::RandomColor(float t_lb, float t_ub) : data_(std::make_shared<Data>(t_lb, t_ub)) {}
490
Parse()491 std::shared_ptr<TensorOperation> RandomColor::Parse() {
492 return std::make_shared<RandomColorOperation>(data_->t_lb_, data_->t_ub_);
493 }
494
495 // RandomColorAdjust Transform Operation.
496 struct RandomColorAdjust::Data {
Datamindspore::dataset::vision::RandomColorAdjust::Data497 Data(const std::vector<float> &brightness, const std::vector<float> &contrast, const std::vector<float> &saturation,
498 const std::vector<float> &hue)
499 : brightness_(brightness), contrast_(contrast), saturation_(saturation), hue_(hue) {}
500 std::vector<float> brightness_;
501 std::vector<float> contrast_;
502 std::vector<float> saturation_;
503 std::vector<float> hue_;
504 };
505
RandomColorAdjust(std::vector<float> brightness,std::vector<float> contrast,std::vector<float> saturation,std::vector<float> hue)506 RandomColorAdjust::RandomColorAdjust(std::vector<float> brightness, std::vector<float> contrast,
507 std::vector<float> saturation, std::vector<float> hue)
508 : data_(std::make_shared<Data>(brightness, contrast, saturation, hue)) {}
509
Parse()510 std::shared_ptr<TensorOperation> RandomColorAdjust::Parse() {
511 return std::make_shared<RandomColorAdjustOperation>(data_->brightness_, data_->contrast_, data_->saturation_,
512 data_->hue_);
513 }
514
515 // RandomCrop Transform Operation.
516 struct RandomCrop::Data {
Datamindspore::dataset::vision::RandomCrop::Data517 Data(const std::vector<int32_t> &size, const std::vector<int32_t> &padding, bool pad_if_needed,
518 const std::vector<uint8_t> &fill_value, BorderType padding_mode)
519 : size_(size),
520 padding_(padding),
521 pad_if_needed_(pad_if_needed),
522 fill_value_(fill_value),
523 padding_mode_(padding_mode) {}
524 std::vector<int32_t> size_;
525 std::vector<int32_t> padding_;
526 bool pad_if_needed_;
527 std::vector<uint8_t> fill_value_;
528 BorderType padding_mode_;
529 };
530
RandomCrop(std::vector<int32_t> size,std::vector<int32_t> padding,bool pad_if_needed,std::vector<uint8_t> fill_value,BorderType padding_mode)531 RandomCrop::RandomCrop(std::vector<int32_t> size, std::vector<int32_t> padding, bool pad_if_needed,
532 std::vector<uint8_t> fill_value, BorderType padding_mode)
533 : data_(std::make_shared<Data>(size, padding, pad_if_needed, fill_value, padding_mode)) {}
534
Parse()535 std::shared_ptr<TensorOperation> RandomCrop::Parse() {
536 return std::make_shared<RandomCropOperation>(data_->size_, data_->padding_, data_->pad_if_needed_, data_->fill_value_,
537 data_->padding_mode_);
538 }
539
540 // RandomCropDecodeResize Transform Operation.
541 struct RandomCropDecodeResize::Data {
Datamindspore::dataset::vision::RandomCropDecodeResize::Data542 Data(const std::vector<int32_t> &size, const std::vector<float> &scale, const std::vector<float> &ratio,
543 InterpolationMode interpolation, int32_t max_attempts)
544 : size_(size), scale_(scale), ratio_(ratio), interpolation_(interpolation), max_attempts_(max_attempts) {}
545 std::vector<int32_t> size_;
546 std::vector<float> scale_;
547 std::vector<float> ratio_;
548 InterpolationMode interpolation_;
549 int32_t max_attempts_;
550 };
551
RandomCropDecodeResize(std::vector<int32_t> size,std::vector<float> scale,std::vector<float> ratio,InterpolationMode interpolation,int32_t max_attempts)552 RandomCropDecodeResize::RandomCropDecodeResize(std::vector<int32_t> size, std::vector<float> scale,
553 std::vector<float> ratio, InterpolationMode interpolation,
554 int32_t max_attempts)
555 : data_(std::make_shared<Data>(size, scale, ratio, interpolation, max_attempts)) {}
556
Parse()557 std::shared_ptr<TensorOperation> RandomCropDecodeResize::Parse() {
558 return std::make_shared<RandomCropDecodeResizeOperation>(data_->size_, data_->scale_, data_->ratio_,
559 data_->interpolation_, data_->max_attempts_);
560 }
561
562 // RandomCropWithBBox Transform Operation.
563 struct RandomCropWithBBox::Data {
Datamindspore::dataset::vision::RandomCropWithBBox::Data564 Data(const std::vector<int32_t> &size, const std::vector<int32_t> &padding, bool pad_if_needed,
565 const std::vector<uint8_t> &fill_value, BorderType padding_mode)
566 : size_(size),
567 padding_(padding),
568 pad_if_needed_(pad_if_needed),
569 fill_value_(fill_value),
570 padding_mode_(padding_mode) {}
571 std::vector<int32_t> size_;
572 std::vector<int32_t> padding_;
573 bool pad_if_needed_;
574 std::vector<uint8_t> fill_value_;
575 BorderType padding_mode_;
576 };
577
RandomCropWithBBox(std::vector<int32_t> size,std::vector<int32_t> padding,bool pad_if_needed,std::vector<uint8_t> fill_value,BorderType padding_mode)578 RandomCropWithBBox::RandomCropWithBBox(std::vector<int32_t> size, std::vector<int32_t> padding, bool pad_if_needed,
579 std::vector<uint8_t> fill_value, BorderType padding_mode)
580 : data_(std::make_shared<Data>(size, padding, pad_if_needed, fill_value, padding_mode)) {}
581
Parse()582 std::shared_ptr<TensorOperation> RandomCropWithBBox::Parse() {
583 return std::make_shared<RandomCropWithBBoxOperation>(data_->size_, data_->padding_, data_->pad_if_needed_,
584 data_->fill_value_, data_->padding_mode_);
585 }
586
587 // RandomHorizontalFlip.
588 struct RandomHorizontalFlip::Data {
Datamindspore::dataset::vision::RandomHorizontalFlip::Data589 explicit Data(float prob) : probability_(prob) {}
590 float probability_;
591 };
592
RandomHorizontalFlip(float prob)593 RandomHorizontalFlip::RandomHorizontalFlip(float prob) : data_(std::make_shared<Data>(prob)) {}
594
Parse()595 std::shared_ptr<TensorOperation> RandomHorizontalFlip::Parse() {
596 return std::make_shared<RandomHorizontalFlipOperation>(data_->probability_);
597 }
598
599 // RandomHorizontalFlipWithBBox
600 struct RandomHorizontalFlipWithBBox::Data {
Datamindspore::dataset::vision::RandomHorizontalFlipWithBBox::Data601 explicit Data(float prob) : probability_(prob) {}
602 float probability_;
603 };
604
RandomHorizontalFlipWithBBox(float prob)605 RandomHorizontalFlipWithBBox::RandomHorizontalFlipWithBBox(float prob) : data_(std::make_shared<Data>(prob)) {}
606
Parse()607 std::shared_ptr<TensorOperation> RandomHorizontalFlipWithBBox::Parse() {
608 return std::make_shared<RandomHorizontalFlipWithBBoxOperation>(data_->probability_);
609 }
610
611 // RandomPosterize Transform Operation.
612 struct RandomPosterize::Data {
Datamindspore::dataset::vision::RandomPosterize::Data613 explicit Data(const std::vector<uint8_t> &bit_range) : bit_range_(bit_range) {}
614 std::vector<uint8_t> bit_range_;
615 };
616
RandomPosterize(const std::vector<uint8_t> & bit_range)617 RandomPosterize::RandomPosterize(const std::vector<uint8_t> &bit_range) : data_(std::make_shared<Data>(bit_range)) {}
618
Parse()619 std::shared_ptr<TensorOperation> RandomPosterize::Parse() {
620 return std::make_shared<RandomPosterizeOperation>(data_->bit_range_);
621 }
622
623 // RandomResize Transform Operation.
624 struct RandomResize::Data {
Datamindspore::dataset::vision::RandomResize::Data625 explicit Data(const std::vector<int32_t> &size) : size_(size) {}
626 std::vector<int32_t> size_;
627 };
628
RandomResize(std::vector<int32_t> size)629 RandomResize::RandomResize(std::vector<int32_t> size) : data_(std::make_shared<Data>(size)) {}
630
Parse()631 std::shared_ptr<TensorOperation> RandomResize::Parse() { return std::make_shared<RandomResizeOperation>(data_->size_); }
632
633 // RandomResizeWithBBox Transform Operation.
634 struct RandomResizeWithBBox::Data {
Datamindspore::dataset::vision::RandomResizeWithBBox::Data635 explicit Data(const std::vector<int32_t> &size) : size_(size) {}
636 std::vector<int32_t> size_;
637 };
638
RandomResizeWithBBox(std::vector<int32_t> size)639 RandomResizeWithBBox::RandomResizeWithBBox(std::vector<int32_t> size) : data_(std::make_shared<Data>(size)) {}
640
Parse()641 std::shared_ptr<TensorOperation> RandomResizeWithBBox::Parse() {
642 return std::make_shared<RandomResizeWithBBoxOperation>(data_->size_);
643 }
644
645 // RandomResizedCrop Transform Operation.
646 struct RandomResizedCrop::Data {
Datamindspore::dataset::vision::RandomResizedCrop::Data647 Data(const std::vector<int32_t> &size, const std::vector<float> &scale, const std::vector<float> &ratio,
648 InterpolationMode interpolation, int32_t max_attempts)
649 : size_(size), scale_(scale), ratio_(ratio), interpolation_(interpolation), max_attempts_(max_attempts) {}
650 std::vector<int32_t> size_;
651 std::vector<float> scale_;
652 std::vector<float> ratio_;
653 InterpolationMode interpolation_;
654 int32_t max_attempts_;
655 };
656
RandomResizedCrop(std::vector<int32_t> size,std::vector<float> scale,std::vector<float> ratio,InterpolationMode interpolation,int32_t max_attempts)657 RandomResizedCrop::RandomResizedCrop(std::vector<int32_t> size, std::vector<float> scale, std::vector<float> ratio,
658 InterpolationMode interpolation, int32_t max_attempts)
659 : data_(std::make_shared<Data>(size, scale, ratio, interpolation, max_attempts)) {}
660
Parse()661 std::shared_ptr<TensorOperation> RandomResizedCrop::Parse() {
662 return std::make_shared<RandomResizedCropOperation>(data_->size_, data_->scale_, data_->ratio_, data_->interpolation_,
663 data_->max_attempts_);
664 }
665
666 // RandomResizedCrop Transform Operation.
667 struct RandomResizedCropWithBBox::Data {
Datamindspore::dataset::vision::RandomResizedCropWithBBox::Data668 Data(const std::vector<int32_t> &size, const std::vector<float> &scale, const std::vector<float> &ratio,
669 InterpolationMode interpolation, int32_t max_attempts)
670 : size_(size), scale_(scale), ratio_(ratio), interpolation_(interpolation), max_attempts_(max_attempts) {}
671 std::vector<int32_t> size_;
672 std::vector<float> scale_;
673 std::vector<float> ratio_;
674 InterpolationMode interpolation_;
675 int32_t max_attempts_;
676 };
677
RandomResizedCropWithBBox(std::vector<int32_t> size,std::vector<float> scale,std::vector<float> ratio,InterpolationMode interpolation,int32_t max_attempts)678 RandomResizedCropWithBBox::RandomResizedCropWithBBox(std::vector<int32_t> size, std::vector<float> scale,
679 std::vector<float> ratio, InterpolationMode interpolation,
680 int32_t max_attempts)
681 : data_(std::make_shared<Data>(size, scale, ratio, interpolation, max_attempts)) {}
682
Parse()683 std::shared_ptr<TensorOperation> RandomResizedCropWithBBox::Parse() {
684 return std::make_shared<RandomResizedCropWithBBoxOperation>(data_->size_, data_->scale_, data_->ratio_,
685 data_->interpolation_, data_->max_attempts_);
686 }
687
688 // RandomRotation Transform Operation.
689 struct RandomRotation::Data {
Datamindspore::dataset::vision::RandomRotation::Data690 Data(const std::vector<float> °rees, InterpolationMode resample, bool expand, const std::vector<float> ¢er,
691 const std::vector<uint8_t> &fill_value)
692 : degrees_(degrees), interpolation_mode_(resample), center_(center), expand_(expand), fill_value_(fill_value) {}
693 std::vector<float> degrees_;
694 InterpolationMode interpolation_mode_;
695 std::vector<float> center_;
696 bool expand_;
697 std::vector<uint8_t> fill_value_;
698 };
699
RandomRotation(std::vector<float> degrees,InterpolationMode resample,bool expand,std::vector<float> center,std::vector<uint8_t> fill_value)700 RandomRotation::RandomRotation(std::vector<float> degrees, InterpolationMode resample, bool expand,
701 std::vector<float> center, std::vector<uint8_t> fill_value)
702 : data_(std::make_shared<Data>(degrees, resample, expand, center, fill_value)) {}
703
Parse()704 std::shared_ptr<TensorOperation> RandomRotation::Parse() {
705 return std::make_shared<RandomRotationOperation>(data_->degrees_, data_->interpolation_mode_, data_->expand_,
706 data_->center_, data_->fill_value_);
707 }
708
709 // RandomSelectSubpolicy Transform Operation.
710 struct RandomSelectSubpolicy::Data {
711 std::vector<std::vector<std::pair<std::shared_ptr<TensorOperation>, double>>> policy_;
712 };
713
RandomSelectSubpolicy(const std::vector<std::vector<std::pair<TensorTransform *,double>>> & policy)714 RandomSelectSubpolicy::RandomSelectSubpolicy(
715 const std::vector<std::vector<std::pair<TensorTransform *, double>>> &policy)
716 : data_(std::make_shared<Data>()) {
717 for (uint32_t i = 0; i < policy.size(); i++) {
718 std::vector<std::pair<std::shared_ptr<TensorOperation>, double>> subpolicy;
719
720 for (uint32_t j = 0; j < policy[i].size(); j++) {
721 TensorTransform *op = policy[i][j].first;
722 std::shared_ptr<TensorOperation> operation = (op ? op->Parse() : nullptr);
723 double prob = policy[i][j].second;
724 subpolicy.emplace_back(std::move(std::make_pair(operation, prob)));
725 }
726 data_->policy_.emplace_back(subpolicy);
727 }
728 }
729
RandomSelectSubpolicy(const std::vector<std::vector<std::pair<std::shared_ptr<TensorTransform>,double>>> & policy)730 RandomSelectSubpolicy::RandomSelectSubpolicy(
731 const std::vector<std::vector<std::pair<std::shared_ptr<TensorTransform>, double>>> &policy)
732 : data_(std::make_shared<Data>()) {
733 for (uint32_t i = 0; i < policy.size(); i++) {
734 std::vector<std::pair<std::shared_ptr<TensorOperation>, double>> subpolicy;
735
736 for (uint32_t j = 0; j < policy[i].size(); j++) {
737 std::shared_ptr<TensorTransform> op = policy[i][j].first;
738 std::shared_ptr<TensorOperation> operation = (op ? op->Parse() : nullptr);
739 double prob = policy[i][j].second;
740 subpolicy.emplace_back(std::move(std::make_pair(operation, prob)));
741 }
742 data_->policy_.emplace_back(subpolicy);
743 }
744 }
745
RandomSelectSubpolicy(const std::vector<std::vector<std::pair<std::reference_wrapper<TensorTransform>,double>>> & policy)746 RandomSelectSubpolicy::RandomSelectSubpolicy(
747 const std::vector<std::vector<std::pair<std::reference_wrapper<TensorTransform>, double>>> &policy)
748 : data_(std::make_shared<Data>()) {
749 for (int32_t i = 0; i < policy.size(); i++) {
750 std::vector<std::pair<std::shared_ptr<TensorOperation>, double>> subpolicy;
751
752 for (int32_t j = 0; j < policy[i].size(); j++) {
753 TensorTransform &op = policy[i][j].first;
754 std::shared_ptr<TensorOperation> operation = op.Parse();
755 double prob = policy[i][j].second;
756 subpolicy.emplace_back(std::move(std::make_pair(operation, prob)));
757 }
758 data_->policy_.emplace_back(subpolicy);
759 }
760 }
761
Parse()762 std::shared_ptr<TensorOperation> RandomSelectSubpolicy::Parse() {
763 return std::make_shared<RandomSelectSubpolicyOperation>(data_->policy_);
764 }
765
766 // RandomSharpness Transform Operation.
767 struct RandomSharpness::Data {
Datamindspore::dataset::vision::RandomSharpness::Data768 explicit Data(const std::vector<float> °rees) : degrees_(degrees) {}
769 std::vector<float> degrees_;
770 };
771
RandomSharpness(std::vector<float> degrees)772 RandomSharpness::RandomSharpness(std::vector<float> degrees) : data_(std::make_shared<Data>(degrees)) {}
773
Parse()774 std::shared_ptr<TensorOperation> RandomSharpness::Parse() {
775 return std::make_shared<RandomSharpnessOperation>(data_->degrees_);
776 }
777
778 // RandomSolarize Transform Operation.
779 struct RandomSolarize::Data {
Datamindspore::dataset::vision::RandomSolarize::Data780 explicit Data(const std::vector<uint8_t> &threshold) : threshold_(threshold) {}
781 std::vector<uint8_t> threshold_;
782 };
783
RandomSolarize(std::vector<uint8_t> threshold)784 RandomSolarize::RandomSolarize(std::vector<uint8_t> threshold) : data_(std::make_shared<Data>(threshold)) {}
785
Parse()786 std::shared_ptr<TensorOperation> RandomSolarize::Parse() {
787 return std::make_shared<RandomSolarizeOperation>(data_->threshold_);
788 }
789
790 // RandomVerticalFlip Transform Operation.
791 struct RandomVerticalFlip::Data {
Datamindspore::dataset::vision::RandomVerticalFlip::Data792 explicit Data(float prob) : probability_(prob) {}
793 float probability_;
794 };
795
RandomVerticalFlip(float prob)796 RandomVerticalFlip::RandomVerticalFlip(float prob) : data_(std::make_shared<Data>(prob)) {}
797
Parse()798 std::shared_ptr<TensorOperation> RandomVerticalFlip::Parse() {
799 return std::make_shared<RandomVerticalFlipOperation>(data_->probability_);
800 }
801
802 // RandomVerticalFlipWithBBox Transform Operation.
803 struct RandomVerticalFlipWithBBox::Data {
Datamindspore::dataset::vision::RandomVerticalFlipWithBBox::Data804 explicit Data(float prob) : probability_(prob) {}
805 float probability_;
806 };
807
RandomVerticalFlipWithBBox(float prob)808 RandomVerticalFlipWithBBox::RandomVerticalFlipWithBBox(float prob) : data_(std::make_shared<Data>(prob)) {}
809
Parse()810 std::shared_ptr<TensorOperation> RandomVerticalFlipWithBBox::Parse() {
811 return std::make_shared<RandomVerticalFlipWithBBoxOperation>(data_->probability_);
812 }
813
814 // Rescale Transform Operation.
815 struct Rescale::Data {
Datamindspore::dataset::vision::Rescale::Data816 Data(float rescale, float shift) : rescale_(rescale), shift_(shift) {}
817 float rescale_;
818 float shift_;
819 };
820
Rescale(float rescale,float shift)821 Rescale::Rescale(float rescale, float shift) : data_(std::make_shared<Data>(rescale, shift)) {}
822
Parse()823 std::shared_ptr<TensorOperation> Rescale::Parse() {
824 return std::make_shared<RescaleOperation>(data_->rescale_, data_->shift_);
825 }
826 #endif // not ENABLE_ANDROID
827
828 // Resize Transform Operation.
829 struct Resize::Data {
Datamindspore::dataset::vision::Resize::Data830 Data(const std::vector<int32_t> &size, InterpolationMode interpolation)
831 : size_(size), interpolation_(interpolation) {}
832 std::vector<int32_t> size_;
833 InterpolationMode interpolation_;
834 };
835
Resize(std::vector<int32_t> size,InterpolationMode interpolation)836 Resize::Resize(std::vector<int32_t> size, InterpolationMode interpolation)
837 : data_(std::make_shared<Data>(size, interpolation)) {}
838
Parse()839 std::shared_ptr<TensorOperation> Resize::Parse() {
840 return std::make_shared<ResizeOperation>(data_->size_, data_->interpolation_);
841 }
842
Parse(const MapTargetDevice & env)843 std::shared_ptr<TensorOperation> Resize::Parse(const MapTargetDevice &env) {
844 if (env == MapTargetDevice::kAscend310) {
845 #ifdef ENABLE_ACL
846 std::vector<uint32_t> usize_;
847 usize_.reserve(data_->size_.size());
848 std::transform(data_->size_.begin(), data_->size_.end(), std::back_inserter(usize_),
849 [](int32_t i) { return (uint32_t)i; });
850 return std::make_shared<DvppResizeJpegOperation>(usize_);
851 #endif // ENABLE_ACL
852 } else if (env == MapTargetDevice::kCpu) {
853 return std::make_shared<ResizeOperation>(data_->size_, data_->interpolation_);
854 }
855 MS_LOG(ERROR) << "Unsupported MapTargetDevice, only supported kCpu and kAscend310.";
856 return nullptr;
857 }
858
859 // ResizePreserveAR Transform Operation.
860 struct ResizePreserveAR::Data {
Datamindspore::dataset::vision::ResizePreserveAR::Data861 Data(int32_t height, int32_t width, int32_t img_orientation)
862 : height_(height), width_(width), img_orientation_(img_orientation) {}
863 int32_t height_;
864 int32_t width_;
865 int32_t img_orientation_;
866 };
867
ResizePreserveAR(int32_t height,int32_t width,int32_t img_orientation)868 ResizePreserveAR::ResizePreserveAR(int32_t height, int32_t width, int32_t img_orientation)
869 : data_(std::make_shared<Data>(height, width, img_orientation)) {}
870
Parse()871 std::shared_ptr<TensorOperation> ResizePreserveAR::Parse() {
872 return std::make_shared<ResizePreserveAROperation>(data_->height_, data_->width_, data_->img_orientation_);
873 }
874
875 // Rotate Transform Operation.
876 struct Rotate::Data {
Datamindspore::dataset::vision::Rotate::Data877 Data(const float °rees, InterpolationMode resample, bool expand, const std::vector<float> ¢er,
878 const std::vector<uint8_t> &fill_value)
879 : degrees_(degrees), interpolation_mode_(resample), center_(center), expand_(expand), fill_value_(fill_value) {}
Datamindspore::dataset::vision::Rotate::Data880 explicit Data(const FixRotationAngle &angle_id) : angle_id_(angle_id), lite_impl_(true) {}
881 FixRotationAngle angle_id_{FixRotationAngle::k0Degree};
882 bool lite_impl_{false};
883 float degrees_{0};
884 InterpolationMode interpolation_mode_{InterpolationMode::kNearestNeighbour};
885 std::vector<float> center_{{}};
886 bool expand_{false};
887 std::vector<uint8_t> fill_value_{0, 0, 0};
888 };
889
Rotate(FixRotationAngle angle_id)890 Rotate::Rotate(FixRotationAngle angle_id) : data_(std::make_shared<Data>(angle_id)) {}
891
Rotate(float degrees,InterpolationMode resample,bool expand,std::vector<float> center,std::vector<uint8_t> fill_value)892 Rotate::Rotate(float degrees, InterpolationMode resample, bool expand, std::vector<float> center,
893 std::vector<uint8_t> fill_value)
894 : data_(std::make_shared<Data>(degrees, resample, expand, center, fill_value)) {}
895
Parse()896 std::shared_ptr<TensorOperation> Rotate::Parse() {
897 #ifndef ENABLE_ANDROID
898 if (!data_->lite_impl_) {
899 return std::make_shared<RotateOperation>(data_->degrees_, data_->interpolation_mode_, data_->expand_,
900 data_->center_, data_->fill_value_);
901 }
902 #else
903 if (data_->lite_impl_) {
904 return std::make_shared<RotateOperation>(data_->angle_id_);
905 }
906 #endif // not ENABLE_ANDROID
907 std::string platform = data_->lite_impl_ ? "Cloud" : "Android";
908 MS_LOG(ERROR) << "This Rotate API is not supported for " + platform + ", use another Rotate API.";
909 return nullptr;
910 }
911
912 #ifndef ENABLE_ANDROID
913 // ResizeWithBBox Transform Operation.
914 struct ResizeWithBBox::Data {
Datamindspore::dataset::vision::ResizeWithBBox::Data915 Data(const std::vector<int32_t> &size, InterpolationMode interpolation)
916 : size_(size), interpolation_(interpolation) {}
917 std::vector<int32_t> size_;
918 InterpolationMode interpolation_;
919 };
920
ResizeWithBBox(std::vector<int32_t> size,InterpolationMode interpolation)921 ResizeWithBBox::ResizeWithBBox(std::vector<int32_t> size, InterpolationMode interpolation)
922 : data_(std::make_shared<Data>(size, interpolation)) {}
923
Parse()924 std::shared_ptr<TensorOperation> ResizeWithBBox::Parse() {
925 return std::make_shared<ResizeWithBBoxOperation>(data_->size_, data_->interpolation_);
926 }
927 #endif // not ENABLE_ANDROID
928
929 // RGB2BGR Transform Operation.
Parse()930 std::shared_ptr<TensorOperation> RGB2BGR::Parse() { return std::make_shared<RgbToBgrOperation>(); }
931
932 // RGB2GRAY Transform Operation.
Parse()933 std::shared_ptr<TensorOperation> RGB2GRAY::Parse() { return std::make_shared<RgbToGrayOperation>(); }
934
935 #ifndef ENABLE_ANDROID
936 // RgbaToBgr Transform Operation.
RGBA2BGR()937 RGBA2BGR::RGBA2BGR() {}
938
Parse()939 std::shared_ptr<TensorOperation> RGBA2BGR::Parse() { return std::make_shared<RgbaToBgrOperation>(); }
940
941 // RgbaToRgb Transform Operation.
RGBA2RGB()942 RGBA2RGB::RGBA2RGB() {}
943
Parse()944 std::shared_ptr<TensorOperation> RGBA2RGB::Parse() { return std::make_shared<RgbaToRgbOperation>(); }
945
946 // SlicePatches Transform Operation.
947 struct SlicePatches::Data {
Datamindspore::dataset::vision::SlicePatches::Data948 Data(int32_t num_height, int32_t num_width, SliceMode slice_mode, uint8_t fill_value)
949 : num_height_(num_height), num_width_(num_width), slice_mode_(slice_mode), fill_value_(fill_value) {}
950 int32_t num_height_;
951 int32_t num_width_;
952 SliceMode slice_mode_;
953 uint8_t fill_value_;
954 };
955
SlicePatches(int32_t num_height,int32_t num_width,SliceMode slice_mode,uint8_t fill_value)956 SlicePatches::SlicePatches(int32_t num_height, int32_t num_width, SliceMode slice_mode, uint8_t fill_value)
957 : data_(std::make_shared<Data>(num_height, num_width, slice_mode, fill_value)) {}
958
Parse()959 std::shared_ptr<TensorOperation> SlicePatches::Parse() {
960 return std::make_shared<SlicePatchesOperation>(data_->num_height_, data_->num_width_, data_->slice_mode_,
961 data_->fill_value_);
962 }
963
964 // SoftDvppDecodeRandomCropResizeJpeg Transform Operation.
965 struct SoftDvppDecodeRandomCropResizeJpeg::Data {
Datamindspore::dataset::vision::SoftDvppDecodeRandomCropResizeJpeg::Data966 Data(const std::vector<int32_t> &size, const std::vector<float> &scale, const std::vector<float> &ratio,
967 int32_t max_attempts)
968 : size_(size), scale_(scale), ratio_(ratio), max_attempts_(max_attempts) {}
969 std::vector<int32_t> size_;
970 std::vector<float> scale_;
971 std::vector<float> ratio_;
972 int32_t max_attempts_;
973 };
974
SoftDvppDecodeRandomCropResizeJpeg(std::vector<int32_t> size,std::vector<float> scale,std::vector<float> ratio,int32_t max_attempts)975 SoftDvppDecodeRandomCropResizeJpeg::SoftDvppDecodeRandomCropResizeJpeg(std::vector<int32_t> size,
976 std::vector<float> scale,
977 std::vector<float> ratio, int32_t max_attempts)
978 : data_(std::make_shared<Data>(size, scale, ratio, max_attempts)) {}
979
Parse()980 std::shared_ptr<TensorOperation> SoftDvppDecodeRandomCropResizeJpeg::Parse() {
981 return std::make_shared<SoftDvppDecodeRandomCropResizeJpegOperation>(data_->size_, data_->scale_, data_->ratio_,
982 data_->max_attempts_);
983 }
984
985 // SoftDvppDecodeResizeJpeg Transform Operation.
986 struct SoftDvppDecodeResizeJpeg::Data {
Datamindspore::dataset::vision::SoftDvppDecodeResizeJpeg::Data987 explicit Data(const std::vector<int32_t> &size) : size_(size) {}
988 std::vector<int32_t> size_;
989 };
990
SoftDvppDecodeResizeJpeg(std::vector<int32_t> size)991 SoftDvppDecodeResizeJpeg::SoftDvppDecodeResizeJpeg(std::vector<int32_t> size) : data_(std::make_shared<Data>(size)) {}
992
Parse()993 std::shared_ptr<TensorOperation> SoftDvppDecodeResizeJpeg::Parse() {
994 return std::make_shared<SoftDvppDecodeResizeJpegOperation>(data_->size_);
995 }
996
997 // SwapRedBlue Transform Operation.
SwapRedBlue()998 SwapRedBlue::SwapRedBlue() {}
999
Parse()1000 std::shared_ptr<TensorOperation> SwapRedBlue::Parse() { return std::make_shared<SwapRedBlueOperation>(); }
1001
1002 // UniformAug Transform Operation.
1003 struct UniformAugment::Data {
1004 std::vector<std::shared_ptr<TensorOperation>> transforms_;
1005 int32_t num_ops_;
1006 };
1007
UniformAugment(const std::vector<TensorTransform * > & transforms,int32_t num_ops)1008 UniformAugment::UniformAugment(const std::vector<TensorTransform *> &transforms, int32_t num_ops)
1009 : data_(std::make_shared<Data>()) {
1010 (void)std::transform(
1011 transforms.begin(), transforms.end(), std::back_inserter(data_->transforms_),
1012 [](TensorTransform *const op) -> std::shared_ptr<TensorOperation> { return op ? op->Parse() : nullptr; });
1013 data_->num_ops_ = num_ops;
1014 }
1015
UniformAugment(const std::vector<std::shared_ptr<TensorTransform>> & transforms,int32_t num_ops)1016 UniformAugment::UniformAugment(const std::vector<std::shared_ptr<TensorTransform>> &transforms, int32_t num_ops)
1017 : data_(std::make_shared<Data>()) {
1018 (void)std::transform(transforms.begin(), transforms.end(), std::back_inserter(data_->transforms_),
1019 [](const std::shared_ptr<TensorTransform> op) -> std::shared_ptr<TensorOperation> {
1020 return op ? op->Parse() : nullptr;
1021 });
1022 data_->num_ops_ = num_ops;
1023 }
1024
UniformAugment(const std::vector<std::reference_wrapper<TensorTransform>> & transforms,int32_t num_ops)1025 UniformAugment::UniformAugment(const std::vector<std::reference_wrapper<TensorTransform>> &transforms, int32_t num_ops)
1026 : data_(std::make_shared<Data>()) {
1027 (void)std::transform(transforms.begin(), transforms.end(), std::back_inserter(data_->transforms_),
1028 [](TensorTransform &op) -> std::shared_ptr<TensorOperation> { return op.Parse(); });
1029 data_->num_ops_ = num_ops;
1030 }
1031
Parse()1032 std::shared_ptr<TensorOperation> UniformAugment::Parse() {
1033 return std::make_shared<UniformAugOperation>(data_->transforms_, data_->num_ops_);
1034 }
1035
1036 // VerticalFlip Transform Operation.
VerticalFlip()1037 VerticalFlip::VerticalFlip() {}
1038
Parse()1039 std::shared_ptr<TensorOperation> VerticalFlip::Parse() { return std::make_shared<VerticalFlipOperation>(); }
1040 #endif // not ENABLE_ANDROID
1041
1042 } // namespace vision
1043 } // namespace dataset
1044 } // namespace mindspore
1045