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/resize_with_bbox_ir.h"
19
20 #ifndef ENABLE_ANDROID
21 #include "minddata/dataset/kernels/image/resize_with_bbox_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 // ResizeWithBBoxOperation
ResizeWithBBoxOperation(const std::vector<int32_t> & size,InterpolationMode interpolation)31 ResizeWithBBoxOperation::ResizeWithBBoxOperation(const std::vector<int32_t> &size, InterpolationMode interpolation)
32 : size_(size), interpolation_(interpolation) {}
33
34 ResizeWithBBoxOperation::~ResizeWithBBoxOperation() = default;
35
Name() const36 std::string ResizeWithBBoxOperation::Name() const { return kResizeWithBBoxOperation; }
37
ValidateParams()38 Status ResizeWithBBoxOperation::ValidateParams() {
39 RETURN_IF_NOT_OK(ValidateVectorSize("ResizeWithBBox", size_));
40 // interpolation
41 if (interpolation_ != InterpolationMode::kLinear && interpolation_ != InterpolationMode::kNearestNeighbour &&
42 interpolation_ != InterpolationMode::kCubic && interpolation_ != InterpolationMode::kArea &&
43 interpolation_ != InterpolationMode::kCubicPil) {
44 std::string err_msg = "ResizeWithBBox: Invalid InterpolationMode, check input value of enum.";
45 MS_LOG(ERROR) << err_msg;
46 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 nlohmann::json args;
69 args["size"] = size_;
70 args["interpolation"] = interpolation_;
71 *out_json = args;
72 return Status::OK();
73 }
74
from_json(nlohmann::json op_params,std::shared_ptr<TensorOperation> * operation)75 Status ResizeWithBBoxOperation::from_json(nlohmann::json op_params, std::shared_ptr<TensorOperation> *operation) {
76 CHECK_FAIL_RETURN_UNEXPECTED(op_params.find("size") != op_params.end(), "Failed to find size");
77 CHECK_FAIL_RETURN_UNEXPECTED(op_params.find("interpolation") != op_params.end(), "Failed to find interpolation");
78 std::vector<int32_t> size = op_params["size"];
79 InterpolationMode interpolation = static_cast<InterpolationMode>(op_params["interpolation"]);
80 *operation = std::make_shared<vision::ResizeWithBBoxOperation>(size, interpolation);
81 return Status::OK();
82 }
83
84 #endif
85 } // namespace vision
86 } // namespace dataset
87 } // namespace mindspore
88