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 7.0 kB

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