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.

map_op_test.cc 26 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739
  1. /**
  2. * Copyright 2019 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 <vector>
  19. #include "common/common.h"
  20. #include "minddata/dataset/core/client.h"
  21. #include "minddata/dataset/core/tensor.h"
  22. #include "minddata/dataset/engine/datasetops/source/image_folder_op.h"
  23. #include "minddata/dataset/kernels/image/decode_op.h"
  24. #include "minddata/dataset/kernels/image/resize_op.h"
  25. #include "minddata/dataset/kernels/tensor_op.h"
  26. #include "utils/log_adapter.h"
  27. using namespace mindspore::dataset;
  28. using mindspore::LogStream;
  29. using mindspore::MsLogLevel::INFO;
  30. namespace mindspore {
  31. namespace dataset {
  32. namespace test {
  33. class NoOp : public TensorOp {
  34. public:
  35. NoOp(){};
  36. ~NoOp(){};
  37. Status Compute(const std::shared_ptr<Tensor> &input, std::shared_ptr<Tensor> *output) override {
  38. *output = std::move(input);
  39. return Status::OK();
  40. };
  41. void Print(std::ostream &out) const override { out << "NoOp"; };
  42. std::string Name() const override { return kNoOp; }
  43. };
  44. class ThreeToOneOp : public TensorOp {
  45. public:
  46. ThreeToOneOp(){};
  47. ~ThreeToOneOp(){};
  48. uint32_t NumInput() override { return 3; }
  49. // Compute function that holds the actual implementation of the operation.
  50. Status Compute(const TensorRow &input, TensorRow *output) override {
  51. output->push_back(input[0]);
  52. return Status::OK();
  53. };
  54. void Print(std::ostream &out) const override { out << "ThreeToOneOp"; };
  55. std::string Name() const override { return "ThreeToOneOp"; }
  56. };
  57. class OneToThreeOp : public TensorOp {
  58. public:
  59. OneToThreeOp(){};
  60. ~OneToThreeOp(){};
  61. uint32_t NumOutput() override { return 3; }
  62. // Compute function that holds the actual implementation of the operation.
  63. // Simply pushing the same shared pointer of the first element of input vector three times.
  64. Status Compute(const TensorRow &input, TensorRow *output) override {
  65. output->push_back(input[0]);
  66. output->push_back(input[0]);
  67. output->push_back(input[0]);
  68. return Status::OK();
  69. };
  70. void Print(std::ostream &out) const override { out << "OneToThreeOp"; };
  71. std::string Name() const override { return "OneToThreeOp"; };
  72. };
  73. } // namespace test
  74. } // namespace dataset
  75. } // namespace mindspore
  76. class MindDataTestMapOp : public UT::DatasetOpTesting {
  77. public:
  78. void SetUp() override {
  79. DatasetOpTesting::SetUp();
  80. dataset_path_ = datasets_root_path_ + "" + "/testDataset2/testDataset2.data";
  81. schema_path_ = datasets_root_path_ + "" + "/testDataset2/datasetSchema.json";
  82. GlobalInit();
  83. // Start with an empty execution tree
  84. my_tree_ = std::make_shared<ExecutionTree>();
  85. }
  86. std::shared_ptr<TFReaderOp> CreateTFReaderOp() {
  87. std::shared_ptr<TFReaderOp> my_tfreader_op;
  88. TFReaderOp::Builder builder;
  89. builder.SetDatasetFilesList({dataset_path_})
  90. .SetColumnsToLoad({"image", "label", "A", "B"})
  91. .SetRowsPerBuffer(2)
  92. .SetWorkerConnectorSize(2)
  93. .SetNumWorkers(2);
  94. std::unique_ptr<DataSchema> schema = std::make_unique<DataSchema>();
  95. schema->LoadSchemaFile(schema_path_, {});
  96. builder.SetDataSchema(std::move(schema));
  97. Status rc = builder.Build(&my_tfreader_op);
  98. EXPECT_TRUE(rc.IsOk());
  99. return my_tfreader_op;
  100. }
  101. std::shared_ptr<ExecutionTree> my_tree_;
  102. private:
  103. std::string dataset_path_;
  104. std::string schema_path_;
  105. };
  106. std::shared_ptr<ImageFolderOp> ImageFolder(int64_t num_works, int64_t rows, int64_t conns, std::string path,
  107. bool shuf = false, std::shared_ptr<Sampler> sampler = nullptr,
  108. std::map<std::string, int32_t> map = {}, bool decode = false);
  109. std::shared_ptr<ExecutionTree> Build(std::vector<std::shared_ptr<DatasetOp>> ops);
  110. // TestAsMap scenario:
  111. // TFReaderOp reads a dataset that have column ordering |image|label|A|B|.
  112. // A TensorOp that does nothing picks the "image" column and produces a column named "X".
  113. // Thus, based on the new MapOp behaviour, the column ordering will be |X|label|A|B|.
  114. // Verify that the "image" column is removed and "X" column is added.
  115. TEST_F(MindDataTestMapOp, TestAsMap) {
  116. Status rc;
  117. MS_LOG(INFO) << "Doing TestAsMap.";
  118. // Note: The above TFReader config yields 5 buffers, each with 2 rows, for a total of 10 rows.
  119. auto my_tfreader_op = this->CreateTFReaderOp();
  120. rc = my_tree_->AssociateNode(my_tfreader_op);
  121. EXPECT_TRUE(rc.IsOk());
  122. auto my_no_op = std::make_shared<mindspore::dataset::test::NoOp>();
  123. std::vector<std::shared_ptr<TensorOp>> my_func_list;
  124. my_func_list.push_back(my_no_op);
  125. std::shared_ptr<MapOp> my_map_op;
  126. MapOp::Builder builder;
  127. builder.SetInColNames({"image"}).SetOutColNames({"X"}).SetTensorFuncs(std::move(my_func_list)).SetNumWorkers(1);
  128. rc = builder.Build(&my_map_op);
  129. rc = my_tree_->AssociateNode(my_map_op);
  130. EXPECT_TRUE(rc.IsOk());
  131. rc = my_map_op->AddChild(my_tfreader_op);
  132. EXPECT_TRUE(rc.IsOk());
  133. // Assign the tree root
  134. rc = my_tree_->AssignRoot(my_map_op);
  135. EXPECT_TRUE(rc.IsOk());
  136. // Now prepare the tree
  137. rc = my_tree_->Prepare();
  138. EXPECT_TRUE(rc.IsOk());
  139. rc = my_tree_->Launch();
  140. EXPECT_TRUE(rc.IsOk());
  141. // Start the loop of reading tensors from our pipeline
  142. DatasetIterator di(my_tree_);
  143. TensorMap tensor_map;
  144. rc = di.GetNextAsMap(&tensor_map);
  145. EXPECT_TRUE(rc.IsOk());
  146. EXPECT_EQ(tensor_map.size(), 4);
  147. EXPECT_EQ(tensor_map.find("image"), tensor_map.end());
  148. EXPECT_NE(tensor_map.find("label"), tensor_map.end());
  149. EXPECT_NE(tensor_map.find("X"), tensor_map.end());
  150. EXPECT_NE(tensor_map.find("A"), tensor_map.end());
  151. EXPECT_NE(tensor_map.find("B"), tensor_map.end());
  152. }
  153. // Test3to1 scenario:
  154. // TFReaderOp reads a dataset that have column ordering |image|label|A|B|.
  155. // A 3-to-1 TensorOp picks the columns [image, A, B] and produce a column named "X".
  156. // Thus, based on the new MapOp behaviour, the column ordering will be |X|label|.
  157. // Verify that the only columns "X" and "label" exist.
  158. TEST_F(MindDataTestMapOp, Test3to1) {
  159. Status rc;
  160. MS_LOG(INFO) << "Doing Test3to1.";
  161. // Note: The above TFReader config yields 5 buffers, each with 2 rows, for a total of 10 rows.
  162. auto my_tfreader_op = this->CreateTFReaderOp();
  163. rc = my_tree_->AssociateNode(my_tfreader_op);
  164. EXPECT_TRUE(rc.IsOk());
  165. auto my_op = std::make_shared<mindspore::dataset::test::ThreeToOneOp>();
  166. std::vector<std::shared_ptr<TensorOp>> my_func_list;
  167. my_func_list.push_back(my_op);
  168. std::shared_ptr<MapOp> my_map_op;
  169. MapOp::Builder builder;
  170. builder.SetInColNames({"image", "A", "B"})
  171. .SetOutColNames({"X"})
  172. .SetTensorFuncs(std::move(my_func_list))
  173. .SetNumWorkers(1);
  174. rc = builder.Build(&my_map_op);
  175. EXPECT_TRUE(rc.IsOk());
  176. rc = my_tree_->AssociateNode(my_map_op);
  177. EXPECT_TRUE(rc.IsOk());
  178. rc = my_map_op->AddChild(my_tfreader_op);
  179. EXPECT_TRUE(rc.IsOk());
  180. rc = my_tree_->AssignRoot(my_map_op);
  181. EXPECT_TRUE(rc.IsOk());
  182. rc = my_tree_->Prepare();
  183. EXPECT_TRUE(rc.IsOk());
  184. rc = my_tree_->Launch();
  185. EXPECT_TRUE(rc.IsOk());
  186. // Start the loop of reading tensors from our pipeline
  187. DatasetIterator di(my_tree_);
  188. TensorMap tensor_map;
  189. rc = di.GetNextAsMap(&tensor_map);
  190. EXPECT_TRUE(rc.IsOk());
  191. while (!tensor_map.empty()) {
  192. EXPECT_EQ(tensor_map.size(), 2);
  193. EXPECT_EQ(tensor_map.find("image"), tensor_map.end());
  194. EXPECT_NE(tensor_map.find("label"), tensor_map.end());
  195. EXPECT_NE(tensor_map.find("X"), tensor_map.end());
  196. EXPECT_EQ(tensor_map.find("A"), tensor_map.end());
  197. EXPECT_EQ(tensor_map.find("B"), tensor_map.end());
  198. rc = di.GetNextAsMap(&tensor_map);
  199. EXPECT_TRUE(rc.IsOk());
  200. }
  201. }
  202. // Test1to3 scenario:
  203. // TFReaderOp reads a dataset that have column ordering |image|label|A|B|.
  204. // A 1-to-3 TensorOp picks the columns [image] and produce a column named [X, Y, Z].
  205. // Thus, based on the new MapOp behaviour, the column ordering will be |X|Y|Z|label|A|B|.
  206. // Verify that the only columns X, Y, Z are added (to the front) and followed by columns label, A, B..
  207. TEST_F(MindDataTestMapOp, Test1to3) {
  208. Status rc;
  209. MS_LOG(INFO) << "Doing Test1to3.";
  210. // Note: The above TFReader config yields 5 buffers, each with 2 rows, for a total of 10 rows.
  211. auto my_tfreader_op = this->CreateTFReaderOp();
  212. rc = my_tree_->AssociateNode(my_tfreader_op);
  213. EXPECT_TRUE(rc.IsOk());
  214. auto my_op = std::make_shared<mindspore::dataset::test::OneToThreeOp>();
  215. std::vector<std::shared_ptr<TensorOp>> my_func_list;
  216. my_func_list.push_back(my_op);
  217. std::shared_ptr<MapOp> my_map_op;
  218. MapOp::Builder builder;
  219. builder.SetInColNames({"image"})
  220. .SetOutColNames({"X", "Y", "Z"})
  221. .SetTensorFuncs(std::move(my_func_list))
  222. .SetNumWorkers(1);
  223. // ProjectOp
  224. std::vector<std::string> columns_to_project = {"X", "Y", "Z", "label", "A", "B"};
  225. std::shared_ptr<ProjectOp> my_project_op = std::make_shared<ProjectOp>(columns_to_project);
  226. rc = my_tree_->AssociateNode(my_project_op);
  227. ASSERT_TRUE(rc.IsOk());
  228. rc = my_tree_->AssignRoot(my_project_op);
  229. ASSERT_TRUE(rc.IsOk());
  230. rc = builder.Build(&my_map_op);
  231. rc = my_tree_->AssociateNode(my_map_op);
  232. EXPECT_TRUE(rc.IsOk());
  233. rc = my_project_op->AddChild(my_map_op);
  234. EXPECT_TRUE(rc.IsOk());
  235. rc = my_map_op->AddChild(my_tfreader_op);
  236. EXPECT_TRUE(rc.IsOk());
  237. rc = my_tree_->Prepare();
  238. EXPECT_TRUE(rc.IsOk());
  239. rc = my_tree_->Launch();
  240. EXPECT_TRUE(rc.IsOk());
  241. // Start the loop of reading tensors from our pipeline
  242. DatasetIterator di(my_tree_);
  243. TensorMap tensor_map;
  244. rc = di.GetNextAsMap(&tensor_map);
  245. EXPECT_TRUE(rc.IsOk());
  246. EXPECT_EQ(tensor_map.size(), 6);
  247. EXPECT_EQ(tensor_map.find("image"), tensor_map.end());
  248. EXPECT_NE(tensor_map.find("label"), tensor_map.end());
  249. EXPECT_NE(tensor_map.find("A"), tensor_map.end());
  250. EXPECT_NE(tensor_map.find("B"), tensor_map.end());
  251. EXPECT_NE(tensor_map.find("X"), tensor_map.end());
  252. EXPECT_NE(tensor_map.find("Y"), tensor_map.end());
  253. EXPECT_NE(tensor_map.find("Z"), tensor_map.end());
  254. // Getting the next row as vector (by position).
  255. TensorRow tensor_list;
  256. rc = di.FetchNextTensorRow(&tensor_list);
  257. EXPECT_TRUE(rc.IsOk());
  258. // Based on the schema file, create the golden result to compare with.
  259. std::vector<DataType::Type> golden_types({DataType::Type::DE_UINT8, DataType::Type::DE_UINT8,
  260. DataType::Type::DE_UINT8, DataType::Type::DE_INT64,
  261. DataType::Type::DE_FLOAT32, DataType::Type::DE_INT64});
  262. std::vector<uint64_t> golden_ranks({3, 3, 3, 1, 4, 1});
  263. std::vector<TensorShape> golden_shapes({TensorShape({3, 4, 2}), TensorShape({3, 4, 2}), TensorShape({3, 4, 2}),
  264. TensorShape({7}), TensorShape({1, 13, 14, 12}), TensorShape({9})});
  265. while (!tensor_list.empty()) {
  266. for (uint32_t i = 0; i < tensor_list.size(); i++) {
  267. EXPECT_EQ(tensor_list[i]->type(), golden_types[i]);
  268. EXPECT_EQ(tensor_list[i]->Rank(), golden_ranks[i]);
  269. EXPECT_EQ(tensor_list[i]->shape(), golden_shapes[i]);
  270. EXPECT_NE(tensor_list[i]->GetBuffer(), nullptr);
  271. }
  272. rc = di.FetchNextTensorRow(&tensor_list);
  273. EXPECT_TRUE(rc.IsOk());
  274. }
  275. }
  276. // TestMultiTensorOp scenario:
  277. // TFReaderOp reads a dataset that have column ordering |image|label|A|B|.
  278. // A series of 3-to-1 and 1-to-3 TensorOps are applied to [image, A, B] and
  279. // produce final output columns [X, Y, Z].
  280. // Based on the new MapOp behaviour, the column ordering will be |X|Y|Z|label|.
  281. TEST_F(MindDataTestMapOp, TestMultiTensorOp) {
  282. Status rc;
  283. MS_LOG(INFO) << "Doing TestMultiTensorOp.";
  284. // Note: The above TFReader config yields 5 buffers, each with 2 rows, for a total of 10 rows.
  285. auto my_tfreader_op = this->CreateTFReaderOp();
  286. rc = my_tree_->AssociateNode(my_tfreader_op);
  287. EXPECT_TRUE(rc.IsOk());
  288. auto my_op1 = std::make_shared<mindspore::dataset::test::ThreeToOneOp>();
  289. auto my_op2 = std::make_shared<mindspore::dataset::test::OneToThreeOp>();
  290. std::vector<std::shared_ptr<TensorOp>> my_func_list;
  291. my_func_list.push_back(my_op1);
  292. my_func_list.push_back(my_op2);
  293. std::shared_ptr<MapOp> my_map_op;
  294. MapOp::Builder builder;
  295. builder.SetInColNames({"image", "A", "B"})
  296. .SetOutColNames({"X", "Y", "Z"})
  297. .SetTensorFuncs(std::move(my_func_list))
  298. .SetNumWorkers(1);
  299. rc = builder.Build(&my_map_op);
  300. EXPECT_TRUE(rc.IsOk());
  301. rc = my_tree_->AssociateNode(my_map_op);
  302. EXPECT_TRUE(rc.IsOk());
  303. rc = my_map_op->AddChild(my_tfreader_op);
  304. EXPECT_TRUE(rc.IsOk());
  305. rc = my_tree_->AssignRoot(my_map_op);
  306. EXPECT_TRUE(rc.IsOk());
  307. rc = my_tree_->Prepare();
  308. EXPECT_TRUE(rc.IsOk());
  309. rc = my_tree_->Launch();
  310. EXPECT_TRUE(rc.IsOk());
  311. // Start the loop of reading tensors from our pipeline
  312. DatasetIterator di(my_tree_);
  313. TensorMap tensor_map;
  314. rc = di.GetNextAsMap(&tensor_map);
  315. EXPECT_TRUE(rc.IsOk());
  316. while (!tensor_map.empty()) {
  317. EXPECT_EQ(tensor_map.size(), 4);
  318. EXPECT_EQ(tensor_map.find("image"), tensor_map.end());
  319. EXPECT_EQ(tensor_map.find("A"), tensor_map.end());
  320. EXPECT_EQ(tensor_map.find("B"), tensor_map.end());
  321. EXPECT_NE(tensor_map.find("label"), tensor_map.end());
  322. EXPECT_NE(tensor_map.find("X"), tensor_map.end());
  323. EXPECT_NE(tensor_map.find("Y"), tensor_map.end());
  324. EXPECT_NE(tensor_map.find("Z"), tensor_map.end());
  325. // XYZ are Tensor shared_ptr to image, so it should have the same shape as image column.
  326. EXPECT_EQ(tensor_map["X"]->shape(), TensorShape({3, 4, 2}));
  327. EXPECT_EQ(tensor_map["Y"]->shape(), TensorShape({3, 4, 2}));
  328. EXPECT_EQ(tensor_map["Z"]->shape(), TensorShape({3, 4, 2}));
  329. rc = di.GetNextAsMap(&tensor_map);
  330. EXPECT_TRUE(rc.IsOk());
  331. }
  332. }
  333. TEST_F(MindDataTestMapOp, TestTFReaderRepeatMap) {
  334. Status rc;
  335. MS_LOG(INFO) << "Doing TestTFReaderRepeatMap.";
  336. uint32_t num_repeats = 3;
  337. // Note: The above TFReader config yields 5 buffers, each with 2 rows, for a total
  338. // of 10 rows.
  339. auto my_tfreader_op = this->CreateTFReaderOp();
  340. rc = my_tree_->AssociateNode(my_tfreader_op);
  341. EXPECT_TRUE(rc.IsOk());
  342. auto my_no_op = std::make_shared<mindspore::dataset::test::NoOp>();
  343. std::vector<std::shared_ptr<TensorOp>> my_func_list;
  344. my_func_list.push_back(my_no_op);
  345. std::shared_ptr<RepeatOp> my_repeat_op;
  346. rc = RepeatOp::Builder(num_repeats).Build(&my_repeat_op);
  347. EXPECT_TRUE(rc.IsOk());
  348. rc = my_tree_->AssociateNode(my_repeat_op);
  349. EXPECT_TRUE(rc.IsOk());
  350. std::shared_ptr<MapOp> my_map_op;
  351. MapOp::Builder builder;
  352. builder.SetInColNames({"label"}).SetOutColNames({}).SetTensorFuncs(std::move(my_func_list)).SetNumWorkers(5);
  353. rc = builder.Build(&my_map_op);
  354. EXPECT_TRUE(rc.IsOk());
  355. rc = my_tree_->AssociateNode(my_map_op);
  356. EXPECT_TRUE(rc.IsOk());
  357. rc = my_map_op->AddChild(my_repeat_op);
  358. EXPECT_TRUE(rc.IsOk());
  359. rc = my_repeat_op->AddChild(my_tfreader_op);
  360. EXPECT_TRUE(rc.IsOk());
  361. rc = my_tree_->AssignRoot(my_map_op);
  362. EXPECT_TRUE(rc.IsOk());
  363. rc = my_tree_->Prepare();
  364. EXPECT_TRUE(rc.IsOk());
  365. rc = my_tree_->Launch();
  366. EXPECT_TRUE(rc.IsOk());
  367. // Start the loop of reading tensors from our pipeline
  368. DatasetIterator di(my_tree_);
  369. TensorRow tensor_list;
  370. rc = di.FetchNextTensorRow(&tensor_list);
  371. EXPECT_TRUE(rc.IsOk());
  372. EXPECT_EQ(tensor_list.size(), 4);
  373. uint32_t row_count = 0;
  374. while (!tensor_list.empty()) {
  375. row_count++;
  376. MS_LOG(INFO) << "row_count: " << row_count << ".";
  377. rc = di.FetchNextTensorRow(&tensor_list);
  378. EXPECT_TRUE(rc.IsOk());
  379. }
  380. ASSERT_EQ(row_count, 10 * num_repeats);
  381. }
  382. TEST_F(MindDataTestMapOp, TestTFReaderMapRepeat) {
  383. Status rc;
  384. MS_LOG(INFO) << "Doing TestTFReaderMapRepeat.";
  385. uint32_t num_repeats = 3;
  386. // Note: The above TFReader config yields 5 buffers, each with 2 rows, for a total
  387. // of 10 rows.
  388. auto my_tfreader_op = this->CreateTFReaderOp();
  389. rc = my_tree_->AssociateNode(my_tfreader_op);
  390. EXPECT_TRUE(rc.IsOk());
  391. auto my_no_op = std::make_shared<mindspore::dataset::test::NoOp>();
  392. std::vector<std::shared_ptr<TensorOp>> my_func_list;
  393. my_func_list.push_back(my_no_op);
  394. std::shared_ptr<RepeatOp> my_repeat_op;
  395. rc = RepeatOp::Builder(num_repeats).Build(&my_repeat_op);
  396. EXPECT_TRUE(rc.IsOk());
  397. rc = my_tree_->AssociateNode(my_repeat_op);
  398. EXPECT_TRUE(rc.IsOk());
  399. std::shared_ptr<MapOp> my_map_op;
  400. MapOp::Builder builder;
  401. builder.SetInColNames({"label"}).SetOutColNames({}).SetTensorFuncs(std::move(my_func_list)).SetNumWorkers(50);
  402. rc = builder.Build(&my_map_op);
  403. EXPECT_TRUE(rc.IsOk());
  404. rc = my_tree_->AssociateNode(my_map_op);
  405. EXPECT_TRUE(rc.IsOk());
  406. rc = my_repeat_op->AddChild(my_map_op);
  407. EXPECT_TRUE(rc.IsOk());
  408. rc = my_map_op->AddChild(my_tfreader_op);
  409. EXPECT_TRUE(rc.IsOk());
  410. rc = my_tree_->AssignRoot(my_repeat_op);
  411. EXPECT_TRUE(rc.IsOk());
  412. rc = my_tree_->Prepare();
  413. EXPECT_TRUE(rc.IsOk());
  414. rc = my_tree_->Launch();
  415. EXPECT_TRUE(rc.IsOk());
  416. // Start the loop of reading tensors from our pipeline
  417. DatasetIterator di(my_tree_);
  418. TensorRow tensor_list;
  419. rc = di.FetchNextTensorRow(&tensor_list);
  420. EXPECT_TRUE(rc.IsOk());
  421. EXPECT_EQ(tensor_list.size(), 4);
  422. uint32_t row_count = 0;
  423. while (!tensor_list.empty()) {
  424. row_count++;
  425. MS_LOG(INFO) << "row_count: " << row_count << ".";
  426. rc = di.FetchNextTensorRow(&tensor_list);
  427. EXPECT_TRUE(rc.IsOk());
  428. }
  429. ASSERT_EQ(row_count, 10 * num_repeats);
  430. }
  431. TEST_F(MindDataTestMapOp, TFReader_Decode_Repeat_Resize) {
  432. Status rc;
  433. MS_LOG(INFO) << "Doing TFReader_Decode_Repeat_Resize.";
  434. uint32_t num_repeats = 2;
  435. std::string dataset_path_ = datasets_root_path_ + "/" + "test_tf_file_3_images/train-0000-of-0001.data";
  436. std::shared_ptr<TFReaderOp> my_tfreader_op;
  437. TFReaderOp::Builder sobuilder;
  438. sobuilder.SetDatasetFilesList({dataset_path_})
  439. .SetColumnsToLoad({"image", "label"})
  440. .SetRowsPerBuffer(2)
  441. .SetWorkerConnectorSize(2)
  442. .SetNumWorkers(2);
  443. rc = sobuilder.Build(&my_tfreader_op);
  444. EXPECT_TRUE(rc.IsOk());
  445. rc = my_tree_->AssociateNode(my_tfreader_op);
  446. EXPECT_TRUE(rc.IsOk());
  447. auto decode_op = std::make_shared<DecodeOp>();
  448. std::vector<std::shared_ptr<TensorOp>> my_func_list;
  449. my_func_list.push_back(decode_op);
  450. std::shared_ptr<RepeatOp> my_repeat_op;
  451. rc = RepeatOp::Builder(num_repeats).Build(&my_repeat_op);
  452. EXPECT_TRUE(rc.IsOk());
  453. rc = my_tree_->AssociateNode(my_repeat_op);
  454. EXPECT_TRUE(rc.IsOk());
  455. std::shared_ptr<MapOp> my_map_decode_op;
  456. MapOp::Builder builder;
  457. builder.SetInColNames({"image"}).SetOutColNames({}).SetTensorFuncs(std::move(my_func_list)).SetNumWorkers(4);
  458. rc = builder.Build(&my_map_decode_op);
  459. EXPECT_TRUE(rc.IsOk());
  460. rc = my_tree_->AssociateNode(my_map_decode_op);
  461. EXPECT_TRUE(rc.IsOk());
  462. auto resize_op = std::make_shared<ResizeOp>(300, 300);
  463. std::vector<std::shared_ptr<TensorOp>> my_func_list2;
  464. my_func_list2.push_back(resize_op);
  465. std::shared_ptr<MapOp> my_map_resize_op;
  466. MapOp::Builder builder2;
  467. builder2.SetInColNames({"image"}).SetOutColNames({}).SetTensorFuncs(std::move(my_func_list2)).SetNumWorkers(5);
  468. rc = builder2.Build(&my_map_resize_op);
  469. EXPECT_TRUE(rc.IsOk());
  470. rc = my_tree_->AssociateNode(my_map_resize_op);
  471. EXPECT_TRUE(rc.IsOk());
  472. rc = my_map_decode_op->AddChild(my_tfreader_op);
  473. EXPECT_TRUE(rc.IsOk());
  474. rc = my_repeat_op->AddChild(my_map_decode_op);
  475. EXPECT_TRUE(rc.IsOk());
  476. rc = my_map_resize_op->AddChild(my_repeat_op);
  477. EXPECT_TRUE(rc.IsOk());
  478. rc = my_tree_->AssignRoot(my_map_resize_op);
  479. EXPECT_TRUE(rc.IsOk());
  480. rc = my_tree_->Prepare();
  481. EXPECT_TRUE(rc.IsOk());
  482. rc = my_tree_->Launch();
  483. EXPECT_TRUE(rc.IsOk());
  484. // Start the loop of reading tensors from our pipeline
  485. DatasetIterator di(my_tree_);
  486. TensorRow tensor_list;
  487. rc = di.FetchNextTensorRow(&tensor_list);
  488. EXPECT_TRUE(rc.IsOk());
  489. EXPECT_EQ(tensor_list.size(), 2);
  490. uint32_t row_count = 0;
  491. while (!tensor_list.empty()) {
  492. row_count++;
  493. rc = di.FetchNextTensorRow(&tensor_list);
  494. EXPECT_TRUE(rc.IsOk());
  495. }
  496. ASSERT_EQ(row_count, 6);
  497. }
  498. TEST_F(MindDataTestMapOp, ImageFolder_Decode_Repeat_Resize) {
  499. Status rc;
  500. MS_LOG(INFO) << "Doing ImageFolder_Decode_Repeat_Resize.";
  501. std::string folder_path = datasets_root_path_ + "/testPK/data";
  502. uint32_t num_repeats = 2;
  503. std::shared_ptr<RepeatOp> repeat_op;
  504. rc = RepeatOp::Builder(num_repeats).Build(&repeat_op);
  505. EXPECT_TRUE(rc.IsOk());
  506. auto decode_op = std::make_shared<DecodeOp>();
  507. std::vector<std::shared_ptr<TensorOp>> func_list;
  508. func_list.push_back(decode_op);
  509. std::shared_ptr<MapOp> map_decode_map;
  510. MapOp::Builder map_decode_builder;
  511. map_decode_builder.SetInColNames({"image"}).SetOutColNames({}).SetTensorFuncs(func_list).SetNumWorkers(4);
  512. rc = map_decode_builder.Build(&map_decode_map);
  513. EXPECT_TRUE(rc.IsOk());
  514. auto resize_op = std::make_shared<ResizeOp>(300, 300);
  515. std::vector<std::shared_ptr<TensorOp>> func_list2;
  516. func_list2.push_back(resize_op);
  517. std::shared_ptr<MapOp> map_resize_op;
  518. MapOp::Builder map_resize_builder;
  519. map_resize_builder.SetInColNames({"image"}).SetOutColNames({}).SetTensorFuncs(func_list2).SetNumWorkers(5);
  520. rc = map_resize_builder.Build(&map_resize_op);
  521. EXPECT_TRUE(rc.IsOk());
  522. my_tree_ = Build({ImageFolder(16, 2, 32, folder_path, false), map_decode_map, repeat_op, map_resize_op});
  523. rc = my_tree_->Prepare();
  524. EXPECT_TRUE(rc.IsOk());
  525. rc = my_tree_->Launch();
  526. EXPECT_TRUE(rc.IsOk());
  527. // Start the loop of reading tensors from our pipeline
  528. DatasetIterator di(my_tree_);
  529. TensorMap tensor_map;
  530. di.GetNextAsMap(&tensor_map);
  531. EXPECT_TRUE(rc.IsOk());
  532. uint64_t i = 0;
  533. int32_t label = 0;
  534. int32_t img_class[] = {0, 1, 2, 3};
  535. std::string result;
  536. while (tensor_map.size() != 0) {
  537. tensor_map["label"]->GetItemAt<int32_t>(&label, {});
  538. MS_LOG(DEBUG) << "row:" << i << "\tlabel:" << label << "\n";
  539. EXPECT_TRUE(img_class[(i % 44) / 11] == label);
  540. // Dump all the image into string, to be used as a comparison later.
  541. result.append((char *)tensor_map["image"]->GetBuffer(), (int64_t)tensor_map["image"]->Size());
  542. di.GetNextAsMap(&tensor_map);
  543. i++;
  544. }
  545. EXPECT_TRUE(i == 88);
  546. // Part-2 : creating mapop with performance mode = false, to check if the result is the same
  547. // as when performance mode = true.
  548. rc = RepeatOp::Builder(num_repeats).Build(&repeat_op);
  549. EXPECT_TRUE(rc.IsOk());
  550. map_decode_builder.SetInColNames({"image"})
  551. .SetOutColNames({})
  552. .SetTensorFuncs(func_list)
  553. .SetNumWorkers(14);
  554. rc = map_decode_builder.Build(&map_decode_map);
  555. EXPECT_TRUE(rc.IsOk());
  556. map_resize_builder.SetInColNames({"image"})
  557. .SetOutColNames({})
  558. .SetTensorFuncs(func_list2)
  559. .SetNumWorkers(15);
  560. rc = map_resize_builder.Build(&map_resize_op);
  561. EXPECT_TRUE(rc.IsOk());
  562. auto my_tree_2 = Build({ImageFolder(16, 2, 32, folder_path, false), map_decode_map, repeat_op, map_resize_op});
  563. rc = my_tree_2->Prepare();
  564. EXPECT_TRUE(rc.IsOk());
  565. rc = my_tree_2->Launch();
  566. EXPECT_TRUE(rc.IsOk());
  567. // Start the loop of reading tensors from our pipeline
  568. DatasetIterator di2(my_tree_2);
  569. di2.GetNextAsMap(&tensor_map);
  570. EXPECT_TRUE(rc.IsOk());
  571. i = 0;
  572. label = 0;
  573. std::string result2;
  574. while (tensor_map.size() != 0) {
  575. tensor_map["label"]->GetItemAt<int32_t>(&label, {});
  576. MS_LOG(DEBUG) << "row:" << i << "\tlabel:" << label << "\n";
  577. EXPECT_TRUE(img_class[(i % 44) / 11] == label);
  578. result2.append((char *)tensor_map["image"]->GetBuffer(), (int64_t)tensor_map["image"]->Size());
  579. di2.GetNextAsMap(&tensor_map);
  580. i++;
  581. }
  582. EXPECT_TRUE(i == 88);
  583. EXPECT_EQ(result.size(), result2.size());
  584. EXPECT_EQ(result, result2);
  585. }
  586. TEST_F(MindDataTestMapOp, ImageFolder_Decode_Repeat_Resize_NoInputColumns) {
  587. Status rc;
  588. MS_LOG(INFO) << "Doing ImageFolder_Decode_Repeat_Resize_NoInputColumns.";
  589. std::string folder_path = datasets_root_path_ + "/testPK/data";
  590. uint32_t num_repeats = 2;
  591. std::shared_ptr<RepeatOp> repeat_op;
  592. rc = RepeatOp::Builder(num_repeats).Build(&repeat_op);
  593. EXPECT_TRUE(rc.IsOk());
  594. auto decode_op = std::make_shared<DecodeOp>();
  595. std::vector<std::shared_ptr<TensorOp>> func_list;
  596. func_list.push_back(decode_op);
  597. std::shared_ptr<MapOp> map_decode_map;
  598. MapOp::Builder map_decode_builder;
  599. map_decode_builder.SetInColNames({}).SetOutColNames({}).SetTensorFuncs(func_list).SetNumWorkers(4);
  600. rc = map_decode_builder.Build(&map_decode_map);
  601. EXPECT_TRUE(rc.IsOk());
  602. auto resize_op = std::make_shared<ResizeOp>(300, 300);
  603. std::vector<std::shared_ptr<TensorOp>> func_list2;
  604. func_list2.push_back(resize_op);
  605. std::shared_ptr<MapOp> map_resize_op;
  606. MapOp::Builder map_resize_builder;
  607. map_resize_builder.SetTensorFuncs(func_list2).SetNumWorkers(5);
  608. rc = map_resize_builder.Build(&map_resize_op);
  609. EXPECT_TRUE(rc.IsOk());
  610. my_tree_ = Build({ImageFolder(16, 2, 32, folder_path, false), map_decode_map, repeat_op, map_resize_op});
  611. rc = my_tree_->Prepare();
  612. EXPECT_TRUE(rc.IsOk());
  613. rc = my_tree_->Launch();
  614. EXPECT_TRUE(rc.IsOk());
  615. // Start the loop of reading tensors from our pipeline
  616. DatasetIterator di(my_tree_);
  617. TensorMap tensor_map;
  618. di.GetNextAsMap(&tensor_map);
  619. EXPECT_TRUE(rc.IsOk());
  620. uint64_t i = 0;
  621. int32_t label = 0;
  622. int32_t img_class[] = {0, 1, 2, 3};
  623. std::string result;
  624. while (tensor_map.size() != 0) {
  625. tensor_map["label"]->GetItemAt<int32_t>(&label, {});
  626. EXPECT_TRUE(img_class[(i % 44) / 11] == label);
  627. di.GetNextAsMap(&tensor_map);
  628. i++;
  629. }
  630. EXPECT_TRUE(i == 88);
  631. }