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