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