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

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