/** * Copyright 2020-2022 Huawei Technologies Co., Ltd * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "minddata/dataset/include/dataset/vision.h" #ifdef ENABLE_ACL #include "minddata/dataset/include/dataset/vision_ascend.h" #include "minddata/dataset/kernels/ir/vision/ascend_vision_ir.h" #endif #include "minddata/dataset/kernels/ir/vision/adjust_gamma_ir.h" #include "minddata/dataset/kernels/ir/vision/affine_ir.h" #include "minddata/dataset/kernels/ir/vision/auto_augment_ir.h" #include "minddata/dataset/kernels/ir/vision/auto_contrast_ir.h" #include "minddata/dataset/kernels/ir/vision/bounding_box_augment_ir.h" #include "minddata/dataset/kernels/ir/vision/center_crop_ir.h" #include "minddata/dataset/kernels/ir/vision/convert_color_ir.h" #include "minddata/dataset/kernels/ir/vision/crop_ir.h" #include "minddata/dataset/kernels/ir/vision/cutmix_batch_ir.h" #include "minddata/dataset/kernels/ir/vision/cutout_ir.h" #include "minddata/dataset/kernels/ir/vision/decode_ir.h" #include "minddata/dataset/kernels/ir/vision/equalize_ir.h" #include "minddata/dataset/kernels/ir/vision/gaussian_blur_ir.h" #include "minddata/dataset/kernels/ir/vision/horizontal_flip_ir.h" #include "minddata/dataset/kernels/ir/vision/hwc_to_chw_ir.h" #include "minddata/dataset/kernels/ir/vision/invert_ir.h" #include "minddata/dataset/kernels/ir/vision/mixup_batch_ir.h" #include "minddata/dataset/kernels/ir/vision/normalize_ir.h" #include "minddata/dataset/kernels/ir/vision/normalize_pad_ir.h" #include "minddata/dataset/kernels/ir/vision/pad_ir.h" #include "minddata/dataset/kernels/ir/vision/random_adjust_sharpness_ir.h" #include "minddata/dataset/kernels/ir/vision/random_affine_ir.h" #include "minddata/dataset/kernels/ir/vision/random_auto_contrast_ir.h" #include "minddata/dataset/kernels/ir/vision/random_color_adjust_ir.h" #include "minddata/dataset/kernels/ir/vision/random_color_ir.h" #include "minddata/dataset/kernels/ir/vision/random_crop_decode_resize_ir.h" #include "minddata/dataset/kernels/ir/vision/random_crop_ir.h" #include "minddata/dataset/kernels/ir/vision/random_crop_with_bbox_ir.h" #include "minddata/dataset/kernels/ir/vision/random_equalize_ir.h" #include "minddata/dataset/kernels/ir/vision/random_horizontal_flip_ir.h" #include "minddata/dataset/kernels/ir/vision/random_horizontal_flip_with_bbox_ir.h" #include "minddata/dataset/kernels/ir/vision/random_invert_ir.h" #include "minddata/dataset/kernels/ir/vision/random_lighting_ir.h" #include "minddata/dataset/kernels/ir/vision/random_posterize_ir.h" #include "minddata/dataset/kernels/ir/vision/random_resize_ir.h" #include "minddata/dataset/kernels/ir/vision/random_resize_with_bbox_ir.h" #include "minddata/dataset/kernels/ir/vision/random_resized_crop_ir.h" #include "minddata/dataset/kernels/ir/vision/random_resized_crop_with_bbox_ir.h" #include "minddata/dataset/kernels/ir/vision/random_rotation_ir.h" #include "minddata/dataset/kernels/ir/vision/random_select_subpolicy_ir.h" #include "minddata/dataset/kernels/ir/vision/random_sharpness_ir.h" #include "minddata/dataset/kernels/ir/vision/random_solarize_ir.h" #include "minddata/dataset/kernels/ir/vision/random_vertical_flip_ir.h" #include "minddata/dataset/kernels/ir/vision/random_vertical_flip_with_bbox_ir.h" #include "minddata/dataset/kernels/ir/vision/rescale_ir.h" #include "minddata/dataset/kernels/ir/vision/resize_ir.h" #include "minddata/dataset/kernels/ir/vision/resize_preserve_ar_ir.h" #include "minddata/dataset/kernels/ir/vision/resize_with_bbox_ir.h" #include "minddata/dataset/kernels/ir/vision/rgb_to_bgr_ir.h" #include "minddata/dataset/kernels/ir/vision/rgb_to_gray_ir.h" #include "minddata/dataset/kernels/ir/vision/rgba_to_bgr_ir.h" #include "minddata/dataset/kernels/ir/vision/rgba_to_rgb_ir.h" #include "minddata/dataset/kernels/ir/vision/rotate_ir.h" #include "minddata/dataset/kernels/ir/vision/slice_patches_ir.h" #include "minddata/dataset/kernels/ir/vision/softdvpp_decode_random_crop_resize_jpeg_ir.h" #include "minddata/dataset/kernels/ir/vision/softdvpp_decode_resize_jpeg_ir.h" #include "minddata/dataset/kernels/ir/vision/swap_red_blue_ir.h" #include "minddata/dataset/kernels/ir/vision/uniform_aug_ir.h" #include "minddata/dataset/kernels/ir/vision/vertical_flip_ir.h" #ifndef ENABLE_ANDROID #include "utils/log_adapter.h" #else #include "mindspore/lite/src/common/log_adapter.h" #endif #include "minddata/dataset/kernels/ir/validators.h" namespace mindspore { namespace dataset { // Transform operations for computer vision. namespace vision { // CONSTRUCTORS FOR API CLASSES TO CREATE VISION TENSOR TRANSFORM OPERATIONS // (In alphabetical order) // Affine Transform Operation. struct Affine::Data { Data(float_t degrees, const std::vector &translation, float scale, const std::vector &shear, InterpolationMode interpolation, const std::vector &fill_value) : degrees_(degrees), translation_(translation), scale_(scale), shear_(shear), interpolation_(interpolation), fill_value_(fill_value) {} float degrees_; std::vector translation_; float scale_; std::vector shear_; InterpolationMode interpolation_; std::vector fill_value_; }; Affine::Affine(float_t degrees, const std::vector &translation, float scale, const std::vector &shear, InterpolationMode interpolation, const std::vector &fill_value) : data_(std::make_shared(degrees, translation, scale, shear, interpolation, fill_value)) {} std::shared_ptr Affine::Parse() { return std::make_shared(data_->degrees_, data_->translation_, data_->scale_, data_->shear_, data_->interpolation_, data_->fill_value_); } #ifndef ENABLE_ANDROID // AdjustGamma Transform Operation. struct AdjustGamma::Data { Data(float gamma, float gain) : gamma_(gamma), gain_(gain) {} float gamma_; float gain_; }; AdjustGamma::AdjustGamma(float gamma, float gain) : data_(std::make_shared(gamma, gain)) {} std::shared_ptr AdjustGamma::Parse() { return std::make_shared(data_->gamma_, data_->gain_); } // AutoAugment Transform Operation. struct AutoAugment::Data { Data(AutoAugmentPolicy policy, InterpolationMode interpolation, const std::vector &fill_value) : policy_(policy), interpolation_(interpolation), fill_value_(fill_value) {} AutoAugmentPolicy policy_; InterpolationMode interpolation_; std::vector fill_value_; }; AutoAugment::AutoAugment(AutoAugmentPolicy policy, InterpolationMode interpolation, const std::vector &fill_value) : data_(std::make_shared(policy, interpolation, fill_value)) {} std::shared_ptr AutoAugment::Parse() { return std::make_shared(data_->policy_, data_->interpolation_, data_->fill_value_); } // AutoContrast Transform Operation. struct AutoContrast::Data { Data(float cutoff, const std::vector &ignore) : cutoff_(cutoff), ignore_(ignore) {} float cutoff_; std::vector ignore_; }; AutoContrast::AutoContrast(float cutoff, const std::vector &ignore) : data_(std::make_shared(cutoff, ignore)) {} std::shared_ptr AutoContrast::Parse() { return std::make_shared(data_->cutoff_, data_->ignore_); } // BoundingBoxAugment Transform Operation. struct BoundingBoxAugment::Data { std::shared_ptr transform_; float ratio_; }; BoundingBoxAugment::BoundingBoxAugment(TensorTransform *transform, float ratio) : data_(std::make_shared()) { data_->transform_ = transform ? transform->Parse() : nullptr; data_->ratio_ = ratio; } BoundingBoxAugment::BoundingBoxAugment(const std::shared_ptr &transform, float ratio) : data_(std::make_shared()) { data_->transform_ = transform ? transform->Parse() : nullptr; data_->ratio_ = ratio; } BoundingBoxAugment::BoundingBoxAugment(const std::reference_wrapper &transform, float ratio) : data_(std::make_shared()) { data_->transform_ = transform.get().Parse(); data_->ratio_ = ratio; } std::shared_ptr BoundingBoxAugment::Parse() { return std::make_shared(data_->transform_, data_->ratio_); } #endif // not ENABLE_ANDROID // CenterCrop Transform Operation. struct CenterCrop::Data { explicit Data(const std::vector &size) : size_(size) {} std::vector size_; }; CenterCrop::CenterCrop(const std::vector &size) : data_(std::make_shared(size)) {} std::shared_ptr CenterCrop::Parse() { return std::make_shared(data_->size_); } std::shared_ptr CenterCrop::Parse(const MapTargetDevice &env) { if (env == MapTargetDevice::kAscend310) { #ifdef ENABLE_ACL std::vector usize_; usize_.reserve(data_->size_.size()); std::transform(data_->size_.begin(), data_->size_.end(), std::back_inserter(usize_), [](int32_t i) { return (uint32_t)i; }); return std::make_shared(usize_); #endif // ENABLE_ACL } else if (env == MapTargetDevice::kCpu) { return std::make_shared(data_->size_); } MS_LOG(ERROR) << "Unsupported MapTargetDevice, only supported kCpu and kAscend310."; return nullptr; } #ifndef ENABLE_ANDROID // ConvertColor Transform Operation. struct ConvertColor::Data { explicit Data(ConvertMode convert_mode) : convert_mode_(convert_mode) {} ConvertMode convert_mode_; }; ConvertColor::ConvertColor(ConvertMode convert_mode) : data_(std::make_shared(convert_mode)) {} std::shared_ptr ConvertColor::Parse() { return std::make_shared(data_->convert_mode_); } #endif // not ENABLE_ANDROID // Crop Transform Operation. struct Crop::Data { Data(const std::vector &coordinates, const std::vector &size) : coordinates_(coordinates), size_(size) {} std::vector coordinates_; std::vector size_; }; Crop::Crop(const std::vector &coordinates, const std::vector &size) : data_(std::make_shared(coordinates, size)) {} std::shared_ptr Crop::Parse() { return std::make_shared(data_->coordinates_, data_->size_); } #ifndef ENABLE_ANDROID // CutMixBatch Transform Operation. struct CutMixBatch::Data { Data(ImageBatchFormat image_batch_format, float alpha, float prob) : image_batch_format_(image_batch_format), alpha_(alpha), prob_(prob) {} float alpha_; float prob_; ImageBatchFormat image_batch_format_; }; CutMixBatch::CutMixBatch(ImageBatchFormat image_batch_format, float alpha, float prob) : data_(std::make_shared(image_batch_format, alpha, prob)) {} std::shared_ptr CutMixBatch::Parse() { return std::make_shared(data_->image_batch_format_, data_->alpha_, data_->prob_); } // CutOutOp. struct CutOut::Data { Data(int32_t length, int32_t num_patches) : length_(length), num_patches_(num_patches) {} int32_t length_; int32_t num_patches_; }; CutOut::CutOut(int32_t length, int32_t num_patches) : data_(std::make_shared(length, num_patches)) {} std::shared_ptr CutOut::Parse() { return std::make_shared(data_->length_, data_->num_patches_); } #endif // not ENABLE_ANDROID // Decode Transform Operation. struct Decode::Data { explicit Data(bool rgb) : rgb_(rgb) {} bool rgb_; }; Decode::Decode(bool rgb) : data_(std::make_shared(rgb)) {} std::shared_ptr Decode::Parse() { return std::make_shared(data_->rgb_); } std::shared_ptr Decode::Parse(const MapTargetDevice &env) { if (env == MapTargetDevice::kAscend310) { #ifdef ENABLE_ACL return std::make_shared(); #endif // ENABLE_ACL } else if (env == MapTargetDevice::kCpu) { return std::make_shared(data_->rgb_); } MS_LOG(ERROR) << "Unsupported MapTargetDevice, only supported kCpu and kAscend310."; return nullptr; } #ifdef ENABLE_ACL // DvppDecodeResize Transform Operation. struct DvppDecodeResizeJpeg::Data { explicit Data(const std::vector &resize) : resize_(resize) {} std::vector resize_; }; DvppDecodeResizeJpeg::DvppDecodeResizeJpeg(const std::vector &resize) : data_(std::make_shared(resize)) {} std::shared_ptr DvppDecodeResizeJpeg::Parse() { return std::make_shared(data_->resize_); } std::shared_ptr DvppDecodeResizeJpeg::Parse(const MapTargetDevice &env) { if (env == MapTargetDevice::kAscend310) { return std::make_shared(data_->resize_); } MS_LOG(ERROR) << "Unsupported MapTargetDevice, only supported kAscend310."; return nullptr; } // DvppDecodeResizeCrop Transform Operation. struct DvppDecodeResizeCropJpeg::Data { Data(const std::vector &crop, const std::vector &resize) : crop_(crop), resize_(resize) {} std::vector crop_; std::vector resize_; }; DvppDecodeResizeCropJpeg::DvppDecodeResizeCropJpeg(const std::vector &crop, const std::vector &resize) : data_(std::make_shared(crop, resize)) {} std::shared_ptr DvppDecodeResizeCropJpeg::Parse() { return std::make_shared(data_->crop_, data_->resize_); } std::shared_ptr DvppDecodeResizeCropJpeg::Parse(const MapTargetDevice &env) { if (env == MapTargetDevice::kAscend310) { return std::make_shared(data_->crop_, data_->resize_); } MS_LOG(ERROR) << "Unsupported MapTargetDevice, only supported kAscend310."; return nullptr; } // DvppDecodePng Transform Operation. DvppDecodePng::DvppDecodePng() {} std::shared_ptr DvppDecodePng::Parse() { return std::make_shared(); } std::shared_ptr DvppDecodePng::Parse(const MapTargetDevice &env) { if (env == MapTargetDevice::kAscend310) { return std::make_shared(); } MS_LOG(ERROR) << "Unsupported MapTargetDevice, only supported kAscend310."; return nullptr; } #endif // ENABLE_ACL #ifndef ENABLE_ANDROID // Equalize Transform Operation. Equalize::Equalize() = default; std::shared_ptr Equalize::Parse() { return std::make_shared(); } #endif // not ENABLE_ANDROID // GaussianBlur Transform Operation. struct GaussianBlur::Data { Data(const std::vector &kernel_size, const std::vector &sigma) : kernel_size_(kernel_size), sigma_(sigma) {} std::vector kernel_size_; std::vector sigma_; }; GaussianBlur::GaussianBlur(const std::vector &kernel_size, const std::vector &sigma) : data_(std::make_shared(kernel_size, sigma)) {} std::shared_ptr GaussianBlur::Parse() { return std::make_shared(data_->kernel_size_, data_->sigma_); } #ifndef ENABLE_ANDROID // HorizontalFlip Transform Operation. HorizontalFlip::HorizontalFlip() = default; std::shared_ptr HorizontalFlip::Parse() { return std::make_shared(); } // HwcToChw Transform Operation. HWC2CHW::HWC2CHW() = default; std::shared_ptr HWC2CHW::Parse() { return std::make_shared(); } // Invert Transform Operation. Invert::Invert() = default; std::shared_ptr Invert::Parse() { return std::make_shared(); } // MixUpBatch Transform Operation. struct MixUpBatch::Data { explicit Data(float alpha) : alpha_(alpha) {} float alpha_; }; MixUpBatch::MixUpBatch(float alpha) : data_(std::make_shared(alpha)) {} std::shared_ptr MixUpBatch::Parse() { return std::make_shared(data_->alpha_); } #endif // not ENABLE_ANDROID // Normalize Transform Operation. struct Normalize::Data { Data(const std::vector &mean, const std::vector &std) : mean_(mean), std_(std) {} std::vector mean_; std::vector std_; }; Normalize::Normalize(const std::vector &mean, const std::vector &std) : data_(std::make_shared(mean, std)) {} std::shared_ptr Normalize::Parse() { return std::make_shared(data_->mean_, data_->std_); } std::shared_ptr Normalize::Parse(const MapTargetDevice &env) { if (env == MapTargetDevice::kAscend310) { #ifdef ENABLE_ACL return std::make_shared(data_->mean_, data_->std_); #endif // ENABLE_ACL } else if (env == MapTargetDevice::kCpu) { return std::make_shared(data_->mean_, data_->std_); } MS_LOG(ERROR) << "Unsupported MapTargetDevice, only supported kCpu and kAscend310."; return nullptr; } #ifndef ENABLE_ANDROID // NormalizePad Transform Operation. struct NormalizePad::Data { Data(const std::vector &mean, const std::vector &std, const std::string &dtype) : mean_(mean), std_(std), dtype_(dtype) {} std::vector mean_; std::vector std_; std::string dtype_; }; NormalizePad::NormalizePad(const std::vector &mean, const std::vector &std, const std::vector &dtype) : data_(std::make_shared(mean, std, CharToString(dtype))) {} std::shared_ptr NormalizePad::Parse() { return std::make_shared(data_->mean_, data_->std_, data_->dtype_); } // Pad Transform Operation. struct Pad::Data { Data(const std::vector &padding, const std::vector &fill_value, BorderType padding_mode) : padding_(padding), fill_value_(fill_value), padding_mode_(padding_mode) {} std::vector padding_; std::vector fill_value_; BorderType padding_mode_; }; Pad::Pad(const std::vector &padding, const std::vector &fill_value, BorderType padding_mode) : data_(std::make_shared(padding, fill_value, padding_mode)) {} std::shared_ptr Pad::Parse() { return std::make_shared(data_->padding_, data_->fill_value_, data_->padding_mode_); } // RandomAdjustSharpness Transform Operation. struct RandomAdjustSharpness::Data { Data(float degree, float prob) : degree_(degree), probability_(prob) {} float degree_; float probability_; }; RandomAdjustSharpness::RandomAdjustSharpness(float degree, float prob) : data_(std::make_shared(degree, prob)) {} std::shared_ptr RandomAdjustSharpness::Parse() { return std::make_shared(data_->degree_, data_->probability_); } #endif // not ENABLE_ANDROID // RandomAffine Transform Operation. struct RandomAffine::Data { Data(const std::vector °rees, const std::vector &translate_range, const std::vector &scale_range, const std::vector &shear_ranges, InterpolationMode interpolation, const std::vector &fill_value) : degrees_(degrees), translate_range_(translate_range), scale_range_(scale_range), shear_ranges_(shear_ranges), interpolation_(interpolation), fill_value_(fill_value) {} std::vector degrees_; // min_degree, max_degree std::vector translate_range_; // maximum x translation percentage, maximum y translation percentage std::vector scale_range_; // min_scale, max_scale std::vector shear_ranges_; // min_x_shear, max_x_shear, min_y_shear, max_y_shear InterpolationMode interpolation_; std::vector fill_value_; }; RandomAffine::RandomAffine(const std::vector °rees, const std::vector &translate_range, const std::vector &scale_range, const std::vector &shear_ranges, InterpolationMode interpolation, const std::vector &fill_value) : data_(std::make_shared(degrees, translate_range, scale_range, shear_ranges, interpolation, fill_value)) {} std::shared_ptr RandomAffine::Parse() { return std::make_shared(data_->degrees_, data_->translate_range_, data_->scale_range_, data_->shear_ranges_, data_->interpolation_, data_->fill_value_); } #ifndef ENABLE_ANDROID // RandomAutoContrast Transform Operation. struct RandomAutoContrast::Data { Data(float cutoff, const std::vector &ignore, float prob) : cutoff_(cutoff), ignore_(ignore), probability_(prob) {} float cutoff_; std::vector ignore_; float probability_; }; RandomAutoContrast::RandomAutoContrast(float cutoff, const std::vector &ignore, float prob) : data_(std::make_shared(cutoff, ignore, prob)) {} std::shared_ptr RandomAutoContrast::Parse() { return std::make_shared(data_->cutoff_, data_->ignore_, data_->probability_); } // RandomColor Transform Operation. struct RandomColor::Data { Data(float t_lb, float t_ub) : t_lb_(t_lb), t_ub_(t_ub) {} float t_lb_; float t_ub_; }; RandomColor::RandomColor(float t_lb, float t_ub) : data_(std::make_shared(t_lb, t_ub)) {} std::shared_ptr RandomColor::Parse() { return std::make_shared(data_->t_lb_, data_->t_ub_); } // RandomColorAdjust Transform Operation. struct RandomColorAdjust::Data { Data(const std::vector &brightness, const std::vector &contrast, const std::vector &saturation, const std::vector &hue) : brightness_(brightness), contrast_(contrast), saturation_(saturation), hue_(hue) {} std::vector brightness_; std::vector contrast_; std::vector saturation_; std::vector hue_; }; RandomColorAdjust::RandomColorAdjust(const std::vector &brightness, const std::vector &contrast, const std::vector &saturation, const std::vector &hue) : data_(std::make_shared(brightness, contrast, saturation, hue)) {} std::shared_ptr RandomColorAdjust::Parse() { return std::make_shared(data_->brightness_, data_->contrast_, data_->saturation_, data_->hue_); } // RandomCrop Transform Operation. struct RandomCrop::Data { Data(const std::vector &size, const std::vector &padding, bool pad_if_needed, const std::vector &fill_value, BorderType padding_mode) : size_(size), padding_(padding), pad_if_needed_(pad_if_needed), fill_value_(fill_value), padding_mode_(padding_mode) {} std::vector size_; std::vector padding_; bool pad_if_needed_; std::vector fill_value_; BorderType padding_mode_; }; RandomCrop::RandomCrop(const std::vector &size, const std::vector &padding, bool pad_if_needed, const std::vector &fill_value, BorderType padding_mode) : data_(std::make_shared(size, padding, pad_if_needed, fill_value, padding_mode)) {} std::shared_ptr RandomCrop::Parse() { return std::make_shared(data_->size_, data_->padding_, data_->pad_if_needed_, data_->fill_value_, data_->padding_mode_); } // RandomCropDecodeResize Transform Operation. struct RandomCropDecodeResize::Data { Data(const std::vector &size, const std::vector &scale, const std::vector &ratio, InterpolationMode interpolation, int32_t max_attempts) : size_(size), scale_(scale), ratio_(ratio), interpolation_(interpolation), max_attempts_(max_attempts) {} std::vector size_; std::vector scale_; std::vector ratio_; InterpolationMode interpolation_; int32_t max_attempts_; }; RandomCropDecodeResize::RandomCropDecodeResize(const std::vector &size, const std::vector &scale, const std::vector &ratio, InterpolationMode interpolation, int32_t max_attempts) : data_(std::make_shared(size, scale, ratio, interpolation, max_attempts)) {} std::shared_ptr RandomCropDecodeResize::Parse() { return std::make_shared(data_->size_, data_->scale_, data_->ratio_, data_->interpolation_, data_->max_attempts_); } // RandomCropWithBBox Transform Operation. struct RandomCropWithBBox::Data { Data(const std::vector &size, const std::vector &padding, bool pad_if_needed, const std::vector &fill_value, BorderType padding_mode) : size_(size), padding_(padding), pad_if_needed_(pad_if_needed), fill_value_(fill_value), padding_mode_(padding_mode) {} std::vector size_; std::vector padding_; bool pad_if_needed_; std::vector fill_value_; BorderType padding_mode_; }; RandomCropWithBBox::RandomCropWithBBox(const std::vector &size, const std::vector &padding, bool pad_if_needed, const std::vector &fill_value, BorderType padding_mode) : data_(std::make_shared(size, padding, pad_if_needed, fill_value, padding_mode)) {} std::shared_ptr RandomCropWithBBox::Parse() { return std::make_shared(data_->size_, data_->padding_, data_->pad_if_needed_, data_->fill_value_, data_->padding_mode_); } // RandomEqualize Transform Operation. struct RandomEqualize::Data { explicit Data(float prob) : probability_(prob) {} float probability_; }; RandomEqualize::RandomEqualize(float prob) : data_(std::make_shared(prob)) {} std::shared_ptr RandomEqualize::Parse() { return std::make_shared(data_->probability_); } // RandomHorizontalFlip. struct RandomHorizontalFlip::Data { explicit Data(float prob) : probability_(prob) {} float probability_; }; RandomHorizontalFlip::RandomHorizontalFlip(float prob) : data_(std::make_shared(prob)) {} std::shared_ptr RandomHorizontalFlip::Parse() { return std::make_shared(data_->probability_); } // RandomHorizontalFlipWithBBox struct RandomHorizontalFlipWithBBox::Data { explicit Data(float prob) : probability_(prob) {} float probability_; }; RandomHorizontalFlipWithBBox::RandomHorizontalFlipWithBBox(float prob) : data_(std::make_shared(prob)) {} std::shared_ptr RandomHorizontalFlipWithBBox::Parse() { return std::make_shared(data_->probability_); } // RandomInvert Operation. struct RandomInvert::Data { explicit Data(float prob) : probability_(prob) {} float probability_; }; RandomInvert::RandomInvert(float prob) : data_(std::make_shared(prob)) {} std::shared_ptr RandomInvert::Parse() { return std::make_shared(data_->probability_); } // RandomLighting Transform Operation. struct RandomLighting::Data { explicit Data(float alpha) : alpha_(alpha) {} float alpha_; }; RandomLighting::RandomLighting(float alpha) : data_(std::make_shared(alpha)) {} std::shared_ptr RandomLighting::Parse() { return std::make_shared(data_->alpha_); } // RandomPosterize Transform Operation. struct RandomPosterize::Data { explicit Data(const std::vector &bit_range) : bit_range_(bit_range) {} std::vector bit_range_; }; RandomPosterize::RandomPosterize(const std::vector &bit_range) : data_(std::make_shared(bit_range)) {} std::shared_ptr RandomPosterize::Parse() { return std::make_shared(data_->bit_range_); } // RandomResize Transform Operation. struct RandomResize::Data { explicit Data(const std::vector &size) : size_(size) {} std::vector size_; }; RandomResize::RandomResize(const std::vector &size) : data_(std::make_shared(size)) {} std::shared_ptr RandomResize::Parse() { return std::make_shared(data_->size_); } // RandomResizeWithBBox Transform Operation. struct RandomResizeWithBBox::Data { explicit Data(const std::vector &size) : size_(size) {} std::vector size_; }; RandomResizeWithBBox::RandomResizeWithBBox(const std::vector &size) : data_(std::make_shared(size)) {} std::shared_ptr RandomResizeWithBBox::Parse() { return std::make_shared(data_->size_); } // RandomResizedCrop Transform Operation. struct RandomResizedCrop::Data { Data(const std::vector &size, const std::vector &scale, const std::vector &ratio, InterpolationMode interpolation, int32_t max_attempts) : size_(size), scale_(scale), ratio_(ratio), interpolation_(interpolation), max_attempts_(max_attempts) {} std::vector size_; std::vector scale_; std::vector ratio_; InterpolationMode interpolation_; int32_t max_attempts_; }; RandomResizedCrop::RandomResizedCrop(const std::vector &size, const std::vector &scale, const std::vector &ratio, InterpolationMode interpolation, int32_t max_attempts) : data_(std::make_shared(size, scale, ratio, interpolation, max_attempts)) {} std::shared_ptr RandomResizedCrop::Parse() { return std::make_shared(data_->size_, data_->scale_, data_->ratio_, data_->interpolation_, data_->max_attempts_); } // RandomResizedCrop Transform Operation. struct RandomResizedCropWithBBox::Data { Data(const std::vector &size, const std::vector &scale, const std::vector &ratio, InterpolationMode interpolation, int32_t max_attempts) : size_(size), scale_(scale), ratio_(ratio), interpolation_(interpolation), max_attempts_(max_attempts) {} std::vector size_; std::vector scale_; std::vector ratio_; InterpolationMode interpolation_; int32_t max_attempts_; }; RandomResizedCropWithBBox::RandomResizedCropWithBBox(const std::vector &size, const std::vector &scale, const std::vector &ratio, InterpolationMode interpolation, int32_t max_attempts) : data_(std::make_shared(size, scale, ratio, interpolation, max_attempts)) {} std::shared_ptr RandomResizedCropWithBBox::Parse() { return std::make_shared(data_->size_, data_->scale_, data_->ratio_, data_->interpolation_, data_->max_attempts_); } // RandomRotation Transform Operation. struct RandomRotation::Data { Data(const std::vector °rees, InterpolationMode resample, bool expand, const std::vector ¢er, const std::vector &fill_value) : degrees_(degrees), interpolation_mode_(resample), center_(center), expand_(expand), fill_value_(fill_value) {} std::vector degrees_; InterpolationMode interpolation_mode_; std::vector center_; bool expand_; std::vector fill_value_; }; RandomRotation::RandomRotation(const std::vector °rees, InterpolationMode resample, bool expand, const std::vector ¢er, const std::vector &fill_value) : data_(std::make_shared(degrees, resample, expand, center, fill_value)) {} std::shared_ptr RandomRotation::Parse() { return std::make_shared(data_->degrees_, data_->interpolation_mode_, data_->expand_, data_->center_, data_->fill_value_); } // RandomSelectSubpolicy Transform Operation. struct RandomSelectSubpolicy::Data { std::vector, double>>> policy_; }; RandomSelectSubpolicy::RandomSelectSubpolicy( const std::vector>> &policy) : data_(std::make_shared()) { for (uint32_t i = 0; i < policy.size(); i++) { std::vector, double>> subpolicy; for (uint32_t j = 0; j < policy[i].size(); j++) { TensorTransform *op = policy[i][j].first; std::shared_ptr operation = (op ? op->Parse() : nullptr); double prob = policy[i][j].second; subpolicy.emplace_back(std::move(std::make_pair(operation, prob))); } data_->policy_.emplace_back(subpolicy); } } RandomSelectSubpolicy::RandomSelectSubpolicy( const std::vector, double>>> &policy) : data_(std::make_shared()) { for (uint32_t i = 0; i < policy.size(); i++) { std::vector, double>> subpolicy; for (uint32_t j = 0; j < policy[i].size(); j++) { std::shared_ptr op = policy[i][j].first; std::shared_ptr operation = (op ? op->Parse() : nullptr); double prob = policy[i][j].second; subpolicy.emplace_back(std::move(std::make_pair(operation, prob))); } data_->policy_.emplace_back(subpolicy); } } RandomSelectSubpolicy::RandomSelectSubpolicy( const std::vector, double>>> &policy) : data_(std::make_shared()) { for (int32_t i = 0; i < policy.size(); i++) { std::vector, double>> subpolicy; for (int32_t j = 0; j < policy[i].size(); j++) { TensorTransform &op = policy[i][j].first; std::shared_ptr operation = op.Parse(); double prob = policy[i][j].second; subpolicy.emplace_back(std::move(std::make_pair(operation, prob))); } data_->policy_.emplace_back(subpolicy); } } std::shared_ptr RandomSelectSubpolicy::Parse() { return std::make_shared(data_->policy_); } // RandomSharpness Transform Operation. struct RandomSharpness::Data { explicit Data(const std::vector °rees) : degrees_(degrees) {} std::vector degrees_; }; RandomSharpness::RandomSharpness(const std::vector °rees) : data_(std::make_shared(degrees)) {} std::shared_ptr RandomSharpness::Parse() { return std::make_shared(data_->degrees_); } // RandomSolarize Transform Operation. struct RandomSolarize::Data { explicit Data(const std::vector &threshold) : threshold_(threshold) {} std::vector threshold_; }; RandomSolarize::RandomSolarize(const std::vector &threshold) : data_(std::make_shared(threshold)) {} std::shared_ptr RandomSolarize::Parse() { return std::make_shared(data_->threshold_); } // RandomVerticalFlip Transform Operation. struct RandomVerticalFlip::Data { explicit Data(float prob) : probability_(prob) {} float probability_; }; RandomVerticalFlip::RandomVerticalFlip(float prob) : data_(std::make_shared(prob)) {} std::shared_ptr RandomVerticalFlip::Parse() { return std::make_shared(data_->probability_); } // RandomVerticalFlipWithBBox Transform Operation. struct RandomVerticalFlipWithBBox::Data { explicit Data(float prob) : probability_(prob) {} float probability_; }; RandomVerticalFlipWithBBox::RandomVerticalFlipWithBBox(float prob) : data_(std::make_shared(prob)) {} std::shared_ptr RandomVerticalFlipWithBBox::Parse() { return std::make_shared(data_->probability_); } // Rescale Transform Operation. struct Rescale::Data { Data(float rescale, float shift) : rescale_(rescale), shift_(shift) {} float rescale_; float shift_; }; Rescale::Rescale(float rescale, float shift) : data_(std::make_shared(rescale, shift)) {} std::shared_ptr Rescale::Parse() { return std::make_shared(data_->rescale_, data_->shift_); } #endif // not ENABLE_ANDROID // Resize Transform Operation. struct Resize::Data { Data(const std::vector &size, InterpolationMode interpolation) : size_(size), interpolation_(interpolation) {} std::vector size_; InterpolationMode interpolation_; }; Resize::Resize(const std::vector &size, InterpolationMode interpolation) : data_(std::make_shared(size, interpolation)) {} std::shared_ptr Resize::Parse() { return std::make_shared(data_->size_, data_->interpolation_); } std::shared_ptr Resize::Parse(const MapTargetDevice &env) { if (env == MapTargetDevice::kAscend310) { #ifdef ENABLE_ACL std::vector usize_; usize_.reserve(data_->size_.size()); std::transform(data_->size_.begin(), data_->size_.end(), std::back_inserter(usize_), [](int32_t i) { return (uint32_t)i; }); return std::make_shared(usize_); #endif // ENABLE_ACL } else if (env == MapTargetDevice::kCpu) { return std::make_shared(data_->size_, data_->interpolation_); } MS_LOG(ERROR) << "Unsupported MapTargetDevice, only supported kCpu and kAscend310."; return nullptr; } // ResizePreserveAR Transform Operation. struct ResizePreserveAR::Data { Data(int32_t height, int32_t width, int32_t img_orientation) : height_(height), width_(width), img_orientation_(img_orientation) {} int32_t height_; int32_t width_; int32_t img_orientation_; }; ResizePreserveAR::ResizePreserveAR(int32_t height, int32_t width, int32_t img_orientation) : data_(std::make_shared(height, width, img_orientation)) {} std::shared_ptr ResizePreserveAR::Parse() { return std::make_shared(data_->height_, data_->width_, data_->img_orientation_); } #ifndef ENABLE_ANDROID // ResizeWithBBox Transform Operation. struct ResizeWithBBox::Data { Data(const std::vector &size, InterpolationMode interpolation) : size_(size), interpolation_(interpolation) {} std::vector size_; InterpolationMode interpolation_; }; ResizeWithBBox::ResizeWithBBox(const std::vector &size, InterpolationMode interpolation) : data_(std::make_shared(size, interpolation)) {} std::shared_ptr ResizeWithBBox::Parse() { return std::make_shared(data_->size_, data_->interpolation_); } #endif // not ENABLE_ANDROID // RGB2BGR Transform Operation. std::shared_ptr RGB2BGR::Parse() { return std::make_shared(); } // RGB2GRAY Transform Operation. std::shared_ptr RGB2GRAY::Parse() { return std::make_shared(); } #ifndef ENABLE_ANDROID // RgbaToBgr Transform Operation. RGBA2BGR::RGBA2BGR() = default; std::shared_ptr RGBA2BGR::Parse() { return std::make_shared(); } // RgbaToRgb Transform Operation. RGBA2RGB::RGBA2RGB() = default; std::shared_ptr RGBA2RGB::Parse() { return std::make_shared(); } // Rotate Transform Operation. struct Rotate::Data { Data(const float °rees, InterpolationMode resample, bool expand, const std::vector ¢er, const std::vector &fill_value) : degrees_(degrees), interpolation_mode_(resample), center_(center), expand_(expand), fill_value_(fill_value) {} explicit Data(const FixRotationAngle &angle_id) : angle_id_(angle_id), lite_impl_(true) {} FixRotationAngle angle_id_{FixRotationAngle::k0Degree}; bool lite_impl_{false}; float degrees_{0}; InterpolationMode interpolation_mode_{InterpolationMode::kNearestNeighbour}; std::vector center_{{}}; bool expand_{false}; std::vector fill_value_{0, 0, 0}; }; Rotate::Rotate(FixRotationAngle angle_id) : data_(std::make_shared(angle_id)) {} Rotate::Rotate(float degrees, InterpolationMode resample, bool expand, const std::vector ¢er, const std::vector &fill_value) : data_(std::make_shared(degrees, resample, expand, center, fill_value)) {} std::shared_ptr Rotate::Parse() { #ifndef ENABLE_ANDROID if (!data_->lite_impl_) { return std::make_shared(data_->degrees_, data_->interpolation_mode_, data_->expand_, data_->center_, data_->fill_value_); } #else if (data_->lite_impl_) { return std::make_shared(data_->angle_id_); } #endif // not ENABLE_ANDROID std::string platform = data_->lite_impl_ ? "Cloud" : "Android"; MS_LOG(ERROR) << "This Rotate API is not supported for " + platform + ", use another Rotate API."; return nullptr; } // SlicePatches Transform Operation. struct SlicePatches::Data { Data(int32_t num_height, int32_t num_width, SliceMode slice_mode, uint8_t fill_value) : num_height_(num_height), num_width_(num_width), slice_mode_(slice_mode), fill_value_(fill_value) {} int32_t num_height_; int32_t num_width_; SliceMode slice_mode_; uint8_t fill_value_; }; SlicePatches::SlicePatches(int32_t num_height, int32_t num_width, SliceMode slice_mode, uint8_t fill_value) : data_(std::make_shared(num_height, num_width, slice_mode, fill_value)) {} std::shared_ptr SlicePatches::Parse() { return std::make_shared(data_->num_height_, data_->num_width_, data_->slice_mode_, data_->fill_value_); } // SoftDvppDecodeRandomCropResizeJpeg Transform Operation. struct SoftDvppDecodeRandomCropResizeJpeg::Data { Data(const std::vector &size, const std::vector &scale, const std::vector &ratio, int32_t max_attempts) : size_(size), scale_(scale), ratio_(ratio), max_attempts_(max_attempts) {} std::vector size_; std::vector scale_; std::vector ratio_; int32_t max_attempts_; }; SoftDvppDecodeRandomCropResizeJpeg::SoftDvppDecodeRandomCropResizeJpeg(const std::vector &size, const std::vector &scale, const std::vector &ratio, int32_t max_attempts) : data_(std::make_shared(size, scale, ratio, max_attempts)) {} std::shared_ptr SoftDvppDecodeRandomCropResizeJpeg::Parse() { return std::make_shared(data_->size_, data_->scale_, data_->ratio_, data_->max_attempts_); } // SoftDvppDecodeResizeJpeg Transform Operation. struct SoftDvppDecodeResizeJpeg::Data { explicit Data(const std::vector &size) : size_(size) {} std::vector size_; }; SoftDvppDecodeResizeJpeg::SoftDvppDecodeResizeJpeg(const std::vector &size) : data_(std::make_shared(size)) {} std::shared_ptr SoftDvppDecodeResizeJpeg::Parse() { return std::make_shared(data_->size_); } // SwapRedBlue Transform Operation. SwapRedBlue::SwapRedBlue() = default; std::shared_ptr SwapRedBlue::Parse() { return std::make_shared(); } // UniformAug Transform Operation. struct UniformAugment::Data { std::vector> transforms_; int32_t num_ops_; }; UniformAugment::UniformAugment(const std::vector &transforms, int32_t num_ops) : data_(std::make_shared()) { (void)std::transform( transforms.begin(), transforms.end(), std::back_inserter(data_->transforms_), [](TensorTransform *const op) -> std::shared_ptr { return op ? op->Parse() : nullptr; }); data_->num_ops_ = num_ops; } UniformAugment::UniformAugment(const std::vector> &transforms, int32_t num_ops) : data_(std::make_shared()) { (void)std::transform(transforms.begin(), transforms.end(), std::back_inserter(data_->transforms_), [](const std::shared_ptr &op) -> std::shared_ptr { return op ? op->Parse() : nullptr; }); data_->num_ops_ = num_ops; } UniformAugment::UniformAugment(const std::vector> &transforms, int32_t num_ops) : data_(std::make_shared()) { (void)std::transform(transforms.begin(), transforms.end(), std::back_inserter(data_->transforms_), [](TensorTransform &op) -> std::shared_ptr { return op.Parse(); }); data_->num_ops_ = num_ops; } std::shared_ptr UniformAugment::Parse() { return std::make_shared(data_->transforms_, data_->num_ops_); } // VerticalFlip Transform Operation. VerticalFlip::VerticalFlip() = default; std::shared_ptr VerticalFlip::Parse() { return std::make_shared(); } #endif // not ENABLE_ANDROID } // namespace vision } // namespace dataset } // namespace mindspore