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
21 using namespace mindspore::dataset;
22
23 class MindDataTestPipeline : public UT::DatasetOpTesting {
24 protected:
25 };
26
27 // Tests for vision C++ API SoftDvpp* TensorTransform Operations (in alphabetical order)
28
TEST_F(MindDataTestPipeline,TestSoftDvppDecodeRandomCropResizeJpegSuccess1)29 TEST_F(MindDataTestPipeline, TestSoftDvppDecodeRandomCropResizeJpegSuccess1) {
30 MS_LOG(INFO)
31 << "Doing MindDataTestPipeline-TestSoftDvppDecodeRandomCropResizeJpegSuccess1 with single integer input.";
32
33 // Create an ImageFolder Dataset
34 std::string folder_path = datasets_root_path_ + "/testPK/data/";
35 std::shared_ptr<Dataset> ds = ImageFolder(folder_path, false, std::make_shared<RandomSampler>(false, 4));
36 EXPECT_NE(ds, nullptr);
37
38 // Create objects for the tensor ops
39 std::shared_ptr<TensorTransform> soft_dvpp_decode_random_crop_resize_jpeg(
40 new vision::SoftDvppDecodeRandomCropResizeJpeg({500}));
41 // Note: No need to check for output after calling API class constructor
42
43 // Create a Map operation on ds
44 ds = ds->Map({soft_dvpp_decode_random_crop_resize_jpeg}, {"image"});
45 EXPECT_NE(ds, nullptr);
46
47 // Create an iterator over the result of the above dataset
48 // This will trigger the creation of the Execution Tree and launch it.
49 std::shared_ptr<Iterator> iter = ds->CreateIterator();
50 EXPECT_NE(iter, nullptr);
51
52 // Iterate the dataset and get each row
53 std::unordered_map<std::string, mindspore::MSTensor> row;
54 ASSERT_OK(iter->GetNextRow(&row));
55
56 uint64_t i = 0;
57 while (row.size() != 0) {
58 i++;
59 auto image = row["image"];
60 MS_LOG(INFO) << "Tensor image shape: " << image.Shape();
61 EXPECT_EQ(image.Shape()[0] == 500 && image.Shape()[1] == 500, true);
62 ASSERT_OK(iter->GetNextRow(&row));
63 }
64
65 EXPECT_EQ(i, 4);
66
67 // Manually terminate the pipeline
68 iter->Stop();
69 }
70
TEST_F(MindDataTestPipeline,TestSoftDvppDecodeRandomCropResizeJpegSuccess2)71 TEST_F(MindDataTestPipeline, TestSoftDvppDecodeRandomCropResizeJpegSuccess2) {
72 MS_LOG(INFO)
73 << "Doing MindDataTestPipeline-TestSoftDvppDecodeRandomCropResizeJpegSuccess2 with (height, width) input.";
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, false, std::make_shared<RandomSampler>(false, 6));
78 EXPECT_NE(ds, nullptr);
79
80 // Create objects for the tensor ops
81 std::shared_ptr<TensorTransform> soft_dvpp_decode_random_crop_resize_jpeg(
82 new vision::SoftDvppDecodeRandomCropResizeJpeg({500, 600}, {0.25, 0.75}, {0.5, 1.25}, 20));
83 // Note: No need to check for output after calling API class constructor
84
85 // Create a Map operation on ds
86 ds = ds->Map({soft_dvpp_decode_random_crop_resize_jpeg}, {"image"});
87 EXPECT_NE(ds, nullptr);
88
89 // Create an iterator over the result of the above dataset
90 // This will trigger the creation of the Execution Tree and launch it.
91 std::shared_ptr<Iterator> iter = ds->CreateIterator();
92 EXPECT_NE(iter, nullptr);
93
94 // Iterate the dataset and get each row
95 std::unordered_map<std::string, mindspore::MSTensor> row;
96 ASSERT_OK(iter->GetNextRow(&row));
97
98 uint64_t i = 0;
99 while (row.size() != 0) {
100 i++;
101 auto image = row["image"];
102 MS_LOG(INFO) << "Tensor image shape: " << image.Shape();
103 EXPECT_EQ(image.Shape()[0] == 500 && image.Shape()[1] == 600, true);
104 ASSERT_OK(iter->GetNextRow(&row));
105 }
106
107 EXPECT_EQ(i, 6);
108
109 // Manually terminate the pipeline
110 iter->Stop();
111 }
112
TEST_F(MindDataTestPipeline,TestSoftDvppDecodeResizeJpegSuccess1)113 TEST_F(MindDataTestPipeline, TestSoftDvppDecodeResizeJpegSuccess1) {
114 MS_LOG(INFO) << "Doing MindDataTestPipeline-TestSoftDvppDecodeResizeJpegSuccess1 with single integer input.";
115 // Create an ImageFolder Dataset
116 std::string folder_path = datasets_root_path_ + "/testPK/data/";
117 std::shared_ptr<Dataset> ds = ImageFolder(folder_path, false, std::make_shared<RandomSampler>(false, 4));
118 EXPECT_NE(ds, nullptr);
119
120 // Create a Repeat operation on ds
121 int32_t repeat_num = 3;
122 ds = ds->Repeat(repeat_num);
123 EXPECT_NE(ds, nullptr);
124
125 // Create SoftDvppDecodeResizeJpeg object with single integer input
126 std::shared_ptr<TensorTransform> soft_dvpp_decode_resize_jpeg_op(new vision::SoftDvppDecodeResizeJpeg({1134}));
127 // Note: No need to check for output after calling API class constructor
128
129 // Create a Map operation on ds
130 ds = ds->Map({soft_dvpp_decode_resize_jpeg_op});
131 EXPECT_NE(ds, nullptr);
132
133 // Create an iterator over the result of the above dataset
134 // This will trigger the creation of the Execution Tree and launch it.
135 std::shared_ptr<Iterator> iter = ds->CreateIterator();
136 EXPECT_NE(iter, nullptr);
137
138 // Iterate the dataset and get each row
139 std::unordered_map<std::string, mindspore::MSTensor> row;
140 ASSERT_OK(iter->GetNextRow(&row));
141
142 uint64_t i = 0;
143 while (row.size() != 0) {
144 i++;
145 auto image = row["image"];
146 MS_LOG(INFO) << "Tensor image shape: " << image.Shape();
147 ASSERT_OK(iter->GetNextRow(&row));
148 }
149
150 EXPECT_EQ(i, 12);
151
152 // Manually terminate the pipeline
153 iter->Stop();
154 }
155
TEST_F(MindDataTestPipeline,TestSoftDvppDecodeResizeJpegSuccess2)156 TEST_F(MindDataTestPipeline, TestSoftDvppDecodeResizeJpegSuccess2) {
157 MS_LOG(INFO) << "Doing MindDataTestPipeline-TestSoftDvppDecodeResizeJpegSuccess2 with (height, width) input.";
158 // Create an ImageFolder Dataset
159 std::string folder_path = datasets_root_path_ + "/testPK/data/";
160 std::shared_ptr<Dataset> ds = ImageFolder(folder_path, false, std::make_shared<RandomSampler>(false, 2));
161 EXPECT_NE(ds, nullptr);
162
163 // Create SoftDvppDecodeResizeJpeg object with single integer input
164 std::shared_ptr<TensorTransform> soft_dvpp_decode_resize_jpeg_op(new vision::SoftDvppDecodeResizeJpeg({100, 200}));
165 // Note: No need to check for output after calling API class constructor
166
167 // Create a Map operation on ds
168 ds = ds->Map({soft_dvpp_decode_resize_jpeg_op});
169 EXPECT_NE(ds, nullptr);
170
171 // Create an iterator over the result of the above dataset
172 // This will trigger the creation of the Execution Tree and launch it.
173 std::shared_ptr<Iterator> iter = ds->CreateIterator();
174 EXPECT_NE(iter, nullptr);
175
176 // Iterate the dataset and get each row
177 std::unordered_map<std::string, mindspore::MSTensor> row;
178 ASSERT_OK(iter->GetNextRow(&row));
179
180 uint64_t i = 0;
181 while (row.size() != 0) {
182 i++;
183 auto image = row["image"];
184 MS_LOG(INFO) << "Tensor image shape: " << image.Shape();
185 ASSERT_OK(iter->GetNextRow(&row));
186 }
187
188 EXPECT_EQ(i, 2);
189
190 // Manually terminate the pipeline
191 iter->Stop();
192 }
193