• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright 2020-2024 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/ir/vision/auto_contrast_ir.h"
18 
19 #include <algorithm>
20 
21 #ifndef ENABLE_ANDROID
22 #include "minddata/dataset/kernels/image/auto_contrast_op.h"
23 #endif
24 #if !defined(BUILD_LITE) && defined(ENABLE_D)
25 #include "minddata/dataset/kernels/image/dvpp/ascend910b/dvpp_auto_contrast_op.h"
26 #endif
27 #include "minddata/dataset/kernels/ir/validators.h"
28 #include "minddata/dataset/util/validators.h"
29 
30 namespace mindspore {
31 namespace dataset {
32 namespace vision {
33 #ifndef ENABLE_ANDROID
34 // AutoContrastOperation
AutoContrastOperation(float cutoff,const std::vector<uint32_t> & ignore,const std::string & device_target)35 AutoContrastOperation::AutoContrastOperation(float cutoff, const std::vector<uint32_t> &ignore,
36                                              const std::string &device_target)
37     : cutoff_(cutoff), ignore_(ignore), device_target_(device_target) {}
38 
39 AutoContrastOperation::~AutoContrastOperation() = default;
40 
Name() const41 std::string AutoContrastOperation::Name() const { return kAutoContrastOperation; }
42 
ValidateParams()43 Status AutoContrastOperation::ValidateParams() {
44   constexpr float kMaxCutOff = 100.0;
45   if (cutoff_ < 0.0 || cutoff_ > kMaxCutOff) {
46     std::string err_msg = "AutoContrast: 'cutoff' has to be between 0 and 100, got: " + std::to_string(cutoff_);
47     LOG_AND_RETURN_STATUS_SYNTAX_ERROR(err_msg);
48   }
49 
50   constexpr uint32_t kMaxIgnoreSize = 255;
51   for (uint32_t single_ignore : ignore_) {
52     if (single_ignore > kMaxIgnoreSize) {
53       std::string err_msg =
54         "AutoContrast: invalid size, 'ignore' has to be between 0 and 255, got: " + std::to_string(single_ignore);
55       LOG_AND_RETURN_STATUS_SYNTAX_ERROR(err_msg);
56     }
57   }
58   // device target
59   if (device_target_ != "CPU" && device_target_ != "Ascend") {
60     std::string err_msg = "AutoContrast: Invalid device target. It's not CPU or Ascend.";
61     LOG_AND_RETURN_STATUS_SYNTAX_ERROR(err_msg);
62   }
63   return Status::OK();
64 }
65 
Build()66 std::shared_ptr<TensorOp> AutoContrastOperation::Build() {
67   if (device_target_ == "CPU") {
68     std::shared_ptr<AutoContrastOp> tensor_op = std::make_shared<AutoContrastOp>(cutoff_, ignore_);
69     return tensor_op;
70 #if !defined(BUILD_LITE) && defined(ENABLE_D)
71   } else if (device_target_ == "Ascend") {
72     std::vector<float> dvpp_cutoff = {cutoff_, cutoff_};
73     std::shared_ptr<DvppAutoContrastOp> dvpp_tensor_op = std::make_shared<DvppAutoContrastOp>(dvpp_cutoff, ignore_);
74     return dvpp_tensor_op;
75 #endif
76   } else {
77     MS_LOG(ERROR) << "AutoContrast: Invalid device target. It's not CPU or Ascend.";
78     return nullptr;
79   }
80 }
81 
to_json(nlohmann::json * out_json)82 Status AutoContrastOperation::to_json(nlohmann::json *out_json) {
83   RETURN_UNEXPECTED_IF_NULL(out_json);
84   nlohmann::json args;
85   args["cutoff"] = cutoff_;
86   args["ignore"] = ignore_;
87   args["device_target"] = device_target_;
88   *out_json = args;
89   return Status::OK();
90 }
91 
from_json(nlohmann::json op_params,std::shared_ptr<TensorOperation> * operation)92 Status AutoContrastOperation::from_json(nlohmann::json op_params, std::shared_ptr<TensorOperation> *operation) {
93   RETURN_UNEXPECTED_IF_NULL(operation);
94   RETURN_IF_NOT_OK(ValidateParamInJson(op_params, "cutoff", kAutoContrastOperation));
95   RETURN_IF_NOT_OK(ValidateParamInJson(op_params, "ignore", kAutoContrastOperation));
96   RETURN_IF_NOT_OK(ValidateParamInJson(op_params, "device_target", kAutoContrastOperation));
97   float cutoff = op_params["cutoff"];
98   std::vector<uint32_t> ignore = op_params["ignore"];
99   std::string device_target = op_params["device_target"];
100   *operation = std::make_shared<vision::AutoContrastOperation>(cutoff, ignore, device_target);
101   return Status::OK();
102 }
103 
Type()104 MapTargetDevice AutoContrastOperation::Type() {
105   if (device_target_ == "CPU") {
106     return MapTargetDevice::kCpu;
107   } else if (device_target_ == "Ascend") {
108     return MapTargetDevice::kAscend910B;
109   } else {
110     MS_LOG(ERROR) << "AutoContrast: Invalid device target. It's not CPU or Ascend.";
111   }
112   return MapTargetDevice::kInvalid;
113 }
114 #endif
115 }  // namespace vision
116 }  // namespace dataset
117 }  // namespace mindspore
118