• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_select_subpolicy_ir.h"
18 
19 #include <algorithm>
20 
21 #ifndef ENABLE_ANDROID
22 #include "minddata/dataset/engine/serdes.h"
23 #include "minddata/dataset/kernels/image/random_select_subpolicy_op.h"
24 #endif
25 #include "minddata/dataset/util/validators.h"
26 
27 namespace mindspore {
28 namespace dataset {
29 namespace vision {
30 #ifndef ENABLE_ANDROID
31 // RandomSelectSubpolicyOperation.
RandomSelectSubpolicyOperation(const std::vector<std::vector<std::pair<std::shared_ptr<TensorOperation>,double>>> & policy)32 RandomSelectSubpolicyOperation::RandomSelectSubpolicyOperation(
33   const std::vector<std::vector<std::pair<std::shared_ptr<TensorOperation>, double>>> &policy)
34     : TensorOperation(true), policy_(policy) {}
35 
36 RandomSelectSubpolicyOperation::~RandomSelectSubpolicyOperation() = default;
37 
Name() const38 std::string RandomSelectSubpolicyOperation::Name() const { return kRandomSelectSubpolicyOperation; }
39 
ValidateParams()40 Status RandomSelectSubpolicyOperation::ValidateParams() {
41   if (policy_.empty()) {
42     std::string err_msg = "RandomSelectSubpolicy: policy must not be empty";
43     LOG_AND_RETURN_STATUS_SYNTAX_ERROR(err_msg);
44   }
45   for (int32_t i = 0; i < policy_.size(); i++) {
46     if (policy_[i].empty()) {
47       std::string err_msg = "RandomSelectSubpolicy: policy[" + std::to_string(i) + "] must not be empty";
48       LOG_AND_RETURN_STATUS_SYNTAX_ERROR(err_msg);
49     }
50     for (int32_t j = 0; j < policy_[i].size(); j++) {
51       if (policy_[i][j].first == nullptr) {
52         std::string transform_pos = "[" + std::to_string(i) + "]" + "[" + std::to_string(j) + "]";
53         std::string err_msg = "RandomSelectSubpolicy: transform in policy" + transform_pos + " must not be null";
54         LOG_AND_RETURN_STATUS_SYNTAX_ERROR(err_msg);
55       } else {
56         RETURN_IF_NOT_OK(policy_[i][j].first->ValidateParams());
57       }
58       if (policy_[i][j].second < 0.0 || policy_[i][j].second > 1.0) {
59         std::string transform_pos = "[" + std::to_string(i) + "]" + "[" + std::to_string(j) + "]";
60         std::string err_msg = "RandomSelectSubpolicy: probability of transform in policy" + transform_pos +
61                               " must be between 0.0 and 1.0, got: " + std::to_string(policy_[i][j].second);
62         LOG_AND_RETURN_STATUS_SYNTAX_ERROR(err_msg);
63       }
64     }
65   }
66   return Status::OK();
67 }
68 
Build()69 std::shared_ptr<TensorOp> RandomSelectSubpolicyOperation::Build() {
70   std::vector<Subpolicy> policy_tensor_ops;
71   for (auto &sub_policy : policy_) {
72     Subpolicy sub_policy_tensor_ops;
73     (void)std::transform(sub_policy.begin(), sub_policy.end(), std::back_inserter(sub_policy_tensor_ops),
74                          [](const auto &op_pair) -> std::pair<std::shared_ptr<TensorOp>, double> {
75                            return std::make_pair(op_pair.first->Build(), op_pair.second);
76                          });
77     policy_tensor_ops.push_back(sub_policy_tensor_ops);
78   }
79   std::shared_ptr<RandomSelectSubpolicyOp> tensor_op = std::make_shared<RandomSelectSubpolicyOp>(policy_tensor_ops);
80   return tensor_op;
81 }
82 
to_json(nlohmann::json * out_json)83 Status RandomSelectSubpolicyOperation::to_json(nlohmann::json *out_json) {
84   RETURN_UNEXPECTED_IF_NULL(out_json);
85   auto policy_tensor_ops = nlohmann::json::array();
86   for (auto &sub_policy : policy_) {
87     auto sub_policy_tensor_ops = nlohmann::json::array();
88     for (auto &op_pair : sub_policy) {
89       nlohmann::json policy, args;
90       auto tensor_op = op_pair.first;
91       RETURN_IF_NOT_OK(tensor_op->to_json(&args));
92       policy["tensor_op"]["tensor_op_params"] = args;
93       policy["tensor_op"]["tensor_op_name"] = tensor_op->Name();
94       policy["prob"] = op_pair.second;
95       sub_policy_tensor_ops.push_back(policy);
96     }
97     policy_tensor_ops.push_back(sub_policy_tensor_ops);
98   }
99   (*out_json)["policy"] = policy_tensor_ops;
100   return Status::OK();
101 }
102 
from_json(nlohmann::json op_params,std::shared_ptr<TensorOperation> * operation)103 Status RandomSelectSubpolicyOperation::from_json(nlohmann::json op_params,
104                                                  std::shared_ptr<TensorOperation> *operation) {
105   RETURN_UNEXPECTED_IF_NULL(operation);
106   RETURN_IF_NOT_OK(ValidateParamInJson(op_params, "policy", kRandomSelectSubpolicyOperation));
107   nlohmann::json policy_json = op_params["policy"];
108   std::vector<std::vector<std::pair<std::shared_ptr<TensorOperation>, double>>> policy;
109   std::vector<std::pair<std::shared_ptr<TensorOperation>, double>> policy_items;
110   for (const nlohmann::json &item : policy_json) {
111     for (nlohmann::json item_pair : item) {
112       RETURN_IF_NOT_OK(ValidateParamInJson(item_pair, "prob", kRandomSelectSubpolicyOperation));
113       RETURN_IF_NOT_OK(ValidateParamInJson(item_pair, "tensor_op", kRandomSelectSubpolicyOperation));
114       std::vector<std::shared_ptr<TensorOperation>> operations;
115       std::pair<std::shared_ptr<TensorOperation>, double> policy_pair;
116       nlohmann::json tensor_op_json;
117       double prob = item_pair["prob"];
118       tensor_op_json.push_back(item_pair["tensor_op"]);
119       RETURN_IF_NOT_OK(Serdes::ConstructTensorOps(tensor_op_json, &operations));
120       CHECK_FAIL_RETURN_UNEXPECTED(operations.size() == 1, "There should be only 1 tensor operation");
121       policy_pair = std::make_pair(operations[0], prob);
122       policy_items.push_back(policy_pair);
123     }
124     policy.push_back(policy_items);
125   }
126   *operation = std::make_shared<vision::RandomSelectSubpolicyOperation>(policy);
127   return Status::OK();
128 }
129 #endif
130 }  // namespace vision
131 }  // namespace dataset
132 }  // namespace mindspore
133