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_rotation_ir.h"
18
19 #include <algorithm>
20
21 #ifndef ENABLE_ANDROID
22 #include "minddata/dataset/kernels/image/random_rotation_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 constexpr size_t dimension_zero = 0;
32 constexpr size_t dimension_one = 1;
33 constexpr size_t dimension_two = 2;
34 constexpr size_t size_one = 1;
35 constexpr size_t size_two = 2;
36 constexpr size_t size_three = 3;
37
38 // Function to create RandomRotationOperation.
RandomRotationOperation(const std::vector<float> & degrees,InterpolationMode resample,bool expand,const std::vector<float> & center,const std::vector<uint8_t> & fill_value)39 RandomRotationOperation::RandomRotationOperation(const std::vector<float> °rees, InterpolationMode resample,
40 bool expand, const std::vector<float> ¢er,
41 const std::vector<uint8_t> &fill_value)
42 : TensorOperation(true),
43 degrees_(degrees),
44 interpolation_mode_(resample),
45 expand_(expand),
46 center_(center),
47 fill_value_(fill_value) {}
48
49 RandomRotationOperation::~RandomRotationOperation() = default;
50
Name() const51 std::string RandomRotationOperation::Name() const { return kRandomRotationOperation; }
52
ValidateParams()53 Status RandomRotationOperation::ValidateParams() {
54 // degrees
55 if (degrees_.size() != size_two && degrees_.size() != size_one) {
56 std::string err_msg =
57 "RandomRotation: degrees must be a vector of one or two values, got: " + std::to_string(degrees_.size());
58 LOG_AND_RETURN_STATUS_SYNTAX_ERROR(err_msg);
59 }
60 if (degrees_.size() == size_two && degrees_[dimension_one] < degrees_[dimension_zero]) {
61 std::string err_msg = "RandomRotation: degrees must be in the format of (min, max), got: (" +
62 std::to_string(degrees_[dimension_zero]) + ", " + std::to_string(degrees_[dimension_one]) +
63 ")";
64 LOG_AND_RETURN_STATUS_SYNTAX_ERROR(err_msg);
65 } else if (degrees_.size() == size_one && degrees_[dimension_zero] < 0.0) {
66 std::string err_msg =
67 "RandomRotation: if degrees only has one value, it must be greater than or equal to 0, got: " +
68 std::to_string(degrees_[dimension_zero]);
69 LOG_AND_RETURN_STATUS_SYNTAX_ERROR(err_msg);
70 }
71 // center
72 if (!center_.empty() && center_.size() != size_two) {
73 std::string err_msg =
74 "RandomRotation: center must be a vector of two values or empty, got: " + std::to_string(center_.size());
75 LOG_AND_RETURN_STATUS_SYNTAX_ERROR(err_msg);
76 }
77 // fill_value
78 RETURN_IF_NOT_OK(ValidateVectorFillvalue("RandomRotation", fill_value_));
79 // interpolation
80 if (interpolation_mode_ != InterpolationMode::kLinear &&
81 interpolation_mode_ != InterpolationMode::kNearestNeighbour && interpolation_mode_ != InterpolationMode::kCubic &&
82 interpolation_mode_ != InterpolationMode::kArea) {
83 std::string err_msg = "RandomRotation: Invalid InterpolationMode, check input value of enum.";
84 LOG_AND_RETURN_STATUS_SYNTAX_ERROR(err_msg);
85 }
86 return Status::OK();
87 }
88
Build()89 std::shared_ptr<TensorOp> RandomRotationOperation::Build() {
90 float start_degree, end_degree;
91 if (degrees_.size() == size_one) {
92 start_degree = -degrees_[dimension_zero];
93 end_degree = degrees_[dimension_zero];
94 } else if (degrees_.size() == size_two) {
95 start_degree = degrees_[dimension_zero];
96 end_degree = degrees_[dimension_one];
97 }
98
99 uint8_t fill_r, fill_g, fill_b;
100 fill_r = fill_value_[dimension_zero];
101 fill_g = fill_value_[dimension_zero];
102 fill_b = fill_value_[dimension_zero];
103
104 if (fill_value_.size() == size_three) {
105 fill_r = fill_value_[dimension_zero];
106 fill_g = fill_value_[dimension_one];
107 fill_b = fill_value_[dimension_two];
108 }
109
110 std::shared_ptr<RandomRotationOp> tensor_op = std::make_shared<RandomRotationOp>(
111 start_degree, end_degree, interpolation_mode_, expand_, center_, fill_r, fill_g, fill_b);
112 return tensor_op;
113 }
114
to_json(nlohmann::json * out_json)115 Status RandomRotationOperation::to_json(nlohmann::json *out_json) {
116 RETURN_UNEXPECTED_IF_NULL(out_json);
117 nlohmann::json args;
118 args["degrees"] = degrees_;
119 args["resample"] = interpolation_mode_;
120 args["expand"] = expand_;
121 args["center"] = center_;
122 args["fill_value"] = fill_value_;
123 *out_json = args;
124 return Status::OK();
125 }
126
from_json(nlohmann::json op_params,std::shared_ptr<TensorOperation> * operation)127 Status RandomRotationOperation::from_json(nlohmann::json op_params, std::shared_ptr<TensorOperation> *operation) {
128 RETURN_UNEXPECTED_IF_NULL(operation);
129 RETURN_IF_NOT_OK(ValidateParamInJson(op_params, "degrees", kRandomRotationOperation));
130 RETURN_IF_NOT_OK(ValidateParamInJson(op_params, "resample", kRandomRotationOperation));
131 RETURN_IF_NOT_OK(ValidateParamInJson(op_params, "expand", kRandomRotationOperation));
132 RETURN_IF_NOT_OK(ValidateParamInJson(op_params, "center", kRandomRotationOperation));
133 RETURN_IF_NOT_OK(ValidateParamInJson(op_params, "fill_value", kRandomRotationOperation));
134 std::vector<float> degrees = op_params["degrees"];
135 auto resample = static_cast<InterpolationMode>(op_params["resample"]);
136 bool expand = op_params["expand"];
137 std::vector<float> center = op_params["center"];
138 std::vector<uint8_t> fill_value = op_params["fill_value"];
139 *operation = std::make_shared<vision::RandomRotationOperation>(degrees, resample, expand, center, fill_value);
140 return Status::OK();
141 }
142 #endif
143 } // namespace vision
144 } // namespace dataset
145 } // namespace mindspore
146