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_posterize_ir.h"
19
20 #ifndef ENABLE_ANDROID
21 #include "minddata/dataset/kernels/image/random_posterize_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 // RandomPosterizeOperation
RandomPosterizeOperation(const std::vector<uint8_t> & bit_range)31 RandomPosterizeOperation::RandomPosterizeOperation(const std::vector<uint8_t> &bit_range)
32 : TensorOperation(true), bit_range_(bit_range) {}
33
34 RandomPosterizeOperation::~RandomPosterizeOperation() = default;
35
Name() const36 std::string RandomPosterizeOperation::Name() const { return kRandomPosterizeOperation; }
37
ValidateParams()38 Status RandomPosterizeOperation::ValidateParams() {
39 constexpr size_t dimension_zero = 0;
40 constexpr size_t dimension_one = 1;
41 constexpr size_t size_two = 2;
42 constexpr uint8_t kMinimumBitValue = 1;
43 constexpr uint8_t kMaximumBitValue = 8;
44
45 if (bit_range_.size() != size_two) {
46 std::string err_msg =
47 "RandomPosterize: bit_range needs to be of size 2 but is of size: " + std::to_string(bit_range_.size());
48 MS_LOG(ERROR) << err_msg;
49 RETURN_STATUS_SYNTAX_ERROR(err_msg);
50 }
51 if (bit_range_[dimension_zero] < kMinimumBitValue || bit_range_[dimension_zero] > kMaximumBitValue) {
52 std::string err_msg =
53 "RandomPosterize: min_bit value is out of range [1-8]: " + std::to_string(bit_range_[dimension_zero]);
54 MS_LOG(ERROR) << err_msg;
55 RETURN_STATUS_SYNTAX_ERROR(err_msg);
56 }
57 if (bit_range_[dimension_one] < kMinimumBitValue || bit_range_[dimension_one] > kMaximumBitValue) {
58 std::string err_msg =
59 "RandomPosterize: max_bit value is out of range [1-8]: " + std::to_string(bit_range_[dimension_one]);
60 MS_LOG(ERROR) << err_msg;
61 RETURN_STATUS_SYNTAX_ERROR(err_msg);
62 }
63 if (bit_range_[dimension_one] < bit_range_[dimension_zero]) {
64 std::string err_msg =
65 "RandomPosterize: max_bit value is less than min_bit: max =" + std::to_string(bit_range_[dimension_one]) +
66 ", min = " + std::to_string(bit_range_[dimension_zero]);
67 MS_LOG(ERROR) << err_msg;
68 RETURN_STATUS_SYNTAX_ERROR(err_msg);
69 }
70 return Status::OK();
71 }
72
Build()73 std::shared_ptr<TensorOp> RandomPosterizeOperation::Build() {
74 std::shared_ptr<RandomPosterizeOp> tensor_op = std::make_shared<RandomPosterizeOp>(bit_range_);
75 return tensor_op;
76 }
77
to_json(nlohmann::json * out_json)78 Status RandomPosterizeOperation::to_json(nlohmann::json *out_json) {
79 (*out_json)["bits"] = bit_range_;
80 return Status::OK();
81 }
82
from_json(nlohmann::json op_params,std::shared_ptr<TensorOperation> * operation)83 Status RandomPosterizeOperation::from_json(nlohmann::json op_params, std::shared_ptr<TensorOperation> *operation) {
84 CHECK_FAIL_RETURN_UNEXPECTED(op_params.find("bits") != op_params.end(), "Failed to find bits");
85 std::vector<uint8_t> bit_range = op_params["bits"];
86 *operation = std::make_shared<vision::RandomPosterizeOperation>(bit_range);
87 return Status::OK();
88 }
89
90 #endif
91 } // namespace vision
92 } // namespace dataset
93 } // namespace mindspore
94