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.

coco_op_test.cc 8.4 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. /**
  2. * Copyright 2020-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/coco_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<ExecutionTree> Build(std::vector<std::shared_ptr<DatasetOp>> ops);
  44. class MindDataTestCocoOp : public UT::DatasetOpTesting {
  45. protected:
  46. };
  47. TEST_F(MindDataTestCocoOp, TestCocoDetection) {
  48. // Start with an empty execution tree
  49. auto my_tree = std::make_shared<ExecutionTree>();
  50. std::string dataset_path, annotation_path;
  51. dataset_path = datasets_root_path_ + "/testCOCO/train/";
  52. annotation_path = datasets_root_path_ + "/testCOCO/annotations/train.json";
  53. std::string task("Detection");
  54. std::shared_ptr<CocoOp> my_coco_op;
  55. CocoOp::Builder builder;
  56. Status rc = builder.SetDir(dataset_path)
  57. .SetFile(annotation_path)
  58. .SetTask(task)
  59. .Build(&my_coco_op);
  60. ASSERT_TRUE(rc.IsOk());
  61. rc = my_tree->AssociateNode(my_coco_op);
  62. ASSERT_TRUE(rc.IsOk());
  63. rc = my_tree->AssignRoot(my_coco_op);
  64. ASSERT_TRUE(rc.IsOk());
  65. MS_LOG(DEBUG) << "Launch tree and begin iteration.";
  66. rc = my_tree->Prepare();
  67. ASSERT_TRUE(rc.IsOk());
  68. rc = my_tree->Launch();
  69. ASSERT_TRUE(rc.IsOk());
  70. // Start the loop of reading tensors from our pipeline
  71. DatasetIterator di(my_tree);
  72. TensorRow tensor_list;
  73. rc = di.FetchNextTensorRow(&tensor_list);
  74. ASSERT_TRUE(rc.IsOk());
  75. int row_count = 0;
  76. while (!tensor_list.empty()) {
  77. MS_LOG(DEBUG) << "Row display for row #: " << row_count << ".";
  78. //Display the tensor by calling the printer on it
  79. for (int i = 0; i < tensor_list.size(); i++) {
  80. std::ostringstream ss;
  81. ss << "(" << tensor_list[i] << "): " << *tensor_list[i] << std::endl;
  82. MS_LOG(DEBUG) << "Tensor print: " << ss.str() << ".";
  83. }
  84. rc = di.FetchNextTensorRow(&tensor_list);
  85. ASSERT_TRUE(rc.IsOk());
  86. row_count++;
  87. }
  88. ASSERT_EQ(row_count, 6);
  89. }
  90. TEST_F(MindDataTestCocoOp, TestCocoStuff) {
  91. // Start with an empty execution tree
  92. auto my_tree = std::make_shared<ExecutionTree>();
  93. std::string dataset_path, annotation_path;
  94. dataset_path = datasets_root_path_ + "/testCOCO/train/";
  95. annotation_path = datasets_root_path_ + "/testCOCO/annotations/train.json";
  96. std::string task("Stuff");
  97. std::shared_ptr<CocoOp> my_coco_op;
  98. CocoOp::Builder builder;
  99. Status rc = builder.SetDir(dataset_path)
  100. .SetFile(annotation_path)
  101. .SetTask(task)
  102. .Build(&my_coco_op);
  103. ASSERT_TRUE(rc.IsOk());
  104. rc = my_tree->AssociateNode(my_coco_op);
  105. ASSERT_TRUE(rc.IsOk());
  106. rc = my_tree->AssignRoot(my_coco_op);
  107. ASSERT_TRUE(rc.IsOk());
  108. MS_LOG(DEBUG) << "Launch tree and begin iteration.";
  109. rc = my_tree->Prepare();
  110. ASSERT_TRUE(rc.IsOk());
  111. rc = my_tree->Launch();
  112. ASSERT_TRUE(rc.IsOk());
  113. // Start the loop of reading tensors from our pipeline
  114. DatasetIterator di(my_tree);
  115. TensorRow tensor_list;
  116. rc = di.FetchNextTensorRow(&tensor_list);
  117. ASSERT_TRUE(rc.IsOk());
  118. int row_count = 0;
  119. while (!tensor_list.empty()) {
  120. MS_LOG(DEBUG) << "Row display for row #: " << row_count << ".";
  121. //Display the tensor by calling the printer on it
  122. for (int i = 0; i < tensor_list.size(); i++) {
  123. std::ostringstream ss;
  124. ss << "(" << tensor_list[i] << "): " << *tensor_list[i] << std::endl;
  125. MS_LOG(DEBUG) << "Tensor print: " << ss.str() << ".";
  126. }
  127. rc = di.FetchNextTensorRow(&tensor_list);
  128. ASSERT_TRUE(rc.IsOk());
  129. row_count++;
  130. }
  131. ASSERT_EQ(row_count, 6);
  132. }
  133. TEST_F(MindDataTestCocoOp, TestCocoKeypoint) {
  134. // Start with an empty execution tree
  135. auto my_tree = std::make_shared<ExecutionTree>();
  136. std::string dataset_path, annotation_path;
  137. dataset_path = datasets_root_path_ + "/testCOCO/train/";
  138. annotation_path = datasets_root_path_ + "/testCOCO/annotations/key_point.json";
  139. std::string task("Keypoint");
  140. std::shared_ptr<CocoOp> my_coco_op;
  141. CocoOp::Builder builder;
  142. Status rc = builder.SetDir(dataset_path)
  143. .SetFile(annotation_path)
  144. .SetTask(task)
  145. .Build(&my_coco_op);
  146. ASSERT_TRUE(rc.IsOk());
  147. rc = my_tree->AssociateNode(my_coco_op);
  148. ASSERT_TRUE(rc.IsOk());
  149. rc = my_tree->AssignRoot(my_coco_op);
  150. ASSERT_TRUE(rc.IsOk());
  151. MS_LOG(DEBUG) << "Launch tree and begin iteration.";
  152. rc = my_tree->Prepare();
  153. ASSERT_TRUE(rc.IsOk());
  154. rc = my_tree->Launch();
  155. ASSERT_TRUE(rc.IsOk());
  156. // Start the loop of reading tensors from our pipeline
  157. DatasetIterator di(my_tree);
  158. TensorRow tensor_list;
  159. rc = di.FetchNextTensorRow(&tensor_list);
  160. ASSERT_TRUE(rc.IsOk());
  161. int row_count = 0;
  162. while (!tensor_list.empty()) {
  163. MS_LOG(DEBUG) << "Row display for row #: " << row_count << ".";
  164. //Display the tensor by calling the printer on it
  165. for (int i = 0; i < tensor_list.size(); i++) {
  166. std::ostringstream ss;
  167. ss << "(" << tensor_list[i] << "): " << *tensor_list[i] << std::endl;
  168. MS_LOG(DEBUG) << "Tensor print: " << ss.str() << ".";
  169. }
  170. rc = di.FetchNextTensorRow(&tensor_list);
  171. ASSERT_TRUE(rc.IsOk());
  172. row_count++;
  173. }
  174. ASSERT_EQ(row_count, 2);
  175. }
  176. TEST_F(MindDataTestCocoOp, TestCocoPanoptic) {
  177. // Start with an empty execution tree
  178. auto my_tree = std::make_shared<ExecutionTree>();
  179. std::string dataset_path, annotation_path;
  180. dataset_path = datasets_root_path_ + "/testCOCO/train/";
  181. annotation_path = datasets_root_path_ + "/testCOCO/annotations/panoptic.json";
  182. std::string task("Panoptic");
  183. std::shared_ptr<CocoOp> my_coco_op;
  184. CocoOp::Builder builder;
  185. Status rc = builder.SetDir(dataset_path)
  186. .SetFile(annotation_path)
  187. .SetTask(task)
  188. .Build(&my_coco_op);
  189. ASSERT_TRUE(rc.IsOk());
  190. rc = my_tree->AssociateNode(my_coco_op);
  191. ASSERT_TRUE(rc.IsOk());
  192. rc = my_tree->AssignRoot(my_coco_op);
  193. ASSERT_TRUE(rc.IsOk());
  194. MS_LOG(DEBUG) << "Launch tree and begin iteration.";
  195. rc = my_tree->Prepare();
  196. ASSERT_TRUE(rc.IsOk());
  197. rc = my_tree->Launch();
  198. ASSERT_TRUE(rc.IsOk());
  199. // Start the loop of reading tensors from our pipeline
  200. DatasetIterator di(my_tree);
  201. TensorRow tensor_list;
  202. rc = di.FetchNextTensorRow(&tensor_list);
  203. ASSERT_TRUE(rc.IsOk());
  204. int row_count = 0;
  205. while (!tensor_list.empty()) {
  206. MS_LOG(DEBUG) << "Row display for row #: " << row_count << ".";
  207. //Display the tensor by calling the printer on it
  208. for (int i = 0; i < tensor_list.size(); i++) {
  209. std::ostringstream ss;
  210. ss << "(" << tensor_list[i] << "): " << *tensor_list[i] << std::endl;
  211. MS_LOG(DEBUG) << "Tensor print: " << ss.str() << ".";
  212. }
  213. rc = di.FetchNextTensorRow(&tensor_list);
  214. ASSERT_TRUE(rc.IsOk());
  215. row_count++;
  216. }
  217. ASSERT_EQ(row_count, 2);
  218. }