• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
17 #include "minddata/dataset/kernels/ir/vision/random_solarize_ir.h"
18 
19 #include <algorithm>
20 
21 #ifndef ENABLE_ANDROID
22 #include "minddata/dataset/kernels/image/random_solarize_op.h"
23 #endif
24 #include "minddata/dataset/util/validators.h"
25 
26 namespace mindspore {
27 namespace dataset {
28 namespace vision {
29 #ifndef ENABLE_ANDROID
30 // RandomSolarizeOperation.
RandomSolarizeOperation(const std::vector<uint8_t> & threshold)31 RandomSolarizeOperation::RandomSolarizeOperation(const std::vector<uint8_t> &threshold)
32     : TensorOperation(true), threshold_(threshold) {}
33 
34 RandomSolarizeOperation::~RandomSolarizeOperation() = default;
35 
Name() const36 std::string RandomSolarizeOperation::Name() const { return kRandomSolarizeOperation; }
37 
ValidateParams()38 Status RandomSolarizeOperation::ValidateParams() {
39   constexpr size_t dimension_zero = 0;
40   constexpr size_t dimension_one = 1;
41   constexpr size_t size_two = 2;
42   constexpr uint8_t kThresholdMax = 255;
43 
44   if (threshold_.size() != size_two) {
45     std::string err_msg =
46       "RandomSolarize: threshold must be a vector of two values, got: " + std::to_string(threshold_.size());
47     LOG_AND_RETURN_STATUS_SYNTAX_ERROR(err_msg);
48   }
49   for (auto threshold_value : threshold_) {
50     if (threshold_value < 0 || threshold_value > kThresholdMax) {
51       std::string err_msg =
52         "RandomSolarize: threshold has to be between 0 and 255, got:" + std::to_string(threshold_value);
53       LOG_AND_RETURN_STATUS_SYNTAX_ERROR(err_msg);
54     }
55   }
56   if (threshold_[dimension_zero] > threshold_[dimension_one]) {
57     std::string err_msg = "RandomSolarize: threshold must be passed in a (min, max) format";
58     LOG_AND_RETURN_STATUS_SYNTAX_ERROR(err_msg);
59   }
60   return Status::OK();
61 }
62 
Build()63 std::shared_ptr<TensorOp> RandomSolarizeOperation::Build() {
64   std::shared_ptr<RandomSolarizeOp> tensor_op = std::make_shared<RandomSolarizeOp>(threshold_);
65   return tensor_op;
66 }
67 
to_json(nlohmann::json * out_json)68 Status RandomSolarizeOperation::to_json(nlohmann::json *out_json) {
69   RETURN_UNEXPECTED_IF_NULL(out_json);
70   (*out_json)["threshold"] = threshold_;
71   return Status::OK();
72 }
73 
from_json(nlohmann::json op_params,std::shared_ptr<TensorOperation> * operation)74 Status RandomSolarizeOperation::from_json(nlohmann::json op_params, std::shared_ptr<TensorOperation> *operation) {
75   RETURN_UNEXPECTED_IF_NULL(operation);
76   RETURN_IF_NOT_OK(ValidateParamInJson(op_params, "threshold", kRandomSolarizeOperation));
77   std::vector<uint8_t> threshold = op_params["threshold"];
78   *operation = std::make_shared<vision::RandomSolarizeOperation>(threshold);
79   return Status::OK();
80 }
81 #endif
82 }  // namespace vision
83 }  // namespace dataset
84 }  // namespace mindspore
85