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/normalize_pad_ir.h"
18
19 #include <algorithm>
20
21 #ifndef ENABLE_ANDROID
22 #include "minddata/dataset/kernels/image/normalize_pad_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 // NormalizePadOperation
NormalizePadOperation(const std::vector<float> & mean,const std::vector<float> & std,const std::string & dtype,bool is_hwc)32 NormalizePadOperation::NormalizePadOperation(const std::vector<float> &mean, const std::vector<float> &std,
33 const std::string &dtype, bool is_hwc)
34 : mean_(mean), std_(std), dtype_(dtype), is_hwc_(is_hwc) {}
35
36 NormalizePadOperation::~NormalizePadOperation() = default;
37
Name() const38 std::string NormalizePadOperation::Name() const { return kNormalizePadOperation; }
39
ValidateParams()40 Status NormalizePadOperation::ValidateParams() {
41 RETURN_IF_NOT_OK(ValidateVectorMeanStd("NormalizePad", mean_, std_));
42 if (dtype_ != "float32" && dtype_ != "float16") {
43 std::string err_msg = "NormalizePad: dtype must be float32 or float16, but got: " + dtype_;
44 LOG_AND_RETURN_STATUS_SYNTAX_ERROR(err_msg);
45 }
46 return Status::OK();
47 }
48
Build()49 std::shared_ptr<TensorOp> NormalizePadOperation::Build() {
50 return std::make_shared<NormalizePadOp>(mean_, std_, dtype_, is_hwc_);
51 }
52
to_json(nlohmann::json * out_json)53 Status NormalizePadOperation::to_json(nlohmann::json *out_json) {
54 RETURN_UNEXPECTED_IF_NULL(out_json);
55 nlohmann::json args;
56 args["mean"] = mean_;
57 args["std"] = std_;
58 args["dtype"] = dtype_;
59 args["is_hwc"] = is_hwc_;
60 *out_json = args;
61 return Status::OK();
62 }
63
from_json(nlohmann::json op_params,std::shared_ptr<TensorOperation> * operation)64 Status NormalizePadOperation::from_json(nlohmann::json op_params, std::shared_ptr<TensorOperation> *operation) {
65 RETURN_UNEXPECTED_IF_NULL(operation);
66 RETURN_IF_NOT_OK(ValidateParamInJson(op_params, "mean", kNormalizePadOperation));
67 RETURN_IF_NOT_OK(ValidateParamInJson(op_params, "std", kNormalizePadOperation));
68 RETURN_IF_NOT_OK(ValidateParamInJson(op_params, "dtype", kNormalizePadOperation));
69 RETURN_IF_NOT_OK(ValidateParamInJson(op_params, "is_hwc", kNormalizePadOperation));
70 std::vector<float> mean = op_params["mean"];
71 std::vector<float> std = op_params["std"];
72 std::string dtype = op_params["dtype"];
73 bool is_hwc = op_params["is_hwc"];
74 *operation = std::make_shared<vision::NormalizePadOperation>(mean, std, dtype, is_hwc);
75 return Status::OK();
76 }
77 #endif
78 } // namespace vision
79 } // namespace dataset
80 } // namespace mindspore
81