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
18 #include "minddata/dataset/include/dataset/datasets.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,TestCifar10Dataset)29 TEST_F(MindDataTestPipeline, TestCifar10Dataset) {
30 MS_LOG(INFO) << "Doing MindDataTestPipeline-TestCifar10Dataset.";
31
32 // Create a Cifar10 Dataset
33 std::string folder_path = datasets_root_path_ + "/testCifar10Data/";
34 std::shared_ptr<Dataset> ds = Cifar10(folder_path, "all", std::make_shared<RandomSampler>(false, 10));
35 EXPECT_NE(ds, nullptr);
36
37 // Create an iterator over the result of the above dataset
38 // This will trigger the creation of the Execution Tree and launch it.
39 std::shared_ptr<Iterator> iter = ds->CreateIterator();
40 EXPECT_NE(iter, nullptr);
41
42 // Iterate the dataset and get each row
43 std::unordered_map<std::string, mindspore::MSTensor> row;
44 ASSERT_OK(iter->GetNextRow(&row));
45
46 EXPECT_NE(row.find("image"), row.end());
47 EXPECT_NE(row.find("label"), row.end());
48
49 uint64_t i = 0;
50 while (row.size() != 0) {
51 i++;
52 auto image = row["image"];
53 MS_LOG(INFO) << "Tensor image shape: " << image.Shape();
54 ASSERT_OK(iter->GetNextRow(&row));
55 }
56
57 EXPECT_EQ(i, 10);
58
59 // Manually terminate the pipeline
60 iter->Stop();
61 }
62
TEST_F(MindDataTestPipeline,TestCifar10DatasetWithPipeline)63 TEST_F(MindDataTestPipeline, TestCifar10DatasetWithPipeline) {
64 MS_LOG(INFO) << "Doing MindDataTestPipeline-TestCifar10DatasetWithPipeline.";
65
66 // Create two Cifar10 Dataset
67 std::string folder_path = datasets_root_path_ + "/testCifar10Data/";
68 std::shared_ptr<Dataset> ds1 = Cifar10(folder_path, "all", std::make_shared<RandomSampler>(false, 10));
69 std::shared_ptr<Dataset> ds2 = Cifar10(folder_path, "all", std::make_shared<RandomSampler>(false, 10));
70 EXPECT_NE(ds1, nullptr);
71 EXPECT_NE(ds2, nullptr);
72
73 // Create two Repeat operation on ds
74 int32_t repeat_num = 1;
75 ds1 = ds1->Repeat(repeat_num);
76 EXPECT_NE(ds1, nullptr);
77 repeat_num = 1;
78 ds2 = ds2->Repeat(repeat_num);
79 EXPECT_NE(ds2, nullptr);
80
81 // Create two Project operation on ds
82 std::vector<std::string> column_project = {"image", "label"};
83 ds1 = ds1->Project(column_project);
84 EXPECT_NE(ds1, nullptr);
85 ds2 = ds2->Project(column_project);
86 EXPECT_NE(ds2, nullptr);
87
88 // Create a Concat operation on the ds
89 ds1 = ds1->Concat({ds2});
90 EXPECT_NE(ds1, nullptr);
91
92 // Create an iterator over the result of the above dataset
93 // This will trigger the creation of the Execution Tree and launch it.
94 std::shared_ptr<Iterator> iter = ds1->CreateIterator();
95 EXPECT_NE(iter, nullptr);
96
97 // Iterate the dataset and get each row
98 std::unordered_map<std::string, mindspore::MSTensor> row;
99 ASSERT_OK(iter->GetNextRow(&row));
100
101 EXPECT_NE(row.find("image"), row.end());
102 EXPECT_NE(row.find("label"), row.end());
103
104 uint64_t i = 0;
105 while (row.size() != 0) {
106 i++;
107 auto image = row["image"];
108 MS_LOG(INFO) << "Tensor image shape: " << image.Shape();
109 ASSERT_OK(iter->GetNextRow(&row));
110 }
111
112 EXPECT_EQ(i, 20);
113
114 // Manually terminate the pipeline
115 iter->Stop();
116 }
117
TEST_F(MindDataTestPipeline,TestCifar10GetDatasetSize)118 TEST_F(MindDataTestPipeline, TestCifar10GetDatasetSize) {
119 MS_LOG(INFO) << "Doing MindDataTestPipeline-TestCifar10GetDatasetSize.";
120
121 // Create a Cifar10 Dataset
122 std::string folder_path = datasets_root_path_ + "/testCifar10Data/";
123 std::shared_ptr<Dataset> ds = Cifar10(folder_path, "all");
124 EXPECT_NE(ds, nullptr);
125
126 EXPECT_EQ(ds->GetDatasetSize(), 10000);
127 }
128
TEST_F(MindDataTestPipeline,TestCifar10Getters)129 TEST_F(MindDataTestPipeline, TestCifar10Getters) {
130 MS_LOG(INFO) << "Doing MindDataTestPipeline-TestCifar10MixGetter.";
131
132 // Create a Cifar10 Dataset
133 std::string folder_path = datasets_root_path_ + "/testCifar10Data/";
134 std::shared_ptr<Dataset> ds = Cifar10(folder_path, "all");
135 EXPECT_NE(ds, nullptr);
136
137 EXPECT_EQ(ds->GetDatasetSize(), 10000);
138 std::vector<DataType> types = ToDETypes(ds->GetOutputTypes());
139 std::vector<TensorShape> shapes = ToTensorShapeVec(ds->GetOutputShapes());
140 std::vector<std::string> column_names = {"image", "label"};
141 int64_t num_classes = ds->GetNumClasses();
142 EXPECT_EQ(types.size(), 2);
143 EXPECT_EQ(types[0].ToString(), "uint8");
144 EXPECT_EQ(types[1].ToString(), "uint32");
145 EXPECT_EQ(shapes.size(), 2);
146 EXPECT_EQ(shapes[0].ToString(), "<32,32,3>");
147 EXPECT_EQ(shapes[1].ToString(), "<>");
148 EXPECT_EQ(num_classes, -1);
149 EXPECT_EQ(ds->GetBatchSize(), 1);
150 EXPECT_EQ(ds->GetRepeatCount(), 1);
151
152 EXPECT_EQ(ds->GetDatasetSize(), 10000);
153 EXPECT_EQ(ToDETypes(ds->GetOutputTypes()), types);
154 EXPECT_EQ(ToTensorShapeVec(ds->GetOutputShapes()), shapes);
155 EXPECT_EQ(ds->GetNumClasses(), -1);
156
157 EXPECT_EQ(ds->GetColumnNames(), column_names);
158 EXPECT_EQ(ds->GetDatasetSize(), 10000);
159 EXPECT_EQ(ToDETypes(ds->GetOutputTypes()), types);
160 EXPECT_EQ(ToTensorShapeVec(ds->GetOutputShapes()), shapes);
161 EXPECT_EQ(ds->GetBatchSize(), 1);
162 EXPECT_EQ(ds->GetRepeatCount(), 1);
163 EXPECT_EQ(ds->GetNumClasses(), -1);
164 EXPECT_EQ(ds->GetDatasetSize(), 10000);
165 }
166
TEST_F(MindDataTestPipeline,TestCifar100Dataset)167 TEST_F(MindDataTestPipeline, TestCifar100Dataset) {
168 MS_LOG(INFO) << "Doing MindDataTestPipeline-TestCifar100Dataset.";
169
170 // Create a Cifar100 Dataset
171 std::string folder_path = datasets_root_path_ + "/testCifar100Data/";
172 std::shared_ptr<Dataset> ds = Cifar100(folder_path, "all", std::make_shared<RandomSampler>(false, 10));
173 EXPECT_NE(ds, nullptr);
174
175 // Create an iterator over the result of the above dataset
176 // This will trigger the creation of the Execution Tree and launch it.
177 std::shared_ptr<Iterator> iter = ds->CreateIterator();
178 EXPECT_NE(iter, nullptr);
179
180 // Iterate the dataset and get each row
181 std::unordered_map<std::string, mindspore::MSTensor> row;
182 ASSERT_OK(iter->GetNextRow(&row));
183
184 EXPECT_NE(row.find("image"), row.end());
185 EXPECT_NE(row.find("coarse_label"), row.end());
186 EXPECT_NE(row.find("fine_label"), row.end());
187
188 uint64_t i = 0;
189 while (row.size() != 0) {
190 i++;
191 auto image = row["image"];
192 MS_LOG(INFO) << "Tensor image shape: " << image.Shape();
193 ASSERT_OK(iter->GetNextRow(&row));
194 }
195
196 EXPECT_EQ(i, 10);
197
198 // Manually terminate the pipeline
199 iter->Stop();
200 }
201
TEST_F(MindDataTestPipeline,TestCifar100Getters)202 TEST_F(MindDataTestPipeline, TestCifar100Getters) {
203 MS_LOG(INFO) << "Doing MindDataTestPipeline-TestCifar100Getters.";
204
205 // Create a Cifar100 Dataset
206 std::string folder_path = datasets_root_path_ + "/testCifar100Data/";
207 std::shared_ptr<Dataset> ds = Cifar100(folder_path, "all", std::make_shared<RandomSampler>(false, 10));
208 EXPECT_NE(ds, nullptr);
209
210 std::vector<std::string> column_names = {"image", "coarse_label", "fine_label"};
211 std::vector<DataType> types = ToDETypes(ds->GetOutputTypes());
212 std::vector<TensorShape> shapes = ToTensorShapeVec(ds->GetOutputShapes());
213 int64_t num_classes = ds->GetNumClasses();
214
215 EXPECT_EQ(types.size(), 3);
216 EXPECT_EQ(types[0].ToString(), "uint8");
217 EXPECT_EQ(types[1].ToString(), "uint32");
218 EXPECT_EQ(types[2].ToString(), "uint32");
219 EXPECT_EQ(shapes.size(), 3);
220 EXPECT_EQ(shapes[0].ToString(), "<32,32,3>");
221 EXPECT_EQ(shapes[1].ToString(), "<>");
222 EXPECT_EQ(shapes[2].ToString(), "<>");
223 EXPECT_EQ(num_classes, -1);
224 EXPECT_EQ(ds->GetBatchSize(), 1);
225 EXPECT_EQ(ds->GetRepeatCount(), 1);
226 EXPECT_EQ(ds->GetDatasetSize(), 10);
227 EXPECT_EQ(ds->GetColumnNames(), column_names);
228 }
229
TEST_F(MindDataTestPipeline,TestCifar100DatasetFail)230 TEST_F(MindDataTestPipeline, TestCifar100DatasetFail) {
231 MS_LOG(INFO) << "Doing MindDataTestPipeline-TestCifar100DatasetFail.";
232
233 // Create a Cifar100 Dataset
234 std::shared_ptr<Dataset> ds = Cifar100("", "all", std::make_shared<RandomSampler>(false, 10));
235 EXPECT_NE(ds, nullptr);
236
237 // Create an iterator over the result of the above dataset
238 std::shared_ptr<Iterator> iter = ds->CreateIterator();
239 // Expect failure: invalid Cifar100 input
240 EXPECT_EQ(iter, nullptr);
241 }
242
TEST_F(MindDataTestPipeline,TestCifar10DatasetFail)243 TEST_F(MindDataTestPipeline, TestCifar10DatasetFail) {
244 MS_LOG(INFO) << "Doing MindDataTestPipeline-TestCifar10DatasetFail.";
245
246 // Create a Cifar10 Dataset
247 std::shared_ptr<Dataset> ds = Cifar10("", "all", std::make_shared<RandomSampler>(false, 10));
248 EXPECT_NE(ds, nullptr);
249
250 // Create an iterator over the result of the above dataset
251 std::shared_ptr<Iterator> iter = ds->CreateIterator();
252 // Expect failure: invalid Cifar10 input
253 EXPECT_EQ(iter, nullptr);
254 }
255
TEST_F(MindDataTestPipeline,TestCifar10DatasetWithInvalidUsageFail)256 TEST_F(MindDataTestPipeline, TestCifar10DatasetWithInvalidUsageFail) {
257 MS_LOG(INFO) << "Doing MindDataTestPipeline-TestCifar10DatasetWithNullSamplerFail.";
258
259 // Create a Cifar10 Dataset
260 std::string folder_path = datasets_root_path_ + "/testCifar10Data/";
261 std::shared_ptr<Dataset> ds = Cifar10(folder_path, "validation");
262 EXPECT_NE(ds, nullptr);
263
264 // Create an iterator over the result of the above dataset
265 std::shared_ptr<Iterator> iter = ds->CreateIterator();
266 // Expect failure: invalid Cifar10 input, validation is not a valid usage
267 EXPECT_EQ(iter, nullptr);
268 }
269
TEST_F(MindDataTestPipeline,TestCifar10DatasetWithNullSamplerFail)270 TEST_F(MindDataTestPipeline, TestCifar10DatasetWithNullSamplerFail) {
271 MS_LOG(INFO) << "Doing MindDataTestPipeline-TestCifar10DatasetWithNullSamplerFail.";
272
273 // Create a Cifar10 Dataset
274 std::string folder_path = datasets_root_path_ + "/testCifar10Data/";
275 std::shared_ptr<Dataset> ds = Cifar10(folder_path, "all", nullptr);
276 EXPECT_NE(ds, nullptr);
277
278 // Create an iterator over the result of the above dataset
279 std::shared_ptr<Iterator> iter = ds->CreateIterator();
280 // Expect failure: invalid Cifar10 input, sampler cannot be nullptr
281 EXPECT_EQ(iter, nullptr);
282 }
283
TEST_F(MindDataTestPipeline,TestCifar100DatasetWithNullSamplerFail)284 TEST_F(MindDataTestPipeline, TestCifar100DatasetWithNullSamplerFail) {
285 MS_LOG(INFO) << "Doing MindDataTestPipeline-TestCifar100DatasetWithNullSamplerFail.";
286
287 // Create a Cifar100 Dataset
288 std::string folder_path = datasets_root_path_ + "/testCifar100Data/";
289 std::shared_ptr<Dataset> ds = Cifar100(folder_path, "all", nullptr);
290 EXPECT_NE(ds, nullptr);
291
292 // Create an iterator over the result of the above dataset
293 std::shared_ptr<Iterator> iter = ds->CreateIterator();
294 // Expect failure: invalid Cifar100 input, sampler cannot be nullptr
295 EXPECT_EQ(iter, nullptr);
296 }
297
TEST_F(MindDataTestPipeline,TestCifar100DatasetWithWrongSamplerFail)298 TEST_F(MindDataTestPipeline, TestCifar100DatasetWithWrongSamplerFail) {
299 MS_LOG(INFO) << "Doing MindDataTestPipeline-TestCifar100DatasetWithWrongSamplerFail.";
300
301 // Create a Cifar100 Dataset
302 std::string folder_path = datasets_root_path_ + "/testCifar100Data/";
303 std::shared_ptr<Dataset> ds = Cifar100(folder_path, "all", std::make_shared<RandomSampler>(false, -10));
304 EXPECT_NE(ds, nullptr);
305
306 // Create an iterator over the result of the above dataset
307 std::shared_ptr<Iterator> iter = ds->CreateIterator();
308 // Expect failure: invalid Cifar100 input, sampler is not constructed correctly
309 EXPECT_EQ(iter, nullptr);
310 }
311