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_cache_test.cc 32 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945
  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/include/dataset/vision.h"
  19. using namespace mindspore::dataset;
  20. // Helper function to get the session id from SESSION_ID env variable
  21. Status GetSessionFromEnv(session_id_type *session_id);
  22. class MindDataTestCacheOp : public UT::DatasetOpTesting {
  23. public:
  24. void SetUp() override { DatasetOpTesting::SetUp(); }
  25. };
  26. TEST_F(MindDataTestCacheOp, DISABLED_TestCacheCApiSamplerNull) {
  27. session_id_type env_session;
  28. Status s = GetSessionFromEnv(&env_session);
  29. EXPECT_EQ(s, Status::OK());
  30. std::shared_ptr<DatasetCache> some_cache = CreateDatasetCache(env_session, 0, false, "127.0.0.1", 50053, 1, 1);
  31. EXPECT_NE(some_cache, nullptr);
  32. // Create an ImageFolder Dataset, this folder_path only has 2 images in it
  33. std::string folder_path = datasets_root_path_ + "/testImageNetData/train/";
  34. std::shared_ptr<Dataset> ds = ImageFolder(folder_path, false, nullptr, {}, {}, some_cache);
  35. EXPECT_NE(ds, nullptr);
  36. // Create an iterator over the result of the above dataset
  37. // This will trigger the creation of the Execution Tree and launch it.
  38. // Now the parameter check for ImageFolderNode would fail and we would end up with a nullptr iter.
  39. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  40. EXPECT_EQ(iter, nullptr);
  41. }
  42. TEST_F(MindDataTestCacheOp, DISABLED_TestCacheCApiNestedCache) {
  43. session_id_type env_session;
  44. Status s = GetSessionFromEnv(&env_session);
  45. EXPECT_EQ(s, Status::OK());
  46. std::shared_ptr<DatasetCache> some_cache = CreateDatasetCache(env_session, 0, false);
  47. EXPECT_NE(some_cache, nullptr);
  48. // Create an ImageFolder Dataset, this folder_path only has 2 images in it
  49. std::string folder_path = datasets_root_path_ + "/testImageNetData/train/";
  50. std::shared_ptr<Dataset> ds = ImageFolder(folder_path, false, std::make_shared<RandomSampler>(), {}, {}, some_cache);
  51. EXPECT_NE(ds, nullptr);
  52. // Create objects for the tensor ops
  53. std::shared_ptr<TensorTransform> decode_op = std::make_shared<vision::Decode>();
  54. EXPECT_NE(decode_op, nullptr);
  55. // Create a Map operation on ds
  56. ds = ds->Map({decode_op}, {}, {}, {"image"}, some_cache);
  57. EXPECT_NE(ds, nullptr);
  58. // Create an iterator over the result of the above dataset
  59. // This will trigger the creation of the Execution Tree and launch it.
  60. // Now in the cache_error_pass would fail and we would end up with a nullptr iter.
  61. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  62. EXPECT_EQ(iter, nullptr);
  63. }
  64. TEST_F(MindDataTestCacheOp, DISABLED_TestCacheImageFolderCApi) {
  65. session_id_type env_session;
  66. Status s = GetSessionFromEnv(&env_session);
  67. EXPECT_EQ(s, Status::OK());
  68. std::shared_ptr<DatasetCache> some_cache = CreateDatasetCache(env_session, 0, false);
  69. EXPECT_NE(some_cache, nullptr);
  70. // Create an ImageFolder Dataset, this folder_path only has 2 images in it
  71. std::string folder_path = datasets_root_path_ + "/testImageNetData/train/";
  72. std::shared_ptr<Dataset> ds = ImageFolder(folder_path, false, std::make_shared<RandomSampler>(), {}, {}, some_cache);
  73. EXPECT_NE(ds, nullptr);
  74. // Create a Repeat operation on ds
  75. int32_t repeat_num = 2;
  76. ds = ds->Repeat(repeat_num);
  77. EXPECT_NE(ds, nullptr);
  78. // Create an iterator over the result of the above dataset
  79. // This will trigger the creation of the Execution Tree and launch it.
  80. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  81. EXPECT_NE(iter, nullptr);
  82. // Iterate the dataset and get each row
  83. std::unordered_map<std::string, mindspore::MSTensor> row;
  84. ASSERT_OK(iter->GetNextRow(&row));
  85. uint64_t i = 0;
  86. while (row.size() != 0) {
  87. i++;
  88. auto image = row["image"];
  89. MS_LOG(INFO) << "Tensor image shape: " << image.Shape();
  90. ASSERT_OK(iter->GetNextRow(&row));
  91. }
  92. EXPECT_EQ(i, 4);
  93. // Manually terminate the pipeline
  94. iter->Stop();
  95. }
  96. TEST_F(MindDataTestCacheOp, DISABLED_TestCacheCocoCApi) {
  97. session_id_type env_session;
  98. Status s = GetSessionFromEnv(&env_session);
  99. EXPECT_EQ(s, Status::OK());
  100. std::shared_ptr<DatasetCache> some_cache = CreateDatasetCache(env_session, 0, false);
  101. EXPECT_NE(some_cache, nullptr);
  102. // Create a Coco Dataset, this folder_path has 6 images in it
  103. std::string folder_path = datasets_root_path_ + "/testCOCO/train/";
  104. std::string annotation_file_path = datasets_root_path_ + "/testCOCO/annotations/train.json";
  105. std::shared_ptr<Dataset> ds =
  106. Coco(folder_path, annotation_file_path, "Detection", false, std::make_shared<RandomSampler>(), some_cache);
  107. EXPECT_NE(ds, nullptr);
  108. // Create a Repeat operation on ds
  109. int32_t repeat_num = 2;
  110. ds = ds->Repeat(repeat_num);
  111. EXPECT_NE(ds, nullptr);
  112. // Create an iterator over the result of the above dataset
  113. // This will trigger the creation of the Execution Tree and launch it.
  114. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  115. EXPECT_NE(iter, nullptr);
  116. // Iterate the dataset and get each row
  117. std::unordered_map<std::string, mindspore::MSTensor> row;
  118. ASSERT_OK(iter->GetNextRow(&row));
  119. uint64_t i = 0;
  120. while (row.size() != 0) {
  121. i++;
  122. auto image = row["image"];
  123. MS_LOG(INFO) << "Tensor image shape: " << image.Shape();
  124. ASSERT_OK(iter->GetNextRow(&row));
  125. }
  126. EXPECT_EQ(i, 12);
  127. // Manually terminate the pipeline
  128. iter->Stop();
  129. }
  130. TEST_F(MindDataTestCacheOp, DISABLED_TestCacheMnistCApi) {
  131. session_id_type env_session;
  132. Status s = GetSessionFromEnv(&env_session);
  133. EXPECT_EQ(s, Status::OK());
  134. std::shared_ptr<DatasetCache> some_cache = CreateDatasetCache(env_session, 0, false);
  135. EXPECT_NE(some_cache, nullptr);
  136. // Create a Mnist Dataset
  137. std::string folder_path = datasets_root_path_ + "/testMnistData/";
  138. std::shared_ptr<Dataset> ds = Mnist(folder_path, "all", std::make_shared<RandomSampler>(false, 10), some_cache);
  139. EXPECT_NE(ds, nullptr);
  140. // Create a Repeat operation on ds
  141. int32_t repeat_num = 2;
  142. ds = ds->Repeat(repeat_num);
  143. EXPECT_NE(ds, nullptr);
  144. // Create an iterator over the result of the above dataset
  145. // This will trigger the creation of the Execution Tree and launch it.
  146. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  147. EXPECT_NE(iter, nullptr);
  148. // Iterate the dataset and get each row
  149. std::unordered_map<std::string, mindspore::MSTensor> row;
  150. ASSERT_OK(iter->GetNextRow(&row));
  151. uint64_t i = 0;
  152. while (row.size() != 0) {
  153. i++;
  154. auto image = row["image"];
  155. MS_LOG(INFO) << "Tensor image shape: " << image.Shape();
  156. ASSERT_OK(iter->GetNextRow(&row));
  157. }
  158. EXPECT_EQ(i, 20);
  159. // Manually terminate the pipeline
  160. iter->Stop();
  161. }
  162. TEST_F(MindDataTestCacheOp, DISABLED_TestCacheCelebaCApi) {
  163. session_id_type env_session;
  164. Status s = GetSessionFromEnv(&env_session);
  165. EXPECT_EQ(s, Status::OK());
  166. std::shared_ptr<DatasetCache> some_cache = CreateDatasetCache(env_session, 0, false);
  167. EXPECT_NE(some_cache, nullptr);
  168. // Create a CelebA Dataset, this folder_path has 4 records in it
  169. std::string folder_path = datasets_root_path_ + "/testCelebAData/";
  170. std::shared_ptr<Dataset> ds =
  171. CelebA(folder_path, "all", std::make_shared<RandomSampler>(false, 10), false, {}, some_cache);
  172. EXPECT_NE(ds, nullptr);
  173. // Create a Repeat operation on ds
  174. int32_t repeat_num = 2;
  175. ds = ds->Repeat(repeat_num);
  176. EXPECT_NE(ds, nullptr);
  177. // Create an iterator over the result of the above dataset
  178. // This will trigger the creation of the Execution Tree and launch it.
  179. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  180. EXPECT_NE(iter, nullptr);
  181. // Iterate the dataset and get each row
  182. std::unordered_map<std::string, mindspore::MSTensor> row;
  183. ASSERT_OK(iter->GetNextRow(&row));
  184. uint64_t i = 0;
  185. while (row.size() != 0) {
  186. i++;
  187. auto image = row["image"];
  188. MS_LOG(INFO) << "Tensor image shape: " << image.Shape();
  189. ASSERT_OK(iter->GetNextRow(&row));
  190. }
  191. EXPECT_EQ(i, 8);
  192. // Manually terminate the pipeline
  193. iter->Stop();
  194. }
  195. TEST_F(MindDataTestCacheOp, DISABLED_TestCacheManifestCApi) {
  196. session_id_type env_session;
  197. Status s = GetSessionFromEnv(&env_session);
  198. EXPECT_EQ(s, Status::OK());
  199. std::shared_ptr<DatasetCache> some_cache = CreateDatasetCache(env_session, 0, false);
  200. EXPECT_NE(some_cache, nullptr);
  201. // Create a Manifest Dataset, this file_path has 2 records in it
  202. std::string file_path = datasets_root_path_ + "/testManifestData/cpp.json";
  203. std::shared_ptr<Dataset> ds = Manifest(file_path, "train", std::make_shared<RandomSampler>(), {}, false, some_cache);
  204. EXPECT_NE(ds, nullptr);
  205. // Create a Repeat operation on ds
  206. int32_t repeat_num = 2;
  207. ds = ds->Repeat(repeat_num);
  208. EXPECT_NE(ds, nullptr);
  209. // Create an iterator over the result of the above dataset
  210. // This will trigger the creation of the Execution Tree and launch it.
  211. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  212. EXPECT_NE(iter, nullptr);
  213. // Iterate the dataset and get each row
  214. std::unordered_map<std::string, mindspore::MSTensor> row;
  215. ASSERT_OK(iter->GetNextRow(&row));
  216. uint64_t i = 0;
  217. while (row.size() != 0) {
  218. i++;
  219. auto image = row["image"];
  220. MS_LOG(INFO) << "Tensor image shape: " << image.Shape();
  221. ASSERT_OK(iter->GetNextRow(&row));
  222. }
  223. EXPECT_EQ(i, 4);
  224. // Manually terminate the pipeline
  225. iter->Stop();
  226. }
  227. TEST_F(MindDataTestCacheOp, DISABLED_TestCacheCifar10CApi) {
  228. session_id_type env_session;
  229. Status s = GetSessionFromEnv(&env_session);
  230. EXPECT_EQ(s, Status::OK());
  231. std::shared_ptr<DatasetCache> some_cache = CreateDatasetCache(env_session, 0, false);
  232. EXPECT_NE(some_cache, nullptr);
  233. // Create a Cifar10 Dataset
  234. std::string folder_path = datasets_root_path_ + "/testCifar10Data/";
  235. std::shared_ptr<Dataset> ds = Cifar10(folder_path, "all", std::make_shared<RandomSampler>(false, 10), some_cache);
  236. EXPECT_NE(ds, nullptr);
  237. // Create a Repeat operation on ds
  238. int32_t repeat_num = 2;
  239. ds = ds->Repeat(repeat_num);
  240. EXPECT_NE(ds, nullptr);
  241. // Create an iterator over the result of the above dataset
  242. // This will trigger the creation of the Execution Tree and launch it.
  243. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  244. EXPECT_NE(iter, nullptr);
  245. // Iterate the dataset and get each row
  246. std::unordered_map<std::string, mindspore::MSTensor> row;
  247. ASSERT_OK(iter->GetNextRow(&row));
  248. uint64_t i = 0;
  249. while (row.size() != 0) {
  250. i++;
  251. auto image = row["image"];
  252. MS_LOG(INFO) << "Tensor image shape: " << image.Shape();
  253. ASSERT_OK(iter->GetNextRow(&row));
  254. }
  255. EXPECT_EQ(i, 20);
  256. // Manually terminate the pipeline
  257. iter->Stop();
  258. }
  259. TEST_F(MindDataTestCacheOp, DISABLED_TestCacheCifar100CApi) {
  260. session_id_type env_session;
  261. Status s = GetSessionFromEnv(&env_session);
  262. EXPECT_EQ(s, Status::OK());
  263. std::shared_ptr<DatasetCache> some_cache = CreateDatasetCache(env_session, 0, false);
  264. EXPECT_NE(some_cache, nullptr);
  265. // Create a Cifar100 Dataset
  266. std::string folder_path = datasets_root_path_ + "/testCifar100Data/";
  267. std::shared_ptr<Dataset> ds = Cifar100(folder_path, "all", std::make_shared<RandomSampler>(false, 10), some_cache);
  268. EXPECT_NE(ds, nullptr);
  269. // Create a Repeat operation on ds
  270. int32_t repeat_num = 2;
  271. ds = ds->Repeat(repeat_num);
  272. EXPECT_NE(ds, nullptr);
  273. // Create an iterator over the result of the above dataset
  274. // This will trigger the creation of the Execution Tree and launch it.
  275. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  276. EXPECT_NE(iter, nullptr);
  277. // Iterate the dataset and get each row
  278. std::unordered_map<std::string, mindspore::MSTensor> row;
  279. ASSERT_OK(iter->GetNextRow(&row));
  280. uint64_t i = 0;
  281. while (row.size() != 0) {
  282. i++;
  283. auto image = row["image"];
  284. MS_LOG(INFO) << "Tensor image shape: " << image.Shape();
  285. ASSERT_OK(iter->GetNextRow(&row));
  286. }
  287. EXPECT_EQ(i, 20);
  288. // Manually terminate the pipeline
  289. iter->Stop();
  290. }
  291. TEST_F(MindDataTestCacheOp, DISABLED_TestCacheVocCApi) {
  292. session_id_type env_session;
  293. Status s = GetSessionFromEnv(&env_session);
  294. EXPECT_EQ(s, Status::OK());
  295. std::shared_ptr<DatasetCache> some_cache = CreateDatasetCache(env_session, 0, false);
  296. EXPECT_NE(some_cache, nullptr);
  297. // Create a VOC Dataset, this folder_path has 9 records in it
  298. std::string folder_path = datasets_root_path_ + "/testVOC2012/";
  299. std::shared_ptr<Dataset> ds =
  300. VOC(folder_path, "Detection", "train", {}, false, std::make_shared<RandomSampler>(), some_cache);
  301. EXPECT_NE(ds, nullptr);
  302. // Create a Repeat operation on ds
  303. int32_t repeat_num = 2;
  304. ds = ds->Repeat(repeat_num);
  305. EXPECT_NE(ds, nullptr);
  306. // Create an iterator over the result of the above dataset
  307. // This will trigger the creation of the Execution Tree and launch it.
  308. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  309. EXPECT_NE(iter, nullptr);
  310. // Iterate the dataset and get each row
  311. std::unordered_map<std::string, mindspore::MSTensor> row;
  312. ASSERT_OK(iter->GetNextRow(&row));
  313. uint64_t i = 0;
  314. while (row.size() != 0) {
  315. i++;
  316. auto image = row["image"];
  317. MS_LOG(INFO) << "Tensor image shape: " << image.Shape();
  318. ASSERT_OK(iter->GetNextRow(&row));
  319. }
  320. EXPECT_EQ(i, 18);
  321. // Manually terminate the pipeline
  322. iter->Stop();
  323. }
  324. TEST_F(MindDataTestCacheOp, DISABLED_TestCacheAlbumCApi) {
  325. session_id_type env_session;
  326. Status s = GetSessionFromEnv(&env_session);
  327. EXPECT_EQ(s, Status::OK());
  328. std::shared_ptr<DatasetCache> some_cache = CreateDatasetCache(env_session, 0, false);
  329. EXPECT_NE(some_cache, nullptr);
  330. std::string folder_path = datasets_root_path_ + "/testAlbum/images";
  331. std::string schema_file = datasets_root_path_ + "/testAlbum/datasetSchema.json";
  332. std::vector<std::string> column_names = {"image", "label", "id"};
  333. // Create a Album Dataset, 7 records in it
  334. std::shared_ptr<Dataset> ds =
  335. Album(folder_path, schema_file, column_names, false, std::make_shared<RandomSampler>(), some_cache);
  336. EXPECT_NE(ds, nullptr);
  337. // Create a Repeat operation on ds
  338. int32_t repeat_num = 2;
  339. ds = ds->Repeat(repeat_num);
  340. EXPECT_NE(ds, nullptr);
  341. // Create an iterator over the result of the above dataset
  342. // This will trigger the creation of the Execution Tree and launch it.
  343. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  344. EXPECT_NE(iter, nullptr);
  345. // Iterate the dataset and get each row
  346. std::unordered_map<std::string, mindspore::MSTensor> row;
  347. ASSERT_OK(iter->GetNextRow(&row));
  348. uint64_t i = 0;
  349. while (row.size() != 0) {
  350. i++;
  351. ASSERT_OK(iter->GetNextRow(&row));
  352. }
  353. EXPECT_EQ(i, 14);
  354. // Manually terminate the pipeline
  355. iter->Stop();
  356. }
  357. TEST_F(MindDataTestCacheOp, DISABLED_TestCacheMindRecordCApi) {
  358. session_id_type env_session;
  359. Status s = GetSessionFromEnv(&env_session);
  360. EXPECT_EQ(s, Status::OK());
  361. std::shared_ptr<DatasetCache> some_cache = CreateDatasetCache(env_session, 0, false);
  362. EXPECT_NE(some_cache, nullptr);
  363. // Create a MindData Dataset
  364. // Pass one mindrecord shard file to parse dataset info, and search for other mindrecord files with same dataset info,
  365. // thus all records in imagenet.mindrecord0 ~ imagenet.mindrecord3 will be read
  366. std::string file_path = datasets_root_path_ + "/../mindrecord/testMindDataSet/testImageNetData/imagenet.mindrecord0";
  367. // Create a MindRecord Dataset, 20 records in it
  368. std::shared_ptr<Dataset> ds = MindData(file_path, {}, std::make_shared<RandomSampler>(), nullptr, 0, some_cache);
  369. EXPECT_NE(ds, nullptr);
  370. // Create an iterator over the result of the above dataset
  371. // This will trigger the creation of the Execution Tree and launch it.
  372. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  373. EXPECT_NE(iter, nullptr);
  374. // Iterate the dataset and get each row
  375. std::unordered_map<std::string, mindspore::MSTensor> row;
  376. ASSERT_OK(iter->GetNextRow(&row));
  377. uint64_t i = 0;
  378. while (row.size() != 0) {
  379. i++;
  380. ASSERT_OK(iter->GetNextRow(&row));
  381. }
  382. EXPECT_EQ(i, 20);
  383. // Manually terminate the pipeline
  384. iter->Stop();
  385. }
  386. TEST_F(MindDataTestCacheOp, DISABLED_TestCacheRandomDataCApi) {
  387. session_id_type env_session;
  388. Status s = GetSessionFromEnv(&env_session);
  389. EXPECT_EQ(s, Status::OK());
  390. std::shared_ptr<DatasetCache> some_cache = CreateDatasetCache(env_session, 0, false);
  391. EXPECT_NE(some_cache, nullptr);
  392. // Create a RandomDataset
  393. std::shared_ptr<SchemaObj> schema = Schema();
  394. ASSERT_OK(schema->add_column("image", mindspore::DataType::kNumberTypeUInt8, {2}));
  395. ASSERT_OK(schema->add_column("label", mindspore::DataType::kNumberTypeUInt8, {1}));
  396. std::shared_ptr<Dataset> ds = RandomData(8, schema, {}, some_cache);
  397. EXPECT_NE(ds, nullptr);
  398. // Create a Repeat operation on ds
  399. int32_t repeat_num = 2;
  400. ds = ds->Repeat(repeat_num);
  401. EXPECT_NE(ds, nullptr);
  402. // Create an iterator over the result of the above dataset
  403. // This will trigger the creation of the Execution Tree and launch it.
  404. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  405. EXPECT_NE(iter, nullptr);
  406. // Iterate the dataset and get each row
  407. std::unordered_map<std::string, mindspore::MSTensor> row;
  408. ASSERT_OK(iter->GetNextRow(&row));
  409. uint64_t i = 0;
  410. while (row.size() != 0) {
  411. i++;
  412. ASSERT_OK(iter->GetNextRow(&row));
  413. }
  414. EXPECT_EQ(i, 16);
  415. // Manually terminate the pipeline
  416. iter->Stop();
  417. }
  418. TEST_F(MindDataTestCacheOp, DISABLED_TestCacheTFRecordCApi1) {
  419. session_id_type env_session;
  420. Status s = GetSessionFromEnv(&env_session);
  421. EXPECT_EQ(s, Status::OK());
  422. std::shared_ptr<DatasetCache> some_cache = CreateDatasetCache(env_session, 0, false);
  423. EXPECT_NE(some_cache, nullptr);
  424. // Create a TFRecord Dataset, this file_path has 3 records in it
  425. std::string file_path = datasets_root_path_ + "/test_tf_file_3_images2/train-0000-of-0001.data";
  426. std::string schema_path = datasets_root_path_ + "/test_tf_file_3_images2/datasetSchema.json";
  427. std::shared_ptr<Dataset> ds =
  428. TFRecord({file_path}, schema_path, {"image"}, 0, ShuffleMode::kFalse, 1, 0, false, some_cache);
  429. EXPECT_NE(ds, nullptr);
  430. // Create a Repeat operation on ds
  431. int32_t repeat_num = 2;
  432. ds = ds->Repeat(repeat_num);
  433. EXPECT_NE(ds, nullptr);
  434. // Create an iterator over the result of the above dataset
  435. // This will trigger the creation of the Execution Tree and launch it.
  436. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  437. EXPECT_NE(iter, nullptr);
  438. // Iterate the dataset and get each row
  439. std::unordered_map<std::string, mindspore::MSTensor> row;
  440. ASSERT_OK(iter->GetNextRow(&row));
  441. uint64_t i = 0;
  442. while (row.size() != 0) {
  443. i++;
  444. auto image = row["image"];
  445. MS_LOG(INFO) << "Tensor image shape: " << image.Shape();
  446. ASSERT_OK(iter->GetNextRow(&row));
  447. }
  448. EXPECT_EQ(i, 6);
  449. // Manually terminate the pipeline
  450. iter->Stop();
  451. }
  452. TEST_F(MindDataTestCacheOp, DISABLED_TestCacheTFRecordCApi2) {
  453. session_id_type env_session;
  454. Status s = GetSessionFromEnv(&env_session);
  455. EXPECT_EQ(s, Status::OK());
  456. std::shared_ptr<DatasetCache> some_cache = CreateDatasetCache(env_session, 0, false);
  457. EXPECT_NE(some_cache, nullptr);
  458. // Create a TFRecord Dataset, this file_path has 3 records in it
  459. std::string file_path = datasets_root_path_ + "/test_tf_file_3_images2/train-0000-of-0001.data";
  460. std::string schema_path = datasets_root_path_ + "/test_tf_file_3_images2/datasetSchema.json";
  461. // In this one, the TFRecord dataset will be given sharding configuration, however since a cache is
  462. // used, the tree prepare should undo the sharding configuration and instead, a distributed
  463. // sampler will be chosen with the same shard config.
  464. // With only 3 records shard into 3, we expect only 1 record returned for this shard
  465. // However, the sharding will be done by the sampler, not by the TFRecord leaf node
  466. // In this case, it is a row-based sharding, not the file-based sharding that would happen if
  467. // there was not any cache.
  468. std::shared_ptr<Dataset> ds =
  469. TFRecord({file_path}, schema_path, {"image"}, 0, ShuffleMode::kFalse, 3, 0, false, some_cache);
  470. EXPECT_NE(ds, nullptr);
  471. // Create a Repeat operation on ds
  472. int32_t repeat_num = 2;
  473. ds = ds->Repeat(repeat_num);
  474. EXPECT_NE(ds, nullptr);
  475. // Create an iterator over the result of the above dataset
  476. // This will trigger the creation of the Execution Tree and launch it.
  477. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  478. EXPECT_NE(iter, nullptr);
  479. // Iterate the dataset and get each row
  480. std::unordered_map<std::string, mindspore::MSTensor> row;
  481. ASSERT_OK(iter->GetNextRow(&row));
  482. uint64_t i = 0;
  483. while (row.size() != 0) {
  484. i++;
  485. auto image = row["image"];
  486. MS_LOG(INFO) << "Tensor image shape: " << image.Shape();
  487. ASSERT_OK(iter->GetNextRow(&row));
  488. }
  489. EXPECT_EQ(i, 2);
  490. // Manually terminate the pipeline
  491. iter->Stop();
  492. }
  493. TEST_F(MindDataTestCacheOp, DISABLED_TestCacheTFRecordCApi3) {
  494. session_id_type env_session;
  495. Status s = GetSessionFromEnv(&env_session);
  496. EXPECT_EQ(s, Status::OK());
  497. std::shared_ptr<DatasetCache> some_cache = CreateDatasetCache(env_session, 0, false);
  498. EXPECT_NE(some_cache, nullptr);
  499. // Create a TFRecord Dataset, this file_path has 3 records in it
  500. std::string file_path = datasets_root_path_ + "/test_tf_file_3_images2/train-0000-of-0001.data";
  501. std::string schema_path = datasets_root_path_ + "/test_tf_file_3_images2/datasetSchema.json";
  502. // In this one, a num_samples argument is given.
  503. // In this case, a sequential sampler would be chosen with the same num_samples argument.
  504. // The samples will be selected by the sequential sampler, not by the TFRecord leaf node.
  505. std::shared_ptr<Dataset> ds =
  506. TFRecord({file_path}, schema_path, {"image"}, 2, ShuffleMode::kFalse, 1, 0, false, some_cache);
  507. EXPECT_NE(ds, nullptr);
  508. // Create a Repeat operation on ds
  509. int32_t repeat_num = 2;
  510. ds = ds->Repeat(repeat_num);
  511. EXPECT_NE(ds, nullptr);
  512. // Create an iterator over the result of the above dataset
  513. // This will trigger the creation of the Execution Tree and launch it.
  514. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  515. EXPECT_NE(iter, nullptr);
  516. // Iterate the dataset and get each row
  517. std::unordered_map<std::string, mindspore::MSTensor> row;
  518. ASSERT_OK(iter->GetNextRow(&row));
  519. uint64_t i = 0;
  520. while (row.size() != 0) {
  521. i++;
  522. auto image = row["image"];
  523. MS_LOG(INFO) << "Tensor image shape: " << image.Shape();
  524. ASSERT_OK(iter->GetNextRow(&row));
  525. }
  526. EXPECT_EQ(i, 4);
  527. // Manually terminate the pipeline
  528. iter->Stop();
  529. }
  530. TEST_F(MindDataTestCacheOp, DISABLED_TestCacheTextfileCApi) {
  531. session_id_type env_session;
  532. Status s = GetSessionFromEnv(&env_session);
  533. EXPECT_EQ(s, Status::OK());
  534. std::shared_ptr<DatasetCache> some_cache = CreateDatasetCache(env_session, 0, false);
  535. EXPECT_NE(some_cache, nullptr);
  536. // Create a TextFile Dataset, this file_path has 3 records in it
  537. std::string file_path = datasets_root_path_ + "/testTextFileDataset/1.txt";
  538. // In this one, a num_samples=2 argument is given.
  539. // In this case, a sequential sampler would be chosen with the same num_samples argument.
  540. // The samples will be selected by the sequential sampler, not by the TextFile leaf node.
  541. std::shared_ptr<Dataset> ds = TextFile({file_path}, 2, ShuffleMode::kGlobal, 1, 0, some_cache);
  542. EXPECT_NE(ds, nullptr);
  543. // Create a Repeat operation on ds
  544. int32_t repeat_num = 2;
  545. ds = ds->Repeat(repeat_num);
  546. EXPECT_NE(ds, nullptr);
  547. // Create an iterator over the result of the above dataset
  548. // This will trigger the creation of the Execution Tree and launch it.
  549. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  550. EXPECT_NE(iter, nullptr);
  551. // Iterate the dataset and get each row
  552. std::unordered_map<std::string, mindspore::MSTensor> row;
  553. ASSERT_OK(iter->GetNextRow(&row));
  554. uint64_t i = 0;
  555. while (row.size() != 0) {
  556. i++;
  557. ASSERT_OK(iter->GetNextRow(&row));
  558. }
  559. EXPECT_EQ(i, 4);
  560. // Manually terminate the pipeline
  561. iter->Stop();
  562. }
  563. TEST_F(MindDataTestCacheOp, DISABLED_TestCacheCsvCApi) {
  564. session_id_type env_session;
  565. Status s = GetSessionFromEnv(&env_session);
  566. EXPECT_EQ(s, Status::OK());
  567. std::shared_ptr<DatasetCache> some_cache = CreateDatasetCache(env_session, 0, false);
  568. EXPECT_NE(some_cache, nullptr);
  569. // Create a CSV Dataset, this file_path has 3 records in it
  570. std::string file_path = datasets_root_path_ + "/testCSV/1.csv";
  571. std::vector<std::string> column_names = {"col1", "col2", "col3", "col4"};
  572. // In this one, a num_samples=2 argument is given.
  573. // In this case, a sequential sampler would be chosen with the same num_samples argument.
  574. // The samples will be selected by the sequential sampler, not by the CSV leaf node.
  575. std::shared_ptr<Dataset> ds = CSV({file_path}, ',', {}, column_names, 2, ShuffleMode::kFalse, 1, 0, some_cache);
  576. EXPECT_NE(ds, nullptr);
  577. // Create a Repeat operation on ds
  578. int32_t repeat_num = 2;
  579. ds = ds->Repeat(repeat_num);
  580. EXPECT_NE(ds, nullptr);
  581. // Create an iterator over the result of the above dataset
  582. // This will trigger the creation of the Execution Tree and launch it.
  583. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  584. EXPECT_NE(iter, nullptr);
  585. // Iterate the dataset and get each row
  586. std::unordered_map<std::string, mindspore::MSTensor> row;
  587. ASSERT_OK(iter->GetNextRow(&row));
  588. uint64_t i = 0;
  589. while (row.size() != 0) {
  590. i++;
  591. ASSERT_OK(iter->GetNextRow(&row));
  592. }
  593. EXPECT_EQ(i, 4);
  594. // Manually terminate the pipeline
  595. iter->Stop();
  596. }
  597. TEST_F(MindDataTestCacheOp, DISABLED_TestCacheClueCApi) {
  598. session_id_type env_session;
  599. Status s = GetSessionFromEnv(&env_session);
  600. EXPECT_EQ(s, Status::OK());
  601. std::shared_ptr<DatasetCache> some_cache = CreateDatasetCache(env_session, 0, false);
  602. EXPECT_NE(some_cache, nullptr);
  603. // Create a CLUE Dataset, this file_path has 3 records in it
  604. std::string file_path = datasets_root_path_ + "/testCLUE/afqmc/train.json";
  605. std::string task = "AFQMC";
  606. std::string usage = "train";
  607. // In this one, a num_samples=2 argument is given.
  608. // In this case, a sequential sampler would be chosen with the same num_samples argument.
  609. // The samples will be selected by the sequential sampler, not by the CLUE leaf node.
  610. std::shared_ptr<Dataset> ds = CLUE({file_path}, task, usage, 2, ShuffleMode::kFalse, 1, 0, some_cache);
  611. EXPECT_NE(ds, nullptr);
  612. // Create a Repeat operation on ds
  613. int32_t repeat_num = 2;
  614. ds = ds->Repeat(repeat_num);
  615. EXPECT_NE(ds, nullptr);
  616. // Create an iterator over the result of the above dataset
  617. // This will trigger the creation of the Execution Tree and launch it.
  618. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  619. EXPECT_NE(iter, nullptr);
  620. // Iterate the dataset and get each row
  621. std::unordered_map<std::string, mindspore::MSTensor> row;
  622. ASSERT_OK(iter->GetNextRow(&row));
  623. uint64_t i = 0;
  624. while (row.size() != 0) {
  625. i++;
  626. ASSERT_OK(iter->GetNextRow(&row));
  627. }
  628. EXPECT_EQ(i, 4);
  629. // Manually terminate the pipeline
  630. iter->Stop();
  631. }
  632. TEST_F(MindDataTestCacheOp, DISABLED_TestCApiCacheShare1) {
  633. session_id_type env_session;
  634. Status s = GetSessionFromEnv(&env_session);
  635. EXPECT_EQ(s, Status::OK());
  636. std::shared_ptr<DatasetCache> some_cache = CreateDatasetCache(env_session, 0, false);
  637. EXPECT_NE(some_cache, nullptr);
  638. // Create an ImageFolder Dataset, this folder_path only has 2 images in it
  639. std::string folder_path = datasets_root_path_ + "/testImageNetData/train/";
  640. std::shared_ptr<Dataset> ds1 = ImageFolder(folder_path, false, std::make_shared<RandomSampler>(), {}, {}, some_cache);
  641. EXPECT_NE(ds1, nullptr);
  642. std::shared_ptr<Dataset> ds2 = ImageFolder(folder_path, false, std::make_shared<RandomSampler>(), {}, {}, some_cache);
  643. EXPECT_NE(ds2, nullptr);
  644. // Create and launch the Execution Tree for ds1
  645. std::shared_ptr<Iterator> iter1 = ds1->CreateIterator();
  646. EXPECT_NE(iter1, nullptr);
  647. // Iterate the dataset and get each row
  648. std::unordered_map<std::string, mindspore::MSTensor> row;
  649. ASSERT_OK(iter1->GetNextRow(&row));
  650. uint64_t i = 0;
  651. while (row.size() != 0) {
  652. i++;
  653. auto image = row["image"];
  654. MS_LOG(INFO) << "Tensor image shape: " << image.Shape();
  655. ASSERT_OK(iter1->GetNextRow(&row));
  656. }
  657. EXPECT_EQ(i, 2);
  658. // Manually terminate the pipeline
  659. iter1->Stop();
  660. // Create and launch the Execution Tree for ds2
  661. std::shared_ptr<Iterator> iter2 = ds2->CreateIterator();
  662. EXPECT_NE(iter2, nullptr);
  663. // Iterate the dataset and get each row
  664. ASSERT_OK(iter2->GetNextRow(&row));
  665. i = 0;
  666. while (row.size() != 0) {
  667. i++;
  668. auto image = row["image"];
  669. MS_LOG(INFO) << "Tensor image shape: " << image.Shape();
  670. ASSERT_OK(iter2->GetNextRow(&row));
  671. }
  672. EXPECT_EQ(i, 2);
  673. // Manually terminate the pipeline
  674. iter2->Stop();
  675. }
  676. TEST_F(MindDataTestCacheOp, DISABLED_TestCApiCacheShare2) {
  677. session_id_type env_session;
  678. Status s = GetSessionFromEnv(&env_session);
  679. EXPECT_EQ(s, Status::OK());
  680. std::shared_ptr<DatasetCache> some_cache = CreateDatasetCache(env_session, 0, false);
  681. EXPECT_NE(some_cache, nullptr);
  682. // Create an ImageFolder Dataset, this folder_path only has 2 images in it
  683. std::string folder_path = datasets_root_path_ + "/testImageNetData/train/";
  684. // The first pipeline is ImageFolder with RandomSampler, the second pipeline is ImageFolder with SequentialSampler
  685. // Since sampler does not influence the data in the source, these two pipelines can share a common cache.
  686. std::shared_ptr<Dataset> ds1 = ImageFolder(folder_path, true, std::make_shared<RandomSampler>(), {}, {}, some_cache);
  687. EXPECT_NE(ds1, nullptr);
  688. std::shared_ptr<Dataset> ds2 =
  689. ImageFolder(folder_path, true, std::make_shared<SequentialSampler>(), {}, {}, some_cache);
  690. EXPECT_NE(ds2, nullptr);
  691. // Create and launch the Execution Tree for ds1
  692. std::shared_ptr<Iterator> iter1 = ds1->CreateIterator();
  693. EXPECT_NE(iter1, nullptr);
  694. // Iterate the dataset and get each row
  695. std::unordered_map<std::string, mindspore::MSTensor> row;
  696. ASSERT_OK(iter1->GetNextRow(&row));
  697. uint64_t i = 0;
  698. while (row.size() != 0) {
  699. i++;
  700. auto image = row["image"];
  701. ASSERT_OK(iter1->GetNextRow(&row));
  702. }
  703. EXPECT_EQ(i, 2);
  704. // Manually terminate the pipeline
  705. iter1->Stop();
  706. // Create and launch the Execution Tree for ds2
  707. std::shared_ptr<Iterator> iter2 = ds2->CreateIterator();
  708. EXPECT_NE(iter2, nullptr);
  709. // Iterate the dataset and get each row
  710. ASSERT_OK(iter2->GetNextRow(&row));
  711. i = 0;
  712. while (row.size() != 0) {
  713. i++;
  714. auto image = row["image"];
  715. ASSERT_OK(iter2->GetNextRow(&row));
  716. }
  717. EXPECT_EQ(i, 2);
  718. // Manually terminate the pipeline
  719. iter2->Stop();
  720. }
  721. TEST_F(MindDataTestCacheOp, DISABLED_TestCApiCacheShareFailure1) {
  722. session_id_type env_session;
  723. Status s = GetSessionFromEnv(&env_session);
  724. EXPECT_EQ(s, Status::OK());
  725. std::shared_ptr<DatasetCache> some_cache = CreateDatasetCache(env_session, 0, false);
  726. EXPECT_NE(some_cache, nullptr);
  727. // Create an ImageFolder Dataset, this folder_path only has 2 images in it
  728. std::string folder_path = datasets_root_path_ + "/testImageNetData/train/";
  729. std::shared_ptr<Dataset> ds1 = ImageFolder(folder_path, true, std::make_shared<RandomSampler>(), {}, {}, some_cache);
  730. EXPECT_NE(ds1, nullptr);
  731. std::shared_ptr<Dataset> ds2 = ImageFolder(folder_path, false, std::make_shared<RandomSampler>(), {}, {}, some_cache);
  732. EXPECT_NE(ds2, nullptr);
  733. // Create and launch the Execution Tree for ds1
  734. std::shared_ptr<Iterator> iter1 = ds1->CreateIterator();
  735. EXPECT_NE(iter1, nullptr);
  736. // Iterate the dataset and get each row
  737. std::unordered_map<std::string, mindspore::MSTensor> row;
  738. ASSERT_OK(iter1->GetNextRow(&row));
  739. uint64_t i = 0;
  740. while (row.size() != 0) {
  741. i++;
  742. auto image = row["image"];
  743. ASSERT_OK(iter1->GetNextRow(&row));
  744. }
  745. EXPECT_EQ(i, 2);
  746. // Manually terminate the pipeline
  747. iter1->Stop();
  748. // Re-use a cache for the second pipeline would fail
  749. std::shared_ptr<Iterator> iter2 = ds2->CreateIterator();
  750. EXPECT_EQ(iter2, nullptr);
  751. }