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.

ir_sampler_test.cc 9.9 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. /**
  2. * Copyright 2021 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 "common/common.h"
  17. #include "minddata/dataset/core/tensor.h"
  18. #include "minddata/dataset/engine/datasetops/source/sampler/sampler.h"
  19. #include "minddata/dataset/engine/ir/datasetops/source/image_folder_node.h"
  20. #include "minddata/dataset/engine/ir/datasetops/source/samplers/distributed_sampler_ir.h"
  21. #include "minddata/dataset/engine/ir/datasetops/source/samplers/pk_sampler_ir.h"
  22. #include "minddata/dataset/engine/ir/datasetops/source/samplers/prebuilt_sampler_ir.h"
  23. #include "minddata/dataset/engine/ir/datasetops/source/samplers/random_sampler_ir.h"
  24. #include "minddata/dataset/engine/ir/datasetops/source/samplers/samplers_ir.h"
  25. #include "minddata/dataset/engine/ir/datasetops/source/samplers/sequential_sampler_ir.h"
  26. #include "minddata/dataset/engine/ir/datasetops/source/samplers/skip_first_epoch_sampler_ir.h"
  27. #include "minddata/dataset/engine/ir/datasetops/source/samplers/subset_random_sampler_ir.h"
  28. #include "minddata/dataset/engine/ir/datasetops/source/samplers/subset_sampler_ir.h"
  29. #include "minddata/dataset/engine/ir/datasetops/source/samplers/weighted_random_sampler_ir.h"
  30. using namespace mindspore::dataset;
  31. using mindspore::dataset::Tensor;
  32. class MindDataTestIrSampler : public UT::DatasetOpTesting {
  33. protected:
  34. };
  35. TEST_F(MindDataTestIrSampler, TestCalculateNumSamples) {
  36. int64_t num_rows = 30; // dummy variable for number of rows in the dataset
  37. std::shared_ptr<SamplerObj> sampl = std::make_shared<DistributedSamplerObj>(2, 1, false, 6, 1, -1, true);
  38. EXPECT_NE(sampl, nullptr);
  39. std::shared_ptr<SamplerRT> sampler_rt;
  40. sampl->SamplerBuild(&sampler_rt);
  41. EXPECT_EQ(sampler_rt->CalculateNumSamples(num_rows), 6);
  42. sampl = std::make_shared<PKSamplerObj>(3, false, 0);
  43. EXPECT_NE(sampl, nullptr);
  44. sampl->SamplerBuild(&sampler_rt);
  45. EXPECT_EQ(sampler_rt->CalculateNumSamples(num_rows), -1);
  46. sampl = std::make_shared<RandomSamplerObj>(false, 12);
  47. EXPECT_NE(sampl, nullptr);
  48. sampl->SamplerBuild(&sampler_rt);
  49. EXPECT_EQ(sampler_rt->CalculateNumSamples(num_rows), 12);
  50. sampl = std::make_shared<SequentialSamplerObj>(0, 10);
  51. EXPECT_NE(sampl, nullptr);
  52. sampl->SamplerBuild(&sampler_rt);
  53. EXPECT_EQ(sampler_rt->CalculateNumSamples(num_rows), 10);
  54. std::vector<double> weights = {0.9, 0.8, 0.68, 0.7, 0.71, 0.6, 0.5, 0.4, 0.3, 0.5, 0.2, 0.1};
  55. sampl = std::make_shared<WeightedRandomSamplerObj>(weights, 12);
  56. EXPECT_NE(sampl, nullptr);
  57. sampl->SamplerBuild(&sampler_rt);
  58. EXPECT_EQ(sampler_rt->CalculateNumSamples(num_rows), 12);
  59. std::vector<int64_t> indices = {1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21};
  60. sampl = std::make_shared<SubsetRandomSamplerObj>(indices, 11);
  61. EXPECT_NE(sampl, nullptr);
  62. sampl->SamplerBuild(&sampler_rt);
  63. EXPECT_EQ(sampler_rt->CalculateNumSamples(num_rows), 11);
  64. sampl = std::make_shared<SkipFirstEpochSamplerObj>(0);
  65. EXPECT_NE(sampl, nullptr);
  66. sampl->SamplerBuild(&sampler_rt);
  67. EXPECT_EQ(sampler_rt->CalculateNumSamples(num_rows), -1);
  68. // Testing chains
  69. // Parent and child have num_samples
  70. std::shared_ptr<SamplerObj> sampl1 = std::make_shared<WeightedRandomSamplerObj>(weights, 12);
  71. EXPECT_NE(sampl1, nullptr);
  72. std::shared_ptr<SamplerRT> sampler_rt1;
  73. sampl1->SamplerBuild(&sampler_rt1);
  74. std::shared_ptr<SamplerObj> sampl2 = std::make_shared<SequentialSamplerObj>(0, 10);
  75. EXPECT_NE(sampl2, nullptr);
  76. std::shared_ptr<SamplerRT> sampler_rt2;
  77. sampl2->SamplerBuild(&sampler_rt2);
  78. sampler_rt2->AddChild(sampler_rt1);
  79. EXPECT_EQ(sampler_rt2->CalculateNumSamples(num_rows), 10);
  80. // Parent doesn't have num_samples
  81. std::shared_ptr<SamplerObj> sampl3 = std::make_shared<WeightedRandomSamplerObj>(weights, 12);
  82. EXPECT_NE(sampl3, nullptr);
  83. std::shared_ptr<SamplerRT> sampler_rt3;
  84. sampl3->SamplerBuild(&sampler_rt3);
  85. std::shared_ptr<SamplerObj> sampl4 = std::make_shared<SubsetRandomSamplerObj>(indices, 0);
  86. EXPECT_NE(sampl4, nullptr);
  87. std::shared_ptr<SamplerRT> sampler_rt4;
  88. sampl4->SamplerBuild(&sampler_rt4);
  89. sampler_rt4->AddChild(sampler_rt3);
  90. EXPECT_EQ(sampler_rt4->CalculateNumSamples(num_rows), 11);
  91. // Child doesn't have num_samples
  92. std::shared_ptr<SamplerObj> sampl5 = std::make_shared<RandomSamplerObj>(false, 0);
  93. EXPECT_NE(sampl5, nullptr);
  94. std::shared_ptr<SamplerRT> sampler_rt5;
  95. sampl5->SamplerBuild(&sampler_rt5);
  96. std::shared_ptr<SamplerObj> sampl6 = std::make_shared<PKSamplerObj>(3, false, 7);
  97. EXPECT_NE(sampl6, nullptr);
  98. std::shared_ptr<SamplerRT> sampler_rt6;
  99. sampl6->SamplerBuild(&sampler_rt6);
  100. sampler_rt6->AddChild(sampler_rt5);
  101. EXPECT_EQ(sampler_rt6->CalculateNumSamples(num_rows), -1);
  102. std::shared_ptr<SamplerObj> sampl7 = std::make_shared<SkipFirstEpochSamplerObj>(0);
  103. EXPECT_NE(sampl7, nullptr);
  104. std::shared_ptr<SamplerRT> sampler_rt7;
  105. sampl7->SamplerBuild(&sampler_rt7);
  106. sampler_rt7->AddChild(sampler_rt5);
  107. EXPECT_EQ(sampler_rt7->CalculateNumSamples(num_rows), -1);
  108. }
  109. TEST_F(MindDataTestIrSampler, TestSamplersMoveParameters) {
  110. std::vector<int64_t> indices = {1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23};
  111. std::shared_ptr<SamplerObj> sampl1 = std::make_shared<SubsetRandomSamplerObj>(indices, 0);
  112. EXPECT_FALSE(indices.empty());
  113. std::shared_ptr<SamplerRT> sampler_rt = nullptr;
  114. sampl1->SamplerBuild(&sampler_rt);
  115. EXPECT_NE(sampler_rt, nullptr);
  116. std::shared_ptr<SamplerObj> sampl2 = std::make_shared<SubsetRandomSamplerObj>(std::move(indices), 0);
  117. EXPECT_TRUE(indices.empty());
  118. std::shared_ptr<SamplerRT> sampler_rt2 = nullptr;
  119. sampl2->SamplerBuild(&sampler_rt2);
  120. EXPECT_NE(sampler_rt, nullptr);
  121. }
  122. /// Feature: MindData IR Sampler Support
  123. /// Description: Test MindData IR Sampler by Compile more than one epoch
  124. /// Expectation: Results are successfully outputted, first epoch has fewer rows.
  125. TEST_F(MindDataTestIrSampler, TestSkipFirstEpochSampler) {
  126. MS_LOG(INFO) << "Doing MindDataTestIrSampler-TestSkipFirstEpochSampler.";
  127. std::string dataset_dir = "./data/dataset/testPK/data";
  128. std::set<std::string> extensions = {};
  129. std::shared_ptr<DatasetCache> cache = nullptr;
  130. std::map<std::string, int32_t> class_indexing = {};
  131. std::shared_ptr<SamplerObj> sampler = std::make_shared<SkipFirstEpochSamplerObj>(1);
  132. std::shared_ptr<DatasetNode> ds =
  133. std::make_shared<ImageFolderNode>(dataset_dir, false, sampler, false, extensions, class_indexing, cache);
  134. auto ir_tree = std::make_shared<TreeAdapter>();
  135. // Compile with more than one epoch
  136. int32_t num_epoch = 3;
  137. EXPECT_OK(ir_tree->Compile(ds, num_epoch, 0));
  138. for (int i = 0; i < num_epoch; i++) {
  139. TensorRow row;
  140. ir_tree->GetNext(&row);
  141. int count = 0;
  142. while (row.size() != 0) {
  143. ir_tree->GetNext(&row);
  144. count++;
  145. }
  146. if (i == 0) {
  147. EXPECT_EQ(count, 43);
  148. } else {
  149. EXPECT_EQ(count, 44);
  150. }
  151. }
  152. }
  153. /// Feature: MindData IR Sampler Support
  154. /// Description: Compare SequentialSampler and SkipFirstEpochSampler with More Than One Epoch
  155. /// Expectation: SequentialSampler and SkipFirstEpochSampler have similar output
  156. TEST_F(MindDataTestIrSampler, CompareSequentialSamplerAndSkipFirstEpochSampler) {
  157. MS_LOG(INFO) << "Doing MindDataTestIrSampler-CompareSequentialSamplerAndSkipFirstEpochSampler.";
  158. std::string dataset_dir = "./data/dataset/testPK/data";
  159. std::set<std::string> extensions = {};
  160. std::shared_ptr<DatasetCache> cache = nullptr;
  161. std::map<std::string, int32_t> class_indexing = {};
  162. int32_t skip_num = 2;
  163. std::shared_ptr<SamplerObj> sampler1 = std::make_shared<SequentialSamplerObj>(skip_num, 0);
  164. std::shared_ptr<SamplerObj> sampler2 = std::make_shared<SkipFirstEpochSamplerObj>(skip_num);
  165. std::shared_ptr<DatasetNode> ds1 =
  166. std::make_shared<ImageFolderNode>(dataset_dir, false, sampler1, false, extensions, class_indexing, cache);
  167. std::shared_ptr<DatasetNode> ds2 =
  168. std::make_shared<ImageFolderNode>(dataset_dir, false, sampler2, false, extensions, class_indexing, cache);
  169. auto ir_tree1 = std::make_shared<TreeAdapter>();
  170. auto ir_tree2 = std::make_shared<TreeAdapter>();
  171. // Compile with more than one epoch
  172. int32_t num_epoch = 3;
  173. EXPECT_OK(ir_tree1->Compile(ds1, num_epoch, 0));
  174. EXPECT_OK(ir_tree2->Compile(ds2, num_epoch, 0));
  175. for (int i = 0; i < num_epoch; i++) {
  176. TensorRow row1;
  177. TensorRow row2;
  178. // only the first epoch has the same output
  179. if (i != 0) {
  180. // SkipFirstEpochSampler doesn't skip after the first epoch
  181. for (int j = 0; j < skip_num; j++) {
  182. EXPECT_OK(ir_tree2->GetNext(&row2));
  183. }
  184. }
  185. EXPECT_OK(ir_tree1->GetNext(&row1));
  186. EXPECT_OK(ir_tree2->GetNext(&row2));
  187. EXPECT_EQ(row1.size(), row2.size());
  188. while (row1.size() != 0 && row2.size() != 0) {
  189. std::vector<std::shared_ptr<Tensor>> r1 = row1.getRow();
  190. std::vector<std::shared_ptr<Tensor>> r2 = row2.getRow();
  191. ASSERT_EQ(r1.size(), r2.size());
  192. for (int i = 0; i < r1.size(); i++) {
  193. nlohmann::json out_json1;
  194. EXPECT_OK(r1[i]->to_json(&out_json1));
  195. std::stringstream json_ss1;
  196. json_ss1 << out_json1;
  197. nlohmann::json out_json2;
  198. EXPECT_OK(r2[i]->to_json(&out_json2));
  199. std::stringstream json_ss2;
  200. json_ss2 << out_json2;
  201. EXPECT_EQ(json_ss1.str(), json_ss2.str());
  202. }
  203. EXPECT_OK(ir_tree1->GetNext(&row1));
  204. EXPECT_OK(ir_tree2->GetNext(&row2));
  205. }
  206. }
  207. }