• 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/core/tensor.h"
19 
20 using namespace mindspore::dataset;
21 using mindspore::dataset::DataType;
22 using mindspore::dataset::Tensor;
23 using mindspore::dataset::TensorShape;
24 
25 class MindDataTestPipeline : public UT::DatasetOpTesting {
26  protected:
27 };
28 
TEST_F(MindDataTestPipeline,TestVOCClassIndex)29 TEST_F(MindDataTestPipeline, TestVOCClassIndex) {
30   MS_LOG(INFO) << "Doing MindDataTestPipeline-TestVOCClassIndex.";
31 
32   // Create a VOC Dataset
33   std::string folder_path = datasets_root_path_ + "/testVOC2012_2";
34   std::map<std::string, int32_t> class_index;
35   class_index["car"] = 0;
36   class_index["cat"] = 1;
37   class_index["train"] = 9;
38 
39   std::shared_ptr<Dataset> ds = VOC(folder_path, "Detection", "train", class_index, false, std::make_shared<SequentialSampler>(0, 6));
40   EXPECT_NE(ds, nullptr);
41 
42   // Create an iterator over the result of the above dataset
43   // This will trigger the creation of the Execution Tree and launch it.
44   std::shared_ptr<Iterator> iter = ds->CreateIterator();
45   EXPECT_NE(iter, nullptr);
46 
47   // Iterate the dataset and get each row
48   std::unordered_map<std::string, mindspore::MSTensor> row;
49   ASSERT_OK(iter->GetNextRow(&row));
50 
51   // Check if VOC() read correct labels
52   // When we provide class_index, label of ["car","cat","train"] become [0,1,9]
53   std::shared_ptr<Tensor> de_expect_label;
54   ASSERT_OK(Tensor::CreateFromMemory(TensorShape({1, 1}), DataType(DataType::DE_UINT32), nullptr, &de_expect_label));
55   uint32_t expect[] = {9, 9, 9, 1, 1, 0};
56 
57   uint64_t i = 0;
58   while (row.size() != 0) {
59     auto image = row["image"];
60     auto label = row["label"];
61     MS_LOG(INFO) << "Tensor image shape: " << image.Shape();
62     MS_LOG(INFO) << "Tensor label shape: " << label.Shape();
63 
64     ASSERT_OK(de_expect_label->SetItemAt({0, 0}, expect[i]));
65     mindspore::MSTensor expect_label = mindspore::MSTensor(std::make_shared<mindspore::dataset::DETensor>(de_expect_label));
66     EXPECT_MSTENSOR_EQ(label, expect_label);
67 
68     ASSERT_OK(iter->GetNextRow(&row));
69     i++;
70   }
71 
72   EXPECT_EQ(i, 6);
73 
74   // Manually terminate the pipeline
75   iter->Stop();
76 }
77 
TEST_F(MindDataTestPipeline,TestVOCGetClassIndex)78 TEST_F(MindDataTestPipeline, TestVOCGetClassIndex) {
79   MS_LOG(INFO) << "Doing MindDataTestPipeline-TestVOCGetClassIndex.";
80   // Create a VOC Dataset
81   std::string folder_path = datasets_root_path_ + "/testVOC2012_2";
82   std::map<std::string, int32_t> class_index;
83   class_index["car"] = 0;
84   class_index["cat"] = 1;
85   class_index["train"] = 9;
86 
87   std::shared_ptr<Dataset> ds = VOC(folder_path, "Detection", "train", class_index, false, std::make_shared<SequentialSampler>(0, 6));
88   EXPECT_NE(ds, nullptr);
89 
90   std::vector<std::pair<std::string, std::vector<int32_t>>> class_index1 = ds->GetClassIndexing();
91   EXPECT_EQ(class_index1.size(), 3);
92   EXPECT_EQ(class_index1[0].first, "car");
93   EXPECT_EQ(class_index1[0].second[0], 0);
94   EXPECT_EQ(class_index1[1].first, "cat");
95   EXPECT_EQ(class_index1[1].second[0], 1);
96   EXPECT_EQ(class_index1[2].first, "train");
97   EXPECT_EQ(class_index1[2].second[0], 9);
98 }
99 
TEST_F(MindDataTestPipeline,TestVOCGetters)100 TEST_F(MindDataTestPipeline, TestVOCGetters) {
101   MS_LOG(INFO) << "Doing MindDataTestPipeline-TestVOCGetters.";
102 
103   // Create a VOC Dataset
104   std::string folder_path = datasets_root_path_ + "/testVOC2012_2";
105   std::map<std::string, int32_t> class_index;
106   class_index["car"] = 0;
107   class_index["cat"] = 1;
108   class_index["train"] = 9;
109 
110   std::shared_ptr<Dataset> ds = VOC(folder_path, "Detection", "train", class_index, false, std::make_shared<SequentialSampler>(0, 6));
111   EXPECT_NE(ds, nullptr);
112 
113   ds = ds->Batch(2);
114   ds = ds->Repeat(2);
115 
116   EXPECT_EQ(ds->GetDatasetSize(), 6);
117   std::vector<std::string> column_names = {"image", "bbox", "label", "difficult", "truncate"};
118   EXPECT_EQ(ds->GetColumnNames(), column_names);
119 }
120 
TEST_F(MindDataTestPipeline,TestVOCDetection)121 TEST_F(MindDataTestPipeline, TestVOCDetection) {
122   MS_LOG(INFO) << "Doing MindDataTestPipeline-TestVOCDetection.";
123 
124   // Create a VOC Dataset
125   std::string folder_path = datasets_root_path_ + "/testVOC2012_2";
126   std::shared_ptr<Dataset> ds = VOC(folder_path, "Detection", "train", {}, false, std::make_shared<SequentialSampler>(0, 4));
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   // Check if VOC() read correct images/labels
139   std::string expect_file[] = {"15", "32", "33", "39"};
140   uint32_t expect_num[] = {5, 5, 4, 3};
141 
142   std::shared_ptr<Tensor> de_expect_label;
143   ASSERT_OK(Tensor::CreateFromMemory(TensorShape({1, 1}), DataType(DataType::DE_UINT32), nullptr, &de_expect_label));
144 
145   uint64_t i = 0;
146   while (row.size() != 0) {
147     auto image = row["image"];
148     auto label = row["label"];
149     MS_LOG(INFO) << "Tensor image shape: " << image.Shape();
150     MS_LOG(INFO) << "Tensor label shape: " << label.Shape();
151 
152     mindspore::MSTensor expect_image = ReadFileToTensor(folder_path + "/JPEGImages/" + expect_file[i] + ".jpg");
153     EXPECT_MSTENSOR_EQ(image, expect_image);
154 
155     ASSERT_OK(de_expect_label->SetItemAt({0, 0}, expect_num[i]));
156     mindspore::MSTensor expect_label = mindspore::MSTensor(std::make_shared<mindspore::dataset::DETensor>(de_expect_label));
157     EXPECT_MSTENSOR_EQ(label, expect_label);
158 
159     ASSERT_OK(iter->GetNextRow(&row));
160     i++;
161   }
162 
163   EXPECT_EQ(i, 4);
164 
165   // Manually terminate the pipeline
166   iter->Stop();
167 }
168 
TEST_F(MindDataTestPipeline,TestVOCInvalidTaskOrModeError1)169 TEST_F(MindDataTestPipeline, TestVOCInvalidTaskOrModeError1) {
170   MS_LOG(INFO) << "Doing MindDataTestPipeline-TestVOCInvalidTaskOrModeError1.";
171 
172   // Create a VOC Dataset
173   std::string folder_path = datasets_root_path_ + "/testVOC2012_2";
174   std::shared_ptr<Dataset> ds1 = VOC(folder_path, "Classification", "train", {}, false, std::make_shared<SequentialSampler>(0, 3));
175   EXPECT_NE(ds1, nullptr);
176 
177   // Create an iterator over the result of the above dataset
178   std::shared_ptr<Iterator> iter1 = ds1->CreateIterator();
179   // Expect failure: invalid Manifest input, invalid task
180   EXPECT_EQ(iter1, nullptr);
181 
182   std::shared_ptr<Dataset> ds2 = VOC(folder_path, "Segmentation", "validation", {}, false, std::make_shared<RandomSampler>(false, 4));
183   EXPECT_NE(ds2, nullptr);
184 
185   // Create an iterator over the result of the above dataset
186   std::shared_ptr<Iterator> iter2 = ds2->CreateIterator();
187   // Expect failure: invalid VOC input, invalid mode
188   EXPECT_EQ(iter2, nullptr);
189 }
190 
TEST_F(MindDataTestPipeline,TestVOCSegmentation)191 TEST_F(MindDataTestPipeline, TestVOCSegmentation) {
192   MS_LOG(INFO) << "Doing MindDataTestPipeline-TestVOCSegmentation.";
193 
194   // Create a VOC Dataset
195   std::string folder_path = datasets_root_path_ + "/testVOC2012_2";
196   std::shared_ptr<Dataset> ds = VOC(folder_path, "Segmentation", "train", {}, false, std::make_shared<SequentialSampler>(0, 3));
197   EXPECT_NE(ds, nullptr);
198 
199   // Create a Repeat operation on ds
200   int32_t repeat_num = 2;
201   ds = ds->Repeat(repeat_num);
202   EXPECT_NE(ds, nullptr);
203 
204   // Create an iterator over the result of the above dataset
205   // This will trigger the creation of the Execution Tree and launch it.
206   std::shared_ptr<Iterator> iter = ds->CreateIterator();
207   EXPECT_NE(iter, nullptr);
208 
209   // Iterate the dataset and get each row
210   std::unordered_map<std::string, mindspore::MSTensor> row;
211   ASSERT_OK(iter->GetNextRow(&row));
212 
213   // Check if VOC() read correct images/targets
214   std::string expect_file[] = {"32", "33", "39", "32", "33", "39"};
215   uint64_t i = 0;
216   while (row.size() != 0) {
217     auto image = row["image"];
218     auto target = row["target"];
219     MS_LOG(INFO) << "Tensor image shape: " << image.Shape();
220     MS_LOG(INFO) << "Tensor target shape: " << target.Shape();
221 
222     mindspore::MSTensor expect_image = ReadFileToTensor(folder_path + "/JPEGImages/" + expect_file[i] + ".jpg");
223     EXPECT_MSTENSOR_EQ(image, expect_image);
224 
225     mindspore::MSTensor expect_target = ReadFileToTensor(folder_path + "/SegmentationClass/" + expect_file[i] + ".png");
226     EXPECT_MSTENSOR_EQ(target, expect_target);
227 
228     ASSERT_OK(iter->GetNextRow(&row));
229     i++;
230   }
231 
232   EXPECT_EQ(i, 6);
233 
234   // Manually terminate the pipeline
235   iter->Stop();
236 }
237 
TEST_F(MindDataTestPipeline,TestVOCSegmentationError2)238 TEST_F(MindDataTestPipeline, TestVOCSegmentationError2) {
239   MS_LOG(INFO) << "Doing MindDataTestPipeline-TestVOCSegmentationError2.";
240 
241   // Create a VOC Dataset
242   std::map<std::string, int32_t> class_index;
243   class_index["car"] = 0;
244   std::string folder_path = datasets_root_path_ + "/testVOC2012_2";
245   std::shared_ptr<Dataset> ds = VOC(folder_path, "Segmentation", "train", class_index, false, std::make_shared<RandomSampler>(false, 6));
246   EXPECT_NE(ds, nullptr);
247 
248   // Create an iterator over the result of the above dataset
249   std::shared_ptr<Iterator> iter = ds->CreateIterator();
250   // Expect failure: invalid VOC input, segmentation task with class_index
251   EXPECT_EQ(iter, nullptr);
252 }
253 
TEST_F(MindDataTestPipeline,TestVOCWithNullSamplerError3)254 TEST_F(MindDataTestPipeline, TestVOCWithNullSamplerError3) {
255   MS_LOG(INFO) << "Doing MindDataTestPipeline-TestVOCWithNullSamplerError3.";
256 
257   // Create a VOC Dataset
258   std::string folder_path = datasets_root_path_ + "/testVOC2012_2";
259   std::shared_ptr<Dataset> ds = VOC(folder_path, "Segmentation", "train", {}, false, nullptr);
260   EXPECT_NE(ds, nullptr);
261 
262   // Create an iterator over the result of the above dataset
263   std::shared_ptr<Iterator> iter = ds->CreateIterator();
264   // Expect failure: invalid VOC input, sampler cannot be nullptr
265   EXPECT_EQ(iter, nullptr);
266 }
267