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.9 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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)
  44. .SetManifestFile(file)
  45. .SetOpConnectorSize(conns)
  46. .SetSampler(std::move(sampler))
  47. .SetClassIndex(map)
  48. .SetDecode(decode)
  49. .SetUsage(usage)
  50. .Build(&so);
  51. return so;
  52. }
  53. class MindDataTestManifest : public UT::DatasetOpTesting {
  54. protected:
  55. };
  56. TEST_F(MindDataTestManifest, TestSequentialManifestWithRepeat) {
  57. std::string file = datasets_root_path_ + "/testManifestData/cpp.json";
  58. auto op1 = Manifest(16, 2, 32, file);
  59. auto op2 = Repeat(2);
  60. op1->set_total_repeats(2);
  61. op1->set_num_repeats_per_epoch(2);
  62. auto tree = Build({op1, op2});
  63. tree->Prepare();
  64. uint32_t res[] = {0, 1, 0, 1};
  65. Status rc = tree->Launch();
  66. if (rc.IsError()) {
  67. MS_LOG(ERROR) << "Return code error detected during tree launch: " << rc.ToString() << ".";
  68. EXPECT_TRUE(false);
  69. } else {
  70. DatasetIterator di(tree);
  71. TensorMap tensor_map;
  72. ASSERT_OK(di.GetNextAsMap(&tensor_map));
  73. EXPECT_TRUE(rc.IsOk());
  74. uint64_t i = 0;
  75. int32_t label = 0;
  76. while (tensor_map.size() != 0) {
  77. tensor_map["label"]->GetItemAt<int32_t>(&label, {});
  78. EXPECT_TRUE(res[i] == label);
  79. MS_LOG(DEBUG) << "row: " << i << "\t" << tensor_map["image"]->shape() << "label:" << label << "\n";
  80. i++;
  81. ASSERT_OK(di.GetNextAsMap(&tensor_map));
  82. }
  83. EXPECT_TRUE(i == 4);
  84. }
  85. }
  86. TEST_F(MindDataTestManifest, TestSubsetRandomSamplerManifest) {
  87. std::vector<int64_t> indices({1});
  88. int64_t num_samples = 0;
  89. std::shared_ptr<SamplerRT> sampler = std::make_shared<SubsetRandomSamplerRT>(num_samples, indices);
  90. std::string file = datasets_root_path_ + "/testManifestData/cpp.json";
  91. // Expect 6 samples for label 0 and 1
  92. auto tree = Build({Manifest(16, 2, 32, file, "train", std::move(sampler))});
  93. tree->Prepare();
  94. Status rc = tree->Launch();
  95. if (rc.IsError()) {
  96. MS_LOG(ERROR) << "Return code error detected during tree launch: " << rc.ToString() << ".";
  97. EXPECT_TRUE(false);
  98. } else {
  99. DatasetIterator di(tree);
  100. TensorMap tensor_map;
  101. rc = di.GetNextAsMap(&tensor_map);
  102. EXPECT_TRUE(rc.IsOk());
  103. uint64_t i = 0;
  104. int32_t label = 0;
  105. while (tensor_map.size() != 0) {
  106. tensor_map["label"]->GetItemAt<int32_t>(&label, {});
  107. i++;
  108. ASSERT_OK(di.GetNextAsMap(&tensor_map));
  109. EXPECT_EQ(label, 1);
  110. }
  111. EXPECT_TRUE(i == 1);
  112. }
  113. }
  114. TEST_F(MindDataTestManifest, MindDataTestManifestClassIndex) {
  115. std::string file = datasets_root_path_ + "/testManifestData/cpp.json";
  116. std::map<std::string, int32_t> map;
  117. map["cat"] = 111; // forward slash is not good, but we need to add this somewhere, also in windows, its a '\'
  118. map["dog"] = 222; // forward slash is not good, but we need to add this somewhere, also in windows, its a '\'
  119. map["wrong folder name"] = 1234; // this is skipped
  120. auto tree = Build({Manifest(16, 2, 32, file, "train", nullptr, map)});
  121. uint64_t res[2] = {111, 222};
  122. tree->Prepare();
  123. Status rc = tree->Launch();
  124. if (rc.IsError()) {
  125. MS_LOG(ERROR) << "Return code error detected during tree launch: " << rc.ToString() << ".";
  126. EXPECT_TRUE(false);
  127. } else {
  128. DatasetIterator di(tree);
  129. TensorMap tensor_map;
  130. ASSERT_OK(di.GetNextAsMap(&tensor_map));
  131. EXPECT_TRUE(rc.IsOk());
  132. uint64_t i = 0;
  133. int32_t label = 0;
  134. while (tensor_map.size() != 0) {
  135. tensor_map["label"]->GetItemAt<int32_t>(&label, {});
  136. EXPECT_TRUE(label == res[i]);
  137. MS_LOG(DEBUG) << "row: " << i << "\t" << tensor_map["image"]->shape() << "label:" << label << "\n";
  138. i++;
  139. ASSERT_OK(di.GetNextAsMap(&tensor_map));
  140. }
  141. EXPECT_TRUE(i == 2);
  142. }
  143. }
  144. TEST_F(MindDataTestManifest, MindDataTestManifestNumSamples) {
  145. std::string file = datasets_root_path_ + "/testManifestData/cpp.json";
  146. int64_t num_samples = 1;
  147. int64_t start_index = 0;
  148. auto seq_sampler = std::make_shared<SequentialSamplerRT>(num_samples, start_index);
  149. auto op1 = Manifest(16, 2, 32, file, "train", std::move(seq_sampler), {});
  150. auto op2 = Repeat(4);
  151. op1->set_total_repeats(4);
  152. op1->set_num_repeats_per_epoch(4);
  153. auto tree = Build({op1, op2});
  154. tree->Prepare();
  155. Status rc = tree->Launch();
  156. if (rc.IsError()) {
  157. MS_LOG(ERROR) << "Return code error detected during tree launch: " << rc.ToString() << ".";
  158. EXPECT_TRUE(false);
  159. } else {
  160. DatasetIterator di(tree);
  161. TensorMap tensor_map;
  162. ASSERT_OK(di.GetNextAsMap(&tensor_map));
  163. EXPECT_TRUE(rc.IsOk());
  164. uint64_t i = 0;
  165. int32_t label = 0;
  166. while (tensor_map.size() != 0) {
  167. tensor_map["label"]->GetItemAt<int32_t>(&label, {});
  168. EXPECT_TRUE(0 == label);
  169. MS_LOG(DEBUG) << "row: " << i << "\t" << tensor_map["image"]->shape() << "label:" << label << "\n";
  170. i++;
  171. ASSERT_OK(di.GetNextAsMap(&tensor_map));
  172. }
  173. EXPECT_TRUE(i == 4);
  174. }
  175. }
  176. TEST_F(MindDataTestManifest, MindDataTestManifestEval) {
  177. std::string file = datasets_root_path_ + "/testManifestData/cpp.json";
  178. int64_t num_samples = 1;
  179. int64_t start_index = 0;
  180. auto seq_sampler = std::make_shared<SequentialSamplerRT>(num_samples, start_index);
  181. auto tree = Build({Manifest(16, 2, 32, file, "eval", std::move(seq_sampler), {})});
  182. tree->Prepare();
  183. Status rc = tree->Launch();
  184. if (rc.IsError()) {
  185. MS_LOG(ERROR) << "Return code error detected during tree launch: " << rc.ToString() << ".";
  186. EXPECT_TRUE(false);
  187. } else {
  188. DatasetIterator di(tree);
  189. TensorMap tensor_map;
  190. ASSERT_OK(di.GetNextAsMap(&tensor_map));
  191. EXPECT_TRUE(rc.IsOk());
  192. uint64_t i = 0;
  193. int32_t label = 0;
  194. while (tensor_map.size() != 0) {
  195. tensor_map["label"]->GetItemAt<int32_t>(&label, {});
  196. EXPECT_TRUE(0 == label);
  197. MS_LOG(DEBUG) << "row: " << i << "\t" << tensor_map["image"]->shape() << "label:" << label << "\n";
  198. i++;
  199. ASSERT_OK(di.GetNextAsMap(&tensor_map));
  200. }
  201. EXPECT_TRUE(i == 1);
  202. }
  203. }