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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /**
  2. * Copyright 2019 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<Sampler> 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 tree = Build({Manifest(16, 2, 32, file), Repeat(2)});
  54. tree->Prepare();
  55. uint32_t res[] = {0, 1, 0, 1};
  56. Status rc = tree->Launch();
  57. if (rc.IsError()) {
  58. MS_LOG(ERROR) << "Return code error detected during tree launch: " << rc.ToString() << ".";
  59. EXPECT_TRUE(false);
  60. } else {
  61. DatasetIterator di(tree);
  62. TensorMap tensor_map;
  63. di.GetNextAsMap(&tensor_map);
  64. EXPECT_TRUE(rc.IsOk());
  65. uint64_t i = 0;
  66. int32_t label = 0;
  67. while (tensor_map.size() != 0) {
  68. tensor_map["label"]->GetItemAt<int32_t>(&label, {});
  69. EXPECT_TRUE(res[i] == label);
  70. MS_LOG(DEBUG) << "row: " << i << "\t" << tensor_map["image"]->shape() << "label:" << label << "\n";
  71. i++;
  72. di.GetNextAsMap(&tensor_map);
  73. }
  74. EXPECT_TRUE(i == 4);
  75. }
  76. }
  77. TEST_F(MindDataTestManifest, TestSubsetRandomSamplerManifest) {
  78. std::vector<int64_t> indices({1});
  79. int64_t num_samples = 0;
  80. std::shared_ptr<Sampler> sampler = std::make_shared<SubsetRandomSampler>(num_samples, indices);
  81. std::string file = datasets_root_path_ + "/testManifestData/cpp.json";
  82. // Expect 6 samples for label 0 and 1
  83. auto tree = Build({Manifest(16, 2, 32, file, "train", std::move(sampler))});
  84. tree->Prepare();
  85. Status rc = tree->Launch();
  86. if (rc.IsError()) {
  87. MS_LOG(ERROR) << "Return code error detected during tree launch: " << rc.ToString() << ".";
  88. EXPECT_TRUE(false);
  89. } else {
  90. DatasetIterator di(tree);
  91. TensorMap tensor_map;
  92. rc = di.GetNextAsMap(&tensor_map);
  93. EXPECT_TRUE(rc.IsOk());
  94. uint64_t i = 0;
  95. int32_t label = 0;
  96. while (tensor_map.size() != 0) {
  97. tensor_map["label"]->GetItemAt<int32_t>(&label, {});
  98. i++;
  99. di.GetNextAsMap(&tensor_map);
  100. EXPECT_EQ(label, 1);
  101. }
  102. EXPECT_TRUE(i == 1);
  103. }
  104. }
  105. TEST_F(MindDataTestManifest, MindDataTestManifestClassIndex) {
  106. std::string file = datasets_root_path_ + "/testManifestData/cpp.json";
  107. std::map<std::string, int32_t> map;
  108. map["cat"] = 111; // forward slash is not good, but we need to add this somewhere, also in windows, its a '\'
  109. map["dog"] = 222; // forward slash is not good, but we need to add this somewhere, also in windows, its a '\'
  110. map["wrong folder name"] = 1234; // this is skipped
  111. auto tree = Build({Manifest(16, 2, 32, file, "train", nullptr, map)});
  112. uint64_t res[2] = {111, 222};
  113. tree->Prepare();
  114. Status rc = tree->Launch();
  115. if (rc.IsError()) {
  116. MS_LOG(ERROR) << "Return code error detected during tree launch: " << rc.ToString() << ".";
  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. EXPECT_TRUE(label == res[i]);
  128. MS_LOG(DEBUG) << "row: " << i << "\t" << tensor_map["image"]->shape() << "label:" << label << "\n";
  129. i++;
  130. di.GetNextAsMap(&tensor_map);
  131. }
  132. EXPECT_TRUE(i == 2);
  133. }
  134. }
  135. TEST_F(MindDataTestManifest, MindDataTestManifestNumSamples) {
  136. std::string file = datasets_root_path_ + "/testManifestData/cpp.json";
  137. int64_t num_samples = 1;
  138. int64_t start_index = 0;
  139. auto seq_sampler = std::make_shared<SequentialSampler>(num_samples, start_index);
  140. auto tree = Build({Manifest(16, 2, 32, file, "train", std::move(seq_sampler), {}), Repeat(4)});
  141. tree->Prepare();
  142. Status rc = tree->Launch();
  143. if (rc.IsError()) {
  144. MS_LOG(ERROR) << "Return code error detected during tree launch: " << rc.ToString() << ".";
  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. while (tensor_map.size() != 0) {
  154. tensor_map["label"]->GetItemAt<int32_t>(&label, {});
  155. EXPECT_TRUE(0 == label);
  156. MS_LOG(DEBUG) << "row: " << i << "\t" << tensor_map["image"]->shape() << "label:" << label << "\n";
  157. i++;
  158. di.GetNextAsMap(&tensor_map);
  159. }
  160. EXPECT_TRUE(i == 4);
  161. }
  162. }
  163. TEST_F(MindDataTestManifest, MindDataTestManifestEval) {
  164. std::string file = datasets_root_path_ + "/testManifestData/cpp.json";
  165. int64_t num_samples = 1;
  166. int64_t start_index = 0;
  167. auto seq_sampler = std::make_shared<SequentialSampler>(num_samples, start_index);
  168. auto tree = Build({Manifest(16, 2, 32, file, "eval", std::move(seq_sampler), {})});
  169. tree->Prepare();
  170. Status rc = tree->Launch();
  171. if (rc.IsError()) {
  172. MS_LOG(ERROR) << "Return code error detected during tree launch: " << rc.ToString() << ".";
  173. EXPECT_TRUE(false);
  174. } else {
  175. DatasetIterator di(tree);
  176. TensorMap tensor_map;
  177. di.GetNextAsMap(&tensor_map);
  178. EXPECT_TRUE(rc.IsOk());
  179. uint64_t i = 0;
  180. int32_t label = 0;
  181. while (tensor_map.size() != 0) {
  182. tensor_map["label"]->GetItemAt<int32_t>(&label, {});
  183. EXPECT_TRUE(0 == label);
  184. MS_LOG(DEBUG) << "row: " << i << "\t" << tensor_map["image"]->shape() << "label:" << label << "\n";
  185. i++;
  186. di.GetNextAsMap(&tensor_map);
  187. }
  188. EXPECT_TRUE(i == 1);
  189. }
  190. }