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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717
  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}).SetRowsPerBuffer(16);
  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}).SetRowsPerBuffer(1);
  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).SetRowsPerBuffer(16);
  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.SetDatasetFilesList({dataset_path})
  203. .SetRowsPerBuffer(16)
  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}).SetRowsPerBuffer(16).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. .SetRowsPerBuffer(16)
  305. .SetNumWorkers(16)
  306. .SetDataSchema(std::move(data_schema));
  307. Status rc = builder.Build(&my_tfreader_op);
  308. ASSERT_TRUE(rc.IsOk());
  309. rc = my_tree->AssociateNode(my_tfreader_op);
  310. ASSERT_TRUE(rc.IsOk());
  311. rc = my_tree->AssignRoot(my_tfreader_op);
  312. ASSERT_TRUE(rc.IsOk());
  313. MS_LOG(INFO) << "Launching tree and begin iteration.";
  314. rc = my_tree->Prepare();
  315. ASSERT_TRUE(rc.IsOk());
  316. rc = my_tree->Launch();
  317. ASSERT_TRUE(rc.IsOk());
  318. // Start the loop of reading tensors from our pipeline
  319. DatasetIterator di(my_tree);
  320. TensorRow tensor_list;
  321. rc = di.FetchNextTensorRow(&tensor_list);
  322. ASSERT_TRUE(rc.IsOk());
  323. int row_count = 0;
  324. while (!tensor_list.empty()) {
  325. // Display the tensor by calling the printer on it
  326. ASSERT_EQ(tensor_list.size(), columns_to_load.size());
  327. for (int i = 0; i < tensor_list.size(); i++) {
  328. std::ostringstream ss;
  329. ss << "(" << tensor_list[i] << "): " << *tensor_list[i] << std::endl;
  330. MS_LOG(INFO) << "Tensor print: " << ss.str() << ".";
  331. }
  332. rc = di.FetchNextTensorRow(&tensor_list);
  333. ASSERT_TRUE(rc.IsOk());
  334. row_count++;
  335. }
  336. ASSERT_EQ(row_count, 12);
  337. }
  338. TEST_F(MindDataTestTFReaderOp, TestTFReaderTake1Row) {
  339. // Start with an empty execution tree
  340. auto my_tree = std::make_shared<ExecutionTree>();
  341. std::string dataset_path;
  342. dataset_path = datasets_root_path_ + "/testTFTestAllTypes";
  343. std::string data_schema_filepath = dataset_path + "/datasetSchema1Row.json";
  344. // TFReaderOp
  345. std::shared_ptr<TFReaderOp> my_tfreader_op;
  346. TFReaderOp::Builder builder;
  347. builder.SetDatasetFilesList({dataset_path + "/test.data"}).SetRowsPerBuffer(5);
  348. std::unique_ptr<DataSchema> schema = std::make_unique<DataSchema>();
  349. schema->LoadSchemaFile(datasets_root_path_ + "/testTFTestAllTypes/datasetSchema1Row.json", {});
  350. builder.SetDataSchema(std::move(schema));
  351. Status rc= builder.Build(&my_tfreader_op);
  352. ASSERT_TRUE(rc.IsOk());
  353. rc = my_tree->AssociateNode(my_tfreader_op);
  354. ASSERT_TRUE(rc.IsOk());
  355. rc = my_tree->AssignRoot(my_tfreader_op);
  356. ASSERT_TRUE(rc.IsOk());
  357. MS_LOG(INFO) << "Launching tree and begin iteration.";
  358. rc = my_tree->Prepare();
  359. ASSERT_TRUE(rc.IsOk());
  360. rc = my_tree->Launch();
  361. ASSERT_TRUE(rc.IsOk());
  362. // Start the loop of reading tensors from our pipeline
  363. DatasetIterator di(my_tree);
  364. TensorRow tensor_list;
  365. rc = di.FetchNextTensorRow(&tensor_list);
  366. ASSERT_TRUE(rc.IsOk());
  367. int row_count = 0;
  368. while (!tensor_list.empty()) {
  369. MS_LOG(INFO) << "Row display for row #: " << row_count << ".";
  370. // Display the tensor by calling the printer on it
  371. for (int i = 0; i < tensor_list.size(); i++) {
  372. std::ostringstream ss;
  373. ss << "(" << tensor_list[i] << "): " << *tensor_list[i] << std::endl;
  374. MS_LOG(INFO) << "Tensor print: " << ss.str() << ".";
  375. }
  376. rc = di.FetchNextTensorRow(&tensor_list);
  377. ASSERT_TRUE(rc.IsOk());
  378. row_count++;
  379. }
  380. ASSERT_EQ(row_count, 1);
  381. }
  382. TEST_F(MindDataTestTFReaderOp, TestTFReaderTake1Buffer) {
  383. // Start with an empty execution tree
  384. auto my_tree = std::make_shared<ExecutionTree>();
  385. std::string dataset_path;
  386. dataset_path = datasets_root_path_ + "/testTFTestAllTypes";
  387. std::string data_schema_filepath = dataset_path + "/datasetSchema5Rows.json";
  388. // TFReaderOp
  389. std::shared_ptr<TFReaderOp> my_tfreader_op;
  390. TFReaderOp::Builder builder;
  391. builder.SetDatasetFilesList({dataset_path + "/test.data"}).SetRowsPerBuffer(5);
  392. std::unique_ptr<DataSchema> schema = std::make_unique<DataSchema>();
  393. schema->LoadSchemaFile(datasets_root_path_ + "/testTFTestAllTypes/datasetSchema5Rows.json", {});
  394. builder.SetDataSchema(std::move(schema));
  395. Status rc= builder.Build(&my_tfreader_op);
  396. ASSERT_TRUE(rc.IsOk());
  397. rc = my_tree->AssociateNode(my_tfreader_op);
  398. ASSERT_TRUE(rc.IsOk());
  399. rc = my_tree->AssignRoot(my_tfreader_op);
  400. ASSERT_TRUE(rc.IsOk());
  401. MS_LOG(INFO) << "Launching tree and begin iteration.";
  402. rc = my_tree->Prepare();
  403. ASSERT_TRUE(rc.IsOk());
  404. rc = my_tree->Launch();
  405. ASSERT_TRUE(rc.IsOk());
  406. // Start the loop of reading tensors from our pipeline
  407. DatasetIterator di(my_tree);
  408. TensorRow tensor_list;
  409. rc = di.FetchNextTensorRow(&tensor_list);
  410. ASSERT_TRUE(rc.IsOk());
  411. int row_count = 0;
  412. while (!tensor_list.empty()) {
  413. MS_LOG(INFO) << "Row display for row #: " << row_count << ".";
  414. // Display the tensor by calling the printer on it
  415. for (int i = 0; i < tensor_list.size(); i++) {
  416. std::ostringstream ss;
  417. ss << "(" << tensor_list[i] << "): " << *tensor_list[i] << std::endl;
  418. MS_LOG(INFO) << "Tensor print: " << ss.str() << ".";
  419. }
  420. rc = di.FetchNextTensorRow(&tensor_list);
  421. ASSERT_TRUE(rc.IsOk());
  422. row_count++;
  423. }
  424. ASSERT_EQ(row_count, 5);
  425. }
  426. TEST_F(MindDataTestTFReaderOp, TestTFReaderTake7Rows) {
  427. // Start with an empty execution tree
  428. auto my_tree = std::make_shared<ExecutionTree>();
  429. std::string dataset_path;
  430. dataset_path = datasets_root_path_ + "/testTFTestAllTypes";
  431. std::string data_schema_filepath = dataset_path + "/datasetSchema7Rows.json";
  432. // TFReaderOp
  433. std::shared_ptr<TFReaderOp> my_tfreader_op;
  434. TFReaderOp::Builder builder;
  435. builder.SetDatasetFilesList({dataset_path + "/test.data"}).SetRowsPerBuffer(5);
  436. std::unique_ptr<DataSchema> schema = std::make_unique<DataSchema>();
  437. schema->LoadSchemaFile(datasets_root_path_ + "/testTFTestAllTypes/datasetSchema7Rows.json", {});
  438. builder.SetDataSchema(std::move(schema));
  439. Status rc= builder.Build(&my_tfreader_op);
  440. ASSERT_TRUE(rc.IsOk());
  441. rc = my_tree->AssociateNode(my_tfreader_op);
  442. ASSERT_TRUE(rc.IsOk());
  443. rc = my_tree->AssignRoot(my_tfreader_op);
  444. ASSERT_TRUE(rc.IsOk());
  445. MS_LOG(INFO) << "Launching tree and begin iteration.";
  446. rc = my_tree->Prepare();
  447. ASSERT_TRUE(rc.IsOk());
  448. rc = my_tree->Launch();
  449. ASSERT_TRUE(rc.IsOk());
  450. // Start the loop of reading tensors from our pipeline
  451. DatasetIterator di(my_tree);
  452. TensorRow tensor_list;
  453. rc = di.FetchNextTensorRow(&tensor_list);
  454. ASSERT_TRUE(rc.IsOk());
  455. int row_count = 0;
  456. while (!tensor_list.empty()) {
  457. MS_LOG(INFO) << "Row display for row #: " << row_count << ".";
  458. // Display the tensor by calling the printer on it
  459. for (int i = 0; i < tensor_list.size(); i++) {
  460. std::ostringstream ss;
  461. ss << "(" << tensor_list[i] << "): " << *tensor_list[i] << std::endl;
  462. MS_LOG(INFO) << "Tensor print: " << ss.str() << ".";
  463. }
  464. rc = di.FetchNextTensorRow(&tensor_list);
  465. ASSERT_TRUE(rc.IsOk());
  466. row_count++;
  467. }
  468. ASSERT_EQ(row_count, 7);
  469. }
  470. TEST_F(MindDataTestTFReaderOp, TestTFReaderBasicNoSchema) {
  471. // Start with an empty execution tree
  472. auto my_tree = std::make_shared<ExecutionTree>();
  473. std::string dataset_path;
  474. dataset_path = datasets_root_path_ + "/testTFTestAllTypes/test.data";
  475. std::shared_ptr<TFReaderOp> my_tfreader_op;
  476. TFReaderOp::Builder builder;
  477. builder.SetDatasetFilesList({dataset_path}).SetRowsPerBuffer(16);
  478. Status rc = builder.Build(&my_tfreader_op);
  479. ASSERT_TRUE(rc.IsOk());
  480. rc = my_tree->AssociateNode(my_tfreader_op);
  481. ASSERT_TRUE(rc.IsOk());
  482. rc = my_tree->AssignRoot(my_tfreader_op);
  483. ASSERT_TRUE(rc.IsOk());
  484. MS_LOG(INFO) << "Launching tree and begin iteration.";
  485. rc = my_tree->Prepare();
  486. ASSERT_TRUE(rc.IsOk());
  487. rc = my_tree->Launch();
  488. ASSERT_TRUE(rc.IsOk());
  489. // Start the loop of reading tensors from our pipeline
  490. DatasetIterator di(my_tree);
  491. TensorRow tensor_list;
  492. rc = di.FetchNextTensorRow(&tensor_list);
  493. ASSERT_TRUE(rc.IsOk());
  494. int row_count = 0;
  495. while (!tensor_list.empty()) {
  496. // Display the tensor by calling the printer on it
  497. ASSERT_EQ(tensor_list.size(), 9);
  498. for (int i = 0; i < tensor_list.size(); i++) {
  499. std::ostringstream ss;
  500. ss << "(" << tensor_list[i] << "): " << *tensor_list[i] << std::endl;
  501. MS_LOG(INFO) << "Tensor print: " << ss.str() << ".";
  502. }
  503. rc = di.FetchNextTensorRow(&tensor_list);
  504. ASSERT_TRUE(rc.IsOk());
  505. row_count++;
  506. }
  507. ASSERT_EQ(row_count, 12);
  508. }
  509. TEST_F(MindDataTestTFReaderOp, TestTotalRowsBasic) {
  510. std::string tf_file = datasets_root_path_ + "/testTFTestAllTypes/test.data";
  511. std::vector<std::string> filenames;
  512. for (int i = 0; i < 5; i++) {
  513. filenames.push_back(tf_file);
  514. }
  515. int64_t total_rows = 0;
  516. TFReaderOp::CountTotalRows(&total_rows, filenames, 1);
  517. ASSERT_EQ(total_rows, 60);
  518. TFReaderOp::CountTotalRows(&total_rows, filenames, 2);
  519. ASSERT_EQ(total_rows, 60);
  520. TFReaderOp::CountTotalRows(&total_rows, filenames, 3);
  521. ASSERT_EQ(total_rows, 60);
  522. TFReaderOp::CountTotalRows(&total_rows, filenames, 4);
  523. ASSERT_EQ(total_rows, 60);
  524. TFReaderOp::CountTotalRows(&total_rows, filenames, 5);
  525. ASSERT_EQ(total_rows, 60);
  526. TFReaderOp::CountTotalRows(&total_rows, filenames, 6);
  527. ASSERT_EQ(total_rows, 60);
  528. TFReaderOp::CountTotalRows(&total_rows, filenames, 729);
  529. ASSERT_EQ(total_rows, 60);
  530. TFReaderOp::CountTotalRows(&total_rows, filenames, 1, true);
  531. ASSERT_EQ(total_rows, 60);
  532. TFReaderOp::CountTotalRows(&total_rows, filenames, 2, true);
  533. ASSERT_EQ(total_rows, 60);
  534. TFReaderOp::CountTotalRows(&total_rows, filenames, 3, true);
  535. ASSERT_EQ(total_rows, 60);
  536. TFReaderOp::CountTotalRows(&total_rows, filenames, 4, true);
  537. ASSERT_EQ(total_rows, 60);
  538. TFReaderOp::CountTotalRows(&total_rows, filenames, 5, true);
  539. ASSERT_EQ(total_rows, 60);
  540. TFReaderOp::CountTotalRows(&total_rows, filenames, 6, true);
  541. ASSERT_EQ(total_rows, 60);
  542. TFReaderOp::CountTotalRows(&total_rows, filenames, 729, true);
  543. ASSERT_EQ(total_rows, 60);
  544. }
  545. TEST_F(MindDataTestTFReaderOp, TestTFReaderInvalidFiles) {
  546. // Start with an empty execution tree
  547. auto my_tree = std::make_shared<ExecutionTree>();
  548. std::string valid_file = datasets_root_path_ + "/testTFTestAllTypes/test.data";
  549. std::string schema_file = datasets_root_path_ + "/testTFTestAllTypes/datasetSchema.json";
  550. std::string invalid_file = datasets_root_path_ + "/testTFTestAllTypes/invalidFile.txt";
  551. std::string nonexistent_file = "this/file/does_not/exist";
  552. std::shared_ptr<TFReaderOp> my_tfreader_op;
  553. TFReaderOp::Builder builder;
  554. builder.SetDatasetFilesList({invalid_file, valid_file, schema_file}).SetRowsPerBuffer(16);
  555. std::unique_ptr<DataSchema> schema = std::make_unique<DataSchema>();
  556. schema->LoadSchemaFile(schema_file, {});
  557. builder.SetDataSchema(std::move(schema));
  558. Status rc = builder.Build(&my_tfreader_op);
  559. ASSERT_TRUE(!rc.IsOk());
  560. builder.SetDatasetFilesList({invalid_file, valid_file, schema_file, nonexistent_file}).SetRowsPerBuffer(16);
  561. schema = std::make_unique<DataSchema>();
  562. schema->LoadSchemaFile(schema_file, {});
  563. builder.SetDataSchema(std::move(schema));
  564. rc = builder.Build(&my_tfreader_op);
  565. ASSERT_TRUE(!rc.IsOk());
  566. }