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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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. tree->Prepare();
  86. Status rc = tree->Launch();
  87. if (rc.IsError()) {
  88. MS_LOG(ERROR) << "Return code error detected during tree launch: " << ".";
  89. EXPECT_TRUE(false);
  90. } else {
  91. DatasetIterator di(tree);
  92. TensorMap tensor_map;
  93. di.GetNextAsMap(&tensor_map);
  94. EXPECT_TRUE(rc.IsOk());
  95. uint64_t i = 0;
  96. int32_t label = 0;
  97. while (tensor_map.size() != 0) {
  98. tensor_map["label"]->GetItemAt<int32_t>(&label, {});
  99. MS_LOG(DEBUG) << "row: " << i << "\t" << tensor_map["image"]->shape() << "label:" << label << "label shape"
  100. << tensor_map["label"] << "\n";
  101. i++;
  102. di.GetNextAsMap(&tensor_map);
  103. }
  104. MS_LOG(INFO) << "got rows" << i << "\n";
  105. EXPECT_TRUE(i == 14);
  106. }
  107. }
  108. TEST_F(MindDataTestAlbum, TestSequentialAlbumWithSchemaNoOrder) {
  109. std::string folder_path = datasets_root_path_ + "/testAlbum/images";
  110. std::string schema_file = datasets_root_path_ + "/testAlbum/datasetSchema.json";
  111. auto tree = Build({AlbumSchema(16, 2, 32, folder_path, schema_file), Repeat(2)});
  112. tree->Prepare();
  113. Status rc = tree->Launch();
  114. if (rc.IsError()) {
  115. MS_LOG(ERROR) << "Return code error detected during tree launch: " << ".";
  116. EXPECT_TRUE(false);
  117. } else {
  118. DatasetIterator di(tree);
  119. TensorMap tensor_map;
  120. di.GetNextAsMap(&tensor_map);
  121. EXPECT_TRUE(rc.IsOk());
  122. uint64_t i = 0;
  123. int32_t label = 0;
  124. while (tensor_map.size() != 0) {
  125. tensor_map["label"]->GetItemAt<int32_t>(&label, {});
  126. MS_LOG(DEBUG) << "row: " << i << "\t" << tensor_map["image"]->shape() << "label:" << label << "label shape"
  127. << tensor_map["label"] << "\n";
  128. i++;
  129. di.GetNextAsMap(&tensor_map);
  130. }
  131. MS_LOG(INFO) << "got rows" << i << "\n";
  132. EXPECT_TRUE(i == 14);
  133. }
  134. }
  135. TEST_F(MindDataTestAlbum, TestSequentialAlbumWithSchemaFloat) {
  136. std::string folder_path = datasets_root_path_ + "/testAlbum/images";
  137. // add the priority column
  138. std::string schema_file = datasets_root_path_ + "/testAlbum/floatSchema.json";
  139. auto tree = Build({AlbumSchema(16, 2, 32, folder_path, schema_file), Repeat(2)});
  140. tree->Prepare();
  141. Status rc = tree->Launch();
  142. if (rc.IsError()) {
  143. MS_LOG(ERROR) << "Return code error detected during tree launch: " << ".";
  144. EXPECT_TRUE(false);
  145. } else {
  146. DatasetIterator di(tree);
  147. TensorMap tensor_map;
  148. di.GetNextAsMap(&tensor_map);
  149. EXPECT_TRUE(rc.IsOk());
  150. uint64_t i = 0;
  151. int32_t label = 0;
  152. double priority = 0;
  153. while (tensor_map.size() != 0) {
  154. tensor_map["label"]->GetItemAt<int32_t>(&label, {});
  155. tensor_map["_priority"]->GetItemAt<double>(&priority, {});
  156. MS_LOG(DEBUG) << "row: " << i << "\t" << tensor_map["image"]->shape() << "label:" << label << "label shape"
  157. << tensor_map["label"] << "priority: " << priority << "\n";
  158. i++;
  159. di.GetNextAsMap(&tensor_map);
  160. }
  161. MS_LOG(INFO) << "got rows" << i << "\n";
  162. EXPECT_TRUE(i == 14);
  163. }
  164. }
  165. TEST_F(MindDataTestAlbum, TestSequentialAlbumWithFullSchema) {
  166. std::string folder_path = datasets_root_path_ + "/testAlbum/images";
  167. // add the priority column
  168. std::string schema_file = datasets_root_path_ + "/testAlbum/fullSchema.json";
  169. auto tree = Build({AlbumSchema(16, 2, 32, folder_path, schema_file), Repeat(2)});
  170. tree->Prepare();
  171. Status rc = tree->Launch();
  172. if (rc.IsError()) {
  173. MS_LOG(ERROR) << "Return code error detected during tree launch: " << ".";
  174. EXPECT_TRUE(false);
  175. } else {
  176. DatasetIterator di(tree);
  177. TensorMap tensor_map;
  178. di.GetNextAsMap(&tensor_map);
  179. EXPECT_TRUE(rc.IsOk());
  180. uint64_t i = 0;
  181. int32_t label = 0;
  182. double priority = 0;
  183. int64_t id = 0;
  184. while (tensor_map.size() != 0) {
  185. tensor_map["label"]->GetItemAt<int32_t>(&label, {});
  186. tensor_map["_priority"]->GetItemAt<double>(&priority, {});
  187. tensor_map["id"]->GetItemAt<int64_t>(&id, {});
  188. MS_LOG(ERROR) << "row: " << i << "\t" << tensor_map["image"]->shape() << "label:" << label << "label shape"
  189. << tensor_map["label"] << "priority: " << priority << " embedding : " <<
  190. tensor_map["_embedding"]->shape() << " id: " << id << "\n";
  191. i++;
  192. di.GetNextAsMap(&tensor_map);
  193. }
  194. MS_LOG(INFO) << "got rows" << i << "\n";
  195. EXPECT_TRUE(i == 14);
  196. }
  197. }