• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright 2020-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 #include <algorithm>
17 
18 #include "minddata/dataset/kernels/ir/vision/random_resized_crop_with_bbox_ir.h"
19 
20 #ifndef ENABLE_ANDROID
21 #include "minddata/dataset/kernels/image/random_crop_and_resize_with_bbox_op.h"
22 #endif
23 
24 #include "minddata/dataset/kernels/ir/validators.h"
25 
26 namespace mindspore {
27 namespace dataset {
28 namespace vision {
29 #ifndef ENABLE_ANDROID
30 // RandomResizedCropWithBBoxOperation
RandomResizedCropWithBBoxOperation(const std::vector<int32_t> & size,const std::vector<float> & scale,const std::vector<float> & ratio,InterpolationMode interpolation,int32_t max_attempts)31 RandomResizedCropWithBBoxOperation::RandomResizedCropWithBBoxOperation(const std::vector<int32_t> &size,
32                                                                        const std::vector<float> &scale,
33                                                                        const std::vector<float> &ratio,
34                                                                        InterpolationMode interpolation,
35                                                                        int32_t max_attempts)
36     : size_(size), scale_(scale), ratio_(ratio), interpolation_(interpolation), max_attempts_(max_attempts) {}
37 
38 RandomResizedCropWithBBoxOperation::~RandomResizedCropWithBBoxOperation() = default;
39 
Name() const40 std::string RandomResizedCropWithBBoxOperation::Name() const { return kRandomResizedCropWithBBoxOperation; }
41 
ValidateParams()42 Status RandomResizedCropWithBBoxOperation::ValidateParams() {
43   // size
44   RETURN_IF_NOT_OK(ValidateVectorSize("RandomResizedCropWithBBox", size_));
45   // scale
46   RETURN_IF_NOT_OK(ValidateVectorScale("RandomResizedCropWithBBox", scale_));
47   // ratio
48   RETURN_IF_NOT_OK(ValidateVectorRatio("RandomResizedCropWithBBox", ratio_));
49   // max_attempts
50   if (max_attempts_ < 1) {
51     std::string err_msg = "RandomResizedCropWithBBox: max_attempts must be greater than or equal to 1, got: " +
52                           std::to_string(max_attempts_);
53     MS_LOG(ERROR) << err_msg;
54     RETURN_STATUS_SYNTAX_ERROR(err_msg);
55   }
56   // interpolation
57   if (interpolation_ != InterpolationMode::kLinear && interpolation_ != InterpolationMode::kNearestNeighbour &&
58       interpolation_ != InterpolationMode::kCubic && interpolation_ != InterpolationMode::kArea &&
59       interpolation_ != InterpolationMode::kCubicPil) {
60     std::string err_msg = "RandomResizedCropWithBBox: Invalid InterpolationMode, check input value of enum.";
61     MS_LOG(ERROR) << err_msg;
62     RETURN_STATUS_SYNTAX_ERROR(err_msg);
63   }
64   return Status::OK();
65 }
66 
Build()67 std::shared_ptr<TensorOp> RandomResizedCropWithBBoxOperation::Build() {
68   constexpr size_t dimension_zero = 0;
69   constexpr size_t dimension_one = 1;
70   constexpr size_t size_two = 2;
71 
72   int32_t height = size_[dimension_zero];
73   int32_t width = size_[dimension_zero];
74   // User specified the width value.
75   if (size_.size() == size_two) {
76     width = size_[dimension_one];
77   }
78   std::shared_ptr<RandomCropAndResizeWithBBoxOp> tensor_op = std::make_shared<RandomCropAndResizeWithBBoxOp>(
79     height, width, scale_[dimension_zero], scale_[dimension_one], ratio_[dimension_zero], ratio_[dimension_one],
80     interpolation_, max_attempts_);
81   return tensor_op;
82 }
83 
to_json(nlohmann::json * out_json)84 Status RandomResizedCropWithBBoxOperation::to_json(nlohmann::json *out_json) {
85   nlohmann::json args;
86   args["size"] = size_;
87   args["scale"] = scale_;
88   args["ratio"] = ratio_;
89   args["interpolation"] = interpolation_;
90   args["max_attempts"] = max_attempts_;
91   *out_json = args;
92   return Status::OK();
93 }
94 
from_json(nlohmann::json op_params,std::shared_ptr<TensorOperation> * operation)95 Status RandomResizedCropWithBBoxOperation::from_json(nlohmann::json op_params,
96                                                      std::shared_ptr<TensorOperation> *operation) {
97   CHECK_FAIL_RETURN_UNEXPECTED(op_params.find("size") != op_params.end(), "Failed to find size");
98   CHECK_FAIL_RETURN_UNEXPECTED(op_params.find("scale") != op_params.end(), "Failed to find scale");
99   CHECK_FAIL_RETURN_UNEXPECTED(op_params.find("ratio") != op_params.end(), "Failed to find ratio");
100   CHECK_FAIL_RETURN_UNEXPECTED(op_params.find("interpolation") != op_params.end(), "Failed to find interpolation");
101   CHECK_FAIL_RETURN_UNEXPECTED(op_params.find("max_attempts") != op_params.end(), "Failed to find max_attempts");
102   std::vector<int32_t> size = op_params["size"];
103   std::vector<float> scale = op_params["scale"];
104   std::vector<float> ratio = op_params["ratio"];
105   InterpolationMode interpolation = static_cast<InterpolationMode>(op_params["interpolation"]);
106   int32_t max_attempts = op_params["max_attempts"];
107   *operation =
108     std::make_shared<vision::RandomResizedCropWithBBoxOperation>(size, scale, ratio, interpolation, max_attempts);
109   return Status::OK();
110 }
111 
112 #endif
113 }  // namespace vision
114 }  // namespace dataset
115 }  // namespace mindspore
116