• 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 #include "mindspore/ccsrc/minddata/dataset/core/tensor.h"
21 #include "mindspore/ccsrc/minddata/dataset/core/data_type.h"
22 
23 using namespace mindspore::dataset;
24 using mindspore::dataset::BorderType;
25 using mindspore::dataset::Tensor;
26 
27 class MindDataTestPipeline : public UT::DatasetOpTesting {
28  protected:
29 };
30 
31 // Tests for data transforms ops (in alphabetical order)
32 
TEST_F(MindDataTestPipeline,TestComposeSuccess)33 TEST_F(MindDataTestPipeline, TestComposeSuccess) {
34   MS_LOG(INFO) << "Doing MindDataTestPipeline-TestComposeSuccess.";
35 
36   // Create an ImageFolder Dataset
37   std::string folder_path = datasets_root_path_ + "/testPK/data/";
38   std::shared_ptr<Dataset> ds = ImageFolder(folder_path, false, std::make_shared<RandomSampler>(false, 3));
39   EXPECT_NE(ds, nullptr);
40 
41   // Create objects for the tensor ops
42   std::shared_ptr<TensorTransform> decode_op(new vision::Decode());
43   std::shared_ptr<TensorTransform> resize_op(new vision::Resize({777, 777}));
44   transforms::Compose compose({decode_op, resize_op});
45 
46   // Create a Map operation on ds
47   ds = ds->Map({compose}, {"image"});
48   EXPECT_NE(ds, nullptr);
49 
50   // Create an iterator over the result of the above dataset
51   // This will trigger the creation of the Execution Tree and launch it.
52   std::shared_ptr<Iterator> iter = ds->CreateIterator();
53   EXPECT_NE(iter, nullptr);
54 
55   // Iterate the dataset and get each row
56   std::unordered_map<std::string, mindspore::MSTensor> row;
57   ASSERT_OK(iter->GetNextRow(&row));
58 
59   uint64_t i = 0;
60   while (row.size() != 0) {
61     i++;
62     auto image = row["image"];
63     auto label = row["label"];
64     MS_LOG(INFO) << "Tensor image shape: " << image.Shape();
65     MS_LOG(INFO) << "Label shape: " << label.Shape();
66     EXPECT_EQ(image.Shape()[0], 777);
67     EXPECT_EQ(image.Shape()[1], 777);
68     ASSERT_OK(iter->GetNextRow(&row));
69   }
70 
71   EXPECT_EQ(i, 3);
72 
73   // Manually terminate the pipeline
74   iter->Stop();
75 }
76 
TEST_F(MindDataTestPipeline,TestComposeFail1)77 TEST_F(MindDataTestPipeline, TestComposeFail1) {
78   MS_LOG(INFO) << "Doing MindDataTestPipeline-TestComposeFail1 with invalid transform.";
79 
80   // Create a Cifar10 Dataset
81   std::string folder_path = datasets_root_path_ + "/testCifar10Data/";
82   std::shared_ptr<Dataset> ds = Cifar10(folder_path, "all", std::make_shared<RandomSampler>(false, 10));
83   EXPECT_NE(ds, nullptr);
84 
85   // Resize: Non-positive size value: -1 at element: 0
86   // Compose: transform ops must not be null
87   auto decode_op = vision::Decode();
88   auto resize_op = vision::Resize({-1});
89   auto compose = transforms::Compose({decode_op, resize_op});
90 
91   // Create a Map operation on ds
92   ds = ds->Map({compose}, {"image"});
93   EXPECT_NE(ds, nullptr);
94 
95   std::shared_ptr<Iterator> iter = ds->CreateIterator();
96   // Expect failure: invalid Compose parameter(invalid transform op)
97   EXPECT_EQ(iter, nullptr);
98 }
99 
TEST_F(MindDataTestPipeline,TestComposeFail2)100 TEST_F(MindDataTestPipeline, TestComposeFail2) {
101   MS_LOG(INFO) << "Doing MindDataTestPipeline-TestComposeFail2 with invalid transform.";
102 
103   // Create a Cifar10 Dataset
104   std::string folder_path = datasets_root_path_ + "/testCifar10Data/";
105   std::shared_ptr<Dataset> ds = Cifar10(folder_path, "all", std::make_shared<RandomSampler>(false, 10));
106   EXPECT_NE(ds, nullptr);
107 
108   // Compose: transform ops must not be null
109   std::shared_ptr<TensorTransform> decode_op = std::make_shared<vision::Decode>();
110   std::shared_ptr<TensorTransform> compose(new transforms::Compose({decode_op, nullptr}));
111 
112   // Create a Map operation on ds
113   ds = ds->Map({compose}, {"image"});
114   EXPECT_NE(ds, nullptr);
115 
116   std::shared_ptr<Iterator> iter = ds->CreateIterator();
117   // Expect failure: invalid Compose parameter (transform ops must not be null)
118   EXPECT_EQ(iter, nullptr);
119 }
120 
TEST_F(MindDataTestPipeline,TestComposeFail3)121 TEST_F(MindDataTestPipeline, TestComposeFail3) {
122   MS_LOG(INFO) << "Doing MindDataTestPipeline-TestComposeFail3 with invalid transform.";
123 
124   // Create a Cifar10 Dataset
125   std::string folder_path = datasets_root_path_ + "/testCifar10Data/";
126   std::shared_ptr<Dataset> ds = Cifar10(folder_path, "all", std::make_shared<RandomSampler>(false, 10));
127   EXPECT_NE(ds, nullptr);
128 
129   // Compose: transform list must not be empty
130   std::vector<std::shared_ptr<TensorTransform>> list = {};
131   auto compose = transforms::Compose(list);
132 
133   // Create a Map operation on ds
134   ds = ds->Map({compose}, {"image"});
135   EXPECT_NE(ds, nullptr);
136 
137   std::shared_ptr<Iterator> iter = ds->CreateIterator();
138   // Expect failure: invalid Compose parameter (transform list must not be empty)
139   EXPECT_EQ(iter, nullptr);
140 }
141 
TEST_F(MindDataTestPipeline,TestConcatenateSuccess1)142 TEST_F(MindDataTestPipeline, TestConcatenateSuccess1) {
143   MS_LOG(INFO) << "Doing MindDataTestPipeline-TestConcatenateSuccess1.";
144   // Test basic concatenate with prepend and append
145 
146   // Create a RandomDataset
147   u_int32_t curr_seed = GlobalContext::config_manager()->seed();
148   GlobalContext::config_manager()->set_seed(246);
149   std::shared_ptr<SchemaObj> schema = Schema();
150   ASSERT_OK(schema->add_column("col1", mindspore::DataType::kNumberTypeInt16, {1}));
151   std::shared_ptr<Dataset> ds = RandomData(4, schema);
152   EXPECT_NE(ds, nullptr);
153   ds = ds->SetNumWorkers(2);
154   EXPECT_NE(ds, nullptr);
155 
156   // Create Concatenate op
157   std::vector<int16_t> prepend_vector = {1, 2};
158   std::shared_ptr<Tensor> prepend_tensor;
159   ASSERT_OK(Tensor::CreateFromVector(prepend_vector, &prepend_tensor));
160   mindspore::MSTensor prepend_MSTensor =
161     mindspore::MSTensor(std::make_shared<mindspore::dataset::DETensor>(prepend_tensor));
162 
163   std::vector<int16_t> append_vector = {3};
164   std::shared_ptr<Tensor> append_tensor;
165   ASSERT_OK(Tensor::CreateFromVector(append_vector, &append_tensor));
166   mindspore::MSTensor append_MSTensor =
167     mindspore::MSTensor(std::make_shared<mindspore::dataset::DETensor>(append_tensor));
168 
169   transforms::Concatenate concatenate = transforms::Concatenate(0, prepend_MSTensor, append_MSTensor);
170 
171   // Create a Map operation on ds
172   ds = ds->Map({concatenate}, {"col1"});
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   std::vector<std::vector<int16_t>> expected = {
185     {1, 2, 31354, 3}, {1, 2, -5655, 3}, {1, 2, -17734, 3}, {1, 2, -17220, 3}};
186 
187   // Check concatenate results
188   uint64_t i = 0;
189   while (row.size() != 0) {
190     auto ind = row["col1"];
191     std::shared_ptr<Tensor> de_expected_tensor;
192     ASSERT_OK(Tensor::CreateFromVector(expected[i], &de_expected_tensor));
193     mindspore::MSTensor expected_tensor =
194       mindspore::MSTensor(std::make_shared<mindspore::dataset::DETensor>(de_expected_tensor));
195     EXPECT_MSTENSOR_EQ(ind, expected_tensor);
196     ASSERT_OK(iter->GetNextRow(&row));
197     i++;
198   }
199 
200   EXPECT_EQ(i, 4);
201 
202   // Manually terminate the pipeline
203   iter->Stop();
204   GlobalContext::config_manager()->set_seed(curr_seed);
205 }
206 
TEST_F(MindDataTestPipeline,TestConcatenateSuccess2)207 TEST_F(MindDataTestPipeline, TestConcatenateSuccess2) {
208   MS_LOG(INFO) << "Doing MindDataTestPipeline-TestConcatenateSuccess2.";
209   // Test concatenate with no input
210 
211   // Create a RandomDataset
212   u_int32_t curr_seed = GlobalContext::config_manager()->seed();
213   GlobalContext::config_manager()->set_seed(246);
214   std::shared_ptr<SchemaObj> schema = Schema();
215   ASSERT_OK(schema->add_column("col1", mindspore::DataType::kNumberTypeInt16, {1}));
216   std::shared_ptr<Dataset> ds = RandomData(4, schema);
217   EXPECT_NE(ds, nullptr);
218   ds = ds->SetNumWorkers(2);
219   EXPECT_NE(ds, nullptr);
220 
221   transforms::Concatenate concatenate = transforms::Concatenate();
222 
223   // Create a Map operation on ds
224   ds = ds->Map({concatenate}, {"col1"});
225   EXPECT_NE(ds, nullptr);
226 
227   // Create an iterator over the result of the above dataset
228   // This will trigger the creation of the Execution Tree and launch it.
229   std::shared_ptr<Iterator> iter = ds->CreateIterator();
230   EXPECT_NE(iter, nullptr);
231 
232   // Iterate the dataset and get each row
233   std::unordered_map<std::string, mindspore::MSTensor> row;
234   ASSERT_OK(iter->GetNextRow(&row));
235 
236   // The data generated by RandomData
237   std::vector<std::vector<int16_t>> expected = {{31354}, {-5655}, {-17734}, {-17220}};
238 
239   // Check concatenate results
240   uint64_t i = 0;
241   while (row.size() != 0) {
242     auto ind = row["col1"];
243     std::shared_ptr<Tensor> de_expected_tensor;
244     ASSERT_OK(Tensor::CreateFromVector(expected[i], &de_expected_tensor));
245     mindspore::MSTensor expected_tensor =
246       mindspore::MSTensor(std::make_shared<mindspore::dataset::DETensor>(de_expected_tensor));
247     EXPECT_MSTENSOR_EQ(ind, expected_tensor);
248     ASSERT_OK(iter->GetNextRow(&row));
249     i++;
250   }
251 
252   EXPECT_EQ(i, 4);
253 
254   // Manually terminate the pipeline
255   iter->Stop();
256   GlobalContext::config_manager()->set_seed(curr_seed);
257 }
258 
TEST_F(MindDataTestPipeline,TestConcatenateSuccess3)259 TEST_F(MindDataTestPipeline, TestConcatenateSuccess3) {
260   MS_LOG(INFO) << "Doing MindDataTestPipeline-TestConcatenateSuccess3.";
261   // Test concatenate of string
262 
263   // Create a TextFile dataset
264   std::string data_file = datasets_root_path_ + "/testTokenizerData/1.txt";
265   std::shared_ptr<Dataset> ds = TextFile({data_file}, 0, ShuffleMode::kFalse);
266   EXPECT_NE(ds, nullptr);
267 
268   // Create Take operation on ds
269   ds = ds->Take(1);
270   EXPECT_NE(ds, nullptr);
271 
272   // Create BasicTokenizer operation on ds
273   std::shared_ptr<TensorTransform> basic_tokenizer = std::make_shared<text::BasicTokenizer>(true);
274   EXPECT_NE(basic_tokenizer, nullptr);
275 
276   // Create Map operation on ds
277   ds = ds->Map({basic_tokenizer}, {"text"});
278   EXPECT_NE(ds, nullptr);
279 
280   // Create Concatenate op
281   std::vector<std::string> prepend_vector = {"1", "2"};
282   std::shared_ptr<Tensor> prepend_tensor;
283   ASSERT_OK(Tensor::CreateFromVector(prepend_vector, &prepend_tensor));
284   mindspore::MSTensor prepend_MSTensor =
285     mindspore::MSTensor(std::make_shared<mindspore::dataset::DETensor>(prepend_tensor));
286 
287   std::vector<std::string> append_vector = {"3"};
288   std::shared_ptr<Tensor> append_tensor;
289   ASSERT_OK(Tensor::CreateFromVector(append_vector, &append_tensor));
290   mindspore::MSTensor append_MSTensor =
291     mindspore::MSTensor(std::make_shared<mindspore::dataset::DETensor>(append_tensor));
292 
293   transforms::Concatenate concatenate = transforms::Concatenate(0, prepend_MSTensor, append_MSTensor);
294 
295   // Create a Map operation on ds
296   ds = ds->Map({concatenate}, {"text"});
297   EXPECT_NE(ds, nullptr);
298 
299   // Create an iterator over the result of the above dataset
300   // This will trigger the creation of the Execution Tree and launch it.
301   std::shared_ptr<Iterator> iter = ds->CreateIterator();
302   EXPECT_NE(iter, nullptr);
303 
304   // Iterate the dataset and get each row
305   std::unordered_map<std::string, mindspore::MSTensor> row;
306   ASSERT_OK(iter->GetNextRow(&row));
307 
308   std::vector<std::vector<std::string>> expected = {{"1", "2", "welcome", "to", "beijing", "!", "3"}};
309 
310   // Check concatenate results
311   uint64_t i = 0;
312   while (row.size() != 0) {
313     auto ind = row["text"];
314     std::shared_ptr<Tensor> de_expected_tensor;
315     ASSERT_OK(Tensor::CreateFromVector(expected[i], &de_expected_tensor));
316     mindspore::MSTensor expected_tensor =
317       mindspore::MSTensor(std::make_shared<mindspore::dataset::DETensor>(de_expected_tensor));
318     EXPECT_MSTENSOR_EQ(ind, expected_tensor);
319     ASSERT_OK(iter->GetNextRow(&row));
320     i++;
321   }
322 
323   EXPECT_EQ(i, 1);
324 
325   // Manually terminate the pipeline
326   iter->Stop();
327   // GlobalContext::config_manager()->set_seed(curr_seed);
328 }
329 
TEST_F(MindDataTestPipeline,TestConcatenateSuccess4)330 TEST_F(MindDataTestPipeline, TestConcatenateSuccess4) {
331   MS_LOG(INFO) << "Doing MindDataTestPipeline-TestConcatenateSuccess4.";
332   // Test concatenate with negative axis
333 
334   // Create a RandomDataset
335   u_int32_t curr_seed = GlobalContext::config_manager()->seed();
336   GlobalContext::config_manager()->set_seed(246);
337   std::shared_ptr<SchemaObj> schema = Schema();
338   ASSERT_OK(schema->add_column("col1", mindspore::DataType::kNumberTypeInt16, {1}));
339   std::shared_ptr<Dataset> ds = RandomData(4, schema);
340   EXPECT_NE(ds, nullptr);
341   ds = ds->SetNumWorkers(2);
342   EXPECT_NE(ds, nullptr);
343 
344   // Create Concatenate op
345   std::vector<int16_t> prepend_vector = {1, 2};
346   std::shared_ptr<Tensor> prepend_tensor;
347   ASSERT_OK(Tensor::CreateFromVector(prepend_vector, &prepend_tensor));
348   mindspore::MSTensor prepend_MSTensor =
349     mindspore::MSTensor(std::make_shared<mindspore::dataset::DETensor>(prepend_tensor));
350 
351   std::vector<int16_t> append_vector = {3};
352   std::shared_ptr<Tensor> append_tensor;
353   ASSERT_OK(Tensor::CreateFromVector(append_vector, &append_tensor));
354   mindspore::MSTensor append_MSTensor =
355     mindspore::MSTensor(std::make_shared<mindspore::dataset::DETensor>(append_tensor));
356 
357   transforms::Concatenate concatenate = transforms::Concatenate(-1, prepend_MSTensor, append_MSTensor);
358 
359   // Create a Map operation on ds
360   ds = ds->Map({concatenate}, {"col1"});
361   EXPECT_NE(ds, nullptr);
362 
363   // Create an iterator over the result of the above dataset
364   // This will trigger the creation of the Execution Tree and launch it.
365   std::shared_ptr<Iterator> iter = ds->CreateIterator();
366   EXPECT_NE(iter, nullptr);
367 
368   // Iterate the dataset and get each row
369   std::unordered_map<std::string, mindspore::MSTensor> row;
370   ASSERT_OK(iter->GetNextRow(&row));
371 
372   std::vector<std::vector<int16_t>> expected = {
373     {1, 2, 31354, 3}, {1, 2, -5655, 3}, {1, 2, -17734, 3}, {1, 2, -17220, 3}};
374 
375   // Check concatenate results
376   uint64_t i = 0;
377   while (row.size() != 0) {
378     auto ind = row["col1"];
379     std::shared_ptr<Tensor> de_expected_tensor;
380     ASSERT_OK(Tensor::CreateFromVector(expected[i], &de_expected_tensor));
381     mindspore::MSTensor expected_tensor =
382       mindspore::MSTensor(std::make_shared<mindspore::dataset::DETensor>(de_expected_tensor));
383     EXPECT_MSTENSOR_EQ(ind, expected_tensor);
384     ASSERT_OK(iter->GetNextRow(&row));
385     i++;
386   }
387 
388   EXPECT_EQ(i, 4);
389 
390   // Manually terminate the pipeline
391   iter->Stop();
392   GlobalContext::config_manager()->set_seed(curr_seed);
393 }
394 
TEST_F(MindDataTestPipeline,TestConcatenateFail1)395 TEST_F(MindDataTestPipeline, TestConcatenateFail1) {
396   MS_LOG(INFO) << "Doing MindDataTestPipeline-TestConcatenateFail1.";
397   // Test concatenate with type mismatch
398 
399   // Create a RandomDataset
400   u_int32_t curr_seed = GlobalContext::config_manager()->seed();
401   GlobalContext::config_manager()->set_seed(246);
402   std::shared_ptr<SchemaObj> schema = Schema();
403   ASSERT_OK(schema->add_column("col1", mindspore::DataType::kNumberTypeInt16, {1}));
404   std::shared_ptr<Dataset> ds = RandomData(1, schema);
405   EXPECT_NE(ds, nullptr);
406   ds = ds->SetNumWorkers(1);
407   EXPECT_NE(ds, nullptr);
408 
409   // Create Concatenate op
410   std::vector<std::string> prepend_vector = {"1", "2"};
411   std::shared_ptr<Tensor> prepend_tensor;
412   ASSERT_OK(Tensor::CreateFromVector(prepend_vector, &prepend_tensor));
413   mindspore::MSTensor prepend_MSTensor =
414     mindspore::MSTensor(std::make_shared<mindspore::dataset::DETensor>(prepend_tensor));
415 
416   std::vector<std::string> append_vector = {"3"};
417   std::shared_ptr<Tensor> append_tensor;
418   ASSERT_OK(Tensor::CreateFromVector(append_vector, &append_tensor));
419   mindspore::MSTensor append_MSTensor =
420     mindspore::MSTensor(std::make_shared<mindspore::dataset::DETensor>(append_tensor));
421 
422   transforms::Concatenate concatenate = transforms::Concatenate(0, prepend_MSTensor, append_MSTensor);
423 
424   // Create a Map operation on ds
425   ds = ds->Map({concatenate}, {"col1"});
426   EXPECT_NE(ds, nullptr);
427 
428   // Create an iterator over the result of the above dataset
429   // This will trigger the creation of the Execution Tree and launch it.
430   std::shared_ptr<Iterator> iter = ds->CreateIterator();
431   // EXPECT_EQ(iter, nullptr);
432   EXPECT_NE(iter, nullptr);
433 
434   // Iterate the dataset and get each row
435   std::unordered_map<std::string, mindspore::MSTensor> row;
436   // Expect failure: type mismatch, concatenate string tensor to dataset of Int16
437   EXPECT_ERROR(iter->GetNextRow(&row));
438 
439   // Manually terminate the pipeline
440   iter->Stop();
441   GlobalContext::config_manager()->set_seed(curr_seed);
442 }
443 
TEST_F(MindDataTestPipeline,TestConcatenateFail2)444 TEST_F(MindDataTestPipeline, TestConcatenateFail2) {
445   MS_LOG(INFO) << "Doing MindDataTestPipeline-TestConcatenateFail2.";
446   // Test concatenate with incorrect dimension
447 
448   // Create a RandomDataset
449   u_int32_t curr_seed = GlobalContext::config_manager()->seed();
450   GlobalContext::config_manager()->set_seed(246);
451   std::shared_ptr<SchemaObj> schema = Schema();
452   ASSERT_OK(schema->add_column("col1", mindspore::DataType::kNumberTypeInt16, {1, 2}));
453   std::shared_ptr<Dataset> ds = RandomData(1, schema);
454   EXPECT_NE(ds, nullptr);
455   ds = ds->SetNumWorkers(1);
456   EXPECT_NE(ds, nullptr);
457 
458   // Create Concatenate op
459   std::vector<int16_t> prepend_vector = {1, 2};
460   std::shared_ptr<Tensor> prepend_tensor;
461   ASSERT_OK(Tensor::CreateFromVector(prepend_vector, &prepend_tensor));
462   mindspore::MSTensor prepend_MSTensor =
463     mindspore::MSTensor(std::make_shared<mindspore::dataset::DETensor>(prepend_tensor));
464 
465   std::vector<int16_t> append_vector = {3};
466   std::shared_ptr<Tensor> append_tensor;
467   ASSERT_OK(Tensor::CreateFromVector(append_vector, &append_tensor));
468   mindspore::MSTensor append_MSTensor =
469     mindspore::MSTensor(std::make_shared<mindspore::dataset::DETensor>(append_tensor));
470 
471   transforms::Concatenate concatenate = transforms::Concatenate(0, prepend_MSTensor, append_MSTensor);
472 
473   // Create a Map operation on ds
474   ds = ds->Map({concatenate}, {"col1"});
475   EXPECT_NE(ds, nullptr);
476 
477   // Create an iterator over the result of the above dataset
478   // This will trigger the creation of the Execution Tree and launch it.
479   std::shared_ptr<Iterator> iter = ds->CreateIterator();
480   EXPECT_NE(iter, nullptr);
481 
482   // Iterate the dataset and get each row
483   std::unordered_map<std::string, mindspore::MSTensor> row;
484   // Expect failure: concatenate on 2D dataset, only support 1D concatenate so far
485   EXPECT_ERROR(iter->GetNextRow(&row));
486 
487   // Manually terminate the pipeline
488   iter->Stop();
489   GlobalContext::config_manager()->set_seed(curr_seed);
490 }
491 
TEST_F(MindDataTestPipeline,TestConcatenateFail3)492 TEST_F(MindDataTestPipeline, TestConcatenateFail3) {
493   MS_LOG(INFO) << "Doing MindDataTestPipeline-TestConcatenateFail3.";
494   // Test concatenate with wrong axis
495 
496   // Create a RandomDataset
497   u_int32_t curr_seed = GlobalContext::config_manager()->seed();
498   GlobalContext::config_manager()->set_seed(246);
499   std::shared_ptr<SchemaObj> schema = Schema();
500   ASSERT_OK(schema->add_column("col1", mindspore::DataType::kNumberTypeInt16, {1}));
501   std::shared_ptr<Dataset> ds = RandomData(1, schema);
502   EXPECT_NE(ds, nullptr);
503   ds = ds->SetNumWorkers(1);
504   EXPECT_NE(ds, nullptr);
505 
506   // Create Concatenate op
507   std::vector<int16_t> prepend_vector = {1, 2};
508   std::shared_ptr<Tensor> prepend_tensor;
509   ASSERT_OK(Tensor::CreateFromVector(prepend_vector, &prepend_tensor));
510   mindspore::MSTensor prepend_MSTensor =
511     mindspore::MSTensor(std::make_shared<mindspore::dataset::DETensor>(prepend_tensor));
512 
513   std::vector<int16_t> append_vector = {3};
514   std::shared_ptr<Tensor> append_tensor;
515   ASSERT_OK(Tensor::CreateFromVector(append_vector, &append_tensor));
516   mindspore::MSTensor append_MSTensor =
517     mindspore::MSTensor(std::make_shared<mindspore::dataset::DETensor>(append_tensor));
518   // The parameter axis support 0 or -1 only for now
519   transforms::Concatenate concatenate = transforms::Concatenate(2, prepend_MSTensor, append_MSTensor);
520 
521   // Create a Map operation on ds
522   ds = ds->Map({concatenate}, {"col1"});
523   EXPECT_NE(ds, nullptr);
524 
525   // Create an iterator over the result of the above dataset
526   // This will trigger the creation of the Execution Tree and launch it.
527   std::shared_ptr<Iterator> iter = ds->CreateIterator();
528   // Expect failure: wrong axis, axis can only be 0 or -1
529   EXPECT_EQ(iter, nullptr);
530 
531   GlobalContext::config_manager()->set_seed(curr_seed);
532 }
533 
TEST_F(MindDataTestPipeline,TestDuplicateSuccess)534 TEST_F(MindDataTestPipeline, TestDuplicateSuccess) {
535   MS_LOG(INFO) << "Doing MindDataTestPipeline-TestDuplicateSuccess.";
536 
537   // Create a Cifar10 Dataset
538   std::string folder_path = datasets_root_path_ + "/testCifar10Data/";
539   std::shared_ptr<Dataset> ds = Cifar10(folder_path, "all", std::make_shared<RandomSampler>(false, 10));
540   EXPECT_NE(ds, nullptr);
541 
542   // Create objects for the tensor ops
543   transforms::Duplicate duplicate = transforms::Duplicate();
544 
545   // Create a Map operation on ds
546   ds = ds->Map({duplicate}, {"image"}, {"image", "image_copy"});
547   EXPECT_NE(ds, nullptr);
548 
549   // Create an iterator over the result of the above dataset
550   // This will trigger the creation of the Execution Tree and launch it.
551   std::shared_ptr<Iterator> iter = ds->CreateIterator();
552   EXPECT_NE(iter, nullptr);
553 
554   // Iterate the dataset and get each row
555   std::unordered_map<std::string, mindspore::MSTensor> row;
556   ASSERT_OK(iter->GetNextRow(&row));
557 
558   uint64_t i = 0;
559   while (row.size() != 0) {
560     i++;
561     auto image = row["image"];
562     auto image_copy = row["image_copy"];
563     MS_LOG(INFO) << "Tensor image shape: " << image.Shape();
564     EXPECT_MSTENSOR_EQ(image, image_copy);
565     ASSERT_OK(iter->GetNextRow(&row));
566   }
567 
568   EXPECT_EQ(i, 10);
569 
570   // Manually terminate the pipeline
571   iter->Stop();
572 }
573 
TEST_F(MindDataTestPipeline,TestFillSuccessInt)574 TEST_F(MindDataTestPipeline, TestFillSuccessInt) {
575   MS_LOG(INFO) << "Doing MindDataTestPipeline-TestFillSuccessInt.";
576 
577   // Create a RandomDataset with Int32 numbers for given shape
578   u_int32_t curr_seed = GlobalContext::config_manager()->seed();
579   GlobalContext::config_manager()->set_seed(864);
580   std::shared_ptr<SchemaObj> schema = Schema();
581   ASSERT_OK(schema->add_column("col1", mindspore::DataType::kNumberTypeInt32, {6}));
582   std::shared_ptr<Dataset> ds = RandomData(5, schema);
583   EXPECT_NE(ds, nullptr);
584   ds = ds->SetNumWorkers(3);
585   EXPECT_NE(ds, nullptr);
586 
587   // Create Fill op - to fill with 3
588   std::shared_ptr<Tensor> fill_value_tensor;
589   ASSERT_OK(Tensor::CreateScalar(3, &fill_value_tensor));
590   mindspore::MSTensor fill_value_MSTensor =
591     mindspore::MSTensor(std::make_shared<mindspore::dataset::DETensor>(fill_value_tensor));
592   transforms::Fill mask = transforms::Fill(fill_value_MSTensor);
593   ds = ds->Map({mask}, {"col1"});
594   EXPECT_NE(ds, nullptr);
595 
596   // Create an iterator over the result of the above dataset
597   // This will trigger the creation of the Execution Tree and launch it.
598   std::shared_ptr<Iterator> iter = ds->CreateIterator();
599   EXPECT_NE(iter, nullptr);
600 
601   // Iterate the dataset and get each row
602   std::unordered_map<std::string, mindspore::MSTensor> row;
603   ASSERT_OK(iter->GetNextRow(&row));
604 
605   std::vector<std::vector<int32_t>> expected = {
606     {3, 3, 3, 3, 3, 3}, {3, 3, 3, 3, 3, 3}, {3, 3, 3, 3, 3, 3}, {3, 3, 3, 3, 3, 3}, {3, 3, 3, 3, 3, 3}};
607 
608   uint64_t i = 0;
609   while (row.size() != 0) {
610     auto ind = row["col1"];
611     TEST_MS_LOG_MSTENSOR(INFO, "ind: ", ind);
612     std::shared_ptr<Tensor> de_expected_tensor;
613     ASSERT_OK(Tensor::CreateFromVector(expected[i], &de_expected_tensor));
614     mindspore::MSTensor expected_tensor =
615       mindspore::MSTensor(std::make_shared<mindspore::dataset::DETensor>(de_expected_tensor));
616     EXPECT_MSTENSOR_EQ(ind, expected_tensor);
617 
618     ASSERT_OK(iter->GetNextRow(&row));
619     i++;
620   }
621 
622   EXPECT_EQ(i, 5);
623 
624   // Manually terminate the pipeline
625   iter->Stop();
626   GlobalContext::config_manager()->set_seed(curr_seed);
627 }
628 
TEST_F(MindDataTestPipeline,TestFillSuccessBool)629 TEST_F(MindDataTestPipeline, TestFillSuccessBool) {
630   MS_LOG(INFO) << "Doing MindDataTestPipeline-TestFillSuccessBool.";
631 
632   // Create a RandomDataset with bool values for given shape
633   u_int32_t curr_seed = GlobalContext::config_manager()->seed();
634   GlobalContext::config_manager()->set_seed(963);
635   std::shared_ptr<SchemaObj> schema = Schema();
636   ASSERT_OK(schema->add_column("col1", mindspore::DataType::kNumberTypeBool, {4}));
637   std::shared_ptr<Dataset> ds = RandomData(3, schema);
638   EXPECT_NE(ds, nullptr);
639   ds = ds->SetNumWorkers(2);
640   EXPECT_NE(ds, nullptr);
641 
642   // Create Fill op - to fill with zero
643   std::shared_ptr<Tensor> fill_value_tensor;
644   ASSERT_OK(Tensor::CreateScalar((bool)true, &fill_value_tensor));
645   mindspore::MSTensor fill_value_MSTensor =
646     mindspore::MSTensor(std::make_shared<mindspore::dataset::DETensor>(fill_value_tensor));
647   transforms::Fill mask = transforms::Fill(fill_value_MSTensor);
648   ds = ds->Map({mask}, {"col1"});
649   EXPECT_NE(ds, nullptr);
650 
651   // Create an iterator over the result of the above dataset
652   // This will trigger the creation of the Execution Tree and launch it.
653   std::shared_ptr<Iterator> iter = ds->CreateIterator();
654   EXPECT_NE(iter, nullptr);
655 
656   // Iterate the dataset and get each row
657   std::unordered_map<std::string, mindspore::MSTensor> row;
658   ASSERT_OK(iter->GetNextRow(&row));
659 
660   std::vector<std::vector<bool>> expected = {
661     {true, true, true, true}, {true, true, true, true}, {true, true, true, true}};
662 
663   uint64_t i = 0;
664   while (row.size() != 0) {
665     auto ind = row["col1"];
666     TEST_MS_LOG_MSTENSOR(INFO, "ind: ", ind);
667     std::shared_ptr<Tensor> de_expected_tensor;
668     ASSERT_OK(Tensor::CreateFromVector(expected[i], &de_expected_tensor));
669     mindspore::MSTensor expected_tensor =
670       mindspore::MSTensor(std::make_shared<mindspore::dataset::DETensor>(de_expected_tensor));
671     EXPECT_MSTENSOR_EQ(ind, expected_tensor);
672     ASSERT_OK(iter->GetNextRow(&row));
673     i++;
674   }
675 
676   EXPECT_EQ(i, 3);
677 
678   // Manually terminate the pipeline
679   iter->Stop();
680   GlobalContext::config_manager()->set_seed(curr_seed);
681 }
682 
TEST_F(MindDataTestPipeline,TestFillSuccessDownTypecast)683 TEST_F(MindDataTestPipeline, TestFillSuccessDownTypecast) {
684   MS_LOG(INFO) << "Doing MindDataTestPipeline-TestFillSuccessDownTypecast.";
685 
686   // Create a RandomDataset with UInt8 numbers for given shape
687   u_int32_t curr_seed = GlobalContext::config_manager()->seed();
688   GlobalContext::config_manager()->set_seed(963);
689   std::shared_ptr<SchemaObj> schema = Schema();
690   ASSERT_OK(schema->add_column("col1", mindspore::DataType::kNumberTypeUInt8, {4}));
691   std::shared_ptr<Dataset> ds = RandomData(3, schema);
692   EXPECT_NE(ds, nullptr);
693   ds = ds->SetNumWorkers(2);
694   EXPECT_NE(ds, nullptr);
695 
696   // Create Fill op - to fill with -3
697   std::shared_ptr<Tensor> fill_value_tensor;
698   ASSERT_OK(Tensor::CreateScalar(-3, &fill_value_tensor));
699   mindspore::MSTensor fill_value_MSTensor =
700     mindspore::MSTensor(std::make_shared<mindspore::dataset::DETensor>(fill_value_tensor));
701   transforms::Fill mask = transforms::Fill(fill_value_MSTensor);
702   ds = ds->Map({mask}, {"col1"});
703   EXPECT_NE(ds, nullptr);
704 
705   // Create an iterator over the result of the above dataset
706   // This will trigger the creation of the Execution Tree and launch it.
707   std::shared_ptr<Iterator> iter = ds->CreateIterator();
708   EXPECT_NE(iter, nullptr);
709 
710   // Iterate the dataset and get each row
711   std::unordered_map<std::string, mindspore::MSTensor> row;
712   ASSERT_OK(iter->GetNextRow(&row));
713 
714   // Note: 2**8 -3 = 256 -3 = 253
715   std::vector<std::vector<uint8_t>> expected = {{253, 253, 253, 253}, {253, 253, 253, 253}, {253, 253, 253, 253}};
716 
717   uint64_t i = 0;
718   while (row.size() != 0) {
719     auto ind = row["col1"];
720     TEST_MS_LOG_MSTENSOR(INFO, "ind: ", ind);
721     std::shared_ptr<Tensor> de_expected_tensor;
722     ASSERT_OK(Tensor::CreateFromVector(expected[i], &de_expected_tensor));
723     mindspore::MSTensor expected_tensor =
724       mindspore::MSTensor(std::make_shared<mindspore::dataset::DETensor>(de_expected_tensor));
725     EXPECT_MSTENSOR_EQ(ind, expected_tensor);
726 
727     ASSERT_OK(iter->GetNextRow(&row));
728     i++;
729   }
730 
731   EXPECT_EQ(i, 3);
732 
733   // Manually terminate the pipeline
734   iter->Stop();
735   GlobalContext::config_manager()->set_seed(curr_seed);
736 }
737 
TEST_F(MindDataTestPipeline,TestFillSuccessDownTypecastZero)738 TEST_F(MindDataTestPipeline, TestFillSuccessDownTypecastZero) {
739   MS_LOG(INFO) << "Doing MindDataTestPipeline-TestFillSuccessDownTypecastZero.";
740 
741   // Create a RandomDataset with UInt8 numbers for given shape
742   u_int32_t curr_seed = GlobalContext::config_manager()->seed();
743   GlobalContext::config_manager()->set_seed(963);
744   std::shared_ptr<SchemaObj> schema = Schema();
745   ASSERT_OK(schema->add_column("col1", mindspore::DataType::kNumberTypeUInt8, {4}));
746   std::shared_ptr<Dataset> ds = RandomData(3, schema);
747   EXPECT_NE(ds, nullptr);
748   ds = ds->SetNumWorkers(2);
749   EXPECT_NE(ds, nullptr);
750 
751   // Create Fill op - to fill with zero
752   std::shared_ptr<Tensor> fill_value_tensor;
753   ASSERT_OK(Tensor::CreateScalar(0, &fill_value_tensor));
754   mindspore::MSTensor fill_value_MSTensor =
755     mindspore::MSTensor(std::make_shared<mindspore::dataset::DETensor>(fill_value_tensor));
756   transforms::Fill mask = transforms::Fill(fill_value_MSTensor);
757   ds = ds->Map({mask}, {"col1"});
758   EXPECT_NE(ds, nullptr);
759 
760   // Create an iterator over the result of the above dataset
761   // This will trigger the creation of the Execution Tree and launch it.
762   std::shared_ptr<Iterator> iter = ds->CreateIterator();
763   EXPECT_NE(iter, nullptr);
764 
765   // Iterate the dataset and get each row
766   std::unordered_map<std::string, mindspore::MSTensor> row;
767   ASSERT_OK(iter->GetNextRow(&row));
768 
769   // Note: 2**8 = 256
770   std::vector<std::vector<uint8_t>> expected = {{0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}};
771 
772   uint64_t i = 0;
773   while (row.size() != 0) {
774     auto ind = row["col1"];
775     TEST_MS_LOG_MSTENSOR(INFO, "ind: ", ind);
776     std::shared_ptr<Tensor> de_expected_tensor;
777     ASSERT_OK(Tensor::CreateFromVector(expected[i], &de_expected_tensor));
778     mindspore::MSTensor expected_tensor =
779       mindspore::MSTensor(std::make_shared<mindspore::dataset::DETensor>(de_expected_tensor));
780     EXPECT_MSTENSOR_EQ(ind, expected_tensor);
781     ASSERT_OK(iter->GetNextRow(&row));
782     i++;
783   }
784 
785   EXPECT_EQ(i, 3);
786 
787   // Manually terminate the pipeline
788   iter->Stop();
789   GlobalContext::config_manager()->set_seed(curr_seed);
790 }
791 
TEST_F(MindDataTestPipeline,TestFillSuccessDownTypecast16)792 TEST_F(MindDataTestPipeline, TestFillSuccessDownTypecast16) {
793   MS_LOG(INFO) << "Doing MindDataTestPipeline-TestFillSuccessDownTypecast16.";
794 
795   // Create a RandomDataset with UInt16 numbers for given shape
796   u_int32_t curr_seed = GlobalContext::config_manager()->seed();
797   GlobalContext::config_manager()->set_seed(963);
798   std::shared_ptr<SchemaObj> schema = Schema();
799   ASSERT_OK(schema->add_column("col1", mindspore::DataType::kNumberTypeUInt16, {4}));
800   std::shared_ptr<Dataset> ds = RandomData(3, schema);
801   EXPECT_NE(ds, nullptr);
802   ds = ds->SetNumWorkers(2);
803   EXPECT_NE(ds, nullptr);
804 
805   // Create Fill op - to fill with -3
806   std::shared_ptr<Tensor> fill_value_tensor;
807   ASSERT_OK(Tensor::CreateScalar(-3, &fill_value_tensor));
808   mindspore::MSTensor fill_value_MSTensor =
809     mindspore::MSTensor(std::make_shared<mindspore::dataset::DETensor>(fill_value_tensor));
810   transforms::Fill mask = transforms::Fill(fill_value_MSTensor);
811   ds = ds->Map({mask}, {"col1"});
812   EXPECT_NE(ds, nullptr);
813 
814   // Create an iterator over the result of the above dataset
815   // This will trigger the creation of the Execution Tree and launch it.
816   std::shared_ptr<Iterator> iter = ds->CreateIterator();
817   EXPECT_NE(iter, nullptr);
818 
819   // Iterate the dataset and get each row
820   std::unordered_map<std::string, mindspore::MSTensor> row;
821   ASSERT_OK(iter->GetNextRow(&row));
822 
823   // Note: 2**16 -3 = 65536 -3 = 65533
824   std::vector<std::vector<uint16_t>> expected = {
825     {65533, 65533, 65533, 65533}, {65533, 65533, 65533, 65533}, {65533, 65533, 65533, 65533}};
826 
827   uint64_t i = 0;
828   while (row.size() != 0) {
829     auto ind = row["col1"];
830     TEST_MS_LOG_MSTENSOR(INFO, "ind: ", ind);
831     std::shared_ptr<Tensor> de_expected_tensor;
832     ASSERT_OK(Tensor::CreateFromVector(expected[i], &de_expected_tensor));
833     mindspore::MSTensor expected_tensor =
834       mindspore::MSTensor(std::make_shared<mindspore::dataset::DETensor>(de_expected_tensor));
835     EXPECT_MSTENSOR_EQ(ind, expected_tensor);
836 
837     ASSERT_OK(iter->GetNextRow(&row));
838     i++;
839   }
840 
841   EXPECT_EQ(i, 3);
842 
843   // Manually terminate the pipeline
844   iter->Stop();
845   GlobalContext::config_manager()->set_seed(curr_seed);
846 }
847 
TEST_F(MindDataTestPipeline,TestFillSuccessUpTypecast)848 TEST_F(MindDataTestPipeline, TestFillSuccessUpTypecast) {
849   MS_LOG(INFO) << "Doing MindDataTestPipeline-TestFillSuccessUpTypecast.";
850 
851   // Create a RandomDataset with Float numbers for given shape
852   u_int32_t curr_seed = GlobalContext::config_manager()->seed();
853   GlobalContext::config_manager()->set_seed(963);
854   std::shared_ptr<SchemaObj> schema = Schema();
855   ASSERT_OK(schema->add_column("col1", mindspore::DataType::kNumberTypeFloat32, {2}));
856   std::shared_ptr<Dataset> ds = RandomData((float)4.0, schema);
857   EXPECT_NE(ds, nullptr);
858   ds = ds->SetNumWorkers(2);
859   EXPECT_NE(ds, nullptr);
860 
861   // Create Fill op - to fill with zeroes
862   std::shared_ptr<Tensor> fill_value_tensor;
863   ASSERT_OK(Tensor::CreateScalar(0, &fill_value_tensor));
864   mindspore::MSTensor fill_value_MSTensor =
865     mindspore::MSTensor(std::make_shared<mindspore::dataset::DETensor>(fill_value_tensor));
866   transforms::Fill mask = transforms::Fill(fill_value_MSTensor);
867   ds = ds->Map({mask}, {"col1"});
868   EXPECT_NE(ds, nullptr);
869 
870   // Create an iterator over the result of the above dataset
871   // This will trigger the creation of the Execution Tree and launch it.
872   std::shared_ptr<Iterator> iter = ds->CreateIterator();
873   EXPECT_NE(iter, nullptr);
874 
875   // Iterate the dataset and get each row
876   std::unordered_map<std::string, mindspore::MSTensor> row;
877   ASSERT_OK(iter->GetNextRow(&row));
878 
879   std::vector<std::vector<float_t>> expected = {{0.0, 0.0}, {0.0, 0.0}, {0.0, 0.0}, {0.0, 0.0}};
880 
881   uint64_t i = 0;
882   while (row.size() != 0) {
883     auto ind = row["col1"];
884     TEST_MS_LOG_MSTENSOR(INFO, "ind: ", ind);
885     std::shared_ptr<Tensor> de_expected_tensor;
886     ASSERT_OK(Tensor::CreateFromVector(expected[i], &de_expected_tensor));
887     mindspore::MSTensor expected_tensor =
888       mindspore::MSTensor(std::make_shared<mindspore::dataset::DETensor>(de_expected_tensor));
889     EXPECT_MSTENSOR_EQ(ind, expected_tensor);
890 
891     ASSERT_OK(iter->GetNextRow(&row));
892     i++;
893   }
894 
895   EXPECT_EQ(i, 4);
896 
897   // Manually terminate the pipeline
898   iter->Stop();
899   GlobalContext::config_manager()->set_seed(curr_seed);
900 }
901 
TEST_F(MindDataTestPipeline,TestFillSuccessString)902 TEST_F(MindDataTestPipeline, TestFillSuccessString) {
903   MS_LOG(INFO) << "Doing MindDataTestPipeline-TestFillSuccessString.";
904 
905   // Create a TextFile dataset
906   std::string data_file = datasets_root_path_ + "/testTokenizerData/basic_tokenizer.txt";
907   std::shared_ptr<Dataset> ds = TextFile({data_file}, 0, ShuffleMode::kFalse);
908   EXPECT_NE(ds, nullptr);
909 
910   // Create Skip operation on ds
911   ds = ds->Skip(6);
912   EXPECT_NE(ds, nullptr);
913 
914   // Create BasicTokenizer operation on ds
915   std::shared_ptr<TensorTransform> basic_tokenizer = std::make_shared<text::BasicTokenizer>(true);
916   EXPECT_NE(basic_tokenizer, nullptr);
917 
918   // Create Map operation on ds
919   ds = ds->Map({basic_tokenizer}, {"text"});
920   EXPECT_NE(ds, nullptr);
921 
922   // Create Fill op - to fill with string
923   std::shared_ptr<Tensor> fill_value_tensor;
924   ASSERT_OK(Tensor::CreateScalar<std::string>("Hello", &fill_value_tensor));
925   mindspore::MSTensor fill_value_MSTensor =
926     mindspore::MSTensor(std::make_shared<mindspore::dataset::DETensor>(fill_value_tensor));
927   transforms::Fill mask = transforms::Fill(fill_value_MSTensor);
928   ds = ds->Map({mask}, {"text"});
929   EXPECT_NE(ds, nullptr);
930 
931   // Create an iterator over the result of the above dataset
932   // This will trigger the creation of the Execution Tree and launch it.
933   std::shared_ptr<Iterator> iter = ds->CreateIterator();
934   EXPECT_NE(iter, nullptr);
935 
936   // Iterate the dataset and get each row
937   std::unordered_map<std::string, mindspore::MSTensor> row;
938   ASSERT_OK(iter->GetNextRow(&row));
939 
940   std::vector<std::string> expected = {"Hello", "Hello", "Hello", "Hello", "Hello"};
941   std::shared_ptr<Tensor> de_expected_tensor;
942   ASSERT_OK(Tensor::CreateFromVector(expected, &de_expected_tensor));
943   mindspore::MSTensor expected_tensor =
944     mindspore::MSTensor(std::make_shared<mindspore::dataset::DETensor>(de_expected_tensor));
945 
946   uint64_t i = 0;
947   while (row.size() != 0) {
948     auto ind = row["text"];
949     TEST_MS_LOG_MSTENSOR(INFO, "ind: ", ind);
950     EXPECT_MSTENSOR_EQ(ind, expected_tensor);
951     ASSERT_OK(iter->GetNextRow(&row));
952     i++;
953   }
954 
955   EXPECT_EQ(i, 1);
956 
957   // Manually terminate the pipeline
958   iter->Stop();
959 }
960 
TEST_F(MindDataTestPipeline,TestFillFailFillValueNotScalar)961 TEST_F(MindDataTestPipeline, TestFillFailFillValueNotScalar) {
962   MS_LOG(INFO) << "Doing MindDataTestPipeline-TestFillFailFillValueNotScalar.";
963   // Test BasicTokenizer with lower_case true
964 
965   // Create a TextFile dataset
966   std::string data_file = datasets_root_path_ + "/testTokenizerData/basic_tokenizer.txt";
967   std::shared_ptr<Dataset> ds = TextFile({data_file}, 0, ShuffleMode::kFalse);
968   EXPECT_NE(ds, nullptr);
969 
970   // Create Skip operation on ds
971   ds = ds->Skip(6);
972   EXPECT_NE(ds, nullptr);
973 
974   // Create BasicTokenizer operation on ds
975   std::shared_ptr<TensorTransform> basic_tokenizer = std::make_shared<text::BasicTokenizer>(true);
976   EXPECT_NE(basic_tokenizer, nullptr);
977 
978   // Create Map operation on ds
979   ds = ds->Map({basic_tokenizer}, {"text"});
980   EXPECT_NE(ds, nullptr);
981 
982   // Create Fill op - with wrongful vector shape instead of scalar
983   std::vector<std::string> fill_string = {"ERROR"};
984   std::shared_ptr<Tensor> fill_value_tensor;
985   ASSERT_OK(Tensor::CreateFromVector(fill_string, &fill_value_tensor));
986   mindspore::MSTensor fill_value_MSTensor =
987     mindspore::MSTensor(std::make_shared<mindspore::dataset::DETensor>(fill_value_tensor));
988   transforms::Fill mask = transforms::Fill(fill_value_MSTensor);
989   ds = ds->Map({mask}, {"text"});
990   EXPECT_NE(ds, nullptr);
991 
992   // Create an iterator over the result of the above dataset
993   // This will trigger the creation of the Execution Tree and launch it.
994   std::shared_ptr<Iterator> iter = ds->CreateIterator();
995 
996   // Expect failure: invalid Fill parameter (the shape of fill_value is not a scalar)
997   EXPECT_EQ(iter, nullptr);
998 }
999 
TEST_F(MindDataTestPipeline,TestMaskSuccess1)1000 TEST_F(MindDataTestPipeline, TestMaskSuccess1) {
1001   MS_LOG(INFO) << "Doing MindDataTestPipeline-TestMaskSuccess1.";
1002   // Test Mask random int dataset with int
1003 
1004   // Create a RandomDataset
1005   u_int32_t curr_seed = GlobalContext::config_manager()->seed();
1006   GlobalContext::config_manager()->set_seed(246);
1007   std::shared_ptr<SchemaObj> schema = Schema();
1008   ASSERT_OK(schema->add_column("col1", mindspore::DataType::kNumberTypeInt16, {4}));
1009   std::shared_ptr<Dataset> ds = RandomData(4, schema);
1010   EXPECT_NE(ds, nullptr);
1011   ds = ds->SetNumWorkers(2);
1012   EXPECT_NE(ds, nullptr);
1013 
1014   // Create an int Mask op
1015   std::shared_ptr<Tensor> constant_tensor;
1016   ASSERT_OK(Tensor::CreateScalar(0, &constant_tensor));
1017   mindspore::MSTensor constant_MSTensor =
1018     mindspore::MSTensor(std::make_shared<mindspore::dataset::DETensor>(constant_tensor));
1019   transforms::Mask mask = transforms::Mask(RelationalOp::kGreater, constant_MSTensor);
1020   ds = ds->Map({mask}, {"col1"});
1021   EXPECT_NE(ds, nullptr);
1022 
1023   // Create an iterator over the result of the above dataset
1024   // This will trigger the creation of the Execution Tree and launch it.
1025   std::shared_ptr<Iterator> iter = ds->CreateIterator();
1026   EXPECT_NE(iter, nullptr);
1027 
1028   // Iterate the dataset and get each row
1029   std::unordered_map<std::string, mindspore::MSTensor> row;
1030   ASSERT_OK(iter->GetNextRow(&row));
1031 
1032   std::vector<std::vector<bool>> expected = {
1033     {true, true, true, true}, {false, false, false, false}, {false, false, false, false}, {false, false, false, false}};
1034 
1035   uint64_t i = 0;
1036   while (row.size() != 0) {
1037     auto ind = row["col1"];
1038     std::shared_ptr<Tensor> de_expected_tensor;
1039     ASSERT_OK(Tensor::CreateFromVector(expected[i], &de_expected_tensor));
1040     mindspore::MSTensor expected_tensor =
1041       mindspore::MSTensor(std::make_shared<mindspore::dataset::DETensor>(de_expected_tensor));
1042     EXPECT_MSTENSOR_EQ(ind, expected_tensor);
1043     ASSERT_OK(iter->GetNextRow(&row));
1044     i++;
1045   }
1046 
1047   EXPECT_EQ(i, 4);
1048 
1049   // Manually terminate the pipeline
1050   iter->Stop();
1051   GlobalContext::config_manager()->set_seed(curr_seed);
1052 }
1053 
TEST_F(MindDataTestPipeline,TestMaskSuccess2)1054 TEST_F(MindDataTestPipeline, TestMaskSuccess2) {
1055   MS_LOG(INFO) << "Doing MindDataTestPipeline-TestMaskSuccess2.";
1056   // Test Mask random float dataset with float
1057 
1058   // Create a RandomDataset
1059   u_int32_t curr_seed = GlobalContext::config_manager()->seed();
1060   GlobalContext::config_manager()->set_seed(246);
1061   std::shared_ptr<SchemaObj> schema = Schema();
1062   ASSERT_OK(schema->add_column("col1", mindspore::DataType::kNumberTypeFloat16, {4}));
1063   std::shared_ptr<Dataset> ds = RandomData(4, schema);
1064   EXPECT_NE(ds, nullptr);
1065   ds = ds->SetNumWorkers(2);
1066   EXPECT_NE(ds, nullptr);
1067 
1068   // Create a float Mask op
1069   std::shared_ptr<Tensor> constant_tensor;
1070   ASSERT_OK(Tensor::CreateScalar(-1.1, &constant_tensor));
1071   mindspore::MSTensor constant_MSTensor =
1072     mindspore::MSTensor(std::make_shared<mindspore::dataset::DETensor>(constant_tensor));
1073   // Use explicit input ms_type(kNumberTypeBool) as the mask return type
1074   transforms::Mask mask =
1075     transforms::Mask(RelationalOp::kLessEqual, constant_MSTensor, mindspore::DataType::kNumberTypeBool);
1076   ds = ds->Map({mask}, {"col1"});
1077   EXPECT_NE(ds, nullptr);
1078 
1079   // Create an iterator over the result of the above dataset
1080   // This will trigger the creation of the Execution Tree and launch it.
1081   std::shared_ptr<Iterator> iter = ds->CreateIterator();
1082   EXPECT_NE(iter, nullptr);
1083 
1084   // Iterate the dataset and get each row
1085   std::unordered_map<std::string, mindspore::MSTensor> row;
1086   ASSERT_OK(iter->GetNextRow(&row));
1087 
1088   std::vector<std::vector<bool>> expected = {
1089     {false, false, false, false}, {true, true, true, true}, {false, false, false, false}, {true, true, true, true}};
1090 
1091   uint64_t i = 0;
1092   while (row.size() != 0) {
1093     auto ind = row["col1"];
1094     std::shared_ptr<Tensor> de_expected_tensor;
1095     ASSERT_OK(Tensor::CreateFromVector(expected[i], &de_expected_tensor));
1096     mindspore::MSTensor expected_tensor =
1097       mindspore::MSTensor(std::make_shared<mindspore::dataset::DETensor>(de_expected_tensor));
1098     EXPECT_MSTENSOR_EQ(ind, expected_tensor);
1099     ASSERT_OK(iter->GetNextRow(&row));
1100     i++;
1101   }
1102 
1103   EXPECT_EQ(i, 4);
1104 
1105   // Manually terminate the pipeline
1106   iter->Stop();
1107 
1108   // Test Mask result boolean dataset with boolean
1109 
1110   // Create another boolean Mask op
1111   std::shared_ptr<Tensor> constant_tensor2;
1112   ASSERT_OK(Tensor::CreateScalar<bool>(false, &constant_tensor2));
1113   mindspore::MSTensor constant_MSTensor2 =
1114     mindspore::MSTensor(std::make_shared<mindspore::dataset::DETensor>(constant_tensor2));
1115   transforms::Mask mask2 = transforms::Mask(RelationalOp::kLessEqual, constant_MSTensor2);
1116   ds = ds->Map({mask2}, {"col1"});
1117   EXPECT_NE(ds, nullptr);
1118 
1119   // Create an iterator over the result of the above dataset
1120   // This will trigger the creation of the Execution Tree and launch it.
1121   iter = ds->CreateIterator();
1122   EXPECT_NE(iter, nullptr);
1123 
1124   // Iterate the dataset and get each row
1125   ASSERT_OK(iter->GetNextRow(&row));
1126 
1127   std::vector<std::vector<bool>> expected2 = {
1128     {true, true, true, true}, {false, false, false, false}, {true, true, true, true}, {false, false, false, false}};
1129 
1130   i = 0;
1131   while (row.size() != 0) {
1132     auto ind2 = row["col1"];
1133     std::shared_ptr<Tensor> de_expected_tensor2;
1134     ASSERT_OK(Tensor::CreateFromVector(expected2[i], &de_expected_tensor2));
1135     mindspore::MSTensor expected_tensor2 =
1136       mindspore::MSTensor(std::make_shared<mindspore::dataset::DETensor>(de_expected_tensor2));
1137     EXPECT_MSTENSOR_EQ(ind2, expected_tensor2);
1138     ASSERT_OK(iter->GetNextRow(&row));
1139     i++;
1140   }
1141 
1142   EXPECT_EQ(i, 4);
1143 
1144   // Manually terminate the pipeline
1145   iter->Stop();
1146 
1147   GlobalContext::config_manager()->set_seed(curr_seed);
1148 }
1149 
TEST_F(MindDataTestPipeline,TestMaskSuccess3)1150 TEST_F(MindDataTestPipeline, TestMaskSuccess3) {
1151   MS_LOG(INFO) << "Doing MindDataTestPipeline-TestMaskSuccess3.";
1152   // Test Mask random text dataset with string
1153 
1154   // Create a TextFile dataset
1155   std::string data_file = datasets_root_path_ + "/testTokenizerData/1.txt";
1156   std::shared_ptr<Dataset> ds = TextFile({data_file}, 0, ShuffleMode::kFalse);
1157   EXPECT_NE(ds, nullptr);
1158 
1159   // Create Take operation on ds
1160   ds = ds->Take(1);
1161   EXPECT_NE(ds, nullptr);
1162 
1163   // Create BasicTokenizer operation on ds
1164   std::shared_ptr<TensorTransform> basic_tokenizer = std::make_shared<text::BasicTokenizer>(true);
1165   EXPECT_NE(basic_tokenizer, nullptr);
1166 
1167   // Create Map operation on ds
1168   ds = ds->Map({basic_tokenizer}, {"text"});
1169   EXPECT_NE(ds, nullptr);
1170 
1171   // Create a string Mask op
1172   std::shared_ptr<Tensor> constant_tensor;
1173   ASSERT_OK(Tensor::CreateScalar<std::string>("to", &constant_tensor));
1174   mindspore::MSTensor constant_MSTensor =
1175     mindspore::MSTensor(std::make_shared<mindspore::dataset::DETensor>(constant_tensor));
1176   // Use kNumberTypeInt16 as an explicit ms_type parameter for the mask return type,
1177   // instead of using default kNumberTypeBool.
1178   transforms::Mask mask =
1179     transforms::Mask(RelationalOp::kEqual, constant_MSTensor, mindspore::DataType::kNumberTypeInt16);
1180   ds = ds->Map({mask}, {"text"});
1181   EXPECT_NE(ds, nullptr);
1182 
1183   // Create an iterator over the result of the above dataset
1184   // This will trigger the creation of the Execution Tree and launch it.
1185   std::shared_ptr<Iterator> iter = ds->CreateIterator();
1186   EXPECT_NE(iter, nullptr);
1187 
1188   // Iterate the dataset and get each row
1189   std::unordered_map<std::string, mindspore::MSTensor> row;
1190   ASSERT_OK(iter->GetNextRow(&row));
1191 
1192   std::vector<std::vector<int16_t>> expected = {{0, 1, 0, 0}};
1193 
1194   uint64_t i = 0;
1195   while (row.size() != 0) {
1196     auto ind = row["text"];
1197     std::shared_ptr<Tensor> de_expected_tensor;
1198     ASSERT_OK(Tensor::CreateFromVector(expected[i], &de_expected_tensor));
1199     mindspore::MSTensor expected_tensor =
1200       mindspore::MSTensor(std::make_shared<mindspore::dataset::DETensor>(de_expected_tensor));
1201     EXPECT_MSTENSOR_EQ(ind, expected_tensor);
1202     ASSERT_OK(iter->GetNextRow(&row));
1203     i++;
1204   }
1205 
1206   EXPECT_EQ(i, 1);
1207 
1208   // Manually terminate the pipeline
1209   iter->Stop();
1210 }
1211 
TEST_F(MindDataTestPipeline,TestMaskFail1)1212 TEST_F(MindDataTestPipeline, TestMaskFail1) {
1213   MS_LOG(INFO) << "Doing MindDataTestPipeline-TestMaskFail1.";
1214   // Test Mask with nun-numeric datatype as output result.
1215 
1216   // Create a RandomDataset
1217   u_int32_t curr_seed = GlobalContext::config_manager()->seed();
1218   GlobalContext::config_manager()->set_seed(246);
1219   std::shared_ptr<SchemaObj> schema = Schema();
1220   ASSERT_OK(schema->add_column("col1", mindspore::DataType::kNumberTypeInt16, {4}));
1221   std::shared_ptr<Dataset> ds = RandomData(1, schema);
1222   EXPECT_NE(ds, nullptr);
1223   ds = ds->SetNumWorkers(1);
1224   EXPECT_NE(ds, nullptr);
1225 
1226   // Create an int Mask op
1227   std::shared_ptr<Tensor> constant_tensor;
1228   ASSERT_OK(Tensor::CreateScalar(0, &constant_tensor));
1229   mindspore::MSTensor constant_MSTensor =
1230     mindspore::MSTensor(std::make_shared<mindspore::dataset::DETensor>(constant_tensor));
1231   transforms::Mask mask =
1232     transforms::Mask(RelationalOp::kGreater, constant_MSTensor, mindspore::DataType::kObjectTypeString);
1233   ds = ds->Map({mask}, {"col1"});
1234   EXPECT_NE(ds, nullptr);
1235 
1236   // Create an iterator over the result of the above dataset
1237   // This will trigger the creation of the Execution Tree and launch it.
1238   std::shared_ptr<Iterator> iter = ds->CreateIterator();
1239   // Expect failure: using string as output datatype which is invalid
1240   EXPECT_EQ(iter, nullptr);
1241 
1242   GlobalContext::config_manager()->set_seed(curr_seed);
1243 }
1244 
TEST_F(MindDataTestPipeline,TestMaskFail2)1245 TEST_F(MindDataTestPipeline, TestMaskFail2) {
1246   MS_LOG(INFO) << "Doing MindDataTestPipeline-TestMaskFail2.";
1247   // Test Mask with mismatched datatype.
1248 
1249   // Create a RandomDataset
1250   u_int32_t curr_seed = GlobalContext::config_manager()->seed();
1251   GlobalContext::config_manager()->set_seed(246);
1252   std::shared_ptr<SchemaObj> schema = Schema();
1253   ASSERT_OK(schema->add_column("col1", mindspore::DataType::kNumberTypeInt16, {4}));
1254   std::shared_ptr<Dataset> ds = RandomData(1, schema);
1255   EXPECT_NE(ds, nullptr);
1256   ds = ds->SetNumWorkers(1);
1257   EXPECT_NE(ds, nullptr);
1258 
1259   // Create a string Mask op
1260   std::shared_ptr<Tensor> constant_tensor;
1261   ASSERT_OK(Tensor::CreateScalar<std::string>("0", &constant_tensor));
1262   mindspore::MSTensor constant_MSTensor =
1263     mindspore::MSTensor(std::make_shared<mindspore::dataset::DETensor>(constant_tensor));
1264   transforms::Mask mask = transforms::Mask(RelationalOp::kGreater, constant_MSTensor);
1265   ds = ds->Map({mask}, {"col1"});
1266   EXPECT_NE(ds, nullptr);
1267 
1268   // Create an iterator over the result of the above dataset
1269   // This will trigger the creation of the Execution Tree and launch it.
1270   std::shared_ptr<Iterator> iter = ds->CreateIterator();
1271   EXPECT_NE(iter, nullptr);
1272 
1273   // Iterate the dataset and get each row
1274   std::unordered_map<std::string, mindspore::MSTensor> row;
1275   // Expect failure: mismatched datatype, mask Int16 with string
1276   EXPECT_ERROR(iter->GetNextRow(&row));
1277 
1278   // Manually terminate the pipeline
1279   iter->Stop();
1280   GlobalContext::config_manager()->set_seed(curr_seed);
1281 }
1282 
TEST_F(MindDataTestPipeline,TestOneHotSuccess1)1283 TEST_F(MindDataTestPipeline, TestOneHotSuccess1) {
1284   MS_LOG(INFO) << "Doing MindDataTestPipeline-TestOneHotSuccess1.";
1285   // Testing CutMixBatch on a batch of CHW images
1286   // Create a Cifar10 Dataset
1287   std::string folder_path = datasets_root_path_ + "/testCifar10Data/";
1288   int number_of_classes = 10;
1289   std::shared_ptr<Dataset> ds = Cifar10(folder_path, "all", std::make_shared<RandomSampler>(false, 10));
1290   EXPECT_NE(ds, nullptr);
1291 
1292   // Create objects for the tensor ops
1293   std::shared_ptr<TensorTransform> hwc_to_chw = std::make_shared<vision::HWC2CHW>();
1294 
1295   // Create a Map operation on ds
1296   ds = ds->Map({hwc_to_chw}, {"image"});
1297   EXPECT_NE(ds, nullptr);
1298 
1299   // Create a Batch operation on ds
1300   int32_t batch_size = 5;
1301   ds = ds->Batch(batch_size);
1302   EXPECT_NE(ds, nullptr);
1303 
1304   // Create objects for the tensor ops
1305   std::shared_ptr<TensorTransform> one_hot_op = std::make_shared<transforms::OneHot>(number_of_classes);
1306 
1307   // Create a Map operation on ds
1308   ds = ds->Map({one_hot_op}, {"label"});
1309   EXPECT_NE(ds, nullptr);
1310 
1311   std::shared_ptr<TensorTransform> cutmix_batch_op =
1312     std::make_shared<vision::CutMixBatch>(mindspore::dataset::ImageBatchFormat::kNCHW, 1.0, 1.0);
1313 
1314   // Create a Map operation on ds
1315   ds = ds->Map({cutmix_batch_op}, {"image", "label"});
1316   EXPECT_NE(ds, nullptr);
1317 
1318   // Create an iterator over the result of the above dataset
1319   // This will trigger the creation of the Execution Tree and launch it.
1320   std::shared_ptr<Iterator> iter = ds->CreateIterator();
1321   EXPECT_NE(iter, nullptr);
1322 
1323   // Iterate the dataset and get each row
1324   std::unordered_map<std::string, mindspore::MSTensor> row;
1325   ASSERT_OK(iter->GetNextRow(&row));
1326 
1327   uint64_t i = 0;
1328   while (row.size() != 0) {
1329     i++;
1330     auto image = row["image"];
1331     auto label = row["label"];
1332     MS_LOG(INFO) << "Tensor image shape: " << image.Shape();
1333     MS_LOG(INFO) << "Label shape: " << label.Shape();
1334     EXPECT_EQ(image.Shape().size() == 4 && batch_size == image.Shape()[0] && 3 == image.Shape()[1] &&
1335                 32 == image.Shape()[2] && 32 == image.Shape()[3],
1336               true);
1337     EXPECT_EQ(label.Shape().size() == 2 && batch_size == label.Shape()[0] && number_of_classes == label.Shape()[1],
1338               true);
1339     ASSERT_OK(iter->GetNextRow(&row));
1340   }
1341 
1342   EXPECT_EQ(i, 2);
1343 
1344   // Manually terminate the pipeline
1345   iter->Stop();
1346 }
1347 
TEST_F(MindDataTestPipeline,TestOneHotSuccess2)1348 TEST_F(MindDataTestPipeline, TestOneHotSuccess2) {
1349   MS_LOG(INFO) << "Doing MindDataTestPipeline-TestOneHotSuccess2.";
1350   // Create a Cifar10 Dataset
1351   std::string folder_path = datasets_root_path_ + "/testCifar10Data/";
1352   std::shared_ptr<Dataset> ds = Cifar10(folder_path, "all", std::make_shared<RandomSampler>(false, 10));
1353   EXPECT_NE(ds, nullptr);
1354 
1355   // Create a Batch operation on ds
1356   int32_t batch_size = 5;
1357   ds = ds->Batch(batch_size);
1358   EXPECT_NE(ds, nullptr);
1359 
1360   // Create objects for the tensor ops
1361   std::shared_ptr<TensorTransform> one_hot_op = std::make_shared<transforms::OneHot>(10);
1362 
1363   // Create a Map operation on ds
1364   ds = ds->Map({one_hot_op}, {"label"});
1365   EXPECT_NE(ds, nullptr);
1366 
1367   std::shared_ptr<TensorTransform> mixup_batch_op = std::make_shared<vision::MixUpBatch>(2.0);
1368 
1369   // Create a Map operation on ds
1370   ds = ds->Map({mixup_batch_op}, {"image", "label"});
1371   EXPECT_NE(ds, nullptr);
1372 
1373   // Create an iterator over the result of the above dataset
1374   // This will trigger the creation of the Execution Tree and launch it.
1375   std::shared_ptr<Iterator> iter = ds->CreateIterator();
1376   EXPECT_NE(iter, nullptr);
1377 
1378   // Iterate the dataset and get each row
1379   std::unordered_map<std::string, mindspore::MSTensor> row;
1380   ASSERT_OK(iter->GetNextRow(&row));
1381 
1382   uint64_t i = 0;
1383   while (row.size() != 0) {
1384     i++;
1385     auto image = row["image"];
1386     MS_LOG(INFO) << "Tensor image shape: " << image.Shape();
1387     ASSERT_OK(iter->GetNextRow(&row));
1388   }
1389 
1390   EXPECT_EQ(i, 2);
1391 
1392   // Manually terminate the pipeline
1393   iter->Stop();
1394 }
1395 
TEST_F(MindDataTestPipeline,TestOneHotFail1)1396 TEST_F(MindDataTestPipeline, TestOneHotFail1) {
1397   MS_LOG(INFO) << "Doing MindDataTestPipeline-TestOneHotFail1 with invalid params.";
1398 
1399   // Create a Cifar10 Dataset
1400   std::string folder_path = datasets_root_path_ + "/testCifar10Data/";
1401   std::shared_ptr<Dataset> ds = Cifar10(folder_path, "all", std::make_shared<RandomSampler>(false, 10));
1402   EXPECT_NE(ds, nullptr);
1403 
1404   // incorrect num_class
1405   std::shared_ptr<TensorTransform> one_hot_op = std::make_shared<transforms::OneHot>(0);
1406 
1407   // Create a Map operation on ds
1408   ds = ds->Map({one_hot_op}, {"label"});
1409   EXPECT_NE(ds, nullptr);
1410 
1411   std::shared_ptr<Iterator> iter = ds->CreateIterator();
1412   // Expect failure: invalid OneHot input
1413   EXPECT_EQ(iter, nullptr);
1414 }
1415 
TEST_F(MindDataTestPipeline,TestOneHotFail2)1416 TEST_F(MindDataTestPipeline, TestOneHotFail2) {
1417   MS_LOG(INFO) << "Doing MindDataTestPipeline-TestOneHotFail2 with invalid params.";
1418 
1419   // Create a Cifar10 Dataset
1420   std::string folder_path = datasets_root_path_ + "/testCifar10Data/";
1421   std::shared_ptr<Dataset> ds = Cifar10(folder_path, "all", std::make_shared<RandomSampler>(false, 10));
1422   EXPECT_NE(ds, nullptr);
1423 
1424   // incorrect num_class
1425   std::shared_ptr<TensorTransform> one_hot_op = std::make_shared<transforms::OneHot>(-5);
1426 
1427   // Create a Map operation on ds
1428   ds = ds->Map({one_hot_op}, {"label"});
1429   EXPECT_NE(ds, nullptr);
1430 
1431   std::shared_ptr<Iterator> iter = ds->CreateIterator();
1432   // Expect failure: invalid OneHot input
1433   EXPECT_EQ(iter, nullptr);
1434 }
1435 
TEST_F(MindDataTestPipeline,TestPadEndSuccess1)1436 TEST_F(MindDataTestPipeline, TestPadEndSuccess1) {
1437   MS_LOG(INFO) << "Doing MindDataTestPipeline-TestPadEndSuccess1.";
1438   // Test PadEnd basic with int as pad_value
1439 
1440   // Create a RandomDataset
1441   u_int32_t curr_seed = GlobalContext::config_manager()->seed();
1442   GlobalContext::config_manager()->set_seed(246);
1443   std::shared_ptr<SchemaObj> schema = Schema();
1444   ASSERT_OK(schema->add_column("col1", mindspore::DataType::kNumberTypeInt16, {1}));
1445   std::shared_ptr<Dataset> ds = RandomData(4, schema);
1446   EXPECT_NE(ds, nullptr);
1447   ds = ds->SetNumWorkers(2);
1448   EXPECT_NE(ds, nullptr);
1449 
1450   // Create PadEnd op
1451   std::shared_ptr<Tensor> pad_value;
1452   ASSERT_OK(Tensor::CreateScalar(0, &pad_value));
1453   mindspore::MSTensor pad_value_MSTensor =
1454     mindspore::MSTensor(std::make_shared<mindspore::dataset::DETensor>(pad_value));
1455 
1456   transforms::PadEnd pad_end = transforms::PadEnd({3}, pad_value_MSTensor);
1457   ds = ds->Map({pad_end}, {"col1"});
1458   EXPECT_NE(ds, nullptr);
1459 
1460   // Create an iterator over the result of the above dataset
1461   // This will trigger the creation of the Execution Tree and launch it.
1462   std::shared_ptr<Iterator> iter = ds->CreateIterator();
1463   EXPECT_NE(iter, nullptr);
1464 
1465   // Iterate the dataset and get each row
1466   std::unordered_map<std::string, mindspore::MSTensor> row;
1467   ASSERT_OK(iter->GetNextRow(&row));
1468 
1469   std::vector<std::vector<int16_t>> expected = {{31354, 0, 0}, {-5655, 0, 0}, {-17734, 0, 0}, {-17220, 0, 0}};
1470 
1471   uint64_t i = 0;
1472   while (row.size() != 0) {
1473     auto ind = row["col1"];
1474     std::shared_ptr<Tensor> de_expected_tensor;
1475     ASSERT_OK(Tensor::CreateFromVector(expected[i], &de_expected_tensor));
1476     mindspore::MSTensor expected_tensor =
1477       mindspore::MSTensor(std::make_shared<mindspore::dataset::DETensor>(de_expected_tensor));
1478     EXPECT_MSTENSOR_EQ(ind, expected_tensor);
1479     ASSERT_OK(iter->GetNextRow(&row));
1480     i++;
1481   }
1482 
1483   EXPECT_EQ(i, 4);
1484 
1485   // Manually terminate the pipeline
1486   iter->Stop();
1487   GlobalContext::config_manager()->set_seed(curr_seed);
1488 }
1489 
TEST_F(MindDataTestPipeline,TestPadEndSuccess2)1490 TEST_F(MindDataTestPipeline, TestPadEndSuccess2) {
1491   MS_LOG(INFO) << "Doing MindDataTestPipeline-TestPadEndSuccess2.";
1492   // Test PadEnd with pad_shape equals to current shape, nothing padded
1493 
1494   // Create a RandomDataset
1495   u_int32_t curr_seed = GlobalContext::config_manager()->seed();
1496   GlobalContext::config_manager()->set_seed(246);
1497   std::shared_ptr<SchemaObj> schema = Schema();
1498   ASSERT_OK(schema->add_column("col1", mindspore::DataType::kNumberTypeInt16, {2}));
1499   std::shared_ptr<Dataset> ds = RandomData(4, schema);
1500   EXPECT_NE(ds, nullptr);
1501   ds = ds->SetNumWorkers(2);
1502   EXPECT_NE(ds, nullptr);
1503 
1504   // Create PadEnd op
1505   std::shared_ptr<Tensor> pad_value;
1506   ASSERT_OK(Tensor::CreateScalar(0, &pad_value));
1507   mindspore::MSTensor pad_value_MSTensor =
1508     mindspore::MSTensor(std::make_shared<mindspore::dataset::DETensor>(pad_value));
1509 
1510   transforms::PadEnd pad_end = transforms::PadEnd({2}, pad_value_MSTensor);
1511   ds = ds->Map({pad_end}, {"col1"});
1512   EXPECT_NE(ds, nullptr);
1513 
1514   // Create an iterator over the result of the above dataset
1515   // This will trigger the creation of the Execution Tree and launch it.
1516   std::shared_ptr<Iterator> iter = ds->CreateIterator();
1517   EXPECT_NE(iter, nullptr);
1518 
1519   // Iterate the dataset and get each row
1520   std::unordered_map<std::string, mindspore::MSTensor> row;
1521   ASSERT_OK(iter->GetNextRow(&row));
1522 
1523   std::vector<std::vector<int16_t>> expected = {{31354, 31354}, {-5655, -5655}, {-17734, -17734}, {-17220, -17220}};
1524 
1525   uint64_t i = 0;
1526   while (row.size() != 0) {
1527     auto ind = row["col1"];
1528     std::shared_ptr<Tensor> de_expected_tensor;
1529     ASSERT_OK(Tensor::CreateFromVector(expected[i], &de_expected_tensor));
1530     mindspore::MSTensor expected_tensor =
1531       mindspore::MSTensor(std::make_shared<mindspore::dataset::DETensor>(de_expected_tensor));
1532     EXPECT_MSTENSOR_EQ(ind, expected_tensor);
1533     ASSERT_OK(iter->GetNextRow(&row));
1534     i++;
1535   }
1536 
1537   EXPECT_EQ(i, 4);
1538 
1539   // Manually terminate the pipeline
1540   iter->Stop();
1541   GlobalContext::config_manager()->set_seed(curr_seed);
1542 }
1543 
TEST_F(MindDataTestPipeline,TestPadEndSuccess3)1544 TEST_F(MindDataTestPipeline, TestPadEndSuccess3) {
1545   MS_LOG(INFO) << "Doing MindDataTestPipeline-TestPadEndSuccess3.";
1546   // Test PadEnd without pad_value (using default pad_value)
1547 
1548   // Create a RandomDataset
1549   u_int32_t curr_seed = GlobalContext::config_manager()->seed();
1550   GlobalContext::config_manager()->set_seed(246);
1551   std::shared_ptr<SchemaObj> schema = Schema();
1552   ASSERT_OK(schema->add_column("col1", mindspore::DataType::kNumberTypeInt16, {1}));
1553   std::shared_ptr<Dataset> ds = RandomData(4, schema);
1554   EXPECT_NE(ds, nullptr);
1555   ds = ds->SetNumWorkers(2);
1556   EXPECT_NE(ds, nullptr);
1557 
1558   // Create PadEnd op
1559   transforms::PadEnd pad_end = transforms::PadEnd({3});
1560   ds = ds->Map({pad_end}, {"col1"});
1561   EXPECT_NE(ds, nullptr);
1562 
1563   // Create an iterator over the result of the above dataset
1564   // This will trigger the creation of the Execution Tree and launch it.
1565   std::shared_ptr<Iterator> iter = ds->CreateIterator();
1566   EXPECT_NE(iter, nullptr);
1567 
1568   // Iterate the dataset and get each row
1569   std::unordered_map<std::string, mindspore::MSTensor> row;
1570   ASSERT_OK(iter->GetNextRow(&row));
1571 
1572   std::vector<std::vector<int16_t>> expected = {{31354, 0, 0}, {-5655, 0, 0}, {-17734, 0, 0}, {-17220, 0, 0}};
1573 
1574   uint64_t i = 0;
1575   while (row.size() != 0) {
1576     auto ind = row["col1"];
1577     std::shared_ptr<Tensor> de_expected_tensor;
1578     ASSERT_OK(Tensor::CreateFromVector(expected[i], &de_expected_tensor));
1579     mindspore::MSTensor expected_tensor =
1580       mindspore::MSTensor(std::make_shared<mindspore::dataset::DETensor>(de_expected_tensor));
1581     EXPECT_MSTENSOR_EQ(ind, expected_tensor);
1582     ASSERT_OK(iter->GetNextRow(&row));
1583     i++;
1584   }
1585 
1586   EXPECT_EQ(i, 4);
1587 
1588   // Manually terminate the pipeline
1589   iter->Stop();
1590   GlobalContext::config_manager()->set_seed(curr_seed);
1591 }
1592 
TEST_F(MindDataTestPipeline,TestPadEndSuccess4)1593 TEST_F(MindDataTestPipeline, TestPadEndSuccess4) {
1594   MS_LOG(INFO) << "Doing MindDataTestPipeline-TestPadEndSuccess4.";
1595   // Test PadEnd with pad_shape less than current shape, will truncate the values
1596 
1597   // Create a RandomDataset
1598   u_int32_t curr_seed = GlobalContext::config_manager()->seed();
1599   GlobalContext::config_manager()->set_seed(246);
1600   std::shared_ptr<SchemaObj> schema = Schema();
1601   ASSERT_OK(schema->add_column("col1", mindspore::DataType::kNumberTypeInt16, {4}));
1602   std::shared_ptr<Dataset> ds = RandomData(4, schema);
1603   EXPECT_NE(ds, nullptr);
1604   ds = ds->SetNumWorkers(2);
1605   EXPECT_NE(ds, nullptr);
1606 
1607   // Create PadEnd op
1608   std::shared_ptr<Tensor> pad_value;
1609   ASSERT_OK(Tensor::CreateScalar(0, &pad_value));
1610   mindspore::MSTensor pad_value_MSTensor =
1611     mindspore::MSTensor(std::make_shared<mindspore::dataset::DETensor>(pad_value));
1612 
1613   transforms::PadEnd pad_end = transforms::PadEnd({2}, pad_value_MSTensor);
1614   ds = ds->Map({pad_end}, {"col1"});
1615   EXPECT_NE(ds, nullptr);
1616 
1617   // Create an iterator over the result of the above dataset
1618   // This will trigger the creation of the Execution Tree and launch it.
1619   std::shared_ptr<Iterator> iter = ds->CreateIterator();
1620   EXPECT_NE(iter, nullptr);
1621 
1622   // Iterate the dataset and get each row
1623   std::unordered_map<std::string, mindspore::MSTensor> row;
1624   ASSERT_OK(iter->GetNextRow(&row));
1625 
1626   std::vector<std::vector<int16_t>> expected = {{31354, 31354}, {-5655, -5655}, {-17734, -17734}, {-17220, -17220}};
1627 
1628   uint64_t i = 0;
1629   while (row.size() != 0) {
1630     auto ind = row["col1"];
1631     std::shared_ptr<Tensor> de_expected_tensor;
1632     ASSERT_OK(Tensor::CreateFromVector(expected[i], &de_expected_tensor));
1633     mindspore::MSTensor expected_tensor =
1634       mindspore::MSTensor(std::make_shared<mindspore::dataset::DETensor>(de_expected_tensor));
1635     EXPECT_MSTENSOR_EQ(ind, expected_tensor);
1636     ASSERT_OK(iter->GetNextRow(&row));
1637     i++;
1638   }
1639 
1640   EXPECT_EQ(i, 4);
1641 
1642   // Manually terminate the pipeline
1643   iter->Stop();
1644   GlobalContext::config_manager()->set_seed(curr_seed);
1645 }
1646 
TEST_F(MindDataTestPipeline,TestPadEndSuccess5)1647 TEST_F(MindDataTestPipeline, TestPadEndSuccess5) {
1648   MS_LOG(INFO) << "Doing MindDataTestPipeline-TestPadEndSuccess5.";
1649   // Test PadEnd with string as pad_value
1650 
1651   // Create a TextFile dataset
1652   std::string data_file = datasets_root_path_ + "/testTokenizerData/1.txt";
1653   std::shared_ptr<Dataset> ds = TextFile({data_file}, 0, ShuffleMode::kFalse);
1654   EXPECT_NE(ds, nullptr);
1655 
1656   // Create Take operation on ds
1657   ds = ds->Take(1);
1658   EXPECT_NE(ds, nullptr);
1659 
1660   // Create BasicTokenizer operation on ds
1661   std::shared_ptr<TensorTransform> basic_tokenizer = std::make_shared<text::BasicTokenizer>(true);
1662   EXPECT_NE(basic_tokenizer, nullptr);
1663 
1664   // Create Map operation on ds
1665   ds = ds->Map({basic_tokenizer}, {"text"});
1666   EXPECT_NE(ds, nullptr);
1667 
1668   // Create PadEnd op
1669   std::shared_ptr<Tensor> pad_value;
1670   ASSERT_OK(Tensor::CreateScalar<std::string>("pad_string", &pad_value));
1671   mindspore::MSTensor pad_value_MSTensor =
1672     mindspore::MSTensor(std::make_shared<mindspore::dataset::DETensor>(pad_value));
1673 
1674   transforms::PadEnd pad_end = transforms::PadEnd({5}, pad_value_MSTensor);
1675   ds = ds->Map({pad_end}, {"text"});
1676   EXPECT_NE(ds, nullptr);
1677 
1678   // Create an iterator over the result of the above dataset
1679   // This will trigger the creation of the Execution Tree and launch it.
1680   std::shared_ptr<Iterator> iter = ds->CreateIterator();
1681   EXPECT_NE(iter, nullptr);
1682 
1683   // Iterate the dataset and get each row
1684   std::unordered_map<std::string, mindspore::MSTensor> row;
1685   ASSERT_OK(iter->GetNextRow(&row));
1686 
1687   std::vector<std::vector<std::string>> expected = {{"welcome", "to", "beijing", "!", "pad_string"}};
1688 
1689   uint64_t i = 0;
1690   while (row.size() != 0) {
1691     auto ind = row["text"];
1692     std::shared_ptr<Tensor> de_expected_tensor;
1693     ASSERT_OK(Tensor::CreateFromVector(expected[i], &de_expected_tensor));
1694     mindspore::MSTensor expected_tensor =
1695       mindspore::MSTensor(std::make_shared<mindspore::dataset::DETensor>(de_expected_tensor));
1696     EXPECT_MSTENSOR_EQ(ind, expected_tensor);
1697     ASSERT_OK(iter->GetNextRow(&row));
1698     i++;
1699   }
1700 
1701   EXPECT_EQ(i, 1);
1702 
1703   // Manually terminate the pipeline
1704   iter->Stop();
1705 }
1706 
TEST_F(MindDataTestPipeline,TestPadEndFail)1707 TEST_F(MindDataTestPipeline, TestPadEndFail) {
1708   MS_LOG(INFO) << "Doing MindDataTestPipeline-TestPadEndFail.";
1709   // Test PadEnd with type mismatch, source and pad_value are not of the same type.
1710 
1711   // Create a RandomDataset
1712   u_int32_t curr_seed = GlobalContext::config_manager()->seed();
1713   GlobalContext::config_manager()->set_seed(246);
1714   std::shared_ptr<SchemaObj> schema = Schema();
1715   ASSERT_OK(schema->add_column("col1", mindspore::DataType::kNumberTypeInt16, {1}));
1716   std::shared_ptr<Dataset> ds = RandomData(1, schema);
1717   EXPECT_NE(ds, nullptr);
1718   ds = ds->SetNumWorkers(1);
1719   EXPECT_NE(ds, nullptr);
1720 
1721   // Create PadEnd op
1722   std::shared_ptr<Tensor> pad_value;
1723   ASSERT_OK(Tensor::CreateScalar<std::string>("0", &pad_value));
1724   mindspore::MSTensor pad_value_MSTensor =
1725     mindspore::MSTensor(std::make_shared<mindspore::dataset::DETensor>(pad_value));
1726 
1727   transforms::PadEnd pad_end = transforms::PadEnd({3}, pad_value_MSTensor);
1728   ds = ds->Map({pad_end}, {"col1"});
1729   EXPECT_NE(ds, nullptr);
1730 
1731   // Create an iterator over the result of the above dataset
1732   // This will trigger the creation of the Execution Tree and launch it.
1733   std::shared_ptr<Iterator> iter = ds->CreateIterator();
1734   EXPECT_NE(iter, nullptr);
1735 
1736   // Iterate the dataset and get each row
1737   std::unordered_map<std::string, mindspore::MSTensor> row;
1738   // Expect failure: type mismatch, pad a string to Int16 dataset
1739   EXPECT_ERROR(iter->GetNextRow(&row));
1740 
1741   // Manually terminate the pipeline
1742   iter->Stop();
1743   GlobalContext::config_manager()->set_seed(curr_seed);
1744 }
1745 
TEST_F(MindDataTestPipeline,TestRandomApplySuccess)1746 TEST_F(MindDataTestPipeline, TestRandomApplySuccess) {
1747   MS_LOG(INFO) << "Doing MindDataTestPipeline-TestRandomApplySuccess.";
1748 
1749   // Create an ImageFolder Dataset
1750   std::string folder_path = datasets_root_path_ + "/testPK/data/";
1751   std::shared_ptr<Dataset> ds = ImageFolder(folder_path, true, std::make_shared<RandomSampler>(false, 5));
1752   EXPECT_NE(ds, nullptr);
1753 
1754   // Create objects for the tensor ops
1755   auto resize_op = vision::Resize({777, 777});
1756   auto random_apply = transforms::RandomApply({resize_op}, 0.8);
1757 
1758   // Create a Map operation on ds
1759   ds = ds->Map({random_apply}, {"image"});
1760   EXPECT_NE(ds, nullptr);
1761 
1762   // Create an iterator over the result of the above dataset
1763   // This will trigger the creation of the Execution Tree and launch it.
1764   std::shared_ptr<Iterator> iter = ds->CreateIterator();
1765   EXPECT_NE(iter, nullptr);
1766 
1767   // Iterate the dataset and get each row
1768   std::unordered_map<std::string, mindspore::MSTensor> row;
1769   ASSERT_OK(iter->GetNextRow(&row));
1770 
1771   uint64_t i = 0;
1772   while (row.size() != 0) {
1773     i++;
1774     auto image = row["image"];
1775     auto label = row["label"];
1776     MS_LOG(INFO) << "Tensor image shape: " << image.Shape();
1777     MS_LOG(INFO) << "Label shape: " << label.Shape();
1778     ASSERT_OK(iter->GetNextRow(&row));
1779   }
1780 
1781   EXPECT_EQ(i, 5);
1782 
1783   // Manually terminate the pipeline
1784   iter->Stop();
1785 }
1786 
TEST_F(MindDataTestPipeline,TestRandomApplyFail1)1787 TEST_F(MindDataTestPipeline, TestRandomApplyFail1) {
1788   MS_LOG(INFO) << "Doing MindDataTestPipeline-TestRandomApplyFail1 with invalid transform.";
1789 
1790   // Create a Cifar10 Dataset
1791   std::string folder_path = datasets_root_path_ + "/testCifar10Data/";
1792   std::shared_ptr<Dataset> ds = Cifar10(folder_path, "all", std::make_shared<RandomSampler>(false, 10));
1793   EXPECT_NE(ds, nullptr);
1794 
1795   // Resize: Non-positive size value: -1 at element: 0
1796   // RandomApply: transform ops must not be null
1797   auto decode_op = vision::Decode();
1798   auto resize_op = vision::Resize({-1});
1799   auto random_apply = transforms::RandomApply({decode_op, resize_op});
1800 
1801   // Create a Map operation on ds
1802   ds = ds->Map({random_apply}, {"image"});
1803   EXPECT_NE(ds, nullptr);
1804 
1805   std::shared_ptr<Iterator> iter = ds->CreateIterator();
1806   // Expect failure: invalid RandomApply parameter (transform ops must not be null)
1807   EXPECT_EQ(iter, nullptr);
1808 }
1809 
TEST_F(MindDataTestPipeline,TestRandomApplyFail2)1810 TEST_F(MindDataTestPipeline, TestRandomApplyFail2) {
1811   MS_LOG(INFO) << "Doing MindDataTestPipeline-TestRandomApplyFail2 with invalid transform.";
1812 
1813   // Create a Cifar10 Dataset
1814   std::string folder_path = datasets_root_path_ + "/testCifar10Data/";
1815   std::shared_ptr<Dataset> ds = Cifar10(folder_path, "all", std::make_shared<RandomSampler>(false, 10));
1816   EXPECT_NE(ds, nullptr);
1817 
1818   // RandomApply: transform ops must not be null
1819   std::shared_ptr<TensorTransform> decode_op = std::make_shared<vision::Decode>();
1820   std::shared_ptr<TensorTransform> random_apply(new transforms::RandomApply({decode_op, nullptr}));
1821 
1822   // Create a Map operation on ds
1823   ds = ds->Map({random_apply}, {"image"});
1824   EXPECT_NE(ds, nullptr);
1825 
1826   std::shared_ptr<Iterator> iter = ds->CreateIterator();
1827   // Expect failure: invalid RandomApply parameter (transform ops must not be null)
1828   EXPECT_EQ(iter, nullptr);
1829 }
1830 
TEST_F(MindDataTestPipeline,TestRandomApplyFail3)1831 TEST_F(MindDataTestPipeline, TestRandomApplyFail3) {
1832   MS_LOG(INFO) << "Doing MindDataTestPipeline-TestRandomApplyFail3 with invalid transform.";
1833 
1834   // Create a Cifar10 Dataset
1835   std::string folder_path = datasets_root_path_ + "/testCifar10Data/";
1836   std::shared_ptr<Dataset> ds = Cifar10(folder_path, "all", std::make_shared<RandomSampler>(false, 10));
1837   EXPECT_NE(ds, nullptr);
1838 
1839   // RandomApply: Probability has to be between 0 and 1
1840   auto resize_op = vision::Resize({100});
1841   auto random_apply = transforms::RandomApply({resize_op}, -1);
1842 
1843   // Create a Map operation on ds
1844   ds = ds->Map({random_apply}, {"image"});
1845   EXPECT_NE(ds, nullptr);
1846 
1847   std::shared_ptr<Iterator> iter = ds->CreateIterator();
1848   // Expect failure: invalid RandomApply parameter (Probability has to be between 0 and 1)
1849   EXPECT_EQ(iter, nullptr);
1850 }
1851 
TEST_F(MindDataTestPipeline,TestRandomApplyFail4)1852 TEST_F(MindDataTestPipeline, TestRandomApplyFail4) {
1853   MS_LOG(INFO) << "Doing MindDataTestPipeline-TestRandomApplyFail4 with invalid transform.";
1854 
1855   // Create a Cifar10 Dataset
1856   std::string folder_path = datasets_root_path_ + "/testCifar10Data/";
1857   std::shared_ptr<Dataset> ds = Cifar10(folder_path, "all", std::make_shared<RandomSampler>(false, 10));
1858   EXPECT_NE(ds, nullptr);
1859 
1860   // RandomApply: transform list must not be empty
1861   std::vector<std::shared_ptr<TensorTransform>> list = {};
1862   auto random_apply = transforms::RandomApply(list);
1863 
1864   // Create a Map operation on ds
1865   ds = ds->Map({random_apply}, {"image"});
1866   EXPECT_NE(ds, nullptr);
1867 
1868   std::shared_ptr<Iterator> iter = ds->CreateIterator();
1869   // Expect failure: invalid RandomApply parameter (transform list must not be empty)
1870   EXPECT_EQ(iter, nullptr);
1871 }
1872 
TEST_F(MindDataTestPipeline,TestRandomChoiceSuccess)1873 TEST_F(MindDataTestPipeline, TestRandomChoiceSuccess) {
1874   MS_LOG(INFO) << "Doing MindDataTestPipeline-TestRandomChoiceSuccess.";
1875 
1876   // Create an ImageFolder Dataset
1877   std::string folder_path = datasets_root_path_ + "/testPK/data/";
1878   std::shared_ptr<Dataset> ds = ImageFolder(folder_path, true, std::make_shared<RandomSampler>(false, 3));
1879   EXPECT_NE(ds, nullptr);
1880 
1881   // Create objects for the tensor ops
1882   std::shared_ptr<TensorTransform> resize_op1(new vision::Resize({777, 777}));
1883   std::shared_ptr<TensorTransform> resize_op2(new vision::Resize({888, 888}));
1884   auto random_choice = transforms::RandomChoice({resize_op1, resize_op2});
1885 
1886   // Create a Map operation on ds
1887   ds = ds->Map({random_choice}, {"image"});
1888   EXPECT_NE(ds, nullptr);
1889 
1890   // Create an iterator over the result of the above dataset
1891   // This will trigger the creation of the Execution Tree and launch it.
1892   std::shared_ptr<Iterator> iter = ds->CreateIterator();
1893   EXPECT_NE(iter, nullptr);
1894 
1895   // Iterate the dataset and get each row
1896   std::unordered_map<std::string, mindspore::MSTensor> row;
1897   ASSERT_OK(iter->GetNextRow(&row));
1898 
1899   uint64_t i = 0;
1900   while (row.size() != 0) {
1901     i++;
1902     auto image = row["image"];
1903     auto label = row["label"];
1904     MS_LOG(INFO) << "Tensor image shape: " << image.Shape();
1905     MS_LOG(INFO) << "Label shape: " << label.Shape();
1906     ASSERT_OK(iter->GetNextRow(&row));
1907   }
1908 
1909   EXPECT_EQ(i, 3);
1910 
1911   // Manually terminate the pipeline
1912   iter->Stop();
1913 }
1914 
TEST_F(MindDataTestPipeline,TestRandomChoiceFail1)1915 TEST_F(MindDataTestPipeline, TestRandomChoiceFail1) {
1916   MS_LOG(INFO) << "Doing MindDataTestPipeline-TestRandomChoiceFail1 with invalid transform.";
1917 
1918   // Create a Cifar10 Dataset
1919   std::string folder_path = datasets_root_path_ + "/testCifar10Data/";
1920   RandomSampler sampler = RandomSampler(false, 10);
1921   std::shared_ptr<Dataset> ds = Cifar10(folder_path, "all", sampler);
1922   EXPECT_NE(ds, nullptr);
1923 
1924   // Resize: Non-positive size value: -1 at element: 0
1925   // RandomChoice: transform ops must not be null
1926   auto decode_op = vision::Decode();
1927   auto resize_op = vision::Resize({-1});
1928   auto random_choice = transforms::RandomChoice({decode_op, resize_op});
1929 
1930   // Create a Map operation on ds
1931   ds = ds->Map({random_choice}, {"image"});
1932   EXPECT_NE(ds, nullptr);
1933 
1934   std::shared_ptr<Iterator> iter = ds->CreateIterator();
1935   // Expect failure: invalid RandomApply parameter (transform ops must not be null)
1936   EXPECT_EQ(iter, nullptr);
1937 }
1938 
TEST_F(MindDataTestPipeline,TestRandomChoiceFail2)1939 TEST_F(MindDataTestPipeline, TestRandomChoiceFail2) {
1940   MS_LOG(INFO) << "Doing MindDataTestPipeline-TestRandomChoiceFail2 with invalid transform.";
1941 
1942   // Create a Cifar10 Dataset
1943   std::string folder_path = datasets_root_path_ + "/testCifar10Data/";
1944   std::shared_ptr<Dataset> ds = Cifar10(folder_path, "all", std::make_shared<RandomSampler>(false, 10));
1945   EXPECT_NE(ds, nullptr);
1946 
1947   // RandomChoice: transform ops must not be null
1948   std::shared_ptr<TensorTransform> decode_op = std::make_shared<vision::Decode>();
1949   std::shared_ptr<TensorTransform> random_choice(new transforms::RandomApply({decode_op, nullptr}));
1950 
1951   // Create a Map operation on ds
1952   ds = ds->Map({random_choice}, {"image"});
1953   EXPECT_NE(ds, nullptr);
1954 
1955   std::shared_ptr<Iterator> iter = ds->CreateIterator();
1956   // Expect failure: invalid RandomApply parameter (transform ops must not be null)
1957   EXPECT_EQ(iter, nullptr);
1958 }
1959 
TEST_F(MindDataTestPipeline,TestRandomChoiceFail3)1960 TEST_F(MindDataTestPipeline, TestRandomChoiceFail3) {
1961   MS_LOG(INFO) << "Doing MindDataTestPipeline-TestRandomChoiceFail3 with invalid transform.";
1962 
1963   // Create a Cifar10 Dataset
1964   std::string folder_path = datasets_root_path_ + "/testCifar10Data/";
1965   std::shared_ptr<Dataset> ds = Cifar10(folder_path, "all", std::make_shared<RandomSampler>(false, 10));
1966   EXPECT_NE(ds, nullptr);
1967 
1968   // RandomChoice: transform list must not be empty
1969   std::vector<std::shared_ptr<TensorTransform>> list = {};
1970   auto random_choice = transforms::RandomChoice(list);
1971 
1972   // Create a Map operation on ds
1973   ds = ds->Map({random_choice}, {"image"});
1974   EXPECT_NE(ds, nullptr);
1975 
1976   std::shared_ptr<Iterator> iter = ds->CreateIterator();
1977   // Expect failure: invalid RandomApply parameter (transform list must not be empty)
1978   EXPECT_EQ(iter, nullptr);
1979 }
1980 
TEST_F(MindDataTestPipeline,TestSliceSuccess1)1981 TEST_F(MindDataTestPipeline, TestSliceSuccess1) {
1982   MS_LOG(INFO) << "Doing MindDataTestPipeline-TestSliceSuccess1.";
1983   // Test Slice int with user defined slice object.
1984 
1985   // Create a RandomDataset
1986   u_int32_t curr_seed = GlobalContext::config_manager()->seed();
1987   GlobalContext::config_manager()->set_seed(246);
1988   std::shared_ptr<SchemaObj> schema = Schema();
1989   ASSERT_OK(schema->add_column("col1", mindspore::DataType::kNumberTypeInt16, {1}));
1990   std::shared_ptr<Dataset> ds = RandomData(4, schema);
1991   EXPECT_NE(ds, nullptr);
1992   ds = ds->SetNumWorkers(2);
1993   EXPECT_NE(ds, nullptr);
1994 
1995   // Create concatenate op
1996   std::vector<int16_t> prepend_vector = {1, 2, 3};
1997   std::shared_ptr<Tensor> prepend_tensor;
1998   ASSERT_OK(Tensor::CreateFromVector(prepend_vector, &prepend_tensor));
1999   mindspore::MSTensor prepend_MSTensor =
2000     mindspore::MSTensor(std::make_shared<mindspore::dataset::DETensor>(prepend_tensor));
2001 
2002   transforms::Concatenate concatenate = transforms::Concatenate(0, prepend_MSTensor);
2003 
2004   // Create a Map operation on ds
2005   ds = ds->Map({concatenate}, {"col1"});
2006   EXPECT_NE(ds, nullptr);
2007 
2008   // Apply Slice op on ds, get the first and third elements in each row.
2009   SliceOption slice_option = SliceOption(Slice(0, 3, 2));
2010   transforms::Slice slice = transforms::Slice({slice_option});
2011   ds = ds->Map({slice}, {"col1"});
2012   EXPECT_NE(ds, nullptr);
2013 
2014   // Create an iterator over the result of the above dataset
2015   // This will trigger the creation of the Execution Tree and launch it.
2016   std::shared_ptr<Iterator> iter = ds->CreateIterator();
2017   EXPECT_NE(iter, nullptr);
2018 
2019   // Iterate the dataset and get each row
2020   std::unordered_map<std::string, mindspore::MSTensor> row;
2021   ASSERT_OK(iter->GetNextRow(&row));
2022 
2023   std::vector<std::vector<int16_t>> expected = {{1, 3}, {1, 3}, {1, 3}, {1, 3}};
2024 
2025   // Check slice results
2026   uint64_t i = 0;
2027   while (row.size() != 0) {
2028     auto ind = row["col1"];
2029     std::shared_ptr<Tensor> de_expected_tensor;
2030     ASSERT_OK(Tensor::CreateFromVector(expected[i], &de_expected_tensor));
2031     mindspore::MSTensor expected_tensor =
2032       mindspore::MSTensor(std::make_shared<mindspore::dataset::DETensor>(de_expected_tensor));
2033     EXPECT_MSTENSOR_EQ(ind, expected_tensor);
2034     ASSERT_OK(iter->GetNextRow(&row));
2035     i++;
2036   }
2037 
2038   EXPECT_EQ(i, 4);
2039 
2040   // Manually terminate the pipeline
2041   iter->Stop();
2042   GlobalContext::config_manager()->set_seed(curr_seed);
2043 }
2044 
TEST_F(MindDataTestPipeline,TestSliceSuccess2)2045 TEST_F(MindDataTestPipeline, TestSliceSuccess2) {
2046   MS_LOG(INFO) << "Doing MindDataTestPipeline-TestSliceSuccess2.";
2047   // Test Slice int with bool true (slice all).
2048 
2049   // Create a RandomDataset
2050   u_int32_t curr_seed = GlobalContext::config_manager()->seed();
2051   GlobalContext::config_manager()->set_seed(246);
2052   std::shared_ptr<SchemaObj> schema = Schema();
2053   ASSERT_OK(schema->add_column("col1", mindspore::DataType::kNumberTypeInt16, {1}));
2054   std::shared_ptr<Dataset> ds = RandomData(4, schema);
2055   EXPECT_NE(ds, nullptr);
2056   ds = ds->SetNumWorkers(2);
2057   EXPECT_NE(ds, nullptr);
2058 
2059   // Create concatenate op
2060   std::vector<int16_t> prepend_vector = {1, 2, 3};
2061   std::shared_ptr<Tensor> prepend_tensor;
2062   ASSERT_OK(Tensor::CreateFromVector(prepend_vector, &prepend_tensor));
2063   mindspore::MSTensor prepend_MSTensor =
2064     mindspore::MSTensor(std::make_shared<mindspore::dataset::DETensor>(prepend_tensor));
2065 
2066   transforms::Concatenate concatenate = transforms::Concatenate(0, prepend_MSTensor);
2067 
2068   // Create a Map operation on ds
2069   ds = ds->Map({concatenate}, {"col1"});
2070   EXPECT_NE(ds, nullptr);
2071 
2072   // Apply Slice op on ds, get the first and third elements in each row.
2073   SliceOption slice_option = SliceOption(true);
2074   transforms::Slice slice = transforms::Slice({slice_option});
2075   ds = ds->Map({slice}, {"col1"});
2076   EXPECT_NE(ds, nullptr);
2077 
2078   // Create an iterator over the result of the above dataset
2079   // This will trigger the creation of the Execution Tree and launch it.
2080   std::shared_ptr<Iterator> iter = ds->CreateIterator();
2081   EXPECT_NE(iter, nullptr);
2082 
2083   // Iterate the dataset and get each row
2084   std::unordered_map<std::string, mindspore::MSTensor> row;
2085   ASSERT_OK(iter->GetNextRow(&row));
2086 
2087   std::vector<std::vector<int16_t>> expected = {
2088     {1, 2, 3, 31354}, {1, 2, 3, -5655}, {1, 2, 3, -17734}, {1, 2, 3, -17220}};
2089 
2090   // Check slice results
2091   uint64_t i = 0;
2092   while (row.size() != 0) {
2093     auto ind = row["col1"];
2094     std::shared_ptr<Tensor> de_expected_tensor;
2095     ASSERT_OK(Tensor::CreateFromVector(expected[i], &de_expected_tensor));
2096     mindspore::MSTensor expected_tensor =
2097       mindspore::MSTensor(std::make_shared<mindspore::dataset::DETensor>(de_expected_tensor));
2098     EXPECT_MSTENSOR_EQ(ind, expected_tensor);
2099     ASSERT_OK(iter->GetNextRow(&row));
2100     i++;
2101   }
2102 
2103   EXPECT_EQ(i, 4);
2104 
2105   // Manually terminate the pipeline
2106   iter->Stop();
2107   GlobalContext::config_manager()->set_seed(curr_seed);
2108 }
2109 
TEST_F(MindDataTestPipeline,TestSliceSuccess3)2110 TEST_F(MindDataTestPipeline, TestSliceSuccess3) {
2111   MS_LOG(INFO) << "Doing MindDataTestPipeline-TestSliceSuccess3.";
2112   // Test Slice int with list of indices including negative.
2113 
2114   // Create a RandomDataset
2115   u_int32_t curr_seed = GlobalContext::config_manager()->seed();
2116   GlobalContext::config_manager()->set_seed(246);
2117   std::shared_ptr<SchemaObj> schema = Schema();
2118   ASSERT_OK(schema->add_column("col1", mindspore::DataType::kNumberTypeInt16, {1}));
2119   std::shared_ptr<Dataset> ds = RandomData(4, schema);
2120   EXPECT_NE(ds, nullptr);
2121   ds = ds->SetNumWorkers(2);
2122   EXPECT_NE(ds, nullptr);
2123 
2124   // Create concatenate op
2125   std::vector<int16_t> prepend_vector = {1, 2, 3};
2126   std::shared_ptr<Tensor> prepend_tensor;
2127   ASSERT_OK(Tensor::CreateFromVector(prepend_vector, &prepend_tensor));
2128   mindspore::MSTensor prepend_MSTensor =
2129     mindspore::MSTensor(std::make_shared<mindspore::dataset::DETensor>(prepend_tensor));
2130 
2131   transforms::Concatenate concatenate = transforms::Concatenate(0, prepend_MSTensor);
2132 
2133   // Create a Map operation on ds
2134   ds = ds->Map({concatenate}, {"col1"});
2135   EXPECT_NE(ds, nullptr);
2136 
2137   // Apply Slice op on ds, get the first and third elements in each row.
2138   std::vector<dsize_t> indices = {-1, 2};
2139   SliceOption slice_option = SliceOption(indices);
2140   transforms::Slice slice = transforms::Slice({slice_option});
2141   ds = ds->Map({slice}, {"col1"});
2142   EXPECT_NE(ds, nullptr);
2143 
2144   // Create an iterator over the result of the above dataset
2145   // This will trigger the creation of the Execution Tree and launch it.
2146   std::shared_ptr<Iterator> iter = ds->CreateIterator();
2147   EXPECT_NE(iter, nullptr);
2148 
2149   // Iterate the dataset and get each row
2150   std::unordered_map<std::string, mindspore::MSTensor> row;
2151   ASSERT_OK(iter->GetNextRow(&row));
2152 
2153   std::vector<std::vector<int16_t>> expected = {{31354, 3}, {-5655, 3}, {-17734, 3}, {-17220, 3}};
2154 
2155   // Check slice results
2156   uint64_t i = 0;
2157   while (row.size() != 0) {
2158     auto ind = row["col1"];
2159     std::shared_ptr<Tensor> de_expected_tensor;
2160     ASSERT_OK(Tensor::CreateFromVector(expected[i], &de_expected_tensor));
2161     mindspore::MSTensor expected_tensor =
2162       mindspore::MSTensor(std::make_shared<mindspore::dataset::DETensor>(de_expected_tensor));
2163     EXPECT_MSTENSOR_EQ(ind, expected_tensor);
2164     ASSERT_OK(iter->GetNextRow(&row));
2165     i++;
2166   }
2167 
2168   EXPECT_EQ(i, 4);
2169 
2170   // Manually terminate the pipeline
2171   iter->Stop();
2172   GlobalContext::config_manager()->set_seed(curr_seed);
2173 }
2174 
TEST_F(MindDataTestPipeline,TestSliceSuccess4)2175 TEST_F(MindDataTestPipeline, TestSliceSuccess4) {
2176   MS_LOG(INFO) << "Doing MindDataTestPipeline-TestSliceSuccess4.";
2177   // Test Slice string with list of indices.
2178 
2179   // Create a TextFile dataset
2180   std::string data_file = datasets_root_path_ + "/testTokenizerData/1.txt";
2181   std::shared_ptr<Dataset> ds = TextFile({data_file}, 0, ShuffleMode::kFalse);
2182   EXPECT_NE(ds, nullptr);
2183 
2184   // Create Take operation on ds
2185   ds = ds->Take(1);
2186   EXPECT_NE(ds, nullptr);
2187 
2188   // Create BasicTokenizer operation on ds
2189   std::shared_ptr<TensorTransform> basic_tokenizer = std::make_shared<text::BasicTokenizer>(true);
2190   EXPECT_NE(basic_tokenizer, nullptr);
2191 
2192   // Create Map operation on ds
2193   ds = ds->Map({basic_tokenizer}, {"text"});
2194   EXPECT_NE(ds, nullptr);
2195 
2196   // Apply Slice op on ds, get the first and third elements in each row.
2197   std::vector<dsize_t> indices = {-1, -2, 1, 0};
2198   SliceOption slice_option = SliceOption(indices);
2199   transforms::Slice slice = transforms::Slice({slice_option});
2200   ds = ds->Map({slice}, {"text"});
2201   EXPECT_NE(ds, nullptr);
2202 
2203   // Create an iterator over the result of the above dataset
2204   // This will trigger the creation of the Execution Tree and launch it.
2205   std::shared_ptr<Iterator> iter = ds->CreateIterator();
2206   EXPECT_NE(iter, nullptr);
2207 
2208   // Iterate the dataset and get each row
2209   std::unordered_map<std::string, mindspore::MSTensor> row;
2210   ASSERT_OK(iter->GetNextRow(&row));
2211 
2212   std::vector<std::vector<std::string>> expected = {{"!", "beijing", "to", "welcome"}};
2213 
2214   // Check slice results
2215   uint64_t i = 0;
2216   while (row.size() != 0) {
2217     auto ind = row["text"];
2218     std::shared_ptr<Tensor> de_expected_tensor;
2219     ASSERT_OK(Tensor::CreateFromVector(expected[i], &de_expected_tensor));
2220     mindspore::MSTensor expected_tensor =
2221       mindspore::MSTensor(std::make_shared<mindspore::dataset::DETensor>(de_expected_tensor));
2222     EXPECT_MSTENSOR_EQ(ind, expected_tensor);
2223     ASSERT_OK(iter->GetNextRow(&row));
2224     i++;
2225   }
2226 
2227   EXPECT_EQ(i, 1);
2228 
2229   // Manually terminate the pipeline
2230   iter->Stop();
2231 }
2232 
TEST_F(MindDataTestPipeline,TestSliceSuccess5)2233 TEST_F(MindDataTestPipeline, TestSliceSuccess5) {
2234   MS_LOG(INFO) << "Doing MindDataTestPipeline-TestSliceSuccess5.";
2235   // Test Slice int on multi-dimension.
2236 
2237   // Create a RandomDataset
2238   u_int32_t curr_seed = GlobalContext::config_manager()->seed();
2239   GlobalContext::config_manager()->set_seed(246);
2240   std::shared_ptr<SchemaObj> schema = Schema();
2241   // Generate a ds of 4 tensors, each tensor has 3 rows and 2 columns
2242   ASSERT_OK(schema->add_column("col1", mindspore::DataType::kNumberTypeInt16, {3, 2}));
2243   std::shared_ptr<Dataset> ds = RandomData(4, schema);
2244   EXPECT_NE(ds, nullptr);
2245   ds = ds->SetNumWorkers(2);
2246   EXPECT_NE(ds, nullptr);
2247 
2248   // Apply two SliceOptions on ds which includes 4 tensors with shape 3*2
2249   // The first SliceOption is to slice the first and the second row of each tensor
2250   // The shape of result tensor changes to 2*2
2251   std::vector<dsize_t> indices1 = {0, 1};
2252   SliceOption slice_option1 = SliceOption(indices1);
2253   // The second SliceOption is to slice the last column of each tensor
2254   // The shape of result tensor changes to 2*1
2255   std::vector<dsize_t> indices2 = {-1};
2256   SliceOption slice_option2 = SliceOption(indices2);
2257 
2258   transforms::Slice slice = transforms::Slice({slice_option1, slice_option2});
2259   ds = ds->Map({slice}, {"col1"});
2260   EXPECT_NE(ds, nullptr);
2261 
2262   // Create an iterator over the result of the above dataset
2263   // This will trigger the creation of the Execution Tree and launch it.
2264   std::shared_ptr<Iterator> iter = ds->CreateIterator();
2265   EXPECT_NE(iter, nullptr);
2266 
2267   // Iterate the dataset and get each row
2268   std::unordered_map<std::string, mindspore::MSTensor> row;
2269   ASSERT_OK(iter->GetNextRow(&row));
2270 
2271   std::vector<std::vector<int16_t>> expected = {{31354, 31354}, {-5655, -5655}, {-17734, -17734}, {-17220, -17220}};
2272 
2273   // Check slice results
2274   uint64_t i = 0;
2275   while (row.size() != 0) {
2276     auto ind = row["col1"];
2277     std::shared_ptr<Tensor> de_expected_tensor;
2278     ASSERT_OK(Tensor::CreateFromVector(expected[i], TensorShape({2, 1}), &de_expected_tensor));
2279     mindspore::MSTensor expected_tensor =
2280       mindspore::MSTensor(std::make_shared<mindspore::dataset::DETensor>(de_expected_tensor));
2281     EXPECT_MSTENSOR_EQ(ind, expected_tensor);
2282     ASSERT_OK(iter->GetNextRow(&row));
2283     i++;
2284   }
2285 
2286   EXPECT_EQ(i, 4);
2287 
2288   // Manually terminate the pipeline
2289   iter->Stop();
2290   GlobalContext::config_manager()->set_seed(curr_seed);
2291 }
2292 
TEST_F(MindDataTestPipeline,TestSliceFail1)2293 TEST_F(MindDataTestPipeline, TestSliceFail1) {
2294   MS_LOG(INFO) << "Doing MindDataTestPipeline-TestSliceFail.";
2295   // Test Slice with index out of bounds.
2296 
2297   // Create a RandomDataset
2298   u_int32_t curr_seed = GlobalContext::config_manager()->seed();
2299   GlobalContext::config_manager()->set_seed(246);
2300   std::shared_ptr<SchemaObj> schema = Schema();
2301   ASSERT_OK(schema->add_column("col1", mindspore::DataType::kNumberTypeInt16, {2}));
2302   std::shared_ptr<Dataset> ds = RandomData(1, schema);
2303   EXPECT_NE(ds, nullptr);
2304   ds = ds->SetNumWorkers(1);
2305   EXPECT_NE(ds, nullptr);
2306 
2307   // Apply Slice op on ds, get the first and third elements in each row.
2308   std::vector<dsize_t> indices = {0, 2};  // index 2 is out of bounds
2309   SliceOption slice_option = SliceOption(indices);
2310   transforms::Slice slice = transforms::Slice({slice_option});
2311   ds = ds->Map({slice}, {"col1"});
2312   EXPECT_NE(ds, nullptr);
2313 
2314   // Create an iterator over the result of the above dataset
2315   // This will trigger the creation of the Execution Tree and launch it.
2316   std::shared_ptr<Iterator> iter = ds->CreateIterator();
2317   EXPECT_NE(iter, nullptr);
2318 
2319   // Iterate the dataset and get each row
2320   std::unordered_map<std::string, mindspore::MSTensor> row;
2321   // Expect failure: the index 2 is out of the bounds
2322   EXPECT_ERROR(iter->GetNextRow(&row));
2323 
2324   // Manually terminate the pipeline
2325   iter->Stop();
2326   GlobalContext::config_manager()->set_seed(curr_seed);
2327 }
2328 
TEST_F(MindDataTestPipeline,TestSliceFail2)2329 TEST_F(MindDataTestPipeline, TestSliceFail2) {
2330   MS_LOG(INFO) << "Doing MindDataTestPipeline-TestSliceFail2.";
2331   // Test Slice with false as input SliceOption only (no other index nor slice list provided)
2332 
2333   // Create a RandomDataset
2334   u_int32_t curr_seed = GlobalContext::config_manager()->seed();
2335   GlobalContext::config_manager()->set_seed(246);
2336   std::shared_ptr<SchemaObj> schema = Schema();
2337   ASSERT_OK(schema->add_column("col1", mindspore::DataType::kNumberTypeInt16, {1}));
2338   std::shared_ptr<Dataset> ds = RandomData(1, schema);
2339   EXPECT_NE(ds, nullptr);
2340   ds = ds->SetNumWorkers(1);
2341   EXPECT_NE(ds, nullptr);
2342 
2343   // Create concatenate op
2344   std::vector<int16_t> prepend_vector = {1, 2, 3};
2345   std::shared_ptr<Tensor> prepend_tensor;
2346   ASSERT_OK(Tensor::CreateFromVector(prepend_vector, &prepend_tensor));
2347   mindspore::MSTensor prepend_MSTensor =
2348     mindspore::MSTensor(std::make_shared<mindspore::dataset::DETensor>(prepend_tensor));
2349 
2350   transforms::Concatenate concatenate = transforms::Concatenate(0, prepend_MSTensor);
2351 
2352   // Create a Map operation on ds
2353   ds = ds->Map({concatenate}, {"col1"});
2354   EXPECT_NE(ds, nullptr);
2355 
2356   // Apply Slice op on ds, get the first and third elements in each row.
2357   SliceOption slice_option = SliceOption(false);
2358   transforms::Slice slice = transforms::Slice({slice_option});
2359   ds = ds->Map({slice}, {"col1"});
2360   EXPECT_NE(ds, nullptr);
2361 
2362   // Create an iterator over the result of the above dataset
2363   // This will trigger the creation of the Execution Tree and launch it.
2364   std::shared_ptr<Iterator> iter = ds->CreateIterator();
2365   EXPECT_NE(iter, nullptr);
2366 
2367   // Iterate the dataset and get each row
2368   std::unordered_map<std::string, mindspore::MSTensor> row;
2369   // Expect failure: SliceOption is false and no other index nor slice list provided
2370   EXPECT_ERROR(iter->GetNextRow(&row));
2371 
2372   iter->Stop();
2373   GlobalContext::config_manager()->set_seed(curr_seed);
2374 }
2375 
TEST_F(MindDataTestPipeline,TestTypeCastSuccess)2376 TEST_F(MindDataTestPipeline, TestTypeCastSuccess) {
2377   MS_LOG(INFO) << "Doing MindDataTestPipeline-TestTypeCastSuccess.";
2378 
2379   // Create a Cifar10 Dataset
2380   std::string folder_path = datasets_root_path_ + "/testCifar10Data/";
2381   std::shared_ptr<Dataset> ds = Cifar10(folder_path, "all", std::make_shared<RandomSampler>(false, 1));
2382   EXPECT_NE(ds, nullptr);
2383 
2384   // Create an iterator over the result of the above dataset
2385   // This will trigger the creation of the Execution Tree and launch it.
2386   std::shared_ptr<Iterator> iter = ds->CreateIterator();
2387   EXPECT_NE(iter, nullptr);
2388 
2389   // Iterate the dataset and get each row
2390   std::unordered_map<std::string, mindspore::MSTensor> row;
2391   ASSERT_OK(iter->GetNextRow(&row));
2392 
2393   // Check original data type of dataset
2394   auto image = row["image"];
2395   auto ori_type = image.DataType();
2396   MS_LOG(INFO) << "Original data type id: " << ori_type;
2397   EXPECT_EQ(ori_type, mindspore::DataType(mindspore::TypeId::kNumberTypeUInt8));
2398 
2399   // Manually terminate the pipeline
2400   iter->Stop();
2401 
2402   // Create objects for the tensor ops
2403   std::shared_ptr<TensorTransform> type_cast =
2404     std::make_shared<transforms::TypeCast>(mindspore::DataType::kNumberTypeUInt16);
2405 
2406   // Create a Map operation on ds
2407   std::shared_ptr<Dataset> ds2 = ds->Map({type_cast}, {"image"});
2408   EXPECT_NE(ds2, nullptr);
2409 
2410   // Create an iterator over the result of the above dataset
2411   // This will trigger the creation of the Execution Tree and launch it.
2412   std::shared_ptr<Iterator> iter2 = ds2->CreateIterator();
2413   EXPECT_NE(iter2, nullptr);
2414 
2415   // Check current data type of dataset
2416   ASSERT_OK(iter2->GetNextRow(&row));
2417   auto image2 = row["image"];
2418   auto cur_type = image2.DataType();
2419   MS_LOG(INFO) << "Current data type id: " << cur_type;
2420   EXPECT_EQ(cur_type, mindspore::DataType(mindspore::TypeId::kNumberTypeUInt16));
2421 
2422   // Manually terminate the pipeline
2423   iter2->Stop();
2424 }
2425 
TEST_F(MindDataTestPipeline,TestTypeCastFail)2426 TEST_F(MindDataTestPipeline, TestTypeCastFail) {
2427   MS_LOG(INFO) << "Doing MindDataTestPipeline-TestTypeCastFail with invalid param.";
2428 
2429   // Create a Cifar10 Dataset
2430   std::string folder_path = datasets_root_path_ + "/testCifar10Data/";
2431   std::shared_ptr<Dataset> ds = Cifar10(folder_path, "all", std::make_shared<RandomSampler>(false, 10));
2432   EXPECT_NE(ds, nullptr);
2433 
2434   // incorrect data type
2435   std::shared_ptr<TensorTransform> type_cast =
2436     std::make_shared<transforms::TypeCast>(mindspore::DataType::kTypeUnknown);
2437 
2438   // Create a Map operation on ds
2439   ds = ds->Map({type_cast}, {"image", "label"});
2440   EXPECT_NE(ds, nullptr);
2441 
2442   std::shared_ptr<Iterator> iter = ds->CreateIterator();
2443   // Expect failure: invalid TypeCast input
2444   EXPECT_EQ(iter, nullptr);
2445 }
2446