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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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,
  46. bool shuf = false, std::unique_ptr<Sampler> sampler = nullptr,
  47. bool decode = false) {
  48. std::shared_ptr<AlbumOp> so;
  49. AlbumOp::Builder builder;
  50. Status rc = builder.SetNumWorkers(num_works)
  51. .SetAlbumDir(path)
  52. .SetRowsPerBuffer(rows)
  53. .SetOpConnectorSize(conns)
  54. .SetExtensions({".json"})
  55. .SetSampler(std::move(sampler))
  56. .SetDecode(decode)
  57. .Build(&so);
  58. return so;
  59. }
  60. std::shared_ptr<AlbumOp> AlbumSchema(int64_t num_works, int64_t rows, int64_t conns, std::string path,
  61. std::string schema_file, std::vector<std::string> column_names = {},
  62. bool shuf = false, std::unique_ptr<Sampler> sampler = nullptr,
  63. bool decode = false) {
  64. std::shared_ptr<AlbumOp> so;
  65. AlbumOp::Builder builder;
  66. Status rc = builder.SetNumWorkers(num_works)
  67. .SetSchemaFile(schema_file)
  68. .SetColumnsToLoad(column_names)
  69. .SetAlbumDir(path)
  70. .SetRowsPerBuffer(rows)
  71. .SetOpConnectorSize(conns)
  72. .SetExtensions({".json"})
  73. .SetSampler(std::move(sampler))
  74. .SetDecode(decode)
  75. .Build(&so);
  76. return so;
  77. }
  78. class MindDataTestAlbum : public UT::DatasetOpTesting {
  79. protected:
  80. };
  81. TEST_F(MindDataTestAlbum, TestSequentialAlbumWithSchema) {
  82. std::string folder_path = datasets_root_path_ + "/testAlbum/images";
  83. std::string schema_file = datasets_root_path_ + "/testAlbum/datasetSchema.json";
  84. std::vector<std::string> column_names = {"image", "label", "id"};
  85. auto tree = Build({AlbumSchema(16, 2, 32, folder_path, schema_file, column_names, false), Repeat(2)});
  86. tree->Prepare();
  87. Status rc = tree->Launch();
  88. if (rc.IsError()) {
  89. MS_LOG(ERROR) << "Return code error detected during tree launch: " << ".";
  90. EXPECT_TRUE(false);
  91. } else {
  92. DatasetIterator di(tree);
  93. TensorMap tensor_map;
  94. di.GetNextAsMap(&tensor_map);
  95. EXPECT_TRUE(rc.IsOk());
  96. uint64_t i = 0;
  97. int32_t label = 0;
  98. while (tensor_map.size() != 0) {
  99. tensor_map["label"]->GetItemAt<int32_t>(&label, {});
  100. MS_LOG(DEBUG) << "row: " << i << "\t" << tensor_map["image"]->shape() << "label:" << label << "label shape"
  101. << tensor_map["label"] << "\n";
  102. i++;
  103. di.GetNextAsMap(&tensor_map);
  104. }
  105. MS_LOG(INFO) << "got rows" << i << "\n";
  106. EXPECT_TRUE(i == 14);
  107. }
  108. }
  109. TEST_F(MindDataTestAlbum, TestSequentialAlbumWithSchemaNoOrder) {
  110. std::string folder_path = datasets_root_path_ + "/testAlbum/images";
  111. std::string schema_file = datasets_root_path_ + "/testAlbum/datasetSchema.json";
  112. auto tree = Build({AlbumSchema(16, 2, 32, folder_path, schema_file), Repeat(2)});
  113. tree->Prepare();
  114. Status rc = tree->Launch();
  115. if (rc.IsError()) {
  116. MS_LOG(ERROR) << "Return code error detected during tree launch: " << ".";
  117. EXPECT_TRUE(false);
  118. } else {
  119. DatasetIterator di(tree);
  120. TensorMap tensor_map;
  121. di.GetNextAsMap(&tensor_map);
  122. EXPECT_TRUE(rc.IsOk());
  123. uint64_t i = 0;
  124. int32_t label = 0;
  125. while (tensor_map.size() != 0) {
  126. tensor_map["label"]->GetItemAt<int32_t>(&label, {});
  127. MS_LOG(DEBUG) << "row: " << i << "\t" << tensor_map["image"]->shape() << "label:" << label << "label shape"
  128. << tensor_map["label"] << "\n";
  129. i++;
  130. di.GetNextAsMap(&tensor_map);
  131. }
  132. MS_LOG(INFO) << "got rows" << i << "\n";
  133. EXPECT_TRUE(i == 14);
  134. }
  135. }
  136. TEST_F(MindDataTestAlbum, TestSequentialAlbumWithSchemaFloat) {
  137. std::string folder_path = datasets_root_path_ + "/testAlbum/images";
  138. // add the priority column
  139. std::string schema_file = datasets_root_path_ + "/testAlbum/floatSchema.json";
  140. auto tree = Build({AlbumSchema(16, 2, 32, folder_path, schema_file), Repeat(2)});
  141. tree->Prepare();
  142. Status rc = tree->Launch();
  143. if (rc.IsError()) {
  144. MS_LOG(ERROR) << "Return code error detected during tree launch: " << ".";
  145. EXPECT_TRUE(false);
  146. } else {
  147. DatasetIterator di(tree);
  148. TensorMap tensor_map;
  149. di.GetNextAsMap(&tensor_map);
  150. EXPECT_TRUE(rc.IsOk());
  151. uint64_t i = 0;
  152. int32_t label = 0;
  153. double priority = 0;
  154. while (tensor_map.size() != 0) {
  155. tensor_map["label"]->GetItemAt<int32_t>(&label, {});
  156. tensor_map["_priority"]->GetItemAt<double>(&priority, {});
  157. MS_LOG(DEBUG) << "row: " << i << "\t" << tensor_map["image"]->shape() << "label:" << label << "label shape"
  158. << tensor_map["label"] << "priority: " << priority << "\n";
  159. i++;
  160. di.GetNextAsMap(&tensor_map);
  161. }
  162. MS_LOG(INFO) << "got rows" << i << "\n";
  163. EXPECT_TRUE(i == 14);
  164. }
  165. }
  166. TEST_F(MindDataTestAlbum, TestSequentialAlbumWithFullSchema) {
  167. std::string folder_path = datasets_root_path_ + "/testAlbum/images";
  168. // add the priority column
  169. std::string schema_file = datasets_root_path_ + "/testAlbum/fullSchema.json";
  170. auto tree = Build({AlbumSchema(16, 2, 32, folder_path, schema_file), Repeat(2)});
  171. tree->Prepare();
  172. Status rc = tree->Launch();
  173. if (rc.IsError()) {
  174. MS_LOG(ERROR) << "Return code error detected during tree launch: " << ".";
  175. EXPECT_TRUE(false);
  176. } else {
  177. DatasetIterator di(tree);
  178. TensorMap tensor_map;
  179. di.GetNextAsMap(&tensor_map);
  180. EXPECT_TRUE(rc.IsOk());
  181. uint64_t i = 0;
  182. int32_t label = 0;
  183. double priority = 0;
  184. while (tensor_map.size() != 0) {
  185. tensor_map["label"]->GetItemAt<int32_t>(&label, {});
  186. tensor_map["_priority"]->GetItemAt<double>(&priority, {});
  187. MS_LOG(DEBUG) << "row: " << i << "\t" << tensor_map["image"]->shape() << "label:" << label << "label shape"
  188. << tensor_map["label"] << "priority: " << priority << " embedding : " <<
  189. tensor_map["_embedding"]->shape() << "\n";
  190. i++;
  191. di.GetNextAsMap(&tensor_map);
  192. }
  193. MS_LOG(INFO) << "got rows" << i << "\n";
  194. EXPECT_TRUE(i == 14);
  195. }
  196. }