• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright 2020-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 #include "minddata/dataset/kernels/data/random_apply_op.h"
17 
18 #include "minddata/dataset/core/tensor.h"
19 #include "minddata/dataset/kernels/tensor_op.h"
20 #include "minddata/dataset/util/status.h"
21 
22 namespace mindspore {
23 namespace dataset {
RandomApplyOp(const std::vector<std::shared_ptr<TensorOp>> & ops,double prob)24 RandomApplyOp::RandomApplyOp(const std::vector<std::shared_ptr<TensorOp>> &ops, double prob)
25     : prob_(prob), rand_double_(0.0, 1.0) {
26   compose_ = std::make_unique<ComposeOp>(ops);
27 }
28 
NumOutput()29 uint32_t RandomApplyOp::NumOutput() {
30   if (compose_->NumOutput() != NumInput()) {
31     MS_LOG(WARNING) << "NumOutput!=NumInput (randomApply would randomly affect number of outputs).";
32     return 0;
33   }
34   return compose_->NumOutput();
35 }
36 
OutputShape(const std::vector<TensorShape> & inputs,std::vector<TensorShape> & outputs)37 Status RandomApplyOp::OutputShape(const std::vector<TensorShape> &inputs, std::vector<TensorShape> &outputs) {
38   RETURN_IF_NOT_OK(compose_->OutputShape(inputs, outputs));
39   // randomApply either runs all ops or do nothing. If the two methods don't give the same result. return unknown shape.
40   if (inputs != outputs) {  // when RandomApply is not applied, input should be the same as output
41     outputs.clear();
42     outputs.resize(NumOutput(), TensorShape::CreateUnknownRankShape());
43   }
44   return Status::OK();
45 }
46 
OutputType(const std::vector<DataType> & inputs,std::vector<DataType> & outputs)47 Status RandomApplyOp::OutputType(const std::vector<DataType> &inputs, std::vector<DataType> &outputs) {
48   RETURN_IF_NOT_OK(compose_->OutputType(inputs, outputs));
49   if (inputs != outputs) {  // when RandomApply is not applied, input should be the same as output
50     outputs.clear();
51     outputs.resize(NumOutput(), DataType(DataType::DE_UNKNOWN));
52   }
53   return Status::OK();
54 }
55 
Compute(const TensorRow & input,TensorRow * output)56 Status RandomApplyOp::Compute(const TensorRow &input, TensorRow *output) {
57   IO_CHECK_VECTOR(input, output);
58   if (rand_double_(random_generator_) <= prob_) {
59     RETURN_IF_NOT_OK(compose_->Compute(input, output));
60   } else {
61     *output = input;  // copy over the tensors
62   }
63   return Status::OK();
64 }
65 }  // namespace dataset
66 }  // namespace mindspore
67