1 /**
2 * Copyright 2021-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
17 #include "minddata/dataset/kernels/ir/vision/random_resized_crop_with_bbox_ir.h"
18
19 #include <algorithm>
20
21 #ifndef ENABLE_ANDROID
22 #include "minddata/dataset/kernels/image/random_crop_and_resize_with_bbox_op.h"
23 #endif
24 #include "minddata/dataset/kernels/ir/validators.h"
25 #include "minddata/dataset/util/validators.h"
26
27 namespace mindspore {
28 namespace dataset {
29 namespace vision {
30 #ifndef ENABLE_ANDROID
31 // RandomResizedCropWithBBoxOperation
RandomResizedCropWithBBoxOperation(const std::vector<int32_t> & size,const std::vector<float> & scale,const std::vector<float> & ratio,InterpolationMode interpolation,int32_t max_attempts)32 RandomResizedCropWithBBoxOperation::RandomResizedCropWithBBoxOperation(const std::vector<int32_t> &size,
33 const std::vector<float> &scale,
34 const std::vector<float> &ratio,
35 InterpolationMode interpolation,
36 int32_t max_attempts)
37 : size_(size), scale_(scale), ratio_(ratio), interpolation_(interpolation), max_attempts_(max_attempts) {}
38
39 RandomResizedCropWithBBoxOperation::~RandomResizedCropWithBBoxOperation() = default;
40
Name() const41 std::string RandomResizedCropWithBBoxOperation::Name() const { return kRandomResizedCropWithBBoxOperation; }
42
ValidateParams()43 Status RandomResizedCropWithBBoxOperation::ValidateParams() {
44 // size
45 RETURN_IF_NOT_OK(ValidateVectorSize("RandomResizedCropWithBBox", size_));
46 // scale
47 RETURN_IF_NOT_OK(ValidateVectorScale("RandomResizedCropWithBBox", scale_));
48 // ratio
49 RETURN_IF_NOT_OK(ValidateVectorRatio("RandomResizedCropWithBBox", ratio_));
50 // max_attempts
51 if (max_attempts_ < 1) {
52 std::string err_msg = "RandomResizedCropWithBBox: max_attempts must be greater than or equal to 1, got: " +
53 std::to_string(max_attempts_);
54 LOG_AND_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 LOG_AND_RETURN_STATUS_SYNTAX_ERROR(err_msg);
62 }
63 return Status::OK();
64 }
65
Build()66 std::shared_ptr<TensorOp> RandomResizedCropWithBBoxOperation::Build() {
67 constexpr size_t dimension_zero = 0;
68 constexpr size_t dimension_one = 1;
69 constexpr size_t size_two = 2;
70
71 int32_t height = size_[dimension_zero];
72 int32_t width = size_[dimension_zero];
73 // User specified the width value.
74 if (size_.size() == size_two) {
75 width = size_[dimension_one];
76 }
77 std::shared_ptr<RandomCropAndResizeWithBBoxOp> tensor_op = std::make_shared<RandomCropAndResizeWithBBoxOp>(
78 height, width, scale_[dimension_zero], scale_[dimension_one], ratio_[dimension_zero], ratio_[dimension_one],
79 interpolation_, max_attempts_);
80 return tensor_op;
81 }
82
to_json(nlohmann::json * out_json)83 Status RandomResizedCropWithBBoxOperation::to_json(nlohmann::json *out_json) {
84 RETURN_UNEXPECTED_IF_NULL(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 RETURN_UNEXPECTED_IF_NULL(operation);
98 RETURN_IF_NOT_OK(ValidateParamInJson(op_params, "size", kRandomResizedCropWithBBoxOperation));
99 RETURN_IF_NOT_OK(ValidateParamInJson(op_params, "scale", kRandomResizedCropWithBBoxOperation));
100 RETURN_IF_NOT_OK(ValidateParamInJson(op_params, "ratio", kRandomResizedCropWithBBoxOperation));
101 RETURN_IF_NOT_OK(ValidateParamInJson(op_params, "interpolation", kRandomResizedCropWithBBoxOperation));
102 RETURN_IF_NOT_OK(ValidateParamInJson(op_params, "max_attempts", kRandomResizedCropWithBBoxOperation));
103 std::vector<int32_t> size = op_params["size"];
104 std::vector<float> scale = op_params["scale"];
105 std::vector<float> ratio = op_params["ratio"];
106 auto interpolation = static_cast<InterpolationMode>(op_params["interpolation"]);
107 int32_t max_attempts = op_params["max_attempts"];
108 *operation =
109 std::make_shared<vision::RandomResizedCropWithBBoxOperation>(size, scale, ratio, interpolation, max_attempts);
110 return Status::OK();
111 }
112 #endif
113 } // namespace vision
114 } // namespace dataset
115 } // namespace mindspore
116