• 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 UniformAugment
28 // Tests for vision C++ API UniformAugment TensorTransform Operations
29 
TEST_F(MindDataTestPipeline,TestUniformAugWithOps1Shr)30 TEST_F(MindDataTestPipeline, TestUniformAugWithOps1Shr) {
31   MS_LOG(INFO) << "Doing MindDataTestPipeline-TestUniformAugWithOps1Shr.";
32 
33   // Create a Mnist Dataset
34   std::string folder_path = datasets_root_path_ + "/testMnistData/";
35   std::shared_ptr<Dataset> ds = Mnist(folder_path, "all", std::make_shared<RandomSampler>(false, 20));
36   EXPECT_NE(ds, nullptr);
37 
38   // Create a Repeat operation on ds
39   int32_t repeat_num = 1;
40   ds = ds->Repeat(repeat_num);
41   EXPECT_NE(ds, nullptr);
42 
43   // Create objects for the tensor ops
44   // Use shared pointers
45   std::shared_ptr<TensorTransform> resize_op(new vision::Resize({30, 30}));
46   std::shared_ptr<TensorTransform> random_crop_op(new vision::RandomCrop({28, 28}));
47   std::shared_ptr<TensorTransform> center_crop_op(new vision::CenterCrop({16, 16}));
48   std::shared_ptr<TensorTransform> uniform_aug_op(new vision::UniformAugment({random_crop_op, center_crop_op}, 2));
49 
50   // Create a Map operation on ds
51   ds = ds->Map({resize_op, uniform_aug_op});
52   EXPECT_NE(ds, nullptr);
53 
54   // Create an iterator over the result of the above dataset
55   // This will trigger the creation of the Execution Tree and launch it.
56   std::shared_ptr<Iterator> iter = ds->CreateIterator();
57   EXPECT_NE(iter, nullptr);
58 
59   // Iterate the dataset and get each row
60   std::unordered_map<std::string, mindspore::MSTensor> row;
61   ASSERT_OK(iter->GetNextRow(&row));
62 
63   uint64_t i = 0;
64   while (row.size() != 0) {
65     i++;
66     auto image = row["image"];
67     MS_LOG(INFO) << "Tensor image shape: " << image.Shape();
68     ASSERT_OK(iter->GetNextRow(&row));
69   }
70 
71   EXPECT_EQ(i, 20);
72 
73   // Manually terminate the pipeline
74   iter->Stop();
75 }
76 
TEST_F(MindDataTestPipeline,TestUniformAugWithOps2Auto)77 TEST_F(MindDataTestPipeline, TestUniformAugWithOps2Auto) {
78   MS_LOG(INFO) << "Doing MindDataTestPipeline-TestUniformAugWithOps2Auto.";
79 
80   // Create a Mnist Dataset
81   std::string folder_path = datasets_root_path_ + "/testMnistData/";
82   std::shared_ptr<Dataset> ds = Mnist(folder_path, "all", std::make_shared<RandomSampler>(false, 20));
83   EXPECT_NE(ds, nullptr);
84 
85   // Create a Repeat operation on ds
86   int32_t repeat_num = 1;
87   ds = ds->Repeat(repeat_num);
88   EXPECT_NE(ds, nullptr);
89 
90   // Create objects for the tensor ops
91   // Use auto for raw pointers
92   // Note that with auto and new, we have to explicitly delete the allocated object as shown below.
93   auto resize_op(new vision::Resize({30, 30}));
94   auto random_crop_op(new vision::RandomCrop({28, 28}));
95   auto center_crop_op(new vision::CenterCrop({16, 16}));
96   auto uniform_aug_op(new vision::UniformAugment({random_crop_op, center_crop_op}, 2));
97 
98   // Create a Map operation on ds
99   ds = ds->Map({resize_op, uniform_aug_op});
100   EXPECT_NE(ds, nullptr);
101 
102   // Create an iterator over the result of the above dataset
103   // This will trigger the creation of the Execution Tree and launch it.
104   std::shared_ptr<Iterator> iter = ds->CreateIterator();
105   EXPECT_NE(iter, nullptr);
106 
107   // Iterate the dataset and get each row
108   std::unordered_map<std::string, mindspore::MSTensor> row;
109   ASSERT_OK(iter->GetNextRow(&row));
110 
111   uint64_t i = 0;
112   while (row.size() != 0) {
113     i++;
114     auto image = row["image"];
115     MS_LOG(INFO) << "Tensor image shape: " << image.Shape();
116     ASSERT_OK(iter->GetNextRow(&row));
117   }
118 
119   EXPECT_EQ(i, 20);
120 
121   // Manually terminate the pipeline
122   iter->Stop();
123 
124   // Delete allocated objects with raw pointers
125   delete resize_op;
126   delete random_crop_op;
127   delete center_crop_op;
128   delete uniform_aug_op;
129 }
130 
TEST_F(MindDataTestPipeline,TestUniformAugWithOps3Obj)131 TEST_F(MindDataTestPipeline, TestUniformAugWithOps3Obj) {
132   MS_LOG(INFO) << "Doing MindDataTestPipeline-TestUniformAugWithOps3Obj.";
133 
134   // Create a Mnist Dataset
135   std::string folder_path = datasets_root_path_ + "/testMnistData/";
136   std::shared_ptr<Dataset> ds = Mnist(folder_path, "all", std::make_shared<RandomSampler>(false, 20));
137   EXPECT_NE(ds, nullptr);
138 
139   // Create a Repeat operation on ds
140   int32_t repeat_num = 1;
141   ds = ds->Repeat(repeat_num);
142   EXPECT_NE(ds, nullptr);
143 
144   // Create objects for the tensor ops
145   // Use object references
146   vision::Resize resize_op = vision::Resize({30, 30});
147   vision::RandomCrop random_crop_op = vision::RandomCrop({28, 28});
148   vision::CenterCrop center_crop_op = vision::CenterCrop({16, 16});
149   vision::UniformAugment uniform_aug_op = vision::UniformAugment({random_crop_op, center_crop_op}, 2);
150 
151   // Create a Map operation on ds
152   ds = ds->Map({resize_op, uniform_aug_op});
153   EXPECT_NE(ds, nullptr);
154 
155   // Create an iterator over the result of the above dataset
156   // This will trigger the creation of the Execution Tree and launch it.
157   std::shared_ptr<Iterator> iter = ds->CreateIterator();
158   EXPECT_NE(iter, nullptr);
159 
160   // Iterate the dataset and get each row
161   std::unordered_map<std::string, mindspore::MSTensor> row;
162   ASSERT_OK(iter->GetNextRow(&row));
163 
164   uint64_t i = 0;
165   while (row.size() != 0) {
166     i++;
167     auto image = row["image"];
168     MS_LOG(INFO) << "Tensor image shape: " << image.Shape();
169     ASSERT_OK(iter->GetNextRow(&row));
170   }
171 
172   EXPECT_EQ(i, 20);
173 
174   // Manually terminate the pipeline
175   iter->Stop();
176 }
177 
TEST_F(MindDataTestPipeline,TestUniformAugmentFail1num_ops)178 TEST_F(MindDataTestPipeline, TestUniformAugmentFail1num_ops) {
179   MS_LOG(INFO) << "Doing MindDataTestPipeline-TestUniformAugmentFail1num_ops with invalid num_ops parameter.";
180 
181   // Create a Mnist Dataset
182   std::string folder_path = datasets_root_path_ + "/testMnistData/";
183   std::shared_ptr<Dataset> ds = Mnist(folder_path, "all", std::make_shared<RandomSampler>(false, 20));
184   EXPECT_NE(ds, nullptr);
185 
186   // Create objects for the tensor ops
187   std::shared_ptr<TensorTransform> random_crop_op(new vision::RandomCrop({28, 28}));
188   std::shared_ptr<TensorTransform> center_crop_op(new vision::CenterCrop({16, 16}));
189 
190   // UniformAug: num_ops must be greater than 0
191   std::shared_ptr<TensorTransform> uniform_aug_op(new vision::UniformAugment({random_crop_op, center_crop_op}, 0));
192 
193   // Create a Map operation on ds
194   ds = ds->Map({uniform_aug_op}, {"image", "bbox"}, {"image", "bbox"}, {"image", "bbox"});
195   EXPECT_NE(ds, nullptr);
196 
197   // Create an iterator over the result of the above dataset
198   std::shared_ptr<Iterator> iter = ds->CreateIterator();
199   // Expect failure: Invalid UniformAugment input
200   EXPECT_EQ(iter, nullptr);
201 }
202 
TEST_F(MindDataTestPipeline,TestUniformAugmentFail2num_ops)203 TEST_F(MindDataTestPipeline, TestUniformAugmentFail2num_ops) {
204   MS_LOG(INFO) << "Doing MindDataTestPipeline-TestUniformAugmentFail2num_ops with invalid num_ops parameter.";
205 
206   // Create a Mnist Dataset
207   std::string folder_path = datasets_root_path_ + "/testMnistData/";
208   std::shared_ptr<Dataset> ds = Mnist(folder_path, "all", std::make_shared<RandomSampler>(false, 20));
209   EXPECT_NE(ds, nullptr);
210 
211   // Create objects for the tensor ops
212   std::shared_ptr<TensorTransform> random_crop_op(new vision::RandomCrop({28, 28}));
213   std::shared_ptr<TensorTransform> center_crop_op(new vision::CenterCrop({16, 16}));
214 
215   // UniformAug: num_ops is greater than transforms size
216   std::shared_ptr<TensorTransform> uniform_aug_op(new vision::UniformAugment({random_crop_op, center_crop_op}, 3));
217 
218   // Create a Map operation on ds
219   ds = ds->Map({uniform_aug_op}, {"image", "bbox"}, {"image", "bbox"}, {"image", "bbox"});
220   EXPECT_NE(ds, nullptr);
221 
222   // Create an iterator over the result of the above dataset
223   std::shared_ptr<Iterator> iter = ds->CreateIterator();
224   // Expect failure: Invalid UniformAugment input
225   EXPECT_EQ(iter, nullptr);
226 }
227 
TEST_F(MindDataTestPipeline,TestUniformAugmentFail3transforms)228 TEST_F(MindDataTestPipeline, TestUniformAugmentFail3transforms) {
229   MS_LOG(INFO) << "Doing MindDataTestPipeline-TestUniformAugmentFail3transforms with invalid transform.";
230 
231   // Create a Mnist Dataset
232   std::string folder_path = datasets_root_path_ + "/testMnistData/";
233   std::shared_ptr<Dataset> ds = Mnist(folder_path, "all", std::make_shared<RandomSampler>(false, 20));
234   EXPECT_NE(ds, nullptr);
235 
236   // Create objects for the tensor ops
237   // RandomRotation has invalid input, negative size
238   std::shared_ptr<TensorTransform> random_crop_op(new vision::RandomCrop({-28}));
239 
240   // Create UniformAug op with invalid transform op
241   std::shared_ptr<TensorTransform> uniform_aug_op(new vision::UniformAugment({random_crop_op}, 1));
242 
243   // Create a Map operation on ds
244   ds = ds->Map({uniform_aug_op}, {"image", "bbox"}, {"image", "bbox"}, {"image", "bbox"});
245   EXPECT_NE(ds, nullptr);
246 
247   // Create an iterator over the result of the above dataset
248   std::shared_ptr<Iterator> iter = ds->CreateIterator();
249   // Expect failure: Invalid UniformAugment input
250   EXPECT_EQ(iter, nullptr);
251 }
252 
TEST_F(MindDataTestPipeline,TestUniformAugmentFail4transforms)253 TEST_F(MindDataTestPipeline, TestUniformAugmentFail4transforms) {
254   MS_LOG(INFO) << "Doing MindDataTestPipeline-TestUniformAugmentFail4transforms with invalid transform.";
255 
256   // Create a Mnist Dataset
257   std::string folder_path = datasets_root_path_ + "/testMnistData/";
258   std::shared_ptr<Dataset> ds = Mnist(folder_path, "all", std::make_shared<RandomSampler>(false, 20));
259   EXPECT_NE(ds, nullptr);
260 
261   // Create objects for the tensor ops
262   std::shared_ptr<TensorTransform> random_crop_op(new vision::RandomCrop({28}));
263 
264   // Create UniformAug op with invalid transform op, nullptr
265   std::shared_ptr<TensorTransform> uniform_aug_op(new vision::UniformAugment({random_crop_op, nullptr}, 2));
266 
267   // Create a Map operation on ds
268   ds = ds->Map({uniform_aug_op}, {"image", "bbox"}, {"image", "bbox"}, {"image", "bbox"});
269   EXPECT_NE(ds, nullptr);
270 
271   // Create an iterator over the result of the above dataset
272   std::shared_ptr<Iterator> iter = ds->CreateIterator();
273   // Expect failure: Invalid UniformAugment input
274   EXPECT_EQ(iter, nullptr);
275 }
276 
TEST_F(MindDataTestPipeline,TestUniformAugmentFail5transforms)277 TEST_F(MindDataTestPipeline, TestUniformAugmentFail5transforms) {
278   MS_LOG(INFO) << "Doing MindDataTestPipeline-TestUniformAugmentFail5transforms with invalid transform.";
279 
280   // Create a Mnist Dataset
281   std::string folder_path = datasets_root_path_ + "/testMnistData/";
282   std::shared_ptr<Dataset> ds = Mnist(folder_path, "all", std::make_shared<RandomSampler>(false, 20));
283   EXPECT_NE(ds, nullptr);
284 
285   // Create UniformAug op with invalid transform op empty list
286   std::vector<std::shared_ptr<TensorTransform>> list = {};
287   std::shared_ptr<TensorTransform> uniform_aug_op(new vision::UniformAugment(list, 1));
288 
289   // Create a Map operation on ds
290   ds = ds->Map({uniform_aug_op}, {"image", "bbox"}, {"image", "bbox"}, {"image", "bbox"});
291   EXPECT_NE(ds, nullptr);
292 
293   // Create an iterator over the result of the above dataset
294   std::shared_ptr<Iterator> iter = ds->CreateIterator();
295   // Expect failure: Invalid UniformAugment input
296   EXPECT_EQ(iter, nullptr);
297 }
298