• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright 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 <fstream>
17 #include <iostream>
18 
19 #include "common/common.h"
20 #include "minddata/dataset/include/dataset/datasets.h"
21 
22 using namespace mindspore::dataset;
23 using mindspore::dataset::Tensor;
24 
25 class MindDataTestPipeline : public UT::DatasetOpTesting {
26  protected:
27 };
28 
TEST_F(MindDataTestPipeline,TestCityscapesBasic)29 TEST_F(MindDataTestPipeline, TestCityscapesBasic) {
30   MS_LOG(INFO) << "Doing MindDataTestPipeline-TestCityscapesBasic.";
31 
32   std::string dataset_path = datasets_root_path_ + "/testCityscapesData/cityscapes";
33   std::string usage = "train";        // quality_mode=fine 'train', 'test', 'val'  else 'train', 'train_extra', 'val'
34   std::string quality_mode = "fine";  // fine coarse
35   std::string task = "color";         // instance semantic polygon color
36 
37   // Create a Cityscapes Dataset
38   std::shared_ptr<Dataset> ds = Cityscapes(dataset_path, usage, quality_mode, task);
39   EXPECT_NE(ds, nullptr);
40 
41   // Create an iterator over the result of the above dataset
42   // This will trigger the creation of the Execution Tree and launch it.
43   std::shared_ptr<Iterator> iter = ds->CreateIterator();
44   EXPECT_NE(iter, nullptr);
45 
46   // Iterate the dataset and get each row
47   std::unordered_map<std::string, mindspore::MSTensor> row;
48   ASSERT_OK(iter->GetNextRow(&row));
49 
50   uint64_t i = 0;
51   while (row.size() != 0) {
52     i++;
53     auto image = row["image"];
54     auto task = row["task"];
55     MS_LOG(INFO) << "Tensor image shape: " << image.Shape();
56     ASSERT_OK(iter->GetNextRow(&row));
57   }
58 
59   EXPECT_EQ(i, 5);
60 
61   // Manually terminate the pipeline
62   iter->Stop();
63 }
64 
TEST_F(MindDataTestPipeline,TestCityscapesBasicWithPipeline)65 TEST_F(MindDataTestPipeline, TestCityscapesBasicWithPipeline) {
66   MS_LOG(INFO) << "Doing MindDataTestPipeline-TestCityscapesBasicWithPipeline.";
67 
68   std::string dataset_path = datasets_root_path_ + "/testCityscapesData/cityscapes";
69   std::string usage = "train";        // quality_mode=fine 'train', 'test', 'val'  else 'train', 'train_extra', 'val'
70   std::string quality_mode = "fine";  // fine coarse
71 
72   // Create two Cityscapes Dataset
73   std::shared_ptr<Dataset> ds1 =
74     Cityscapes(dataset_path, usage, quality_mode, "color", false, std::make_shared<RandomSampler>(false, 2));
75   std::shared_ptr<Dataset> ds2 =
76     Cityscapes(dataset_path, usage, quality_mode, "color", false, std::make_shared<RandomSampler>(false, 3));
77   EXPECT_NE(ds1, nullptr);
78   EXPECT_NE(ds2, nullptr);
79 
80   // Create two Repeat operation on ds
81   int32_t repeat_num = 3;
82   ds1 = ds1->Repeat(repeat_num);
83   EXPECT_NE(ds1, nullptr);
84   repeat_num = 2;
85   ds2 = ds2->Repeat(repeat_num);
86   EXPECT_NE(ds2, nullptr);
87 
88   // Create two Project operation on ds
89   std::vector<std::string> column_project = {"image"};
90   ds1 = ds1->Project(column_project);
91   EXPECT_NE(ds1, nullptr);
92   ds2 = ds2->Project(column_project);
93   EXPECT_NE(ds2, nullptr);
94 
95   // Create a Concat operation on the ds
96   ds1 = ds1->Concat({ds2});
97   EXPECT_NE(ds1, nullptr);
98 
99   // Create an iterator over the result of the above dataset
100   // This will trigger the creation of the Execution Tree and launch it.
101   std::shared_ptr<Iterator> iter = ds1->CreateIterator();
102   EXPECT_NE(iter, nullptr);
103 
104   // Iterate the dataset and get each row
105   std::unordered_map<std::string, mindspore::MSTensor> row;
106   ASSERT_OK(iter->GetNextRow(&row));
107 
108   uint64_t i = 0;
109   while (row.size() != 0) {
110     i++;
111     auto image = row["image"];
112     MS_LOG(INFO) << "Tensor image shape: " << image.Shape();
113     ASSERT_OK(iter->GetNextRow(&row));
114   }
115 
116   EXPECT_EQ(i, 12);
117 
118   // Manually terminate the pipeline
119   iter->Stop();
120 }
121 
TEST_F(MindDataTestPipeline,TestCityscapesGetters)122 TEST_F(MindDataTestPipeline, TestCityscapesGetters) {
123   MS_LOG(INFO) << "Doing MindDataTestPipeline-TestCityscapesGetters.";
124 
125   std::string dataset_path = datasets_root_path_ + "/testCityscapesData/cityscapes";
126   std::string usage = "train";        // quality_mode=fine 'train', 'test', 'val'  else 'train', 'train_extra', 'val'
127   std::string quality_mode = "fine";  // fine coarse
128   std::string task = "color";         // instance semantic polygon color
129 
130   // Create a Cityscapes Dataset
131   std::shared_ptr<Dataset> ds1 =
132     Cityscapes(dataset_path, usage, quality_mode, task, false, std::make_shared<RandomSampler>(false, 4));
133   std::shared_ptr<Dataset> ds2 = Cityscapes(dataset_path, usage, quality_mode, task);
134   std::vector<std::string> column_names = {"image", "task"};
135 
136   EXPECT_NE(ds1, nullptr);
137   EXPECT_EQ(ds1->GetDatasetSize(), 4);
138   EXPECT_EQ(ds1->GetColumnNames(), column_names);
139   EXPECT_EQ(ds1->GetBatchSize(), 1);
140 
141   EXPECT_NE(ds2, nullptr);
142   EXPECT_EQ(ds2->GetDatasetSize(), 5);
143   EXPECT_EQ(ds2->GetColumnNames(), column_names);
144   EXPECT_EQ(ds2->GetBatchSize(), 1);
145 }
146 
TEST_F(MindDataTestPipeline,TestCityscapesTaskJson)147 TEST_F(MindDataTestPipeline, TestCityscapesTaskJson) {
148   MS_LOG(INFO) << "Doing MindDataTestPipeline-TestCityscapesTaskJson.";
149 
150   std::string dataset_path = datasets_root_path_ + "/testCityscapesData/cityscapes/testTaskJson";
151   std::string usage = "train";        // quality_mode=fine 'train', 'test', 'val'  else 'train', 'train_extra', 'val'
152   std::string quality_mode = "fine";  // fine coarse
153   std::string task = "polygon";       // instance semantic polygon color
154 
155   std::shared_ptr<Dataset> ds = Cityscapes(dataset_path, usage, quality_mode, task);
156 
157   // Create an iterator over the result of the above dataset
158   // This will trigger the creation of the Execution Tree and launch it.
159   std::shared_ptr<Iterator> iter = ds->CreateIterator();
160   EXPECT_NE(iter, nullptr);
161 
162   // Iterate the dataset and get each row
163   std::unordered_map<std::string, mindspore::MSTensor> row;
164   ASSERT_OK(iter->GetNextRow(&row));
165 
166   std::string json_file_path = dataset_path + "/gtFine/train/aa/aa_000000_gtFine_polygons.json";
167   std::ifstream file_handle(json_file_path);
168   std::string contents((std::istreambuf_iterator<char>(file_handle)), std::istreambuf_iterator<char>());
169   nlohmann::json contents_js = nlohmann::json::parse(contents);
170   std::shared_ptr<Tensor> t_expect_item;
171   Tensor::CreateScalar(contents_js.dump(), &t_expect_item);
172   file_handle.close();
173 
174   mindspore::MSTensor expect_item = mindspore::MSTensor(std::make_shared<mindspore::dataset::DETensor>(t_expect_item));
175 
176   uint64_t i = 0;
177   while (row.size() != 0) {
178     i++;
179     auto image = row["image"];
180     auto task = row["task"];
181     MS_LOG(INFO) << "Tensor image shape: " << image.Shape();
182     MS_LOG(INFO) << "Tensor task shape: " << task.Shape();
183 
184     EXPECT_MSTENSOR_EQ(task, expect_item);
185     ASSERT_OK(iter->GetNextRow(&row));
186   }
187 
188   EXPECT_EQ(i, 1);
189 
190   // Manually terminate the pipeline
191   iter->Stop();
192 }
193 
TEST_F(MindDataTestPipeline,TestCityscapesDecode)194 TEST_F(MindDataTestPipeline, TestCityscapesDecode) {
195   MS_LOG(INFO) << "Doing MindDataTestPipeline-TestCityscapesDecode.";
196 
197   std::string dataset_path = datasets_root_path_ + "/testCityscapesData/cityscapes";
198   std::string usage = "train";        // quality_mode=fine 'train', 'test', 'val' else 'train', 'train_extra', 'val'
199   std::string quality_mode = "fine";  // fine coarse
200   std::string task = "color";         // instance semantic polygon color
201 
202   // Create a Cityscapes Dataset
203   std::shared_ptr<Dataset> ds =
204     Cityscapes(dataset_path, usage, quality_mode, task, true, std::make_shared<RandomSampler>());
205   EXPECT_NE(ds, nullptr);
206 
207   // Create an iterator over the result of the above dataset
208   // This will trigger the creation of the Execution Tree and launch it.
209   std::shared_ptr<Iterator> iter = ds->CreateIterator();
210   EXPECT_NE(iter, nullptr);
211 
212   // Iterate the dataset and get each row
213   std::unordered_map<std::string, mindspore::MSTensor> row;
214   ASSERT_OK(iter->GetNextRow(&row));
215 
216   uint64_t i = 0;
217   while (row.size() != 0) {
218     i++;
219     auto image = row["image"];
220     auto task = row["task"];
221 
222     EXPECT_EQ(image.Shape().size(), 3);
223     EXPECT_EQ(task.Shape().size(), 3);
224 
225     ASSERT_OK(iter->GetNextRow(&row));
226   }
227 
228   EXPECT_EQ(i, 5);
229 
230   // Manually terminate the pipeline
231   iter->Stop();
232 }
233 
TEST_F(MindDataTestPipeline,TestCityscapesNumSamplers)234 TEST_F(MindDataTestPipeline, TestCityscapesNumSamplers) {
235   MS_LOG(INFO) << "Doing MindDataTestPipeline-TestCityscapesNumSamplers.";
236 
237   std::string dataset_path = datasets_root_path_ + "/testCityscapesData/cityscapes";
238   std::string usage = "train";        // quality_mode=fine 'train', 'test', 'val'  else 'train', 'train_extra', 'val'
239   std::string quality_mode = "fine";  // fine coarse
240   std::string task = "color";         // instance semantic polygon color
241 
242   // Create a Cityscapes Dataset
243   std::shared_ptr<Dataset> ds =
244     Cityscapes(dataset_path, usage, quality_mode, task, true, std::make_shared<RandomSampler>(false, 5));
245   EXPECT_NE(ds, nullptr);
246 
247   // Create an iterator over the result of the above dataset
248   // This will trigger the creation of the Execution Tree and launch it.
249   std::shared_ptr<Iterator> iter = ds->CreateIterator();
250   EXPECT_NE(iter, nullptr);
251 
252   // Iterate the dataset and get each row
253   std::unordered_map<std::string, mindspore::MSTensor> row;
254   ASSERT_OK(iter->GetNextRow(&row));
255 
256   uint64_t i = 0;
257   while (row.size() != 0) {
258     i++;
259     auto image = row["image"];
260     auto task = row["task"];
261     MS_LOG(INFO) << "Tensor image shape: " << image.Shape();
262     MS_LOG(INFO) << "Tensor task shape: " << task.Shape();
263 
264     ASSERT_OK(iter->GetNextRow(&row));
265   }
266 
267   EXPECT_EQ(i, 5);
268 
269   // Manually terminate the pipeline
270   iter->Stop();
271 }
272 
TEST_F(MindDataTestPipeline,TestCityscapesError)273 TEST_F(MindDataTestPipeline, TestCityscapesError) {
274   MS_LOG(INFO) << "Doing MindDataTestPipeline-TestCityscapesError.";
275 
276   std::string dataset_path = datasets_root_path_ + "/testCityscapesData/cityscapes";
277   std::string usage = "train";        // quality_mode=fine 'train', 'test', 'val'  else 'train', 'train_extra', 'val'
278   std::string quality_mode = "fine";  // fine coarse
279   std::string task = "color";      // instance semantic polygon color
280 
281   // Create a Cityscapes Dataset with non-existing dataset dir
282   std::shared_ptr<Dataset> ds0 = Cityscapes("NotExistDir", usage, quality_mode, task);
283   EXPECT_NE(ds0, nullptr);
284 
285   // Create an iterator over the result of the above dataset
286   std::shared_ptr<Iterator> iter0 = ds0->CreateIterator();
287   // Expect failure: invalid Cityscapes input
288   EXPECT_EQ(iter0, nullptr);
289 
290   // Create a Cityscapes Dataset with err task
291   std::shared_ptr<Dataset> ds1 = Cityscapes(dataset_path, usage, quality_mode, "task");
292   EXPECT_NE(ds1, nullptr);
293 
294   // Create an iterator over the result of the above dataset
295   std::shared_ptr<Iterator> iter1 = ds1->CreateIterator();
296   // Expect failure: invalid Cityscapes input
297   EXPECT_EQ(iter1, nullptr);
298 
299   // Create a Cityscapes Dataset with err quality_mode
300   std::shared_ptr<Dataset> ds2 = Cityscapes(dataset_path, usage, "quality_mode", task);
301   EXPECT_NE(ds2, nullptr);
302 
303   // Create an iterator over the result of the above dataset
304   std::shared_ptr<Iterator> iter2 = ds2->CreateIterator();
305   // Expect failure: invalid Cityscapes input
306   EXPECT_EQ(iter2, nullptr);
307 
308   // Create a Cityscapes Dataset with err usage
309   std::shared_ptr<Dataset> ds3 = Cityscapes(dataset_path, "usage", quality_mode, task);
310   EXPECT_NE(ds3, nullptr);
311 
312   // Create an iterator over the result of the above dataset
313   std::shared_ptr<Iterator> iter3 = ds3->CreateIterator();
314   // Expect failure: invalid Cityscapes input
315   EXPECT_EQ(iter3, nullptr);
316 }
317 
TEST_F(MindDataTestPipeline,TestCityscapesWithNullSamplerError)318 TEST_F(MindDataTestPipeline, TestCityscapesWithNullSamplerError) {
319   MS_LOG(INFO) << "Doing MindDataTestPipeline-TestCityscapesWithNullSamplerError.";
320 
321   std::string dataset_path = datasets_root_path_ + "/testCityscapesData/cityscapes";
322   std::string usage = "train";        // quality_mode=fine 'train', 'test', 'val'  else 'train', 'train_extra', 'val'
323   std::string quality_mode = "fine";  // fine coarse
324   std::string task = "color";      // instance semantic polygon color
325 
326   // Create a Cityscapes Dataset
327   std::shared_ptr<Dataset> ds = Cityscapes(dataset_path, usage, quality_mode, task, false, nullptr);
328   EXPECT_NE(ds, nullptr);
329 
330   // Create an iterator over the result of the above dataset
331   std::shared_ptr<Iterator> iter = ds->CreateIterator();
332   // Expect failure: invalid Cityscapes input, sampler cannot be nullptr
333   EXPECT_EQ(iter, nullptr);
334 }