• 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_sharpness_ir.h"
18 
19 #include <algorithm>
20 
21 #ifndef ENABLE_ANDROID
22 #include "minddata/dataset/kernels/image/random_sharpness_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 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.0 || degrees_[dimension_one] < 0.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     LOG_AND_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     LOG_AND_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   RETURN_UNEXPECTED_IF_NULL(out_json);
65   (*out_json)["degrees"] = degrees_;
66   return Status::OK();
67 }
68 
from_json(nlohmann::json op_params,std::shared_ptr<TensorOperation> * operation)69 Status RandomSharpnessOperation::from_json(nlohmann::json op_params, std::shared_ptr<TensorOperation> *operation) {
70   RETURN_UNEXPECTED_IF_NULL(operation);
71   RETURN_IF_NOT_OK(ValidateParamInJson(op_params, "degrees", kRandomSharpnessOperation));
72   std::vector<float> degrees = op_params["degrees"];
73   *operation = std::make_shared<vision::RandomSharpnessOperation>(degrees);
74   return Status::OK();
75 }
76 #endif
77 }  // namespace vision
78 }  // namespace dataset
79 }  // namespace mindspore
80