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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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 <cmath>
  19. #include "common/common_test.h"
  20. #include "include/api/types.h"
  21. #include "minddata/dataset/include/minddata_eager.h"
  22. #include "minddata/dataset/include/vision.h"
  23. #include "minddata/dataset/kernels/tensor_op.h"
  24. #include "include/api/model.h"
  25. #include "include/api/serialization.h"
  26. #include "include/api/context.h"
  27. using namespace mindspore::api;
  28. using namespace mindspore::dataset::vision;
  29. class TestDE : public ST::Common {
  30. public:
  31. TestDE() {}
  32. };
  33. TEST_F(TestDE, TestResNetPreprocess) {
  34. // Read images from target directory
  35. std::vector<std::shared_ptr<Tensor>> images;
  36. MindDataEager::LoadImageFromDir("/home/workspace/mindspore_dataset/imagenet/imagenet_original/val/n01440764",
  37. &images);
  38. // Define transform operations
  39. MindDataEager Transform({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. for (auto &img : images) {
  44. img = Transform(img);
  45. }
  46. // Check shape of result
  47. ASSERT_NE(images.size(), 0);
  48. ASSERT_EQ(images[0]->Shape().size(), 3);
  49. ASSERT_EQ(images[0]->Shape()[0], 3);
  50. ASSERT_EQ(images[0]->Shape()[1], 224);
  51. ASSERT_EQ(images[0]->Shape()[2], 224);
  52. }
  53. TEST_F(TestDE, TestDvpp) {
  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. if (crop_size.size() == 1) {
  68. ASSERT_EQ(img->Shape()[0], pow(crop_size[0], 2) * 1.5);
  69. } else {
  70. ASSERT_EQ(img->Shape()[0], crop_size[0] * crop_size[1] * 1.5);
  71. }
  72. ASSERT_EQ(img->Shape()[1], 1);
  73. ASSERT_EQ(img->Shape()[2], 1);
  74. }
  75. }