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 7.9 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. /**
  2. * Copyright 2020 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/samplers.h"
  17. #include "minddata/dataset/engine/datasetops/source/sampler/sampler.h"
  18. #include "minddata/dataset/engine/datasetops/source/sampler/distributed_sampler.h"
  19. #include "minddata/dataset/engine/datasetops/source/sampler/random_sampler.h"
  20. #include "minddata/dataset/engine/datasetops/source/sampler/sequential_sampler.h"
  21. #include "minddata/dataset/engine/datasetops/source/sampler/subset_random_sampler.h"
  22. #include "minddata/dataset/engine/datasetops/source/sampler/weighted_random_sampler.h"
  23. #include "minddata/dataset/engine/datasetops/source/sampler/pk_sampler.h"
  24. namespace mindspore {
  25. namespace dataset {
  26. namespace api {
  27. SamplerObj::SamplerObj() {}
  28. /// Function to create a Distributed Sampler.
  29. std::shared_ptr<DistributedSamplerObj> DistributedSampler(int64_t num_shards, int64_t shard_id, bool shuffle,
  30. int64_t num_samples, uint32_t seed, bool even_dist) {
  31. auto sampler = std::make_shared<DistributedSamplerObj>(num_shards, shard_id, shuffle, num_samples, seed, even_dist);
  32. // Input validation
  33. if (!sampler->ValidateParams()) {
  34. return nullptr;
  35. }
  36. return sampler;
  37. }
  38. /// Function to create a PK Sampler.
  39. std::shared_ptr<PKSamplerObj> PKSampler(int64_t num_val, bool shuffle, int64_t num_samples) {
  40. auto sampler = std::make_shared<PKSamplerObj>(num_val, shuffle, num_samples);
  41. // Input validation
  42. if (!sampler->ValidateParams()) {
  43. return nullptr;
  44. }
  45. return sampler;
  46. }
  47. /// Function to create a Random Sampler.
  48. std::shared_ptr<RandomSamplerObj> RandomSampler(bool replacement, int64_t num_samples) {
  49. auto sampler = std::make_shared<RandomSamplerObj>(replacement, num_samples);
  50. // Input validation
  51. if (!sampler->ValidateParams()) {
  52. return nullptr;
  53. }
  54. return sampler;
  55. }
  56. /// Function to create a Sequential Sampler.
  57. std::shared_ptr<SequentialSamplerObj> SequentialSampler(int64_t start_index, int64_t num_samples) {
  58. auto sampler = std::make_shared<SequentialSamplerObj>(start_index, num_samples);
  59. // Input validation
  60. if (!sampler->ValidateParams()) {
  61. return nullptr;
  62. }
  63. return sampler;
  64. }
  65. /// Function to create a Subset Random Sampler.
  66. std::shared_ptr<SubsetRandomSamplerObj> SubsetRandomSampler(std::vector<int64_t> indices, int64_t num_samples) {
  67. auto sampler = std::make_shared<SubsetRandomSamplerObj>(std::move(indices), num_samples);
  68. // Input validation
  69. if (!sampler->ValidateParams()) {
  70. return nullptr;
  71. }
  72. return sampler;
  73. }
  74. /// Function to create a Weighted Random Sampler.
  75. std::shared_ptr<WeightedRandomSamplerObj> WeightedRandomSampler(std::vector<double> weights, int64_t num_samples,
  76. bool replacement) {
  77. auto sampler = std::make_shared<WeightedRandomSamplerObj>(std::move(weights), num_samples, replacement);
  78. // Input validation
  79. if (!sampler->ValidateParams()) {
  80. return nullptr;
  81. }
  82. return sampler;
  83. }
  84. /* ####################################### Derived Sampler classes ################################# */
  85. // DistributedSampler
  86. DistributedSamplerObj::DistributedSamplerObj(int64_t num_shards, int64_t shard_id, bool shuffle, int64_t num_samples,
  87. uint32_t seed, bool even_dist)
  88. : num_shards_(num_shards),
  89. shard_id_(shard_id),
  90. shuffle_(shuffle),
  91. num_samples_(num_samples),
  92. seed_(seed),
  93. even_dist_(even_dist) {}
  94. bool DistributedSamplerObj::ValidateParams() {
  95. if (num_shards_ <= 0) {
  96. MS_LOG(ERROR) << "DistributedSampler: invalid num_shards: " << num_shards_;
  97. return false;
  98. }
  99. if (shard_id_ < 0 || shard_id_ >= num_shards_) {
  100. MS_LOG(ERROR) << "DistributedSampler: invalid input, shard_id: " << shard_id_ << ", num_shards: " << num_shards_;
  101. return false;
  102. }
  103. if (num_samples_ < 0) {
  104. MS_LOG(ERROR) << "DistributedSampler: invalid num_samples: " << num_samples_;
  105. return false;
  106. }
  107. return true;
  108. }
  109. std::shared_ptr<Sampler> DistributedSamplerObj::Build() {
  110. return std::make_shared<dataset::DistributedSampler>(num_samples_, num_shards_, shard_id_, shuffle_, seed_,
  111. even_dist_);
  112. }
  113. // PKSampler
  114. PKSamplerObj::PKSamplerObj(int64_t num_val, bool shuffle, int64_t num_samples)
  115. : num_val_(num_val), shuffle_(shuffle), num_samples_(num_samples) {}
  116. bool PKSamplerObj::ValidateParams() {
  117. if (num_val_ <= 0) {
  118. MS_LOG(ERROR) << "PKSampler: invalid num_val: " << num_val_;
  119. return false;
  120. }
  121. if (num_samples_ < 0) {
  122. MS_LOG(ERROR) << "PKSampler: invalid num_samples: " << num_samples_;
  123. return false;
  124. }
  125. return true;
  126. }
  127. std::shared_ptr<Sampler> PKSamplerObj::Build() {
  128. return std::make_shared<dataset::PKSampler>(num_samples_, num_val_, shuffle_);
  129. }
  130. // RandomSampler
  131. RandomSamplerObj::RandomSamplerObj(bool replacement, int64_t num_samples)
  132. : replacement_(replacement), num_samples_(num_samples) {}
  133. bool RandomSamplerObj::ValidateParams() {
  134. if (num_samples_ < 0) {
  135. MS_LOG(ERROR) << "RandomSampler: invalid num_samples: " << num_samples_;
  136. return false;
  137. }
  138. return true;
  139. }
  140. std::shared_ptr<Sampler> RandomSamplerObj::Build() {
  141. bool reshuffle_each_epoch = true;
  142. auto sampler = std::make_shared<dataset::RandomSampler>(num_samples_, replacement_, reshuffle_each_epoch);
  143. return sampler;
  144. }
  145. // SequentialSampler
  146. SequentialSamplerObj::SequentialSamplerObj(int64_t start_index, int64_t num_samples)
  147. : start_index_(start_index), num_samples_(num_samples) {}
  148. bool SequentialSamplerObj::ValidateParams() {
  149. if (num_samples_ < 0) {
  150. MS_LOG(ERROR) << "SequentialSampler: invalid num_samples: " << num_samples_;
  151. return false;
  152. }
  153. if (start_index_ < 0) {
  154. MS_LOG(ERROR) << "SequentialSampler: invalid start_index: " << start_index_;
  155. return false;
  156. }
  157. return true;
  158. }
  159. std::shared_ptr<Sampler> SequentialSamplerObj::Build() {
  160. auto sampler = std::make_shared<dataset::SequentialSampler>(num_samples_, start_index_);
  161. return sampler;
  162. }
  163. // SubsetRandomSampler
  164. SubsetRandomSamplerObj::SubsetRandomSamplerObj(std::vector<int64_t> indices, int64_t num_samples)
  165. : indices_(std::move(indices)), num_samples_(num_samples) {}
  166. bool SubsetRandomSamplerObj::ValidateParams() {
  167. if (num_samples_ < 0) {
  168. MS_LOG(ERROR) << "SubsetRandomSampler: invalid num_samples: " << num_samples_;
  169. return false;
  170. }
  171. return true;
  172. }
  173. std::shared_ptr<Sampler> SubsetRandomSamplerObj::Build() {
  174. auto sampler = std::make_shared<dataset::SubsetRandomSampler>(num_samples_, indices_);
  175. return sampler;
  176. }
  177. // WeightedRandomSampler
  178. WeightedRandomSamplerObj::WeightedRandomSamplerObj(std::vector<double> weights, int64_t num_samples, bool replacement)
  179. : weights_(std::move(weights)), num_samples_(num_samples), replacement_(replacement) {}
  180. bool WeightedRandomSamplerObj::ValidateParams() {
  181. if (num_samples_ < 0) {
  182. MS_LOG(ERROR) << "WeightedRandomSampler: invalid num_samples: " << num_samples_;
  183. return false;
  184. }
  185. return true;
  186. }
  187. std::shared_ptr<Sampler> WeightedRandomSamplerObj::Build() {
  188. auto sampler = std::make_shared<dataset::WeightedRandomSampler>(num_samples_, weights_, replacement_);
  189. return sampler;
  190. }
  191. } // namespace api
  192. } // namespace dataset
  193. } // namespace mindspore