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 6.3 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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 <string>
  17. #include <vector>
  18. #include "common/common_test.h"
  19. #include "include/api/types.h"
  20. #include "minddata/dataset/include/execute.h"
  21. #include "minddata/dataset/include/transforms.h"
  22. #include "minddata/dataset/include/vision.h"
  23. #ifdef ENABLE_ACL
  24. #include "minddata/dataset/include/vision_ascend.h"
  25. #endif
  26. #include "minddata/dataset/kernels/tensor_op.h"
  27. #include "include/api/model.h"
  28. #include "include/api/serialization.h"
  29. #include "include/api/context.h"
  30. using namespace mindspore;
  31. using namespace mindspore::dataset;
  32. using namespace mindspore::dataset::vision;
  33. class TestDE : public ST::Common {
  34. public:
  35. TestDE() {}
  36. };
  37. TEST_F(TestDE, TestResNetPreprocess) {
  38. // Read images
  39. std::shared_ptr<mindspore::dataset::Tensor> de_tensor;
  40. mindspore::dataset::Tensor::CreateFromFile("./data/dataset/apple.jpg", &de_tensor);
  41. auto image = mindspore::MSTensor(std::make_shared<mindspore::dataset::DETensor>(de_tensor));
  42. // Define transform operations
  43. auto decode(new vision::Decode());
  44. auto resize(new vision::Resize({224, 224}));
  45. auto normalize(
  46. new vision::Normalize({0.485 * 255, 0.456 * 255, 0.406 * 255}, {0.229 * 255, 0.224 * 255, 0.225 * 255}));
  47. auto hwc2chw(new vision::HWC2CHW());
  48. mindspore::dataset::Execute Transform({decode, resize, normalize, hwc2chw});
  49. // Apply transform on images
  50. Status rc = Transform(image, &image);
  51. // Check image info
  52. ASSERT_TRUE(rc.IsOk());
  53. ASSERT_EQ(image.Shape().size(), 3);
  54. ASSERT_EQ(image.Shape()[0], 3);
  55. ASSERT_EQ(image.Shape()[1], 224);
  56. ASSERT_EQ(image.Shape()[2], 224);
  57. }
  58. TEST_F(TestDE, TestDvpp) {
  59. #ifdef ENABLE_ACL
  60. // Read images from target directory
  61. std::shared_ptr<mindspore::dataset::Tensor> de_tensor;
  62. mindspore::dataset::Tensor::CreateFromFile("./data/dataset/apple.jpg", &de_tensor);
  63. auto image = MSTensor(std::make_shared<mindspore::dataset::DETensor>(de_tensor));
  64. // Define dvpp transform
  65. std::vector<uint32_t> crop_paras = {224, 224};
  66. std::vector<uint32_t> resize_paras = {256, 256};
  67. mindspore::dataset::Execute Transform(DvppDecodeResizeCropJpeg(crop_paras, resize_paras));
  68. // Apply transform on images
  69. Status rc = Transform(image, &image);
  70. // Check image info
  71. ASSERT_TRUE(rc.IsOk());
  72. ASSERT_EQ(image.Shape().size(), 3);
  73. int32_t real_h = 0;
  74. int32_t real_w = 0;
  75. int32_t remainder = crop_paras[crop_paras.size() - 1] % 16;
  76. if (crop_paras.size() == 1) {
  77. real_h = (crop_paras[0] % 2 == 0) ? crop_paras[0] : crop_paras[0] + 1;
  78. real_w = (remainder == 0) ? crop_paras[0] : crop_paras[0] + 16 - remainder;
  79. } else {
  80. real_h = (crop_paras[0] % 2 == 0) ? crop_paras[0] : crop_paras[0] + 1;
  81. real_w = (remainder == 0) ? crop_paras[1] : crop_paras[1] + 16 - remainder;
  82. }
  83. ASSERT_EQ(image.Shape()[0], real_h * real_w * 1.5); // For image in YUV format, each pixel takes 1.5 byte
  84. ASSERT_EQ(image.Shape()[1], 1);
  85. ASSERT_EQ(image.Shape()[2], 1);
  86. #endif
  87. }
  88. TEST_F(TestDE, TestDvppSinkMode) {
  89. #ifdef ENABLE_ACL
  90. // Read images from target directory
  91. std::shared_ptr<mindspore::dataset::Tensor> de_tensor;
  92. mindspore::dataset::Tensor::CreateFromFile("./data/dataset/apple.jpg", &de_tensor);
  93. auto image = MSTensor(std::make_shared<mindspore::dataset::DETensor>(de_tensor));
  94. // Define dvpp transform
  95. std::vector<uint32_t> crop_paras = {224, 224};
  96. std::vector<uint32_t> resize_paras = {256};
  97. mindspore::dataset::Execute Transform({DvppDecodeJpeg(), DvppResizeJpeg(resize_paras), DvppCropJpeg(crop_paras)},
  98. "Ascend310");
  99. // Apply transform on images
  100. Status rc = Transform(image, &image);
  101. // Check image info
  102. ASSERT_TRUE(rc.IsOk());
  103. ASSERT_EQ(image.Shape().size(), 2);
  104. int32_t real_h = 0;
  105. int32_t real_w = 0;
  106. int32_t remainder = crop_paras[crop_paras.size() - 1] % 16;
  107. if (crop_paras.size() == 1) {
  108. real_h = (crop_paras[0] % 2 == 0) ? crop_paras[0] : crop_paras[0] + 1;
  109. real_w = (remainder == 0) ? crop_paras[0] : crop_paras[0] + 16 - remainder;
  110. } else {
  111. real_h = (crop_paras[0] % 2 == 0) ? crop_paras[0] : crop_paras[0] + 1;
  112. real_w = (remainder == 0) ? crop_paras[1] : crop_paras[1] + 16 - remainder;
  113. }
  114. ASSERT_EQ(image.Shape()[0], real_h); // For image in YUV format, each pixel takes 1.5 byte
  115. ASSERT_EQ(image.Shape()[1], real_w);
  116. ASSERT_EQ(image.DataSize(), 1.5 * real_w * real_h);
  117. Transform.DeviceMemoryRelease();
  118. #endif
  119. }
  120. TEST_F(TestDE, TestDvppDecodeResizeCrop) {
  121. #ifdef ENABLE_ACL
  122. std::shared_ptr<mindspore::dataset::Tensor> de_tensor;
  123. mindspore::dataset::Tensor::CreateFromFile("./data/dataset/apple.jpg", &de_tensor);
  124. auto image = MSTensor(std::make_shared<mindspore::dataset::DETensor>(de_tensor));
  125. // Define dvpp transform
  126. std::vector<uint32_t> crop_paras = {416};
  127. std::vector<uint32_t> resize_paras = {512};
  128. mindspore::dataset::Execute Transform(DvppDecodeResizeCropJpeg(crop_paras, resize_paras), "Ascend310");
  129. // Apply transform on images
  130. Status rc = Transform(image, &image);
  131. // Check image info
  132. ASSERT_TRUE(rc.IsOk());
  133. ASSERT_EQ(image.Shape().size(), 2);
  134. int32_t real_h = 0;
  135. int32_t real_w = 0;
  136. int32_t remainder = crop_paras[crop_paras.size() - 1] % 16;
  137. if (crop_paras.size() == 1) {
  138. real_h = (crop_paras[0] % 2 == 0) ? crop_paras[0] : crop_paras[0] + 1;
  139. real_w = (remainder == 0) ? crop_paras[0] : crop_paras[0] + 16 - remainder;
  140. } else {
  141. real_h = (crop_paras[0] % 2 == 0) ? crop_paras[0] : crop_paras[0] + 1;
  142. real_w = (remainder == 0) ? crop_paras[1] : crop_paras[1] + 16 - remainder;
  143. }
  144. ASSERT_EQ(image.Shape()[0], real_h); // For image in YUV format, each pixel takes 1.5 byte
  145. ASSERT_EQ(image.Shape()[1], real_w);
  146. ASSERT_EQ(image.DataSize(), 1.5 * real_w * real_h);
  147. Transform.DeviceMemoryRelease();
  148. #endif
  149. }