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_OP_H_ 17 #define MINDSPORE_CCSRC_MINDDATA_DATASET_KERNELS_IMAGE_RANDOM_CROP_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/image/image_utils.h" 26 #include "minddata/dataset/kernels/tensor_op.h" 27 #include "minddata/dataset/util/status.h" 28 29 namespace mindspore { 30 namespace dataset { 31 class RandomCropOp : public RandomTensorOp { 32 public: 33 RandomCropOp(int32_t crop_height, int32_t crop_width, int32_t pad_top, int32_t pad_bottom, int32_t pad_left, 34 int32_t pad_right, bool pad_if_needed, BorderType padding_mode, uint8_t fill_r, uint8_t fill_g, 35 uint8_t fill_b); 36 37 RandomCropOp(const RandomCropOp &rhs) = default; 38 39 RandomCropOp(RandomCropOp &&rhs) = default; 40 41 ~RandomCropOp() override = default; 42 Print(std::ostream & out)43 void Print(std::ostream &out) const override { out << Name() << ": " << crop_height_ << " " << crop_width_; } 44 45 Status Compute(const TensorRow &input, TensorRow *output) override; 46 47 // Function breaks out the compute function's image padding functionality and makes available to other Ops 48 // Using this class as a base - re-structured to allow for RandomCropWithBBox Augmentation Op 49 // @param input: Input is the original Image 50 // @param pad_image: Pointer to new Padded image 51 // @param t_pad_top: Total Top Padding - Based on input and value calculated in function if required 52 // @param t_pad_bottom: Total bottom Padding - Based on input and value calculated in function if required 53 // @param t_pad_left: Total left Padding - Based on input and value calculated in function if required 54 // @param t_pad_right: Total right Padding - Based on input and value calculated in function if required 55 // @param padded_image_w: Final Width of the 'pad_image' 56 // @param padded_image_h: Final Height of the 'pad_image' 57 // @param crop_further: Whether image required cropping after padding - False if new padded image matches required 58 // dimensions 59 Status ImagePadding(const std::shared_ptr<Tensor> &input, std::shared_ptr<Tensor> *pad_image, int32_t *t_pad_top, 60 int32_t *t_pad_bottom, int32_t *t_pad_left, int32_t *t_pad_right, int32_t *padded_image_w, 61 int32_t *padded_image_h, bool *crop_further); 62 63 // Function breaks X,Y generation functionality out of original compute function and makes available to other Ops 64 void GenRandomXY(int32_t *x, int32_t *y, int32_t padded_image_w, int32_t padded_image_h); 65 66 Status OutputShape(const std::vector<TensorShape> &inputs, std::vector<TensorShape> &outputs) override; 67 Name()68 std::string Name() const override { return kRandomCropOp; } 69 NumInput()70 uint32_t NumInput() override { return 1; } 71 NumOutput()72 uint32_t NumOutput() override { return 1; } 73 74 protected: 75 int32_t crop_height_ = 0; 76 int32_t crop_width_ = 0; 77 78 private: 79 Status RandomCropImg(const std::shared_ptr<Tensor> &input, std::shared_ptr<Tensor> *output, int32_t *x, int32_t *y, 80 int32_t index); 81 82 Status ConstructShape(const TensorShape &in_shape, std::shared_ptr<TensorShape> *out_shape) const; 83 84 int32_t pad_top_ = 0; 85 int32_t pad_bottom_ = 0; 86 int32_t pad_left_ = 0; 87 int32_t pad_right_ = 0; 88 bool pad_if_needed_ = false; 89 BorderType border_type_; 90 uint8_t fill_r_ = 0; 91 uint8_t fill_g_ = 0; 92 uint8_t fill_b_ = 0; 93 }; 94 } // namespace dataset 95 } // namespace mindspore 96 #endif // MINDSPORE_CCSRC_MINDDATA_DATASET_KERNELS_IMAGE_RANDOM_CROP_OP_H_ 97