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

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