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.

c_api_samplers_test.cc 4.0 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 "common/common.h"
  17. #include "minddata/dataset/include/datasets.h"
  18. #include "minddata/dataset/engine/ir/datasetops/batch_node.h"
  19. #include "minddata/dataset/engine/ir/datasetops/bucket_batch_by_length_node.h"
  20. #include "minddata/dataset/engine/ir/datasetops/concat_node.h"
  21. #include "minddata/dataset/engine/ir/datasetops/project_node.h"
  22. #include "minddata/dataset/engine/ir/datasetops/rename_node.h"
  23. #include "minddata/dataset/engine/ir/datasetops/shuffle_node.h"
  24. #include "minddata/dataset/engine/ir/datasetops/source/image_folder_node.h"
  25. using namespace mindspore::dataset::api;
  26. using mindspore::dataset::Tensor;
  27. class MindDataTestPipeline : public UT::DatasetOpTesting {
  28. protected:
  29. };
  30. TEST_F(MindDataTestPipeline, TestImageFolderWithSamplers) {
  31. std::shared_ptr<SamplerObj> sampl = DistributedSampler(2, 1);
  32. EXPECT_NE(sampl, nullptr);
  33. sampl = PKSampler(3);
  34. EXPECT_NE(sampl, nullptr);
  35. sampl = RandomSampler(false, 12);
  36. EXPECT_NE(sampl, nullptr);
  37. sampl = SequentialSampler(0, 12);
  38. EXPECT_NE(sampl, nullptr);
  39. 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};
  40. sampl = WeightedRandomSampler(weights, 12);
  41. EXPECT_NE(sampl, nullptr);
  42. std::vector<int64_t> indices = {1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23};
  43. sampl = SubsetRandomSampler(indices);
  44. EXPECT_NE(sampl, nullptr);
  45. // Create an ImageFolder Dataset
  46. std::string folder_path = datasets_root_path_ + "/testPK/data/";
  47. std::shared_ptr<Dataset> ds = ImageFolder(folder_path, false, sampl);
  48. EXPECT_NE(ds, nullptr);
  49. // Create a Repeat operation on ds
  50. int32_t repeat_num = 2;
  51. ds = ds->Repeat(repeat_num);
  52. EXPECT_NE(ds, nullptr);
  53. // Create a Batch operation on ds
  54. int32_t batch_size = 2;
  55. ds = ds->Batch(batch_size);
  56. EXPECT_NE(ds, nullptr);
  57. // Create an iterator over the result of the above dataset
  58. // This will trigger the creation of the Execution Tree and launch it.
  59. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  60. EXPECT_NE(iter, nullptr);
  61. // Iterate the dataset and get each row
  62. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  63. iter->GetNextRow(&row);
  64. uint64_t i = 0;
  65. while (row.size() != 0) {
  66. i++;
  67. auto image = row["image"];
  68. MS_LOG(INFO) << "Tensor image shape: " << image->shape();
  69. iter->GetNextRow(&row);
  70. }
  71. EXPECT_EQ(i, 12);
  72. // Manually terminate the pipeline
  73. iter->Stop();
  74. }
  75. TEST_F(MindDataTestPipeline, TestSamplersMoveParameters) {
  76. std::vector<int64_t> indices = {1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23};
  77. std::shared_ptr<SamplerObj> sampl1 = SubsetRandomSampler(indices);
  78. EXPECT_FALSE(indices.empty());
  79. EXPECT_NE(sampl1->Build(), nullptr);
  80. std::shared_ptr<SamplerObj> sampl2 = SubsetRandomSampler(std::move(indices));
  81. EXPECT_TRUE(indices.empty());
  82. EXPECT_NE(sampl2->Build(), nullptr);
  83. }
  84. TEST_F(MindDataTestPipeline, TestWeightedRandomSamplerFail) {
  85. // weights is empty
  86. std::vector<double> weights1 = {};
  87. std::shared_ptr<SamplerObj> sampl1 = WeightedRandomSampler(weights1);
  88. EXPECT_EQ(sampl1, nullptr);
  89. // weights has negative number
  90. std::vector<double> weights2 = {0.5, 0.2, -0.4};
  91. std::shared_ptr<SamplerObj> sampl2 = WeightedRandomSampler(weights2);
  92. EXPECT_EQ(sampl2, nullptr);
  93. // weights elements are all zero
  94. std::vector<double> weights3 = {0, 0, 0};
  95. std::shared_ptr<SamplerObj> sampl3 = WeightedRandomSampler(weights3);
  96. EXPECT_EQ(sampl3, nullptr);
  97. }