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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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/subset_random_sampler.h"
  26. #include "dataset/util/de_error.h"
  27. #include "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::unique_ptr<Sampler> sampler = nullptr,
  40. std::map<std::string, int32_t> map = {}, uint64_t num_samples = 0,
  41. 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. .SetNumSamples(num_samples).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. std::unique_ptr<Sampler> sampler = std::make_unique<SubsetRandomSampler>(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. uint32_t label = 0;
  96. while (tensor_map.size() != 0) {
  97. tensor_map["label"]->GetItemAt<uint32_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. uint32_t label = 0;
  125. while (tensor_map.size() != 0) {
  126. tensor_map["label"]->GetItemAt<uint32_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. auto tree = Build({Manifest(16, 2, 32, file, "train", nullptr, {}, 1), Repeat(4)});
  138. tree->Prepare();
  139. Status rc = tree->Launch();
  140. if (rc.IsError()) {
  141. MS_LOG(ERROR) << "Return code error detected during tree launch: " << rc.ToString() << ".";
  142. EXPECT_TRUE(false);
  143. } else {
  144. DatasetIterator di(tree);
  145. TensorMap tensor_map;
  146. di.GetNextAsMap(&tensor_map);
  147. EXPECT_TRUE(rc.IsOk());
  148. uint64_t i = 0;
  149. uint32_t label = 0;
  150. while (tensor_map.size() != 0) {
  151. tensor_map["label"]->GetItemAt<uint32_t>(&label, {});
  152. EXPECT_TRUE(0 == label);
  153. MS_LOG(DEBUG) << "row: " << i << "\t" << tensor_map["image"]->shape() << "label:" << label << "\n";
  154. i++;
  155. di.GetNextAsMap(&tensor_map);
  156. }
  157. EXPECT_TRUE(i == 4);
  158. }
  159. }
  160. TEST_F(MindDataTestManifest, MindDataTestManifestEval) {
  161. std::string file = datasets_root_path_ + "/testManifestData/cpp.json";
  162. auto tree = Build({Manifest(16, 2, 32, file, "eval", nullptr, {}, 1)});
  163. tree->Prepare();
  164. Status rc = tree->Launch();
  165. if (rc.IsError()) {
  166. MS_LOG(ERROR) << "Return code error detected during tree launch: " << rc.ToString() << ".";
  167. EXPECT_TRUE(false);
  168. } else {
  169. DatasetIterator di(tree);
  170. TensorMap tensor_map;
  171. di.GetNextAsMap(&tensor_map);
  172. EXPECT_TRUE(rc.IsOk());
  173. uint64_t i = 0;
  174. uint32_t label = 0;
  175. while (tensor_map.size() != 0) {
  176. tensor_map["label"]->GetItemAt<uint32_t>(&label, {});
  177. EXPECT_TRUE(0 == label);
  178. MS_LOG(DEBUG) << "row: " << i << "\t" << tensor_map["image"]->shape() << "label:" << label << "\n";
  179. i++;
  180. di.GetNextAsMap(&tensor_map);
  181. }
  182. EXPECT_TRUE(i == 1);
  183. }
  184. }