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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. /**
  2. * Copyright 2020 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<RepeatOp> Repeat(int repeat_cnt);
  44. std::shared_ptr<ExecutionTree> Build(std::vector<std::shared_ptr<DatasetOp>> ops);
  45. class MindDataTestCocoOp : public UT::DatasetOpTesting {
  46. protected:
  47. };
  48. TEST_F(MindDataTestCocoOp, TestCocoDetection) {
  49. // Start with an empty execution tree
  50. auto my_tree = std::make_shared<ExecutionTree>();
  51. std::string dataset_path, annotation_path;
  52. dataset_path = datasets_root_path_ + "/testCOCO/train/";
  53. annotation_path = datasets_root_path_ + "/testCOCO/annotations/train.json";
  54. std::string task("Detection");
  55. std::shared_ptr<CocoOp> my_coco_op;
  56. CocoOp::Builder builder;
  57. Status rc = builder.SetDir(dataset_path)
  58. .SetFile(annotation_path)
  59. .SetTask(task)
  60. .Build(&my_coco_op);
  61. ASSERT_TRUE(rc.IsOk());
  62. rc = my_tree->AssociateNode(my_coco_op);
  63. ASSERT_TRUE(rc.IsOk());
  64. rc = my_tree->AssignRoot(my_coco_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, 6);
  90. }
  91. TEST_F(MindDataTestCocoOp, TestCocoStuff) {
  92. // Start with an empty execution tree
  93. auto my_tree = std::make_shared<ExecutionTree>();
  94. std::string dataset_path, annotation_path;
  95. dataset_path = datasets_root_path_ + "/testCOCO/train/";
  96. annotation_path = datasets_root_path_ + "/testCOCO/annotations/train.json";
  97. std::string task("Stuff");
  98. std::shared_ptr<CocoOp> my_coco_op;
  99. CocoOp::Builder builder;
  100. Status rc = builder.SetDir(dataset_path)
  101. .SetFile(annotation_path)
  102. .SetTask(task)
  103. .Build(&my_coco_op);
  104. ASSERT_TRUE(rc.IsOk());
  105. rc = my_tree->AssociateNode(my_coco_op);
  106. ASSERT_TRUE(rc.IsOk());
  107. rc = my_tree->AssignRoot(my_coco_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, 6);
  133. }
  134. TEST_F(MindDataTestCocoOp, TestCocoKeypoint) {
  135. // Start with an empty execution tree
  136. auto my_tree = std::make_shared<ExecutionTree>();
  137. std::string dataset_path, annotation_path;
  138. dataset_path = datasets_root_path_ + "/testCOCO/train/";
  139. annotation_path = datasets_root_path_ + "/testCOCO/annotations/key_point.json";
  140. std::string task("Keypoint");
  141. std::shared_ptr<CocoOp> my_coco_op;
  142. CocoOp::Builder builder;
  143. Status rc = builder.SetDir(dataset_path)
  144. .SetFile(annotation_path)
  145. .SetTask(task)
  146. .Build(&my_coco_op);
  147. ASSERT_TRUE(rc.IsOk());
  148. rc = my_tree->AssociateNode(my_coco_op);
  149. ASSERT_TRUE(rc.IsOk());
  150. rc = my_tree->AssignRoot(my_coco_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, 2);
  176. }
  177. TEST_F(MindDataTestCocoOp, TestCocoPanoptic) {
  178. // Start with an empty execution tree
  179. auto my_tree = std::make_shared<ExecutionTree>();
  180. std::string dataset_path, annotation_path;
  181. dataset_path = datasets_root_path_ + "/testCOCO/train/";
  182. annotation_path = datasets_root_path_ + "/testCOCO/annotations/panoptic.json";
  183. std::string task("Panoptic");
  184. std::shared_ptr<CocoOp> my_coco_op;
  185. CocoOp::Builder builder;
  186. Status rc = builder.SetDir(dataset_path)
  187. .SetFile(annotation_path)
  188. .SetTask(task)
  189. .Build(&my_coco_op);
  190. ASSERT_TRUE(rc.IsOk());
  191. rc = my_tree->AssociateNode(my_coco_op);
  192. ASSERT_TRUE(rc.IsOk());
  193. rc = my_tree->AssignRoot(my_coco_op);
  194. ASSERT_TRUE(rc.IsOk());
  195. MS_LOG(DEBUG) << "Launch tree and begin iteration.";
  196. rc = my_tree->Prepare();
  197. ASSERT_TRUE(rc.IsOk());
  198. rc = my_tree->Launch();
  199. ASSERT_TRUE(rc.IsOk());
  200. // Start the loop of reading tensors from our pipeline
  201. DatasetIterator di(my_tree);
  202. TensorRow tensor_list;
  203. rc = di.FetchNextTensorRow(&tensor_list);
  204. ASSERT_TRUE(rc.IsOk());
  205. int row_count = 0;
  206. while (!tensor_list.empty()) {
  207. MS_LOG(DEBUG) << "Row display for row #: " << row_count << ".";
  208. //Display the tensor by calling the printer on it
  209. for (int i = 0; i < tensor_list.size(); i++) {
  210. std::ostringstream ss;
  211. ss << "(" << tensor_list[i] << "): " << *tensor_list[i] << std::endl;
  212. MS_LOG(DEBUG) << "Tensor print: " << ss.str() << ".";
  213. }
  214. rc = di.FetchNextTensorRow(&tensor_list);
  215. ASSERT_TRUE(rc.IsOk());
  216. row_count++;
  217. }
  218. ASSERT_EQ(row_count, 2);
  219. }