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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763
  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 TensorRow &input, TensorRow *output) override {
  50. output->push_back(input[0]);
  51. return Status::OK();
  52. };
  53. void Print(std::ostream &out) const override { out << "ThreeToOneOp"; };
  54. };
  55. class OneToThreeOp : public TensorOp {
  56. public:
  57. OneToThreeOp() {};
  58. ~OneToThreeOp() {};
  59. uint32_t NumOutput() override { return 3; }
  60. // Compute function that holds the actual implementation of the operation.
  61. // Simply pushing the same shared pointer of the first element of input vector three times.
  62. Status Compute(const TensorRow &input, TensorRow *output) override {
  63. output->push_back(input[0]);
  64. output->push_back(input[0]);
  65. output->push_back(input[0]);
  66. return Status::OK();
  67. };
  68. void Print(std::ostream &out) const override { out << "OneToThreeOp"; };
  69. };
  70. } // namespace test
  71. } // namespace dataset
  72. } // namespace mindspore
  73. class MindDataTestMapOp : public UT::DatasetOpTesting {
  74. public:
  75. void SetUp() override {
  76. DatasetOpTesting::SetUp();
  77. dataset_path_ = datasets_root_path_ + "" + "/testDataset2/testDataset2.data";
  78. schema_path_ = datasets_root_path_ + "" + "/testDataset2/datasetSchema.json";
  79. GlobalInit();
  80. // Start with an empty execution tree
  81. my_tree_ = std::make_shared<ExecutionTree>();
  82. }
  83. std::shared_ptr<TFReaderOp> CreateTFReaderOp() {
  84. std::shared_ptr<TFReaderOp> my_tfreader_op;
  85. TFReaderOp::Builder builder;
  86. builder.SetDatasetFilesList({dataset_path_})
  87. .SetColumnsToLoad({"image", "label", "A", "B"})
  88. .SetRowsPerBuffer(2)
  89. .SetWorkerConnectorSize(2)
  90. .SetNumWorkers(2);
  91. std::unique_ptr<DataSchema> schema = std::make_unique<DataSchema>();
  92. schema->LoadSchemaFile(schema_path_, {});
  93. builder.SetDataSchema(std::move(schema));
  94. Status rc = builder.Build(&my_tfreader_op);
  95. EXPECT_TRUE(rc.IsOk());
  96. return my_tfreader_op;
  97. }
  98. std::shared_ptr<ExecutionTree> my_tree_;
  99. private:
  100. std::string dataset_path_;
  101. std::string schema_path_;
  102. };
  103. std::shared_ptr<ImageFolderOp> ImageFolder(int64_t num_works, int64_t rows, int64_t conns, std::string path,
  104. bool shuf = false, std::shared_ptr<Sampler> sampler = nullptr,
  105. std::map<std::string, int32_t> map = {}, bool decode = false);
  106. std::shared_ptr<ExecutionTree> Build(std::vector<std::shared_ptr<DatasetOp>> ops);
  107. // TestAsMap scenario:
  108. // TFReaderOp reads a dataset that have column ordering |image|label|A|B|.
  109. // A TensorOp that does nothing picks the "image" column and produces a column named "X".
  110. // Thus, based on the new MapOp behaviour, the column ordering will be |X|label|A|B|.
  111. // Verify that the "image" column is removed and "X" column is added.
  112. TEST_F(MindDataTestMapOp, TestAsMap) {
  113. Status rc;
  114. MS_LOG(INFO) << "Doing TestAsMap.";
  115. // Note: The above TFReader config yields 5 buffers, each with 2 rows, for a total of 10 rows.
  116. auto my_tfreader_op = this->CreateTFReaderOp();
  117. rc = my_tree_->AssociateNode(my_tfreader_op);
  118. EXPECT_TRUE(rc.IsOk());
  119. auto my_no_op = std::make_shared<mindspore::dataset::test::NoOp>();
  120. std::vector<std::shared_ptr<TensorOp>> my_func_list;
  121. my_func_list.push_back(my_no_op);
  122. std::shared_ptr<MapOp> my_map_op;
  123. MapOp::Builder builder;
  124. builder.SetInColNames({"image"})
  125. .SetOutColNames({"X"})
  126. .SetTensorFuncs(std::move(my_func_list))
  127. .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. );
  263. std::vector<uint64_t> golden_ranks({3, 3, 3, 1, 4, 1});
  264. std::vector<TensorShape> golden_shapes({TensorShape({3, 4, 2}), TensorShape({3, 4, 2}), TensorShape({3, 4, 2}),
  265. TensorShape({7}), TensorShape({1, 13, 14, 12}), TensorShape({9})} );
  266. while (!tensor_list.empty()) {
  267. for (uint32_t i = 0; i < tensor_list.size(); i++) {
  268. EXPECT_EQ(tensor_list[i]->type(), golden_types[i]);
  269. EXPECT_EQ(tensor_list[i]->Rank(), golden_ranks[i]);
  270. EXPECT_EQ(tensor_list[i]->shape(), golden_shapes[i]);
  271. EXPECT_NE(tensor_list[i]->GetBuffer(), nullptr);
  272. }
  273. rc = di.FetchNextTensorRow(&tensor_list);
  274. EXPECT_TRUE(rc.IsOk());
  275. }
  276. }
  277. // TestMultiTensorOp scenario:
  278. // TFReaderOp reads a dataset that have column ordering |image|label|A|B|.
  279. // A series of 3-to-1 and 1-to-3 TensorOps are applied to [image, A, B] and
  280. // produce final output columns [X, Y, Z].
  281. // Based on the new MapOp behaviour, the column ordering will be |X|Y|Z|label|.
  282. TEST_F(MindDataTestMapOp, TestMultiTensorOp) {
  283. Status rc;
  284. MS_LOG(INFO) << "Doing TestMultiTensorOp.";
  285. // Note: The above TFReader config yields 5 buffers, each with 2 rows, for a total of 10 rows.
  286. auto my_tfreader_op = this->CreateTFReaderOp();
  287. rc = my_tree_->AssociateNode(my_tfreader_op);
  288. EXPECT_TRUE(rc.IsOk());
  289. auto my_op1 = std::make_shared<mindspore::dataset::test::ThreeToOneOp>();
  290. auto my_op2 = std::make_shared<mindspore::dataset::test::OneToThreeOp>();
  291. std::vector<std::shared_ptr<TensorOp>> my_func_list;
  292. my_func_list.push_back(my_op1);
  293. my_func_list.push_back(my_op2);
  294. std::shared_ptr<MapOp> my_map_op;
  295. MapOp::Builder builder;
  296. builder.SetInColNames({"image", "A", "B"})
  297. .SetOutColNames({"X", "Y", "Z"})
  298. .SetTensorFuncs(std::move(my_func_list))
  299. .SetNumWorkers(1);
  300. rc = builder.Build(&my_map_op);
  301. EXPECT_TRUE(rc.IsOk());
  302. rc = my_tree_->AssociateNode(my_map_op);
  303. EXPECT_TRUE(rc.IsOk());
  304. rc = my_map_op->AddChild(my_tfreader_op);
  305. EXPECT_TRUE(rc.IsOk());
  306. rc = my_tree_->AssignRoot(my_map_op);
  307. EXPECT_TRUE(rc.IsOk());
  308. rc = my_tree_->Prepare();
  309. EXPECT_TRUE(rc.IsOk());
  310. rc = my_tree_->Launch();
  311. EXPECT_TRUE(rc.IsOk());
  312. // Start the loop of reading tensors from our pipeline
  313. DatasetIterator di(my_tree_);
  314. TensorMap tensor_map;
  315. rc = di.GetNextAsMap(&tensor_map);
  316. EXPECT_TRUE(rc.IsOk());
  317. while (!tensor_map.empty()) {
  318. EXPECT_EQ(tensor_map.size(), 4);
  319. EXPECT_EQ(tensor_map.find("image"), tensor_map.end());
  320. EXPECT_EQ(tensor_map.find("A"), tensor_map.end());
  321. EXPECT_EQ(tensor_map.find("B"), tensor_map.end());
  322. EXPECT_NE(tensor_map.find("label"), tensor_map.end());
  323. EXPECT_NE(tensor_map.find("X"), tensor_map.end());
  324. EXPECT_NE(tensor_map.find("Y"), tensor_map.end());
  325. EXPECT_NE(tensor_map.find("Z"), tensor_map.end());
  326. // XYZ are Tensor shared_ptr to image, so it should have the same shape as image column.
  327. EXPECT_EQ(tensor_map["X"]->shape(), TensorShape({3, 4, 2}));
  328. EXPECT_EQ(tensor_map["Y"]->shape(), TensorShape({3, 4, 2}));
  329. EXPECT_EQ(tensor_map["Z"]->shape(), TensorShape({3, 4, 2}));
  330. rc = di.GetNextAsMap(&tensor_map);
  331. EXPECT_TRUE(rc.IsOk());
  332. }
  333. }
  334. TEST_F(MindDataTestMapOp, TestTFReaderRepeatMap) {
  335. Status rc;
  336. MS_LOG(INFO) << "Doing TestTFReaderRepeatMap.";
  337. uint32_t num_repeats = 3;
  338. // Note: The above TFReader config yields 5 buffers, each with 2 rows, for a total
  339. // of 10 rows.
  340. auto my_tfreader_op = this->CreateTFReaderOp();
  341. rc = my_tree_->AssociateNode(my_tfreader_op);
  342. EXPECT_TRUE(rc.IsOk());
  343. auto my_no_op = std::make_shared<mindspore::dataset::test::NoOp>();
  344. std::vector<std::shared_ptr<TensorOp>> my_func_list;
  345. my_func_list.push_back(my_no_op);
  346. std::shared_ptr<RepeatOp> my_repeat_op;
  347. rc = RepeatOp::Builder(num_repeats).Build(&my_repeat_op);
  348. EXPECT_TRUE(rc.IsOk());
  349. rc = my_tree_->AssociateNode(my_repeat_op);
  350. EXPECT_TRUE(rc.IsOk());
  351. std::shared_ptr<MapOp> my_map_op;
  352. MapOp::Builder builder;
  353. builder.SetInColNames({"label"})
  354. .SetOutColNames({})
  355. .SetTensorFuncs(std::move(my_func_list))
  356. .SetNumWorkers(5);
  357. rc = builder.Build(&my_map_op);
  358. EXPECT_TRUE(rc.IsOk());
  359. rc = my_tree_->AssociateNode(my_map_op);
  360. EXPECT_TRUE(rc.IsOk());
  361. rc = my_map_op->AddChild(my_repeat_op);
  362. EXPECT_TRUE(rc.IsOk());
  363. rc = my_repeat_op->AddChild(my_tfreader_op);
  364. EXPECT_TRUE(rc.IsOk());
  365. rc = my_tree_->AssignRoot(my_map_op);
  366. EXPECT_TRUE(rc.IsOk());
  367. rc = my_tree_->Prepare();
  368. EXPECT_TRUE(rc.IsOk());
  369. rc = my_tree_->Launch();
  370. EXPECT_TRUE(rc.IsOk());
  371. // Start the loop of reading tensors from our pipeline
  372. DatasetIterator di(my_tree_);
  373. TensorRow tensor_list;
  374. rc = di.FetchNextTensorRow(&tensor_list);
  375. EXPECT_TRUE(rc.IsOk());
  376. EXPECT_EQ(tensor_list.size(), 4);
  377. uint32_t row_count = 0;
  378. while (!tensor_list.empty()) {
  379. row_count++;
  380. MS_LOG(INFO) << "row_count: " << row_count << ".";
  381. rc = di.FetchNextTensorRow(&tensor_list);
  382. EXPECT_TRUE(rc.IsOk());
  383. }
  384. ASSERT_EQ(row_count, 10 * num_repeats);
  385. }
  386. TEST_F(MindDataTestMapOp, TestTFReaderMapRepeat) {
  387. Status rc;
  388. MS_LOG(INFO) << "Doing TestTFReaderMapRepeat.";
  389. uint32_t num_repeats = 3;
  390. // Note: The above TFReader config yields 5 buffers, each with 2 rows, for a total
  391. // of 10 rows.
  392. auto my_tfreader_op = this->CreateTFReaderOp();
  393. rc = my_tree_->AssociateNode(my_tfreader_op);
  394. EXPECT_TRUE(rc.IsOk());
  395. auto my_no_op = std::make_shared<mindspore::dataset::test::NoOp>();
  396. std::vector<std::shared_ptr<TensorOp>> my_func_list;
  397. my_func_list.push_back(my_no_op);
  398. std::shared_ptr<RepeatOp> my_repeat_op;
  399. rc = RepeatOp::Builder(num_repeats).Build(&my_repeat_op);
  400. EXPECT_TRUE(rc.IsOk());
  401. rc = my_tree_->AssociateNode(my_repeat_op);
  402. EXPECT_TRUE(rc.IsOk());
  403. std::shared_ptr<MapOp> my_map_op;
  404. MapOp::Builder builder;
  405. builder.SetInColNames({"label"})
  406. .SetOutColNames({})
  407. .SetTensorFuncs(std::move(my_func_list))
  408. .SetNumWorkers(50);
  409. rc = builder.Build(&my_map_op);
  410. EXPECT_TRUE(rc.IsOk());
  411. rc = my_tree_->AssociateNode(my_map_op);
  412. EXPECT_TRUE(rc.IsOk());
  413. rc = my_repeat_op->AddChild(my_map_op);
  414. EXPECT_TRUE(rc.IsOk());
  415. rc = my_map_op->AddChild(my_tfreader_op);
  416. EXPECT_TRUE(rc.IsOk());
  417. rc = my_tree_->AssignRoot(my_repeat_op);
  418. EXPECT_TRUE(rc.IsOk());
  419. rc = my_tree_->Prepare();
  420. EXPECT_TRUE(rc.IsOk());
  421. rc = my_tree_->Launch();
  422. EXPECT_TRUE(rc.IsOk());
  423. // Start the loop of reading tensors from our pipeline
  424. DatasetIterator di(my_tree_);
  425. TensorRow tensor_list;
  426. rc = di.FetchNextTensorRow(&tensor_list);
  427. EXPECT_TRUE(rc.IsOk());
  428. EXPECT_EQ(tensor_list.size(), 4);
  429. uint32_t row_count = 0;
  430. while (!tensor_list.empty()) {
  431. row_count++;
  432. MS_LOG(INFO) << "row_count: " << row_count << ".";
  433. rc = di.FetchNextTensorRow(&tensor_list);
  434. EXPECT_TRUE(rc.IsOk());
  435. }
  436. ASSERT_EQ(row_count, 10 * num_repeats);
  437. }
  438. TEST_F(MindDataTestMapOp, TFReader_Decode_Repeat_Resize) {
  439. Status rc;
  440. MS_LOG(INFO) << "Doing TFReader_Decode_Repeat_Resize.";
  441. uint32_t num_repeats = 2;
  442. std::string dataset_path_ = datasets_root_path_ + "/" + "test_tf_file_3_images/train-0000-of-0001.data";
  443. std::shared_ptr<TFReaderOp> my_tfreader_op;
  444. TFReaderOp::Builder sobuilder;
  445. sobuilder.SetDatasetFilesList({dataset_path_})
  446. .SetColumnsToLoad({"image", "label"})
  447. .SetRowsPerBuffer(2)
  448. .SetWorkerConnectorSize(2)
  449. .SetNumWorkers(2);
  450. rc = sobuilder.Build(&my_tfreader_op);
  451. EXPECT_TRUE(rc.IsOk());
  452. rc = my_tree_->AssociateNode(my_tfreader_op);
  453. EXPECT_TRUE(rc.IsOk());
  454. auto decode_op = std::make_shared<DecodeOp>();
  455. std::vector<std::shared_ptr<TensorOp>> my_func_list;
  456. my_func_list.push_back(decode_op);
  457. std::shared_ptr<RepeatOp> my_repeat_op;
  458. rc = RepeatOp::Builder(num_repeats).Build(&my_repeat_op);
  459. EXPECT_TRUE(rc.IsOk());
  460. rc = my_tree_->AssociateNode(my_repeat_op);
  461. EXPECT_TRUE(rc.IsOk());
  462. std::shared_ptr<MapOp> my_map_decode_op;
  463. MapOp::Builder builder;
  464. builder.SetInColNames({"image"})
  465. .SetOutColNames({})
  466. .SetTensorFuncs(std::move(my_func_list))
  467. .SetNumWorkers(4);
  468. rc = builder.Build(&my_map_decode_op);
  469. EXPECT_TRUE(rc.IsOk());
  470. rc = my_tree_->AssociateNode(my_map_decode_op);
  471. EXPECT_TRUE(rc.IsOk());
  472. auto resize_op = std::make_shared<ResizeOp>(300, 300);
  473. std::vector<std::shared_ptr<TensorOp>> my_func_list2;
  474. my_func_list2.push_back(resize_op);
  475. std::shared_ptr<MapOp> my_map_resize_op;
  476. MapOp::Builder builder2;
  477. builder2.SetInColNames({"image"})
  478. .SetOutColNames({})
  479. .SetTensorFuncs(std::move(my_func_list2))
  480. .SetNumWorkers(5);
  481. rc = builder2.Build(&my_map_resize_op);
  482. EXPECT_TRUE(rc.IsOk());
  483. rc = my_tree_->AssociateNode(my_map_resize_op);
  484. EXPECT_TRUE(rc.IsOk());
  485. rc = my_map_decode_op->AddChild(my_tfreader_op);
  486. EXPECT_TRUE(rc.IsOk());
  487. rc = my_repeat_op->AddChild(my_map_decode_op);
  488. EXPECT_TRUE(rc.IsOk());
  489. rc = my_map_resize_op->AddChild(my_repeat_op);
  490. EXPECT_TRUE(rc.IsOk());
  491. rc = my_tree_->AssignRoot(my_map_resize_op);
  492. EXPECT_TRUE(rc.IsOk());
  493. rc = my_tree_->Prepare();
  494. EXPECT_TRUE(rc.IsOk());
  495. rc = my_tree_->Launch();
  496. EXPECT_TRUE(rc.IsOk());
  497. // Start the loop of reading tensors from our pipeline
  498. DatasetIterator di(my_tree_);
  499. TensorRow tensor_list;
  500. rc = di.FetchNextTensorRow(&tensor_list);
  501. EXPECT_TRUE(rc.IsOk());
  502. EXPECT_EQ(tensor_list.size(), 2);
  503. uint32_t row_count = 0;
  504. while (!tensor_list.empty()) {
  505. row_count++;
  506. rc = di.FetchNextTensorRow(&tensor_list);
  507. EXPECT_TRUE(rc.IsOk());
  508. }
  509. ASSERT_EQ(row_count, 6);
  510. }
  511. TEST_F(MindDataTestMapOp, ImageFolder_Decode_Repeat_Resize) {
  512. Status rc;
  513. MS_LOG(INFO) << "Doing ImageFolder_Decode_Repeat_Resize.";
  514. std::string folder_path = datasets_root_path_ + "/testPK/data";
  515. uint32_t num_repeats = 2;
  516. std::shared_ptr<RepeatOp> repeat_op;
  517. rc = RepeatOp::Builder(num_repeats).Build(&repeat_op);
  518. EXPECT_TRUE(rc.IsOk());
  519. auto decode_op = std::make_shared<DecodeOp>();
  520. std::vector<std::shared_ptr<TensorOp>> func_list;
  521. func_list.push_back(decode_op);
  522. std::shared_ptr<MapOp> map_decode_map;
  523. MapOp::Builder map_decode_builder;
  524. map_decode_builder.SetInColNames({"image"})
  525. .SetOutColNames({})
  526. .SetTensorFuncs(func_list)
  527. .SetNumWorkers(4);
  528. rc = map_decode_builder.Build(&map_decode_map);
  529. EXPECT_TRUE(rc.IsOk());
  530. auto resize_op = std::make_shared<ResizeOp>(300, 300);
  531. std::vector<std::shared_ptr<TensorOp>> func_list2;
  532. func_list2.push_back(resize_op);
  533. std::shared_ptr<MapOp> map_resize_op;
  534. MapOp::Builder map_resize_builder;
  535. map_resize_builder.SetInColNames({"image"})
  536. .SetOutColNames({})
  537. .SetTensorFuncs(func_list2)
  538. .SetNumWorkers(5);
  539. rc = map_resize_builder.Build(&map_resize_op);
  540. EXPECT_TRUE(rc.IsOk());
  541. my_tree_ = Build({ImageFolder(16, 2, 32, folder_path, false), map_decode_map, repeat_op, map_resize_op});
  542. rc = my_tree_->Prepare();
  543. EXPECT_TRUE(rc.IsOk());
  544. rc = my_tree_->Launch();
  545. EXPECT_TRUE(rc.IsOk());
  546. // Start the loop of reading tensors from our pipeline
  547. DatasetIterator di(my_tree_);
  548. TensorMap tensor_map;
  549. di.GetNextAsMap(&tensor_map);
  550. EXPECT_TRUE(rc.IsOk());
  551. uint64_t i = 0;
  552. int32_t label = 0;
  553. int32_t img_class[] = {0, 1, 2, 3};
  554. std::string result;
  555. while (tensor_map.size() != 0) {
  556. tensor_map["label"]->GetItemAt<int32_t>(&label, {});
  557. MS_LOG(DEBUG) << "row:" << i << "\tlabel:" << label << "\n";
  558. EXPECT_TRUE(img_class[(i % 44) / 11] == label);
  559. // Dump all the image into string, to be used as a comparison later.
  560. result.append((char *)tensor_map["image"]->GetBuffer(), (int64_t)tensor_map["image"]->Size());
  561. di.GetNextAsMap(&tensor_map);
  562. i++;
  563. }
  564. EXPECT_TRUE(i == 88);
  565. // Part-2 : creating mapop with performance mode = false, to check if the result is the same
  566. // as when performance mode = true.
  567. rc = RepeatOp::Builder(num_repeats).Build(&repeat_op);
  568. EXPECT_TRUE(rc.IsOk());
  569. map_decode_builder.SetInColNames({"image"})
  570. .SetOutColNames({})
  571. .SetTensorFuncs(func_list)
  572. .SetNumWorkers(14)
  573. .SetPerformanceMode(false);
  574. rc = map_decode_builder.Build(&map_decode_map);
  575. EXPECT_TRUE(rc.IsOk());
  576. map_resize_builder.SetInColNames({"image"})
  577. .SetOutColNames({})
  578. .SetTensorFuncs(func_list2)
  579. .SetNumWorkers(15)
  580. .SetPerformanceMode(false);
  581. rc = map_resize_builder.Build(&map_resize_op);
  582. EXPECT_TRUE(rc.IsOk());
  583. auto my_tree_2 = Build({ImageFolder(16, 2, 32, folder_path, false), map_decode_map, repeat_op, map_resize_op});
  584. rc = my_tree_2->Prepare();
  585. EXPECT_TRUE(rc.IsOk());
  586. rc = my_tree_2->Launch();
  587. EXPECT_TRUE(rc.IsOk());
  588. // Start the loop of reading tensors from our pipeline
  589. DatasetIterator di2(my_tree_2);
  590. di2.GetNextAsMap(&tensor_map);
  591. EXPECT_TRUE(rc.IsOk());
  592. i = 0;
  593. label = 0;
  594. std::string result2;
  595. while (tensor_map.size() != 0) {
  596. tensor_map["label"]->GetItemAt<int32_t>(&label, {});
  597. MS_LOG(DEBUG) << "row:" << i << "\tlabel:" << label << "\n";
  598. EXPECT_TRUE(img_class[(i % 44) / 11] == label);
  599. result2.append((char *)tensor_map["image"]->GetBuffer(), (int64_t)tensor_map["image"]->Size());
  600. di2.GetNextAsMap(&tensor_map);
  601. i++;
  602. }
  603. EXPECT_TRUE(i == 88);
  604. EXPECT_EQ(result.size(), result2.size());
  605. EXPECT_EQ(result, result2);
  606. }
  607. TEST_F(MindDataTestMapOp, ImageFolder_Decode_Repeat_Resize_NoInputColumns) {
  608. Status rc;
  609. MS_LOG(INFO) << "Doing ImageFolder_Decode_Repeat_Resize_NoInputColumns.";
  610. std::string folder_path = datasets_root_path_ + "/testPK/data";
  611. uint32_t num_repeats = 2;
  612. std::shared_ptr<RepeatOp> repeat_op;
  613. rc = RepeatOp::Builder(num_repeats).Build(&repeat_op);
  614. EXPECT_TRUE(rc.IsOk());
  615. auto decode_op = std::make_shared<DecodeOp>();
  616. std::vector<std::shared_ptr<TensorOp>> func_list;
  617. func_list.push_back(decode_op);
  618. std::shared_ptr<MapOp> map_decode_map;
  619. MapOp::Builder map_decode_builder;
  620. map_decode_builder.SetInColNames({})
  621. .SetOutColNames({})
  622. .SetTensorFuncs(func_list)
  623. .SetNumWorkers(4);
  624. rc = map_decode_builder.Build(&map_decode_map);
  625. EXPECT_TRUE(rc.IsOk());
  626. auto resize_op = std::make_shared<ResizeOp>(300, 300);
  627. std::vector<std::shared_ptr<TensorOp>> func_list2;
  628. func_list2.push_back(resize_op);
  629. std::shared_ptr<MapOp> map_resize_op;
  630. MapOp::Builder map_resize_builder;
  631. map_resize_builder.SetTensorFuncs(func_list2).SetNumWorkers(5);
  632. rc = map_resize_builder.Build(&map_resize_op);
  633. EXPECT_TRUE(rc.IsOk());
  634. my_tree_ = Build({ImageFolder(16, 2, 32, folder_path, false), map_decode_map, repeat_op, map_resize_op});
  635. rc = my_tree_->Prepare();
  636. EXPECT_TRUE(rc.IsOk());
  637. rc = my_tree_->Launch();
  638. EXPECT_TRUE(rc.IsOk());
  639. // Start the loop of reading tensors from our pipeline
  640. DatasetIterator di(my_tree_);
  641. TensorMap tensor_map;
  642. di.GetNextAsMap(&tensor_map);
  643. EXPECT_TRUE(rc.IsOk());
  644. uint64_t i = 0;
  645. int32_t label = 0;
  646. int32_t img_class[] = {0, 1, 2, 3};
  647. std::string result;
  648. while (tensor_map.size() != 0) {
  649. tensor_map["label"]->GetItemAt<int32_t>(&label, {});
  650. EXPECT_TRUE(img_class[(i % 44) / 11] == label);
  651. di.GetNextAsMap(&tensor_map);
  652. i++;
  653. }
  654. EXPECT_TRUE(i == 88);
  655. }