• 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/convert_color_ir.h"
18 
19 #ifndef ENABLE_ANDROID
20 #include "minddata/dataset/kernels/image/convert_color_op.h"
21 #endif
22 #if !defined(BUILD_LITE) && defined(ENABLE_D)
23 #include "minddata/dataset/kernels/image/dvpp/ascend910b/dvpp_convert_color_op.h"
24 #endif
25 #include "minddata/dataset/kernels/ir/validators.h"
26 #include "minddata/dataset/util/validators.h"
27 
28 namespace mindspore {
29 namespace dataset {
30 namespace vision {
31 #ifndef ENABLE_ANDROID
32 // ConvertColorOperation
ConvertColorOperation(ConvertMode convert_mode,const std::string & device_target)33 ConvertColorOperation::ConvertColorOperation(ConvertMode convert_mode, const std::string &device_target)
34     : convert_mode_(convert_mode), device_target_(device_target) {}
35 
36 ConvertColorOperation::~ConvertColorOperation() = default;
37 
Name() const38 std::string ConvertColorOperation::Name() const { return kConvertColorOperation; }
39 
ValidateParams()40 Status ConvertColorOperation::ValidateParams() {
41   if (convert_mode_ < ConvertMode::COLOR_BGR2BGRA || convert_mode_ > ConvertMode::COLOR_RGBA2GRAY) {
42     std::string err_msg = "ConvertColorOperation: convert_mode must be in ConvertMode.";
43     LOG_AND_RETURN_STATUS_SYNTAX_ERROR(err_msg);
44   }
45   // device target
46   if (device_target_ != "CPU" && device_target_ != "Ascend") {
47     std::string err_msg = "ConvertColor: Invalid device target. It's not CPU or Ascend.";
48     LOG_AND_RETURN_STATUS_SYNTAX_ERROR(err_msg);
49   }
50   return Status::OK();
51 }
52 
Build()53 std::shared_ptr<TensorOp> ConvertColorOperation::Build() {
54   if (device_target_ == "CPU") {
55     std::shared_ptr<ConvertColorOp> tensor_op = std::make_shared<ConvertColorOp>(convert_mode_);
56     return tensor_op;
57 #if !defined(BUILD_LITE) && defined(ENABLE_D)
58   } else if (device_target_ == "Ascend") {
59     std::shared_ptr<DvppConvertColorOp> dvpp_tensor_op = std::make_shared<DvppConvertColorOp>(convert_mode_);
60     return dvpp_tensor_op;
61 #endif
62   } else {
63     MS_LOG(ERROR) << "ConvertColor: Invalid device target. It's not CPU or Ascend.";
64     return nullptr;
65   }
66 }
67 
to_json(nlohmann::json * out_json)68 Status ConvertColorOperation::to_json(nlohmann::json *out_json) {
69   RETURN_UNEXPECTED_IF_NULL(out_json);
70   nlohmann::json args;
71   args["convert_mode"] = convert_mode_;
72   args["device_target"] = device_target_;
73   *out_json = args;
74   return Status::OK();
75 }
76 
from_json(nlohmann::json op_params,std::shared_ptr<TensorOperation> * operation)77 Status ConvertColorOperation::from_json(nlohmann::json op_params, std::shared_ptr<TensorOperation> *operation) {
78   RETURN_UNEXPECTED_IF_NULL(operation);
79   RETURN_IF_NOT_OK(ValidateParamInJson(op_params, "convert_mode", kConvertColorOperation));
80   RETURN_IF_NOT_OK(ValidateParamInJson(op_params, "device_target", kConvertColorOperation));
81   auto convert_mode = static_cast<ConvertMode>(op_params["convert_mode"]);
82   std::string device_target = op_params["device_target"];
83   *operation = std::make_shared<vision::ConvertColorOperation>(convert_mode, device_target);
84   return Status::OK();
85 }
86 
Type()87 MapTargetDevice ConvertColorOperation::Type() {
88   if (device_target_ == "CPU") {
89     return MapTargetDevice::kCpu;
90   } else if (device_target_ == "Ascend") {
91     return MapTargetDevice::kAscend910B;
92   } else {
93     MS_LOG(ERROR) << "ConvertColor: Invalid device target. It's not CPU or Ascend.";
94   }
95   return MapTargetDevice::kInvalid;
96 }
97 #endif
98 }  // namespace vision
99 }  // namespace dataset
100 }  // namespace mindspore
101