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

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