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

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