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

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