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