• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/random_resized_crop_ir.h"
19 
20 #ifndef ENABLE_ANDROID
21 #include "minddata/dataset/kernels/image/random_crop_and_resize_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 RandomResizedCropOperation::RandomResizedCropOperation(const RandomResizedCropOperation &) = default;
31 
32 // RandomResizedCropOperation
RandomResizedCropOperation(const std::vector<int32_t> & size,const std::vector<float> & scale,const std::vector<float> & ratio,InterpolationMode interpolation,int32_t max_attempts)33 RandomResizedCropOperation::RandomResizedCropOperation(const std::vector<int32_t> &size,
34                                                        const std::vector<float> &scale, const std::vector<float> &ratio,
35                                                        InterpolationMode interpolation, int32_t max_attempts)
36     : TensorOperation(true),
37       size_(size),
38       scale_(scale),
39       ratio_(ratio),
40       interpolation_(interpolation),
41       max_attempts_(max_attempts) {}
42 
43 RandomResizedCropOperation::~RandomResizedCropOperation() = default;
44 
Name() const45 std::string RandomResizedCropOperation::Name() const { return kRandomResizedCropOperation; }
46 
ValidateParams()47 Status RandomResizedCropOperation::ValidateParams() {
48   // size
49   RETURN_IF_NOT_OK(ValidateVectorSize(Name(), size_));
50   // scale
51   RETURN_IF_NOT_OK(ValidateVectorScale(Name(), scale_));
52   // ratio
53   RETURN_IF_NOT_OK(ValidateVectorRatio(Name(), ratio_));
54   // max_attempts
55   if (max_attempts_ < 1) {
56     std::string err_msg =
57       Name() + ": max_attempts must be greater than or equal to 1, got: " + std::to_string(max_attempts_);
58     MS_LOG(ERROR) << err_msg;
59     RETURN_STATUS_SYNTAX_ERROR(err_msg);
60   }
61   // interpolation
62   if (interpolation_ != InterpolationMode::kLinear && interpolation_ != InterpolationMode::kNearestNeighbour &&
63       interpolation_ != InterpolationMode::kCubic && interpolation_ != InterpolationMode::kArea &&
64       interpolation_ != InterpolationMode::kCubicPil) {
65     std::string err_msg = "RandomResizedCrop: Invalid InterpolationMode, check input value of enum.";
66     MS_LOG(ERROR) << err_msg;
67     RETURN_STATUS_SYNTAX_ERROR(err_msg);
68   }
69   return Status::OK();
70 }
71 
Build()72 std::shared_ptr<TensorOp> RandomResizedCropOperation::Build() {
73   constexpr size_t dimension_zero = 0;
74   constexpr size_t dimension_one = 1;
75   constexpr size_t size_two = 2;
76 
77   int32_t height = size_[dimension_zero];
78   int32_t width = size_[dimension_zero];
79   // User specified the width value.
80   if (size_.size() == size_two) {
81     width = size_[dimension_one];
82   }
83   std::shared_ptr<RandomCropAndResizeOp> tensor_op = std::make_shared<RandomCropAndResizeOp>(
84     height, width, scale_[dimension_zero], scale_[dimension_one], ratio_[dimension_zero], ratio_[dimension_one],
85     interpolation_, max_attempts_);
86   return tensor_op;
87 }
88 
to_json(nlohmann::json * out_json)89 Status RandomResizedCropOperation::to_json(nlohmann::json *out_json) {
90   nlohmann::json args;
91   args["size"] = size_;
92   args["scale"] = scale_;
93   args["ratio"] = ratio_;
94   args["interpolation"] = interpolation_;
95   args["max_attempts"] = max_attempts_;
96   *out_json = args;
97   return Status::OK();
98 }
99 
from_json(nlohmann::json op_params,std::shared_ptr<TensorOperation> * operation)100 Status RandomResizedCropOperation::from_json(nlohmann::json op_params, std::shared_ptr<TensorOperation> *operation) {
101   CHECK_FAIL_RETURN_UNEXPECTED(op_params.find("size") != op_params.end(), "Failed to find size");
102   CHECK_FAIL_RETURN_UNEXPECTED(op_params.find("scale") != op_params.end(), "Failed to find scale");
103   CHECK_FAIL_RETURN_UNEXPECTED(op_params.find("ratio") != op_params.end(), "Failed to find ratio");
104   CHECK_FAIL_RETURN_UNEXPECTED(op_params.find("interpolation") != op_params.end(), "Failed to find interpolation");
105   CHECK_FAIL_RETURN_UNEXPECTED(op_params.find("max_attempts") != op_params.end(), "Failed to find max_attempts");
106   std::vector<int32_t> size = op_params["size"];
107   std::vector<float> scale = op_params["scale"];
108   std::vector<float> ratio = op_params["ratio"];
109   InterpolationMode interpolation = static_cast<InterpolationMode>(op_params["interpolation"]);
110   int32_t max_attempts = op_params["max_attempts"];
111   *operation = std::make_shared<vision::RandomResizedCropOperation>(size, scale, ratio, interpolation, max_attempts);
112   return Status::OK();
113 }
114 
115 #endif
116 }  // namespace vision
117 }  // namespace dataset
118 }  // namespace mindspore
119