• 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 #include "minddata/dataset/kernels/ir/vision/decode_ir.h"
17 
18 #include "minddata/dataset/kernels/image/decode_op.h"
19 #if !defined(BUILD_LITE) && defined(ENABLE_D)
20 #include "minddata/dataset/kernels/image/dvpp/ascend910b/dvpp_decode_op.h"
21 #endif
22 #include "minddata/dataset/util/validators.h"
23 
24 namespace mindspore {
25 namespace dataset {
26 namespace vision {
27 // DecodeOperation
DecodeOperation(bool rgb,const std::string & device_target)28 DecodeOperation::DecodeOperation(bool rgb, const std::string &device_target)
29     : rgb_(rgb), device_target_(device_target) {}
30 
31 DecodeOperation::~DecodeOperation() = default;
32 
Name() const33 std::string DecodeOperation::Name() const { return kDecodeOperation; }
34 
ValidateParams()35 Status DecodeOperation::ValidateParams() {
36   // device target
37   if (device_target_ != "CPU" && device_target_ != "Ascend") {
38     std::string err_msg = "Decode: Invalid device target. It's not CPU or Ascend.";
39     LOG_AND_RETURN_STATUS_SYNTAX_ERROR(err_msg);
40   }
41 
42   return Status::OK();
43 }
44 
Build()45 std::shared_ptr<TensorOp> DecodeOperation::Build() {
46   if (device_target_ == "CPU") {
47     return std::make_shared<DecodeOp>(rgb_);
48 #if !defined(BUILD_LITE) && defined(ENABLE_D)
49   } else if (device_target_ == "Ascend") {
50     return std::make_shared<DvppDecodeOp>();
51 #endif
52   } else {
53     MS_LOG(ERROR) << "Decode: Invalid device target. It's not CPU or Ascend.";
54     return nullptr;
55   }
56 }
57 
to_json(nlohmann::json * out_json)58 Status DecodeOperation::to_json(nlohmann::json *out_json) {
59   RETURN_UNEXPECTED_IF_NULL(out_json);
60   (*out_json)["rgb"] = rgb_;
61   (*out_json)["device_target"] = device_target_;
62   return Status::OK();
63 }
64 
from_json(nlohmann::json op_params,std::shared_ptr<TensorOperation> * operation)65 Status DecodeOperation::from_json(nlohmann::json op_params, std::shared_ptr<TensorOperation> *operation) {
66   RETURN_UNEXPECTED_IF_NULL(operation);
67   RETURN_IF_NOT_OK(ValidateParamInJson(op_params, "rgb", kDecodeOperation));
68   RETURN_IF_NOT_OK(ValidateParamInJson(op_params, "device_target", kDecodeOperation));
69   bool rgb = op_params["rgb"];
70   std::string device_target = op_params["device_target"];
71   *operation = std::make_shared<vision::DecodeOperation>(rgb, device_target);
72   return Status::OK();
73 }
74 
Type()75 MapTargetDevice DecodeOperation::Type() {
76   if (device_target_ == "CPU") {
77     return MapTargetDevice::kCpu;
78   } else if (device_target_ == "Ascend") {
79     return MapTargetDevice::kAscend910B;
80   } else {
81     MS_LOG(ERROR) << "Resize: Invalid device target. It's not CPU or Ascend.";
82     return MapTargetDevice::kInvalid;
83   }
84 }
85 }  // namespace vision
86 }  // namespace dataset
87 }  // namespace mindspore
88