You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

samplers.cc 5.5 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. #include "minddata/dataset/include/dataset/samplers.h"
  17. #include <utility>
  18. #include "minddata/dataset/engine/ir/datasetops/source/samplers/distributed_sampler_ir.h"
  19. #include "minddata/dataset/engine/ir/datasetops/source/samplers/pk_sampler_ir.h"
  20. #include "minddata/dataset/engine/ir/datasetops/source/samplers/random_sampler_ir.h"
  21. #include "minddata/dataset/engine/ir/datasetops/source/samplers/samplers_ir.h"
  22. #include "minddata/dataset/engine/ir/datasetops/source/samplers/sequential_sampler_ir.h"
  23. #include "minddata/dataset/engine/ir/datasetops/source/samplers/subset_random_sampler_ir.h"
  24. #include "minddata/dataset/engine/ir/datasetops/source/samplers/subset_sampler_ir.h"
  25. #include "minddata/dataset/engine/ir/datasetops/source/samplers/weighted_random_sampler_ir.h"
  26. namespace mindspore {
  27. namespace dataset {
  28. Status Sampler::BuildChildren(std::shared_ptr<SamplerObj> *const sampler) const {
  29. for (const auto &child : children_) {
  30. std::shared_ptr<SamplerObj> sampler_obj = child->Parse();
  31. RETURN_IF_NOT_OK((*sampler)->AddChildSampler(sampler_obj));
  32. }
  33. return Status::OK();
  34. }
  35. // DistributedSampler
  36. DistributedSampler::DistributedSampler(int64_t num_shards, int64_t shard_id, bool shuffle, int64_t num_samples,
  37. uint32_t seed, int64_t offset, bool even_dist)
  38. : num_shards_(num_shards),
  39. shard_id_(shard_id),
  40. shuffle_(shuffle),
  41. num_samples_(num_samples),
  42. seed_(seed),
  43. offset_(offset),
  44. even_dist_(even_dist) {}
  45. std::shared_ptr<SamplerObj> DistributedSampler::Parse() const {
  46. std::shared_ptr<SamplerObj> output =
  47. std::make_shared<DistributedSamplerObj>(num_shards_, shard_id_, shuffle_, num_samples_, seed_, offset_, even_dist_);
  48. Status s = BuildChildren(&output);
  49. if (s.IsError()) {
  50. MS_LOG(ERROR) << "[Internal ERROR] Error in Parse. Message: " << s;
  51. }
  52. return output;
  53. }
  54. // PKSampler
  55. PKSampler::PKSampler(int64_t num_val, bool shuffle, int64_t num_samples)
  56. : num_val_(num_val), shuffle_(shuffle), num_samples_(num_samples) {}
  57. std::shared_ptr<SamplerObj> PKSampler::Parse() const {
  58. std::shared_ptr<SamplerObj> output = std::make_shared<PKSamplerObj>(num_val_, shuffle_, num_samples_);
  59. Status s = BuildChildren(&output);
  60. if (s.IsError()) {
  61. MS_LOG(ERROR) << "[Internal ERROR] Error in Parse. Message: " << s;
  62. }
  63. return output;
  64. }
  65. // RandomSampler
  66. RandomSampler::RandomSampler(bool replacement, int64_t num_samples)
  67. : replacement_(replacement), num_samples_(num_samples) {}
  68. std::shared_ptr<SamplerObj> RandomSampler::Parse() const {
  69. std::shared_ptr<SamplerObj> output = std::make_shared<RandomSamplerObj>(replacement_, num_samples_);
  70. Status s = BuildChildren(&output);
  71. if (s.IsError()) {
  72. MS_LOG(ERROR) << "[Internal ERROR] Error in Parse. Message: " << s;
  73. }
  74. return output;
  75. }
  76. // SequentialSampler
  77. SequentialSampler::SequentialSampler(int64_t start_index, int64_t num_samples)
  78. : start_index_(start_index), num_samples_(num_samples) {}
  79. std::shared_ptr<SamplerObj> SequentialSampler::Parse() const {
  80. std::shared_ptr<SamplerObj> output = std::make_shared<SequentialSamplerObj>(start_index_, num_samples_);
  81. Status s = BuildChildren(&output);
  82. if (s.IsError()) {
  83. MS_LOG(ERROR) << "[Internal ERROR] Error in Parse. Message: " << s;
  84. }
  85. return output;
  86. }
  87. // SubsetSampler
  88. SubsetSampler::SubsetSampler(const std::vector<int64_t> &indices, int64_t num_samples)
  89. : indices_(indices), num_samples_(num_samples) {}
  90. std::shared_ptr<SamplerObj> SubsetSampler::Parse() const {
  91. std::shared_ptr<SamplerObj> output = std::make_shared<SubsetSamplerObj>(indices_, num_samples_);
  92. Status s = BuildChildren(&output);
  93. if (s.IsError()) {
  94. MS_LOG(ERROR) << "[Internal ERROR] Error in Parse. Message: " << s;
  95. }
  96. return output;
  97. }
  98. // SubsetRandomSampler
  99. SubsetRandomSampler::SubsetRandomSampler(const std::vector<int64_t> &indices, int64_t num_samples)
  100. : SubsetSampler(indices, num_samples) {}
  101. std::shared_ptr<SamplerObj> SubsetRandomSampler::Parse() const {
  102. std::shared_ptr<SamplerObj> output = std::make_shared<SubsetRandomSamplerObj>(indices_, num_samples_);
  103. Status s = BuildChildren(&output);
  104. if (s.IsError()) {
  105. MS_LOG(ERROR) << "[Internal ERROR] Error in Parse. Message: " << s;
  106. }
  107. return output;
  108. }
  109. // WeightedRandomSampler
  110. WeightedRandomSampler::WeightedRandomSampler(const std::vector<double> &weights, int64_t num_samples, bool replacement)
  111. : weights_(weights), num_samples_(num_samples), replacement_(replacement) {}
  112. std::shared_ptr<SamplerObj> WeightedRandomSampler::Parse() const {
  113. std::shared_ptr<SamplerObj> output = std::make_shared<WeightedRandomSamplerObj>(weights_, num_samples_, replacement_);
  114. Status s = BuildChildren(&output);
  115. if (s.IsError()) {
  116. MS_LOG(ERROR) << "[Internal ERROR] Error in Parse. Message: " << s;
  117. }
  118. return output;
  119. }
  120. } // namespace dataset
  121. } // namespace mindspore