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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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/minddata_eager.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::api;
  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 from target directory
  34. std::vector<std::shared_ptr<Tensor>> images;
  35. MindDataEager::LoadImageFromDir("/home/workspace/mindspore_dataset/imagenet/imagenet_original/val/n01440764",
  36. &images);
  37. // Define transform operations
  38. MindDataEager Transform({Decode(), Resize({224, 224}),
  39. Normalize({0.485 * 255, 0.456 * 255, 0.406 * 255}, {0.229 * 255, 0.224 * 255, 0.225 * 255}),
  40. HWC2CHW()});
  41. // Apply transform on images
  42. for (auto &img : images) {
  43. img = Transform(img);
  44. }
  45. // Check shape of result
  46. ASSERT_NE(images.size(), 0);
  47. ASSERT_EQ(images[0]->Shape().size(), 3);
  48. ASSERT_EQ(images[0]->Shape()[0], 3);
  49. ASSERT_EQ(images[0]->Shape()[1], 224);
  50. ASSERT_EQ(images[0]->Shape()[2], 224);
  51. }
  52. TEST_F(TestDE, TestDvpp) {
  53. ContextAutoSet();
  54. // Read images from target directory
  55. std::vector<std::shared_ptr<Tensor>> images;
  56. MindDataEager::LoadImageFromDir("/home/workspace/mindspore_dataset/imagenet/imagenet_original/val/n01440764",
  57. &images);
  58. // Define dvpp transform
  59. std::vector<uint32_t> crop_size = {224, 224};
  60. std::vector<uint32_t> resize_size = {256, 256};
  61. MindDataEager Transform({DvppDecodeResizeCropJpeg(crop_size, resize_size)});
  62. // Apply transform on images
  63. for (auto &img : images) {
  64. img = Transform(img);
  65. ASSERT_NE(img, nullptr);
  66. ASSERT_EQ(img->Shape().size(), 3);
  67. int32_t real_h = 0;
  68. int32_t real_w = 0;
  69. int32_t remainder = crop_size[crop_size.size() - 1] % 16;
  70. if (crop_size.size() == 1) {
  71. real_h = (crop_size[0] % 2 == 0) ? crop_size[0] : crop_size[0] + 1;
  72. real_w = (remainder == 0) ? crop_size[0] : crop_size[0] + 16 - remainder;
  73. } else {
  74. real_h = (crop_size[0] % 2 == 0) ? crop_size[0] : crop_size[0] + 1;
  75. real_w = (remainder == 0) ? crop_size[1] : crop_size[1] + 16 - remainder;
  76. }
  77. ASSERT_EQ(img->Shape()[0], real_h * real_w * 1.5); // For image in YUV format, each pixel takes 1.5 byte
  78. ASSERT_EQ(img->Shape()[1], 1);
  79. ASSERT_EQ(img->Shape()[2], 1);
  80. }
  81. }