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
17 #include "minddata/dataset/kernels/ir/vision/random_posterize_ir.h"
18
19 #include <algorithm>
20
21 #ifndef ENABLE_ANDROID
22 #include "minddata/dataset/kernels/image/random_posterize_op.h"
23 #endif
24 #include "minddata/dataset/util/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 LOG_AND_RETURN_STATUS_SYNTAX_ERROR(err_msg);
49 }
50 if (bit_range_[dimension_zero] < kMinimumBitValue || bit_range_[dimension_zero] > kMaximumBitValue) {
51 std::string err_msg =
52 "RandomPosterize: min_bit value is out of range [1-8]: " + std::to_string(bit_range_[dimension_zero]);
53 LOG_AND_RETURN_STATUS_SYNTAX_ERROR(err_msg);
54 }
55 if (bit_range_[dimension_one] < kMinimumBitValue || bit_range_[dimension_one] > kMaximumBitValue) {
56 std::string err_msg =
57 "RandomPosterize: max_bit value is out of range [1-8]: " + std::to_string(bit_range_[dimension_one]);
58 LOG_AND_RETURN_STATUS_SYNTAX_ERROR(err_msg);
59 }
60 if (bit_range_[dimension_one] < bit_range_[dimension_zero]) {
61 std::string err_msg =
62 "RandomPosterize: max_bit value is less than min_bit: max =" + std::to_string(bit_range_[dimension_one]) +
63 ", min = " + std::to_string(bit_range_[dimension_zero]);
64 LOG_AND_RETURN_STATUS_SYNTAX_ERROR(err_msg);
65 }
66 return Status::OK();
67 }
68
Build()69 std::shared_ptr<TensorOp> RandomPosterizeOperation::Build() {
70 std::shared_ptr<RandomPosterizeOp> tensor_op = std::make_shared<RandomPosterizeOp>(bit_range_);
71 return tensor_op;
72 }
73
to_json(nlohmann::json * out_json)74 Status RandomPosterizeOperation::to_json(nlohmann::json *out_json) {
75 RETURN_UNEXPECTED_IF_NULL(out_json);
76 (*out_json)["bits"] = bit_range_;
77 return Status::OK();
78 }
79
from_json(nlohmann::json op_params,std::shared_ptr<TensorOperation> * operation)80 Status RandomPosterizeOperation::from_json(nlohmann::json op_params, std::shared_ptr<TensorOperation> *operation) {
81 RETURN_UNEXPECTED_IF_NULL(operation);
82 RETURN_IF_NOT_OK(ValidateParamInJson(op_params, "bits", kRandomPosterizeOperation));
83 std::vector<uint8_t> bit_range = op_params["bits"];
84 *operation = std::make_shared<vision::RandomPosterizeOperation>(bit_range);
85 return Status::OK();
86 }
87 #endif
88 } // namespace vision
89 } // namespace dataset
90 } // namespace mindspore
91