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