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