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