• 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/normalize_ir.h"
17 
18 #include "minddata/dataset/kernels/image/normalize_op.h"
19 #if !defined(BUILD_LITE) && defined(ENABLE_D)
20 #include "minddata/dataset/kernels/image/dvpp/ascend910b/dvpp_normalize_v2_op.h"
21 #endif
22 #include "minddata/dataset/kernels/ir/validators.h"
23 #include "minddata/dataset/util/validators.h"
24 
25 namespace mindspore {
26 namespace dataset {
27 namespace vision {
28 // NormalizeOperation
NormalizeOperation(const std::vector<float> & mean,const std::vector<float> & std,bool is_hwc,const std::string & device_target)29 NormalizeOperation::NormalizeOperation(const std::vector<float> &mean, const std::vector<float> &std, bool is_hwc,
30                                        const std::string &device_target)
31     : mean_(mean), std_(std), is_hwc_(is_hwc), device_target_(device_target) {}
32 
33 NormalizeOperation::~NormalizeOperation() = default;
34 
Name() const35 std::string NormalizeOperation::Name() const { return kNormalizeOperation; }
36 
ValidateParams()37 Status NormalizeOperation::ValidateParams() {
38   RETURN_IF_NOT_OK(ValidateVectorMeanStd("Normalize", mean_, std_));
39   // device target
40   if (device_target_ != "CPU" && device_target_ != "Ascend") {
41     std::string err_msg = "Normalize: Invalid device target. It's not CPU or Ascend.";
42     LOG_AND_RETURN_STATUS_SYNTAX_ERROR(err_msg);
43   }
44   return Status::OK();
45 }
46 
Build()47 std::shared_ptr<TensorOp> NormalizeOperation::Build() {
48   if (device_target_ == "CPU") {
49     return std::make_shared<NormalizeOp>(mean_, std_, is_hwc_);
50 #if !defined(BUILD_LITE) && defined(ENABLE_D)
51   } else if (device_target_ == "Ascend") {
52     return std::make_shared<DvppNormalizeV2Op>(mean_, std_, is_hwc_);
53 #endif
54   } else {
55     MS_LOG(ERROR) << "Normalize: Invalid device target. It's not CPU or Ascend.";
56     return nullptr;
57   }
58 }
59 
to_json(nlohmann::json * out_json)60 Status NormalizeOperation::to_json(nlohmann::json *out_json) {
61   RETURN_UNEXPECTED_IF_NULL(out_json);
62   nlohmann::json args;
63   args["mean"] = mean_;
64   args["std"] = std_;
65   args["is_hwc"] = is_hwc_;
66   args["device_target"] = device_target_;
67   *out_json = args;
68   return Status::OK();
69 }
70 
from_json(nlohmann::json op_params,std::shared_ptr<TensorOperation> * operation)71 Status NormalizeOperation::from_json(nlohmann::json op_params, std::shared_ptr<TensorOperation> *operation) {
72   RETURN_UNEXPECTED_IF_NULL(operation);
73   RETURN_IF_NOT_OK(ValidateParamInJson(op_params, "mean", kNormalizeOperation));
74   RETURN_IF_NOT_OK(ValidateParamInJson(op_params, "std", kNormalizeOperation));
75   RETURN_IF_NOT_OK(ValidateParamInJson(op_params, "is_hwc", kNormalizeOperation));
76   RETURN_IF_NOT_OK(ValidateParamInJson(op_params, "device_target", kNormalizeOperation));
77   std::vector<float> mean = op_params["mean"];
78   std::vector<float> std = op_params["std"];
79   bool is_hwc = op_params["is_hwc"];
80   std::string device_target = op_params["device_target"];
81   *operation = std::make_shared<vision::NormalizeOperation>(mean, std, is_hwc, device_target);
82   return Status::OK();
83 }
84 
Type()85 MapTargetDevice NormalizeOperation::Type() {
86   if (device_target_ == "CPU") {
87     return MapTargetDevice::kCpu;
88   } else if (device_target_ == "Ascend") {
89     return MapTargetDevice::kAscend910B;
90   } else {
91     MS_LOG(ERROR) << "Normalize: Invalid device target. It's not CPU or Ascend.";
92     return MapTargetDevice::kInvalid;
93   }
94 }
95 }  // namespace vision
96 }  // namespace dataset
97 }  // namespace mindspore
98