• 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/auto_contrast_ir.h"
19 
20 #ifndef ENABLE_ANDROID
21 #include "minddata/dataset/kernels/image/auto_contrast_op.h"
22 #endif
23 
24 #include "minddata/dataset/kernels/ir/validators.h"
25 
26 namespace mindspore {
27 namespace dataset {
28 namespace vision {
29 #ifndef ENABLE_ANDROID
30 // AutoContrastOperation
AutoContrastOperation(float cutoff,const std::vector<uint32_t> & ignore)31 AutoContrastOperation::AutoContrastOperation(float cutoff, const std::vector<uint32_t> &ignore)
32     : cutoff_(cutoff), ignore_(ignore) {}
33 
34 AutoContrastOperation::~AutoContrastOperation() = default;
35 
Name() const36 std::string AutoContrastOperation::Name() const { return kAutoContrastOperation; }
37 
ValidateParams()38 Status AutoContrastOperation::ValidateParams() {
39   if (cutoff_ < 0 || cutoff_ > 100) {
40     std::string err_msg = "AutoContrast: cutoff has to be between 0 and 100, got: " + std::to_string(cutoff_);
41     MS_LOG(ERROR) << err_msg;
42     RETURN_STATUS_SYNTAX_ERROR(err_msg);
43   }
44 
45   constexpr uint32_t kMaxIgnoreSize = 255;
46   for (uint32_t single_ignore : ignore_) {
47     if (single_ignore > kMaxIgnoreSize) {
48       std::string err_msg =
49         "AutoContrast: invalid size, ignore has to be between 0 and 255, got: " + std::to_string(single_ignore);
50       MS_LOG(ERROR) << err_msg;
51       RETURN_STATUS_SYNTAX_ERROR(err_msg);
52     }
53   }
54   return Status::OK();
55 }
56 
Build()57 std::shared_ptr<TensorOp> AutoContrastOperation::Build() {
58   std::shared_ptr<AutoContrastOp> tensor_op = std::make_shared<AutoContrastOp>(cutoff_, ignore_);
59   return tensor_op;
60 }
61 
to_json(nlohmann::json * out_json)62 Status AutoContrastOperation::to_json(nlohmann::json *out_json) {
63   nlohmann::json args;
64   args["cutoff"] = cutoff_;
65   args["ignore"] = ignore_;
66   *out_json = args;
67   return Status::OK();
68 }
69 
from_json(nlohmann::json op_params,std::shared_ptr<TensorOperation> * operation)70 Status AutoContrastOperation::from_json(nlohmann::json op_params, std::shared_ptr<TensorOperation> *operation) {
71   CHECK_FAIL_RETURN_UNEXPECTED(op_params.find("cutoff") != op_params.end(), "Failed to find cutoff");
72   CHECK_FAIL_RETURN_UNEXPECTED(op_params.find("ignore") != op_params.end(), "Failed to find ignore");
73   float cutoff = op_params["cutoff"];
74   std::vector<uint32_t> ignore = op_params["ignore"];
75   *operation = std::make_shared<vision::AutoContrastOperation>(cutoff, ignore);
76   return Status::OK();
77 }
78 
79 #endif
80 }  // namespace vision
81 }  // namespace dataset
82 }  // namespace mindspore
83