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_color_ir.h"
19
20 #ifndef ENABLE_ANDROID
21 #include "minddata/dataset/kernels/image/random_color_op.h"
22
23 #endif
24
25 #include "minddata/dataset/kernels/ir/validators.h"
26
27 namespace mindspore {
28 namespace dataset {
29 namespace vision {
30 #ifndef ENABLE_ANDROID
31 // RandomColorOperation.
RandomColorOperation(float t_lb,float t_ub)32 RandomColorOperation::RandomColorOperation(float t_lb, float t_ub) : t_lb_(t_lb), t_ub_(t_ub) { random_op_ = true; }
33
34 RandomColorOperation::~RandomColorOperation() = default;
35
Name() const36 std::string RandomColorOperation::Name() const { return kRandomColorOperation; }
37
ValidateParams()38 Status RandomColorOperation::ValidateParams() {
39 if (t_lb_ < 0 || t_ub_ < 0) {
40 std::string err_msg =
41 "RandomColor: lower bound or upper bound must be greater than or equal to 0, got t_lb: " + std::to_string(t_lb_) +
42 ", t_ub: " + std::to_string(t_ub_);
43 MS_LOG(ERROR) << err_msg;
44 RETURN_STATUS_SYNTAX_ERROR(err_msg);
45 }
46 if (t_lb_ > t_ub_) {
47 std::string err_msg =
48 "RandomColor: lower bound must be less or equal to upper bound, got t_lb: " + std::to_string(t_lb_) +
49 ", t_ub: " + std::to_string(t_ub_);
50 MS_LOG(ERROR) << err_msg;
51 RETURN_STATUS_SYNTAX_ERROR(err_msg);
52 }
53 return Status::OK();
54 }
55
Build()56 std::shared_ptr<TensorOp> RandomColorOperation::Build() {
57 std::shared_ptr<RandomColorOp> tensor_op = std::make_shared<RandomColorOp>(t_lb_, t_ub_);
58 return tensor_op;
59 }
60
to_json(nlohmann::json * out_json)61 Status RandomColorOperation::to_json(nlohmann::json *out_json) {
62 (*out_json)["degrees"] = std::vector<float>{t_lb_, t_ub_};
63 return Status::OK();
64 }
65
from_json(nlohmann::json op_params,std::shared_ptr<TensorOperation> * operation)66 Status RandomColorOperation::from_json(nlohmann::json op_params, std::shared_ptr<TensorOperation> *operation) {
67 CHECK_FAIL_RETURN_UNEXPECTED(op_params.find("degrees") != op_params.end(), "Failed to find degrees");
68 std::vector<float> degrees = op_params["degrees"];
69 CHECK_FAIL_RETURN_UNEXPECTED(degrees.size() == 2, "The number of degrees should be 2");
70 float t_lb = degrees[0];
71 float t_ub = degrees[1];
72 *operation = std::make_shared<vision::RandomColorOperation>(t_lb, t_ub);
73 return Status::OK();
74 }
75
76 #endif
77 } // namespace vision
78 } // namespace dataset
79 } // namespace mindspore
80