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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907
  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/include/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, true, "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, true);
  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, true);
  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. 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. 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, true);
  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. 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. 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, true);
  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. 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. 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, true);
  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. 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. 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, true);
  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. 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. 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, true);
  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. 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. 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, true);
  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. 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. 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, true);
  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. 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. 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, true);
  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. iter->GetNextRow(&row);
  348. uint64_t i = 0;
  349. while (row.size() != 0) {
  350. i++;
  351. iter->GetNextRow(&row);
  352. }
  353. EXPECT_EQ(i, 14);
  354. // Manually terminate the pipeline
  355. iter->Stop();
  356. }
  357. TEST_F(MindDataTestCacheOp, DISABLED_TestCacheRandomDataCApi) {
  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, true);
  362. EXPECT_NE(some_cache, nullptr);
  363. // Create a RandomDataset
  364. std::shared_ptr<SchemaObj> schema = Schema();
  365. schema->add_column("image", mindspore::DataType::kNumberTypeUInt8, {2});
  366. schema->add_column("label", mindspore::DataType::kNumberTypeUInt8, {1});
  367. std::shared_ptr<Dataset> ds = RandomData(8, schema, {}, some_cache);
  368. EXPECT_NE(ds, nullptr);
  369. // Create a Repeat operation on ds
  370. int32_t repeat_num = 2;
  371. ds = ds->Repeat(repeat_num);
  372. EXPECT_NE(ds, nullptr);
  373. // Create an iterator over the result of the above dataset
  374. // This will trigger the creation of the Execution Tree and launch it.
  375. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  376. EXPECT_NE(iter, nullptr);
  377. // Iterate the dataset and get each row
  378. std::unordered_map<std::string, mindspore::MSTensor> row;
  379. iter->GetNextRow(&row);
  380. uint64_t i = 0;
  381. while (row.size() != 0) {
  382. i++;
  383. iter->GetNextRow(&row);
  384. }
  385. EXPECT_EQ(i, 16);
  386. // Manually terminate the pipeline
  387. iter->Stop();
  388. }
  389. TEST_F(MindDataTestCacheOp, DISABLED_TestCacheTFRecordCApi1) {
  390. session_id_type env_session;
  391. Status s = GetSessionFromEnv(&env_session);
  392. EXPECT_EQ(s, Status::OK());
  393. std::shared_ptr<DatasetCache> some_cache = CreateDatasetCache(env_session, 0, true);
  394. EXPECT_NE(some_cache, nullptr);
  395. // Create a TFRecord Dataset, this file_path has 3 records in it
  396. std::string file_path = datasets_root_path_ + "/test_tf_file_3_images2/train-0000-of-0001.data";
  397. std::string schema_path = datasets_root_path_ + "/test_tf_file_3_images2/datasetSchema.json";
  398. std::shared_ptr<Dataset> ds =
  399. TFRecord({file_path}, schema_path, {"image"}, 0, ShuffleMode::kFalse, 1, 0, false, some_cache);
  400. EXPECT_NE(ds, nullptr);
  401. // Create a Repeat operation on ds
  402. int32_t repeat_num = 2;
  403. ds = ds->Repeat(repeat_num);
  404. EXPECT_NE(ds, nullptr);
  405. // Create an iterator over the result of the above dataset
  406. // This will trigger the creation of the Execution Tree and launch it.
  407. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  408. EXPECT_NE(iter, nullptr);
  409. // Iterate the dataset and get each row
  410. std::unordered_map<std::string, mindspore::MSTensor> row;
  411. iter->GetNextRow(&row);
  412. uint64_t i = 0;
  413. while (row.size() != 0) {
  414. i++;
  415. auto image = row["image"];
  416. MS_LOG(INFO) << "Tensor image shape: " << image.Shape();
  417. iter->GetNextRow(&row);
  418. }
  419. EXPECT_EQ(i, 6);
  420. // Manually terminate the pipeline
  421. iter->Stop();
  422. }
  423. TEST_F(MindDataTestCacheOp, DISABLED_TestCacheTFRecordCApi2) {
  424. session_id_type env_session;
  425. Status s = GetSessionFromEnv(&env_session);
  426. EXPECT_EQ(s, Status::OK());
  427. std::shared_ptr<DatasetCache> some_cache = CreateDatasetCache(env_session, 0, true);
  428. EXPECT_NE(some_cache, nullptr);
  429. // Create a TFRecord Dataset, this file_path has 3 records in it
  430. std::string file_path = datasets_root_path_ + "/test_tf_file_3_images2/train-0000-of-0001.data";
  431. std::string schema_path = datasets_root_path_ + "/test_tf_file_3_images2/datasetSchema.json";
  432. // In this one, the TFRecord dataset will be given sharding configuration, however since a cache is
  433. // used, the tree prepare should undo the sharding configuration and instead, a distributed
  434. // sampler will be chosen with the same shard config.
  435. // With only 3 records shard into 3, we expect only 1 record returned for this shard
  436. // However, the sharding will be done by the sampler, not by the TFRecord leaf node
  437. // In this case, it is a row-based sharding, not the file-based sharding that would happen if
  438. // there was not any cache.
  439. std::shared_ptr<Dataset> ds =
  440. TFRecord({file_path}, schema_path, {"image"}, 0, ShuffleMode::kFalse, 3, 0, false, some_cache);
  441. EXPECT_NE(ds, nullptr);
  442. // Create a Repeat operation on ds
  443. int32_t repeat_num = 2;
  444. ds = ds->Repeat(repeat_num);
  445. EXPECT_NE(ds, nullptr);
  446. // Create an iterator over the result of the above dataset
  447. // This will trigger the creation of the Execution Tree and launch it.
  448. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  449. EXPECT_NE(iter, nullptr);
  450. // Iterate the dataset and get each row
  451. std::unordered_map<std::string, mindspore::MSTensor> row;
  452. iter->GetNextRow(&row);
  453. uint64_t i = 0;
  454. while (row.size() != 0) {
  455. i++;
  456. auto image = row["image"];
  457. MS_LOG(INFO) << "Tensor image shape: " << image.Shape();
  458. iter->GetNextRow(&row);
  459. }
  460. EXPECT_EQ(i, 2);
  461. // Manually terminate the pipeline
  462. iter->Stop();
  463. }
  464. TEST_F(MindDataTestCacheOp, DISABLED_TestCacheTFRecordCApi3) {
  465. session_id_type env_session;
  466. Status s = GetSessionFromEnv(&env_session);
  467. EXPECT_EQ(s, Status::OK());
  468. std::shared_ptr<DatasetCache> some_cache = CreateDatasetCache(env_session, 0, true);
  469. EXPECT_NE(some_cache, nullptr);
  470. // Create a TFRecord Dataset, this file_path has 3 records in it
  471. std::string file_path = datasets_root_path_ + "/test_tf_file_3_images2/train-0000-of-0001.data";
  472. std::string schema_path = datasets_root_path_ + "/test_tf_file_3_images2/datasetSchema.json";
  473. // In this one, a num_samples argument is given.
  474. // In this case, a sequential sampler would be chosen with the same num_samples argument.
  475. // The samples will be selected by the sequential sampler, not by the TFRecord leaf node.
  476. std::shared_ptr<Dataset> ds =
  477. TFRecord({file_path}, schema_path, {"image"}, 2, ShuffleMode::kFalse, 1, 0, false, some_cache);
  478. EXPECT_NE(ds, nullptr);
  479. // Create a Repeat operation on ds
  480. int32_t repeat_num = 2;
  481. ds = ds->Repeat(repeat_num);
  482. EXPECT_NE(ds, nullptr);
  483. // Create an iterator over the result of the above dataset
  484. // This will trigger the creation of the Execution Tree and launch it.
  485. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  486. EXPECT_NE(iter, nullptr);
  487. // Iterate the dataset and get each row
  488. std::unordered_map<std::string, mindspore::MSTensor> row;
  489. iter->GetNextRow(&row);
  490. uint64_t i = 0;
  491. while (row.size() != 0) {
  492. i++;
  493. auto image = row["image"];
  494. MS_LOG(INFO) << "Tensor image shape: " << image.Shape();
  495. iter->GetNextRow(&row);
  496. }
  497. EXPECT_EQ(i, 4);
  498. // Manually terminate the pipeline
  499. iter->Stop();
  500. }
  501. TEST_F(MindDataTestCacheOp, DISABLED_TestCacheTextfileCApi) {
  502. session_id_type env_session;
  503. Status s = GetSessionFromEnv(&env_session);
  504. EXPECT_EQ(s, Status::OK());
  505. std::shared_ptr<DatasetCache> some_cache = CreateDatasetCache(env_session, 0, true);
  506. EXPECT_NE(some_cache, nullptr);
  507. // Create a TextFile Dataset, this file_path has 3 records in it
  508. std::string file_path = datasets_root_path_ + "/testTextFileDataset/1.txt";
  509. // In this one, a num_samples=2 argument is given.
  510. // In this case, a sequential sampler would be chosen with the same num_samples argument.
  511. // The samples will be selected by the sequential sampler, not by the TextFile leaf node.
  512. std::shared_ptr<Dataset> ds = TextFile({file_path}, 2, ShuffleMode::kGlobal, 1, 0, some_cache);
  513. EXPECT_NE(ds, nullptr);
  514. // Create a Repeat operation on ds
  515. int32_t repeat_num = 2;
  516. ds = ds->Repeat(repeat_num);
  517. EXPECT_NE(ds, nullptr);
  518. // Create an iterator over the result of the above dataset
  519. // This will trigger the creation of the Execution Tree and launch it.
  520. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  521. EXPECT_NE(iter, nullptr);
  522. // Iterate the dataset and get each row
  523. std::unordered_map<std::string, mindspore::MSTensor> row;
  524. iter->GetNextRow(&row);
  525. uint64_t i = 0;
  526. while (row.size() != 0) {
  527. i++;
  528. iter->GetNextRow(&row);
  529. }
  530. EXPECT_EQ(i, 4);
  531. // Manually terminate the pipeline
  532. iter->Stop();
  533. }
  534. TEST_F(MindDataTestCacheOp, DISABLED_TestCacheCsvCApi) {
  535. session_id_type env_session;
  536. Status s = GetSessionFromEnv(&env_session);
  537. EXPECT_EQ(s, Status::OK());
  538. std::shared_ptr<DatasetCache> some_cache = CreateDatasetCache(env_session, 0, true);
  539. EXPECT_NE(some_cache, nullptr);
  540. // Create a CSV Dataset, this file_path has 3 records in it
  541. std::string file_path = datasets_root_path_ + "/testCSV/1.csv";
  542. std::vector<std::string> column_names = {"col1", "col2", "col3", "col4"};
  543. // In this one, a num_samples=2 argument is given.
  544. // In this case, a sequential sampler would be chosen with the same num_samples argument.
  545. // The samples will be selected by the sequential sampler, not by the CSV leaf node.
  546. std::shared_ptr<Dataset> ds = CSV({file_path}, ',', {}, column_names, 2, ShuffleMode::kFalse, 1, 0, some_cache);
  547. EXPECT_NE(ds, nullptr);
  548. // Create a Repeat operation on ds
  549. int32_t repeat_num = 2;
  550. ds = ds->Repeat(repeat_num);
  551. EXPECT_NE(ds, nullptr);
  552. // Create an iterator over the result of the above dataset
  553. // This will trigger the creation of the Execution Tree and launch it.
  554. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  555. EXPECT_NE(iter, nullptr);
  556. // Iterate the dataset and get each row
  557. std::unordered_map<std::string, mindspore::MSTensor> row;
  558. iter->GetNextRow(&row);
  559. uint64_t i = 0;
  560. while (row.size() != 0) {
  561. i++;
  562. iter->GetNextRow(&row);
  563. }
  564. EXPECT_EQ(i, 4);
  565. // Manually terminate the pipeline
  566. iter->Stop();
  567. }
  568. TEST_F(MindDataTestCacheOp, DISABLED_TestCacheClueCApi) {
  569. session_id_type env_session;
  570. Status s = GetSessionFromEnv(&env_session);
  571. EXPECT_EQ(s, Status::OK());
  572. std::shared_ptr<DatasetCache> some_cache = CreateDatasetCache(env_session, 0, true);
  573. EXPECT_NE(some_cache, nullptr);
  574. // Create a CLUE Dataset, this file_path has 3 records in it
  575. std::string file_path = datasets_root_path_ + "/testCLUE/afqmc/train.json";
  576. std::string task = "AFQMC";
  577. std::string usage = "train";
  578. // In this one, a num_samples=2 argument is given.
  579. // In this case, a sequential sampler would be chosen with the same num_samples argument.
  580. // The samples will be selected by the sequential sampler, not by the CLUE leaf node.
  581. std::shared_ptr<Dataset> ds = CLUE({file_path}, task, usage, 2, ShuffleMode::kFalse, 1, 0, some_cache);
  582. EXPECT_NE(ds, nullptr);
  583. // Create a Repeat operation on ds
  584. int32_t repeat_num = 2;
  585. ds = ds->Repeat(repeat_num);
  586. EXPECT_NE(ds, nullptr);
  587. // Create an iterator over the result of the above dataset
  588. // This will trigger the creation of the Execution Tree and launch it.
  589. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  590. EXPECT_NE(iter, nullptr);
  591. // Iterate the dataset and get each row
  592. std::unordered_map<std::string, mindspore::MSTensor> row;
  593. iter->GetNextRow(&row);
  594. uint64_t i = 0;
  595. while (row.size() != 0) {
  596. i++;
  597. iter->GetNextRow(&row);
  598. }
  599. EXPECT_EQ(i, 4);
  600. // Manually terminate the pipeline
  601. iter->Stop();
  602. }
  603. TEST_F(MindDataTestCacheOp, DISABLED_TestCApiCacheShare1) {
  604. session_id_type env_session;
  605. Status s = GetSessionFromEnv(&env_session);
  606. EXPECT_EQ(s, Status::OK());
  607. std::shared_ptr<DatasetCache> some_cache = CreateDatasetCache(env_session, 0, true);
  608. EXPECT_NE(some_cache, nullptr);
  609. // Create an ImageFolder Dataset, this folder_path only has 2 images in it
  610. std::string folder_path = datasets_root_path_ + "/testImageNetData/train/";
  611. std::shared_ptr<Dataset> ds1 = ImageFolder(folder_path, false, std::make_shared<RandomSampler>(), {}, {}, some_cache);
  612. EXPECT_NE(ds1, nullptr);
  613. std::shared_ptr<Dataset> ds2 = ImageFolder(folder_path, false, std::make_shared<RandomSampler>(), {}, {}, some_cache);
  614. EXPECT_NE(ds2, nullptr);
  615. // Create and launch the Execution Tree for ds1
  616. std::shared_ptr<Iterator> iter1 = ds1->CreateIterator();
  617. EXPECT_NE(iter1, nullptr);
  618. // Iterate the dataset and get each row
  619. std::unordered_map<std::string, mindspore::MSTensor> row;
  620. iter1->GetNextRow(&row);
  621. uint64_t i = 0;
  622. while (row.size() != 0) {
  623. i++;
  624. auto image = row["image"];
  625. MS_LOG(INFO) << "Tensor image shape: " << image.Shape();
  626. iter1->GetNextRow(&row);
  627. }
  628. EXPECT_EQ(i, 2);
  629. // Manually terminate the pipeline
  630. iter1->Stop();
  631. // Create and launch the Execution Tree for ds2
  632. std::shared_ptr<Iterator> iter2 = ds2->CreateIterator();
  633. EXPECT_NE(iter2, nullptr);
  634. // Iterate the dataset and get each row
  635. iter2->GetNextRow(&row);
  636. i = 0;
  637. while (row.size() != 0) {
  638. i++;
  639. auto image = row["image"];
  640. MS_LOG(INFO) << "Tensor image shape: " << image.Shape();
  641. iter2->GetNextRow(&row);
  642. }
  643. EXPECT_EQ(i, 2);
  644. // Manually terminate the pipeline
  645. iter2->Stop();
  646. }
  647. TEST_F(MindDataTestCacheOp, DISABLED_TestCApiCacheShare2) {
  648. session_id_type env_session;
  649. Status s = GetSessionFromEnv(&env_session);
  650. EXPECT_EQ(s, Status::OK());
  651. std::shared_ptr<DatasetCache> some_cache = CreateDatasetCache(env_session, 0, true);
  652. EXPECT_NE(some_cache, nullptr);
  653. // Create an ImageFolder Dataset, this folder_path only has 2 images in it
  654. std::string folder_path = datasets_root_path_ + "/testImageNetData/train/";
  655. // The first pipeline is ImageFolder with RandomSampler, the second pipeline is ImageFolder with SequentialSampler
  656. // Since sampler does not influence the data in the source, these two pipelines can share a common cache.
  657. std::shared_ptr<Dataset> ds1 = ImageFolder(folder_path, true, std::make_shared<RandomSampler>(), {}, {}, some_cache);
  658. EXPECT_NE(ds1, nullptr);
  659. std::shared_ptr<Dataset> ds2 =
  660. ImageFolder(folder_path, true, std::make_shared<SequentialSampler>(), {}, {}, some_cache);
  661. EXPECT_NE(ds2, nullptr);
  662. // Create and launch the Execution Tree for ds1
  663. std::shared_ptr<Iterator> iter1 = ds1->CreateIterator();
  664. EXPECT_NE(iter1, nullptr);
  665. // Iterate the dataset and get each row
  666. std::unordered_map<std::string, mindspore::MSTensor> row;
  667. iter1->GetNextRow(&row);
  668. uint64_t i = 0;
  669. while (row.size() != 0) {
  670. i++;
  671. auto image = row["image"];
  672. iter1->GetNextRow(&row);
  673. }
  674. EXPECT_EQ(i, 2);
  675. // Manually terminate the pipeline
  676. iter1->Stop();
  677. // Create and launch the Execution Tree for ds2
  678. std::shared_ptr<Iterator> iter2 = ds2->CreateIterator();
  679. EXPECT_NE(iter2, nullptr);
  680. // Iterate the dataset and get each row
  681. iter2->GetNextRow(&row);
  682. i = 0;
  683. while (row.size() != 0) {
  684. i++;
  685. auto image = row["image"];
  686. iter2->GetNextRow(&row);
  687. }
  688. EXPECT_EQ(i, 2);
  689. // Manually terminate the pipeline
  690. iter2->Stop();
  691. }
  692. TEST_F(MindDataTestCacheOp, DISABLED_TestCApiCacheShareFailure1) {
  693. session_id_type env_session;
  694. Status s = GetSessionFromEnv(&env_session);
  695. EXPECT_EQ(s, Status::OK());
  696. std::shared_ptr<DatasetCache> some_cache = CreateDatasetCache(env_session, 0, true);
  697. EXPECT_NE(some_cache, nullptr);
  698. // Create an ImageFolder Dataset, this folder_path only has 2 images in it
  699. std::string folder_path = datasets_root_path_ + "/testImageNetData/train/";
  700. std::shared_ptr<Dataset> ds1 = ImageFolder(folder_path, true, std::make_shared<RandomSampler>(), {}, {}, some_cache);
  701. EXPECT_NE(ds1, nullptr);
  702. std::shared_ptr<Dataset> ds2 = ImageFolder(folder_path, false, std::make_shared<RandomSampler>(), {}, {}, some_cache);
  703. EXPECT_NE(ds2, nullptr);
  704. // Create and launch the Execution Tree for ds1
  705. std::shared_ptr<Iterator> iter1 = ds1->CreateIterator();
  706. EXPECT_NE(iter1, nullptr);
  707. // Iterate the dataset and get each row
  708. std::unordered_map<std::string, mindspore::MSTensor> row;
  709. iter1->GetNextRow(&row);
  710. uint64_t i = 0;
  711. while (row.size() != 0) {
  712. i++;
  713. auto image = row["image"];
  714. iter1->GetNextRow(&row);
  715. }
  716. EXPECT_EQ(i, 2);
  717. // Manually terminate the pipeline
  718. iter1->Stop();
  719. // Re-use a cache for the second pipeline would fail
  720. std::shared_ptr<Iterator> iter2 = ds2->CreateIterator();
  721. EXPECT_EQ(iter2, nullptr);
  722. }