• 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 BoundingBoxAugment TensorTransform Operation
28 
TEST_F(MindDataTestPipeline,TestBoundingBoxAugmentSuccess1Shr)29 TEST_F(MindDataTestPipeline, TestBoundingBoxAugmentSuccess1Shr) {
30   MS_LOG(INFO) << "Doing MindDataTestPipeline-TestBoundingBoxAugmentSuccess1Shr.";
31   // Create an VOC Dataset
32   std::string folder_path = datasets_root_path_ + "/testVOC2012_2";
33   std::shared_ptr<Dataset> ds =
34     VOC(folder_path, "Detection", "train", {}, true, std::make_shared<SequentialSampler>(0, 3));
35   EXPECT_NE(ds, nullptr);
36 
37   // Create objects for the tensor ops
38   // Use shared pointers
39   std::shared_ptr<TensorTransform> random_rotation_op(new vision::RandomRotation({90.0}));
40   std::shared_ptr<TensorTransform> bound_box_augment_op(new vision::BoundingBoxAugment({random_rotation_op}, 1.0));
41 
42   // Create a Map operation on ds
43   ds = ds->Map({bound_box_augment_op}, {"image", "bbox"}, {"image", "bbox"}, {"image", "bbox"});
44   EXPECT_NE(ds, nullptr);
45 
46   // Create an iterator over the result of the above dataset
47   // This will trigger the creation of the Execution Tree and launch it.
48   std::shared_ptr<Iterator> iter = ds->CreateIterator();
49   EXPECT_NE(iter, nullptr);
50 
51   // Iterate the dataset and get each row
52   std::unordered_map<std::string, mindspore::MSTensor> row;
53   ASSERT_OK(iter->GetNextRow(&row));
54 
55   uint64_t i = 0;
56   while (row.size() != 0) {
57     i++;
58     auto image = row["image"];
59     MS_LOG(INFO) << "Tensor image shape: " << image.Shape();
60     ASSERT_OK(iter->GetNextRow(&row));
61   }
62 
63   EXPECT_EQ(i, 3);
64   // Manually terminate the pipeline
65   iter->Stop();
66 }
67 
TEST_F(MindDataTestPipeline,TestBoundingBoxAugmentSuccess2Auto)68 TEST_F(MindDataTestPipeline, TestBoundingBoxAugmentSuccess2Auto) {
69   MS_LOG(INFO) << "Doing MindDataTestPipeline-TestBoundingBoxAugmentSuccess2Auto.";
70   // Create an VOC Dataset
71   std::string folder_path = datasets_root_path_ + "/testVOC2012_2";
72   std::shared_ptr<Dataset> ds =
73     VOC(folder_path, "Detection", "train", {}, true, std::make_shared<SequentialSampler>(0, 3));
74   EXPECT_NE(ds, nullptr);
75 
76   // Create objects for the tensor ops
77   // Use auto for raw pointers
78   // Note that with auto and new, we have to explicitly delete the allocated object as shown below.
79   auto random_rotation_op(new vision::RandomRotation({90.0}));
80   auto bound_box_augment_op(new vision::BoundingBoxAugment({random_rotation_op}, 1.0));
81 
82   // Create a Map operation on ds
83   ds = ds->Map({bound_box_augment_op}, {"image", "bbox"}, {"image", "bbox"}, {"image", "bbox"});
84   EXPECT_NE(ds, nullptr);
85 
86   // Create an iterator over the result of the above dataset
87   // This will trigger the creation of the Execution Tree and launch it.
88   std::shared_ptr<Iterator> iter = ds->CreateIterator();
89   EXPECT_NE(iter, nullptr);
90 
91   // Iterate the dataset and get each row
92   std::unordered_map<std::string, mindspore::MSTensor> row;
93   ASSERT_OK(iter->GetNextRow(&row));
94 
95   uint64_t i = 0;
96   while (row.size() != 0) {
97     i++;
98     auto image = row["image"];
99     MS_LOG(INFO) << "Tensor image shape: " << image.Shape();
100     ASSERT_OK(iter->GetNextRow(&row));
101   }
102 
103   EXPECT_EQ(i, 3);
104   // Manually terminate the pipeline
105   iter->Stop();
106 
107   // Delete allocated objects with raw pointers
108   delete random_rotation_op;
109   delete bound_box_augment_op;
110 }
111 
TEST_F(MindDataTestPipeline,TestBoundingBoxAugmentSuccess3Obj)112 TEST_F(MindDataTestPipeline, TestBoundingBoxAugmentSuccess3Obj) {
113   MS_LOG(INFO) << "Doing MindDataTestPipeline-TestBoundingBoxAugmentSuccess3Obj.";
114   // Create an VOC Dataset
115   std::string folder_path = datasets_root_path_ + "/testVOC2012_2";
116   std::shared_ptr<Dataset> ds =
117     VOC(folder_path, "Detection", "train", {}, true, std::make_shared<SequentialSampler>(0, 3));
118   EXPECT_NE(ds, nullptr);
119 
120   // Create objects for the tensor ops
121   // Use object references
122   vision::RandomRotation random_rotation_op = vision::RandomRotation({90.0});
123   vision::BoundingBoxAugment bound_box_augment_op = vision::BoundingBoxAugment({random_rotation_op}, 1.0);
124 
125   // Create a Map operation on ds
126   ds = ds->Map({bound_box_augment_op}, {"image", "bbox"}, {"image", "bbox"}, {"image", "bbox"});
127   EXPECT_NE(ds, nullptr);
128 
129   // Create an iterator over the result of the above dataset
130   // This will trigger the creation of the Execution Tree and launch it.
131   std::shared_ptr<Iterator> iter = ds->CreateIterator();
132   EXPECT_NE(iter, nullptr);
133 
134   // Iterate the dataset and get each row
135   std::unordered_map<std::string, mindspore::MSTensor> row;
136   ASSERT_OK(iter->GetNextRow(&row));
137 
138   uint64_t i = 0;
139   while (row.size() != 0) {
140     i++;
141     auto image = row["image"];
142     MS_LOG(INFO) << "Tensor image shape: " << image.Shape();
143     ASSERT_OK(iter->GetNextRow(&row));
144   }
145 
146   EXPECT_EQ(i, 3);
147   // Manually terminate the pipeline
148   iter->Stop();
149 }
150 
TEST_F(MindDataTestPipeline,TestBoundingBoxAugmentFail1)151 TEST_F(MindDataTestPipeline, TestBoundingBoxAugmentFail1) {
152   MS_LOG(INFO) << "Doing MindDataTestPipeline-TestBoundingBoxAugmentFail1 with invalid ratio parameter.";
153 
154   // Create an VOC Dataset
155   std::string folder_path = datasets_root_path_ + "/testVOC2012_2";
156   std::shared_ptr<Dataset> ds =
157     VOC(folder_path, "Detection", "train", {}, true, std::make_shared<SequentialSampler>(0, 3));
158   EXPECT_NE(ds, nullptr);
159 
160   // Create objects for the tensor ops
161   std::shared_ptr<TensorTransform> random_rotation_op(new vision::RandomRotation({90.0}));
162 
163   // Create BoundingBoxAugment op with invalid ratio < 0.0
164   std::shared_ptr<TensorTransform> bound_box_augment_op(new vision::BoundingBoxAugment({random_rotation_op}, -1.0));
165 
166   // Create a Map operation on ds
167   ds = ds->Map({bound_box_augment_op}, {"image", "bbox"}, {"image", "bbox"}, {"image", "bbox"});
168   EXPECT_NE(ds, nullptr);
169 
170   // Create an iterator over the result of the above dataset
171   std::shared_ptr<Iterator> iter = ds->CreateIterator();
172   // Expect failure: Invalid BoundingBoxAugment input
173   EXPECT_EQ(iter, nullptr);
174 }
175 
TEST_F(MindDataTestPipeline,TestBoundingBoxAugmentFail2)176 TEST_F(MindDataTestPipeline, TestBoundingBoxAugmentFail2) {
177   MS_LOG(INFO) << "Doing MindDataTestPipeline-TestBoundingBoxAugmentFail2 with invalid ratio parameter.";
178 
179   // Create an VOC Dataset
180   std::string folder_path = datasets_root_path_ + "/testVOC2012_2";
181   std::shared_ptr<Dataset> ds =
182     VOC(folder_path, "Detection", "train", {}, true, std::make_shared<SequentialSampler>(0, 3));
183   EXPECT_NE(ds, nullptr);
184 
185   // Create objects for the tensor ops
186   std::shared_ptr<TensorTransform> random_rotation_op(new vision::RandomRotation({90.0}));
187 
188   // Create BoundingBoxAugment op with invalid ratio > 1.0
189   std::shared_ptr<TensorTransform> bound_box_augment_op(new vision::BoundingBoxAugment({random_rotation_op}, 2.0));
190 
191   // Create a Map operation on ds
192   ds = ds->Map({bound_box_augment_op}, {"image", "bbox"}, {"image", "bbox"}, {"image", "bbox"});
193   EXPECT_NE(ds, nullptr);
194 
195   // Create an iterator over the result of the above dataset
196   std::shared_ptr<Iterator> iter = ds->CreateIterator();
197   // Expect failure: Invalid BoundingBoxAugment input
198   EXPECT_EQ(iter, nullptr);
199 }
200 
TEST_F(MindDataTestPipeline,TestBoundingBoxAugmentFail3)201 TEST_F(MindDataTestPipeline, TestBoundingBoxAugmentFail3) {
202   MS_LOG(INFO) << "Doing MindDataTestPipeline-TestBoundingBoxAugmentFail3 with invalid transform.";
203 
204   // Create an VOC Dataset
205   std::string folder_path = datasets_root_path_ + "/testVOC2012_2";
206   std::shared_ptr<Dataset> ds =
207     VOC(folder_path, "Detection", "train", {}, true, std::make_shared<SequentialSampler>(0, 3));
208   EXPECT_NE(ds, nullptr);
209 
210   // Create BoundingBoxAugment op with invalid nullptr transform
211   std::shared_ptr<TensorTransform> bound_box_augment_op(new vision::BoundingBoxAugment(nullptr, 0.5));
212 
213   // Create a Map operation on ds
214   ds = ds->Map({bound_box_augment_op}, {"image", "bbox"}, {"image", "bbox"}, {"image", "bbox"});
215   EXPECT_NE(ds, nullptr);
216 
217   // Create an iterator over the result of the above dataset
218   std::shared_ptr<Iterator> iter = ds->CreateIterator();
219   // Expect failure: Invalid BoundingBoxAugment input
220   EXPECT_EQ(iter, nullptr);
221 }
222 
TEST_F(MindDataTestPipeline,TestBoundingBoxAugmentFail4)223 TEST_F(MindDataTestPipeline, TestBoundingBoxAugmentFail4) {
224   MS_LOG(INFO) << "Doing MindDataTestPipeline-TestBoundingBoxAugmentFail4 with invalid transform input.";
225 
226   // Create an VOC Dataset
227   std::string folder_path = datasets_root_path_ + "/testVOC2012_2";
228   std::shared_ptr<Dataset> ds =
229     VOC(folder_path, "Detection", "train", {}, true, std::make_shared<SequentialSampler>(0, 3));
230   EXPECT_NE(ds, nullptr);
231 
232   // Create objects for the tensor ops
233   // RandomRotation has invalid input, first column value of degrees is greater than the second column value
234   std::shared_ptr<TensorTransform> random_rotation_op(new vision::RandomRotation({50.0, -50.0}));
235 
236   // Create BoundingBoxAugment op with invalid transform
237   std::shared_ptr<TensorTransform> bound_box_augment_op(new vision::BoundingBoxAugment({random_rotation_op}, 0.25));
238 
239   // Create a Map operation on ds
240   ds = ds->Map({bound_box_augment_op}, {"image", "bbox"}, {"image", "bbox"}, {"image", "bbox"});
241   EXPECT_NE(ds, nullptr);
242 
243   // Create an iterator over the result of the above dataset
244   std::shared_ptr<Iterator> iter = ds->CreateIterator();
245   // Expect failure: Invalid BoundingBoxAugment input
246   EXPECT_EQ(iter, nullptr);
247 }
248