• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright 2020-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 "common/common.h"
17 #include "minddata/dataset/include/dataset/datasets.h"
18 #include "minddata/dataset/include/dataset/transforms.h"
19 #include "minddata/dataset/include/dataset/vision.h"
20 
21 using namespace mindspore::dataset;
22 
23 class MindDataTestPipeline : public UT::DatasetOpTesting {
24  protected:
25 };
26 
27 // Tests for vision C++ API R to Z TensorTransform Operations (in alphabetical order)
28 
TEST_F(MindDataTestPipeline,TestRescaleSucess1)29 TEST_F(MindDataTestPipeline, TestRescaleSucess1) {
30   MS_LOG(INFO) << "Doing MindDataTestPipeline-TestRescaleSucess1.";
31   // Create an ImageFolder Dataset
32   std::string folder_path = datasets_root_path_ + "/testPK/data/";
33   std::shared_ptr<Dataset> ds = ImageFolder(folder_path, true, std::make_shared<SequentialSampler>(0, 1));
34   EXPECT_NE(ds, nullptr);
35 
36   // Create an iterator over the result of the above dataset
37   // This will trigger the creation of the Execution Tree and launch it.
38   std::shared_ptr<Iterator> iter = ds->CreateIterator();
39   EXPECT_NE(iter, nullptr);
40 
41   // Iterate the dataset and get each row
42   std::unordered_map<std::string, mindspore::MSTensor> row;
43   ASSERT_OK(iter->GetNextRow(&row));
44 
45   auto image = row["image"];
46 
47   // Create objects for the tensor ops
48   std::shared_ptr<TensorTransform> rescale(new mindspore::dataset::vision::Rescale(1.0, 0.0));
49   // Note: No need to check for output after calling API class constructor
50 
51   // Convert to the same type
52   std::shared_ptr<TensorTransform> type_cast(new transforms::TypeCast(mindspore::DataType::kNumberTypeUInt8));
53   // Note: No need to check for output after calling API class constructor
54 
55   ds = ds->Map({rescale, type_cast}, {"image"});
56   EXPECT_NE(ds, nullptr);
57 
58   // Create an iterator over the result of the above dataset
59   // This will trigger the creation of the Execution Tree and launch it.
60   std::shared_ptr<Iterator> iter1 = ds->CreateIterator();
61   EXPECT_NE(iter1, nullptr);
62 
63   // Iterate the dataset and get each row1
64   std::unordered_map<std::string, mindspore::MSTensor> row1;
65   ASSERT_OK(iter1->GetNextRow(&row1));
66 
67   auto image1 = row1["image"];
68 
69   EXPECT_MSTENSOR_EQ(image, image1);
70 
71   // Manually terminate the pipeline
72   iter1->Stop();
73 }
74 
TEST_F(MindDataTestPipeline,TestRescaleSucess2)75 TEST_F(MindDataTestPipeline, TestRescaleSucess2) {
76   MS_LOG(INFO) << "Doing MindDataTestPipeline-TestRescaleSucess2 with different params.";
77   // Create an ImageFolder Dataset
78   std::string folder_path = datasets_root_path_ + "/testPK/data/";
79   std::shared_ptr<Dataset> ds = ImageFolder(folder_path, true, std::make_shared<RandomSampler>(false, 1));
80   EXPECT_NE(ds, nullptr);
81 
82   // Create objects for the tensor ops
83   std::shared_ptr<TensorTransform> rescale(new mindspore::dataset::vision::Rescale(1.0 / 255, 1.0));
84   // Note: No need to check for output after calling API class constructor
85 
86   ds = ds->Map({rescale}, {"image"});
87   EXPECT_NE(ds, nullptr);
88 
89   // Create an iterator over the result of the above dataset
90   // This will trigger the creation of the Execution Tree and launch it.
91   std::shared_ptr<Iterator> iter = ds->CreateIterator();
92   EXPECT_NE(iter, nullptr);
93 
94   // Iterate the dataset and get each row
95   std::unordered_map<std::string, mindspore::MSTensor> row;
96   ASSERT_OK(iter->GetNextRow(&row));
97 
98   uint64_t i = 0;
99   while (row.size() != 0) {
100     i++;
101     auto image = row["image"];
102     MS_LOG(INFO) << "Tensor image shape: " << image.Shape();
103     ASSERT_OK(iter->GetNextRow(&row));
104   }
105 
106   EXPECT_EQ(i, 1);
107 
108   // Manually terminate the pipeline
109   iter->Stop();
110 }
111 
TEST_F(MindDataTestPipeline,TestResize1)112 TEST_F(MindDataTestPipeline, TestResize1) {
113   MS_LOG(INFO) << "Doing MindDataTestPipeline-TestResize1 with single integer input.";
114   // Create an ImageFolder Dataset
115   std::string folder_path = datasets_root_path_ + "/testPK/data/";
116   std::shared_ptr<Dataset> ds = ImageFolder(folder_path, true, std::make_shared<RandomSampler>(false, 6));
117   EXPECT_NE(ds, nullptr);
118 
119   // Create a Repeat operation on ds
120   int32_t repeat_num = 4;
121   ds = ds->Repeat(repeat_num);
122   EXPECT_NE(ds, nullptr);
123 
124   // Create resize object with single integer input
125   std::shared_ptr<TensorTransform> resize_op(new vision::Resize({30}));
126   // Note: No need to check for output after calling API class constructor
127 
128   // Create a Map operation on ds
129   ds = ds->Map({resize_op});
130   EXPECT_NE(ds, nullptr);
131 
132   // Create a Batch operation on ds
133   int32_t batch_size = 1;
134   ds = ds->Batch(batch_size);
135   EXPECT_NE(ds, nullptr);
136 
137   // Create an iterator over the result of the above dataset
138   // This will trigger the creation of the Execution Tree and launch it.
139   std::shared_ptr<Iterator> iter = ds->CreateIterator();
140   EXPECT_NE(iter, nullptr);
141 
142   // Iterate the dataset and get each row
143   std::unordered_map<std::string, mindspore::MSTensor> row;
144   ASSERT_OK(iter->GetNextRow(&row));
145 
146   uint64_t i = 0;
147   while (row.size() != 0) {
148     i++;
149     auto image = row["image"];
150     MS_LOG(INFO) << "Tensor image shape: " << image.Shape();
151     ASSERT_OK(iter->GetNextRow(&row));
152   }
153 
154   EXPECT_EQ(i, 24);
155 
156   // Manually terminate the pipeline
157   iter->Stop();
158 }
159 
TEST_F(MindDataTestPipeline,TestResizeWithBBoxSuccess)160 TEST_F(MindDataTestPipeline, TestResizeWithBBoxSuccess) {
161   MS_LOG(INFO) << "Doing MindDataTestPipeline-TestResizeWithBBoxSuccess.";
162   // Create an VOC Dataset
163   std::string folder_path = datasets_root_path_ + "/testVOC2012_2";
164   std::shared_ptr<Dataset> ds =
165     VOC(folder_path, "Detection", "train", {}, true, std::make_shared<SequentialSampler>(0, 3));
166   EXPECT_NE(ds, nullptr);
167 
168   // Create objects for the tensor ops
169   std::shared_ptr<TensorTransform> resize_with_bbox_op(new vision::ResizeWithBBox({30}));
170   std::shared_ptr<TensorTransform> resize_with_bbox_op1(new vision::ResizeWithBBox({30, 30}));
171   // Note: No need to check for output after calling API class constructor
172 
173   // Create a Map operation on ds
174   ds = ds->Map({resize_with_bbox_op, resize_with_bbox_op1}, {"image", "bbox"}, {"image", "bbox"}, {"image", "bbox"});
175   EXPECT_NE(ds, nullptr);
176 
177   // Create an iterator over the result of the above dataset
178   // This will trigger the creation of the Execution Tree and launch it.
179   std::shared_ptr<Iterator> iter = ds->CreateIterator();
180   EXPECT_NE(iter, nullptr);
181 
182   // Iterate the dataset and get each row
183   std::unordered_map<std::string, mindspore::MSTensor> row;
184   ASSERT_OK(iter->GetNextRow(&row));
185 
186   uint64_t i = 0;
187   while (row.size() != 0) {
188     i++;
189     auto image = row["image"];
190     MS_LOG(INFO) << "Tensor image shape: " << image.Shape();
191     ASSERT_OK(iter->GetNextRow(&row));
192   }
193 
194   EXPECT_EQ(i, 3);
195   // Manually terminate the pipeline
196   iter->Stop();
197 }
198 
TEST_F(MindDataTestPipeline,TestRGB2GRAYSucess)199 TEST_F(MindDataTestPipeline, TestRGB2GRAYSucess) {
200   MS_LOG(INFO) << "Doing MindDataTestPipeline-TestRGB2GRAYSucess.";
201   // Create an ImageFolder Dataset
202   std::string folder_path = datasets_root_path_ + "/testPK/data/";
203   std::shared_ptr<Dataset> ds = ImageFolder(folder_path, true, std::make_shared<SequentialSampler>(0, 1));
204   EXPECT_NE(ds, nullptr);
205 
206   // Create objects for the tensor ops
207   std::shared_ptr<TensorTransform> convert(new mindspore::dataset::vision::RGB2GRAY());
208 
209   ds = ds->Map({convert});
210   EXPECT_NE(ds, nullptr);
211 
212   // Create an iterator over the result of the above dataset
213   // This will trigger the creation of the Execution Tree and launch it.
214   std::shared_ptr<Iterator> iter = ds->CreateIterator();
215   EXPECT_NE(iter, nullptr);
216 
217   // Iterate the dataset and get each row
218   std::unordered_map<std::string, mindspore::MSTensor> row;
219   ASSERT_OK(iter->GetNextRow(&row));
220 
221   uint64_t i = 0;
222   while (row.size() != 0) {
223     i++;
224     auto image = row["image"];
225     MS_LOG(INFO) << "Tensor image shape: " << image.Shape();
226     ASSERT_OK(iter->GetNextRow(&row));
227   }
228 
229   EXPECT_EQ(i, 1);
230 
231   // Manually terminate the pipeline
232   iter->Stop();
233 }
234 
TEST_F(MindDataTestPipeline,TestRotateParamCheck)235 TEST_F(MindDataTestPipeline, TestRotateParamCheck) {
236   MS_LOG(INFO) << "Doing MindDataTestPipeline-TestRotateParamCheck with invalid parameters.";
237   // Create an ImageFolder Dataset
238   std::string folder_path = datasets_root_path_ + "/testPK/data/";
239   std::shared_ptr<Dataset> ds = ImageFolder(folder_path, true, std::make_shared<RandomSampler>(false, 10));
240   EXPECT_NE(ds, nullptr);
241 
242   // Case 1: Size of center is not 2
243   // Create objects for the tensor ops
244   std::shared_ptr<TensorTransform> rotate1(new vision::Rotate(90.0, InterpolationMode::kNearestNeighbour, false, {0.}));
245   auto ds2 = ds->Map({rotate1});
246   EXPECT_NE(ds2, nullptr);
247   // Create an iterator over the result of the above dataset
248   std::shared_ptr<Iterator> iter2 = ds2->CreateIterator();
249   // Expect failure: invalid center for Rotate
250   EXPECT_EQ(iter2, nullptr);
251 
252   // Case 2: Size of fill_value is not 1 or 3
253   // Create objects for the tensor ops
254   std::shared_ptr<TensorTransform> rotate2(
255     new vision::Rotate(-30, InterpolationMode::kNearestNeighbour, false, {1.0, 1.0}, {2, 2}));
256   auto ds3 = ds->Map({rotate2});
257   EXPECT_NE(ds3, nullptr);
258   // Create an iterator over the result of the above dataset
259   std::shared_ptr<Iterator> iter3 = ds3->CreateIterator();
260   // Expect failure: invalid fill_value for Rotate
261   EXPECT_EQ(iter3, nullptr);
262 }
263 
TEST_F(MindDataTestPipeline,TestRotatePass)264 TEST_F(MindDataTestPipeline, TestRotatePass) {
265   MS_LOG(INFO) << "Doing MindDataTestPipeline-TestRotatePass.";
266 
267   // Create an ImageFolder Dataset
268   std::string folder_path = datasets_root_path_ + "/testPK/data/";
269   std::shared_ptr<Dataset> ds = ImageFolder(folder_path, true, std::make_shared<RandomSampler>(false, 10));
270   EXPECT_NE(ds, nullptr);
271 
272   // Create objects for the tensor ops
273   std::shared_ptr<TensorTransform> resize(new vision::Resize({50, 25}));
274 
275   std::shared_ptr<TensorTransform> rotate(
276     new vision::Rotate(90, InterpolationMode::kLinear, true, {-1, -1}, {255, 255, 255}));
277 
278   // Resize the image to 50 * 25
279   ds = ds->Map({resize});
280   EXPECT_NE(ds, nullptr);
281 
282   // Rotate the image 90 degrees
283   ds = ds->Map({rotate});
284   EXPECT_NE(ds, nullptr);
285 
286   // Create a Batch operation on ds
287   int32_t batch_size = 1;
288   ds = ds->Batch(batch_size);
289   EXPECT_NE(ds, nullptr);
290 
291   // Create an iterator over the result of the above dataset
292   // This will trigger the creation of the Execution Tree and launch it.
293   std::shared_ptr<Iterator> iter = ds->CreateIterator();
294   EXPECT_NE(iter, nullptr);
295 
296   // Iterate the dataset and get each row
297   std::unordered_map<std::string, mindspore::MSTensor> row;
298   ASSERT_OK(iter->GetNextRow(&row));
299 
300   uint64_t i = 0;
301   while (row.size() != 0) {
302     i++;
303     auto image = row["image"];
304     // After rotation with expanding, the image size comes to 25 * 50
305     EXPECT_EQ(image.Shape()[1], 25);
306     EXPECT_EQ(image.Shape()[2], 50);
307     ASSERT_OK(iter->GetNextRow(&row));
308   }
309 
310   EXPECT_EQ(i, 10);
311 
312   // Manually terminate the pipeline
313   iter->Stop();
314 }
315 
TEST_F(MindDataTestPipeline,TestRGB2BGR)316 TEST_F(MindDataTestPipeline, TestRGB2BGR) {
317   MS_LOG(INFO) << "Doing MindDataTestPipeline-TestRGB2BGR.";
318   // create two imagenet dataset
319   std::string MindDataPath = "data/dataset";
320   std::string folder_path = MindDataPath + "/testImageNetData/train/";
321   std::shared_ptr<Dataset> ds1 = ImageFolder(folder_path, true, std::make_shared<RandomSampler>(false, 2));
322   EXPECT_NE(ds1, nullptr);
323   std::shared_ptr<Dataset> ds2 = ImageFolder(folder_path, true, std::make_shared<RandomSampler>(false, 2));
324   EXPECT_NE(ds2, nullptr);
325 
326   auto rgb2bgr_op = vision::RGB2BGR();
327 
328   ds1 = ds1->Map({rgb2bgr_op});
329   EXPECT_NE(ds1, nullptr);
330 
331   std::shared_ptr<Iterator> iter1 = ds1->CreateIterator();
332   EXPECT_NE(iter1, nullptr);
333   std::unordered_map<std::string, mindspore::MSTensor> row1;
334   iter1->GetNextRow(&row1);
335 
336   std::shared_ptr<Iterator> iter2 = ds2->CreateIterator();
337   EXPECT_NE(iter2, nullptr);
338   std::unordered_map<std::string, mindspore::MSTensor> row2;
339   iter2->GetNextRow(&row2);
340 
341   uint64_t i = 0;
342   while (row1.size() != 0) {
343     i++;
344     auto image =row1["image"];
345     iter1->GetNextRow(&row1);
346     iter2->GetNextRow(&row2);
347   }
348   EXPECT_EQ(i, 2);
349 
350   iter1->Stop();
351   iter2->Stop();
352 }
353