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.

c_api_test.cc 50 kB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566
  1. /**
  2. * Copyright 2020 Huawei Technologies Co., Ltd
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include <fstream>
  17. #include <iostream>
  18. #include <memory>
  19. #include <vector>
  20. #include <string>
  21. #include "utils/log_adapter.h"
  22. #include "common/utils.h"
  23. #include "common/common.h"
  24. #include "gtest/gtest.h"
  25. #include "securec.h"
  26. #include "minddata/dataset/include/datasets.h"
  27. #include "minddata/dataset/include/status.h"
  28. #include "minddata/dataset/include/transforms.h"
  29. #include "minddata/dataset/include/iterator.h"
  30. #include "minddata/dataset/core/constants.h"
  31. #include "minddata/dataset/core/tensor_shape.h"
  32. #include "minddata/dataset/core/tensor.h"
  33. #include "minddata/dataset/include/samplers.h"
  34. #include "minddata/dataset/engine/datasetops/source/voc_op.h"
  35. using namespace mindspore::dataset::api;
  36. using mindspore::MsLogLevel::ERROR;
  37. using mindspore::ExceptionType::NoExceptionType;
  38. using mindspore::LogStream;
  39. using mindspore::dataset::Tensor;
  40. using mindspore::dataset::TensorShape;
  41. using mindspore::dataset::TensorImpl;
  42. using mindspore::dataset::DataType;
  43. using mindspore::dataset::Status;
  44. using mindspore::dataset::BorderType;
  45. class MindDataTestPipeline : public UT::DatasetOpTesting {
  46. protected:
  47. };
  48. TEST_F(MindDataTestPipeline, TestBatchAndRepeat) {
  49. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestBatchAndRepeat.";
  50. // Create a Mnist Dataset
  51. std::string folder_path = datasets_root_path_ + "/testMnistData/";
  52. std::shared_ptr<Dataset> ds = Mnist(folder_path, RandomSampler(false, 10));
  53. EXPECT_NE(ds, nullptr);
  54. // Create a Repeat operation on ds
  55. int32_t repeat_num = 2;
  56. ds = ds->Repeat(repeat_num);
  57. EXPECT_NE(ds, nullptr);
  58. // Create a Batch operation on ds
  59. int32_t batch_size = 2;
  60. ds = ds->Batch(batch_size);
  61. EXPECT_NE(ds, nullptr);
  62. // Create an iterator over the result of the above dataset
  63. // This will trigger the creation of the Execution Tree and launch it.
  64. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  65. EXPECT_NE(iter, nullptr);
  66. // Iterate the dataset and get each row
  67. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  68. iter->GetNextRow(&row);
  69. uint64_t i = 0;
  70. while (row.size() != 0) {
  71. i++;
  72. auto image = row["image"];
  73. MS_LOG(INFO) << "Tensor image shape: " << image->shape();
  74. iter->GetNextRow(&row);
  75. }
  76. EXPECT_EQ(i, 10);
  77. // Manually terminate the pipeline
  78. iter->Stop();
  79. }
  80. TEST_F(MindDataTestPipeline, TestMnistFail1) {
  81. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestMnistFail1.";
  82. // Create a Mnist Dataset
  83. std::shared_ptr<Dataset> ds = Mnist("", RandomSampler(false, 10));
  84. EXPECT_EQ(ds, nullptr);
  85. }
  86. TEST_F(MindDataTestPipeline, TestTensorOpsAndMap) {
  87. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestTensorOpsAndMap.";
  88. // Create a Mnist Dataset
  89. std::string folder_path = datasets_root_path_ + "/testMnistData/";
  90. std::shared_ptr<Dataset> ds = Mnist(folder_path, RandomSampler(false, 20));
  91. EXPECT_NE(ds, nullptr);
  92. // Create a Repeat operation on ds
  93. int32_t repeat_num = 2;
  94. ds = ds->Repeat(repeat_num);
  95. EXPECT_NE(ds, nullptr);
  96. // Create objects for the tensor ops
  97. std::shared_ptr<TensorOperation> resize_op = vision::Resize({30, 30});
  98. EXPECT_NE(resize_op, nullptr);
  99. std::shared_ptr<TensorOperation> center_crop_op = vision::CenterCrop({16, 16});
  100. EXPECT_NE(center_crop_op, nullptr);
  101. // Create a Map operation on ds
  102. ds = ds->Map({resize_op, center_crop_op});
  103. EXPECT_NE(ds, nullptr);
  104. // Create a Batch operation on ds
  105. int32_t batch_size = 1;
  106. ds = ds->Batch(batch_size);
  107. EXPECT_NE(ds, nullptr);
  108. // Create an iterator over the result of the above dataset
  109. // This will trigger the creation of the Execution Tree and launch it.
  110. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  111. EXPECT_NE(iter, nullptr);
  112. // Iterate the dataset and get each row
  113. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  114. iter->GetNextRow(&row);
  115. uint64_t i = 0;
  116. while (row.size() != 0) {
  117. i++;
  118. auto image = row["image"];
  119. MS_LOG(INFO) << "Tensor image shape: " << image->shape();
  120. iter->GetNextRow(&row);
  121. }
  122. EXPECT_EQ(i, 40);
  123. // Manually terminate the pipeline
  124. iter->Stop();
  125. }
  126. TEST_F(MindDataTestPipeline, TestUniformAugWithOps) {
  127. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestUniformAugWithOps.";
  128. // Create a Mnist Dataset
  129. std::string folder_path = datasets_root_path_ + "/testMnistData/";
  130. std::shared_ptr<Dataset> ds = Mnist(folder_path, RandomSampler(false, 20));
  131. EXPECT_NE(ds, nullptr);
  132. // Create a Repeat operation on ds
  133. int32_t repeat_num = 1;
  134. ds = ds->Repeat(repeat_num);
  135. EXPECT_NE(ds, nullptr);
  136. // Create objects for the tensor ops
  137. std::shared_ptr<TensorOperation> resize_op = vision::Resize({30, 30});
  138. EXPECT_NE(resize_op, nullptr);
  139. std::shared_ptr<TensorOperation> random_crop_op = vision::RandomCrop({28, 28});
  140. EXPECT_NE(random_crop_op, nullptr);
  141. std::shared_ptr<TensorOperation> center_crop_op = vision::CenterCrop({16, 16});
  142. EXPECT_NE(center_crop_op, nullptr);
  143. std::shared_ptr<TensorOperation> uniform_aug_op = vision::UniformAugment({random_crop_op, center_crop_op}, 2);
  144. EXPECT_NE(uniform_aug_op, nullptr);
  145. // Create a Map operation on ds
  146. ds = ds->Map({resize_op, uniform_aug_op});
  147. EXPECT_NE(ds, nullptr);
  148. // Create an iterator over the result of the above dataset
  149. // This will trigger the creation of the Execution Tree and launch it.
  150. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  151. EXPECT_NE(iter, nullptr);
  152. // Iterate the dataset and get each row
  153. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  154. iter->GetNextRow(&row);
  155. uint64_t i = 0;
  156. while (row.size() != 0) {
  157. i++;
  158. auto image = row["image"];
  159. MS_LOG(INFO) << "Tensor image shape: " << image->shape();
  160. iter->GetNextRow(&row);
  161. }
  162. EXPECT_EQ(i, 20);
  163. // Manually terminate the pipeline
  164. iter->Stop();
  165. }
  166. TEST_F(MindDataTestPipeline, TestRandomFlip) {
  167. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestRandomFlip.";
  168. // Create an ImageFolder Dataset
  169. std::string folder_path = datasets_root_path_ + "/testPK/data/";
  170. std::shared_ptr<Dataset> ds = ImageFolder(folder_path, true, RandomSampler(false, 10));
  171. EXPECT_NE(ds, nullptr);
  172. // Create a Repeat operation on ds
  173. int32_t repeat_num = 2;
  174. ds = ds->Repeat(repeat_num);
  175. EXPECT_NE(ds, nullptr);
  176. // Create objects for the tensor ops
  177. std::shared_ptr<TensorOperation> random_vertical_flip_op = vision::RandomVerticalFlip(0.5);
  178. EXPECT_NE(random_vertical_flip_op, nullptr);
  179. std::shared_ptr<TensorOperation> random_horizontal_flip_op = vision::RandomHorizontalFlip(0.5);
  180. EXPECT_NE(random_horizontal_flip_op, nullptr);
  181. // Create a Map operation on ds
  182. ds = ds->Map({random_vertical_flip_op, random_horizontal_flip_op});
  183. EXPECT_NE(ds, nullptr);
  184. // Create a Batch operation on ds
  185. int32_t batch_size = 1;
  186. ds = ds->Batch(batch_size);
  187. EXPECT_NE(ds, nullptr);
  188. // Create an iterator over the result of the above dataset
  189. // This will trigger the creation of the Execution Tree and launch it.
  190. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  191. EXPECT_NE(iter, nullptr);
  192. // Iterate the dataset and get each row
  193. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  194. iter->GetNextRow(&row);
  195. uint64_t i = 0;
  196. while (row.size() != 0) {
  197. i++;
  198. auto image = row["image"];
  199. MS_LOG(INFO) << "Tensor image shape: " << image->shape();
  200. iter->GetNextRow(&row);
  201. }
  202. EXPECT_EQ(i, 20);
  203. // Manually terminate the pipeline
  204. iter->Stop();
  205. }
  206. TEST_F(MindDataTestPipeline, TestImageFolderBatchAndRepeat) {
  207. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestImageFolderBatchAndRepeat.";
  208. // Create an ImageFolder Dataset
  209. std::string folder_path = datasets_root_path_ + "/testPK/data/";
  210. std::shared_ptr<Dataset> ds = ImageFolder(folder_path, true, RandomSampler(false, 10));
  211. EXPECT_NE(ds, nullptr);
  212. // Create a Repeat operation on ds
  213. int32_t repeat_num = 2;
  214. ds = ds->Repeat(repeat_num);
  215. EXPECT_NE(ds, nullptr);
  216. // Create a Batch operation on ds
  217. int32_t batch_size = 2;
  218. ds = ds->Batch(batch_size);
  219. EXPECT_NE(ds, nullptr);
  220. // Create an iterator over the result of the above dataset
  221. // This will trigger the creation of the Execution Tree and launch it.
  222. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  223. EXPECT_NE(iter, nullptr);
  224. // Iterate the dataset and get each row
  225. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  226. iter->GetNextRow(&row);
  227. uint64_t i = 0;
  228. while (row.size() != 0) {
  229. i++;
  230. auto image = row["image"];
  231. MS_LOG(INFO) << "Tensor image shape: " << image->shape();
  232. iter->GetNextRow(&row);
  233. }
  234. EXPECT_EQ(i, 10);
  235. // Manually terminate the pipeline
  236. iter->Stop();
  237. }
  238. TEST_F(MindDataTestPipeline, TestImageFolderFail1) {
  239. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestImageFolderFail1.";
  240. // Create an ImageFolder Dataset
  241. std::shared_ptr<Dataset> ds = ImageFolder("", true, nullptr);
  242. EXPECT_EQ(ds, nullptr);
  243. }
  244. TEST_F(MindDataTestPipeline, TestImageFolderWithSamplers) {
  245. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestImageFolderWithSamplers.";
  246. std::shared_ptr<SamplerObj> sampl = DistributedSampler(2, 1);
  247. EXPECT_NE(sampl, nullptr);
  248. sampl = PKSampler(3);
  249. EXPECT_NE(sampl, nullptr);
  250. sampl = RandomSampler(false, 12);
  251. EXPECT_NE(sampl, nullptr);
  252. sampl = SequentialSampler(0, 12);
  253. EXPECT_NE(sampl, nullptr);
  254. std::vector<double> weights = {0.9, 0.8, 0.68, 0.7, 0.71, 0.6, 0.5, 0.4, 0.3, 0.5, 0.2, 0.1};
  255. sampl = WeightedRandomSampler(weights, 12);
  256. EXPECT_NE(sampl, nullptr);
  257. std::vector<int64_t> indices = {1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23};
  258. sampl = SubsetRandomSampler(indices);
  259. EXPECT_NE(sampl, nullptr);
  260. // Create an ImageFolder Dataset
  261. std::string folder_path = datasets_root_path_ + "/testPK/data/";
  262. std::shared_ptr<Dataset> ds = ImageFolder(folder_path, false, sampl);
  263. EXPECT_NE(ds, nullptr);
  264. // Create a Repeat operation on ds
  265. int32_t repeat_num = 2;
  266. ds = ds->Repeat(repeat_num);
  267. EXPECT_NE(ds, nullptr);
  268. // Create a Batch operation on ds
  269. int32_t batch_size = 2;
  270. ds = ds->Batch(batch_size);
  271. EXPECT_NE(ds, nullptr);
  272. // Create an iterator over the result of the above dataset
  273. // This will trigger the creation of the Execution Tree and launch it.
  274. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  275. EXPECT_NE(iter, nullptr);
  276. // Iterate the dataset and get each row
  277. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  278. iter->GetNextRow(&row);
  279. uint64_t i = 0;
  280. while (row.size() != 0) {
  281. i++;
  282. auto image = row["image"];
  283. MS_LOG(INFO) << "Tensor image shape: " << image->shape();
  284. iter->GetNextRow(&row);
  285. }
  286. EXPECT_EQ(i, 12);
  287. // Manually terminate the pipeline
  288. iter->Stop();
  289. }
  290. TEST_F(MindDataTestPipeline, TestPad) {
  291. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestPad.";
  292. // Create an ImageFolder Dataset
  293. std::string folder_path = datasets_root_path_ + "/testPK/data/";
  294. std::shared_ptr<Dataset> ds = ImageFolder(folder_path, true, RandomSampler(false, 10));
  295. EXPECT_NE(ds, nullptr);
  296. // Create a Repeat operation on ds
  297. int32_t repeat_num = 2;
  298. ds = ds->Repeat(repeat_num);
  299. EXPECT_NE(ds, nullptr);
  300. // Create objects for the tensor ops
  301. std::shared_ptr<TensorOperation> pad_op1 = vision::Pad({1, 2, 3, 4}, {0}, BorderType::kSymmetric);
  302. EXPECT_NE(pad_op1, nullptr);
  303. std::shared_ptr<TensorOperation> pad_op2 = vision::Pad({1}, {1, 1, 1}, BorderType::kEdge);
  304. EXPECT_NE(pad_op2, nullptr);
  305. std::shared_ptr<TensorOperation> pad_op3 = vision::Pad({1, 4});
  306. EXPECT_NE(pad_op3, nullptr);
  307. // Create a Map operation on ds
  308. ds = ds->Map({pad_op1, pad_op2, pad_op3});
  309. EXPECT_NE(ds, nullptr);
  310. // Create a Batch operation on ds
  311. int32_t batch_size = 1;
  312. ds = ds->Batch(batch_size);
  313. EXPECT_NE(ds, nullptr);
  314. // Create an iterator over the result of the above dataset
  315. // This will trigger the creation of the Execution Tree and launch it.
  316. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  317. EXPECT_NE(iter, nullptr);
  318. // Iterate the dataset and get each row
  319. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  320. iter->GetNextRow(&row);
  321. uint64_t i = 0;
  322. while (row.size() != 0) {
  323. i++;
  324. auto image = row["image"];
  325. MS_LOG(INFO) << "Tensor image shape: " << image->shape();
  326. iter->GetNextRow(&row);
  327. }
  328. EXPECT_EQ(i, 20);
  329. // Manually terminate the pipeline
  330. iter->Stop();
  331. }
  332. TEST_F(MindDataTestPipeline, TestCutOut) {
  333. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestCutOut.";
  334. // Create an ImageFolder Dataset
  335. std::string folder_path = datasets_root_path_ + "/testPK/data/";
  336. std::shared_ptr<Dataset> ds = ImageFolder(folder_path, true, RandomSampler(false, 10));
  337. EXPECT_NE(ds, nullptr);
  338. // Create a Repeat operation on ds
  339. int32_t repeat_num = 2;
  340. ds = ds->Repeat(repeat_num);
  341. EXPECT_NE(ds, nullptr);
  342. // Create objects for the tensor ops
  343. std::shared_ptr<TensorOperation> cut_out1 = vision::CutOut(30, 5);
  344. EXPECT_NE(cut_out1, nullptr);
  345. std::shared_ptr<TensorOperation> cut_out2 = vision::CutOut(30);
  346. EXPECT_NE(cut_out2, nullptr);
  347. // Create a Map operation on ds
  348. ds = ds->Map({cut_out1, cut_out2});
  349. EXPECT_NE(ds, nullptr);
  350. // Create a Batch operation on ds
  351. int32_t batch_size = 1;
  352. ds = ds->Batch(batch_size);
  353. EXPECT_NE(ds, nullptr);
  354. // Create an iterator over the result of the above dataset
  355. // This will trigger the creation of the Execution Tree and launch it.
  356. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  357. EXPECT_NE(iter, nullptr);
  358. // Iterate the dataset and get each row
  359. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  360. iter->GetNextRow(&row);
  361. uint64_t i = 0;
  362. while (row.size() != 0) {
  363. i++;
  364. auto image = row["image"];
  365. MS_LOG(INFO) << "Tensor image shape: " << image->shape();
  366. iter->GetNextRow(&row);
  367. }
  368. EXPECT_EQ(i, 20);
  369. // Manually terminate the pipeline
  370. iter->Stop();
  371. }
  372. TEST_F(MindDataTestPipeline, TestNormalize) {
  373. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestNormalize.";
  374. // Create an ImageFolder Dataset
  375. std::string folder_path = datasets_root_path_ + "/testPK/data/";
  376. std::shared_ptr<Dataset> ds = ImageFolder(folder_path, true, RandomSampler(false, 10));
  377. EXPECT_NE(ds, nullptr);
  378. // Create a Repeat operation on ds
  379. int32_t repeat_num = 2;
  380. ds = ds->Repeat(repeat_num);
  381. EXPECT_NE(ds, nullptr);
  382. // Create objects for the tensor ops
  383. std::shared_ptr<TensorOperation> normalize = vision::Normalize({121.0, 115.0, 100.0}, {70.0, 68.0, 71.0});
  384. EXPECT_NE(normalize, nullptr);
  385. // Create a Map operation on ds
  386. ds = ds->Map({normalize});
  387. EXPECT_NE(ds, nullptr);
  388. // Create a Batch operation on ds
  389. int32_t batch_size = 1;
  390. ds = ds->Batch(batch_size);
  391. EXPECT_NE(ds, nullptr);
  392. // Create an iterator over the result of the above dataset
  393. // This will trigger the creation of the Execution Tree and launch it.
  394. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  395. EXPECT_NE(iter, nullptr);
  396. // Iterate the dataset and get each row
  397. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  398. iter->GetNextRow(&row);
  399. uint64_t i = 0;
  400. while (row.size() != 0) {
  401. i++;
  402. auto image = row["image"];
  403. MS_LOG(INFO) << "Tensor image shape: " << image->shape();
  404. iter->GetNextRow(&row);
  405. }
  406. EXPECT_EQ(i, 20);
  407. // Manually terminate the pipeline
  408. iter->Stop();
  409. }
  410. TEST_F(MindDataTestPipeline, TestDecode) {
  411. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestDecode.";
  412. // Create an ImageFolder Dataset
  413. std::string folder_path = datasets_root_path_ + "/testPK/data/";
  414. std::shared_ptr<Dataset> ds = ImageFolder(folder_path, false, RandomSampler(false, 10));
  415. EXPECT_NE(ds, nullptr);
  416. // Create a Repeat operation on ds
  417. int32_t repeat_num = 2;
  418. ds = ds->Repeat(repeat_num);
  419. EXPECT_NE(ds, nullptr);
  420. // Create objects for the tensor ops
  421. std::shared_ptr<TensorOperation> decode = vision::Decode(true);
  422. EXPECT_NE(decode, nullptr);
  423. // Create a Map operation on ds
  424. ds = ds->Map({decode});
  425. EXPECT_NE(ds, nullptr);
  426. // Create a Batch operation on ds
  427. int32_t batch_size = 1;
  428. ds = ds->Batch(batch_size);
  429. EXPECT_NE(ds, nullptr);
  430. // Create an iterator over the result of the above dataset
  431. // This will trigger the creation of the Execution Tree and launch it.
  432. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  433. EXPECT_NE(iter, nullptr);
  434. // Iterate the dataset and get each row
  435. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  436. iter->GetNextRow(&row);
  437. uint64_t i = 0;
  438. while (row.size() != 0) {
  439. i++;
  440. auto image = row["image"];
  441. MS_LOG(INFO) << "Tensor image shape: " << image->shape();
  442. iter->GetNextRow(&row);
  443. }
  444. EXPECT_EQ(i, 20);
  445. // Manually terminate the pipeline
  446. iter->Stop();
  447. }
  448. TEST_F(MindDataTestPipeline, TestShuffleDataset) {
  449. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestShuffleDataset.";
  450. // Create an ImageFolder Dataset
  451. std::string folder_path = datasets_root_path_ + "/testPK/data/";
  452. std::shared_ptr<Dataset> ds = ImageFolder(folder_path, true, RandomSampler(false, 10));
  453. EXPECT_NE(ds, nullptr);
  454. // Create a Shuffle operation on ds
  455. int32_t shuffle_size = 10;
  456. ds = ds->Shuffle(shuffle_size);
  457. EXPECT_NE(ds, nullptr);
  458. // Create a Repeat operation on ds
  459. int32_t repeat_num = 2;
  460. ds = ds->Repeat(repeat_num);
  461. EXPECT_NE(ds, nullptr);
  462. // Create a Batch operation on ds
  463. int32_t batch_size = 2;
  464. ds = ds->Batch(batch_size);
  465. EXPECT_NE(ds, nullptr);
  466. // Create an iterator over the result of the above dataset
  467. // This will trigger the creation of the Execution Tree and launch it.
  468. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  469. EXPECT_NE(iter, nullptr);
  470. // Iterate the dataset and get each row
  471. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  472. iter->GetNextRow(&row);
  473. uint64_t i = 0;
  474. while (row.size() != 0) {
  475. i++;
  476. auto image = row["image"];
  477. MS_LOG(INFO) << "Tensor image shape: " << image->shape();
  478. iter->GetNextRow(&row);
  479. }
  480. EXPECT_EQ(i, 10);
  481. // Manually terminate the pipeline
  482. iter->Stop();
  483. }
  484. TEST_F(MindDataTestPipeline, TestSkipDataset) {
  485. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestSkipDataset.";
  486. // Create an ImageFolder Dataset
  487. std::string folder_path = datasets_root_path_ + "/testPK/data/";
  488. std::shared_ptr<Dataset> ds = ImageFolder(folder_path, true, RandomSampler(false, 10));
  489. EXPECT_NE(ds, nullptr);
  490. // Create a Skip operation on ds
  491. int32_t count = 3;
  492. ds = ds->Skip(count);
  493. EXPECT_NE(ds, nullptr);
  494. // Create an iterator over the result of the above dataset
  495. // This will trigger the creation of the Execution Tree and launch it.
  496. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  497. EXPECT_NE(iter, nullptr);
  498. // Iterate the dataset and get each row
  499. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  500. iter->GetNextRow(&row);
  501. uint64_t i = 0;
  502. while (row.size() != 0) {
  503. i++;
  504. auto image = row["image"];
  505. MS_LOG(INFO) << "Tensor image shape: " << image->shape();
  506. iter->GetNextRow(&row);
  507. }
  508. MS_LOG(INFO) << "Number of rows: " << i;
  509. // Expect 10-3=7 rows
  510. EXPECT_EQ(i, 7);
  511. // Manually terminate the pipeline
  512. iter->Stop();
  513. }
  514. TEST_F(MindDataTestPipeline, TestSkipDatasetError1) {
  515. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestSkipDatasetError1.";
  516. // Create an ImageFolder Dataset
  517. std::string folder_path = datasets_root_path_ + "/testPK/data/";
  518. std::shared_ptr<Dataset> ds = ImageFolder(folder_path, true, RandomSampler(false, 10));
  519. EXPECT_NE(ds, nullptr);
  520. // Create a Skip operation on ds with invalid count input
  521. int32_t count = -1;
  522. ds = ds->Skip(count);
  523. // Expect nullptr for invalid input skip_count
  524. EXPECT_EQ(ds, nullptr);
  525. }
  526. TEST_F(MindDataTestPipeline, TestTakeDatasetDefault) {
  527. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestTakeDatasetDefault.";
  528. // Create an ImageFolder Dataset
  529. std::string folder_path = datasets_root_path_ + "/testPK/data/";
  530. std::shared_ptr<Dataset> ds = ImageFolder(folder_path, true, RandomSampler(false, 7));
  531. EXPECT_NE(ds, nullptr);
  532. // Create a Take operation on ds, dafault count = -1
  533. ds = ds->Take();
  534. EXPECT_NE(ds, nullptr);
  535. // Create an iterator over the result of the above dataset
  536. // This will trigger the creation of the Execution Tree and launch it.
  537. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  538. EXPECT_NE(iter, nullptr);
  539. // Iterate the dataset and get each row
  540. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  541. iter->GetNextRow(&row);
  542. uint64_t i = 0;
  543. while (row.size() != 0) {
  544. i++;
  545. auto image = row["image"];
  546. MS_LOG(INFO) << "Tensor image shape: " << image->shape();
  547. iter->GetNextRow(&row);
  548. }
  549. MS_LOG(INFO) << "Number of rows: " << i;
  550. // Expect 7 rows
  551. EXPECT_EQ(i, 7);
  552. // Manually terminate the pipeline
  553. iter->Stop();
  554. }
  555. TEST_F(MindDataTestPipeline, TestTakeDatasetNormal) {
  556. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestTakeDatasetNormal.";
  557. // Create an ImageFolder Dataset
  558. std::string folder_path = datasets_root_path_ + "/testPK/data/";
  559. std::shared_ptr<Dataset> ds = ImageFolder(folder_path, true, RandomSampler(false, 8));
  560. EXPECT_NE(ds, nullptr);
  561. // Create a Take operation on ds
  562. ds = ds->Take(5);
  563. EXPECT_NE(ds, nullptr);
  564. // Create an iterator over the result of the above dataset
  565. // This will trigger the creation of the Execution Tree and launch it.
  566. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  567. EXPECT_NE(iter, nullptr);
  568. // Iterate the dataset and get each row
  569. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  570. iter->GetNextRow(&row);
  571. uint64_t i = 0;
  572. while (row.size() != 0) {
  573. i++;
  574. auto image = row["image"];
  575. MS_LOG(INFO) << "Tensor image shape: " << image->shape();
  576. iter->GetNextRow(&row);
  577. }
  578. MS_LOG(INFO) << "Number of rows: " << i;
  579. // Expect 5 rows
  580. EXPECT_EQ(i, 5);
  581. // Manually terminate the pipeline
  582. iter->Stop();
  583. }
  584. TEST_F(MindDataTestPipeline, TestTakeDatasetError1) {
  585. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestTakeDatasetError1.";
  586. // Create an ImageFolder Dataset
  587. std::string folder_path = datasets_root_path_ + "/testPK/data/";
  588. std::shared_ptr<Dataset> ds = ImageFolder(folder_path, true, RandomSampler(false, 10));
  589. EXPECT_NE(ds, nullptr);
  590. // Create a Take operation on ds with invalid count input
  591. int32_t count = -5;
  592. ds = ds->Take(count);
  593. // Expect nullptr for invalid input take_count
  594. EXPECT_EQ(ds, nullptr);
  595. }
  596. TEST_F(MindDataTestPipeline, TestCifar10Dataset) {
  597. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestCifar10Dataset.";
  598. // Create a Cifar10 Dataset
  599. std::string folder_path = datasets_root_path_ + "/testCifar10Data/";
  600. std::shared_ptr<Dataset> ds = Cifar10(folder_path, RandomSampler(false, 10));
  601. EXPECT_NE(ds, nullptr);
  602. // Create an iterator over the result of the above dataset
  603. // This will trigger the creation of the Execution Tree and launch it.
  604. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  605. EXPECT_NE(iter, nullptr);
  606. // Iterate the dataset and get each row
  607. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  608. iter->GetNextRow(&row);
  609. EXPECT_NE(row.find("image"), row.end());
  610. EXPECT_NE(row.find("label"), row.end());
  611. uint64_t i = 0;
  612. while (row.size() != 0) {
  613. i++;
  614. auto image = row["image"];
  615. MS_LOG(INFO) << "Tensor image shape: " << image->shape();
  616. iter->GetNextRow(&row);
  617. }
  618. EXPECT_EQ(i, 10);
  619. // Manually terminate the pipeline
  620. iter->Stop();
  621. }
  622. TEST_F(MindDataTestPipeline, TestCifar10DatasetFail1) {
  623. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestCifar10DatasetFail1.";
  624. // Create a Cifar10 Dataset
  625. std::shared_ptr<Dataset> ds = Cifar10("", RandomSampler(false, 10));
  626. EXPECT_EQ(ds, nullptr);
  627. }
  628. TEST_F(MindDataTestPipeline, TestCifar100Dataset) {
  629. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestCifar100Dataset.";
  630. // Create a Cifar100 Dataset
  631. std::string folder_path = datasets_root_path_ + "/testCifar100Data/";
  632. std::shared_ptr<Dataset> ds = Cifar100(folder_path, RandomSampler(false, 10));
  633. EXPECT_NE(ds, nullptr);
  634. // Create an iterator over the result of the above dataset
  635. // This will trigger the creation of the Execution Tree and launch it.
  636. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  637. EXPECT_NE(iter, nullptr);
  638. // Iterate the dataset and get each row
  639. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  640. iter->GetNextRow(&row);
  641. EXPECT_NE(row.find("image"), row.end());
  642. EXPECT_NE(row.find("coarse_label"), row.end());
  643. EXPECT_NE(row.find("fine_label"), row.end());
  644. uint64_t i = 0;
  645. while (row.size() != 0) {
  646. i++;
  647. auto image = row["image"];
  648. MS_LOG(INFO) << "Tensor image shape: " << image->shape();
  649. iter->GetNextRow(&row);
  650. }
  651. EXPECT_EQ(i, 10);
  652. // Manually terminate the pipeline
  653. iter->Stop();
  654. }
  655. TEST_F(MindDataTestPipeline, TestCifar100DatasetFail1) {
  656. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestCifar100DatasetFail1.";
  657. // Create a Cifar100 Dataset
  658. std::shared_ptr<Dataset> ds = Cifar100("", RandomSampler(false, 10));
  659. EXPECT_EQ(ds, nullptr);
  660. }
  661. TEST_F(MindDataTestPipeline, TestRandomColorAdjust) {
  662. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestRandomColorAdjust.";
  663. // Create an ImageFolder Dataset
  664. std::string folder_path = datasets_root_path_ + "/testPK/data/";
  665. std::shared_ptr<Dataset> ds = ImageFolder(folder_path, true, RandomSampler(false, 10));
  666. EXPECT_NE(ds, nullptr);
  667. // Create a Repeat operation on ds
  668. int32_t repeat_num = 2;
  669. ds = ds->Repeat(repeat_num);
  670. EXPECT_NE(ds, nullptr);
  671. // Create objects for the tensor ops
  672. std::shared_ptr<TensorOperation> random_color_adjust1 = vision::RandomColorAdjust({1.0}, {0.0}, {0.5}, {0.5});
  673. EXPECT_NE(random_color_adjust1, nullptr);
  674. std::shared_ptr<TensorOperation> random_color_adjust2 = vision::RandomColorAdjust({1.0, 1.0}, {0.0, 0.0}, {0.5, 0.5},
  675. {0.5, 0.5});
  676. EXPECT_NE(random_color_adjust2, nullptr);
  677. std::shared_ptr<TensorOperation> random_color_adjust3 = vision::RandomColorAdjust({0.5, 1.0}, {0.0, 0.5}, {0.25, 0.5},
  678. {0.25, 0.5});
  679. EXPECT_NE(random_color_adjust3, nullptr);
  680. std::shared_ptr<TensorOperation> random_color_adjust4 = vision::RandomColorAdjust();
  681. EXPECT_NE(random_color_adjust4, nullptr);
  682. // Create a Map operation on ds
  683. ds = ds->Map({random_color_adjust1, random_color_adjust2, random_color_adjust3, random_color_adjust4});
  684. EXPECT_NE(ds, nullptr);
  685. // Create a Batch operation on ds
  686. int32_t batch_size = 1;
  687. ds = ds->Batch(batch_size);
  688. EXPECT_NE(ds, nullptr);
  689. // Create an iterator over the result of the above dataset
  690. // This will trigger the creation of the Execution Tree and launch it.
  691. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  692. EXPECT_NE(iter, nullptr);
  693. // Iterate the dataset and get each row
  694. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  695. iter->GetNextRow(&row);
  696. uint64_t i = 0;
  697. while (row.size() != 0) {
  698. i++;
  699. auto image = row["image"];
  700. MS_LOG(INFO) << "Tensor image shape: " << image->shape();
  701. iter->GetNextRow(&row);
  702. }
  703. EXPECT_EQ(i, 20);
  704. // Manually terminate the pipeline
  705. iter->Stop();
  706. }
  707. TEST_F(MindDataTestPipeline, TestRandomRotation) {
  708. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestRandomRotation.";
  709. // Create an ImageFolder Dataset
  710. std::string folder_path = datasets_root_path_ + "/testPK/data/";
  711. std::shared_ptr<Dataset> ds = ImageFolder(folder_path, true, RandomSampler(false, 10));
  712. EXPECT_NE(ds, nullptr);
  713. // Create a Repeat operation on ds
  714. int32_t repeat_num = 2;
  715. ds = ds->Repeat(repeat_num);
  716. EXPECT_NE(ds, nullptr);
  717. // Create objects for the tensor ops
  718. std::shared_ptr<TensorOperation> random_rotation_op = vision::RandomRotation({-180, 180});
  719. EXPECT_NE(random_rotation_op, nullptr);
  720. // Create a Map operation on ds
  721. ds = ds->Map({random_rotation_op});
  722. EXPECT_NE(ds, nullptr);
  723. // Create a Batch operation on ds
  724. int32_t batch_size = 1;
  725. ds = ds->Batch(batch_size);
  726. EXPECT_NE(ds, nullptr);
  727. // Create an iterator over the result of the above dataset
  728. // This will trigger the creation of the Execution Tree and launch it.
  729. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  730. EXPECT_NE(iter, nullptr);
  731. // Iterate the dataset and get each row
  732. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  733. iter->GetNextRow(&row);
  734. uint64_t i = 0;
  735. while (row.size() != 0) {
  736. i++;
  737. auto image = row["image"];
  738. MS_LOG(INFO) << "Tensor image shape: " << image->shape();
  739. iter->GetNextRow(&row);
  740. }
  741. EXPECT_EQ(i, 20);
  742. // Manually terminate the pipeline
  743. iter->Stop();
  744. }
  745. TEST_F(MindDataTestPipeline, TestProjectMap) {
  746. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestProjectMap.";
  747. // Create an ImageFolder Dataset
  748. std::string folder_path = datasets_root_path_ + "/testPK/data/";
  749. std::shared_ptr<Dataset> ds = ImageFolder(folder_path, true, RandomSampler(false, 10));
  750. EXPECT_NE(ds, nullptr);
  751. // Create a Repeat operation on ds
  752. int32_t repeat_num = 2;
  753. ds = ds->Repeat(repeat_num);
  754. EXPECT_NE(ds, nullptr);
  755. // Create objects for the tensor ops
  756. std::shared_ptr<TensorOperation> random_vertical_flip_op = vision::RandomVerticalFlip(0.5);
  757. EXPECT_NE(random_vertical_flip_op, nullptr);
  758. // Create a Map operation on ds
  759. ds = ds->Map({random_vertical_flip_op}, {}, {}, {"image", "label"});
  760. EXPECT_NE(ds, nullptr);
  761. // Create a Project operation on ds
  762. std::vector<std::string> column_project = {"image"};
  763. ds = ds->Project(column_project);
  764. EXPECT_NE(ds, nullptr);
  765. // Create a Batch operation on ds
  766. int32_t batch_size = 1;
  767. ds = ds->Batch(batch_size);
  768. EXPECT_NE(ds, nullptr);
  769. // Create an iterator over the result of the above dataset
  770. // This will trigger the creation of the Execution Tree and launch it.
  771. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  772. EXPECT_NE(iter, nullptr);
  773. // Iterate the dataset and get each row
  774. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  775. iter->GetNextRow(&row);
  776. uint64_t i = 0;
  777. while (row.size() != 0) {
  778. i++;
  779. auto image = row["image"];
  780. MS_LOG(INFO) << "Tensor image shape: " << image->shape();
  781. iter->GetNextRow(&row);
  782. }
  783. EXPECT_EQ(i, 20);
  784. // Manually terminate the pipeline
  785. iter->Stop();
  786. }
  787. TEST_F(MindDataTestPipeline, TestZipSuccess) {
  788. // Testing the member zip() function
  789. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestZipSuccess.";
  790. // Create an ImageFolder Dataset
  791. std::string folder_path = datasets_root_path_ + "/testPK/data/";
  792. std::shared_ptr<Dataset> ds = ImageFolder(folder_path, true, RandomSampler(false, 10));
  793. EXPECT_NE(ds, nullptr);
  794. // Create a Project operation on ds
  795. std::vector<std::string> column_project = {"image"};
  796. ds = ds->Project(column_project);
  797. EXPECT_NE(ds, nullptr);
  798. // Create an ImageFolder Dataset
  799. std::shared_ptr<Dataset> ds1 = ImageFolder(folder_path, true, RandomSampler(false, 10));
  800. EXPECT_NE(ds1, nullptr);
  801. // Create a Rename operation on ds (so that the 3 datasets we are going to zip have distinct column names)
  802. ds1 = ds1->Rename({"image", "label"}, {"col1", "col2"});
  803. EXPECT_NE(ds1, nullptr);
  804. folder_path = datasets_root_path_ + "/testCifar10Data/";
  805. std::shared_ptr<Dataset> ds2 = Cifar10(folder_path, RandomSampler(false, 10));
  806. EXPECT_NE(ds2, nullptr);
  807. // Create a Project operation on ds
  808. column_project = {"label"};
  809. ds2 = ds2->Project(column_project);
  810. EXPECT_NE(ds2, nullptr);
  811. // Create a Zip operation on the datasets
  812. ds = ds->Zip({ds1, ds2});
  813. EXPECT_NE(ds, nullptr);
  814. // Create a Batch operation on ds
  815. int32_t batch_size = 1;
  816. ds = ds->Batch(batch_size);
  817. EXPECT_NE(ds, nullptr);
  818. // Create an iterator over the result of the above dataset
  819. // This will trigger the creation of the Execution Tree and launch it.
  820. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  821. EXPECT_NE(iter, nullptr);
  822. // Iterate the dataset and get each row
  823. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  824. iter->GetNextRow(&row);
  825. // Check zipped column names
  826. EXPECT_EQ(row.size(), 4);
  827. EXPECT_NE(row.find("image"), row.end());
  828. EXPECT_NE(row.find("label"), row.end());
  829. EXPECT_NE(row.find("col1"), row.end());
  830. EXPECT_NE(row.find("col2"), row.end());
  831. uint64_t i = 0;
  832. while (row.size() != 0) {
  833. i++;
  834. auto image = row["image"];
  835. MS_LOG(INFO) << "Tensor image shape: " << image->shape();
  836. iter->GetNextRow(&row);
  837. }
  838. EXPECT_EQ(i, 10);
  839. // Manually terminate the pipeline
  840. iter->Stop();
  841. }
  842. TEST_F(MindDataTestPipeline, TestZipSuccess2) {
  843. // Testing the static zip() function
  844. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestZipSuccess2.";
  845. // Create an ImageFolder Dataset
  846. std::string folder_path = datasets_root_path_ + "/testPK/data/";
  847. std::shared_ptr<Dataset> ds = ImageFolder(folder_path, true, RandomSampler(false, 9));
  848. EXPECT_NE(ds, nullptr);
  849. std::shared_ptr<Dataset> ds2 = ImageFolder(folder_path, true, RandomSampler(false, 10));
  850. EXPECT_NE(ds2, nullptr);
  851. // Create a Rename operation on ds (so that the 2 datasets we are going to zip have distinct column names)
  852. ds = ds->Rename({"image", "label"}, {"col1", "col2"});
  853. EXPECT_NE(ds, nullptr);
  854. // Create a Zip operation on the datasets
  855. ds = Zip({ds, ds2});
  856. EXPECT_NE(ds, nullptr);
  857. // Create a Batch operation on ds
  858. int32_t batch_size = 1;
  859. ds = ds->Batch(batch_size);
  860. EXPECT_NE(ds, nullptr);
  861. // Create an iterator over the result of the above dataset
  862. // This will trigger the creation of the Execution Tree and launch it.
  863. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  864. EXPECT_NE(iter, nullptr);
  865. // Iterate the dataset and get each row
  866. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  867. iter->GetNextRow(&row);
  868. // Check zipped column names
  869. EXPECT_EQ(row.size(), 4);
  870. EXPECT_NE(row.find("image"), row.end());
  871. EXPECT_NE(row.find("label"), row.end());
  872. EXPECT_NE(row.find("col1"), row.end());
  873. EXPECT_NE(row.find("col2"), row.end());
  874. uint64_t i = 0;
  875. while (row.size() != 0) {
  876. i++;
  877. auto image = row["image"];
  878. MS_LOG(INFO) << "Tensor image shape: " << image->shape();
  879. iter->GetNextRow(&row);
  880. }
  881. EXPECT_EQ(i, 9);
  882. // Manually terminate the pipeline
  883. iter->Stop();
  884. }
  885. TEST_F(MindDataTestPipeline, TestZipFail) {
  886. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestZipFail.";
  887. // We expect this test to fail because we are the both datasets we are zipping have "image" and "label" columns
  888. // and zip doesn't accept datasets with same column names
  889. // Create an ImageFolder Dataset
  890. std::string folder_path = datasets_root_path_ + "/testPK/data/";
  891. std::shared_ptr<Dataset> ds = ImageFolder(folder_path, true, RandomSampler(false, 10));
  892. EXPECT_NE(ds, nullptr);
  893. // Create an ImageFolder Dataset
  894. std::shared_ptr<Dataset> ds1 = ImageFolder(folder_path, true, RandomSampler(false, 10));
  895. EXPECT_NE(ds1, nullptr);
  896. // Create a Zip operation on the datasets
  897. ds = Zip({ds, ds1});
  898. EXPECT_NE(ds, nullptr);
  899. // Create a Batch operation on ds
  900. int32_t batch_size = 1;
  901. ds = ds->Batch(batch_size);
  902. EXPECT_NE(ds, nullptr);
  903. // Create an iterator over the result of the above dataset
  904. // This will trigger the creation of the Execution Tree and launch it.
  905. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  906. EXPECT_EQ(iter, nullptr);
  907. }
  908. TEST_F(MindDataTestPipeline, TestZipFail2) {
  909. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestZipFail2.";
  910. // This case is expected to fail because the input dataset is empty.
  911. // Create an ImageFolder Dataset
  912. std::string folder_path = datasets_root_path_ + "/testPK/data/";
  913. std::shared_ptr<Dataset> ds = ImageFolder(folder_path, true, RandomSampler(false, 10));
  914. EXPECT_NE(ds, nullptr);
  915. // Create a Zip operation on the datasets
  916. // Input dataset to zip is empty
  917. ds = Zip({});
  918. EXPECT_EQ(ds, nullptr);
  919. }
  920. TEST_F(MindDataTestPipeline, TestRenameSuccess) {
  921. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestRenameSuccess.";
  922. // Create an ImageFolder Dataset
  923. std::string folder_path = datasets_root_path_ + "/testPK/data/";
  924. std::shared_ptr<Dataset> ds = ImageFolder(folder_path, true, RandomSampler(false, 10));
  925. EXPECT_NE(ds, nullptr);
  926. // Create a Repeat operation on ds
  927. int32_t repeat_num = 2;
  928. ds = ds->Repeat(repeat_num);
  929. EXPECT_NE(ds, nullptr);
  930. // Create a Rename operation on ds
  931. ds = ds->Rename({"image", "label"}, {"col1", "col2"});
  932. EXPECT_NE(ds, nullptr);
  933. // Create a Batch operation on ds
  934. int32_t batch_size = 1;
  935. ds = ds->Batch(batch_size);
  936. EXPECT_NE(ds, nullptr);
  937. // Create an iterator over the result of the above dataset
  938. // This will trigger the creation of the Execution Tree and launch it.
  939. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  940. EXPECT_NE(iter, nullptr);
  941. // Iterate the dataset and get each row
  942. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  943. iter->GetNextRow(&row);
  944. uint64_t i = 0;
  945. EXPECT_NE(row.find("col1"), row.end());
  946. EXPECT_NE(row.find("col2"), row.end());
  947. EXPECT_EQ(row.find("image"), row.end());
  948. EXPECT_EQ(row.find("label"), row.end());
  949. while (row.size() != 0) {
  950. i++;
  951. auto image = row["col1"];
  952. MS_LOG(INFO) << "Tensor image shape: " << image->shape();
  953. iter->GetNextRow(&row);
  954. }
  955. EXPECT_EQ(i, 20);
  956. // Manually terminate the pipeline
  957. iter->Stop();
  958. }
  959. TEST_F(MindDataTestPipeline, TestRenameFail) {
  960. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestRenameFail.";
  961. // We expect this test to fail because input and output in Rename are not the same size
  962. // Create an ImageFolder Dataset
  963. std::string folder_path = datasets_root_path_ + "/testPK/data/";
  964. std::shared_ptr<Dataset> ds = ImageFolder(folder_path, true, RandomSampler(false, 10));
  965. EXPECT_NE(ds, nullptr);
  966. // Create a Repeat operation on ds
  967. int32_t repeat_num = 2;
  968. ds = ds->Repeat(repeat_num);
  969. EXPECT_NE(ds, nullptr);
  970. // Create a Rename operation on ds
  971. ds = ds->Rename({"image", "label"}, {"col2"});
  972. EXPECT_EQ(ds, nullptr);
  973. }
  974. TEST_F(MindDataTestPipeline, TestVOCSegmentation) {
  975. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestVOCSegmentation.";
  976. // Create a VOC Dataset
  977. std::string folder_path = datasets_root_path_ + "/testVOC2012_2";
  978. std::shared_ptr<Dataset> ds = VOC(folder_path, "Segmentation", "train", {}, false, SequentialSampler(0, 3));
  979. EXPECT_NE(ds, nullptr);
  980. // Create a Repeat operation on ds
  981. int32_t repeat_num = 2;
  982. ds = ds->Repeat(repeat_num);
  983. EXPECT_NE(ds, nullptr);
  984. // Create an iterator over the result of the above dataset
  985. // This will trigger the creation of the Execution Tree and launch it.
  986. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  987. EXPECT_NE(iter, nullptr);
  988. // Iterate the dataset and get each row
  989. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  990. iter->GetNextRow(&row);
  991. // Check if VOCOp read correct images/targets
  992. using Tensor = mindspore::dataset::Tensor;
  993. std::string expect_file[] = {"32", "33", "39", "32", "33", "39"};
  994. uint64_t i = 0;
  995. while (row.size() != 0) {
  996. auto image = row["image"];
  997. auto target = row["target"];
  998. MS_LOG(INFO) << "Tensor image shape: " << image->shape();
  999. MS_LOG(INFO) << "Tensor target shape: " << target->shape();
  1000. std::shared_ptr<Tensor> expect_image;
  1001. Tensor::CreateFromFile(folder_path + "/JPEGImages/" + expect_file[i] + ".jpg", &expect_image);
  1002. EXPECT_EQ(*image, *expect_image);
  1003. std::shared_ptr<Tensor> expect_target;
  1004. Tensor::CreateFromFile(folder_path + "/SegmentationClass/" + expect_file[i] + ".png", &expect_target);
  1005. EXPECT_EQ(*target, *expect_target);
  1006. iter->GetNextRow(&row);
  1007. i++;
  1008. }
  1009. EXPECT_EQ(i, 6);
  1010. // Manually terminate the pipeline
  1011. iter->Stop();
  1012. }
  1013. TEST_F(MindDataTestPipeline, TestVOCSegmentationError1) {
  1014. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestVOCSegmentationError1.";
  1015. // Create a VOC Dataset
  1016. std::map<std::string, int32_t> class_index;
  1017. class_index["car"] = 0;
  1018. std::string folder_path = datasets_root_path_ + "/testVOC2012_2";
  1019. std::shared_ptr<Dataset> ds = VOC(folder_path, "Segmentation", "train", class_index, false, RandomSampler(false, 6));
  1020. // Expect nullptr for segmentation task with class_index
  1021. EXPECT_EQ(ds, nullptr);
  1022. }
  1023. TEST_F(MindDataTestPipeline, TestVOCInvalidTaskOrMode) {
  1024. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestVOCInvalidTaskOrMode.";
  1025. // Create a VOC Dataset
  1026. std::string folder_path = datasets_root_path_ + "/testVOC2012_2";
  1027. std::shared_ptr<Dataset> ds_1 = VOC(folder_path, "Classification", "train", {}, false, SequentialSampler(0, 3));
  1028. // Expect nullptr for invalid task
  1029. EXPECT_EQ(ds_1, nullptr);
  1030. std::shared_ptr<Dataset> ds_2 = VOC(folder_path, "Segmentation", "validation", {}, false, RandomSampler(false, 4));
  1031. // Expect nullptr for invalid mode
  1032. EXPECT_EQ(ds_2, nullptr);
  1033. }
  1034. TEST_F(MindDataTestPipeline, TestVOCDetection) {
  1035. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestVOCDetection.";
  1036. // Create a VOC Dataset
  1037. std::string folder_path = datasets_root_path_ + "/testVOC2012_2";
  1038. std::shared_ptr<Dataset> ds = VOC(folder_path, "Detection", "train", {}, false, SequentialSampler(0, 4));
  1039. EXPECT_NE(ds, nullptr);
  1040. // Create an iterator over the result of the above dataset
  1041. // This will trigger the creation of the Execution Tree and launch it.
  1042. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  1043. EXPECT_NE(iter, nullptr);
  1044. // Iterate the dataset and get each row
  1045. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  1046. iter->GetNextRow(&row);
  1047. // Check if VOCOp read correct images/labels
  1048. std::string expect_file[] = {"15", "32", "33", "39"};
  1049. uint32_t expect_num[] = {5, 5, 4, 3};
  1050. uint64_t i = 0;
  1051. while (row.size() != 0) {
  1052. auto image = row["image"];
  1053. auto label = row["label"];
  1054. MS_LOG(INFO) << "Tensor image shape: " << image->shape();
  1055. MS_LOG(INFO) << "Tensor label shape: " << label->shape();
  1056. std::shared_ptr<Tensor> expect_image;
  1057. Tensor::CreateFromFile(folder_path + "/JPEGImages/" + expect_file[i] + ".jpg", &expect_image);
  1058. EXPECT_EQ(*image, *expect_image);
  1059. std::shared_ptr<Tensor> expect_label;
  1060. Tensor::CreateFromMemory(TensorShape({1, 1}), DataType(DataType::DE_UINT32), nullptr, &expect_label);
  1061. expect_label->SetItemAt({0, 0}, expect_num[i]);
  1062. EXPECT_EQ(*label, *expect_label);
  1063. iter->GetNextRow(&row);
  1064. i++;
  1065. }
  1066. EXPECT_EQ(i, 4);
  1067. // Manually terminate the pipeline
  1068. iter->Stop();
  1069. }
  1070. TEST_F(MindDataTestPipeline, TestVOCClassIndex) {
  1071. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestVOCClassIndex.";
  1072. // Create a VOC Dataset
  1073. std::string folder_path = datasets_root_path_ + "/testVOC2012_2";
  1074. std::map<std::string, int32_t> class_index;
  1075. class_index["car"] = 0;
  1076. class_index["cat"] = 1;
  1077. class_index["train"] = 9;
  1078. std::shared_ptr<Dataset> ds = VOC(folder_path, "Detection", "train", class_index, false, SequentialSampler(0, 6));
  1079. EXPECT_NE(ds, nullptr);
  1080. // Create an iterator over the result of the above dataset
  1081. // This will trigger the creation of the Execution Tree and launch it.
  1082. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  1083. EXPECT_NE(iter, nullptr);
  1084. // Iterate the dataset and get each row
  1085. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  1086. iter->GetNextRow(&row);
  1087. // Check if VOCOp read correct labels
  1088. // When we provide class_index, label of ["car","cat","train"] become [0,1,9]
  1089. std::shared_ptr<Tensor> expect_label;
  1090. Tensor::CreateFromMemory(TensorShape({1, 1}), DataType(DataType::DE_UINT32), nullptr, &expect_label);
  1091. uint32_t expect[] = {9, 9, 9, 1, 1, 0};
  1092. uint64_t i = 0;
  1093. while (row.size() != 0) {
  1094. auto image = row["image"];
  1095. auto label = row["label"];
  1096. MS_LOG(INFO) << "Tensor image shape: " << image->shape();
  1097. MS_LOG(INFO) << "Tensor label shape: " << label->shape();
  1098. expect_label->SetItemAt({0, 0}, expect[i]);
  1099. EXPECT_EQ(*label, *expect_label);
  1100. iter->GetNextRow(&row);
  1101. i++;
  1102. }
  1103. EXPECT_EQ(i, 6);
  1104. // Manually terminate the pipeline
  1105. iter->Stop();
  1106. }
  1107. TEST_F(MindDataTestPipeline, TestConcatSuccess) {
  1108. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestConcatSuccess.";
  1109. // Create an ImageFolder Dataset
  1110. // Column names: {"image", "label"}
  1111. std::string folder_path = datasets_root_path_ + "/testPK/data/";
  1112. std::shared_ptr<Dataset> ds = ImageFolder(folder_path, true, RandomSampler(false, 10));
  1113. EXPECT_NE(ds, nullptr);
  1114. // Create a Cifar10 Dataset
  1115. // Column names: {"image", "label"}
  1116. folder_path = datasets_root_path_ + "/testCifar10Data/";
  1117. std::shared_ptr<Dataset> ds2 = Cifar10(folder_path, RandomSampler(false, 9));
  1118. EXPECT_NE(ds2, nullptr);
  1119. // Create a Project operation on ds
  1120. ds = ds->Project({"image"});
  1121. EXPECT_NE(ds, nullptr);
  1122. ds2 = ds2->Project({"image"});
  1123. EXPECT_NE(ds, nullptr);
  1124. // Create a Concat operation on the ds
  1125. ds = ds->Concat({ds2});
  1126. EXPECT_NE(ds, nullptr);
  1127. // Create a Batch operation on ds
  1128. int32_t batch_size = 1;
  1129. ds = ds->Batch(batch_size);
  1130. EXPECT_NE(ds, nullptr);
  1131. // Create an iterator over the result of the above dataset
  1132. // This will trigger the creation of the Execution Tree and launch it.
  1133. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  1134. EXPECT_NE(iter, nullptr);
  1135. // Iterate the dataset and get each row
  1136. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  1137. iter->GetNextRow(&row);
  1138. uint64_t i = 0;
  1139. while (row.size() != 0) {
  1140. i++;
  1141. auto image = row["image"];
  1142. MS_LOG(INFO) << "Tensor image shape: " << image->shape();
  1143. iter->GetNextRow(&row);
  1144. }
  1145. EXPECT_EQ(i, 19);
  1146. // Manually terminate the pipeline
  1147. iter->Stop();
  1148. }
  1149. TEST_F(MindDataTestPipeline, TestConcatSuccess2) {
  1150. // Test "+" operator to concat two datasets
  1151. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestConcatSuccess2.";
  1152. // Create an ImageFolder Dataset
  1153. // Column names: {"image", "label"}
  1154. std::string folder_path = datasets_root_path_ + "/testPK/data/";
  1155. std::shared_ptr<Dataset> ds = ImageFolder(folder_path, true, RandomSampler(false, 10));
  1156. EXPECT_NE(ds, nullptr);
  1157. // Create a Cifar10 Dataset
  1158. // Column names: {"image", "label"}
  1159. folder_path = datasets_root_path_ + "/testCifar10Data/";
  1160. std::shared_ptr<Dataset> ds2 = Cifar10(folder_path, RandomSampler(false, 9));
  1161. EXPECT_NE(ds2, nullptr);
  1162. // Create a Project operation on ds
  1163. ds = ds->Project({"image"});
  1164. EXPECT_NE(ds, nullptr);
  1165. ds2 = ds2->Project({"image"});
  1166. EXPECT_NE(ds, nullptr);
  1167. // Create a Concat operation on the ds
  1168. ds = ds + ds2;
  1169. EXPECT_NE(ds, nullptr);
  1170. // Create a Batch operation on ds
  1171. int32_t batch_size = 1;
  1172. ds = ds->Batch(batch_size);
  1173. EXPECT_NE(ds, nullptr);
  1174. // Create an iterator over the result of the above dataset
  1175. // This will trigger the creation of the Execution Tree and launch it.
  1176. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  1177. EXPECT_NE(iter, nullptr);
  1178. // Iterate the dataset and get each row
  1179. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  1180. iter->GetNextRow(&row);
  1181. uint64_t i = 0;
  1182. while (row.size() != 0) {
  1183. i++;
  1184. auto image = row["image"];
  1185. MS_LOG(INFO) << "Tensor image shape: " << image->shape();
  1186. iter->GetNextRow(&row);
  1187. }
  1188. EXPECT_EQ(i, 19);
  1189. // Manually terminate the pipeline
  1190. iter->Stop();
  1191. }
  1192. TEST_F(MindDataTestPipeline, TestConcatFail1) {
  1193. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestConcatFail1.";
  1194. // This case is expected to fail because the input column names of concatenated datasets are not the same
  1195. // Create an ImageFolder Dataset
  1196. // Column names: {"image", "label"}
  1197. std::string folder_path = datasets_root_path_ + "/testPK/data/";
  1198. std::shared_ptr<Dataset> ds = ImageFolder(folder_path, true, RandomSampler(false, 10));
  1199. EXPECT_NE(ds, nullptr);
  1200. std::shared_ptr<Dataset> ds2 = ImageFolder(folder_path, true, RandomSampler(false, 10));
  1201. EXPECT_NE(ds, nullptr);
  1202. // Create a Rename operation on ds
  1203. ds2 = ds2->Rename({"image", "label"}, {"col1", "col2"});
  1204. EXPECT_NE(ds, nullptr);
  1205. // Create a Project operation on the ds
  1206. // Name of datasets to concat doesn't not match
  1207. ds = ds->Concat({ds2});
  1208. EXPECT_NE(ds, nullptr);
  1209. // Create a Batch operation on ds
  1210. int32_t batch_size = 1;
  1211. ds = ds->Batch(batch_size);
  1212. EXPECT_NE(ds, nullptr);
  1213. // Create an iterator over the result of the above dataset
  1214. // This will trigger the creation of the Execution Tree and launch it.
  1215. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  1216. EXPECT_EQ(iter, nullptr);
  1217. }
  1218. TEST_F(MindDataTestPipeline, TestConcatFail2) {
  1219. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestConcatFail2.";
  1220. // This case is expected to fail because the input dataset is empty.
  1221. // Create an ImageFolder Dataset
  1222. std::string folder_path = datasets_root_path_ + "/testPK/data/";
  1223. std::shared_ptr<Dataset> ds = ImageFolder(folder_path, true, RandomSampler(false, 10));
  1224. EXPECT_NE(ds, nullptr);
  1225. // Create a Project operation on the ds
  1226. // Input dataset to concat is empty
  1227. ds = ds->Concat({});
  1228. EXPECT_EQ(ds, nullptr);
  1229. }