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

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