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_datasets_test.cc 7.2 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. using mindspore::dataset::TensorShape;
  21. class MindDataTestPipeline : public UT::DatasetOpTesting {
  22. protected:
  23. };
  24. TEST_F(MindDataTestPipeline, TestCelebADataset) {
  25. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestCelebADataset.";
  26. // Create a CelebA Dataset
  27. std::string folder_path = datasets_root_path_ + "/testCelebAData/";
  28. std::shared_ptr<Dataset> ds = CelebA(folder_path, "all", SequentialSampler(0, 2), false, {});
  29. EXPECT_NE(ds, nullptr);
  30. // Create an iterator over the result of the above dataset
  31. // This will trigger the creation of the Execution Tree and launch it.
  32. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  33. EXPECT_NE(iter, nullptr);
  34. // Iterate the dataset and get each row
  35. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  36. iter->GetNextRow(&row);
  37. // Check if CelebAOp read correct images/attr
  38. std::string expect_file[] = {"1.JPEG", "2.jpg"};
  39. std::vector<std::vector<uint32_t>> expect_attr_vector = {
  40. {0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1,
  41. 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 1, 1, 0, 1, 0, 1, 0, 0, 1},
  42. {0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1,
  43. 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1}};
  44. uint64_t i = 0;
  45. while (row.size() != 0) {
  46. auto image = row["image"];
  47. auto attr = row["attr"];
  48. std::shared_ptr<Tensor> expect_image;
  49. Tensor::CreateFromFile(folder_path + expect_file[i], &expect_image);
  50. EXPECT_EQ(*image, *expect_image);
  51. std::shared_ptr<Tensor> expect_attr;
  52. Tensor::CreateFromVector(expect_attr_vector[i], TensorShape({40}), &expect_attr);
  53. EXPECT_EQ(*attr, *expect_attr);
  54. iter->GetNextRow(&row);
  55. i++;
  56. }
  57. EXPECT_EQ(i, 2);
  58. // Manually terminate the pipeline
  59. iter->Stop();
  60. }
  61. TEST_F(MindDataTestPipeline, TestCelebADefault) {
  62. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestCelebADefault.";
  63. // Create a CelebA Dataset
  64. std::string folder_path = datasets_root_path_ + "/testCelebAData/";
  65. std::shared_ptr<Dataset> ds = CelebA(folder_path);
  66. EXPECT_NE(ds, nullptr);
  67. // Create an iterator over the result of the above dataset
  68. // This will trigger the creation of the Execution Tree and launch it.
  69. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  70. EXPECT_NE(iter, nullptr);
  71. // Iterate the dataset and get each row
  72. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  73. iter->GetNextRow(&row);
  74. // Check if CelebAOp read correct images/attr
  75. uint64_t i = 0;
  76. while (row.size() != 0) {
  77. auto image = row["image"];
  78. auto attr = row["attr"];
  79. MS_LOG(INFO) << "Tensor image shape: " << image->shape();
  80. MS_LOG(INFO) << "Tensor attr shape: " << attr->shape();
  81. iter->GetNextRow(&row);
  82. i++;
  83. }
  84. EXPECT_EQ(i, 4);
  85. // Manually terminate the pipeline
  86. iter->Stop();
  87. }
  88. TEST_F(MindDataTestPipeline, TestCelebAException) {
  89. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestCelebAException.";
  90. // Create a CelebA Dataset
  91. std::string folder_path = datasets_root_path_ + "/testCelebAData/";
  92. std::string invalid_folder_path = "./testNotExist";
  93. std::string invalid_dataset_type = "invalid_type";
  94. std::shared_ptr<Dataset> ds = CelebA(invalid_folder_path);
  95. EXPECT_EQ(ds, nullptr);
  96. std::shared_ptr<Dataset> ds1 = CelebA(folder_path, invalid_dataset_type);
  97. EXPECT_EQ(ds1, nullptr);
  98. }
  99. TEST_F(MindDataTestPipeline, TestCelebADatasetWithNullSampler) {
  100. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestCelebADataset.";
  101. // Create a CelebA Dataset
  102. std::string folder_path = datasets_root_path_ + "/testCelebAData/";
  103. std::shared_ptr<Dataset> ds = CelebA(folder_path, "all", nullptr, false, {});
  104. // Expect failure: sampler can not be nullptr
  105. EXPECT_EQ(ds, nullptr);
  106. }
  107. TEST_F(MindDataTestPipeline, TestMnistFailWithWrongDatasetDir) {
  108. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestMnistFailWithWrongDatasetDir.";
  109. // Create a Mnist Dataset
  110. std::shared_ptr<Dataset> ds = Mnist("", "all", RandomSampler(false, 10));
  111. EXPECT_EQ(ds, nullptr);
  112. }
  113. TEST_F(MindDataTestPipeline, TestMnistFailWithNullSampler) {
  114. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestMnistFailWithNullSampler.";
  115. // Create a Mnist Dataset
  116. std::string folder_path = datasets_root_path_ + "/testMnistData/";
  117. std::shared_ptr<Dataset> ds = Mnist(folder_path, "all", nullptr);
  118. // Expect failure: sampler can not be nullptr
  119. EXPECT_EQ(ds, nullptr);
  120. }
  121. TEST_F(MindDataTestPipeline, TestImageFolderWithWrongDatasetDir) {
  122. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestImageFolderWithWrongDatasetDir.";
  123. // Create an ImageFolder Dataset
  124. std::shared_ptr<Dataset> ds = ImageFolder("", true, nullptr);
  125. EXPECT_EQ(ds, nullptr);
  126. }
  127. TEST_F(MindDataTestPipeline, TestImageFolderFailWithWrongExtension) {
  128. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestImageFolderFailWithWrongExtension.";
  129. // Create an ImageFolder Dataset
  130. std::string folder_path = datasets_root_path_ + "/testPK/data/";
  131. std::shared_ptr<Dataset> ds = ImageFolder(folder_path, true, RandomSampler(false, 2), {".JGP"});
  132. EXPECT_NE(ds, nullptr);
  133. // Create an iterator over the result of the above dataset
  134. // This will trigger the creation of the Execution Tree and launch it.
  135. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  136. EXPECT_NE(iter, nullptr);
  137. // Iterate the dataset and get each row
  138. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  139. iter->GetNextRow(&row);
  140. // Expect no data: can not find files with specified extension
  141. EXPECT_EQ(row.size(), 0);
  142. // Manually terminate the pipeline
  143. iter->Stop();
  144. }
  145. TEST_F(MindDataTestPipeline, TestImageFolderFailWithNullSampler) {
  146. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestImageFolderFailWithNullSampler.";
  147. // Create an ImageFolder Dataset
  148. std::string folder_path = datasets_root_path_ + "/testPK/data/";
  149. std::shared_ptr<Dataset> ds = ImageFolder(folder_path, true, nullptr);
  150. // Expect failure: sampler can not be nullptr
  151. EXPECT_EQ(ds, nullptr);
  152. }
  153. TEST_F(MindDataTestPipeline, TestImageFolderFailWithWrongSampler) {
  154. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestImageFolderFailWithWrongSampler.";
  155. // Create a Cifar10 Dataset
  156. std::string folder_path = datasets_root_path_ + "/testCifar100Data/";
  157. std::shared_ptr<Dataset> ds = ImageFolder(folder_path, true, SequentialSampler(-2, 5));
  158. // Expect failure: sampler is not construnced correctly
  159. EXPECT_EQ(ds, nullptr);
  160. }