• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_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 <vector>
22 #include <string>
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 RandomCropAndResizeOp : public TensorOp {
32  public:
33   // Default values, also used by python_bindings.cc
34   static const float kDefScaleLb;
35   static const float kDefScaleUb;
36   static const float kDefAspectLb;
37   static const float kDefAspectUb;
38   static const InterpolationMode kDefInterpolation;
39   static const int32_t kDefMaxIter;
40 
41   RandomCropAndResizeOp(int32_t target_height, int32_t target_width, float scale_lb = kDefScaleLb,
42                         float scale_ub = kDefScaleUb, float aspect_lb = kDefAspectLb, float aspect_ub = kDefAspectUb,
43                         InterpolationMode interpolation = kDefInterpolation, int32_t max_attempts = kDefMaxIter);
44 
45   RandomCropAndResizeOp() = default;
46 
47   RandomCropAndResizeOp(const RandomCropAndResizeOp &rhs) = default;
48 
49   RandomCropAndResizeOp(RandomCropAndResizeOp &&rhs) = default;
50 
51   ~RandomCropAndResizeOp() override = default;
52 
Print(std::ostream & out)53   void Print(std::ostream &out) const override {
54     out << "RandomCropAndResize: " << target_height_ << " " << target_width_;
55   }
56 
57   Status Compute(const TensorRow &input, TensorRow *output) override;
58 
59   Status OutputShape(const std::vector<TensorShape> &inputs, std::vector<TensorShape> &outputs) override;
60 
61   Status GetCropBox(int h_in, int w_in, int *x, int *y, int *crop_height, int *crop_width);
62 
Name()63   std::string Name() const override { return kRandomCropAndResizeOp; }
64 
NumInput()65   uint32_t NumInput() override { return -1; }
66 
NumOutput()67   uint32_t NumOutput() override { return -1; }
68 
69  protected:
70   int32_t target_height_;
71   int32_t target_width_;
72   std::uniform_real_distribution<float> rnd_scale_;
73   std::uniform_real_distribution<float> rnd_aspect_;
74   std::mt19937 rnd_;
75   InterpolationMode interpolation_;
76   int32_t max_iter_;
77   double aspect_lb_;
78   double aspect_ub_;
79 };
80 }  // namespace dataset
81 }  // namespace mindspore
82 
83 #endif  // MINDSPORE_CCSRC_MINDDATA_DATASET_KERNELS_IMAGE_RANDOM_CROP_AND_RESIZE_OP_H_
84