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 "minddata/dataset/kernels/ir/vision/resize_ir.h"
17
18 #include "minddata/dataset/kernels/image/resize_op.h"
19
20 #include "minddata/dataset/kernels/ir/validators.h"
21
22 namespace mindspore {
23 namespace dataset {
24 namespace vision {
25 // ResizeOperation
ResizeOperation(const std::vector<int32_t> & size,InterpolationMode interpolation)26 ResizeOperation::ResizeOperation(const std::vector<int32_t> &size, InterpolationMode interpolation)
27 : size_(size), interpolation_(interpolation) {}
28
29 ResizeOperation::~ResizeOperation() = default;
30
Name() const31 std::string ResizeOperation::Name() const { return kResizeOperation; }
32
ValidateParams()33 Status ResizeOperation::ValidateParams() {
34 RETURN_IF_NOT_OK(ValidateVectorSize("Resize", size_));
35 // interpolation
36 if (interpolation_ != InterpolationMode::kLinear && interpolation_ != InterpolationMode::kNearestNeighbour &&
37 interpolation_ != InterpolationMode::kCubic && interpolation_ != InterpolationMode::kArea &&
38 interpolation_ != InterpolationMode::kCubicPil) {
39 std::string err_msg = "Resize: Invalid InterpolationMode, check input value of enum.";
40 MS_LOG(ERROR) << err_msg;
41 RETURN_STATUS_SYNTAX_ERROR(err_msg);
42 }
43 return Status::OK();
44 }
45
Build()46 std::shared_ptr<TensorOp> ResizeOperation::Build() {
47 constexpr size_t dimension_zero = 0;
48 constexpr size_t dimension_one = 1;
49 constexpr size_t size_two = 2;
50
51 // If size is a single value, the smaller edge of the image will be
52 // resized to this value with the same image aspect ratio.
53 int32_t height = size_[dimension_zero];
54 int32_t width = 0;
55
56 // User specified the width value.
57 if (size_.size() == size_two) {
58 width = size_[dimension_one];
59 }
60
61 return std::make_shared<ResizeOp>(height, width, interpolation_);
62 }
63
to_json(nlohmann::json * out_json)64 Status ResizeOperation::to_json(nlohmann::json *out_json) {
65 nlohmann::json args;
66 args["size"] = size_;
67 args["interpolation"] = interpolation_;
68 *out_json = args;
69 return Status::OK();
70 }
71
from_json(nlohmann::json op_params,std::shared_ptr<TensorOperation> * operation)72 Status ResizeOperation::from_json(nlohmann::json op_params, std::shared_ptr<TensorOperation> *operation) {
73 CHECK_FAIL_RETURN_UNEXPECTED(op_params.find("size") != op_params.end(), "Failed to find size");
74 CHECK_FAIL_RETURN_UNEXPECTED(op_params.find("interpolation") != op_params.end(), "Failed to find interpolation");
75 std::vector<int32_t> size = op_params["size"];
76 InterpolationMode interpolation = static_cast<InterpolationMode>(op_params["interpolation"]);
77 *operation = std::make_shared<vision::ResizeOperation>(size, interpolation);
78 return Status::OK();
79 }
80 } // namespace vision
81 } // namespace dataset
82 } // namespace mindspore
83