• 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_adjust_ir.h"
18 
19 #include <algorithm>
20 
21 #ifndef ENABLE_ANDROID
22 #include "minddata/dataset/kernels/image/random_color_adjust_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 constexpr size_t dimension_zero = 0;
31 constexpr size_t dimension_one = 1;
32 constexpr size_t size_two = 2;
33 
34 #ifndef ENABLE_ANDROID
35 // RandomColorAdjustOperation.
RandomColorAdjustOperation(const std::vector<float> & brightness,const std::vector<float> & contrast,const std::vector<float> & saturation,const std::vector<float> & hue)36 RandomColorAdjustOperation::RandomColorAdjustOperation(const std::vector<float> &brightness,
37                                                        const std::vector<float> &contrast,
38                                                        const std::vector<float> &saturation,
39                                                        const std::vector<float> &hue)
40     : brightness_(brightness), contrast_(contrast), saturation_(saturation), hue_(hue) {
41   random_op_ = true;
42 }
43 
44 RandomColorAdjustOperation::~RandomColorAdjustOperation() = default;
45 
Name() const46 std::string RandomColorAdjustOperation::Name() const { return kRandomColorAdjustOperation; }
47 
ValidateParams()48 Status RandomColorAdjustOperation::ValidateParams() {
49   // brightness
50   RETURN_IF_NOT_OK(ValidateVectorColorAttribute("RandomColorAdjust", "brightness", brightness_, {0}));
51   // contrast
52   RETURN_IF_NOT_OK(ValidateVectorColorAttribute("RandomColorAdjust", "contrast", contrast_, {0}));
53   // saturation
54   RETURN_IF_NOT_OK(ValidateVectorColorAttribute("RandomColorAdjust", "saturation", saturation_, {0}));
55   // hue
56   RETURN_IF_NOT_OK(ValidateVectorColorAttribute("RandomColorAdjust", "hue", hue_, {-0.5, 0.5}));
57   return Status::OK();
58 }
59 
Build()60 std::shared_ptr<TensorOp> RandomColorAdjustOperation::Build() {
61   float brightness_lb, brightness_ub, contrast_lb, contrast_ub, saturation_lb, saturation_ub, hue_lb, hue_ub;
62 
63   brightness_lb = brightness_[dimension_zero];
64   brightness_ub = brightness_[dimension_zero];
65 
66   if (brightness_.size() == size_two) {
67     brightness_ub = brightness_[dimension_one];
68   }
69 
70   contrast_lb = contrast_[dimension_zero];
71   contrast_ub = contrast_[dimension_zero];
72 
73   if (contrast_.size() == size_two) {
74     contrast_ub = contrast_[dimension_one];
75   }
76 
77   saturation_lb = saturation_[dimension_zero];
78   saturation_ub = saturation_[dimension_zero];
79 
80   if (saturation_.size() == size_two) {
81     saturation_ub = saturation_[dimension_one];
82   }
83 
84   hue_lb = hue_[dimension_zero];
85   hue_ub = hue_[dimension_zero];
86 
87   if (hue_.size() == size_two) {
88     hue_ub = hue_[dimension_one];
89   }
90 
91   std::shared_ptr<RandomColorAdjustOp> tensor_op = std::make_shared<RandomColorAdjustOp>(
92     brightness_lb, brightness_ub, contrast_lb, contrast_ub, saturation_lb, saturation_ub, hue_lb, hue_ub);
93   return tensor_op;
94 }
95 
to_json(nlohmann::json * out_json)96 Status RandomColorAdjustOperation::to_json(nlohmann::json *out_json) {
97   RETURN_UNEXPECTED_IF_NULL(out_json);
98   nlohmann::json args;
99   args["brightness"] = brightness_;
100   args["contrast"] = contrast_;
101   args["saturation"] = saturation_;
102   args["hue"] = hue_;
103   *out_json = args;
104   return Status::OK();
105 }
106 
from_json(nlohmann::json op_params,std::shared_ptr<TensorOperation> * operation)107 Status RandomColorAdjustOperation::from_json(nlohmann::json op_params, std::shared_ptr<TensorOperation> *operation) {
108   RETURN_UNEXPECTED_IF_NULL(operation);
109   RETURN_IF_NOT_OK(ValidateParamInJson(op_params, "brightness", kRandomColorAdjustOperation));
110   RETURN_IF_NOT_OK(ValidateParamInJson(op_params, "contrast", kRandomColorAdjustOperation));
111   RETURN_IF_NOT_OK(ValidateParamInJson(op_params, "saturation", kRandomColorAdjustOperation));
112   RETURN_IF_NOT_OK(ValidateParamInJson(op_params, "hue", kRandomColorAdjustOperation));
113   std::vector<float> brightness = op_params["brightness"];
114   std::vector<float> contrast = op_params["contrast"];
115   std::vector<float> saturation = op_params["saturation"];
116   std::vector<float> hue = op_params["hue"];
117   *operation = std::make_shared<vision::RandomColorAdjustOperation>(brightness, contrast, saturation, hue);
118   return Status::OK();
119 }
120 #endif
121 }  // namespace vision
122 }  // namespace dataset
123 }  // namespace mindspore
124