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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. #include "minddata/dataset/kernels/tensor_op.h"
  23. #include "include/api/model.h"
  24. #include "include/api/serialization.h"
  25. #include "include/api/context.h"
  26. using namespace mindspore;
  27. using namespace mindspore::dataset::vision;
  28. class TestDE : public ST::Common {
  29. public:
  30. TestDE() {}
  31. };
  32. TEST_F(TestDE, TestResNetPreprocess) {
  33. // Read images
  34. std::shared_ptr<mindspore::dataset::Tensor> de_tensor;
  35. mindspore::dataset::Tensor::CreateFromFile("./data/dataset/apple.jpg", &de_tensor);
  36. auto image = mindspore::MSTensor(std::make_shared<mindspore::dataset::DETensor>(de_tensor));
  37. // Define transform operations
  38. mindspore::dataset::Execute Transform({
  39. Decode(), Resize({224, 224}),
  40. Normalize({0.485 * 255, 0.456 * 255, 0.406 * 255}, {0.229 * 255, 0.224 * 255, 0.225 * 255}),
  41. HWC2CHW()});
  42. // Apply transform on images
  43. Status rc = Transform(image, &image);
  44. // Check image info
  45. ASSERT_TRUE(rc.IsOk());
  46. ASSERT_EQ(image.Shape().size(), 3);
  47. ASSERT_EQ(image.Shape()[0], 3);
  48. ASSERT_EQ(image.Shape()[1], 224);
  49. ASSERT_EQ(image.Shape()[2], 224);
  50. }
  51. TEST_F(TestDE, TestDvpp) {
  52. #ifdef ENABLE_ACL
  53. // Read images from target directory
  54. std::shared_ptr<mindspore::dataset::Tensor> de_tensor;
  55. mindspore::dataset::Tensor::CreateFromFile("./data/dataset/apple.jpg", &de_tensor);
  56. auto image = MSTensor(std::make_shared<mindspore::dataset::DETensor>(de_tensor));
  57. // Define dvpp transform
  58. std::vector<uint32_t> crop_size = {224, 224};
  59. std::vector<uint32_t> resize_size = {256, 256};
  60. mindspore::dataset::Execute Transform(DvppDecodeResizeCropJpeg(crop_size, resize_size));
  61. // Apply transform on images
  62. Status rc = Transform(image, &image);
  63. // Check image info
  64. ASSERT_TRUE(rc.IsOk());
  65. ASSERT_EQ(image.Shape().size(), 3);
  66. int32_t real_h = 0;
  67. int32_t real_w = 0;
  68. int32_t remainder = crop_size[crop_size.size() - 1] % 16;
  69. if (crop_size.size() == 1) {
  70. real_h = (crop_size[0] % 2 == 0) ? crop_size[0] : crop_size[0] + 1;
  71. real_w = (remainder == 0) ? crop_size[0] : crop_size[0] + 16 - remainder;
  72. } else {
  73. real_h = (crop_size[0] % 2 == 0) ? crop_size[0] : crop_size[0] + 1;
  74. real_w = (remainder == 0) ? crop_size[1] : crop_size[1] + 16 - remainder;
  75. }
  76. ASSERT_EQ(image.Shape()[0], real_h * real_w * 1.5); // For image in YUV format, each pixel takes 1.5 byte
  77. ASSERT_EQ(image.Shape()[1], 1);
  78. ASSERT_EQ(image.Shape()[2], 1);
  79. #endif
  80. }