1 /**
2 * Copyright 2020 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 <iostream>
17 #include <string>
18 #include <vector>
19 #include "common/common_test.h"
20 #include "include/api/types.h"
21 #include "minddata/dataset/include/dataset/execute.h"
22 #include "minddata/dataset/include/dataset/transforms.h"
23 #include "minddata/dataset/include/dataset/vision.h"
24 #ifdef ENABLE_ACL
25 #include "minddata/dataset/include/dataset/vision_ascend.h"
26 #endif
27 #include "minddata/dataset/kernels/tensor_op.h"
28 #include "include/api/model.h"
29 #include "include/api/serialization.h"
30 #include "include/api/context.h"
31
32 using namespace mindspore;
33 using namespace mindspore::dataset;
34 using namespace mindspore::dataset::vision;
35
36 class TestDE : public ST::Common {
37 public:
TestDE()38 TestDE() {}
39 };
40
ReadFileToTensor(const std::string & file)41 mindspore::MSTensor ReadFileToTensor(const std::string &file) {
42 if (file.empty()) {
43 std::cout << "[ERROR]Pointer file is nullptr, return an empty Tensor." << std::endl;
44 return mindspore::MSTensor();
45 }
46 std::ifstream ifs(file);
47 if (!ifs.good()) {
48 std::cout << "[ERROR]File: " << file << " does not exist, return an empty Tensor." << std::endl;
49 return mindspore::MSTensor();
50 }
51 if (!ifs.is_open()) {
52 std::cout << "[ERROR]File: " << file << "open failed, return an empty Tensor." << std::endl;
53 return mindspore::MSTensor();
54 }
55
56 ifs.seekg(0, std::ios::end);
57 size_t size = ifs.tellg();
58 mindspore::MSTensor buf("file", mindspore::DataType::kNumberTypeUInt8, {static_cast<int64_t>(size)}, nullptr, size);
59
60 ifs.seekg(0, std::ios::beg);
61 ifs.read(reinterpret_cast<char *>(buf.MutableData()), size);
62 ifs.close();
63
64 return buf;
65 }
66
TEST_F(TestDE,TestResNetPreprocess)67 TEST_F(TestDE, TestResNetPreprocess) {
68 // Read images
69 auto image = ReadFileToTensor("./data/dataset/apple.jpg");
70
71 // Define transform operations
72 std::shared_ptr<TensorTransform> decode(new vision::Decode());
73 std::shared_ptr<TensorTransform> resize(new vision::Resize({224, 224}));
74 std::shared_ptr<TensorTransform> normalize(
75 new vision::Normalize({0.485 * 255, 0.456 * 255, 0.406 * 255}, {0.229 * 255, 0.224 * 255, 0.225 * 255}));
76 std::shared_ptr<TensorTransform> hwc2chw(new vision::HWC2CHW());
77
78 mindspore::dataset::Execute Transform({decode, resize, normalize, hwc2chw});
79
80 // Apply transform on images
81 Status rc = Transform(image, &image);
82
83 // Check image info
84 ASSERT_TRUE(rc.IsOk());
85 ASSERT_EQ(image.Shape().size(), 3);
86 ASSERT_EQ(image.Shape()[0], 3);
87 ASSERT_EQ(image.Shape()[1], 224);
88 ASSERT_EQ(image.Shape()[2], 224);
89 }
90
TEST_F(TestDE,TestDvpp)91 TEST_F(TestDE, TestDvpp) {
92 #ifdef ENABLE_ACL
93 // Read images from target directory
94
95 /* Old internal method, we deprecate it
96 std::shared_ptr<mindspore::dataset::Tensor> de_tensor;
97 Status rc = mindspore::dataset::Tensor::CreateFromFile("./data/dataset/apple.jpg", &de_tensor);
98 ASSERT_TRUE(rc.IsOk());
99 auto image = MSTensor(std::make_shared<mindspore::dataset::DETensor>(de_tensor));
100 */
101 auto context = ContextAutoSet();
102 ASSERT_TRUE(context != nullptr);
103 ASSERT_TRUE(context->MutableDeviceInfo().size() == 1);
104 auto ascend310_info = context->MutableDeviceInfo()[0]->Cast<Ascend310DeviceInfo>();
105 ASSERT_TRUE(ascend310_info != nullptr);
106 auto device_id = ascend310_info->GetDeviceID();
107
108 auto image = ReadFileToTensor("./data/dataset/apple.jpg");
109
110 // Define dvpp transform
111 std::vector<uint32_t> crop_paras = {224, 224};
112 std::vector<uint32_t> resize_paras = {256, 256};
113 std::shared_ptr<TensorTransform> decode_resize_crop(new vision::DvppDecodeResizeCropJpeg(crop_paras, resize_paras));
114 mindspore::dataset::Execute Transform(decode_resize_crop, MapTargetDevice::kAscend310, device_id);
115
116 // Apply transform on images
117 Status rc = Transform(image, &image);
118 std::string aipp_cfg = Transform.AippCfgGenerator();
119 ASSERT_EQ(aipp_cfg, "./aipp.cfg");
120
121 // Check image info
122 ASSERT_TRUE(rc.IsOk());
123 ASSERT_EQ(image.Shape().size(), 2);
124 int32_t real_h = 0;
125 int32_t real_w = 0;
126 int32_t remainder = crop_paras[crop_paras.size() - 1] % 16;
127 if (crop_paras.size() == 1) {
128 real_h = (crop_paras[0] % 2 == 0) ? crop_paras[0] : crop_paras[0] + 1;
129 real_w = (remainder == 0) ? crop_paras[0] : crop_paras[0] + 16 - remainder;
130 } else {
131 real_h = (crop_paras[0] % 2 == 0) ? crop_paras[0] : crop_paras[0] + 1;
132 real_w = (remainder == 0) ? crop_paras[1] : crop_paras[1] + 16 - remainder;
133 }
134
135 ASSERT_EQ(image.Shape()[0], real_h); // For image in YUV format, each pixel takes 1.5 byte
136 ASSERT_EQ(image.Shape()[1], real_w);
137 ASSERT_EQ(image.DataSize(), real_h * real_w * 1.5);
138
139 ASSERT_TRUE(image.Data().get() != nullptr);
140 ASSERT_EQ(image.DataType(), mindspore::DataType::kNumberTypeUInt8);
141 ASSERT_EQ(image.IsDevice(), true);
142
143 /* This is the criterion for previous method(Without pop)
144 ASSERT_EQ(image.Shape()[0], 1.5 * real_h * real_w); // For image in YUV format, each pixel takes 1.5 byte
145 ASSERT_EQ(image.Shape()[1], 1);
146 ASSERT_EQ(image.Shape()[2], 1);
147 ASSERT_EQ(image.DataSize(), real_h * real_w * 1.5);
148 */
149 #endif
150 }
151
TEST_F(TestDE,TestDvppSinkMode)152 TEST_F(TestDE, TestDvppSinkMode) {
153 #ifdef ENABLE_ACL
154 auto context = ContextAutoSet();
155 ASSERT_TRUE(context != nullptr);
156 ASSERT_TRUE(context->MutableDeviceInfo().size() == 1);
157 auto ascend310_info = context->MutableDeviceInfo()[0]->Cast<Ascend310DeviceInfo>();
158 ASSERT_TRUE(ascend310_info != nullptr);
159 auto device_id = ascend310_info->GetDeviceID();
160
161 // Read images from target directory
162 auto image = ReadFileToTensor("./data/dataset/apple.jpg");
163
164 // Define dvpp transform
165 std::vector<int32_t> crop_paras = {224, 224};
166 std::vector<int32_t> resize_paras = {256};
167 std::shared_ptr<TensorTransform> decode(new vision::Decode());
168 std::shared_ptr<TensorTransform> resize(new vision::Resize(resize_paras));
169 std::shared_ptr<TensorTransform> centercrop(new vision::CenterCrop(crop_paras));
170 std::vector<std::shared_ptr<TensorTransform>> trans_list = {decode, resize, centercrop};
171 mindspore::dataset::Execute Transform(trans_list, MapTargetDevice::kAscend310, device_id);
172
173 // Apply transform on images
174 Status rc = Transform(image, &image);
175
176 // Check image info
177 ASSERT_TRUE(rc.IsOk());
178 ASSERT_EQ(image.Shape().size(), 2);
179 int32_t real_h = 0;
180 int32_t real_w = 0;
181 int32_t remainder = crop_paras[crop_paras.size() - 1] % 16;
182 if (crop_paras.size() == 1) {
183 real_h = (crop_paras[0] % 2 == 0) ? crop_paras[0] : crop_paras[0] + 1;
184 real_w = (remainder == 0) ? crop_paras[0] : crop_paras[0] + 16 - remainder;
185 } else {
186 real_h = (crop_paras[0] % 2 == 0) ? crop_paras[0] : crop_paras[0] + 1;
187 real_w = (remainder == 0) ? crop_paras[1] : crop_paras[1] + 16 - remainder;
188 }
189 ASSERT_EQ(image.Shape()[0], real_h); // For image in YUV format, each pixel takes 1.5 byte
190 ASSERT_EQ(image.Shape()[1], real_w);
191 ASSERT_EQ(image.DataSize(), real_h * real_w * 1.5);
192
193 ASSERT_TRUE(image.Data().get() != nullptr);
194 ASSERT_EQ(image.DataType(), mindspore::DataType::kNumberTypeUInt8);
195 ASSERT_EQ(image.IsDevice(), true);
196 Transform.DeviceMemoryRelease();
197 #endif
198 }
199
TEST_F(TestDE,TestDvppDecodeResizeCropNormalize)200 TEST_F(TestDE, TestDvppDecodeResizeCropNormalize) {
201 #ifdef ENABLE_ACL
202 auto context = ContextAutoSet();
203 ASSERT_TRUE(context != nullptr);
204 ASSERT_TRUE(context->MutableDeviceInfo().size() == 1);
205 auto ascend310_info = context->MutableDeviceInfo()[0]->Cast<Ascend310DeviceInfo>();
206 ASSERT_TRUE(ascend310_info != nullptr);
207 auto device_id = ascend310_info->GetDeviceID();
208
209 auto image = ReadFileToTensor("./data/dataset/apple.jpg");
210
211 // Define dvpp transform
212 std::vector<int32_t> crop_paras = {416};
213 std::vector<int32_t> resize_paras = {512};
214 std::vector<float> mean = {0.485 * 255, 0.456 * 255, 0.406 * 255};
215 std::vector<float> std = {0.229 * 255, 0.224 * 255, 0.225 * 255};
216
217 std::shared_ptr<TensorTransform> decode(new vision::Decode());
218 std::shared_ptr<TensorTransform> resize(new vision::Resize(resize_paras));
219 std::shared_ptr<TensorTransform> centercrop(new vision::CenterCrop(crop_paras));
220 std::shared_ptr<TensorTransform> normalize(new vision::Normalize(mean, std));
221
222 std::vector<std::shared_ptr<TensorTransform>> trans_list = {decode, resize, centercrop, normalize};
223 mindspore::dataset::Execute Transform(trans_list, MapTargetDevice::kAscend310, device_id);
224
225 std::string aipp_cfg = Transform.AippCfgGenerator();
226 ASSERT_EQ(aipp_cfg, "./aipp.cfg");
227
228 // Apply transform on images
229 Status rc = Transform(image, &image);
230
231 // Check image info
232 ASSERT_TRUE(rc.IsOk());
233 ASSERT_EQ(image.Shape().size(), 2);
234 int32_t real_h = 0;
235 int32_t real_w = 0;
236 int32_t remainder = crop_paras[crop_paras.size() - 1] % 16;
237 if (crop_paras.size() == 1) {
238 real_h = (crop_paras[0] % 2 == 0) ? crop_paras[0] : crop_paras[0] + 1;
239 real_w = (remainder == 0) ? crop_paras[0] : crop_paras[0] + 16 - remainder;
240 } else {
241 real_h = (crop_paras[0] % 2 == 0) ? crop_paras[0] : crop_paras[0] + 1;
242 real_w = (remainder == 0) ? crop_paras[1] : crop_paras[1] + 16 - remainder;
243 }
244
245 ASSERT_EQ(image.Shape()[0], real_h); // For image in YUV format, each pixel takes 1.5 byte
246 ASSERT_EQ(image.Shape()[1], real_w);
247 ASSERT_EQ(image.DataSize(), real_h * real_w * 1.5);
248
249 ASSERT_TRUE(image.Data().get() != nullptr);
250 ASSERT_EQ(image.DataType(), mindspore::DataType::kNumberTypeUInt8);
251 ASSERT_EQ(image.IsDevice(), true);
252 Transform.DeviceMemoryRelease();
253 #endif
254 }
255