1 /**
2 * Copyright 2020-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/image/random_color_adjust_op.h"
18
19 #include <random>
20
21 #include "minddata/dataset/kernels/image/image_utils.h"
22 #include "minddata/dataset/util/random.h"
23 #include "minddata/dataset/util/status.h"
24
25 namespace mindspore {
26 namespace dataset {
RandomColorAdjustOp(float s_bright_factor,float e_bright_factor,float s_contrast_factor,float e_contrast_factor,float s_saturation_factor,float e_saturation_factor,float s_hue_factor,float e_hue_factor)27 RandomColorAdjustOp::RandomColorAdjustOp(float s_bright_factor, float e_bright_factor, float s_contrast_factor,
28 float e_contrast_factor, float s_saturation_factor, float e_saturation_factor,
29 float s_hue_factor, float e_hue_factor)
30 : bright_factor_start_(s_bright_factor),
31 bright_factor_end_(e_bright_factor),
32 contrast_factor_start_(s_contrast_factor),
33 contrast_factor_end_(e_contrast_factor),
34 saturation_factor_start_(s_saturation_factor),
35 saturation_factor_end_(e_saturation_factor),
36 hue_factor_start_(s_hue_factor),
37 hue_factor_end_(e_hue_factor) {}
38
Compute(const std::shared_ptr<Tensor> & input,std::shared_ptr<Tensor> * output)39 Status RandomColorAdjustOp::Compute(const std::shared_ptr<Tensor> &input, std::shared_ptr<Tensor> *output) {
40 IO_CHECK(input, output);
41 RETURN_IF_NOT_OK(ValidateImage(input, "RandomColorAdjust", {}, {3}, {3}));
42
43 // randomly select an augmentation to apply to the input image until all the transformations run
44 std::vector<std::string> params_vector = {"brightness", "contrast", "saturation", "hue"};
45
46 std::shuffle(params_vector.begin(), params_vector.end(), random_generator_);
47
48 *output = std::static_pointer_cast<Tensor>(input);
49 // determine if certain augmentation needs to be executed:
50 for (const auto ¶m : params_vector) {
51 // case switch
52 if (param == "brightness") {
53 if (CmpFloat(bright_factor_start_, bright_factor_end_) && CmpFloat(bright_factor_start_, 1.0f)) {
54 MS_LOG(DEBUG) << "Not running brightness.";
55 } else {
56 // adjust the brightness of an image
57 float random_factor =
58 std::uniform_real_distribution<float>(bright_factor_start_, bright_factor_end_)(random_generator_);
59 RETURN_IF_NOT_OK(AdjustBrightness(*output, output, random_factor));
60 }
61 } else if (param == "contrast") {
62 if (CmpFloat(contrast_factor_start_, contrast_factor_end_) && CmpFloat(contrast_factor_start_, 1.0f)) {
63 MS_LOG(DEBUG) << "Not running contrast.";
64 } else {
65 float random_factor =
66 std::uniform_real_distribution<float>(contrast_factor_start_, contrast_factor_end_)(random_generator_);
67 RETURN_IF_NOT_OK(AdjustContrast(*output, output, random_factor));
68 }
69 } else if (param == "saturation") {
70 // adjust the Saturation of an image
71 if (CmpFloat(saturation_factor_start_, saturation_factor_end_) && CmpFloat(saturation_factor_start_, 1.0f)) {
72 MS_LOG(DEBUG) << "Not running saturation.";
73 } else {
74 float random_factor =
75 std::uniform_real_distribution<float>(saturation_factor_start_, saturation_factor_end_)(random_generator_);
76 RETURN_IF_NOT_OK(AdjustSaturation(*output, output, random_factor));
77 }
78 } else if (param == "hue") {
79 if (CmpFloat(hue_factor_start_, hue_factor_end_) && CmpFloat(hue_factor_start_, 0.0f)) {
80 MS_LOG(DEBUG) << "Not running hue.";
81 } else {
82 // adjust the Hue of an image
83 float random_factor =
84 std::uniform_real_distribution<float>(hue_factor_start_, hue_factor_end_)(random_generator_);
85 RETURN_IF_NOT_OK(AdjustHue(*output, output, random_factor));
86 }
87 }
88 }
89 // now after we do all the transformations, the last one is fine
90 return Status::OK();
91 }
92 } // namespace dataset
93 } // namespace mindspore
94