/** * Copyright 2020-2021 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/vision.h" #ifdef ENABLE_ACL #include "minddata/dataset/include/vision_ascend.h" #include "minddata/dataset/kernels/ir/vision/ascend_vision_ir.h" #endif #include "minddata/dataset/include/transforms.h" #include "minddata/dataset/kernels/ir/vision/vision_ir.h" #ifndef ENABLE_ANDROID #include "minddata/dataset/kernels/image/image_utils.h" #include "utils/log_adapter.h" #else #include "mindspore/lite/src/common/log_adapter.h" #endif #include "minddata/dataset/kernels/ir/validators.h" // Kernel image headers (in alphabetical order) namespace mindspore { namespace dataset { // Transform operations for computer vision. namespace vision { #ifndef ENABLE_ANDROID // CONSTRUCTORS FOR API CLASSES TO CREATE VISION TENSOR TRANSFORM OPERATIONS // (In alphabetical order) Affine::Affine(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) {} std::shared_ptr Affine::Parse() { return std::make_shared(degrees_, translation_, scale_, shear_, interpolation_, fill_value_); } // AutoContrast Transform Operation. AutoContrast::AutoContrast(float cutoff, std::vector ignore) : cutoff_(cutoff), ignore_(ignore) {} std::shared_ptr AutoContrast::Parse() { return std::make_shared(cutoff_, ignore_); } // BoundingBoxAugment Transform Operation. BoundingBoxAugment::BoundingBoxAugment(TensorTransform *transform, float ratio) : ratio_(ratio) { transform_ = transform ? transform->Parse() : nullptr; } BoundingBoxAugment::BoundingBoxAugment(const std::shared_ptr &transform, float ratio) : ratio_(ratio) { transform_ = transform ? transform->Parse() : nullptr; } BoundingBoxAugment::BoundingBoxAugment(const std::reference_wrapper transform, float ratio) : ratio_(ratio) { transform_ = transform.get().Parse(); } std::shared_ptr BoundingBoxAugment::Parse() { return std::make_shared(transform_, ratio_); } #endif // not ENABLE_ANDROID // CenterCrop Transform Operation. CenterCrop::CenterCrop(std::vector size) : size_(size) {} std::shared_ptr CenterCrop::Parse() { return std::make_shared(size_); } std::shared_ptr CenterCrop::Parse(const MapTargetDevice &env) { if (env == MapTargetDevice::kAscend310) { #ifdef ENABLE_ACL std::vector usize_; usize_.reserve(size_.size()); std::transform(size_.begin(), size_.end(), std::back_inserter(usize_), [](int32_t i) { return (uint32_t)i; }); return std::make_shared(usize_); #endif // ENABLE_ACL } return std::make_shared(size_); } // Crop Transform Operation. Crop::Crop(std::vector coordinates, std::vector size) : coordinates_(coordinates), size_(size) {} std::shared_ptr Crop::Parse() { return std::make_shared(coordinates_, size_); } #ifndef ENABLE_ANDROID // CutMixBatch Transform Operation. CutMixBatch::CutMixBatch(ImageBatchFormat image_batch_format, float alpha, float prob) : image_batch_format_(image_batch_format), alpha_(alpha), prob_(prob) {} std::shared_ptr CutMixBatch::Parse() { return std::make_shared(image_batch_format_, alpha_, prob_); } // CutOutOp. CutOut::CutOut(int32_t length, int32_t num_patches) : length_(length), num_patches_(num_patches) {} std::shared_ptr CutOut::Parse() { return std::make_shared(length_, num_patches_); } #endif // not ENABLE_ANDROID // Decode Transform Operation. Decode::Decode(bool rgb) : rgb_(rgb) {} std::shared_ptr Decode::Parse() { return std::make_shared(rgb_); } std::shared_ptr Decode::Parse(const MapTargetDevice &env) { if (env == MapTargetDevice::kAscend310) { #ifdef ENABLE_ACL return std::make_shared(); #endif // ENABLE_ACL } return std::make_shared(rgb_); } #ifdef ENABLE_ACL // DvppDecodeResize Transform Operation. DvppDecodeResizeJpeg::DvppDecodeResizeJpeg(std::vector resize) : resize_(resize) {} std::shared_ptr DvppDecodeResizeJpeg::Parse() { return std::make_shared(resize_); } std::shared_ptr DvppDecodeResizeJpeg::Parse(const MapTargetDevice &env) { return std::make_shared(resize_); } // DvppDecodeResizeCrop Transform Operation. DvppDecodeResizeCropJpeg::DvppDecodeResizeCropJpeg(std::vector crop, std::vector resize) : crop_(crop), resize_(resize) {} std::shared_ptr DvppDecodeResizeCropJpeg::Parse() { return std::make_shared(crop_, resize_); } std::shared_ptr DvppDecodeResizeCropJpeg::Parse(const MapTargetDevice &env) { return std::make_shared(crop_, resize_); } // DvppDecodePng Transform Operation. DvppDecodePng::DvppDecodePng() {} std::shared_ptr DvppDecodePng::Parse() { return std::make_shared(); } std::shared_ptr DvppDecodePng::Parse(const MapTargetDevice &env) { return std::make_shared(); } #endif // ENABLE_ACL #ifndef ENABLE_ANDROID // Equalize Transform Operation. Equalize::Equalize() {} std::shared_ptr Equalize::Parse() { return std::make_shared(); } // HwcToChw Transform Operation. HWC2CHW::HWC2CHW() {} std::shared_ptr HWC2CHW::Parse() { return std::make_shared(); } // Invert Transform Operation. Invert::Invert() {} std::shared_ptr Invert::Parse() { return std::make_shared(); } // MixUpBatch Transform Operation. MixUpBatch::MixUpBatch(float alpha) : alpha_(alpha) {} std::shared_ptr MixUpBatch::Parse() { return std::make_shared(alpha_); } #endif // not ENABLE_ANDROID // Normalize Transform Operation. Normalize::Normalize(std::vector mean, std::vector std) : mean_(mean), std_(std) {} std::shared_ptr Normalize::Parse() { return std::make_shared(mean_, std_); } std::shared_ptr Normalize::Parse(const MapTargetDevice &env) { if (env == MapTargetDevice::kAscend310) { #ifdef ENABLE_ACL return std::make_shared(mean_, std_); #endif } return std::make_shared(mean_, std_); } #ifndef ENABLE_ANDROID // NormalizePad Transform Operation. NormalizePad::NormalizePad(const std::vector &mean, const std::vector &std, const std::string &dtype) : mean_(mean), std_(std), dtype_(dtype) {} std::shared_ptr NormalizePad::Parse() { return std::make_shared(mean_, std_, dtype_); } // Pad Transform Operation. Pad::Pad(std::vector padding, std::vector fill_value, BorderType padding_mode) : padding_(padding), fill_value_(fill_value), padding_mode_(padding_mode) {} std::shared_ptr Pad::Parse() { return std::make_shared(padding_, fill_value_, padding_mode_); } // RandomAffine Transform Operation. 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) : degrees_(degrees), translate_range_(translate_range), scale_range_(scale_range), shear_ranges_(shear_ranges), interpolation_(interpolation), fill_value_(fill_value) {} std::shared_ptr RandomAffine::Parse() { return std::make_shared(degrees_, translate_range_, scale_range_, shear_ranges_, interpolation_, fill_value_); } // RandomColor Transform Operation. RandomColor::RandomColor(float t_lb, float t_ub) : t_lb_(t_lb), t_ub_(t_ub) {} std::shared_ptr RandomColor::Parse() { return std::make_shared(t_lb_, t_ub_); } // RandomColorAdjust Transform Operation. RandomColorAdjust::RandomColorAdjust(std::vector brightness, std::vector contrast, std::vector saturation, std::vector hue) : brightness_(brightness), contrast_(contrast), saturation_(saturation), hue_(hue) {} std::shared_ptr RandomColorAdjust::Parse() { return std::make_shared(brightness_, contrast_, saturation_, hue_); } // RandomCrop Transform Operation. RandomCrop::RandomCrop(std::vector size, std::vector padding, bool pad_if_needed, 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::shared_ptr RandomCrop::Parse() { return std::make_shared(size_, padding_, pad_if_needed_, fill_value_, padding_mode_); } // RandomCropDecodeResize Transform Operation. RandomCropDecodeResize::RandomCropDecodeResize(std::vector size, std::vector scale, std::vector ratio, InterpolationMode interpolation, int32_t max_attempts) : size_(size), scale_(scale), ratio_(ratio), interpolation_(interpolation), max_attempts_(max_attempts) {} std::shared_ptr RandomCropDecodeResize::Parse() { return std::make_shared(size_, scale_, ratio_, interpolation_, max_attempts_); } // RandomCropWithBBox Transform Operation. RandomCropWithBBox::RandomCropWithBBox(std::vector size, std::vector padding, bool pad_if_needed, 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::shared_ptr RandomCropWithBBox::Parse() { return std::make_shared(size_, padding_, pad_if_needed_, fill_value_, padding_mode_); } // RandomHorizontalFlip. RandomHorizontalFlip::RandomHorizontalFlip(float prob) : probability_(prob) {} std::shared_ptr RandomHorizontalFlip::Parse() { return std::make_shared(probability_); } // RandomHorizontalFlipWithBBox RandomHorizontalFlipWithBBox::RandomHorizontalFlipWithBBox(float prob) : probability_(prob) {} std::shared_ptr RandomHorizontalFlipWithBBox::Parse() { return std::make_shared(probability_); } // RandomPosterize Transform Operation. RandomPosterize::RandomPosterize(const std::vector &bit_range) : bit_range_(bit_range) {} std::shared_ptr RandomPosterize::Parse() { return std::make_shared(bit_range_); } // RandomResize Transform Operation. RandomResize::RandomResize(std::vector size) : size_(size) {} std::shared_ptr RandomResize::Parse() { return std::make_shared(size_); } // RandomResizeWithBBox Transform Operation. RandomResizeWithBBox::RandomResizeWithBBox(std::vector size) : size_(size) {} std::shared_ptr RandomResizeWithBBox::Parse() { return std::make_shared(size_); } // RandomResizedCrop Transform Operation. RandomResizedCrop::RandomResizedCrop(std::vector size, std::vector scale, std::vector ratio, InterpolationMode interpolation, int32_t max_attempts) : size_(size), scale_(scale), ratio_(ratio), interpolation_(interpolation), max_attempts_(max_attempts) {} std::shared_ptr RandomResizedCrop::Parse() { return std::make_shared(size_, scale_, ratio_, interpolation_, max_attempts_); } // RandomResizedCrop Transform Operation. RandomResizedCropWithBBox::RandomResizedCropWithBBox(std::vector size, std::vector scale, std::vector ratio, InterpolationMode interpolation, int32_t max_attempts) : size_(size), scale_(scale), ratio_(ratio), interpolation_(interpolation), max_attempts_(max_attempts) {} std::shared_ptr RandomResizedCropWithBBox::Parse() { return std::make_shared(size_, scale_, ratio_, interpolation_, max_attempts_); } // RandomRotation Transform Operation. RandomRotation::RandomRotation(std::vector degrees, InterpolationMode interpolation_mode, bool expand, std::vector center, std::vector fill_value) : degrees_(degrees), interpolation_mode_(interpolation_mode), expand_(expand), center_(center), fill_value_(fill_value) {} std::shared_ptr RandomRotation::Parse() { return std::make_shared(degrees_, interpolation_mode_, expand_, center_, fill_value_); } // RandomSelectSubpolicy Transform Operation. RandomSelectSubpolicy::RandomSelectSubpolicy(std::vector>> policy) { 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 ? op->Parse() : nullptr); double prob = policy[i][j].second; subpolicy.emplace_back(std::move(std::make_pair(operation, prob))); } policy_.emplace_back(subpolicy); } } RandomSelectSubpolicy::RandomSelectSubpolicy( std::vector, double>>> policy) { for (int32_t i = 0; i < policy.size(); i++) { std::vector, double>> subpolicy; for (int32_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))); } policy_.emplace_back(subpolicy); } } RandomSelectSubpolicy::RandomSelectSubpolicy( std::vector, double>>> policy) { 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))); } policy_.emplace_back(subpolicy); } } std::shared_ptr RandomSelectSubpolicy::Parse() { return std::make_shared(policy_); } // RandomSharpness Transform Operation. RandomSharpness::RandomSharpness(std::vector degrees) : degrees_(degrees) {} std::shared_ptr RandomSharpness::Parse() { return std::make_shared(degrees_); } // RandomSolarize Transform Operation. RandomSolarize::RandomSolarize(std::vector threshold) : threshold_(threshold) {} std::shared_ptr RandomSolarize::Parse() { return std::make_shared(threshold_); } // RandomVerticalFlip Transform Operation. RandomVerticalFlip::RandomVerticalFlip(float prob) : probability_(prob) {} std::shared_ptr RandomVerticalFlip::Parse() { return std::make_shared(probability_); } // RandomVerticalFlipWithBBox Transform Operation. RandomVerticalFlipWithBBox::RandomVerticalFlipWithBBox(float prob) : probability_(prob) {} std::shared_ptr RandomVerticalFlipWithBBox::Parse() { return std::make_shared(probability_); } // Rescale Transform Operation. Rescale::Rescale(float rescale, float shift) : rescale_(rescale), shift_(shift) {} std::shared_ptr Rescale::Parse() { return std::make_shared(rescale_, shift_); } #endif // not ENABLE_ANDROID // Resize Transform Operation. Resize::Resize(std::vector size, InterpolationMode interpolation) : size_(size), interpolation_(interpolation) {} std::shared_ptr Resize::Parse() { return std::make_shared(size_, interpolation_); } std::shared_ptr Resize::Parse(const MapTargetDevice &env) { if (env == MapTargetDevice::kAscend310) { #ifdef ENABLE_ACL std::vector usize_; usize_.reserve(size_.size()); std::transform(size_.begin(), size_.end(), std::back_inserter(usize_), [](int32_t i) { return (uint32_t)i; }); return std::make_shared(usize_); #endif // ENABLE_ACL } return std::make_shared(size_, interpolation_); } #ifdef ENABLE_ANDROID // Rotate Transform Operation. Rotate::Rotate() {} std::shared_ptr Rotate::Parse() { return std::make_shared(); } #endif // ENABLE_ANDROID #ifndef ENABLE_ANDROID // ResizeWithBBox Transform Operation. ResizeWithBBox::ResizeWithBBox(std::vector size, InterpolationMode interpolation) : size_(size), interpolation_(interpolation) {} std::shared_ptr ResizeWithBBox::Parse() { return std::make_shared(size_, interpolation_); } // RgbaToBgr Transform Operation. RGBA2BGR::RGBA2BGR() {} std::shared_ptr RGBA2BGR::Parse() { return std::make_shared(); } // RgbaToRgb Transform Operation. RGBA2RGB::RGBA2RGB() {} std::shared_ptr RGBA2RGB::Parse() { return std::make_shared(); } // SoftDvppDecodeRandomCropResizeJpeg Transform Operation. SoftDvppDecodeRandomCropResizeJpeg::SoftDvppDecodeRandomCropResizeJpeg(std::vector size, std::vector scale, std::vector ratio, int32_t max_attempts) : size_(size), scale_(scale), ratio_(ratio), max_attempts_(max_attempts) {} std::shared_ptr SoftDvppDecodeRandomCropResizeJpeg::Parse() { return std::make_shared(size_, scale_, ratio_, max_attempts_); } // SoftDvppDecodeResizeJpeg Transform Operation. SoftDvppDecodeResizeJpeg::SoftDvppDecodeResizeJpeg(std::vector size) : size_(size) {} std::shared_ptr SoftDvppDecodeResizeJpeg::Parse() { return std::make_shared(size_); } // SwapRedBlue Transform Operation. SwapRedBlue::SwapRedBlue() {} std::shared_ptr SwapRedBlue::Parse() { return std::make_shared(); } // UniformAug Transform Operation. UniformAugment::UniformAugment(const std::vector &transforms, int32_t num_ops) : num_ops_(num_ops) { (void)std::transform( transforms.begin(), transforms.end(), std::back_inserter(transforms_), [](TensorTransform *op) -> std::shared_ptr { return op ? op->Parse() : nullptr; }); } UniformAugment::UniformAugment(const std::vector> &transforms, int32_t num_ops) : num_ops_(num_ops) { (void)std::transform( transforms.begin(), transforms.end(), std::back_inserter(transforms_), [](std::shared_ptr op) -> std::shared_ptr { return op ? op->Parse() : nullptr; }); } UniformAugment::UniformAugment(const std::vector> &transforms, int32_t num_ops) : num_ops_(num_ops) { (void)std::transform(transforms.begin(), transforms.end(), std::back_inserter(transforms_), [](TensorTransform &op) -> std::shared_ptr { return op.Parse(); }); } std::shared_ptr UniformAugment::Parse() { return std::make_shared(transforms_, num_ops_); } #endif // not ENABLE_ANDROID } // namespace vision } // namespace dataset } // namespace mindspore