• 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_sharpness_ir.h"
19 
20 #ifndef ENABLE_ANDROID
21 #include "minddata/dataset/kernels/image/random_sharpness_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 constexpr size_t dimension_zero = 0;
31 constexpr size_t dimension_one = 1;
32 constexpr size_t size_two = 2;
33 
34 // Function to create RandomSharpness.
RandomSharpnessOperation(const std::vector<float> & degrees)35 RandomSharpnessOperation::RandomSharpnessOperation(const std::vector<float> &degrees)
36     : TensorOperation(true), degrees_(degrees) {}
37 
38 RandomSharpnessOperation::~RandomSharpnessOperation() = default;
39 
Name() const40 std::string RandomSharpnessOperation::Name() const { return kRandomSharpnessOperation; }
41 
ValidateParams()42 Status RandomSharpnessOperation::ValidateParams() {
43   if (degrees_.size() != size_two || degrees_[dimension_zero] < 0 || degrees_[dimension_one] < 0) {
44     std::string err_msg = "RandomSharpness: degrees must be a vector of two values and greater than or equal to 0.";
45     MS_LOG(ERROR) << "RandomSharpness: degrees must be a vector of two values and greater than or equal to 0, got: "
46                   << degrees_;
47     RETURN_STATUS_SYNTAX_ERROR(err_msg);
48   }
49   if (degrees_[dimension_one] < degrees_[dimension_zero]) {
50     std::string err_msg = "RandomSharpness: degrees must be in the format of (min, max).";
51     MS_LOG(ERROR) << "RandomSharpness: degrees must be in the format of (min, max), got: " << degrees_;
52     RETURN_STATUS_SYNTAX_ERROR(err_msg);
53   }
54   return Status::OK();
55 }
56 
Build()57 std::shared_ptr<TensorOp> RandomSharpnessOperation::Build() {
58   std::shared_ptr<RandomSharpnessOp> tensor_op =
59     std::make_shared<RandomSharpnessOp>(degrees_[dimension_zero], degrees_[dimension_one]);
60   return tensor_op;
61 }
62 
to_json(nlohmann::json * out_json)63 Status RandomSharpnessOperation::to_json(nlohmann::json *out_json) {
64   (*out_json)["degrees"] = degrees_;
65   return Status::OK();
66 }
67 
from_json(nlohmann::json op_params,std::shared_ptr<TensorOperation> * operation)68 Status RandomSharpnessOperation::from_json(nlohmann::json op_params, std::shared_ptr<TensorOperation> *operation) {
69   CHECK_FAIL_RETURN_UNEXPECTED(op_params.find("degrees") != op_params.end(), "Failed to find degrees");
70   std::vector<float> degrees = op_params["degrees"];
71   *operation = std::make_shared<vision::RandomSharpnessOperation>(degrees);
72   return Status::OK();
73 }
74 
75 #endif
76 }  // namespace vision
77 }  // namespace dataset
78 }  // namespace mindspore
79