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

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