1 /**
2 * Copyright 2021 Huawei Technologies Co., Ltd
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 #include "common/common.h"
17 #include "minddata/dataset/include/dataset/datasets.h"
18 #include "minddata/dataset/include/dataset/transforms.h"
19 #include "minddata/dataset/include/dataset/vision.h"
20
21 using namespace mindspore::dataset;
22 using mindspore::dataset::InterpolationMode;
23 using mindspore::dataset::Tensor;
24
25 class MindDataTestPipeline : public UT::DatasetOpTesting {
26 protected:
27 };
28
TEST_F(MindDataTestPipeline,TestAffineAPI)29 TEST_F(MindDataTestPipeline, TestAffineAPI) {
30 MS_LOG(INFO) << "Doing MindDataTestAffine-TestAffineAPI.";
31
32 // Create an ImageFolder Dataset
33 std::string folder_path = datasets_root_path_ + "/testPK/data/";
34 std::shared_ptr<Dataset> ds = ImageFolder(folder_path, true, std::make_shared<RandomSampler>(false, 5));
35
36 // Create a Repeat operation on ds
37 int32_t repeat_num = 3;
38 ds = ds->Repeat(repeat_num);
39
40 // Create auto contrast object with default values
41
42 std::shared_ptr<TensorTransform> crop(new vision::RandomCrop({256, 256}));
43 std::shared_ptr<TensorTransform> affine(
44 new vision::Affine(0.0, {0.0, 0.0}, 0.0, {0.0, 0.0}, InterpolationMode::kLinear));
45
46 // Create a Map operation on ds
47 ds = ds->Map({crop, affine});
48
49 // Create an iterator over the result of the above dataset
50 // This will trigger the creation of the Execution Tree and launch it.
51 std::shared_ptr<Iterator> iter = ds->CreateIterator();
52
53 // Iterate the dataset and get each row
54 std::unordered_map<std::string, mindspore::MSTensor> row;
55 ASSERT_OK(iter->GetNextRow(&row));
56
57 uint64_t i = 0;
58 while (row.size() != 0) {
59 i++;
60 auto image = row["image"];
61 MS_LOG(INFO) << "Tensor image shape: " << image.Shape();
62 EXPECT_EQ(row["image"].Shape().at(0), 256);
63 ASSERT_OK(iter->GetNextRow(&row));
64 }
65
66 EXPECT_EQ(i, 15);
67
68 // Manually terminate the pipeline
69 iter->Stop();
70 }
71
TEST_F(MindDataTestPipeline,TestAffineAPIFail)72 TEST_F(MindDataTestPipeline, TestAffineAPIFail) {
73 MS_LOG(INFO) << "Doing MindDataTestAffine-TestAffineAPI.";
74
75 // Create an ImageFolder Dataset
76 std::string folder_path = datasets_root_path_ + "/testPK/data/";
77 std::shared_ptr<Dataset> ds = ImageFolder(folder_path, true, std::make_shared<RandomSampler>(false, 5));
78
79 // Create a Repeat operation on ds
80 int32_t repeat_num = 3;
81 ds = ds->Repeat(repeat_num);
82
83 // Create auto contrast object with default values
84
85 std::shared_ptr<TensorTransform> crop(new vision::RandomCrop({256, 256}));
86 std::shared_ptr<TensorTransform> affine(
87 new vision::Affine(0.0, {2.0, -1.0}, 0.0, {0.0, 0.0}, InterpolationMode::kLinear));
88
89 // Create a Map operation on ds
90 ds = ds->Map({crop, affine});
91
92 // Create an iterator over the result of the above dataset
93 // This will trigger the creation of the Execution Tree and launch it.
94 std::shared_ptr<Iterator> iter = ds->CreateIterator();
95 EXPECT_EQ(iter, nullptr);
96 }
97