• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright 2022-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/ir/vision/solarize_ir.h"
18 
19 #include "minddata/dataset/kernels/image/solarize_op.h"
20 #if !defined(BUILD_LITE) && defined(ENABLE_D)
21 #include "minddata/dataset/kernels/image/dvpp/ascend910b/dvpp_solarize_op.h"
22 #endif
23 #include "minddata/dataset/util/validators.h"
24 
25 namespace mindspore {
26 namespace dataset {
27 namespace vision {
28 #ifndef ENABLE_ANDROID
29 // SolarizeOperation
SolarizeOperation(const std::vector<float> & threshold,const std::string & device_target)30 SolarizeOperation::SolarizeOperation(const std::vector<float> &threshold, const std::string &device_target)
31     : threshold_(threshold), device_target_(device_target) {}
32 
33 SolarizeOperation::~SolarizeOperation() = default;
34 
ValidateParams()35 Status SolarizeOperation::ValidateParams() {
36   constexpr size_t kThresholdSize = 2;
37   constexpr float kThresholdMax = 255.0;
38 
39   if (threshold_.size() != kThresholdSize) {
40     std::string err_msg =
41       "Solarize: threshold must be a vector of two values, got: " + std::to_string(threshold_.size());
42     LOG_AND_RETURN_STATUS_SYNTAX_ERROR(err_msg);
43   }
44   for (float threshold_value : threshold_) {
45     if (threshold_value < 0 || threshold_value > kThresholdMax) {
46       std::string err_msg = "Solarize: threshold has to be between 0 and 255, got:" + std::to_string(threshold_value);
47       LOG_AND_RETURN_STATUS_SYNTAX_ERROR(err_msg);
48     }
49   }
50   if (threshold_[0] > threshold_[1]) {
51     std::string err_msg = "Solarize: threshold must be passed in a (min, max) format";
52     LOG_AND_RETURN_STATUS_SYNTAX_ERROR(err_msg);
53   }
54 
55   // device target
56   if (device_target_ != "CPU" && device_target_ != "Ascend") {
57     std::string err_msg = "Solarize: Invalid device target. It's not CPU or Ascend.";
58     LOG_AND_RETURN_STATUS_SYNTAX_ERROR(err_msg);
59   }
60   return Status::OK();
61 }
62 
Build()63 std::shared_ptr<TensorOp> SolarizeOperation::Build() {
64   if (device_target_ == "CPU") {
65     std::shared_ptr<SolarizeOp> tensor_op = std::make_shared<SolarizeOp>(threshold_);
66     return tensor_op;
67 #if !defined(BUILD_LITE) && defined(ENABLE_D)
68   } else if (device_target_ == "Ascend") {
69     std::shared_ptr<DvppSolarizeOp> dvpp_tensor_op = std::make_shared<DvppSolarizeOp>(threshold_);
70     return dvpp_tensor_op;
71 #endif
72   } else {
73     MS_LOG(ERROR) << "Solarize: Invalid device target. It's not CPU or Ascend.";
74     return nullptr;
75   }
76 }
77 
to_json(nlohmann::json * out_json)78 Status SolarizeOperation::to_json(nlohmann::json *out_json) {
79   (*out_json)["threshold"] = threshold_;
80   (*out_json)["device_target"] = device_target_;
81   return Status::OK();
82 }
83 
from_json(nlohmann::json op_params,std::shared_ptr<TensorOperation> * operation)84 Status SolarizeOperation::from_json(nlohmann::json op_params, std::shared_ptr<TensorOperation> *operation) {
85   RETURN_IF_NOT_OK(ValidateParamInJson(op_params, "threshold", kSolarizeOperation));
86   RETURN_IF_NOT_OK(ValidateParamInJson(op_params, "device_target", kSolarizeOperation));
87   std::vector<float> threshold = op_params["threshold"];
88   std::string device_target = op_params["device_target"];
89   *operation = std::make_shared<vision::SolarizeOperation>(threshold, device_target);
90   return Status::OK();
91 }
92 
Type()93 MapTargetDevice SolarizeOperation::Type() {
94   if (device_target_ == "CPU") {
95     return MapTargetDevice::kCpu;
96   } else if (device_target_ == "Ascend") {
97     return MapTargetDevice::kAscend910B;
98   } else {
99     MS_LOG(ERROR) << "Solarize: Invalid device target. It's not CPU or Ascend.";
100   }
101   return MapTargetDevice::kInvalid;
102 }
103 #endif
104 }  // namespace vision
105 }  // namespace dataset
106 }  // namespace mindspore
107