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.0 kB

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