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.2 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. // Read images from target directory
  54. std::vector<std::shared_ptr<Tensor>> images;
  55. MindDataEager::LoadImageFromDir("/home/workspace/mindspore_dataset/imagenet/imagenet_original/val/n01440764",
  56. &images);
  57. // Define dvpp transform
  58. std::vector<uint32_t> crop_size = {224, 224};
  59. std::vector<uint32_t> resize_size = {256, 256};
  60. MindDataEager Transform({DvppDecodeResizeCropJpeg(crop_size, resize_size)});
  61. // Apply transform on images
  62. for (auto &img : images) {
  63. img = Transform(img);
  64. ASSERT_NE(img, nullptr);
  65. ASSERT_EQ(img->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(img->Shape()[0], real_h * real_w * 1.5); // For image in YUV format, each pixel takes 1.5 byte
  77. ASSERT_EQ(img->Shape()[1], 1);
  78. ASSERT_EQ(img->Shape()[2], 1);
  79. }
  80. }