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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  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/dataset/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. ASSERT_OK(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. ASSERT_OK(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. ASSERT_OK(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. ASSERT_OK(iter->GetNextRow(&row));
  85. i++;
  86. }
  87. EXPECT_EQ(i, 4);
  88. // Manually terminate the pipeline
  89. iter->Stop();
  90. }
  91. // Feature: Test Repeat operation on CelebA dataset
  92. // Description: Perform repeat operation with count = 2, and count rows in the dataset
  93. // Expectation: Dataset should have 8 rows (2 times original size since it is repeated)
  94. TEST_F(MindDataTestPipeline, TestCelebARepeat) {
  95. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestCelebARepeat.";
  96. std::string file_path = datasets_root_path_ + "/testCelebAData/";
  97. std::shared_ptr<Dataset> ds = CelebA(file_path);
  98. ds = ds->Repeat(2);
  99. // Create an iterator over the result of the above dataset
  100. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  101. // Expect a valid iterator
  102. ASSERT_NE(iter, nullptr);
  103. // Iterate the dataset and get each row
  104. std::unordered_map<std::string, mindspore::MSTensor> row;
  105. ASSERT_OK(iter->GetNextRow(&row));
  106. uint64_t i = 0;
  107. while (row.size() != 0) {
  108. i++;
  109. ASSERT_OK(iter->GetNextRow(&row));
  110. }
  111. // Verify correct number of rows fetched
  112. EXPECT_EQ(i, 8);
  113. // Manually terminate the pipeline
  114. iter->Stop();
  115. }
  116. // Feature: Test SubsetRandomSampler on CelebA dataset
  117. // Description: Create dataset with SubsetRandomSampler given a single index and count rows in the dataset
  118. // Expectation: Dataset should have 1 row
  119. TEST_F(MindDataTestPipeline, TestCelebASubsetRandomSampler) {
  120. std::vector<int64_t> indices = {1};
  121. std::shared_ptr<Sampler> sampl = std::make_shared<SubsetRandomSampler>(indices);
  122. EXPECT_NE(sampl, nullptr);
  123. // Create an ImageFolder Dataset
  124. std::string folder_path = datasets_root_path_ + "/testCelebAData/";
  125. std::shared_ptr<Dataset> ds = CelebA(folder_path, "all", sampl);
  126. EXPECT_NE(ds, nullptr);
  127. // Create an iterator over the result of the above dataset
  128. // This will trigger the creation of the Execution Tree and launch it.
  129. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  130. EXPECT_NE(iter, nullptr);
  131. // Iterate the dataset and get each row
  132. std::unordered_map<std::string, mindspore::MSTensor> row;
  133. ASSERT_OK(iter->GetNextRow(&row));
  134. uint64_t i = 0;
  135. while (row.size() != 0) {
  136. i++;
  137. ASSERT_OK(iter->GetNextRow(&row));
  138. }
  139. EXPECT_EQ(i, 1);
  140. // Manually terminate the pipeline
  141. iter->Stop();
  142. }
  143. TEST_F(MindDataTestPipeline, TestGetRepeatCount) {
  144. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestGetRepeatCount.";
  145. // Create an ImageFolder Dataset
  146. std::string folder_path = datasets_root_path_ + "/testPK/data/";
  147. std::shared_ptr<Dataset> ds = ImageFolder(folder_path, true);
  148. EXPECT_NE(ds, nullptr);
  149. EXPECT_EQ(ds->GetRepeatCount(), 1);
  150. ds = ds->Repeat(4);
  151. EXPECT_NE(ds, nullptr);
  152. EXPECT_EQ(ds->GetRepeatCount(), 4);
  153. ds = ds->Repeat(3);
  154. EXPECT_NE(ds, nullptr);
  155. EXPECT_EQ(ds->GetRepeatCount(), 3);
  156. }
  157. TEST_F(MindDataTestPipeline, TestGetBatchSize) {
  158. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestGetRepeatCount.";
  159. // Create an ImageFolder Dataset
  160. std::string folder_path = datasets_root_path_ + "/testPK/data/";
  161. std::shared_ptr<Dataset> ds = ImageFolder(folder_path, true)->Project({"label"});
  162. EXPECT_NE(ds, nullptr);
  163. EXPECT_EQ(ds->GetBatchSize(), 1);
  164. ds = ds->Batch(2);
  165. EXPECT_NE(ds, nullptr);
  166. EXPECT_EQ(ds->GetBatchSize(), 2);
  167. ds = ds->Batch(3);
  168. EXPECT_NE(ds, nullptr);
  169. EXPECT_EQ(ds->GetBatchSize(), 3);
  170. }
  171. TEST_F(MindDataTestPipeline, TestCelebAGetDatasetSize) {
  172. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestCelebAGetDatasetSize.";
  173. // Create a CelebA Dataset
  174. std::string folder_path = datasets_root_path_ + "/testCelebAData/";
  175. std::shared_ptr<Dataset> ds = CelebA(folder_path, "valid");
  176. EXPECT_NE(ds, nullptr);
  177. EXPECT_EQ(ds->GetDatasetSize(), 1);
  178. }
  179. TEST_F(MindDataTestPipeline, TestCelebAError) {
  180. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestCelebAError.";
  181. std::string folder_path = datasets_root_path_ + "/testCelebAData/";
  182. std::string invalid_folder_path = "./testNotExist";
  183. std::string invalid_dataset_type = "invalid_type";
  184. // Create a CelebA Dataset
  185. std::shared_ptr<Dataset> ds1 = CelebA(invalid_folder_path);
  186. EXPECT_NE(ds1, nullptr);
  187. // Create an iterator over the result of the above dataset
  188. std::shared_ptr<Iterator> iter1 = ds1->CreateIterator();
  189. // Expect failure: invalid CelebA input, invalid dataset path
  190. EXPECT_EQ(iter1, nullptr);
  191. // Create a CelebA Dataset
  192. std::shared_ptr<Dataset> ds2 = CelebA(folder_path, invalid_dataset_type);
  193. EXPECT_NE(ds2, nullptr);
  194. // Create an iterator over the result of the above dataset
  195. std::shared_ptr<Iterator> iter2 = ds2->CreateIterator();
  196. // Expect failure: invalid CelebA input, invalid dataset type
  197. EXPECT_EQ(iter2, nullptr);
  198. }
  199. TEST_F(MindDataTestPipeline, TestCelebADatasetWithNullSamplerError) {
  200. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestCelebADataset.";
  201. // Create a CelebA Dataset
  202. std::string folder_path = datasets_root_path_ + "/testCelebAData/";
  203. std::shared_ptr<Dataset> ds = CelebA(folder_path, "all", nullptr, false, {});
  204. EXPECT_NE(ds, nullptr);
  205. // Create an iterator over the result of the above dataset
  206. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  207. // Expect failure: invalid CelebA input, sampler cannot be nullptr
  208. EXPECT_EQ(iter, nullptr);
  209. }
  210. TEST_F(MindDataTestPipeline, TestImageFolderWithWrongDatasetDirFail) {
  211. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestImageFolderWithWrongDatasetDirFail.";
  212. // Create an ImageFolder Dataset
  213. std::shared_ptr<Dataset> ds = ImageFolder("", true, nullptr);
  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
  218. EXPECT_EQ(iter, nullptr);
  219. }
  220. TEST_F(MindDataTestPipeline, TestImageFolderFailWithWrongExtensionFail) {
  221. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestImageFolderFailWithWrongExtensionFail.";
  222. // Create an ImageFolder Dataset
  223. std::string folder_path = datasets_root_path_ + "/testPK/data/";
  224. std::shared_ptr<Dataset> ds = ImageFolder(folder_path, true, std::make_shared<RandomSampler>(false, 2), {".JGP"});
  225. EXPECT_NE(ds, nullptr);
  226. // Create an iterator over the result of the above dataset
  227. // This will trigger the creation of the Execution Tree and launch it.
  228. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  229. EXPECT_NE(iter, nullptr);
  230. // Iterate the dataset and get each row
  231. std::unordered_map<std::string, mindspore::MSTensor> row;
  232. // Expect no data: cannot find files with specified extension
  233. EXPECT_ERROR(iter->GetNextRow(&row));
  234. EXPECT_EQ(row.size(), 0);
  235. // Manually terminate the pipeline
  236. iter->Stop();
  237. }
  238. TEST_F(MindDataTestPipeline, TestImageFolderGetters) {
  239. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestImageFolderGetDatasetSize.";
  240. // Create an ImageFolder Dataset
  241. std::string folder_path = datasets_root_path_ + "/testPK/data/";
  242. std::shared_ptr<Dataset> ds = ImageFolder(folder_path, true);
  243. EXPECT_NE(ds, nullptr);
  244. EXPECT_EQ(ds->GetDatasetSize(), 44);
  245. EXPECT_EQ(ds->GetNumClasses(), 4);
  246. EXPECT_EQ(ds->GetNumClasses(), 4);
  247. EXPECT_EQ(ds->GetDatasetSize(), 44);
  248. EXPECT_EQ(ds->GetDatasetSize(), 44);
  249. }
  250. TEST_F(MindDataTestPipeline, TestImageFolderFailWithNullSamplerFail) {
  251. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestImageFolderFailWithNullSamplerFail.";
  252. // Create an ImageFolder Dataset
  253. std::string folder_path = datasets_root_path_ + "/testPK/data/";
  254. std::shared_ptr<Dataset> ds = ImageFolder(folder_path, true, nullptr);
  255. EXPECT_NE(ds, nullptr);
  256. // Create an iterator over the result of the above dataset
  257. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  258. // Expect failure: invalid ImageFolder input, sampler cannot be nullptr
  259. EXPECT_EQ(iter, nullptr);
  260. }
  261. TEST_F(MindDataTestPipeline, TestImageFolderFailWithWrongSamplerFail) {
  262. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestImageFolderFailWithWrongSamplerFail.";
  263. // Create an ImageFolder Dataset
  264. std::string folder_path = datasets_root_path_ + "/testPK/data/";
  265. std::shared_ptr<Dataset> ds = ImageFolder(folder_path, true, std::make_shared<SequentialSampler>(-2, 5));
  266. EXPECT_NE(ds, nullptr);
  267. // Create an iterator over the result of the above dataset
  268. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  269. // Expect failure: invalid ImageFolder input, sampler is not constructed correctly
  270. EXPECT_EQ(iter, nullptr);
  271. }
  272. TEST_F(MindDataTestPipeline, TestMnistGetDatasetSize) {
  273. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestMnistGetDatasetSize.";
  274. // Create a Mnist Dataset
  275. std::string folder_path = datasets_root_path_ + "/testMnistData/";
  276. std::shared_ptr<Dataset> ds = Mnist(folder_path, "all", std::make_shared<RandomSampler>(false, 20));
  277. EXPECT_NE(ds, nullptr);
  278. EXPECT_EQ(ds->GetDatasetSize(), 20);
  279. }
  280. TEST_F(MindDataTestPipeline, TestMnistFailWithWrongDatasetDirFail) {
  281. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestMnistFailWithWrongDatasetDirFail.";
  282. // Create a Mnist Dataset
  283. std::shared_ptr<Dataset> ds = Mnist("", "all", std::make_shared<RandomSampler>(false, 10));
  284. EXPECT_NE(ds, nullptr);
  285. // Create an iterator over the result of the above dataset
  286. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  287. // Expect failure: invalid Mnist input, incorrect dataset directory input
  288. EXPECT_EQ(iter, nullptr);
  289. }
  290. TEST_F(MindDataTestPipeline, TestMnistFailWithNullSamplerFail) {
  291. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestMnistFailWithNullSamplerFail.";
  292. // Create a Mnist Dataset
  293. std::string folder_path = datasets_root_path_ + "/testMnistData/";
  294. std::shared_ptr<Dataset> ds = Mnist(folder_path, "all", nullptr);
  295. EXPECT_NE(ds, nullptr);
  296. // Create an iterator over the result of the above dataset
  297. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  298. // Expect failure: invalid Mnist input, sampler cannot be nullptr
  299. EXPECT_EQ(iter, nullptr);
  300. }
  301. TEST_F(MindDataTestPipeline, TestImageFolderClassIndexDatasetSize) {
  302. std::string folder_path = datasets_root_path_ + "/testPK/data";
  303. std::map<std::string, int32_t> class_index;
  304. class_index["class1"] = 111;
  305. class_index["class2"] = 333;
  306. auto ds = ImageFolder(folder_path, false, std::make_shared<RandomSampler>(), {}, class_index);
  307. EXPECT_EQ(ds->GetNumClasses(), 2);
  308. }
  309. TEST_F(MindDataTestPipeline, TestImageFolderClassIndexDatasetSizeFail) {
  310. std::string folder_path = datasets_root_path_ + "/testPK/data";
  311. std::map<std::string, int32_t> class_index;
  312. class_index["class1"] = 111;
  313. class_index["wrong class"] = 333;
  314. auto ds = ImageFolder(folder_path, false, std::make_shared<RandomSampler>(), {}, class_index);
  315. EXPECT_EQ(ds->GetNumClasses(), -1);
  316. }