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.

manifest_op_test.cc 7.7 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /**
  2. * Copyright 2019-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 <fstream>
  17. #include <iostream>
  18. #include <memory>
  19. #include <string>
  20. #include "common/common.h"
  21. #include "utils/ms_utils.h"
  22. #include "minddata/dataset/core/client.h"
  23. #include "minddata/dataset/core/global_context.h"
  24. #include "minddata/dataset/engine/datasetops/source/manifest_op.h"
  25. #include "minddata/dataset/engine/datasetops/source/sampler/sequential_sampler.h"
  26. #include "minddata/dataset/engine/datasetops/source/sampler/subset_random_sampler.h"
  27. #include "minddata/dataset/util/status.h"
  28. #include "gtest/gtest.h"
  29. #include "utils/log_adapter.h"
  30. #include "securec.h"
  31. namespace common = mindspore::common;
  32. using namespace mindspore::dataset;
  33. using mindspore::MsLogLevel::ERROR;
  34. using mindspore::ExceptionType::NoExceptionType;
  35. using mindspore::LogStream;
  36. std::shared_ptr<RepeatOp> Repeat(int repeatCnt);
  37. std::shared_ptr<ExecutionTree> Build(std::vector<std::shared_ptr<DatasetOp>> ops);
  38. std::shared_ptr<ManifestOp> Manifest(int32_t num_works, int32_t rows, int32_t conns, const std::string &file,
  39. std::string usage = "train", std::shared_ptr<SamplerRT> sampler = nullptr,
  40. std::map<std::string, int32_t> map = {}, bool decode = false) {
  41. std::shared_ptr<ManifestOp> so;
  42. ManifestOp::Builder builder;
  43. Status rc = builder.SetNumWorkers(num_works).SetManifestFile(file).SetRowsPerBuffer(
  44. rows).SetOpConnectorSize(conns).SetSampler(std::move(sampler)).SetClassIndex(map).SetDecode(decode)
  45. .SetUsage(usage).Build(&so);
  46. return so;
  47. }
  48. class MindDataTestManifest : public UT::DatasetOpTesting {
  49. protected:
  50. };
  51. TEST_F(MindDataTestManifest, TestSequentialManifestWithRepeat) {
  52. std::string file = datasets_root_path_ + "/testManifestData/cpp.json";
  53. auto op1 = Manifest(16, 2, 32, file);
  54. auto op2 = Repeat(2);
  55. op1->set_total_repeats(2);
  56. op1->set_num_repeats_per_epoch(2);
  57. auto tree = Build({op1, op2});
  58. tree->Prepare();
  59. uint32_t res[] = {0, 1, 0, 1};
  60. Status rc = tree->Launch();
  61. if (rc.IsError()) {
  62. MS_LOG(ERROR) << "Return code error detected during tree launch: " << rc.ToString() << ".";
  63. EXPECT_TRUE(false);
  64. } else {
  65. DatasetIterator di(tree);
  66. TensorMap tensor_map;
  67. di.GetNextAsMap(&tensor_map);
  68. EXPECT_TRUE(rc.IsOk());
  69. uint64_t i = 0;
  70. int32_t label = 0;
  71. while (tensor_map.size() != 0) {
  72. tensor_map["label"]->GetItemAt<int32_t>(&label, {});
  73. EXPECT_TRUE(res[i] == label);
  74. MS_LOG(DEBUG) << "row: " << i << "\t" << tensor_map["image"]->shape() << "label:" << label << "\n";
  75. i++;
  76. di.GetNextAsMap(&tensor_map);
  77. }
  78. EXPECT_TRUE(i == 4);
  79. }
  80. }
  81. TEST_F(MindDataTestManifest, TestSubsetRandomSamplerManifest) {
  82. std::vector<int64_t> indices({1});
  83. int64_t num_samples = 0;
  84. std::shared_ptr<SamplerRT> sampler = std::make_shared<SubsetRandomSamplerRT>(num_samples, indices);
  85. std::string file = datasets_root_path_ + "/testManifestData/cpp.json";
  86. // Expect 6 samples for label 0 and 1
  87. auto tree = Build({Manifest(16, 2, 32, file, "train", std::move(sampler))});
  88. tree->Prepare();
  89. Status rc = tree->Launch();
  90. if (rc.IsError()) {
  91. MS_LOG(ERROR) << "Return code error detected during tree launch: " << rc.ToString() << ".";
  92. EXPECT_TRUE(false);
  93. } else {
  94. DatasetIterator di(tree);
  95. TensorMap tensor_map;
  96. rc = di.GetNextAsMap(&tensor_map);
  97. EXPECT_TRUE(rc.IsOk());
  98. uint64_t i = 0;
  99. int32_t label = 0;
  100. while (tensor_map.size() != 0) {
  101. tensor_map["label"]->GetItemAt<int32_t>(&label, {});
  102. i++;
  103. di.GetNextAsMap(&tensor_map);
  104. EXPECT_EQ(label, 1);
  105. }
  106. EXPECT_TRUE(i == 1);
  107. }
  108. }
  109. TEST_F(MindDataTestManifest, MindDataTestManifestClassIndex) {
  110. std::string file = datasets_root_path_ + "/testManifestData/cpp.json";
  111. std::map<std::string, int32_t> map;
  112. map["cat"] = 111; // forward slash is not good, but we need to add this somewhere, also in windows, its a '\'
  113. map["dog"] = 222; // forward slash is not good, but we need to add this somewhere, also in windows, its a '\'
  114. map["wrong folder name"] = 1234; // this is skipped
  115. auto tree = Build({Manifest(16, 2, 32, file, "train", nullptr, map)});
  116. uint64_t res[2] = {111, 222};
  117. tree->Prepare();
  118. Status rc = tree->Launch();
  119. if (rc.IsError()) {
  120. MS_LOG(ERROR) << "Return code error detected during tree launch: " << rc.ToString() << ".";
  121. EXPECT_TRUE(false);
  122. } else {
  123. DatasetIterator di(tree);
  124. TensorMap tensor_map;
  125. di.GetNextAsMap(&tensor_map);
  126. EXPECT_TRUE(rc.IsOk());
  127. uint64_t i = 0;
  128. int32_t label = 0;
  129. while (tensor_map.size() != 0) {
  130. tensor_map["label"]->GetItemAt<int32_t>(&label, {});
  131. EXPECT_TRUE(label == res[i]);
  132. MS_LOG(DEBUG) << "row: " << i << "\t" << tensor_map["image"]->shape() << "label:" << label << "\n";
  133. i++;
  134. di.GetNextAsMap(&tensor_map);
  135. }
  136. EXPECT_TRUE(i == 2);
  137. }
  138. }
  139. TEST_F(MindDataTestManifest, MindDataTestManifestNumSamples) {
  140. std::string file = datasets_root_path_ + "/testManifestData/cpp.json";
  141. int64_t num_samples = 1;
  142. int64_t start_index = 0;
  143. auto seq_sampler = std::make_shared<SequentialSamplerRT>(num_samples, start_index);
  144. auto op1 = Manifest(16, 2, 32, file, "train", std::move(seq_sampler), {});
  145. auto op2 = Repeat(4);
  146. op1->set_total_repeats(4);
  147. op1->set_num_repeats_per_epoch(4);
  148. auto tree = Build({op1, op2});
  149. tree->Prepare();
  150. Status rc = tree->Launch();
  151. if (rc.IsError()) {
  152. MS_LOG(ERROR) << "Return code error detected during tree launch: " << rc.ToString() << ".";
  153. EXPECT_TRUE(false);
  154. } else {
  155. DatasetIterator di(tree);
  156. TensorMap tensor_map;
  157. di.GetNextAsMap(&tensor_map);
  158. EXPECT_TRUE(rc.IsOk());
  159. uint64_t i = 0;
  160. int32_t label = 0;
  161. while (tensor_map.size() != 0) {
  162. tensor_map["label"]->GetItemAt<int32_t>(&label, {});
  163. EXPECT_TRUE(0 == label);
  164. MS_LOG(DEBUG) << "row: " << i << "\t" << tensor_map["image"]->shape() << "label:" << label << "\n";
  165. i++;
  166. di.GetNextAsMap(&tensor_map);
  167. }
  168. EXPECT_TRUE(i == 4);
  169. }
  170. }
  171. TEST_F(MindDataTestManifest, MindDataTestManifestEval) {
  172. std::string file = datasets_root_path_ + "/testManifestData/cpp.json";
  173. int64_t num_samples = 1;
  174. int64_t start_index = 0;
  175. auto seq_sampler = std::make_shared<SequentialSamplerRT>(num_samples, start_index);
  176. auto tree = Build({Manifest(16, 2, 32, file, "eval", std::move(seq_sampler), {})});
  177. tree->Prepare();
  178. Status rc = tree->Launch();
  179. if (rc.IsError()) {
  180. MS_LOG(ERROR) << "Return code error detected during tree launch: " << rc.ToString() << ".";
  181. EXPECT_TRUE(false);
  182. } else {
  183. DatasetIterator di(tree);
  184. TensorMap tensor_map;
  185. di.GetNextAsMap(&tensor_map);
  186. EXPECT_TRUE(rc.IsOk());
  187. uint64_t i = 0;
  188. int32_t label = 0;
  189. while (tensor_map.size() != 0) {
  190. tensor_map["label"]->GetItemAt<int32_t>(&label, {});
  191. EXPECT_TRUE(0 == label);
  192. MS_LOG(DEBUG) << "row: " << i << "\t" << tensor_map["image"]->shape() << "label:" << label << "\n";
  193. i++;
  194. di.GetNextAsMap(&tensor_map);
  195. }
  196. EXPECT_TRUE(i == 1);
  197. }
  198. }