• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "minddata/dataset/kernels/ir/vision/random_resize_ir.h"
17 
18 #include <algorithm>
19 
20 #ifndef ENABLE_ANDROID
21 #include "minddata/dataset/kernels/image/random_resize_op.h"
22 #endif
23 #include "minddata/dataset/kernels/ir/validators.h"
24 #include "minddata/dataset/util/validators.h"
25 
26 namespace mindspore {
27 namespace dataset {
28 namespace vision {
29 #ifndef ENABLE_ANDROID
30 // RandomResizeOperation
RandomResizeOperation(const std::vector<int32_t> & size)31 RandomResizeOperation::RandomResizeOperation(const std::vector<int32_t> &size) : TensorOperation(true), size_(size) {}
32 
33 RandomResizeOperation::~RandomResizeOperation() = default;
34 
Name() const35 std::string RandomResizeOperation::Name() const { return kRandomResizeOperation; }
36 
ValidateParams()37 Status RandomResizeOperation::ValidateParams() {
38   RETURN_IF_NOT_OK(ValidateVectorSize("RandomResize", size_));
39   return Status::OK();
40 }
41 
Build()42 std::shared_ptr<TensorOp> RandomResizeOperation::Build() {
43   constexpr size_t dimension_zero = 0;
44   constexpr size_t dimension_one = 1;
45   constexpr size_t size_two = 2;
46 
47   // If size is a single value, the smaller edge of the image will be
48   // resized to this value with the same image aspect ratio.
49   int32_t height = size_[dimension_zero];
50   int32_t width = 0;
51 
52   // User specified the width value.
53   if (size_.size() == size_two) {
54     width = size_[dimension_one];
55   }
56 
57   std::shared_ptr<RandomResizeOp> tensor_op = std::make_shared<RandomResizeOp>(height, width);
58   return tensor_op;
59 }
60 
to_json(nlohmann::json * out_json)61 Status RandomResizeOperation::to_json(nlohmann::json *out_json) {
62   RETURN_UNEXPECTED_IF_NULL(out_json);
63   (*out_json)["size"] = size_;
64   return Status::OK();
65 }
66 
from_json(nlohmann::json op_params,std::shared_ptr<TensorOperation> * operation)67 Status RandomResizeOperation::from_json(nlohmann::json op_params, std::shared_ptr<TensorOperation> *operation) {
68   RETURN_UNEXPECTED_IF_NULL(operation);
69   RETURN_IF_NOT_OK(ValidateParamInJson(op_params, "size", kRandomResizeOperation));
70   std::vector<int32_t> size = op_params["size"];
71   *operation = std::make_shared<vision::RandomResizeOperation>(size);
72   return Status::OK();
73 }
74 #endif
75 }  // namespace vision
76 }  // namespace dataset
77 }  // namespace mindspore
78