1 /** 2 * Copyright 2020-2023 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 #ifndef MINDSPORE_CCSRC_MINDDATA_DATASET_KERNELS_IMAGE_RANDOM_CROP_AND_RESIZE_OP_H_ 17 #define MINDSPORE_CCSRC_MINDDATA_DATASET_KERNELS_IMAGE_RANDOM_CROP_AND_RESIZE_OP_H_ 18 19 #include <memory> 20 #include <random> 21 #include <string> 22 #include <vector> 23 24 #include "minddata/dataset/core/tensor.h" 25 #include "minddata/dataset/kernels/tensor_op.h" 26 #include "minddata/dataset/util/status.h" 27 28 namespace mindspore { 29 namespace dataset { 30 class RandomCropAndResizeOp : public RandomTensorOp { 31 public: 32 RandomCropAndResizeOp(int32_t target_height, int32_t target_width, float scale_lb, float scale_ub, float aspect_lb, 33 float aspect_ub, InterpolationMode interpolation, int32_t max_attempts); 34 35 RandomCropAndResizeOp() = default; 36 37 RandomCropAndResizeOp(const RandomCropAndResizeOp &rhs) = default; 38 39 RandomCropAndResizeOp(RandomCropAndResizeOp &&rhs) = default; 40 41 ~RandomCropAndResizeOp() override = default; 42 Print(std::ostream & out)43 void Print(std::ostream &out) const override { 44 out << "RandomCropAndResize: " << target_height_ << " " << target_width_; 45 } 46 47 Status Compute(const TensorRow &input, TensorRow *output) override; 48 49 Status OutputShape(const std::vector<TensorShape> &inputs, std::vector<TensorShape> &outputs) override; 50 51 TensorShape ComputeOutputShape(const TensorShape &input) const; 52 53 Status GetCropBox(int h_in, int w_in, int *x, int *y, int *crop_height, int *crop_width); 54 Name()55 std::string Name() const override { return kRandomCropAndResizeOp; } 56 NumInput()57 uint32_t NumInput() override { return 1; } 58 NumOutput()59 uint32_t NumOutput() override { return 1; } 60 61 protected: 62 int32_t target_height_; 63 int32_t target_width_; 64 std::uniform_real_distribution<float> rnd_scale_; 65 std::uniform_real_distribution<float> rnd_aspect_; 66 InterpolationMode interpolation_; 67 int32_t max_iter_; 68 double aspect_lb_; 69 double aspect_ub_; 70 }; 71 } // namespace dataset 72 } // namespace mindspore 73 #endif // MINDSPORE_CCSRC_MINDDATA_DATASET_KERNELS_IMAGE_RANDOM_CROP_AND_RESIZE_OP_H_ 74