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

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