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::Tensor;
22 using mindspore::dataset::TensorShape;
23
24 class MindDataTestPipeline : public UT::DatasetOpTesting {
25 protected:
26 };
27
28 // Tests for datasets (in alphabetical order)
29
TEST_F(MindDataTestPipeline,TestCelebADataset)30 TEST_F(MindDataTestPipeline, TestCelebADataset) {
31 MS_LOG(INFO) << "Doing MindDataTestPipeline-TestCelebADataset.";
32
33 // Create a CelebA Dataset
34 std::string folder_path = datasets_root_path_ + "/testCelebAData/";
35 std::shared_ptr<Dataset> ds = CelebA(folder_path, "all", std::make_shared<SequentialSampler>(0, 2), false, {});
36 EXPECT_NE(ds, nullptr);
37
38 // Create an iterator over the result of the above dataset
39 // This will trigger the creation of the Execution Tree and launch it.
40 std::shared_ptr<Iterator> iter = ds->CreateIterator();
41 EXPECT_NE(iter, nullptr);
42
43 // Iterate the dataset and get each row
44 std::unordered_map<std::string, mindspore::MSTensor> row;
45 ASSERT_OK(iter->GetNextRow(&row));
46
47 // Check if CelebA() read correct images/attr
48 std::string expect_file[] = {"1.JPEG", "2.jpg"};
49 std::vector<std::vector<uint32_t>> expect_attr_vector = {
50 {0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1,
51 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 1, 1, 0, 1, 0, 1, 0, 0, 1},
52 {0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1,
53 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1}};
54 uint64_t i = 0;
55 while (row.size() != 0) {
56 auto image = row["image"];
57 auto attr = row["attr"];
58
59 mindspore::MSTensor expect_image = ReadFileToTensor(folder_path + expect_file[i]);
60 EXPECT_MSTENSOR_EQ(image, expect_image);
61
62 std::shared_ptr<Tensor> de_expect_attr;
63 ASSERT_OK(Tensor::CreateFromVector(expect_attr_vector[i], TensorShape({40}), &de_expect_attr));
64 mindspore::MSTensor expect_attr =
65 mindspore::MSTensor(std::make_shared<mindspore::dataset::DETensor>(de_expect_attr));
66 EXPECT_MSTENSOR_EQ(attr, expect_attr);
67
68 ASSERT_OK(iter->GetNextRow(&row));
69 i++;
70 }
71
72 EXPECT_EQ(i, 2);
73
74 // Manually terminate the pipeline
75 iter->Stop();
76 }
77
TEST_F(MindDataTestPipeline,TestCelebADefault)78 TEST_F(MindDataTestPipeline, TestCelebADefault) {
79 MS_LOG(INFO) << "Doing MindDataTestPipeline-TestCelebADefault.";
80
81 // Create a CelebA Dataset
82 std::string folder_path = datasets_root_path_ + "/testCelebAData/";
83 std::shared_ptr<Dataset> ds = CelebA(folder_path);
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 // Check if CelebA() read correct images/attr
96 uint64_t i = 0;
97 while (row.size() != 0) {
98 auto image = row["image"];
99 auto attr = row["attr"];
100 MS_LOG(INFO) << "Tensor image shape: " << image.Shape();
101 MS_LOG(INFO) << "Tensor attr shape: " << attr.Shape();
102
103 ASSERT_OK(iter->GetNextRow(&row));
104 i++;
105 }
106
107 EXPECT_EQ(i, 4);
108
109 // Manually terminate the pipeline
110 iter->Stop();
111 }
112
TEST_F(MindDataTestPipeline,TestGetRepeatCount)113 TEST_F(MindDataTestPipeline, TestGetRepeatCount) {
114 MS_LOG(INFO) << "Doing MindDataTestPipeline-TestGetRepeatCount.";
115
116 // Create an ImageFolder Dataset
117 std::string folder_path = datasets_root_path_ + "/testPK/data/";
118 std::shared_ptr<Dataset> ds = ImageFolder(folder_path, true);
119 EXPECT_NE(ds, nullptr);
120 EXPECT_EQ(ds->GetRepeatCount(), 1);
121 ds = ds->Repeat(4);
122 EXPECT_NE(ds, nullptr);
123 EXPECT_EQ(ds->GetRepeatCount(), 4);
124 ds = ds->Repeat(3);
125 EXPECT_NE(ds, nullptr);
126 EXPECT_EQ(ds->GetRepeatCount(), 3);
127 }
128
TEST_F(MindDataTestPipeline,TestGetBatchSize)129 TEST_F(MindDataTestPipeline, TestGetBatchSize) {
130 MS_LOG(INFO) << "Doing MindDataTestPipeline-TestGetRepeatCount.";
131
132 // Create an ImageFolder Dataset
133 std::string folder_path = datasets_root_path_ + "/testPK/data/";
134 std::shared_ptr<Dataset> ds = ImageFolder(folder_path, true)->Project({"label"});
135 EXPECT_NE(ds, nullptr);
136 EXPECT_EQ(ds->GetBatchSize(), 1);
137 ds = ds->Batch(2);
138 EXPECT_NE(ds, nullptr);
139 EXPECT_EQ(ds->GetBatchSize(), 2);
140 ds = ds->Batch(3);
141 EXPECT_NE(ds, nullptr);
142 EXPECT_EQ(ds->GetBatchSize(), 3);
143 }
TEST_F(MindDataTestPipeline,TestCelebAGetDatasetSize)144 TEST_F(MindDataTestPipeline, TestCelebAGetDatasetSize) {
145 MS_LOG(INFO) << "Doing MindDataTestPipeline-TestCelebAGetDatasetSize.";
146
147 // Create a CelebA Dataset
148 std::string folder_path = datasets_root_path_ + "/testCelebAData/";
149 std::shared_ptr<Dataset> ds = CelebA(folder_path, "valid");
150 EXPECT_NE(ds, nullptr);
151
152 EXPECT_EQ(ds->GetDatasetSize(), 1);
153 }
154
TEST_F(MindDataTestPipeline,TestCelebAError)155 TEST_F(MindDataTestPipeline, TestCelebAError) {
156 MS_LOG(INFO) << "Doing MindDataTestPipeline-TestCelebAError.";
157
158 std::string folder_path = datasets_root_path_ + "/testCelebAData/";
159 std::string invalid_folder_path = "./testNotExist";
160 std::string invalid_dataset_type = "invalid_type";
161
162 // Create a CelebA Dataset
163 std::shared_ptr<Dataset> ds1 = CelebA(invalid_folder_path);
164 EXPECT_NE(ds1, nullptr);
165
166 // Create an iterator over the result of the above dataset
167 std::shared_ptr<Iterator> iter1 = ds1->CreateIterator();
168 // Expect failure: invalid CelebA input, invalid dataset path
169 EXPECT_EQ(iter1, nullptr);
170
171 // Create a CelebA Dataset
172 std::shared_ptr<Dataset> ds2 = CelebA(folder_path, invalid_dataset_type);
173 EXPECT_NE(ds2, nullptr);
174
175 // Create an iterator over the result of the above dataset
176 std::shared_ptr<Iterator> iter2 = ds2->CreateIterator();
177 // Expect failure: invalid CelebA input, invalid dataset type
178 EXPECT_EQ(iter2, nullptr);
179 }
180
TEST_F(MindDataTestPipeline,TestCelebADatasetWithNullSamplerError)181 TEST_F(MindDataTestPipeline, TestCelebADatasetWithNullSamplerError) {
182 MS_LOG(INFO) << "Doing MindDataTestPipeline-TestCelebADataset.";
183
184 // Create a CelebA Dataset
185 std::string folder_path = datasets_root_path_ + "/testCelebAData/";
186 std::shared_ptr<Dataset> ds = CelebA(folder_path, "all", nullptr, false, {});
187 EXPECT_NE(ds, nullptr);
188
189 // Create an iterator over the result of the above dataset
190 std::shared_ptr<Iterator> iter = ds->CreateIterator();
191 // Expect failure: invalid CelebA input, sampler cannot be nullptr
192 EXPECT_EQ(iter, nullptr);
193 }
194
TEST_F(MindDataTestPipeline,TestImageFolderWithWrongDatasetDirFail)195 TEST_F(MindDataTestPipeline, TestImageFolderWithWrongDatasetDirFail) {
196 MS_LOG(INFO) << "Doing MindDataTestPipeline-TestImageFolderWithWrongDatasetDirFail.";
197
198 // Create an ImageFolder Dataset
199 std::shared_ptr<Dataset> ds = ImageFolder("", true, nullptr);
200 EXPECT_NE(ds, nullptr);
201
202 // Create an iterator over the result of the above dataset
203 std::shared_ptr<Iterator> iter = ds->CreateIterator();
204 // Expect failure: invalid ImageFolder input
205 EXPECT_EQ(iter, nullptr);
206 }
207
TEST_F(MindDataTestPipeline,TestImageFolderFailWithWrongExtensionFail)208 TEST_F(MindDataTestPipeline, TestImageFolderFailWithWrongExtensionFail) {
209 MS_LOG(INFO) << "Doing MindDataTestPipeline-TestImageFolderFailWithWrongExtensionFail.";
210
211 // Create an ImageFolder Dataset
212 std::string folder_path = datasets_root_path_ + "/testPK/data/";
213 std::shared_ptr<Dataset> ds = ImageFolder(folder_path, true, std::make_shared<RandomSampler>(false, 2), {".JGP"});
214 EXPECT_NE(ds, nullptr);
215
216 // Create an iterator over the result of the above dataset
217 // This will trigger the creation of the Execution Tree and launch it.
218 std::shared_ptr<Iterator> iter = ds->CreateIterator();
219 EXPECT_NE(iter, nullptr);
220
221 // Iterate the dataset and get each row
222 std::unordered_map<std::string, mindspore::MSTensor> row;
223 // Expect no data: cannot find files with specified extension
224 EXPECT_ERROR(iter->GetNextRow(&row));
225 EXPECT_EQ(row.size(), 0);
226
227 // Manually terminate the pipeline
228 iter->Stop();
229 }
230
TEST_F(MindDataTestPipeline,TestImageFolderGetters)231 TEST_F(MindDataTestPipeline, TestImageFolderGetters) {
232 MS_LOG(INFO) << "Doing MindDataTestPipeline-TestImageFolderGetDatasetSize.";
233
234 // Create an ImageFolder Dataset
235 std::string folder_path = datasets_root_path_ + "/testPK/data/";
236 std::shared_ptr<Dataset> ds = ImageFolder(folder_path, true);
237 EXPECT_NE(ds, nullptr);
238
239 EXPECT_EQ(ds->GetDatasetSize(), 44);
240 EXPECT_EQ(ds->GetNumClasses(), 4);
241 EXPECT_EQ(ds->GetNumClasses(), 4);
242 EXPECT_EQ(ds->GetDatasetSize(), 44);
243 EXPECT_EQ(ds->GetDatasetSize(), 44);
244 }
245
TEST_F(MindDataTestPipeline,TestImageFolderFailWithNullSamplerFail)246 TEST_F(MindDataTestPipeline, TestImageFolderFailWithNullSamplerFail) {
247 MS_LOG(INFO) << "Doing MindDataTestPipeline-TestImageFolderFailWithNullSamplerFail.";
248
249 // Create an ImageFolder Dataset
250 std::string folder_path = datasets_root_path_ + "/testPK/data/";
251 std::shared_ptr<Dataset> ds = ImageFolder(folder_path, true, nullptr);
252 EXPECT_NE(ds, nullptr);
253
254 // Create an iterator over the result of the above dataset
255 std::shared_ptr<Iterator> iter = ds->CreateIterator();
256 // Expect failure: invalid ImageFolder input, sampler cannot be nullptr
257 EXPECT_EQ(iter, nullptr);
258 }
259
TEST_F(MindDataTestPipeline,TestImageFolderFailWithWrongSamplerFail)260 TEST_F(MindDataTestPipeline, TestImageFolderFailWithWrongSamplerFail) {
261 MS_LOG(INFO) << "Doing MindDataTestPipeline-TestImageFolderFailWithWrongSamplerFail.";
262
263 // Create an ImageFolder Dataset
264 std::string folder_path = datasets_root_path_ + "/testPK/data/";
265 std::shared_ptr<Dataset> ds = ImageFolder(folder_path, true, std::make_shared<SequentialSampler>(-2, 5));
266 EXPECT_NE(ds, nullptr);
267
268 // Create an iterator over the result of the above dataset
269 std::shared_ptr<Iterator> iter = ds->CreateIterator();
270 // Expect failure: invalid ImageFolder input, sampler is not constructed correctly
271 EXPECT_EQ(iter, nullptr);
272 }
273
TEST_F(MindDataTestPipeline,TestMnistGetDatasetSize)274 TEST_F(MindDataTestPipeline, TestMnistGetDatasetSize) {
275 MS_LOG(INFO) << "Doing MindDataTestPipeline-TestMnistGetDatasetSize.";
276
277 // Create a Mnist Dataset
278 std::string folder_path = datasets_root_path_ + "/testMnistData/";
279 std::shared_ptr<Dataset> ds = Mnist(folder_path, "all", std::make_shared<RandomSampler>(false, 20));
280 EXPECT_NE(ds, nullptr);
281 EXPECT_EQ(ds->GetDatasetSize(), 20);
282 }
283
TEST_F(MindDataTestPipeline,TestMnistFailWithWrongDatasetDirFail)284 TEST_F(MindDataTestPipeline, TestMnistFailWithWrongDatasetDirFail) {
285 MS_LOG(INFO) << "Doing MindDataTestPipeline-TestMnistFailWithWrongDatasetDirFail.";
286
287 // Create a Mnist Dataset
288 std::shared_ptr<Dataset> ds = Mnist("", "all", std::make_shared<RandomSampler>(false, 10));
289 EXPECT_NE(ds, nullptr);
290
291 // Create an iterator over the result of the above dataset
292 std::shared_ptr<Iterator> iter = ds->CreateIterator();
293 // Expect failure: invalid Mnist input, incorrect dataset directory input
294 EXPECT_EQ(iter, nullptr);
295 }
296
TEST_F(MindDataTestPipeline,TestMnistFailWithNullSamplerFail)297 TEST_F(MindDataTestPipeline, TestMnistFailWithNullSamplerFail) {
298 MS_LOG(INFO) << "Doing MindDataTestPipeline-TestMnistFailWithNullSamplerFail.";
299
300 // Create a Mnist Dataset
301 std::string folder_path = datasets_root_path_ + "/testMnistData/";
302 std::shared_ptr<Dataset> ds = Mnist(folder_path, "all", nullptr);
303 EXPECT_NE(ds, nullptr);
304
305 // Create an iterator over the result of the above dataset
306 std::shared_ptr<Iterator> iter = ds->CreateIterator();
307 // Expect failure: invalid Mnist input, sampler cannot be nullptr
308 EXPECT_EQ(iter, nullptr);
309 }
310
TEST_F(MindDataTestPipeline,TestImageFolderClassIndexDatasetSize)311 TEST_F(MindDataTestPipeline, TestImageFolderClassIndexDatasetSize) {
312 std::string folder_path = datasets_root_path_ + "/testPK/data";
313 std::map<std::string, int32_t> class_index;
314 class_index["class1"] = 111;
315 class_index["class2"] = 333;
316 auto ds = ImageFolder(folder_path, false, std::make_shared<RandomSampler>(), {}, class_index);
317 EXPECT_EQ(ds->GetNumClasses(), 2);
318 }
319
TEST_F(MindDataTestPipeline,TestImageFolderClassIndexDatasetSizeFail)320 TEST_F(MindDataTestPipeline, TestImageFolderClassIndexDatasetSizeFail) {
321 std::string folder_path = datasets_root_path_ + "/testPK/data";
322 std::map<std::string, int32_t> class_index;
323 class_index["class1"] = 111;
324 class_index["wrong class"] = 333;
325 auto ds = ImageFolder(folder_path, false, std::make_shared<RandomSampler>(), {}, class_index);
326 EXPECT_EQ(ds->GetNumClasses(), -1);
327 }
328