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

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