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.

album_op_test.cc 7.6 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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 <fstream>
  17. #include <iostream>
  18. #include <memory>
  19. #include <string>
  20. #include "common/common.h"
  21. #include "minddata/dataset/core/client.h"
  22. #include "minddata/dataset/core/global_context.h"
  23. #include "minddata/dataset/engine/datasetops/source/album_op.h"
  24. #include "minddata/dataset/engine/datasetops/source/sampler/distributed_sampler.h"
  25. #include "minddata/dataset/engine/datasetops/source/sampler/pk_sampler.h"
  26. #include "minddata/dataset/engine/datasetops/source/sampler/random_sampler.h"
  27. #include "minddata/dataset/engine/datasetops/source/sampler/sampler.h"
  28. #include "minddata/dataset/engine/datasetops/source/sampler/sequential_sampler.h"
  29. #include "minddata/dataset/engine/datasetops/source/sampler/subset_random_sampler.h"
  30. #include "minddata/dataset/engine/datasetops/source/sampler/weighted_random_sampler.h"
  31. #include "minddata/dataset/util/path.h"
  32. #include "minddata/dataset/util/status.h"
  33. #include "gtest/gtest.h"
  34. #include "utils/log_adapter.h"
  35. #include "securec.h"
  36. #include "minddata/dataset/include/datasets.h"
  37. #include "minddata/dataset/include/transforms.h"
  38. using namespace mindspore::dataset;
  39. using mindspore::MsLogLevel::ERROR;
  40. using mindspore::ExceptionType::NoExceptionType;
  41. using mindspore::LogStream;
  42. std::shared_ptr<BatchOp> Batch(int batch_size = 1, bool drop = false, int rows_per_buf = 2);
  43. std::shared_ptr<RepeatOp> Repeat(int repeat_cnt);
  44. std::shared_ptr<ExecutionTree> Build(std::vector<std::shared_ptr<DatasetOp>> ops);
  45. std::shared_ptr<AlbumOp> Album(int64_t num_works, int64_t rows, int64_t conns, std::string path, bool shuf = false,
  46. std::unique_ptr<SamplerRT> sampler = nullptr, bool decode = false) {
  47. std::shared_ptr<AlbumOp> so;
  48. AlbumOp::Builder builder;
  49. Status rc = builder.SetNumWorkers(num_works)
  50. .SetAlbumDir(path)
  51. .SetRowsPerBuffer(rows)
  52. .SetOpConnectorSize(conns)
  53. .SetExtensions({".json"})
  54. .SetSampler(std::move(sampler))
  55. .SetDecode(decode)
  56. .Build(&so);
  57. return so;
  58. }
  59. std::shared_ptr<AlbumOp> AlbumSchema(int64_t num_works, int64_t rows, int64_t conns, std::string path,
  60. std::string schema_file, std::vector<std::string> column_names = {},
  61. bool shuf = false, std::unique_ptr<SamplerRT> sampler = nullptr,
  62. bool decode = false) {
  63. std::shared_ptr<AlbumOp> so;
  64. AlbumOp::Builder builder;
  65. Status rc = builder.SetNumWorkers(num_works)
  66. .SetSchemaFile(schema_file)
  67. .SetColumnsToLoad(column_names)
  68. .SetAlbumDir(path)
  69. .SetRowsPerBuffer(rows)
  70. .SetOpConnectorSize(conns)
  71. .SetExtensions({".json"})
  72. .SetSampler(std::move(sampler))
  73. .SetDecode(decode)
  74. .Build(&so);
  75. return so;
  76. }
  77. class MindDataTestAlbum : public UT::DatasetOpTesting {
  78. protected:
  79. };
  80. TEST_F(MindDataTestAlbum, TestSequentialAlbumWithSchema) {
  81. std::string folder_path = datasets_root_path_ + "/testAlbum/images";
  82. std::string schema_file = datasets_root_path_ + "/testAlbum/datasetSchema.json";
  83. std::vector<std::string> column_names = {"image", "label", "id"};
  84. auto tree = Build({AlbumSchema(16, 2, 32, folder_path, schema_file, column_names, false), Repeat(2)});
  85. ASSERT_OK(tree->Prepare());
  86. ASSERT_OK(tree->Launch());
  87. DatasetIterator di(tree);
  88. TensorMap tensor_map;
  89. ASSERT_OK(di.GetNextAsMap(&tensor_map));
  90. uint64_t i = 0;
  91. std::string_view label = 0;
  92. while (tensor_map.size() != 0) {
  93. EXPECT_TRUE(tensor_map["label"]->GetItemAt(&label, {}));
  94. MS_LOG(DEBUG) << "row: " << i << "\t" << tensor_map["image"]->shape() << "label:" << label << "label shape"
  95. << tensor_map["label"] << "\n";
  96. i++;
  97. di.GetNextAsMap(&tensor_map);
  98. }
  99. MS_LOG(INFO) << "got rows: " << i << "\n";
  100. EXPECT_TRUE(i == 14);
  101. }
  102. TEST_F(MindDataTestAlbum, TestSequentialAlbumWithSchemaNoOrder) {
  103. std::string folder_path = datasets_root_path_ + "/testAlbum/images";
  104. std::string schema_file = datasets_root_path_ + "/testAlbum/datasetSchema.json";
  105. auto tree = Build({AlbumSchema(16, 2, 32, folder_path, schema_file), Repeat(2)});
  106. ASSERT_OK(tree->Prepare());
  107. ASSERT_OK(tree->Launch());
  108. DatasetIterator di(tree);
  109. TensorMap tensor_map;
  110. ASSERT_TRUE(di.GetNextAsMap(&tensor_map));
  111. uint64_t i = 0;
  112. std::string_view label;
  113. while (tensor_map.size() != 0) {
  114. EXPECT_OK(tensor_map["label"]->GetItemAt(&label, {}));
  115. MS_LOG(DEBUG) << "row: " << i << "\t" << tensor_map["image"]->shape() << "label:" << label << "label shape"
  116. << tensor_map["label"] << "\n";
  117. i++;
  118. di.GetNextAsMap(&tensor_map);
  119. }
  120. MS_LOG(INFO) << "got rows: " << i << "\n";
  121. EXPECT_TRUE(i == 14);
  122. }
  123. TEST_F(MindDataTestAlbum, TestSequentialAlbumWithSchemaFloat) {
  124. std::string folder_path = datasets_root_path_ + "/testAlbum/images";
  125. // add the priority column
  126. std::string schema_file = datasets_root_path_ + "/testAlbum/floatSchema.json";
  127. auto tree = Build({AlbumSchema(16, 2, 32, folder_path, schema_file), Repeat(2)});
  128. tree->Prepare();
  129. ASSERT_OK(tree->Launch());
  130. DatasetIterator di(tree);
  131. TensorMap tensor_map;
  132. ASSERT_OK(di.GetNextAsMap(&tensor_map));
  133. uint64_t i = 0;
  134. std::string_view label;
  135. double priority = 0;
  136. while (tensor_map.size() != 0) {
  137. EXPECT_OK(tensor_map["label"]->GetItemAt(&label, {}));
  138. EXPECT_OK(tensor_map["_priority"]->GetItemAt<double>(&priority, {}));
  139. MS_LOG(DEBUG) << "row: " << i << "\t" << tensor_map["image"]->shape() << "label:" << label << "label shape"
  140. << tensor_map["label"] << "priority: " << priority << "\n";
  141. i++;
  142. di.GetNextAsMap(&tensor_map);
  143. }
  144. MS_LOG(INFO) << "got rows: " << i << "\n";
  145. EXPECT_TRUE(i == 14);
  146. }
  147. TEST_F(MindDataTestAlbum, TestSequentialAlbumWithFullSchema) {
  148. std::string folder_path = datasets_root_path_ + "/testAlbum/images";
  149. // add the priority column
  150. std::string schema_file = datasets_root_path_ + "/testAlbum/fullSchema.json";
  151. auto tree = Build({AlbumSchema(16, 2, 32, folder_path, schema_file), Repeat(2)});
  152. ASSERT_OK(tree->Prepare());
  153. ASSERT_OK(tree->Launch());
  154. DatasetIterator di(tree);
  155. TensorMap tensor_map;
  156. ASSERT_OK(di.GetNextAsMap(&tensor_map));
  157. uint64_t i = 0;
  158. std::string_view label = 0;
  159. double priority = 0;
  160. int64_t id = 0;
  161. while (tensor_map.size() != 0) {
  162. EXPECT_OK(tensor_map["label"]->GetItemAt(&label, {}));
  163. EXPECT_OK(tensor_map["_priority"]->GetItemAt<double>(&priority, {}));
  164. EXPECT_OK(tensor_map["id"]->GetItemAt<int64_t>(&id, {}));
  165. MS_LOG(DEBUG) << "row: " << i << "\t" << tensor_map["image"]->shape() << "label:" << label << "label shape"
  166. << tensor_map["label"] << "priority: " << priority << " embedding : "
  167. << tensor_map["_embedding"]->shape() << " id: " << id << "\n";
  168. i++;
  169. di.GetNextAsMap(&tensor_map);
  170. }
  171. MS_LOG(INFO) << "got rows: " << i << "\n";
  172. EXPECT_TRUE(i == 14);
  173. }