您最多选择25个标签 标签必须以中文、字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

album_op_test.cc 7.4 kB

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