• 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 #include "minddata/dataset/kernels/ir/vision/adjust_sharpness_ir.h"
17 
18 #ifndef ENABLE_ANDROID
19 #include "minddata/dataset/kernels/image/sharpness_op.h"
20 #endif
21 #if !defined(BUILD_LITE) && defined(ENABLE_D)
22 #include "minddata/dataset/kernels/image/dvpp/ascend910b/dvpp_adjust_sharpness_op.h"
23 #endif
24 #include "minddata/dataset/kernels/ir/validators.h"
25 #include "minddata/dataset/util/validators.h"
26 
27 namespace mindspore {
28 namespace dataset {
29 namespace vision {
30 #ifndef ENABLE_ANDROID
31 // AdjustSharpnessOperation
AdjustSharpnessOperation(float sharpness_factor,const std::string & device_target)32 AdjustSharpnessOperation::AdjustSharpnessOperation(float sharpness_factor, const std::string &device_target)
33     : sharpness_factor_(sharpness_factor), device_target_(device_target) {}
34 
ValidateParams()35 Status AdjustSharpnessOperation::ValidateParams() {
36   // sharpness_factor
37   RETURN_IF_NOT_OK(ValidateFloatScalarNonNegative("AdjustSharpness", "sharpness_factor", sharpness_factor_));
38   // device target
39   if (device_target_ != "CPU" && device_target_ != "Ascend") {
40     std::string err_msg = "AdjustSharpness: Invalid device target. It's not CPU or Ascend.";
41     LOG_AND_RETURN_STATUS_SYNTAX_ERROR(err_msg);
42   }
43   return Status::OK();
44 }
45 
Build()46 std::shared_ptr<TensorOp> AdjustSharpnessOperation::Build() {
47   if (device_target_ == "CPU") {
48     std::shared_ptr<SharpnessOp> tensor_op = std::make_shared<SharpnessOp>(sharpness_factor_);
49     return tensor_op;
50 #if !defined(BUILD_LITE) && defined(ENABLE_D)
51   } else if (device_target_ == "Ascend") {
52     return std::make_shared<DvppAdjustSharpnessOp>(sharpness_factor_);
53 #endif
54   } else {
55     MS_LOG(ERROR) << "AdjustSharpness: Invalid device target. It's not CPU or Ascend.";
56     return nullptr;
57   }
58 }
59 
to_json(nlohmann::json * out_json)60 Status AdjustSharpnessOperation::to_json(nlohmann::json *out_json) {
61   RETURN_UNEXPECTED_IF_NULL(out_json);
62   nlohmann::json args;
63   args["sharpness_factor"] = sharpness_factor_;
64   args["device_target"] = device_target_;
65   *out_json = args;
66   return Status::OK();
67 }
68 
from_json(nlohmann::json op_params,std::shared_ptr<TensorOperation> * operation)69 Status AdjustSharpnessOperation::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, "sharpness_factor", kAdjustSharpnessOperation));
72   RETURN_IF_NOT_OK(ValidateParamInJson(op_params, "device_target", kAdjustSharpnessOperation));
73   float sharpness_factor = op_params["sharpness_factor"];
74   std::string device_target = op_params["device_target"];
75   *operation = std::make_shared<vision::AdjustSharpnessOperation>(sharpness_factor, device_target);
76   return Status::OK();
77 }
78 
Type()79 MapTargetDevice AdjustSharpnessOperation::Type() {
80   if (device_target_ == "CPU") {
81     return MapTargetDevice::kCpu;
82   } else if (device_target_ == "Ascend") {
83     return MapTargetDevice::kAscend910B;
84   } else {
85     MS_LOG(ERROR) << "AdjustSharpness: Invalid device target. It's not CPU or Ascend.";
86   }
87   return MapTargetDevice::kInvalid;
88 }
89 #endif
90 }  // namespace vision
91 }  // namespace dataset
92 }  // namespace mindspore
93