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/random_resize_with_bbox_ir.h"
18
19 #include <algorithm>
20
21 #ifndef ENABLE_ANDROID
22 #include "minddata/dataset/kernels/image/random_resize_with_bbox_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 // RandomResizeWithBBoxOperation
RandomResizeWithBBoxOperation(const std::vector<int32_t> & size)32 RandomResizeWithBBoxOperation::RandomResizeWithBBoxOperation(const std::vector<int32_t> &size)
33 : TensorOperation(true), size_(size) {}
34
35 RandomResizeWithBBoxOperation::~RandomResizeWithBBoxOperation() = default;
36
Name() const37 std::string RandomResizeWithBBoxOperation::Name() const { return kRandomResizeWithBBoxOperation; }
38
ValidateParams()39 Status RandomResizeWithBBoxOperation::ValidateParams() {
40 RETURN_IF_NOT_OK(ValidateVectorSize("RandomResizeWithBBox", size_));
41 return Status::OK();
42 }
43
Build()44 std::shared_ptr<TensorOp> RandomResizeWithBBoxOperation::Build() {
45 constexpr size_t dimension_zero = 0;
46 constexpr size_t dimension_one = 1;
47 constexpr size_t size_two = 2;
48
49 // If size is a single value, the smaller edge of the image will be
50 // resized to this value with the same image aspect ratio.
51 int32_t height = size_[dimension_zero];
52 int32_t width = 0;
53
54 // User specified the width value.
55 if (size_.size() == size_two) {
56 width = size_[dimension_one];
57 }
58
59 std::shared_ptr<RandomResizeWithBBoxOp> tensor_op = std::make_shared<RandomResizeWithBBoxOp>(height, width);
60 return tensor_op;
61 }
62
to_json(nlohmann::json * out_json)63 Status RandomResizeWithBBoxOperation::to_json(nlohmann::json *out_json) {
64 RETURN_UNEXPECTED_IF_NULL(out_json);
65 (*out_json)["size"] = size_;
66 return Status::OK();
67 }
68
from_json(nlohmann::json op_params,std::shared_ptr<TensorOperation> * operation)69 Status RandomResizeWithBBoxOperation::from_json(nlohmann::json op_params, std::shared_ptr<TensorOperation> *operation) {
70 RETURN_UNEXPECTED_IF_NULL(operation);
71 RETURN_IF_NOT_OK(ValidateParamInJson(op_params, "size", kRandomResizeWithBBoxOperation));
72 std::vector<int32_t> size = op_params["size"];
73 *operation = std::make_shared<vision::RandomResizeWithBBoxOperation>(size);
74 return Status::OK();
75 }
76 #endif
77 } // namespace vision
78 } // namespace dataset
79 } // namespace mindspore
80