• 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/random_solarize_ir.h"
19 
20 #ifndef ENABLE_ANDROID
21 #include "minddata/dataset/kernels/image/random_solarize_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 // 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     MS_LOG(ERROR) << err_msg;
48     RETURN_STATUS_SYNTAX_ERROR(err_msg);
49   }
50   for (size_t i = 0; i < threshold_.size(); ++i) {
51     if (threshold_[i] < 0 || threshold_[i] > kThresholdMax) {
52       std::string err_msg =
53         "RandomSolarize: threshold has to be between 0 and 255, got:" + std::to_string(threshold_[i]);
54       MS_LOG(ERROR) << err_msg;
55       RETURN_STATUS_SYNTAX_ERROR(err_msg);
56     }
57   }
58   if (threshold_[dimension_zero] > threshold_[dimension_one]) {
59     std::string err_msg = "RandomSolarize: threshold must be passed in a (min, max) format";
60     MS_LOG(ERROR) << err_msg;
61     RETURN_STATUS_SYNTAX_ERROR(err_msg);
62   }
63   return Status::OK();
64 }
65 
Build()66 std::shared_ptr<TensorOp> RandomSolarizeOperation::Build() {
67   std::shared_ptr<RandomSolarizeOp> tensor_op = std::make_shared<RandomSolarizeOp>(threshold_);
68   return tensor_op;
69 }
70 
to_json(nlohmann::json * out_json)71 Status RandomSolarizeOperation::to_json(nlohmann::json *out_json) {
72   (*out_json)["threshold"] = threshold_;
73   return Status::OK();
74 }
75 
from_json(nlohmann::json op_params,std::shared_ptr<TensorOperation> * operation)76 Status RandomSolarizeOperation::from_json(nlohmann::json op_params, std::shared_ptr<TensorOperation> *operation) {
77   CHECK_FAIL_RETURN_UNEXPECTED(op_params.find("threshold") != op_params.end(), "Failed to find threshold");
78   std::vector<uint8_t> threshold = op_params["threshold"];
79   *operation = std::make_shared<vision::RandomSolarizeOperation>(threshold);
80   return Status::OK();
81 }
82 
83 #endif
84 }  // namespace vision
85 }  // namespace dataset
86 }  // namespace mindspore
87