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/uniform_aug_ir.h"
19
20 #ifndef ENABLE_ANDROID
21 #include "minddata/dataset/engine/serdes.h"
22 #include "minddata/dataset/kernels/image/uniform_aug_op.h"
23 #endif
24
25 #include "minddata/dataset/kernels/ir/validators.h"
26
27 namespace mindspore {
28 namespace dataset {
29 namespace vision {
30 #ifndef ENABLE_ANDROID
31 // UniformAugOperation
UniformAugOperation(const std::vector<std::shared_ptr<TensorOperation>> & transforms,int32_t num_ops)32 UniformAugOperation::UniformAugOperation(const std::vector<std::shared_ptr<TensorOperation>> &transforms,
33 int32_t num_ops)
34 : transforms_(transforms), num_ops_(num_ops) {}
35
36 UniformAugOperation::~UniformAugOperation() = default;
37
Name() const38 std::string UniformAugOperation::Name() const { return kUniformAugOperation; }
39
ValidateParams()40 Status UniformAugOperation::ValidateParams() {
41 // transforms
42 RETURN_IF_NOT_OK(ValidateVectorTransforms("UniformAug", transforms_));
43 if (num_ops_ > transforms_.size()) {
44 std::string err_msg =
45 "UniformAug: num_ops must be less than or equal to transforms size, but got: " + std::to_string(num_ops_);
46 MS_LOG(ERROR) << err_msg;
47 RETURN_STATUS_SYNTAX_ERROR(err_msg);
48 }
49 // num_ops
50 RETURN_IF_NOT_OK(ValidateIntScalarPositive("UniformAug", "num_ops", num_ops_));
51 return Status::OK();
52 }
53
Build()54 std::shared_ptr<TensorOp> UniformAugOperation::Build() {
55 std::vector<std::shared_ptr<TensorOp>> tensor_ops;
56 (void)std::transform(
57 transforms_.begin(), transforms_.end(), std::back_inserter(tensor_ops),
58 [](const std::shared_ptr<TensorOperation> &op) -> std::shared_ptr<TensorOp> { return op->Build(); });
59 std::shared_ptr<UniformAugOp> tensor_op = std::make_shared<UniformAugOp>(tensor_ops, num_ops_);
60 return tensor_op;
61 }
62
to_json(nlohmann::json * out_json)63 Status UniformAugOperation::to_json(nlohmann::json *out_json) {
64 CHECK_FAIL_RETURN_UNEXPECTED(out_json != nullptr, "parameter out_json is nullptr");
65 nlohmann::json args;
66 std::vector<nlohmann::json> transforms;
67 for (auto op : transforms_) {
68 nlohmann::json op_item, op_args;
69 RETURN_IF_NOT_OK(op->to_json(&op_args));
70 op_item["tensor_op_params"] = op_args;
71 op_item["tensor_op_name"] = op->Name();
72 transforms.push_back(op_item);
73 }
74 args["transforms"] = transforms;
75 args["num_ops"] = num_ops_;
76 *out_json = args;
77 return Status::OK();
78 }
79
from_json(nlohmann::json op_params,std::shared_ptr<TensorOperation> * operation)80 Status UniformAugOperation::from_json(nlohmann::json op_params, std::shared_ptr<TensorOperation> *operation) {
81 CHECK_FAIL_RETURN_UNEXPECTED(op_params.find("transforms") != op_params.end(), "Failed to find transforms");
82 CHECK_FAIL_RETURN_UNEXPECTED(op_params.find("num_ops") != op_params.end(), "Failed to find num_ops");
83 std::vector<std::shared_ptr<TensorOperation>> transforms = {};
84 RETURN_IF_NOT_OK(Serdes::ConstructTensorOps(op_params["transforms"], &transforms));
85 int32_t num_ops = op_params["num_ops"];
86 *operation = std::make_shared<vision::UniformAugOperation>(transforms, num_ops);
87 return Status::OK();
88 }
89 #endif
90 } // namespace vision
91 } // namespace dataset
92 } // namespace mindspore
93