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.

tree_adapter_test.cc 4.5 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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 "minddata/dataset/engine/tree_adapter.h"
  17. #include "common/common.h"
  18. #include "minddata/dataset/core/tensor_row.h"
  19. #include "minddata/dataset/include/datasets.h"
  20. #include "minddata/dataset/include/transforms.h"
  21. #include "minddata/dataset/engine/ir/datasetops/source/image_folder_node.h"
  22. #include "minddata/dataset/engine/ir/datasetops/batch_node.h"
  23. using namespace mindspore::dataset;
  24. using mindspore::dataset::Tensor;
  25. class MindDataTestTreeAdapter : public UT::DatasetOpTesting {
  26. protected:
  27. };
  28. TEST_F(MindDataTestTreeAdapter, TestSimpleTreeAdapter) {
  29. MS_LOG(INFO) << "Doing MindDataTestTreeAdapter-TestSimpleTreeAdapter.";
  30. // Create a Mnist Dataset
  31. std::string folder_path = datasets_root_path_ + "/testMnistData/";
  32. std::shared_ptr<api::Dataset> ds = Mnist(folder_path, "all", api::SequentialSampler(0, 4));
  33. EXPECT_NE(ds, nullptr);
  34. ds = ds->Batch(2);
  35. EXPECT_NE(ds, nullptr);
  36. mindspore::dataset::TreeAdapter tree_adapter;
  37. Status rc = tree_adapter.BuildAndPrepare(ds, 1);
  38. EXPECT_TRUE(rc.IsOk());
  39. const std::unordered_map<std::string, int32_t> map = {{"label", 1}, {"image", 0}};
  40. EXPECT_EQ(tree_adapter.GetColumnNameMap(), map);
  41. std::vector<size_t> row_sizes = {2, 2, 0, 0};
  42. TensorRow row;
  43. for (size_t sz : row_sizes) {
  44. rc = tree_adapter.GetNext(&row);
  45. EXPECT_TRUE(rc.IsOk());
  46. EXPECT_EQ(row.size(), sz);
  47. }
  48. rc = tree_adapter.GetNext(&row);
  49. EXPECT_TRUE(rc.IsError());
  50. const std::string err_msg = rc.ToString();
  51. EXPECT_TRUE(err_msg.find("EOF has already been reached") != err_msg.npos);
  52. }
  53. TEST_F(MindDataTestTreeAdapter, TestTreeAdapterWithRepeat) {
  54. MS_LOG(INFO) << "Doing MindDataTestTreeAdapter-TestTreeAdapterWithRepeat.";
  55. // Create a Mnist Dataset
  56. std::string folder_path = datasets_root_path_ + "/testMnistData/";
  57. std::shared_ptr<api::Dataset> ds = Mnist(folder_path, "all", api::SequentialSampler(0, 3));
  58. EXPECT_NE(ds, nullptr);
  59. ds = ds->Batch(2, false);
  60. EXPECT_NE(ds, nullptr);
  61. mindspore::dataset::TreeAdapter tree_adapter;
  62. Status rc = tree_adapter.BuildAndPrepare(ds, 2);
  63. EXPECT_TRUE(rc.IsOk());
  64. const std::unordered_map<std::string, int32_t> map = tree_adapter.GetColumnNameMap();
  65. EXPECT_EQ(tree_adapter.GetColumnNameMap(), map);
  66. std::vector<size_t> row_sizes = {2, 2, 0, 2, 2, 0, 0};
  67. TensorRow row;
  68. for (size_t sz : row_sizes) {
  69. rc = tree_adapter.GetNext(&row);
  70. EXPECT_TRUE(rc.IsOk());
  71. EXPECT_EQ(row.size(), sz);
  72. }
  73. rc = tree_adapter.GetNext(&row);
  74. const std::string err_msg = rc.ToString();
  75. EXPECT_TRUE(err_msg.find("EOF has already been reached") != err_msg.npos);
  76. }
  77. TEST_F(MindDataTestTreeAdapter, TestProjectMapTreeAdapter) {
  78. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestProjectMap.";
  79. // Create an ImageFolder Dataset
  80. std::string folder_path = datasets_root_path_ + "/testPK/data/";
  81. std::shared_ptr<api::Dataset> ds = ImageFolder(folder_path, true, api::SequentialSampler(0, 2));
  82. EXPECT_NE(ds, nullptr);
  83. // Create objects for the tensor ops
  84. std::shared_ptr<api::TensorOperation> one_hot = api::transforms::OneHot(10);
  85. EXPECT_NE(one_hot, nullptr);
  86. // Create a Map operation, this will automatically add a project after map
  87. ds = ds->Map({one_hot}, {"label"}, {"label"}, {"label"});
  88. EXPECT_NE(ds, nullptr);
  89. mindspore::dataset::TreeAdapter tree_adapter;
  90. Status rc = tree_adapter.BuildAndPrepare(ds, 2);
  91. EXPECT_TRUE(rc.IsOk());
  92. const std::unordered_map<std::string, int32_t> map = {{"label", 0}};
  93. EXPECT_EQ(tree_adapter.GetColumnNameMap(), map);
  94. std::vector<size_t> row_sizes = {1, 1, 0, 1, 1, 0, 0};
  95. TensorRow row;
  96. for (size_t sz : row_sizes) {
  97. rc = tree_adapter.GetNext(&row);
  98. EXPECT_TRUE(rc.IsOk());
  99. EXPECT_EQ(row.size(), sz);
  100. }
  101. rc = tree_adapter.GetNext(&row);
  102. const std::string err_msg = rc.ToString();
  103. EXPECT_TRUE(err_msg.find("EOF has already been reached") != err_msg.npos);
  104. }