You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

test_de.cc 8.8 kB

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