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.

tfReader_op_test.cc 23 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718
  1. /**
  2. * Copyright 2019-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 <iostream>
  17. #include <memory>
  18. #include <vector>
  19. #include "minddata/dataset/core/client.h"
  20. #include "minddata/dataset/engine/data_schema.h"
  21. #include "common/common.h"
  22. #include "gtest/gtest.h"
  23. #include "utils/log_adapter.h"
  24. namespace common = mindspore::common;
  25. using namespace mindspore::dataset;
  26. using mindspore::MsLogLevel::INFO;
  27. using mindspore::ExceptionType::NoExceptionType;
  28. using mindspore::LogStream;
  29. class MindDataTestTFReaderOp : public UT::DatasetOpTesting {
  30. };
  31. TEST_F(MindDataTestTFReaderOp, TestTFReaderBasic1) {
  32. // Start with an empty execution tree
  33. auto my_tree = std::make_shared<ExecutionTree>();
  34. std::string dataset_path;
  35. dataset_path = datasets_root_path_ + "/testTFTestAllTypes/test.data";
  36. std::shared_ptr<TFReaderOp> my_tfreader_op;
  37. TFReaderOp::Builder builder;
  38. builder.SetDatasetFilesList({dataset_path});
  39. std::unique_ptr<DataSchema> schema = std::make_unique<DataSchema>();
  40. schema->LoadSchemaFile(datasets_root_path_ + "/testTFTestAllTypes/datasetSchema.json", {});
  41. builder.SetDataSchema(std::move(schema));
  42. Status rc = builder.Build(&my_tfreader_op);
  43. ASSERT_TRUE(rc.IsOk());
  44. rc = my_tree->AssociateNode(my_tfreader_op);
  45. ASSERT_TRUE(rc.IsOk());
  46. rc = my_tree->AssignRoot(my_tfreader_op);
  47. ASSERT_TRUE(rc.IsOk());
  48. MS_LOG(INFO) << "Launching tree and begin iteration.";
  49. rc = my_tree->Prepare();
  50. ASSERT_TRUE(rc.IsOk());
  51. rc = my_tree->Launch();
  52. ASSERT_TRUE(rc.IsOk());
  53. // Start the loop of reading tensors from our pipeline
  54. DatasetIterator di(my_tree);
  55. TensorRow tensor_list;
  56. rc = di.FetchNextTensorRow(&tensor_list);
  57. ASSERT_TRUE(rc.IsOk());
  58. int row_count = 0;
  59. while (!tensor_list.empty()) {
  60. // Display the tensor by calling the printer on it
  61. for (int i = 0; i < tensor_list.size(); i++) {
  62. std::ostringstream ss;
  63. ss << "(" << tensor_list[i] << "): " << *tensor_list[i] << std::endl;
  64. MS_LOG(INFO) << "Tensor print: " << ss.str() << ".";
  65. }
  66. rc = di.FetchNextTensorRow(&tensor_list);
  67. ASSERT_TRUE(rc.IsOk());
  68. row_count++;
  69. }
  70. ASSERT_EQ(row_count, 12);
  71. }
  72. TEST_F(MindDataTestTFReaderOp, TestTFReaderLargeRowsPerBuffer) {
  73. // Start with an empty execution tree
  74. auto my_tree = std::make_shared<ExecutionTree>();
  75. std::string dataset_path;
  76. dataset_path = datasets_root_path_ + "/testTFTestAllTypes/test.data";
  77. std::shared_ptr<TFReaderOp> my_tfreader_op;
  78. TFReaderOp::Builder builder;
  79. builder.SetDatasetFilesList({dataset_path}).SetRowsPerBuffer(500);
  80. std::unique_ptr<DataSchema> schema = std::make_unique<DataSchema>();
  81. schema->LoadSchemaFile(datasets_root_path_ + "/testTFTestAllTypes/datasetSchema.json", {});
  82. builder.SetDataSchema(std::move(schema));
  83. Status rc = builder.Build(&my_tfreader_op);
  84. ASSERT_TRUE(rc.IsOk());
  85. rc = my_tree->AssociateNode(my_tfreader_op);
  86. ASSERT_TRUE(rc.IsOk());
  87. rc = my_tree->AssignRoot(my_tfreader_op);
  88. ASSERT_TRUE(rc.IsOk());
  89. MS_LOG(INFO) << "Launching tree and begin iteration.";
  90. rc = my_tree->Prepare();
  91. ASSERT_TRUE(rc.IsOk());
  92. rc = my_tree->Launch();
  93. ASSERT_TRUE(rc.IsOk());
  94. // Start the loop of reading tensors from our pipeline
  95. DatasetIterator di(my_tree);
  96. TensorRow tensor_list;
  97. rc = di.FetchNextTensorRow(&tensor_list);
  98. ASSERT_TRUE(rc.IsOk());
  99. int row_count = 0;
  100. while (!tensor_list.empty()) {
  101. // Display the tensor by calling the printer on it
  102. for (int i = 0; i < tensor_list.size(); i++) {
  103. std::ostringstream ss;
  104. ss << "(" << tensor_list[i] << "): " << *tensor_list[i] << std::endl;
  105. MS_LOG(INFO) << "Tensor print: " << ss.str() << ".";
  106. }
  107. rc = di.FetchNextTensorRow(&tensor_list);
  108. ASSERT_TRUE(rc.IsOk());
  109. row_count++;
  110. }
  111. ASSERT_EQ(row_count, 12);
  112. }
  113. TEST_F(MindDataTestTFReaderOp, TestTFReaderSmallRowsPerBuffer) {
  114. // Start with an empty execution tree
  115. auto my_tree = std::make_shared<ExecutionTree>();
  116. std::string dataset_path;
  117. dataset_path = datasets_root_path_ + "/testTFTestAllTypes/test.data";
  118. std::shared_ptr<TFReaderOp> my_tfreader_op;
  119. TFReaderOp::Builder builder;
  120. builder.SetDatasetFilesList({dataset_path});
  121. std::unique_ptr<DataSchema> schema = std::make_unique<DataSchema>();
  122. schema->LoadSchemaFile(datasets_root_path_ + "/testTFTestAllTypes/datasetSchema.json", {});
  123. builder.SetDataSchema(std::move(schema));
  124. Status rc = builder.Build(&my_tfreader_op);
  125. ASSERT_TRUE(rc.IsOk());
  126. rc = my_tree->AssociateNode(my_tfreader_op);
  127. ASSERT_TRUE(rc.IsOk());
  128. rc = my_tree->AssignRoot(my_tfreader_op);
  129. ASSERT_TRUE(rc.IsOk());
  130. MS_LOG(INFO) << "Launching tree and begin iteration.";
  131. rc = my_tree->Prepare();
  132. ASSERT_TRUE(rc.IsOk());
  133. rc = my_tree->Launch();
  134. ASSERT_TRUE(rc.IsOk());
  135. // Start the loop of reading tensors from our pipeline
  136. DatasetIterator di(my_tree);
  137. TensorRow tensor_list;
  138. rc = di.FetchNextTensorRow(&tensor_list);
  139. ASSERT_TRUE(rc.IsOk());
  140. int row_count = 0;
  141. while (!tensor_list.empty()) {
  142. // Display the tensor by calling the printer on it
  143. for (int i = 0; i < tensor_list.size(); i++) {
  144. std::ostringstream ss;
  145. ss << "(" << tensor_list[i] << "): " << *tensor_list[i] << std::endl;
  146. MS_LOG(INFO) << "Tensor print: " << ss.str() << ".";
  147. }
  148. rc = di.FetchNextTensorRow(&tensor_list);
  149. ASSERT_TRUE(rc.IsOk());
  150. row_count++;
  151. }
  152. ASSERT_EQ(row_count, 12);
  153. }
  154. TEST_F(MindDataTestTFReaderOp, TestTFReaderLargeQueueSize) {
  155. // Start with an empty execution tree
  156. auto my_tree = std::make_shared<ExecutionTree>();
  157. std::string dataset_path;
  158. dataset_path = datasets_root_path_ + "/testTFTestAllTypes/test.data";
  159. std::shared_ptr<TFReaderOp> my_tfreader_op;
  160. TFReaderOp::Builder builder;
  161. builder.SetDatasetFilesList({dataset_path}).SetWorkerConnectorSize(1);
  162. std::unique_ptr<DataSchema> schema = std::make_unique<DataSchema>();
  163. schema->LoadSchemaFile(datasets_root_path_ + "/testTFTestAllTypes/datasetSchema.json", {});
  164. builder.SetDataSchema(std::move(schema));
  165. Status rc = builder.Build(&my_tfreader_op);
  166. ASSERT_TRUE(rc.IsOk());
  167. rc = my_tree->AssociateNode(my_tfreader_op);
  168. ASSERT_TRUE(rc.IsOk());
  169. rc = my_tree->AssignRoot(my_tfreader_op);
  170. ASSERT_TRUE(rc.IsOk());
  171. MS_LOG(INFO) << "Launching tree and begin iteration.";
  172. rc = my_tree->Prepare();
  173. ASSERT_TRUE(rc.IsOk());
  174. rc = my_tree->Launch();
  175. ASSERT_TRUE(rc.IsOk());
  176. // Start the loop of reading tensors from our pipeline
  177. DatasetIterator di(my_tree);
  178. TensorRow tensor_list;
  179. rc = di.FetchNextTensorRow(&tensor_list);
  180. ASSERT_TRUE(rc.IsOk());
  181. int row_count = 0;
  182. while (!tensor_list.empty()) {
  183. // Display the tensor by calling the printer on it
  184. for (int i = 0; i < tensor_list.size(); i++) {
  185. std::ostringstream ss;
  186. ss << "(" << tensor_list[i] << "): " << *tensor_list[i] << std::endl;
  187. MS_LOG(INFO) << "Tensor print: " << ss.str() << ".";
  188. }
  189. rc = di.FetchNextTensorRow(&tensor_list);
  190. ASSERT_TRUE(rc.IsOk());
  191. row_count++;
  192. }
  193. ASSERT_EQ(row_count, 12);
  194. }
  195. TEST_F(MindDataTestTFReaderOp, TestTFReaderOneThread) {
  196. // Start with an empty execution tree
  197. auto my_tree = std::make_shared<ExecutionTree>();
  198. std::string dataset_path;
  199. dataset_path = datasets_root_path_ + "/testTFTestAllTypes/test.data";
  200. std::shared_ptr<TFReaderOp> my_tfreader_op;
  201. TFReaderOp::Builder builder;
  202. builder
  203. .SetDatasetFilesList({dataset_path})
  204. .SetNumWorkers(1);
  205. std::unique_ptr<DataSchema> schema = std::make_unique<DataSchema>();
  206. schema->LoadSchemaFile(datasets_root_path_ + "/testTFTestAllTypes/datasetSchema.json", {});
  207. builder.SetDataSchema(std::move(schema));
  208. Status rc = builder.Build(&my_tfreader_op);
  209. ASSERT_TRUE(rc.IsOk());
  210. rc = my_tree->AssociateNode(my_tfreader_op);
  211. ASSERT_TRUE(rc.IsOk());
  212. rc = my_tree->AssignRoot(my_tfreader_op);
  213. ASSERT_TRUE(rc.IsOk());
  214. MS_LOG(INFO) << "Launching tree and begin iteration.";
  215. rc = my_tree->Prepare();
  216. ASSERT_TRUE(rc.IsOk());
  217. rc = my_tree->Launch();
  218. ASSERT_TRUE(rc.IsOk());
  219. // Start the loop of reading tensors from our pipeline
  220. DatasetIterator di(my_tree);
  221. TensorRow tensor_list;
  222. rc = di.FetchNextTensorRow(&tensor_list);
  223. ASSERT_TRUE(rc.IsOk());
  224. int row_count = 0;
  225. while (!tensor_list.empty()) {
  226. // Display the tensor by calling the printer on it
  227. for (int i = 0; i < tensor_list.size(); i++) {
  228. std::ostringstream ss;
  229. ss << "(" << tensor_list[i] << "): " << *tensor_list[i] << std::endl;
  230. MS_LOG(INFO) << "Tensor print: " << ss.str() << ".";
  231. }
  232. rc = di.FetchNextTensorRow(&tensor_list);
  233. ASSERT_TRUE(rc.IsOk());
  234. row_count++;
  235. }
  236. ASSERT_EQ(row_count, 12);
  237. }
  238. TEST_F(MindDataTestTFReaderOp, TestTFReaderRepeat) {
  239. // Start with an empty execution tree
  240. auto my_tree = std::make_shared<ExecutionTree>();
  241. std::string dataset_path;
  242. dataset_path = datasets_root_path_ + "/testTFTestAllTypes/test.data";
  243. // TFReaderOp
  244. std::shared_ptr<TFReaderOp> my_tfreader_op;
  245. TFReaderOp::Builder builder;
  246. builder.SetDatasetFilesList({dataset_path}).SetWorkerConnectorSize(16);
  247. std::unique_ptr<DataSchema> schema = std::make_unique<DataSchema>();
  248. schema->LoadSchemaFile(datasets_root_path_ + "/testTFTestAllTypes/datasetSchema.json", {});
  249. builder.SetDataSchema(std::move(schema));
  250. Status rc= builder.Build(&my_tfreader_op);
  251. ASSERT_TRUE(rc.IsOk());
  252. rc = my_tree->AssociateNode(my_tfreader_op);
  253. ASSERT_TRUE(rc.IsOk());
  254. // RepeatOp
  255. uint32_t num_repeats = 3;
  256. std::shared_ptr<RepeatOp> my_repeat_op = std::make_shared<RepeatOp>(num_repeats);
  257. rc = my_tree->AssociateNode(my_repeat_op);
  258. ASSERT_TRUE(rc.IsOk());
  259. // Set children/root layout.
  260. my_tfreader_op->set_total_repeats(num_repeats);
  261. my_tfreader_op->set_num_repeats_per_epoch(num_repeats);
  262. rc = my_repeat_op->AddChild(my_tfreader_op);
  263. ASSERT_TRUE(rc.IsOk());
  264. rc = my_tree->AssignRoot(my_repeat_op);
  265. ASSERT_TRUE(rc.IsOk());
  266. MS_LOG(INFO) << "Launching tree and begin iteration.";
  267. rc = my_tree->Prepare();
  268. ASSERT_TRUE(rc.IsOk());
  269. rc = my_tree->Launch();
  270. ASSERT_TRUE(rc.IsOk());
  271. // Start the loop of reading tensors from our pipeline
  272. DatasetIterator di(my_tree);
  273. TensorRow tensor_list;
  274. rc = di.FetchNextTensorRow(&tensor_list);
  275. ASSERT_TRUE(rc.IsOk());
  276. int row_count = 0;
  277. while (!tensor_list.empty()) {
  278. MS_LOG(INFO) << "Row display for row #: " << row_count << ".";
  279. // Display the tensor by calling the printer on it
  280. for (int i = 0; i < tensor_list.size(); i++) {
  281. std::ostringstream ss;
  282. ss << "(" << tensor_list[i] << "): " << *tensor_list[i] << std::endl;
  283. MS_LOG(INFO) << "Tensor print: " << ss.str() << ".";
  284. }
  285. rc = di.FetchNextTensorRow(&tensor_list);
  286. ASSERT_TRUE(rc.IsOk());
  287. row_count++;
  288. }
  289. ASSERT_EQ(row_count, 12 * 3);
  290. }
  291. TEST_F(MindDataTestTFReaderOp, TestTFReaderSchemaConstructor) {
  292. // Start with an empty execution tree
  293. auto my_tree = std::make_shared<ExecutionTree>();
  294. std::string dataset_path;
  295. dataset_path = datasets_root_path_ + "/testTFTestAllTypes";
  296. std::unique_ptr<DataSchema> data_schema = std::make_unique<DataSchema>();
  297. std::vector<std::string> columns_to_load;
  298. columns_to_load.push_back("col_sint32");
  299. columns_to_load.push_back("col_binary");
  300. data_schema->LoadSchemaFile(dataset_path + "/datasetSchema.json", columns_to_load);
  301. std::shared_ptr<TFReaderOp> my_tfreader_op;
  302. TFReaderOp::Builder builder;
  303. builder.SetDatasetFilesList({dataset_path + "/test.data"})
  304. .SetNumWorkers(16)
  305. .SetDataSchema(std::move(data_schema));
  306. Status rc = builder.Build(&my_tfreader_op);
  307. ASSERT_TRUE(rc.IsOk());
  308. rc = my_tree->AssociateNode(my_tfreader_op);
  309. ASSERT_TRUE(rc.IsOk());
  310. rc = my_tree->AssignRoot(my_tfreader_op);
  311. ASSERT_TRUE(rc.IsOk());
  312. MS_LOG(INFO) << "Launching tree and begin iteration.";
  313. rc = my_tree->Prepare();
  314. ASSERT_TRUE(rc.IsOk());
  315. rc = my_tree->Launch();
  316. ASSERT_TRUE(rc.IsOk());
  317. // Start the loop of reading tensors from our pipeline
  318. DatasetIterator di(my_tree);
  319. TensorRow tensor_list;
  320. rc = di.FetchNextTensorRow(&tensor_list);
  321. ASSERT_TRUE(rc.IsOk());
  322. int row_count = 0;
  323. while (!tensor_list.empty()) {
  324. // Display the tensor by calling the printer on it
  325. ASSERT_EQ(tensor_list.size(), columns_to_load.size());
  326. for (int i = 0; i < tensor_list.size(); i++) {
  327. std::ostringstream ss;
  328. ss << "(" << tensor_list[i] << "): " << *tensor_list[i] << std::endl;
  329. MS_LOG(INFO) << "Tensor print: " << ss.str() << ".";
  330. }
  331. rc = di.FetchNextTensorRow(&tensor_list);
  332. ASSERT_TRUE(rc.IsOk());
  333. row_count++;
  334. }
  335. ASSERT_EQ(row_count, 12);
  336. }
  337. TEST_F(MindDataTestTFReaderOp, TestTFReaderTake1Row) {
  338. // Start with an empty execution tree
  339. auto my_tree = std::make_shared<ExecutionTree>();
  340. std::string dataset_path;
  341. dataset_path = datasets_root_path_ + "/testTFTestAllTypes";
  342. std::string data_schema_filepath = dataset_path + "/datasetSchema1Row.json";
  343. // TFReaderOp
  344. std::shared_ptr<TFReaderOp> my_tfreader_op;
  345. TFReaderOp::Builder builder;
  346. builder.SetDatasetFilesList({dataset_path + "/test.data"}).SetRowsPerBuffer(5);
  347. std::unique_ptr<DataSchema> schema = std::make_unique<DataSchema>();
  348. schema->LoadSchemaFile(datasets_root_path_ + "/testTFTestAllTypes/datasetSchema1Row.json", {});
  349. builder.SetDataSchema(std::move(schema));
  350. Status rc= builder.Build(&my_tfreader_op);
  351. ASSERT_TRUE(rc.IsOk());
  352. rc = my_tree->AssociateNode(my_tfreader_op);
  353. ASSERT_TRUE(rc.IsOk());
  354. rc = my_tree->AssignRoot(my_tfreader_op);
  355. ASSERT_TRUE(rc.IsOk());
  356. MS_LOG(INFO) << "Launching tree and begin iteration.";
  357. rc = my_tree->Prepare();
  358. ASSERT_TRUE(rc.IsOk());
  359. rc = my_tree->Launch();
  360. ASSERT_TRUE(rc.IsOk());
  361. // Start the loop of reading tensors from our pipeline
  362. DatasetIterator di(my_tree);
  363. TensorRow tensor_list;
  364. rc = di.FetchNextTensorRow(&tensor_list);
  365. ASSERT_TRUE(rc.IsOk());
  366. int row_count = 0;
  367. while (!tensor_list.empty()) {
  368. MS_LOG(INFO) << "Row display for row #: " << row_count << ".";
  369. // Display the tensor by calling the printer on it
  370. for (int i = 0; i < tensor_list.size(); i++) {
  371. std::ostringstream ss;
  372. ss << "(" << tensor_list[i] << "): " << *tensor_list[i] << std::endl;
  373. MS_LOG(INFO) << "Tensor print: " << ss.str() << ".";
  374. }
  375. rc = di.FetchNextTensorRow(&tensor_list);
  376. ASSERT_TRUE(rc.IsOk());
  377. row_count++;
  378. }
  379. ASSERT_EQ(row_count, 1);
  380. }
  381. TEST_F(MindDataTestTFReaderOp, TestTFReaderTake1Buffer) {
  382. // Start with an empty execution tree
  383. auto my_tree = std::make_shared<ExecutionTree>();
  384. std::string dataset_path;
  385. dataset_path = datasets_root_path_ + "/testTFTestAllTypes";
  386. std::string data_schema_filepath = dataset_path + "/datasetSchema5Rows.json";
  387. // TFReaderOp
  388. std::shared_ptr<TFReaderOp> my_tfreader_op;
  389. TFReaderOp::Builder builder;
  390. builder.SetDatasetFilesList({dataset_path + "/test.data"}).SetRowsPerBuffer(5);
  391. std::unique_ptr<DataSchema> schema = std::make_unique<DataSchema>();
  392. schema->LoadSchemaFile(datasets_root_path_ + "/testTFTestAllTypes/datasetSchema5Rows.json", {});
  393. builder.SetDataSchema(std::move(schema));
  394. Status rc= builder.Build(&my_tfreader_op);
  395. ASSERT_TRUE(rc.IsOk());
  396. rc = my_tree->AssociateNode(my_tfreader_op);
  397. ASSERT_TRUE(rc.IsOk());
  398. rc = my_tree->AssignRoot(my_tfreader_op);
  399. ASSERT_TRUE(rc.IsOk());
  400. MS_LOG(INFO) << "Launching tree and begin iteration.";
  401. rc = my_tree->Prepare();
  402. ASSERT_TRUE(rc.IsOk());
  403. rc = my_tree->Launch();
  404. ASSERT_TRUE(rc.IsOk());
  405. // Start the loop of reading tensors from our pipeline
  406. DatasetIterator di(my_tree);
  407. TensorRow tensor_list;
  408. rc = di.FetchNextTensorRow(&tensor_list);
  409. ASSERT_TRUE(rc.IsOk());
  410. int row_count = 0;
  411. while (!tensor_list.empty()) {
  412. MS_LOG(INFO) << "Row display for row #: " << row_count << ".";
  413. // Display the tensor by calling the printer on it
  414. for (int i = 0; i < tensor_list.size(); i++) {
  415. std::ostringstream ss;
  416. ss << "(" << tensor_list[i] << "): " << *tensor_list[i] << std::endl;
  417. MS_LOG(INFO) << "Tensor print: " << ss.str() << ".";
  418. }
  419. rc = di.FetchNextTensorRow(&tensor_list);
  420. ASSERT_TRUE(rc.IsOk());
  421. row_count++;
  422. }
  423. ASSERT_EQ(row_count, 5);
  424. }
  425. TEST_F(MindDataTestTFReaderOp, TestTFReaderTake7Rows) {
  426. // Start with an empty execution tree
  427. auto my_tree = std::make_shared<ExecutionTree>();
  428. std::string dataset_path;
  429. dataset_path = datasets_root_path_ + "/testTFTestAllTypes";
  430. std::string data_schema_filepath = dataset_path + "/datasetSchema7Rows.json";
  431. // TFReaderOp
  432. std::shared_ptr<TFReaderOp> my_tfreader_op;
  433. TFReaderOp::Builder builder;
  434. builder.SetDatasetFilesList({dataset_path + "/test.data"}).SetRowsPerBuffer(5);
  435. std::unique_ptr<DataSchema> schema = std::make_unique<DataSchema>();
  436. schema->LoadSchemaFile(datasets_root_path_ + "/testTFTestAllTypes/datasetSchema7Rows.json", {});
  437. builder.SetDataSchema(std::move(schema));
  438. Status rc= builder.Build(&my_tfreader_op);
  439. ASSERT_TRUE(rc.IsOk());
  440. rc = my_tree->AssociateNode(my_tfreader_op);
  441. ASSERT_TRUE(rc.IsOk());
  442. rc = my_tree->AssignRoot(my_tfreader_op);
  443. ASSERT_TRUE(rc.IsOk());
  444. MS_LOG(INFO) << "Launching tree and begin iteration.";
  445. rc = my_tree->Prepare();
  446. ASSERT_TRUE(rc.IsOk());
  447. rc = my_tree->Launch();
  448. ASSERT_TRUE(rc.IsOk());
  449. // Start the loop of reading tensors from our pipeline
  450. DatasetIterator di(my_tree);
  451. TensorRow tensor_list;
  452. rc = di.FetchNextTensorRow(&tensor_list);
  453. ASSERT_TRUE(rc.IsOk());
  454. int row_count = 0;
  455. while (!tensor_list.empty()) {
  456. MS_LOG(INFO) << "Row display for row #: " << row_count << ".";
  457. // Display the tensor by calling the printer on it
  458. for (int i = 0; i < tensor_list.size(); i++) {
  459. std::ostringstream ss;
  460. ss << "(" << tensor_list[i] << "): " << *tensor_list[i] << std::endl;
  461. MS_LOG(INFO) << "Tensor print: " << ss.str() << ".";
  462. }
  463. rc = di.FetchNextTensorRow(&tensor_list);
  464. ASSERT_TRUE(rc.IsOk());
  465. row_count++;
  466. }
  467. ASSERT_EQ(row_count, 7);
  468. }
  469. TEST_F(MindDataTestTFReaderOp, TestTFReaderBasicNoSchema) {
  470. // Start with an empty execution tree
  471. auto my_tree = std::make_shared<ExecutionTree>();
  472. std::string dataset_path;
  473. dataset_path = datasets_root_path_ + "/testTFTestAllTypes/test.data";
  474. std::shared_ptr<TFReaderOp> my_tfreader_op;
  475. TFReaderOp::Builder builder;
  476. builder.SetDatasetFilesList({dataset_path});
  477. Status rc = builder.Build(&my_tfreader_op);
  478. ASSERT_TRUE(rc.IsOk());
  479. rc = my_tree->AssociateNode(my_tfreader_op);
  480. ASSERT_TRUE(rc.IsOk());
  481. rc = my_tree->AssignRoot(my_tfreader_op);
  482. ASSERT_TRUE(rc.IsOk());
  483. MS_LOG(INFO) << "Launching tree and begin iteration.";
  484. rc = my_tree->Prepare();
  485. ASSERT_TRUE(rc.IsOk());
  486. rc = my_tree->Launch();
  487. ASSERT_TRUE(rc.IsOk());
  488. // Start the loop of reading tensors from our pipeline
  489. DatasetIterator di(my_tree);
  490. TensorRow tensor_list;
  491. rc = di.FetchNextTensorRow(&tensor_list);
  492. ASSERT_TRUE(rc.IsOk());
  493. int row_count = 0;
  494. while (!tensor_list.empty()) {
  495. // Display the tensor by calling the printer on it
  496. ASSERT_EQ(tensor_list.size(), 9);
  497. for (int i = 0; i < tensor_list.size(); i++) {
  498. std::ostringstream ss;
  499. ss << "(" << tensor_list[i] << "): " << *tensor_list[i] << std::endl;
  500. MS_LOG(INFO) << "Tensor print: " << ss.str() << ".";
  501. }
  502. rc = di.FetchNextTensorRow(&tensor_list);
  503. ASSERT_TRUE(rc.IsOk());
  504. row_count++;
  505. }
  506. ASSERT_EQ(row_count, 12);
  507. }
  508. TEST_F(MindDataTestTFReaderOp, TestTotalRowsBasic) {
  509. std::string tf_file = datasets_root_path_ + "/testTFTestAllTypes/test.data";
  510. std::vector<std::string> filenames;
  511. for (int i = 0; i < 5; i++) {
  512. filenames.push_back(tf_file);
  513. }
  514. int64_t total_rows = 0;
  515. TFReaderOp::CountTotalRows(&total_rows, filenames, 1);
  516. ASSERT_EQ(total_rows, 60);
  517. TFReaderOp::CountTotalRows(&total_rows, filenames, 2);
  518. ASSERT_EQ(total_rows, 60);
  519. TFReaderOp::CountTotalRows(&total_rows, filenames, 3);
  520. ASSERT_EQ(total_rows, 60);
  521. TFReaderOp::CountTotalRows(&total_rows, filenames, 4);
  522. ASSERT_EQ(total_rows, 60);
  523. TFReaderOp::CountTotalRows(&total_rows, filenames, 5);
  524. ASSERT_EQ(total_rows, 60);
  525. TFReaderOp::CountTotalRows(&total_rows, filenames, 6);
  526. ASSERT_EQ(total_rows, 60);
  527. TFReaderOp::CountTotalRows(&total_rows, filenames, 729);
  528. ASSERT_EQ(total_rows, 60);
  529. TFReaderOp::CountTotalRows(&total_rows, filenames, 1, true);
  530. ASSERT_EQ(total_rows, 60);
  531. TFReaderOp::CountTotalRows(&total_rows, filenames, 2, true);
  532. ASSERT_EQ(total_rows, 60);
  533. TFReaderOp::CountTotalRows(&total_rows, filenames, 3, true);
  534. ASSERT_EQ(total_rows, 60);
  535. TFReaderOp::CountTotalRows(&total_rows, filenames, 4, true);
  536. ASSERT_EQ(total_rows, 60);
  537. TFReaderOp::CountTotalRows(&total_rows, filenames, 5, true);
  538. ASSERT_EQ(total_rows, 60);
  539. TFReaderOp::CountTotalRows(&total_rows, filenames, 6, true);
  540. ASSERT_EQ(total_rows, 60);
  541. TFReaderOp::CountTotalRows(&total_rows, filenames, 729, true);
  542. ASSERT_EQ(total_rows, 60);
  543. }
  544. TEST_F(MindDataTestTFReaderOp, TestTFReaderInvalidFiles) {
  545. // Start with an empty execution tree
  546. auto my_tree = std::make_shared<ExecutionTree>();
  547. std::string valid_file = datasets_root_path_ + "/testTFTestAllTypes/test.data";
  548. std::string schema_file = datasets_root_path_ + "/testTFTestAllTypes/datasetSchema.json";
  549. std::string invalid_file = datasets_root_path_ + "/testTFTestAllTypes/invalidFile.txt";
  550. std::string nonexistent_file = "this/file/does_not/exist";
  551. std::shared_ptr<TFReaderOp> my_tfreader_op;
  552. TFReaderOp::Builder builder;
  553. builder.SetDatasetFilesList({invalid_file, valid_file, schema_file});
  554. std::unique_ptr<DataSchema> schema = std::make_unique<DataSchema>();
  555. schema->LoadSchemaFile(schema_file, {});
  556. builder.SetDataSchema(std::move(schema));
  557. Status rc = builder.Build(&my_tfreader_op);
  558. ASSERT_TRUE(!rc.IsOk());
  559. builder.SetDatasetFilesList({invalid_file, valid_file, schema_file, nonexistent_file});
  560. schema = std::make_unique<DataSchema>();
  561. schema->LoadSchemaFile(schema_file, {});
  562. builder.SetDataSchema(std::move(schema));
  563. rc = builder.Build(&my_tfreader_op);
  564. ASSERT_TRUE(!rc.IsOk());
  565. }