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

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