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.

zip_op_test.cc 7.2 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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 <chrono>
  17. #include <cstring>
  18. #include <iostream>
  19. #include <memory>
  20. #include <string>
  21. #include "minddata/dataset/core/client.h"
  22. #include "minddata/dataset/engine/datasetops/zip_op.h"
  23. #include "minddata/dataset/core/tensor.h"
  24. #include "minddata/dataset/core/config_manager.h"
  25. #include "common/common.h"
  26. #include "utils/ms_utils.h"
  27. #include "gtest/gtest.h"
  28. #include "utils/log_adapter.h"
  29. namespace common = mindspore::common;
  30. using namespace mindspore::dataset;
  31. using mindspore::LogStream;
  32. using mindspore::ExceptionType::NoExceptionType;
  33. using mindspore::MsLogLevel::INFO;
  34. class MindDataTestZipOp : public UT::DatasetOpTesting {};
  35. TEST_F(MindDataTestZipOp, MindDataTestZipOpDefault) {
  36. /* Tree:
  37. *
  38. *
  39. * OpId(2) ZipOp
  40. * / \
  41. * OpId(0) TFReaderOp OpId(1) TFReaderOp
  42. * Start with an empty execution tree
  43. */
  44. Status rc;
  45. MS_LOG(INFO) << "UT test TestZipBasic.";
  46. auto my_tree = std::make_shared<ExecutionTree>();
  47. // Creating TFReaderOp
  48. std::string dataset_path = datasets_root_path_ + "/test_tf_file_3_images/train-0000-of-0001.data";
  49. std::string dataset_path2 = datasets_root_path_ + "/testBatchDataset/test.data";
  50. std::shared_ptr<TFReaderOp> my_tfreader_op;
  51. rc = TFReaderOp::Builder()
  52. .SetDatasetFilesList({dataset_path})
  53. .SetWorkerConnectorSize(16)
  54. .SetNumWorkers(1)
  55. .Build(&my_tfreader_op);
  56. EXPECT_TRUE(rc.IsOk());
  57. rc = my_tree->AssociateNode(my_tfreader_op);
  58. EXPECT_TRUE(rc.IsOk());
  59. std::shared_ptr<TFReaderOp> my_tfreader_op2;
  60. rc = TFReaderOp::Builder()
  61. .SetDatasetFilesList({dataset_path2})
  62. .SetWorkerConnectorSize(1)
  63. .SetNumWorkers(1)
  64. .Build(&my_tfreader_op2);
  65. EXPECT_TRUE(rc.IsOk());
  66. rc = my_tree->AssociateNode(my_tfreader_op2);
  67. EXPECT_TRUE(rc.IsOk());
  68. // Creating DatasetOp
  69. std::shared_ptr<ZipOp> zip_op;
  70. rc = ZipOp::Builder().Build(&zip_op);
  71. EXPECT_TRUE(rc.IsOk());
  72. rc = my_tree->AssociateNode(zip_op);
  73. EXPECT_TRUE(rc.IsOk());
  74. rc = zip_op->AddChild(std::move(my_tfreader_op));
  75. EXPECT_TRUE(rc.IsOk());
  76. rc = zip_op->AddChild(std::move(my_tfreader_op2));
  77. EXPECT_TRUE(rc.IsOk());
  78. rc = my_tree->AssignRoot(zip_op);
  79. EXPECT_TRUE(rc.IsOk());
  80. rc = my_tree->Prepare();
  81. EXPECT_TRUE(rc.IsOk());
  82. // Launch the tree execution to kick off threads and start running the pipeline
  83. MS_LOG(INFO) << "Launching my tree.";
  84. rc = my_tree->Launch();
  85. EXPECT_TRUE(rc.IsOk());
  86. // Simulate a parse of data from our pipeline.
  87. std::shared_ptr<DatasetOp> rootNode = my_tree->root();
  88. DatasetIterator di(my_tree);
  89. TensorRow tensor_list;
  90. rc = di.FetchNextTensorRow(&tensor_list);
  91. EXPECT_TRUE(rc.IsOk());
  92. int row_count = 0;
  93. while (!tensor_list.empty()) {
  94. MS_LOG(INFO) << "Row display for row #: " << row_count << ".";
  95. // Display the tensor by calling the printer on it
  96. for (int i = 0; i < tensor_list.size(); i++) {
  97. std::ostringstream ss;
  98. ss << "(" << tensor_list[i] << "): " << *tensor_list[i] << std::endl;
  99. MS_LOG(INFO) << "Tensor print: " << common::SafeCStr(ss.str()) << ".";
  100. }
  101. rc = di.FetchNextTensorRow(&tensor_list);
  102. EXPECT_TRUE(rc.IsOk());
  103. row_count++;
  104. }
  105. ASSERT_EQ(row_count, 3); // Should be 3 rows fetched
  106. }
  107. TEST_F(MindDataTestZipOp, MindDataTestZipOpRepeat) {
  108. /* Tree:
  109. * OpId(3) Repeat(3)
  110. *
  111. * OpId(2) ZipOp
  112. * / \
  113. * OpId(0) TFReaderOp OpId(1) TFReaderOp
  114. *
  115. * Start with an empty execution tree
  116. */
  117. Status rc;
  118. MS_LOG(INFO) << "UT test TestZipRepeat.";
  119. auto my_tree = std::make_shared<ExecutionTree>();
  120. uint32_t num_repeats = 3;
  121. std::string dataset_path = datasets_root_path_ + "/test_tf_file_3_images/train-0000-of-0001.data";
  122. std::string dataset_path2 = datasets_root_path_ + "/testBatchDataset/test.data";
  123. std::shared_ptr<TFReaderOp> my_tfreader_op;
  124. rc = TFReaderOp::Builder()
  125. .SetDatasetFilesList({dataset_path})
  126. .SetWorkerConnectorSize(16)
  127. .SetNumWorkers(1)
  128. .Build(&my_tfreader_op);
  129. EXPECT_TRUE(rc.IsOk());
  130. rc = my_tree->AssociateNode(my_tfreader_op);
  131. EXPECT_TRUE(rc.IsOk());
  132. std::shared_ptr<TFReaderOp> my_tfreader_op2;
  133. rc = TFReaderOp::Builder()
  134. .SetDatasetFilesList({dataset_path2})
  135. .SetWorkerConnectorSize(1)
  136. .SetNumWorkers(1)
  137. .Build(&my_tfreader_op2);
  138. EXPECT_TRUE(rc.IsOk());
  139. rc = my_tree->AssociateNode(my_tfreader_op2);
  140. EXPECT_TRUE(rc.IsOk());
  141. // Creating DatasetOp
  142. std::shared_ptr<ZipOp> zip_op;
  143. rc = ZipOp::Builder().Build(&zip_op);
  144. EXPECT_TRUE(rc.IsOk());
  145. rc = my_tree->AssociateNode(zip_op);
  146. EXPECT_TRUE(rc.IsOk());
  147. my_tfreader_op->set_total_repeats(num_repeats);
  148. my_tfreader_op->set_num_repeats_per_epoch(num_repeats);
  149. rc = zip_op->AddChild(std::move(my_tfreader_op));
  150. EXPECT_TRUE(rc.IsOk());
  151. my_tfreader_op2->set_total_repeats(num_repeats);
  152. my_tfreader_op2->set_num_repeats_per_epoch(num_repeats);
  153. rc = zip_op->AddChild(std::move(my_tfreader_op2));
  154. EXPECT_TRUE(rc.IsOk());
  155. // Builder(num_of_repeats)
  156. std::shared_ptr<RepeatOp> my_repeat_op;
  157. rc = RepeatOp::Builder(num_repeats).Build(&my_repeat_op);
  158. EXPECT_TRUE(rc.IsOk());
  159. rc = my_tree->AssociateNode(my_repeat_op);
  160. EXPECT_TRUE(rc.IsOk());
  161. zip_op->set_total_repeats(num_repeats);
  162. zip_op->set_num_repeats_per_epoch(num_repeats);
  163. rc = my_repeat_op->AddChild(zip_op);
  164. EXPECT_TRUE(rc.IsOk());
  165. rc = my_tree->AssignRoot(my_repeat_op);
  166. EXPECT_TRUE(rc.IsOk());
  167. rc = my_tree->Prepare();
  168. EXPECT_TRUE(rc.IsOk());
  169. // Launch the tree execution to kick off threads and start running the pipeline
  170. MS_LOG(INFO) << "Launching my tree.";
  171. rc = my_tree->Launch();
  172. EXPECT_TRUE(rc.IsOk());
  173. // Simulate a parse of data from our pipeline.
  174. std::shared_ptr<DatasetOp> rootNode = my_tree->root();
  175. DatasetIterator di(my_tree);
  176. TensorRow tensor_list;
  177. rc = di.FetchNextTensorRow(&tensor_list);
  178. EXPECT_TRUE(rc.IsOk());
  179. int row_count = 0;
  180. while (!tensor_list.empty()) {
  181. MS_LOG(INFO) << "Row display for row #: " << row_count << ".";
  182. // Display the tensor by calling the printer on it
  183. for (int i = 0; i < tensor_list.size(); i++) {
  184. std::ostringstream ss;
  185. ss << "(" << tensor_list[i] << "): " << *tensor_list[i] << std::endl;
  186. MS_LOG(INFO) << "Tensor print: " << common::SafeCStr(ss.str()) << ".";
  187. }
  188. rc = di.FetchNextTensorRow(&tensor_list);
  189. EXPECT_TRUE(rc.IsOk());
  190. row_count++;
  191. }
  192. ASSERT_EQ(row_count, 9); // Should be 9 rows fetched
  193. }