1 /**
2 * Copyright 2020-2021 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 <algorithm>
17
18 #include "minddata/dataset/kernels/ir/vision/normalize_pad_ir.h"
19
20 #ifndef ENABLE_ANDROID
21 #include "minddata/dataset/kernels/image/normalize_pad_op.h"
22 #endif
23
24 #include "minddata/dataset/kernels/ir/validators.h"
25
26 namespace mindspore {
27 namespace dataset {
28 namespace vision {
29 #ifndef ENABLE_ANDROID
30 // NormalizePadOperation
NormalizePadOperation(const std::vector<float> & mean,const std::vector<float> & std,const std::string & dtype)31 NormalizePadOperation::NormalizePadOperation(const std::vector<float> &mean, const std::vector<float> &std,
32 const std::string &dtype)
33 : mean_(mean), std_(std), dtype_(dtype) {}
34
35 NormalizePadOperation::~NormalizePadOperation() = default;
36
Name() const37 std::string NormalizePadOperation::Name() const { return kNormalizePadOperation; }
38
ValidateParams()39 Status NormalizePadOperation::ValidateParams() {
40 RETURN_IF_NOT_OK(ValidateVectorMeanStd("NormalizePad", mean_, std_));
41 if (dtype_ != "float32" && dtype_ != "float16") {
42 std::string err_msg = "NormalizePad: dtype must be float32 or float16, but got: " + dtype_;
43 MS_LOG(ERROR) << err_msg;
44 RETURN_STATUS_SYNTAX_ERROR(err_msg);
45 }
46 return Status::OK();
47 }
48
Build()49 std::shared_ptr<TensorOp> NormalizePadOperation::Build() {
50 constexpr size_t dimension_zero = 0;
51 constexpr size_t dimension_one = 1;
52 constexpr size_t dimension_two = 2;
53 return std::make_shared<NormalizePadOp>(mean_[dimension_zero], mean_[dimension_one], mean_[dimension_two],
54 std_[dimension_zero], std_[dimension_one], std_[dimension_two], dtype_);
55 }
56
to_json(nlohmann::json * out_json)57 Status NormalizePadOperation::to_json(nlohmann::json *out_json) {
58 nlohmann::json args;
59 args["mean"] = mean_;
60 args["std"] = std_;
61 args["dtype"] = dtype_;
62 *out_json = args;
63 return Status::OK();
64 }
65
from_json(nlohmann::json op_params,std::shared_ptr<TensorOperation> * operation)66 Status NormalizePadOperation::from_json(nlohmann::json op_params, std::shared_ptr<TensorOperation> *operation) {
67 CHECK_FAIL_RETURN_UNEXPECTED(op_params.find("mean") != op_params.end(), "Failed to find mean");
68 CHECK_FAIL_RETURN_UNEXPECTED(op_params.find("std") != op_params.end(), "Failed to find std");
69 CHECK_FAIL_RETURN_UNEXPECTED(op_params.find("dtype") != op_params.end(), "Failed to find dtype");
70 std::vector<float> mean = op_params["mean"];
71 std::vector<float> std = op_params["std"];
72 std::string dtype = op_params["dtype"];
73 *operation = std::make_shared<vision::NormalizePadOperation>(mean, std, dtype);
74 return Status::OK();
75 }
76
77 #endif
78 } // namespace vision
79 } // namespace dataset
80 } // namespace mindspore
81