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 #include "minddata/dataset/kernels/ir/vision/random_auto_contrast_ir.h"
17
18 #ifndef ENABLE_ANDROID
19 #include "minddata/dataset/kernels/image/random_auto_contrast_op.h"
20 #endif
21 #include "minddata/dataset/kernels/ir/validators.h"
22 #include "minddata/dataset/util/validators.h"
23
24 namespace mindspore {
25 namespace dataset {
26 namespace vision {
27 #ifndef ENABLE_ANDROID
28 // RandomAutoContrastOperation
RandomAutoContrastOperation(float cutoff,const std::vector<uint32_t> & ignore,float prob)29 RandomAutoContrastOperation::RandomAutoContrastOperation(float cutoff, const std::vector<uint32_t> &ignore, float prob)
30 : cutoff_(cutoff), ignore_(ignore), probability_(prob) {}
31
32 RandomAutoContrastOperation::~RandomAutoContrastOperation() = default;
33
Name() const34 std::string RandomAutoContrastOperation::Name() const { return kRandomAutoContrastOperation; }
35
ValidateParams()36 Status RandomAutoContrastOperation::ValidateParams() {
37 RETURN_IF_NOT_OK(ValidateScalar("RandomAutoContrast", "cutoff", cutoff_, {0, 50}, false, true));
38
39 for (auto i = 0; i < ignore_.size(); i++) {
40 RETURN_IF_NOT_OK(ValidateScalar("RandomAutoContrast", "ignore[" + std::to_string(i) + "]", ignore_[i], {0, 255}));
41 }
42
43 RETURN_IF_NOT_OK(ValidateProbability("RandomAutoContrast", probability_));
44 return Status::OK();
45 }
46
Build()47 std::shared_ptr<TensorOp> RandomAutoContrastOperation::Build() {
48 std::shared_ptr<RandomAutoContrastOp> tensor_op =
49 std::make_shared<RandomAutoContrastOp>(cutoff_, ignore_, probability_);
50 return tensor_op;
51 }
52
to_json(nlohmann::json * out_json)53 Status RandomAutoContrastOperation::to_json(nlohmann::json *out_json) {
54 RETURN_UNEXPECTED_IF_NULL(out_json);
55 nlohmann::json args;
56 args["cutoff"] = cutoff_;
57 args["ignore"] = ignore_;
58 args["prob"] = probability_;
59 *out_json = args;
60 return Status::OK();
61 }
62
from_json(nlohmann::json op_params,std::shared_ptr<TensorOperation> * operation)63 Status RandomAutoContrastOperation::from_json(nlohmann::json op_params, std::shared_ptr<TensorOperation> *operation) {
64 RETURN_UNEXPECTED_IF_NULL(operation);
65 RETURN_IF_NOT_OK(ValidateParamInJson(op_params, "cutoff", kRandomAutoContrastOperation));
66 RETURN_IF_NOT_OK(ValidateParamInJson(op_params, "ignore", kRandomAutoContrastOperation));
67 RETURN_IF_NOT_OK(ValidateParamInJson(op_params, "prob", kRandomAutoContrastOperation));
68 float cutoff = op_params["cutoff"];
69 std::vector<uint32_t> ignore = op_params["ignore"];
70 float prob = op_params["prob"];
71 *operation = std::make_shared<vision::RandomAutoContrastOperation>(cutoff, ignore, prob);
72 return Status::OK();
73 }
74 #endif
75 } // namespace vision
76 } // namespace dataset
77 } // namespace mindspore
78