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

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