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/bounding_box_augment_ir.h"
19
20 #ifndef ENABLE_ANDROID
21 #include "minddata/dataset/engine/serdes.h"
22 #include "minddata/dataset/kernels/image/bounding_box_augment_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
BoundingBoxAugmentOperation(const std::shared_ptr<TensorOperation> & transform,float ratio)31 BoundingBoxAugmentOperation::BoundingBoxAugmentOperation(const std::shared_ptr<TensorOperation> &transform, float ratio)
32 : transform_(transform), ratio_(ratio) {}
33
34 BoundingBoxAugmentOperation::~BoundingBoxAugmentOperation() = default;
35
Name() const36 std::string BoundingBoxAugmentOperation::Name() const { return kBoundingBoxAugmentOperation; }
37
ValidateParams()38 Status BoundingBoxAugmentOperation::ValidateParams() {
39 RETURN_IF_NOT_OK(ValidateVectorTransforms("BoundingBoxAugment", {transform_}));
40 RETURN_IF_NOT_OK(ValidateScalar("BoundingBoxAugment", "ratio", ratio_, {0.0, 1.0}, false, false));
41 return Status::OK();
42 }
43
Build()44 std::shared_ptr<TensorOp> BoundingBoxAugmentOperation::Build() {
45 std::shared_ptr<BoundingBoxAugmentOp> tensor_op = std::make_shared<BoundingBoxAugmentOp>(transform_->Build(), ratio_);
46 return tensor_op;
47 }
48
to_json(nlohmann::json * out_json)49 Status BoundingBoxAugmentOperation::to_json(nlohmann::json *out_json) {
50 nlohmann::json args, transform_args;
51 nlohmann::json op_item;
52 RETURN_IF_NOT_OK(transform_->to_json(&transform_args));
53 op_item["tensor_op_params"] = transform_args;
54 op_item["tensor_op_name"] = transform_->Name();
55 args["transform"] = op_item;
56 args["ratio"] = ratio_;
57 *out_json = args;
58 return Status::OK();
59 }
60
from_json(nlohmann::json op_params,std::shared_ptr<TensorOperation> * operation)61 Status BoundingBoxAugmentOperation::from_json(nlohmann::json op_params, std::shared_ptr<TensorOperation> *operation) {
62 CHECK_FAIL_RETURN_UNEXPECTED(op_params.find("transform") != op_params.end(), "Failed to find transform");
63 CHECK_FAIL_RETURN_UNEXPECTED(op_params.find("ratio") != op_params.end(), "Failed to find ratio");
64 std::vector<std::shared_ptr<TensorOperation>> transforms;
65 std::vector<nlohmann::json> json_operations = {};
66 json_operations.push_back(op_params["transform"]);
67 RETURN_IF_NOT_OK(Serdes::ConstructTensorOps(json_operations, &transforms));
68 float ratio = op_params["ratio"];
69 CHECK_FAIL_RETURN_UNEXPECTED(transforms.size() == 1,
70 "Expect size one of transforms parameter, but got:" + std::to_string(transforms.size()));
71 *operation = std::make_shared<vision::BoundingBoxAugmentOperation>(transforms[0], ratio);
72 return Status::OK();
73 }
74 #endif
75 } // namespace vision
76 } // namespace dataset
77 } // namespace mindspore
78