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 7.8 kB

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