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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. using namespace mindspore::dataset::api;
  19. using mindspore::dataset::Tensor;
  20. class MindDataTestPipeline : public UT::DatasetOpTesting {
  21. protected:
  22. };
  23. TEST_F(MindDataTestPipeline, TestImageFolderWithSamplers) {
  24. std::shared_ptr<SamplerObj> sampl = DistributedSampler(2, 1);
  25. EXPECT_NE(sampl, nullptr);
  26. sampl = PKSampler(3);
  27. EXPECT_NE(sampl, nullptr);
  28. sampl = RandomSampler(false, 12);
  29. EXPECT_NE(sampl, nullptr);
  30. sampl = SequentialSampler(0, 12);
  31. EXPECT_NE(sampl, nullptr);
  32. 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};
  33. sampl = WeightedRandomSampler(weights, 12);
  34. EXPECT_NE(sampl, nullptr);
  35. std::vector<int64_t> indices = {1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23};
  36. sampl = SubsetRandomSampler(indices);
  37. EXPECT_NE(sampl, nullptr);
  38. // Create an ImageFolder Dataset
  39. std::string folder_path = datasets_root_path_ + "/testPK/data/";
  40. std::shared_ptr<Dataset> ds = ImageFolder(folder_path, false, sampl);
  41. EXPECT_NE(ds, nullptr);
  42. // Create a Repeat operation on ds
  43. int32_t repeat_num = 2;
  44. ds = ds->Repeat(repeat_num);
  45. EXPECT_NE(ds, nullptr);
  46. // Create a Batch operation on ds
  47. int32_t batch_size = 2;
  48. ds = ds->Batch(batch_size);
  49. EXPECT_NE(ds, nullptr);
  50. // Create an iterator over the result of the above dataset
  51. // This will trigger the creation of the Execution Tree and launch it.
  52. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  53. EXPECT_NE(iter, nullptr);
  54. // Iterate the dataset and get each row
  55. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  56. iter->GetNextRow(&row);
  57. uint64_t i = 0;
  58. while (row.size() != 0) {
  59. i++;
  60. auto image = row["image"];
  61. MS_LOG(INFO) << "Tensor image shape: " << image->shape();
  62. iter->GetNextRow(&row);
  63. }
  64. EXPECT_EQ(i, 12);
  65. // Manually terminate the pipeline
  66. iter->Stop();
  67. }
  68. TEST_F(MindDataTestPipeline, TestSamplersMoveParameters) {
  69. std::vector<int64_t> indices = {1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23};
  70. std::shared_ptr<SamplerObj> sampl1 = SubsetRandomSampler(indices);
  71. EXPECT_FALSE(indices.empty());
  72. EXPECT_NE(sampl1->Build(), nullptr);
  73. std::shared_ptr<SamplerObj> sampl2 = SubsetRandomSampler(std::move(indices));
  74. EXPECT_TRUE(indices.empty());
  75. EXPECT_NE(sampl2->Build(), nullptr);
  76. }