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_crop_with_bbox_ir.h"
19
20 #ifndef ENABLE_ANDROID
21 #include "minddata/dataset/kernels/image/random_crop_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 // RandomCropWithBBoxOperation
RandomCropWithBBoxOperation(const std::vector<int32_t> & size,const std::vector<int32_t> & padding,bool pad_if_needed,const std::vector<uint8_t> & fill_value,BorderType padding_mode)31 RandomCropWithBBoxOperation::RandomCropWithBBoxOperation(const std::vector<int32_t> &size,
32 const std::vector<int32_t> &padding, bool pad_if_needed,
33 const std::vector<uint8_t> &fill_value,
34 BorderType padding_mode)
35 : TensorOperation(true),
36 size_(size),
37 padding_(padding),
38 pad_if_needed_(pad_if_needed),
39 fill_value_(fill_value),
40 padding_mode_(padding_mode) {}
41
42 RandomCropWithBBoxOperation::~RandomCropWithBBoxOperation() = default;
43
Name() const44 std::string RandomCropWithBBoxOperation::Name() const { return kRandomCropWithBBoxOperation; }
45
ValidateParams()46 Status RandomCropWithBBoxOperation::ValidateParams() {
47 // size
48 RETURN_IF_NOT_OK(ValidateVectorSize("RandomCropWithBBox", size_));
49 // padding
50 RETURN_IF_NOT_OK(ValidateVectorPadding("RandomCropWithBBox", padding_));
51 // fill_value
52 RETURN_IF_NOT_OK(ValidateVectorFillvalue("RandomCropWithBBox", fill_value_));
53 // padding_mode
54 if (padding_mode_ != BorderType::kConstant && padding_mode_ != BorderType::kEdge &&
55 padding_mode_ != BorderType::kReflect && padding_mode_ != BorderType::kSymmetric) {
56 std::string err_msg = "RandomCropWithBBox: Invalid BorderType, check input value of enum.";
57 MS_LOG(ERROR) << err_msg;
58 RETURN_STATUS_SYNTAX_ERROR(err_msg);
59 }
60 return Status::OK();
61 }
62
Build()63 std::shared_ptr<TensorOp> RandomCropWithBBoxOperation::Build() {
64 constexpr size_t dimension_zero = 0;
65 constexpr size_t dimension_one = 1;
66 constexpr size_t dimension_two = 2;
67 constexpr size_t dimension_three = 3;
68 constexpr size_t size_one = 1;
69 constexpr size_t size_two = 2;
70 constexpr size_t size_three = 3;
71
72 int32_t crop_height = size_[dimension_zero];
73 int32_t crop_width = size_[dimension_zero];
74
75 // User has specified the crop_width value.
76 if (size_.size() == size_two) {
77 crop_width = size_[dimension_one];
78 }
79
80 int32_t pad_top, pad_bottom, pad_left, pad_right;
81 switch (padding_.size()) {
82 case size_one:
83 pad_left = padding_[dimension_zero];
84 pad_top = padding_[dimension_zero];
85 pad_right = padding_[dimension_zero];
86 pad_bottom = padding_[dimension_zero];
87 break;
88 case size_two:
89 pad_left = padding_[dimension_zero];
90 pad_top = padding_[dimension_zero];
91 pad_right = padding_[dimension_one];
92 pad_bottom = padding_[dimension_one];
93 break;
94 default:
95 pad_left = padding_[dimension_zero];
96 pad_top = padding_[dimension_one];
97 pad_right = padding_[dimension_two];
98 pad_bottom = padding_[dimension_three];
99 }
100
101 uint8_t fill_r, fill_g, fill_b;
102 fill_r = fill_value_[dimension_zero];
103 fill_g = fill_value_[dimension_zero];
104 fill_b = fill_value_[dimension_zero];
105
106 if (fill_value_.size() == size_three) {
107 fill_r = fill_value_[dimension_zero];
108 fill_g = fill_value_[dimension_one];
109 fill_b = fill_value_[dimension_two];
110 }
111
112 auto tensor_op =
113 std::make_shared<RandomCropWithBBoxOp>(crop_height, crop_width, pad_top, pad_bottom, pad_left, pad_right,
114 pad_if_needed_, padding_mode_, fill_r, fill_g, fill_b);
115 return tensor_op;
116 }
117
to_json(nlohmann::json * out_json)118 Status RandomCropWithBBoxOperation::to_json(nlohmann::json *out_json) {
119 nlohmann::json args;
120 args["size"] = size_;
121 args["padding"] = padding_;
122 args["pad_if_needed"] = pad_if_needed_;
123 args["fill_value"] = fill_value_;
124 args["padding_mode"] = padding_mode_;
125 *out_json = args;
126 return Status::OK();
127 }
128
from_json(nlohmann::json op_params,std::shared_ptr<TensorOperation> * operation)129 Status RandomCropWithBBoxOperation::from_json(nlohmann::json op_params, std::shared_ptr<TensorOperation> *operation) {
130 CHECK_FAIL_RETURN_UNEXPECTED(op_params.find("size") != op_params.end(), "Failed to find size");
131 CHECK_FAIL_RETURN_UNEXPECTED(op_params.find("padding") != op_params.end(), "Failed to find padding");
132 CHECK_FAIL_RETURN_UNEXPECTED(op_params.find("pad_if_needed") != op_params.end(), "Failed to find pad_if_needed");
133 CHECK_FAIL_RETURN_UNEXPECTED(op_params.find("fill_value") != op_params.end(), "Failed to find fill_value");
134 CHECK_FAIL_RETURN_UNEXPECTED(op_params.find("padding_mode") != op_params.end(), "Failed to find padding_mode");
135 std::vector<int32_t> size = op_params["size"];
136 std::vector<int32_t> padding = op_params["padding"];
137 bool pad_if_needed = op_params["pad_if_needed"];
138 std::vector<uint8_t> fill_value = op_params["fill_value"];
139 BorderType padding_mode = static_cast<BorderType>(op_params["padding_mode"]);
140 *operation =
141 std::make_shared<vision::RandomCropWithBBoxOperation>(size, padding, pad_if_needed, fill_value, padding_mode);
142 return Status::OK();
143 }
144
145 #endif
146 } // namespace vision
147 } // namespace dataset
148 } // namespace mindspore
149