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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 1, 1, 0, 1, 0,
  41. 1, 0, 0, 1}, {0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0,
  42. 1, 0, 0, 0, 0, 0, 0, 0, 1}};
  43. uint64_t i = 0;
  44. while (row.size() != 0) {
  45. auto image = row["image"];
  46. auto attr = row["attr"];
  47. std::shared_ptr<Tensor> expect_image;
  48. Tensor::CreateFromFile(folder_path + expect_file[i], &expect_image);
  49. EXPECT_EQ(*image, *expect_image);
  50. std::shared_ptr<Tensor> expect_attr;
  51. Tensor::CreateFromVector(expect_attr_vector[i], TensorShape({40}), &expect_attr);
  52. EXPECT_EQ(*attr, *expect_attr);
  53. iter->GetNextRow(&row);
  54. i++;
  55. }
  56. EXPECT_EQ(i, 2);
  57. // Manually terminate the pipeline
  58. iter->Stop();
  59. }
  60. TEST_F(MindDataTestPipeline, TestCelebADefault) {
  61. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestCelebADefault.";
  62. // Create a CelebA Dataset
  63. std::string folder_path = datasets_root_path_ + "/testCelebAData/";
  64. std::shared_ptr<Dataset> ds = CelebA(folder_path);
  65. EXPECT_NE(ds, nullptr);
  66. // Create an iterator over the result of the above dataset
  67. // This will trigger the creation of the Execution Tree and launch it.
  68. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  69. EXPECT_NE(iter, nullptr);
  70. // Iterate the dataset and get each row
  71. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  72. iter->GetNextRow(&row);
  73. // Check if CelebAOp read correct images/attr
  74. uint64_t i = 0;
  75. while (row.size() != 0) {
  76. auto image = row["image"];
  77. auto attr = row["attr"];
  78. MS_LOG(INFO) << "Tensor image shape: " << image->shape();
  79. MS_LOG(INFO) << "Tensor attr shape: " << attr->shape();
  80. iter->GetNextRow(&row);
  81. i++;
  82. }
  83. EXPECT_EQ(i, 2);
  84. // Manually terminate the pipeline
  85. iter->Stop();
  86. }
  87. TEST_F(MindDataTestPipeline, TestCelebAException) {
  88. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestCelebAException.";
  89. // Create a CelebA Dataset
  90. std::string folder_path = datasets_root_path_ + "/testCelebAData/";
  91. std::string invalid_folder_path = "./testNotExist";
  92. std::string invalid_dataset_type = "invalid_type";
  93. std::shared_ptr<Dataset> ds = CelebA(invalid_folder_path);
  94. EXPECT_EQ(ds, nullptr);
  95. std::shared_ptr<Dataset> ds1 = CelebA(folder_path, invalid_dataset_type);
  96. EXPECT_EQ(ds1, nullptr);
  97. }
  98. TEST_F(MindDataTestPipeline, TestImageFolderFail1) {
  99. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestImageFolderFail1.";
  100. // Create an ImageFolder Dataset
  101. std::shared_ptr<Dataset> ds = ImageFolder("", true, nullptr);
  102. EXPECT_EQ(ds, nullptr);
  103. }
  104. TEST_F(MindDataTestPipeline, TestMnistFail1) {
  105. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestMnistFail1.";
  106. // Create a Mnist Dataset
  107. std::shared_ptr<Dataset> ds = Mnist("", RandomSampler(false, 10));
  108. EXPECT_EQ(ds, nullptr);
  109. }
  110. TEST_F(MindDataTestPipeline, TestImageFolderFail2) {
  111. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestImageFolderFail2.";
  112. // Create an ImageFolder Dataset
  113. std::string folder_path = datasets_root_path_ + "/testPK/data/";
  114. std::shared_ptr<Dataset> ds = ImageFolder(folder_path, true, RandomSampler(false, 2), {".JGP"});
  115. EXPECT_NE(ds, nullptr);
  116. // Create an iterator over the result of the above dataset
  117. // This will trigger the creation of the Execution Tree and launch it.
  118. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  119. EXPECT_NE(iter, nullptr);
  120. // Iterate the dataset and get each row
  121. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  122. iter->GetNextRow(&row);
  123. EXPECT_EQ(row.size(), 0);
  124. // Manually terminate the pipeline
  125. iter->Stop();
  126. }