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