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.

voc_op_test.cc 6.7 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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/voc_op.h"
  25. #include "minddata/dataset/engine/datasetops/source/sampler/distributed_sampler.h"
  26. #include "minddata/dataset/engine/datasetops/source/sampler/pk_sampler.h"
  27. #include "minddata/dataset/engine/datasetops/source/sampler/random_sampler.h"
  28. #include "minddata/dataset/engine/datasetops/source/sampler/sampler.h"
  29. #include "minddata/dataset/engine/datasetops/source/sampler/sequential_sampler.h"
  30. #include "minddata/dataset/engine/datasetops/source/sampler/subset_random_sampler.h"
  31. #include "minddata/dataset/engine/datasetops/source/sampler/weighted_random_sampler.h"
  32. #include "minddata/dataset/util/path.h"
  33. #include "minddata/dataset/util/status.h"
  34. #include "gtest/gtest.h"
  35. #include "utils/log_adapter.h"
  36. #include "securec.h"
  37. namespace common = mindspore::common;
  38. using namespace mindspore::dataset;
  39. using mindspore::LogStream;
  40. using mindspore::ExceptionType::NoExceptionType;
  41. using mindspore::MsLogLevel::ERROR;
  42. std::shared_ptr<BatchOp> Batch(int batch_size = 1, bool drop = false);
  43. std::shared_ptr<ExecutionTree> Build(std::vector<std::shared_ptr<DatasetOp>> ops);
  44. class MindDataTestVOCOp : public UT::DatasetOpTesting {
  45. protected:
  46. };
  47. TEST_F(MindDataTestVOCOp, TestVOCDetection) {
  48. // Start with an empty execution tree
  49. auto my_tree = std::make_shared<ExecutionTree>();
  50. std::string dataset_path;
  51. dataset_path = datasets_root_path_ + "/testVOC2012";
  52. std::string task_type("Detection");
  53. std::string task_mode("train");
  54. std::shared_ptr<VOCOp> my_voc_op;
  55. VOCOp::Builder builder;
  56. Status rc = builder.SetDir(dataset_path).SetTask(task_type).SetUsage(task_mode).Build(&my_voc_op);
  57. ASSERT_TRUE(rc.IsOk());
  58. rc = my_tree->AssociateNode(my_voc_op);
  59. ASSERT_TRUE(rc.IsOk());
  60. rc = my_tree->AssignRoot(my_voc_op);
  61. ASSERT_TRUE(rc.IsOk());
  62. MS_LOG(DEBUG) << "Launch tree and begin iteration.";
  63. rc = my_tree->Prepare();
  64. ASSERT_TRUE(rc.IsOk());
  65. rc = my_tree->Launch();
  66. ASSERT_TRUE(rc.IsOk());
  67. // Start the loop of reading tensors from our pipeline
  68. DatasetIterator di(my_tree);
  69. TensorRow tensor_list;
  70. rc = di.FetchNextTensorRow(&tensor_list);
  71. ASSERT_TRUE(rc.IsOk());
  72. int row_count = 0;
  73. while (!tensor_list.empty()) {
  74. MS_LOG(DEBUG) << "Row display for row #: " << row_count << ".";
  75. // Display the tensor by calling the printer on it
  76. for (int i = 0; i < tensor_list.size(); i++) {
  77. std::ostringstream ss;
  78. ss << "(" << tensor_list[i] << "): " << *tensor_list[i] << std::endl;
  79. MS_LOG(DEBUG) << "Tensor print: " << ss.str() << ".";
  80. }
  81. rc = di.FetchNextTensorRow(&tensor_list);
  82. ASSERT_TRUE(rc.IsOk());
  83. row_count++;
  84. }
  85. ASSERT_EQ(row_count, 9);
  86. }
  87. TEST_F(MindDataTestVOCOp, TestVOCSegmentation) {
  88. // Start with an empty execution tree
  89. auto my_tree = std::make_shared<ExecutionTree>();
  90. std::string dataset_path;
  91. dataset_path = datasets_root_path_ + "/testVOC2012";
  92. std::string task_type("Segmentation");
  93. std::string task_mode("train");
  94. std::shared_ptr<VOCOp> my_voc_op;
  95. VOCOp::Builder builder;
  96. Status rc = builder.SetDir(dataset_path).SetTask(task_type).SetUsage(task_mode).Build(&my_voc_op);
  97. ASSERT_TRUE(rc.IsOk());
  98. rc = my_tree->AssociateNode(my_voc_op);
  99. ASSERT_TRUE(rc.IsOk());
  100. rc = my_tree->AssignRoot(my_voc_op);
  101. ASSERT_TRUE(rc.IsOk());
  102. MS_LOG(DEBUG) << "Launch tree and begin iteration.";
  103. rc = my_tree->Prepare();
  104. ASSERT_TRUE(rc.IsOk());
  105. rc = my_tree->Launch();
  106. ASSERT_TRUE(rc.IsOk());
  107. // Start the loop of reading tensors from our pipeline
  108. DatasetIterator di(my_tree);
  109. TensorRow tensor_list;
  110. rc = di.FetchNextTensorRow(&tensor_list);
  111. ASSERT_TRUE(rc.IsOk());
  112. int row_count = 0;
  113. while (!tensor_list.empty()) {
  114. MS_LOG(DEBUG) << "Row display for row #: " << row_count << ".";
  115. // Display the tensor by calling the printer on it
  116. for (int i = 0; i < tensor_list.size(); i++) {
  117. std::ostringstream ss;
  118. ss << "(" << tensor_list[i] << "): " << *tensor_list[i] << std::endl;
  119. MS_LOG(DEBUG) << "Tensor print: " << ss.str() << ".";
  120. }
  121. rc = di.FetchNextTensorRow(&tensor_list);
  122. ASSERT_TRUE(rc.IsOk());
  123. row_count++;
  124. }
  125. ASSERT_EQ(row_count, 10);
  126. }
  127. TEST_F(MindDataTestVOCOp, TestVOCClassIndex) {
  128. // Start with an empty execution tree
  129. auto my_tree = std::make_shared<ExecutionTree>();
  130. std::string dataset_path;
  131. dataset_path = datasets_root_path_ + "/testVOC2012";
  132. std::string task_type("Detection");
  133. std::string task_mode("train");
  134. std::map<std::string, int32_t> class_index;
  135. class_index["car"] = 0;
  136. class_index["cat"] = 1;
  137. class_index["train"] = 5;
  138. std::shared_ptr<VOCOp> my_voc_op;
  139. VOCOp::Builder builder;
  140. Status rc =
  141. builder.SetDir(dataset_path).SetTask(task_type).SetUsage(task_mode).SetClassIndex(class_index).Build(&my_voc_op);
  142. ASSERT_TRUE(rc.IsOk());
  143. rc = my_tree->AssociateNode(my_voc_op);
  144. ASSERT_TRUE(rc.IsOk());
  145. rc = my_tree->AssignRoot(my_voc_op);
  146. ASSERT_TRUE(rc.IsOk());
  147. MS_LOG(DEBUG) << "Launch tree and begin iteration.";
  148. rc = my_tree->Prepare();
  149. ASSERT_TRUE(rc.IsOk());
  150. rc = my_tree->Launch();
  151. ASSERT_TRUE(rc.IsOk());
  152. // Start the loop of reading tensors from our pipeline
  153. DatasetIterator di(my_tree);
  154. TensorRow tensor_list;
  155. rc = di.FetchNextTensorRow(&tensor_list);
  156. ASSERT_TRUE(rc.IsOk());
  157. int row_count = 0;
  158. while (!tensor_list.empty()) {
  159. MS_LOG(DEBUG) << "Row display for row #: " << row_count << ".";
  160. // Display the tensor by calling the printer on it
  161. for (int i = 0; i < tensor_list.size(); i++) {
  162. std::ostringstream ss;
  163. ss << "(" << tensor_list[i] << "): " << *tensor_list[i] << std::endl;
  164. MS_LOG(DEBUG) << "Tensor print: " << ss.str() << ".";
  165. }
  166. rc = di.FetchNextTensorRow(&tensor_list);
  167. ASSERT_TRUE(rc.IsOk());
  168. row_count++;
  169. }
  170. ASSERT_EQ(row_count, 6);
  171. }