1 /** 2 * Copyright 2020-2022 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 #ifndef MINDSPORE_CCSRC_MINDDATA_DATASET_ENGINE_IR_DATASETOPS_SOURCE_SAMPLERS_RANDOM_SAMPLER_IR_H_ 18 #define MINDSPORE_CCSRC_MINDDATA_DATASET_ENGINE_IR_DATASETOPS_SOURCE_SAMPLERS_RANDOM_SAMPLER_IR_H_ 19 20 #include <memory> 21 #include <string> 22 #include <nlohmann/json.hpp> 23 24 #include "minddata/dataset/engine/ir/datasetops/source/samplers/samplers_ir.h" 25 #include "include/api/status.h" 26 #ifndef ENABLE_ANDROID 27 #include "minddata/mindrecord/include/shard_operator.h" 28 #endif 29 30 namespace mindspore { 31 namespace dataset { 32 // Internal Sampler class forward declaration 33 class SamplerRT; 34 35 class RandomSamplerObj : public SamplerObj { 36 public: 37 RandomSamplerObj(bool replacement, int64_t num_samples, bool reshuffle_each_epoch = true); 38 39 ~RandomSamplerObj() override; 40 41 Status SamplerBuild(std::shared_ptr<SamplerRT> *sampler) override; 42 43 std::shared_ptr<SamplerObj> SamplerCopy() override; 44 45 #ifndef ENABLE_ANDROID 46 std::shared_ptr<mindrecord::ShardOperator> BuildForMindDataset() override; 47 #endif 48 49 /// \brief Get the arguments of node 50 /// \param[out] out_json JSON string of all attributes 51 /// \return Status of the function 52 Status to_json(nlohmann::json *const out_json) override; 53 54 #ifndef ENABLE_ANDROID 55 /// \brief Function for read sampler from JSON object 56 /// \param[in] json_obj JSON object to be read 57 /// \param[in] num_samples number of sample in the sampler 58 /// \param[out] sampler Sampler constructed from parameters in JSON object 59 /// \return Status of the function 60 static Status from_json(nlohmann::json json_obj, int64_t num_samples, std::shared_ptr<SamplerObj> *sampler); 61 #endif 62 63 Status ValidateParams() override; 64 65 private: 66 bool replacement_; 67 int64_t num_samples_; 68 bool reshuffle_each_epoch_; 69 }; 70 } // namespace dataset 71 } // namespace mindspore 72 #endif // MINDSPORE_CCSRC_MINDDATA_DATASET_ENGINE_IR_DATASETOPS_SOURCE_SAMPLERS_RANDOM_SAMPLER_IR_H_ 73