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/softdvpp_decode_random_crop_resize_jpeg_ir.h"
19
20 #ifndef ENABLE_ANDROID
21 #include "minddata/dataset/kernels/image/soft_dvpp/soft_dvpp_decode_random_crop_resize_jpeg_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 // SoftDvppDecodeRandomCropResizeJpegOperation
SoftDvppDecodeRandomCropResizeJpegOperation(const std::vector<int32_t> & size,const std::vector<float> & scale,const std::vector<float> & ratio,int32_t max_attempts)31 SoftDvppDecodeRandomCropResizeJpegOperation::SoftDvppDecodeRandomCropResizeJpegOperation(
32 const std::vector<int32_t> &size, const std::vector<float> &scale, const std::vector<float> &ratio,
33 int32_t max_attempts)
34 : size_(size), scale_(scale), ratio_(ratio), max_attempts_(max_attempts) {}
35
36 SoftDvppDecodeRandomCropResizeJpegOperation::~SoftDvppDecodeRandomCropResizeJpegOperation() = default;
37
Name() const38 std::string SoftDvppDecodeRandomCropResizeJpegOperation::Name() const {
39 return kSoftDvppDecodeRandomCropResizeJpegOperation;
40 }
41
ValidateParams()42 Status SoftDvppDecodeRandomCropResizeJpegOperation::ValidateParams() {
43 // size
44 RETURN_IF_NOT_OK(ValidateVectorSize("SoftDvppDecodeRandomCropResizeJpeg", size_));
45 constexpr int32_t value_one = 1;
46 constexpr int32_t value_two = 2;
47 for (size_t i = 0; i < size_.size(); i++) {
48 if (size_[i] % value_two == value_one) {
49 std::string err_msg = "SoftDvppDecodeRandomCropResizeJpeg: size[" + std::to_string(i) +
50 "] must be even values, got: " + std::to_string(size_[i]);
51 MS_LOG(ERROR) << err_msg;
52 RETURN_STATUS_SYNTAX_ERROR(err_msg);
53 }
54 }
55 // scale
56 RETURN_IF_NOT_OK(ValidateVectorScale("SoftDvppDecodeRandomCropResizeJpeg", scale_));
57 // ratio
58 RETURN_IF_NOT_OK(ValidateVectorRatio("SoftDvppDecodeRandomCropResizeJpeg", ratio_));
59 // max_attempts
60 if (max_attempts_ < 1) {
61 std::string err_msg = "SoftDvppDecodeRandomCropResizeJpeg: max_attempts must be greater than or equal to 1, got: " +
62 std::to_string(max_attempts_);
63 MS_LOG(ERROR) << err_msg;
64 RETURN_STATUS_SYNTAX_ERROR(err_msg);
65 }
66 return Status::OK();
67 }
68
Build()69 std::shared_ptr<TensorOp> SoftDvppDecodeRandomCropResizeJpegOperation::Build() {
70 constexpr size_t dimension_zero = 0;
71 constexpr size_t dimension_one = 1;
72 constexpr size_t size_two = 2;
73
74 int32_t height = size_[dimension_zero];
75 int32_t width = size_[dimension_zero];
76 // User specified the width value.
77 if (size_.size() == size_two) {
78 width = size_[dimension_one];
79 }
80
81 auto tensor_op = std::make_shared<SoftDvppDecodeRandomCropResizeJpegOp>(height, width, scale_[dimension_zero],
82 scale_[dimension_one], ratio_[dimension_zero],
83 ratio_[dimension_one], max_attempts_);
84 return tensor_op;
85 }
86
to_json(nlohmann::json * out_json)87 Status SoftDvppDecodeRandomCropResizeJpegOperation::to_json(nlohmann::json *out_json) {
88 nlohmann::json args;
89 args["size"] = size_;
90 args["scale"] = scale_;
91 args["ratio"] = ratio_;
92 args["max_attempts"] = max_attempts_;
93 *out_json = args;
94 return Status::OK();
95 }
96
from_json(nlohmann::json op_params,std::shared_ptr<TensorOperation> * operation)97 Status SoftDvppDecodeRandomCropResizeJpegOperation::from_json(nlohmann::json op_params,
98 std::shared_ptr<TensorOperation> *operation) {
99 CHECK_FAIL_RETURN_UNEXPECTED(op_params.find("size") != op_params.end(), "Failed to find size");
100 CHECK_FAIL_RETURN_UNEXPECTED(op_params.find("scale") != op_params.end(), "Failed to find scale");
101 CHECK_FAIL_RETURN_UNEXPECTED(op_params.find("ratio") != op_params.end(), "Failed to find ratio");
102 CHECK_FAIL_RETURN_UNEXPECTED(op_params.find("max_attempts") != op_params.end(), "Failed to find max_attempts");
103 std::vector<int32_t> size = op_params["size"];
104 std::vector<float> scale = op_params["scale"];
105 std::vector<float> ratio = op_params["ratio"];
106 int32_t max_attempts = op_params["max_attempts"];
107 *operation = std::make_shared<vision::SoftDvppDecodeRandomCropResizeJpegOperation>(size, scale, ratio, max_attempts);
108 return Status::OK();
109 }
110
111 #endif
112 } // namespace vision
113 } // namespace dataset
114 } // namespace mindspore
115