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/resize_with_bbox_ir.h"
18
19 #include <algorithm>
20
21 #ifndef ENABLE_ANDROID
22 #include "minddata/dataset/kernels/image/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 // ResizeWithBBoxOperation
ResizeWithBBoxOperation(const std::vector<int32_t> & size,InterpolationMode interpolation)32 ResizeWithBBoxOperation::ResizeWithBBoxOperation(const std::vector<int32_t> &size, InterpolationMode interpolation)
33 : size_(size), interpolation_(interpolation) {}
34
35 ResizeWithBBoxOperation::~ResizeWithBBoxOperation() = default;
36
Name() const37 std::string ResizeWithBBoxOperation::Name() const { return kResizeWithBBoxOperation; }
38
ValidateParams()39 Status ResizeWithBBoxOperation::ValidateParams() {
40 RETURN_IF_NOT_OK(ValidateVectorSize("ResizeWithBBox", size_));
41 // interpolation
42 if (interpolation_ != InterpolationMode::kLinear && interpolation_ != InterpolationMode::kNearestNeighbour &&
43 interpolation_ != InterpolationMode::kCubic && interpolation_ != InterpolationMode::kArea &&
44 interpolation_ != InterpolationMode::kCubicPil) {
45 std::string err_msg = "ResizeWithBBox: Invalid InterpolationMode, check input value of enum.";
46 LOG_AND_RETURN_STATUS_SYNTAX_ERROR(err_msg);
47 }
48 return Status::OK();
49 }
50
Build()51 std::shared_ptr<TensorOp> ResizeWithBBoxOperation::Build() {
52 constexpr size_t dimension_zero = 0;
53 constexpr size_t dimension_one = 1;
54 constexpr size_t size_two = 2;
55
56 int32_t height = size_[dimension_zero];
57 int32_t width = 0;
58
59 // User specified the width value.
60 if (size_.size() == size_two) {
61 width = size_[dimension_one];
62 }
63
64 return std::make_shared<ResizeWithBBoxOp>(height, width, interpolation_);
65 }
66
to_json(nlohmann::json * out_json)67 Status ResizeWithBBoxOperation::to_json(nlohmann::json *out_json) {
68 RETURN_UNEXPECTED_IF_NULL(out_json);
69 nlohmann::json args;
70 args["size"] = size_;
71 args["interpolation"] = interpolation_;
72 *out_json = args;
73 return Status::OK();
74 }
75
from_json(nlohmann::json op_params,std::shared_ptr<TensorOperation> * operation)76 Status ResizeWithBBoxOperation::from_json(nlohmann::json op_params, std::shared_ptr<TensorOperation> *operation) {
77 RETURN_UNEXPECTED_IF_NULL(operation);
78 RETURN_IF_NOT_OK(ValidateParamInJson(op_params, "size", kResizeWithBBoxOperation));
79 RETURN_IF_NOT_OK(ValidateParamInJson(op_params, "interpolation", kResizeWithBBoxOperation));
80 std::vector<int32_t> size = op_params["size"];
81 auto interpolation = static_cast<InterpolationMode>(op_params["interpolation"]);
82 *operation = std::make_shared<vision::ResizeWithBBoxOperation>(size, interpolation);
83 return Status::OK();
84 }
85 #endif
86 } // namespace vision
87 } // namespace dataset
88 } // namespace mindspore
89