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

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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/core/tensor.h"
  23. #include "minddata/dataset/core/config_manager.h"
  24. #include "minddata/dataset/engine/datasetops/zip_op.h"
  25. #include "minddata/dataset/engine/jagged_connector.h"
  26. #include "common/common.h"
  27. #include "utils/ms_utils.h"
  28. #include "gtest/gtest.h"
  29. #include "utils/log_adapter.h"
  30. namespace common = mindspore::common;
  31. using namespace mindspore::dataset;
  32. using mindspore::LogStream;
  33. using mindspore::ExceptionType::NoExceptionType;
  34. using mindspore::MsLogLevel::INFO;
  35. class MindDataTestZipOp : public UT::DatasetOpTesting {};
  36. TEST_F(MindDataTestZipOp, MindDataTestZipOpDefault) {
  37. /* Tree:
  38. *
  39. *
  40. * OpId(2) ZipOp
  41. * / \
  42. * OpId(0) TFReaderOp OpId(1) TFReaderOp
  43. * Start with an empty execution tree
  44. */
  45. Status rc;
  46. MS_LOG(INFO) << "UT test TestZipBasic.";
  47. auto my_tree = std::make_shared<ExecutionTree>();
  48. // Creating TFReaderOp
  49. std::string dataset_path = datasets_root_path_ + "/test_tf_file_3_images/train-0000-of-0001.data";
  50. std::string dataset_path2 = datasets_root_path_ + "/testBatchDataset/test.data";
  51. std::shared_ptr<ConfigManager> config_manager = GlobalContext::config_manager();
  52. std::vector<std::string> columns_to_load = {};
  53. std::unique_ptr<DataSchema> schema = std::make_unique<DataSchema>();
  54. std::vector<std::string> files1 = {dataset_path};
  55. auto op_connector_size = config_manager->op_connector_size();
  56. std::shared_ptr<TFReaderOp> my_tfreader_op = std::make_shared<TFReaderOp>(
  57. 1, 16, 0, files1, std::move(schema), op_connector_size, columns_to_load, false, 1, 0, false);
  58. rc = my_tfreader_op->Init();
  59. EXPECT_TRUE(rc.IsOk());
  60. rc = my_tree->AssociateNode(my_tfreader_op);
  61. EXPECT_TRUE(rc.IsOk());
  62. std::vector<std::string> files2 = {dataset_path2};
  63. std::unique_ptr<DataSchema> schema2 = std::make_unique<DataSchema>();
  64. std::shared_ptr<TFReaderOp> my_tfreader_op2 = std::make_shared<TFReaderOp>(
  65. 1, 1, 0, files2, std::make_unique<DataSchema>(), op_connector_size, columns_to_load, false, 1, 0, false);
  66. rc = my_tfreader_op2->Init();
  67. EXPECT_TRUE(rc.IsOk());
  68. rc = my_tree->AssociateNode(my_tfreader_op2);
  69. EXPECT_TRUE(rc.IsOk());
  70. // Creating DatasetOp
  71. std::shared_ptr<ZipOp> zip_op = std::make_shared<ZipOp>();
  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<ConfigManager> config_manager = GlobalContext::config_manager();
  124. auto op_connector_size = config_manager->op_connector_size();
  125. std::vector<std::string> columns_to_load = {};
  126. std::vector<std::string> files1 = {dataset_path};
  127. std::unique_ptr<DataSchema> schema1 = std::make_unique<DataSchema>();
  128. std::shared_ptr<TFReaderOp> my_tfreader_op = std::make_shared<TFReaderOp>(
  129. 1, 16, 0, files1, std::move(schema1), op_connector_size, columns_to_load, false, 1, 0, false);
  130. rc = my_tfreader_op->Init();
  131. EXPECT_TRUE(rc.IsOk());
  132. rc = my_tree->AssociateNode(my_tfreader_op);
  133. rc = my_tree->AssociateNode(my_tfreader_op);
  134. EXPECT_TRUE(rc.IsOk());
  135. std::vector<std::string> files2 = {dataset_path2};
  136. std::unique_ptr<DataSchema> schema2 = std::make_unique<DataSchema>();
  137. std::shared_ptr<TFReaderOp> my_tfreader_op2 = std::make_shared<TFReaderOp>(
  138. 1, 1, 0, files2, std::move(schema2), op_connector_size, columns_to_load, false, 1, 0, false);
  139. rc = my_tfreader_op2->Init();
  140. EXPECT_TRUE(rc.IsOk());
  141. rc = my_tree->AssociateNode(my_tfreader_op2);
  142. EXPECT_TRUE(rc.IsOk());
  143. // Creating DatasetOp
  144. std::shared_ptr<ZipOp> zip_op = std::make_shared<ZipOp>();
  145. rc = my_tree->AssociateNode(zip_op);
  146. EXPECT_TRUE(rc.IsOk());
  147. my_tfreader_op->SetTotalRepeats(num_repeats);
  148. my_tfreader_op->SetNumRepeatsPerEpoch(num_repeats);
  149. rc = zip_op->AddChild(std::move(my_tfreader_op));
  150. EXPECT_TRUE(rc.IsOk());
  151. my_tfreader_op2->SetTotalRepeats(num_repeats);
  152. my_tfreader_op2->SetNumRepeatsPerEpoch(num_repeats);
  153. rc = zip_op->AddChild(std::move(my_tfreader_op2));
  154. EXPECT_TRUE(rc.IsOk());
  155. std::shared_ptr<RepeatOp> my_repeat_op = std::make_shared<RepeatOp>(num_repeats);
  156. rc = my_tree->AssociateNode(my_repeat_op);
  157. EXPECT_TRUE(rc.IsOk());
  158. zip_op->SetTotalRepeats(num_repeats);
  159. zip_op->SetNumRepeatsPerEpoch(num_repeats);
  160. rc = my_repeat_op->AddChild(zip_op);
  161. EXPECT_TRUE(rc.IsOk());
  162. rc = my_tree->AssignRoot(my_repeat_op);
  163. EXPECT_TRUE(rc.IsOk());
  164. rc = my_tree->Prepare();
  165. EXPECT_TRUE(rc.IsOk());
  166. // Launch the tree execution to kick off threads and start running the pipeline
  167. MS_LOG(INFO) << "Launching my tree.";
  168. rc = my_tree->Launch();
  169. EXPECT_TRUE(rc.IsOk());
  170. // Simulate a parse of data from our pipeline.
  171. std::shared_ptr<DatasetOp> rootNode = my_tree->root();
  172. DatasetIterator di(my_tree);
  173. TensorRow tensor_list;
  174. rc = di.FetchNextTensorRow(&tensor_list);
  175. EXPECT_TRUE(rc.IsOk());
  176. int row_count = 0;
  177. while (!tensor_list.empty()) {
  178. MS_LOG(INFO) << "Row display for row #: " << row_count << ".";
  179. // Display the tensor by calling the printer on it
  180. for (int i = 0; i < tensor_list.size(); i++) {
  181. std::ostringstream ss;
  182. ss << "(" << tensor_list[i] << "): " << *tensor_list[i] << std::endl;
  183. MS_LOG(INFO) << "Tensor print: " << common::SafeCStr(ss.str()) << ".";
  184. }
  185. rc = di.FetchNextTensorRow(&tensor_list);
  186. EXPECT_TRUE(rc.IsOk());
  187. row_count++;
  188. }
  189. ASSERT_EQ(row_count, 9); // Should be 9 rows fetched
  190. }