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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. /**
  2. * Copyright 2020-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/include/datasets.h"
  18. using namespace mindspore::dataset;
  19. using mindspore::dataset::Tensor;
  20. using mindspore::dataset::TensorShape;
  21. class MindDataTestPipeline : public UT::DatasetOpTesting {
  22. protected:
  23. };
  24. // Tests for datasets (in alphabetical order)
  25. TEST_F(MindDataTestPipeline, TestCelebADataset) {
  26. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestCelebADataset.";
  27. // Create a CelebA Dataset
  28. std::string folder_path = datasets_root_path_ + "/testCelebAData/";
  29. std::shared_ptr<Dataset> ds = CelebA(folder_path, "all", std::make_shared<SequentialSampler>(0, 2), false, {});
  30. EXPECT_NE(ds, nullptr);
  31. // Create an iterator over the result of the above dataset
  32. // This will trigger the creation of the Execution Tree and launch it.
  33. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  34. EXPECT_NE(iter, nullptr);
  35. // Iterate the dataset and get each row
  36. std::unordered_map<std::string, mindspore::MSTensor> row;
  37. iter->GetNextRow(&row);
  38. // Check if CelebAOp read correct images/attr
  39. // std::string expect_file[] = {"1.JPEG", "2.jpg"};
  40. // std::vector<std::vector<uint32_t>> expect_attr_vector = {
  41. // {0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1,
  42. // 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 1, 1, 0, 1, 0, 1, 0, 0, 1},
  43. // {0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1,
  44. // 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1}};
  45. uint64_t i = 0;
  46. while (row.size() != 0) {
  47. // auto image = row["image"];
  48. // auto attr = row["attr"];
  49. // std::shared_ptr<Tensor> expect_image;
  50. // Tensor::CreateFromFile(folder_path + expect_file[i], &expect_image);
  51. // EXPECT_EQ(*image, *expect_image);
  52. // std::shared_ptr<Tensor> expect_attr;
  53. // Tensor::CreateFromVector(expect_attr_vector[i], TensorShape({40}), &expect_attr);
  54. // EXPECT_EQ(*attr, *expect_attr);
  55. iter->GetNextRow(&row);
  56. i++;
  57. }
  58. EXPECT_EQ(i, 2);
  59. // Manually terminate the pipeline
  60. iter->Stop();
  61. }
  62. TEST_F(MindDataTestPipeline, TestCelebADefault) {
  63. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestCelebADefault.";
  64. // Create a CelebA Dataset
  65. std::string folder_path = datasets_root_path_ + "/testCelebAData/";
  66. std::shared_ptr<Dataset> ds = CelebA(folder_path);
  67. EXPECT_NE(ds, nullptr);
  68. // Create an iterator over the result of the above dataset
  69. // This will trigger the creation of the Execution Tree and launch it.
  70. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  71. EXPECT_NE(iter, nullptr);
  72. // Iterate the dataset and get each row
  73. std::unordered_map<std::string, mindspore::MSTensor> row;
  74. iter->GetNextRow(&row);
  75. // Check if CelebAOp read correct images/attr
  76. uint64_t i = 0;
  77. while (row.size() != 0) {
  78. // auto image = row["image"];
  79. // auto attr = row["attr"];
  80. // MS_LOG(INFO) << "Tensor image shape: " << image->shape();
  81. // MS_LOG(INFO) << "Tensor attr shape: " << attr->shape();
  82. iter->GetNextRow(&row);
  83. i++;
  84. }
  85. EXPECT_EQ(i, 4);
  86. // Manually terminate the pipeline
  87. iter->Stop();
  88. }
  89. TEST_F(MindDataTestPipeline, TestGetRepeatCount) {
  90. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestGetRepeatCount.";
  91. // Create an ImageFolder Dataset
  92. std::string folder_path = datasets_root_path_ + "/testPK/data/";
  93. std::shared_ptr<Dataset> ds = ImageFolder(folder_path, true);
  94. EXPECT_NE(ds, nullptr);
  95. EXPECT_EQ(ds->GetRepeatCount(), 1);
  96. ds = ds->Repeat(4);
  97. EXPECT_NE(ds, nullptr);
  98. EXPECT_EQ(ds->GetRepeatCount(), 4);
  99. ds = ds->Repeat(3);
  100. EXPECT_NE(ds, nullptr);
  101. EXPECT_EQ(ds->GetRepeatCount(), 3);
  102. }
  103. TEST_F(MindDataTestPipeline, TestGetBatchSize) {
  104. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestGetRepeatCount.";
  105. // Create an ImageFolder Dataset
  106. std::string folder_path = datasets_root_path_ + "/testPK/data/";
  107. std::shared_ptr<Dataset> ds = ImageFolder(folder_path, true)->Project({"label"});
  108. EXPECT_NE(ds, nullptr);
  109. EXPECT_EQ(ds->GetBatchSize(), 1);
  110. ds = ds->Batch(2);
  111. EXPECT_NE(ds, nullptr);
  112. EXPECT_EQ(ds->GetBatchSize(), 2);
  113. ds = ds->Batch(3);
  114. EXPECT_NE(ds, nullptr);
  115. EXPECT_EQ(ds->GetBatchSize(), 3);
  116. }
  117. TEST_F(MindDataTestPipeline, TestCelebAGetDatasetSize) {
  118. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestCelebAGetDatasetSize.";
  119. // Create a CelebA Dataset
  120. std::string folder_path = datasets_root_path_ + "/testCelebAData/";
  121. std::shared_ptr<Dataset> ds = CelebA(folder_path, "valid");
  122. EXPECT_NE(ds, nullptr);
  123. EXPECT_EQ(ds->GetDatasetSize(), 1);
  124. }
  125. TEST_F(MindDataTestPipeline, TestCelebAError) {
  126. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestCelebAError.";
  127. std::string folder_path = datasets_root_path_ + "/testCelebAData/";
  128. std::string invalid_folder_path = "./testNotExist";
  129. std::string invalid_dataset_type = "invalid_type";
  130. // Create a CelebA Dataset
  131. std::shared_ptr<Dataset> ds1 = CelebA(invalid_folder_path);
  132. EXPECT_NE(ds1, nullptr);
  133. // Create an iterator over the result of the above dataset
  134. std::shared_ptr<Iterator> iter1 = ds1->CreateIterator();
  135. // Expect failure: invalid CelebA input, invalid dataset path
  136. EXPECT_EQ(iter1, nullptr);
  137. // Create a CelebA Dataset
  138. std::shared_ptr<Dataset> ds2 = CelebA(folder_path, invalid_dataset_type);
  139. EXPECT_NE(ds2, nullptr);
  140. // Create an iterator over the result of the above dataset
  141. std::shared_ptr<Iterator> iter2 = ds2->CreateIterator();
  142. // Expect failure: invalid CelebA input, invalid dataset type
  143. EXPECT_EQ(iter2, nullptr);
  144. }
  145. TEST_F(MindDataTestPipeline, TestCelebADatasetWithNullSamplerError) {
  146. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestCelebADataset.";
  147. // Create a CelebA Dataset
  148. std::string folder_path = datasets_root_path_ + "/testCelebAData/";
  149. std::shared_ptr<Dataset> ds = CelebA(folder_path, "all", nullptr, false, {});
  150. EXPECT_NE(ds, nullptr);
  151. // Create an iterator over the result of the above dataset
  152. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  153. // Expect failure: invalid CelebA input, sampler cannot be nullptr
  154. EXPECT_EQ(iter, nullptr);
  155. }
  156. TEST_F(MindDataTestPipeline, TestImageFolderWithWrongDatasetDirFail) {
  157. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestImageFolderWithWrongDatasetDirFail.";
  158. // Create an ImageFolder Dataset
  159. std::shared_ptr<Dataset> ds = ImageFolder("", true, nullptr);
  160. EXPECT_NE(ds, nullptr);
  161. // Create an iterator over the result of the above dataset
  162. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  163. // Expect failure: invalid ImageFolder input
  164. EXPECT_EQ(iter, nullptr);
  165. }
  166. TEST_F(MindDataTestPipeline, TestImageFolderFailWithWrongExtensionFail) {
  167. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestImageFolderFailWithWrongExtensionFail.";
  168. // Create an ImageFolder Dataset
  169. std::string folder_path = datasets_root_path_ + "/testPK/data/";
  170. std::shared_ptr<Dataset> ds = ImageFolder(folder_path, true, std::make_shared<RandomSampler>(false, 2), {".JGP"});
  171. EXPECT_NE(ds, nullptr);
  172. // Create an iterator over the result of the above dataset
  173. // This will trigger the creation of the Execution Tree and launch it.
  174. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  175. EXPECT_NE(iter, nullptr);
  176. // Iterate the dataset and get each row
  177. std::unordered_map<std::string, mindspore::MSTensor> row;
  178. iter->GetNextRow(&row);
  179. // Expect no data: cannot find files with specified extension
  180. // EXPECT_EQ(row.size(), 0);
  181. // Manually terminate the pipeline
  182. iter->Stop();
  183. }
  184. TEST_F(MindDataTestPipeline, TestImageFolderGetters) {
  185. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestImageFolderGetDatasetSize.";
  186. // Create an ImageFolder Dataset
  187. std::string folder_path = datasets_root_path_ + "/testPK/data/";
  188. std::shared_ptr<Dataset> ds = ImageFolder(folder_path, true);
  189. EXPECT_NE(ds, nullptr);
  190. EXPECT_EQ(ds->GetDatasetSize(), 44);
  191. EXPECT_EQ(ds->GetNumClasses(), 4);
  192. EXPECT_EQ(ds->GetNumClasses(), 4);
  193. EXPECT_EQ(ds->GetDatasetSize(), 44);
  194. EXPECT_EQ(ds->GetDatasetSize(), 44);
  195. }
  196. TEST_F(MindDataTestPipeline, TestImageFolderFailWithNullSamplerFail) {
  197. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestImageFolderFailWithNullSamplerFail.";
  198. // Create an ImageFolder Dataset
  199. std::string folder_path = datasets_root_path_ + "/testPK/data/";
  200. std::shared_ptr<Dataset> ds = ImageFolder(folder_path, true, nullptr);
  201. EXPECT_NE(ds, nullptr);
  202. // Create an iterator over the result of the above dataset
  203. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  204. // Expect failure: invalid ImageFolder input, sampler cannot be nullptr
  205. EXPECT_EQ(iter, nullptr);
  206. }
  207. TEST_F(MindDataTestPipeline, TestImageFolderFailWithWrongSamplerFail) {
  208. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestImageFolderFailWithWrongSamplerFail.";
  209. // Create an ImageFolder Dataset
  210. std::string folder_path = datasets_root_path_ + "/testPK/data/";
  211. std::shared_ptr<Dataset> ds = ImageFolder(folder_path, true, std::make_shared<SequentialSampler>(-2, 5));
  212. EXPECT_NE(ds, nullptr);
  213. // Create an iterator over the result of the above dataset
  214. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  215. // Expect failure: invalid ImageFolder input, sampler is not constructed correctly
  216. EXPECT_EQ(iter, nullptr);
  217. }
  218. TEST_F(MindDataTestPipeline, TestMnistGetDatasetSize) {
  219. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestMnistGetDatasetSize.";
  220. // Create a Mnist Dataset
  221. std::string folder_path = datasets_root_path_ + "/testMnistData/";
  222. std::shared_ptr<Dataset> ds = Mnist(folder_path, "all", std::make_shared<RandomSampler>(false, 20));
  223. EXPECT_NE(ds, nullptr);
  224. EXPECT_EQ(ds->GetDatasetSize(), 20);
  225. }
  226. TEST_F(MindDataTestPipeline, TestMnistFailWithWrongDatasetDirFail) {
  227. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestMnistFailWithWrongDatasetDirFail.";
  228. // Create a Mnist Dataset
  229. std::shared_ptr<Dataset> ds = Mnist("", "all", std::make_shared<RandomSampler>(false, 10));
  230. EXPECT_NE(ds, nullptr);
  231. // Create an iterator over the result of the above dataset
  232. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  233. // Expect failure: invalid Mnist input, incorrect dataset directory input
  234. EXPECT_EQ(iter, nullptr);
  235. }
  236. TEST_F(MindDataTestPipeline, TestMnistFailWithNullSamplerFail) {
  237. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestMnistFailWithNullSamplerFail.";
  238. // Create a Mnist Dataset
  239. std::string folder_path = datasets_root_path_ + "/testMnistData/";
  240. std::shared_ptr<Dataset> ds = Mnist(folder_path, "all", nullptr);
  241. EXPECT_NE(ds, nullptr);
  242. // Create an iterator over the result of the above dataset
  243. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  244. // Expect failure: invalid Mnist input, sampler cannot be nullptr
  245. EXPECT_EQ(iter, nullptr);
  246. }
  247. TEST_F(MindDataTestPipeline, TestImageFolderClassIndexDatasetSize) {
  248. std::string folder_path = datasets_root_path_ + "/testPK/data";
  249. std::map<std::string, int32_t> class_index;
  250. class_index["class1"] = 111;
  251. class_index["class2"] = 333;
  252. auto ds = ImageFolder(folder_path, false, std::make_shared<RandomSampler>(), {}, class_index);
  253. EXPECT_EQ(ds->GetNumClasses(), 2);
  254. }
  255. TEST_F(MindDataTestPipeline, TestImageFolderClassIndexDatasetSizeFail) {
  256. std::string folder_path = datasets_root_path_ + "/testPK/data";
  257. std::map<std::string, int32_t> class_index;
  258. class_index["class1"] = 111;
  259. class_index["wrong class"] = 333;
  260. auto ds = ImageFolder(folder_path, false, std::make_shared<RandomSampler>(), {}, class_index);
  261. EXPECT_EQ(ds->GetNumClasses(), -1);
  262. }