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 62 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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833
  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. using mindspore::dataset::dsize_t;
  46. class MindDataTestPipeline : public UT::DatasetOpTesting {
  47. protected:
  48. };
  49. TEST_F(MindDataTestPipeline, TestBatchAndRepeat) {
  50. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestBatchAndRepeat.";
  51. // Create a Mnist Dataset
  52. std::string folder_path = datasets_root_path_ + "/testMnistData/";
  53. std::shared_ptr<Dataset> ds = Mnist(folder_path, RandomSampler(false, 10));
  54. EXPECT_NE(ds, nullptr);
  55. // Create a Repeat operation on ds
  56. int32_t repeat_num = 2;
  57. ds = ds->Repeat(repeat_num);
  58. EXPECT_NE(ds, nullptr);
  59. // Create a Batch operation on ds
  60. int32_t batch_size = 2;
  61. ds = ds->Batch(batch_size);
  62. EXPECT_NE(ds, nullptr);
  63. // Create an iterator over the result of the above dataset
  64. // This will trigger the creation of the Execution Tree and launch it.
  65. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  66. EXPECT_NE(iter, nullptr);
  67. // Iterate the dataset and get each row
  68. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  69. iter->GetNextRow(&row);
  70. uint64_t i = 0;
  71. while (row.size() != 0) {
  72. i++;
  73. auto image = row["image"];
  74. MS_LOG(INFO) << "Tensor image shape: " << image->shape();
  75. iter->GetNextRow(&row);
  76. }
  77. EXPECT_EQ(i, 10);
  78. // Manually terminate the pipeline
  79. iter->Stop();
  80. }
  81. TEST_F(MindDataTestPipeline, TestMnistFail1) {
  82. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestMnistFail1.";
  83. // Create a Mnist Dataset
  84. std::shared_ptr<Dataset> ds = Mnist("", RandomSampler(false, 10));
  85. EXPECT_EQ(ds, nullptr);
  86. }
  87. TEST_F(MindDataTestPipeline, TestTensorOpsAndMap) {
  88. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestTensorOpsAndMap.";
  89. // Create a Mnist Dataset
  90. std::string folder_path = datasets_root_path_ + "/testMnistData/";
  91. std::shared_ptr<Dataset> ds = Mnist(folder_path, RandomSampler(false, 20));
  92. EXPECT_NE(ds, nullptr);
  93. // Create a Repeat operation on ds
  94. int32_t repeat_num = 2;
  95. ds = ds->Repeat(repeat_num);
  96. EXPECT_NE(ds, nullptr);
  97. // Create objects for the tensor ops
  98. std::shared_ptr<TensorOperation> resize_op = vision::Resize({30, 30});
  99. EXPECT_NE(resize_op, nullptr);
  100. std::shared_ptr<TensorOperation> center_crop_op = vision::CenterCrop({16, 16});
  101. EXPECT_NE(center_crop_op, nullptr);
  102. // Create a Map operation on ds
  103. ds = ds->Map({resize_op, center_crop_op});
  104. EXPECT_NE(ds, nullptr);
  105. // Create a Batch operation on ds
  106. int32_t batch_size = 1;
  107. ds = ds->Batch(batch_size);
  108. EXPECT_NE(ds, nullptr);
  109. // Create an iterator over the result of the above dataset
  110. // This will trigger the creation of the Execution Tree and launch it.
  111. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  112. EXPECT_NE(iter, nullptr);
  113. // Iterate the dataset and get each row
  114. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  115. iter->GetNextRow(&row);
  116. uint64_t i = 0;
  117. while (row.size() != 0) {
  118. i++;
  119. auto image = row["image"];
  120. MS_LOG(INFO) << "Tensor image shape: " << image->shape();
  121. iter->GetNextRow(&row);
  122. }
  123. EXPECT_EQ(i, 40);
  124. // Manually terminate the pipeline
  125. iter->Stop();
  126. }
  127. TEST_F(MindDataTestPipeline, TestUniformAugWithOps) {
  128. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestUniformAugWithOps.";
  129. // Create a Mnist Dataset
  130. std::string folder_path = datasets_root_path_ + "/testMnistData/";
  131. std::shared_ptr<Dataset> ds = Mnist(folder_path, RandomSampler(false, 20));
  132. EXPECT_NE(ds, nullptr);
  133. // Create a Repeat operation on ds
  134. int32_t repeat_num = 1;
  135. ds = ds->Repeat(repeat_num);
  136. EXPECT_NE(ds, nullptr);
  137. // Create objects for the tensor ops
  138. std::shared_ptr<TensorOperation> resize_op = vision::Resize({30, 30});
  139. EXPECT_NE(resize_op, nullptr);
  140. std::shared_ptr<TensorOperation> random_crop_op = vision::RandomCrop({28, 28});
  141. EXPECT_NE(random_crop_op, nullptr);
  142. std::shared_ptr<TensorOperation> center_crop_op = vision::CenterCrop({16, 16});
  143. EXPECT_NE(center_crop_op, nullptr);
  144. std::shared_ptr<TensorOperation> uniform_aug_op = vision::UniformAugment({random_crop_op, center_crop_op}, 2);
  145. EXPECT_NE(uniform_aug_op, nullptr);
  146. // Create a Map operation on ds
  147. ds = ds->Map({resize_op, uniform_aug_op});
  148. EXPECT_NE(ds, nullptr);
  149. // Create an iterator over the result of the above dataset
  150. // This will trigger the creation of the Execution Tree and launch it.
  151. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  152. EXPECT_NE(iter, nullptr);
  153. // Iterate the dataset and get each row
  154. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  155. iter->GetNextRow(&row);
  156. uint64_t i = 0;
  157. while (row.size() != 0) {
  158. i++;
  159. auto image = row["image"];
  160. MS_LOG(INFO) << "Tensor image shape: " << image->shape();
  161. iter->GetNextRow(&row);
  162. }
  163. EXPECT_EQ(i, 20);
  164. // Manually terminate the pipeline
  165. iter->Stop();
  166. }
  167. TEST_F(MindDataTestPipeline, TestRandomFlip) {
  168. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestRandomFlip.";
  169. // Create an ImageFolder Dataset
  170. std::string folder_path = datasets_root_path_ + "/testPK/data/";
  171. std::shared_ptr<Dataset> ds = ImageFolder(folder_path, true, RandomSampler(false, 10));
  172. EXPECT_NE(ds, nullptr);
  173. // Create a Repeat operation on ds
  174. int32_t repeat_num = 2;
  175. ds = ds->Repeat(repeat_num);
  176. EXPECT_NE(ds, nullptr);
  177. // Create objects for the tensor ops
  178. std::shared_ptr<TensorOperation> random_vertical_flip_op = vision::RandomVerticalFlip(0.5);
  179. EXPECT_NE(random_vertical_flip_op, nullptr);
  180. std::shared_ptr<TensorOperation> random_horizontal_flip_op = vision::RandomHorizontalFlip(0.5);
  181. EXPECT_NE(random_horizontal_flip_op, nullptr);
  182. // Create a Map operation on ds
  183. ds = ds->Map({random_vertical_flip_op, random_horizontal_flip_op});
  184. EXPECT_NE(ds, nullptr);
  185. // Create a Batch operation on ds
  186. int32_t batch_size = 1;
  187. ds = ds->Batch(batch_size);
  188. EXPECT_NE(ds, nullptr);
  189. // Create an iterator over the result of the above dataset
  190. // This will trigger the creation of the Execution Tree and launch it.
  191. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  192. EXPECT_NE(iter, nullptr);
  193. // Iterate the dataset and get each row
  194. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  195. iter->GetNextRow(&row);
  196. uint64_t i = 0;
  197. while (row.size() != 0) {
  198. i++;
  199. auto image = row["image"];
  200. MS_LOG(INFO) << "Tensor image shape: " << image->shape();
  201. iter->GetNextRow(&row);
  202. }
  203. EXPECT_EQ(i, 20);
  204. // Manually terminate the pipeline
  205. iter->Stop();
  206. }
  207. TEST_F(MindDataTestPipeline, TestImageFolderBatchAndRepeat) {
  208. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestImageFolderBatchAndRepeat.";
  209. // Create an ImageFolder Dataset
  210. std::string folder_path = datasets_root_path_ + "/testPK/data/";
  211. std::shared_ptr<Dataset> ds = ImageFolder(folder_path, true, RandomSampler(false, 10));
  212. EXPECT_NE(ds, nullptr);
  213. // Create a Repeat operation on ds
  214. int32_t repeat_num = 2;
  215. ds = ds->Repeat(repeat_num);
  216. EXPECT_NE(ds, nullptr);
  217. // Create a Batch operation on ds
  218. int32_t batch_size = 2;
  219. ds = ds->Batch(batch_size);
  220. EXPECT_NE(ds, nullptr);
  221. // Create an iterator over the result of the above dataset
  222. // This will trigger the creation of the Execution Tree and launch it.
  223. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  224. EXPECT_NE(iter, nullptr);
  225. // Iterate the dataset and get each row
  226. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  227. iter->GetNextRow(&row);
  228. uint64_t i = 0;
  229. while (row.size() != 0) {
  230. i++;
  231. auto image = row["image"];
  232. MS_LOG(INFO) << "Tensor image shape: " << image->shape();
  233. iter->GetNextRow(&row);
  234. }
  235. EXPECT_EQ(i, 10);
  236. // Manually terminate the pipeline
  237. iter->Stop();
  238. }
  239. TEST_F(MindDataTestPipeline, TestImageFolderFail1) {
  240. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestImageFolderFail1.";
  241. // Create an ImageFolder Dataset
  242. std::shared_ptr<Dataset> ds = ImageFolder("", true, nullptr);
  243. EXPECT_EQ(ds, nullptr);
  244. }
  245. TEST_F(MindDataTestPipeline, TestImageFolderWithSamplers) {
  246. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestImageFolderWithSamplers.";
  247. std::shared_ptr<SamplerObj> sampl = DistributedSampler(2, 1);
  248. EXPECT_NE(sampl, nullptr);
  249. sampl = PKSampler(3);
  250. EXPECT_NE(sampl, nullptr);
  251. sampl = RandomSampler(false, 12);
  252. EXPECT_NE(sampl, nullptr);
  253. sampl = SequentialSampler(0, 12);
  254. EXPECT_NE(sampl, nullptr);
  255. 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};
  256. sampl = WeightedRandomSampler(weights, 12);
  257. EXPECT_NE(sampl, nullptr);
  258. std::vector<int64_t> indices = {1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23};
  259. sampl = SubsetRandomSampler(indices);
  260. EXPECT_NE(sampl, nullptr);
  261. // Create an ImageFolder Dataset
  262. std::string folder_path = datasets_root_path_ + "/testPK/data/";
  263. std::shared_ptr<Dataset> ds = ImageFolder(folder_path, false, sampl);
  264. EXPECT_NE(ds, nullptr);
  265. // Create a Repeat operation on ds
  266. int32_t repeat_num = 2;
  267. ds = ds->Repeat(repeat_num);
  268. EXPECT_NE(ds, nullptr);
  269. // Create a Batch operation on ds
  270. int32_t batch_size = 2;
  271. ds = ds->Batch(batch_size);
  272. EXPECT_NE(ds, nullptr);
  273. // Create an iterator over the result of the above dataset
  274. // This will trigger the creation of the Execution Tree and launch it.
  275. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  276. EXPECT_NE(iter, nullptr);
  277. // Iterate the dataset and get each row
  278. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  279. iter->GetNextRow(&row);
  280. uint64_t i = 0;
  281. while (row.size() != 0) {
  282. i++;
  283. auto image = row["image"];
  284. MS_LOG(INFO) << "Tensor image shape: " << image->shape();
  285. iter->GetNextRow(&row);
  286. }
  287. EXPECT_EQ(i, 12);
  288. // Manually terminate the pipeline
  289. iter->Stop();
  290. }
  291. TEST_F(MindDataTestPipeline, TestPad) {
  292. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestPad.";
  293. // Create an ImageFolder Dataset
  294. std::string folder_path = datasets_root_path_ + "/testPK/data/";
  295. std::shared_ptr<Dataset> ds = ImageFolder(folder_path, true, RandomSampler(false, 10));
  296. EXPECT_NE(ds, nullptr);
  297. // Create a Repeat operation on ds
  298. int32_t repeat_num = 2;
  299. ds = ds->Repeat(repeat_num);
  300. EXPECT_NE(ds, nullptr);
  301. // Create objects for the tensor ops
  302. std::shared_ptr<TensorOperation> pad_op1 = vision::Pad({1, 2, 3, 4}, {0}, BorderType::kSymmetric);
  303. EXPECT_NE(pad_op1, nullptr);
  304. std::shared_ptr<TensorOperation> pad_op2 = vision::Pad({1}, {1, 1, 1}, BorderType::kEdge);
  305. EXPECT_NE(pad_op2, nullptr);
  306. std::shared_ptr<TensorOperation> pad_op3 = vision::Pad({1, 4});
  307. EXPECT_NE(pad_op3, nullptr);
  308. // Create a Map operation on ds
  309. ds = ds->Map({pad_op1, pad_op2, pad_op3});
  310. EXPECT_NE(ds, nullptr);
  311. // Create a Batch operation on ds
  312. int32_t batch_size = 1;
  313. ds = ds->Batch(batch_size);
  314. EXPECT_NE(ds, nullptr);
  315. // Create an iterator over the result of the above dataset
  316. // This will trigger the creation of the Execution Tree and launch it.
  317. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  318. EXPECT_NE(iter, nullptr);
  319. // Iterate the dataset and get each row
  320. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  321. iter->GetNextRow(&row);
  322. uint64_t i = 0;
  323. while (row.size() != 0) {
  324. i++;
  325. auto image = row["image"];
  326. MS_LOG(INFO) << "Tensor image shape: " << image->shape();
  327. iter->GetNextRow(&row);
  328. }
  329. EXPECT_EQ(i, 20);
  330. // Manually terminate the pipeline
  331. iter->Stop();
  332. }
  333. TEST_F(MindDataTestPipeline, TestCutOut) {
  334. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestCutOut.";
  335. // Create an ImageFolder Dataset
  336. std::string folder_path = datasets_root_path_ + "/testPK/data/";
  337. std::shared_ptr<Dataset> ds = ImageFolder(folder_path, true, RandomSampler(false, 10));
  338. EXPECT_NE(ds, nullptr);
  339. // Create a Repeat operation on ds
  340. int32_t repeat_num = 2;
  341. ds = ds->Repeat(repeat_num);
  342. EXPECT_NE(ds, nullptr);
  343. // Create objects for the tensor ops
  344. std::shared_ptr<TensorOperation> cut_out1 = vision::CutOut(30, 5);
  345. EXPECT_NE(cut_out1, nullptr);
  346. std::shared_ptr<TensorOperation> cut_out2 = vision::CutOut(30);
  347. EXPECT_NE(cut_out2, nullptr);
  348. // Create a Map operation on ds
  349. ds = ds->Map({cut_out1, cut_out2});
  350. EXPECT_NE(ds, nullptr);
  351. // Create a Batch operation on ds
  352. int32_t batch_size = 1;
  353. ds = ds->Batch(batch_size);
  354. EXPECT_NE(ds, nullptr);
  355. // Create an iterator over the result of the above dataset
  356. // This will trigger the creation of the Execution Tree and launch it.
  357. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  358. EXPECT_NE(iter, nullptr);
  359. // Iterate the dataset and get each row
  360. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  361. iter->GetNextRow(&row);
  362. uint64_t i = 0;
  363. while (row.size() != 0) {
  364. i++;
  365. auto image = row["image"];
  366. MS_LOG(INFO) << "Tensor image shape: " << image->shape();
  367. iter->GetNextRow(&row);
  368. }
  369. EXPECT_EQ(i, 20);
  370. // Manually terminate the pipeline
  371. iter->Stop();
  372. }
  373. TEST_F(MindDataTestPipeline, TestNormalize) {
  374. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestNormalize.";
  375. // Create an ImageFolder Dataset
  376. std::string folder_path = datasets_root_path_ + "/testPK/data/";
  377. std::shared_ptr<Dataset> ds = ImageFolder(folder_path, true, RandomSampler(false, 10));
  378. EXPECT_NE(ds, nullptr);
  379. // Create a Repeat operation on ds
  380. int32_t repeat_num = 2;
  381. ds = ds->Repeat(repeat_num);
  382. EXPECT_NE(ds, nullptr);
  383. // Create objects for the tensor ops
  384. std::shared_ptr<TensorOperation> normalize = vision::Normalize({121.0, 115.0, 100.0}, {70.0, 68.0, 71.0});
  385. EXPECT_NE(normalize, nullptr);
  386. // Create a Map operation on ds
  387. ds = ds->Map({normalize});
  388. EXPECT_NE(ds, nullptr);
  389. // Create a Batch operation on ds
  390. int32_t batch_size = 1;
  391. ds = ds->Batch(batch_size);
  392. EXPECT_NE(ds, nullptr);
  393. // Create an iterator over the result of the above dataset
  394. // This will trigger the creation of the Execution Tree and launch it.
  395. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  396. EXPECT_NE(iter, nullptr);
  397. // Iterate the dataset and get each row
  398. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  399. iter->GetNextRow(&row);
  400. uint64_t i = 0;
  401. while (row.size() != 0) {
  402. i++;
  403. auto image = row["image"];
  404. MS_LOG(INFO) << "Tensor image shape: " << image->shape();
  405. iter->GetNextRow(&row);
  406. }
  407. EXPECT_EQ(i, 20);
  408. // Manually terminate the pipeline
  409. iter->Stop();
  410. }
  411. TEST_F(MindDataTestPipeline, TestDecode) {
  412. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestDecode.";
  413. // Create an ImageFolder Dataset
  414. std::string folder_path = datasets_root_path_ + "/testPK/data/";
  415. std::shared_ptr<Dataset> ds = ImageFolder(folder_path, false, RandomSampler(false, 10));
  416. EXPECT_NE(ds, nullptr);
  417. // Create a Repeat operation on ds
  418. int32_t repeat_num = 2;
  419. ds = ds->Repeat(repeat_num);
  420. EXPECT_NE(ds, nullptr);
  421. // Create objects for the tensor ops
  422. std::shared_ptr<TensorOperation> decode = vision::Decode(true);
  423. EXPECT_NE(decode, nullptr);
  424. // Create a Map operation on ds
  425. ds = ds->Map({decode});
  426. EXPECT_NE(ds, nullptr);
  427. // Create a Batch operation on ds
  428. int32_t batch_size = 1;
  429. ds = ds->Batch(batch_size);
  430. EXPECT_NE(ds, nullptr);
  431. // Create an iterator over the result of the above dataset
  432. // This will trigger the creation of the Execution Tree and launch it.
  433. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  434. EXPECT_NE(iter, nullptr);
  435. // Iterate the dataset and get each row
  436. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  437. iter->GetNextRow(&row);
  438. uint64_t i = 0;
  439. while (row.size() != 0) {
  440. i++;
  441. auto image = row["image"];
  442. MS_LOG(INFO) << "Tensor image shape: " << image->shape();
  443. iter->GetNextRow(&row);
  444. }
  445. EXPECT_EQ(i, 20);
  446. // Manually terminate the pipeline
  447. iter->Stop();
  448. }
  449. TEST_F(MindDataTestPipeline, TestShuffleDataset) {
  450. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestShuffleDataset.";
  451. // Create an ImageFolder Dataset
  452. std::string folder_path = datasets_root_path_ + "/testPK/data/";
  453. std::shared_ptr<Dataset> ds = ImageFolder(folder_path, true, RandomSampler(false, 10));
  454. EXPECT_NE(ds, nullptr);
  455. // Create a Shuffle operation on ds
  456. int32_t shuffle_size = 10;
  457. ds = ds->Shuffle(shuffle_size);
  458. EXPECT_NE(ds, nullptr);
  459. // Create a Repeat operation on ds
  460. int32_t repeat_num = 2;
  461. ds = ds->Repeat(repeat_num);
  462. EXPECT_NE(ds, nullptr);
  463. // Create a Batch operation on ds
  464. int32_t batch_size = 2;
  465. ds = ds->Batch(batch_size);
  466. EXPECT_NE(ds, nullptr);
  467. // Create an iterator over the result of the above dataset
  468. // This will trigger the creation of the Execution Tree and launch it.
  469. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  470. EXPECT_NE(iter, nullptr);
  471. // Iterate the dataset and get each row
  472. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  473. iter->GetNextRow(&row);
  474. uint64_t i = 0;
  475. while (row.size() != 0) {
  476. i++;
  477. auto image = row["image"];
  478. MS_LOG(INFO) << "Tensor image shape: " << image->shape();
  479. iter->GetNextRow(&row);
  480. }
  481. EXPECT_EQ(i, 10);
  482. // Manually terminate the pipeline
  483. iter->Stop();
  484. }
  485. TEST_F(MindDataTestPipeline, TestSkipDataset) {
  486. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestSkipDataset.";
  487. // Create an ImageFolder Dataset
  488. std::string folder_path = datasets_root_path_ + "/testPK/data/";
  489. std::shared_ptr<Dataset> ds = ImageFolder(folder_path, true, RandomSampler(false, 10));
  490. EXPECT_NE(ds, nullptr);
  491. // Create a Skip operation on ds
  492. int32_t count = 3;
  493. ds = ds->Skip(count);
  494. EXPECT_NE(ds, nullptr);
  495. // Create an iterator over the result of the above dataset
  496. // This will trigger the creation of the Execution Tree and launch it.
  497. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  498. EXPECT_NE(iter, nullptr);
  499. // Iterate the dataset and get each row
  500. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  501. iter->GetNextRow(&row);
  502. uint64_t i = 0;
  503. while (row.size() != 0) {
  504. i++;
  505. auto image = row["image"];
  506. MS_LOG(INFO) << "Tensor image shape: " << image->shape();
  507. iter->GetNextRow(&row);
  508. }
  509. MS_LOG(INFO) << "Number of rows: " << i;
  510. // Expect 10-3=7 rows
  511. EXPECT_EQ(i, 7);
  512. // Manually terminate the pipeline
  513. iter->Stop();
  514. }
  515. TEST_F(MindDataTestPipeline, TestSkipDatasetError1) {
  516. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestSkipDatasetError1.";
  517. // Create an ImageFolder Dataset
  518. std::string folder_path = datasets_root_path_ + "/testPK/data/";
  519. std::shared_ptr<Dataset> ds = ImageFolder(folder_path, true, RandomSampler(false, 10));
  520. EXPECT_NE(ds, nullptr);
  521. // Create a Skip operation on ds with invalid count input
  522. int32_t count = -1;
  523. ds = ds->Skip(count);
  524. // Expect nullptr for invalid input skip_count
  525. EXPECT_EQ(ds, nullptr);
  526. }
  527. TEST_F(MindDataTestPipeline, TestTakeDatasetDefault) {
  528. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestTakeDatasetDefault.";
  529. // Create an ImageFolder Dataset
  530. std::string folder_path = datasets_root_path_ + "/testPK/data/";
  531. std::shared_ptr<Dataset> ds = ImageFolder(folder_path, true, RandomSampler(false, 7));
  532. EXPECT_NE(ds, nullptr);
  533. // Create a Take operation on ds, dafault count = -1
  534. ds = ds->Take();
  535. EXPECT_NE(ds, nullptr);
  536. // Create an iterator over the result of the above dataset
  537. // This will trigger the creation of the Execution Tree and launch it.
  538. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  539. EXPECT_NE(iter, nullptr);
  540. // Iterate the dataset and get each row
  541. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  542. iter->GetNextRow(&row);
  543. uint64_t i = 0;
  544. while (row.size() != 0) {
  545. i++;
  546. auto image = row["image"];
  547. MS_LOG(INFO) << "Tensor image shape: " << image->shape();
  548. iter->GetNextRow(&row);
  549. }
  550. MS_LOG(INFO) << "Number of rows: " << i;
  551. // Expect 7 rows
  552. EXPECT_EQ(i, 7);
  553. // Manually terminate the pipeline
  554. iter->Stop();
  555. }
  556. TEST_F(MindDataTestPipeline, TestTakeDatasetNormal) {
  557. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestTakeDatasetNormal.";
  558. // Create an ImageFolder Dataset
  559. std::string folder_path = datasets_root_path_ + "/testPK/data/";
  560. std::shared_ptr<Dataset> ds = ImageFolder(folder_path, true, RandomSampler(false, 8));
  561. EXPECT_NE(ds, nullptr);
  562. // Create a Take operation on ds
  563. ds = ds->Take(5);
  564. EXPECT_NE(ds, nullptr);
  565. // Create an iterator over the result of the above dataset
  566. // This will trigger the creation of the Execution Tree and launch it.
  567. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  568. EXPECT_NE(iter, nullptr);
  569. // Iterate the dataset and get each row
  570. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  571. iter->GetNextRow(&row);
  572. uint64_t i = 0;
  573. while (row.size() != 0) {
  574. i++;
  575. auto image = row["image"];
  576. MS_LOG(INFO) << "Tensor image shape: " << image->shape();
  577. iter->GetNextRow(&row);
  578. }
  579. MS_LOG(INFO) << "Number of rows: " << i;
  580. // Expect 5 rows
  581. EXPECT_EQ(i, 5);
  582. // Manually terminate the pipeline
  583. iter->Stop();
  584. }
  585. TEST_F(MindDataTestPipeline, TestTakeDatasetError1) {
  586. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestTakeDatasetError1.";
  587. // Create an ImageFolder Dataset
  588. std::string folder_path = datasets_root_path_ + "/testPK/data/";
  589. std::shared_ptr<Dataset> ds = ImageFolder(folder_path, true, RandomSampler(false, 10));
  590. EXPECT_NE(ds, nullptr);
  591. // Create a Take operation on ds with invalid count input
  592. int32_t count = -5;
  593. ds = ds->Take(count);
  594. // Expect nullptr for invalid input take_count
  595. EXPECT_EQ(ds, nullptr);
  596. }
  597. TEST_F(MindDataTestPipeline, TestCifar10Dataset) {
  598. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestCifar10Dataset.";
  599. // Create a Cifar10 Dataset
  600. std::string folder_path = datasets_root_path_ + "/testCifar10Data/";
  601. std::shared_ptr<Dataset> ds = Cifar10(folder_path, RandomSampler(false, 10));
  602. EXPECT_NE(ds, nullptr);
  603. // Create an iterator over the result of the above dataset
  604. // This will trigger the creation of the Execution Tree and launch it.
  605. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  606. EXPECT_NE(iter, nullptr);
  607. // Iterate the dataset and get each row
  608. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  609. iter->GetNextRow(&row);
  610. EXPECT_NE(row.find("image"), row.end());
  611. EXPECT_NE(row.find("label"), row.end());
  612. uint64_t i = 0;
  613. while (row.size() != 0) {
  614. i++;
  615. auto image = row["image"];
  616. MS_LOG(INFO) << "Tensor image shape: " << image->shape();
  617. iter->GetNextRow(&row);
  618. }
  619. EXPECT_EQ(i, 10);
  620. // Manually terminate the pipeline
  621. iter->Stop();
  622. }
  623. TEST_F(MindDataTestPipeline, TestCifar10DatasetFail1) {
  624. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestCifar10DatasetFail1.";
  625. // Create a Cifar10 Dataset
  626. std::shared_ptr<Dataset> ds = Cifar10("", RandomSampler(false, 10));
  627. EXPECT_EQ(ds, nullptr);
  628. }
  629. TEST_F(MindDataTestPipeline, TestCifar100Dataset) {
  630. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestCifar100Dataset.";
  631. // Create a Cifar100 Dataset
  632. std::string folder_path = datasets_root_path_ + "/testCifar100Data/";
  633. std::shared_ptr<Dataset> ds = Cifar100(folder_path, RandomSampler(false, 10));
  634. EXPECT_NE(ds, nullptr);
  635. // Create an iterator over the result of the above dataset
  636. // This will trigger the creation of the Execution Tree and launch it.
  637. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  638. EXPECT_NE(iter, nullptr);
  639. // Iterate the dataset and get each row
  640. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  641. iter->GetNextRow(&row);
  642. EXPECT_NE(row.find("image"), row.end());
  643. EXPECT_NE(row.find("coarse_label"), row.end());
  644. EXPECT_NE(row.find("fine_label"), row.end());
  645. uint64_t i = 0;
  646. while (row.size() != 0) {
  647. i++;
  648. auto image = row["image"];
  649. MS_LOG(INFO) << "Tensor image shape: " << image->shape();
  650. iter->GetNextRow(&row);
  651. }
  652. EXPECT_EQ(i, 10);
  653. // Manually terminate the pipeline
  654. iter->Stop();
  655. }
  656. TEST_F(MindDataTestPipeline, TestCifar100DatasetFail1) {
  657. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestCifar100DatasetFail1.";
  658. // Create a Cifar100 Dataset
  659. std::shared_ptr<Dataset> ds = Cifar100("", RandomSampler(false, 10));
  660. EXPECT_EQ(ds, nullptr);
  661. }
  662. TEST_F(MindDataTestPipeline, TestRandomColorAdjust) {
  663. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestRandomColorAdjust.";
  664. // Create an ImageFolder Dataset
  665. std::string folder_path = datasets_root_path_ + "/testPK/data/";
  666. std::shared_ptr<Dataset> ds = ImageFolder(folder_path, true, RandomSampler(false, 10));
  667. EXPECT_NE(ds, nullptr);
  668. // Create a Repeat operation on ds
  669. int32_t repeat_num = 2;
  670. ds = ds->Repeat(repeat_num);
  671. EXPECT_NE(ds, nullptr);
  672. // Create objects for the tensor ops
  673. std::shared_ptr<TensorOperation> random_color_adjust1 = vision::RandomColorAdjust({1.0}, {0.0}, {0.5}, {0.5});
  674. EXPECT_NE(random_color_adjust1, nullptr);
  675. std::shared_ptr<TensorOperation> random_color_adjust2 = vision::RandomColorAdjust({1.0, 1.0}, {0.0, 0.0}, {0.5, 0.5},
  676. {0.5, 0.5});
  677. EXPECT_NE(random_color_adjust2, nullptr);
  678. std::shared_ptr<TensorOperation> random_color_adjust3 = vision::RandomColorAdjust({0.5, 1.0}, {0.0, 0.5}, {0.25, 0.5},
  679. {0.25, 0.5});
  680. EXPECT_NE(random_color_adjust3, nullptr);
  681. std::shared_ptr<TensorOperation> random_color_adjust4 = vision::RandomColorAdjust();
  682. EXPECT_NE(random_color_adjust4, nullptr);
  683. // Create a Map operation on ds
  684. ds = ds->Map({random_color_adjust1, random_color_adjust2, random_color_adjust3, random_color_adjust4});
  685. EXPECT_NE(ds, nullptr);
  686. // Create a Batch operation on ds
  687. int32_t batch_size = 1;
  688. ds = ds->Batch(batch_size);
  689. EXPECT_NE(ds, nullptr);
  690. // Create an iterator over the result of the above dataset
  691. // This will trigger the creation of the Execution Tree and launch it.
  692. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  693. EXPECT_NE(iter, nullptr);
  694. // Iterate the dataset and get each row
  695. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  696. iter->GetNextRow(&row);
  697. uint64_t i = 0;
  698. while (row.size() != 0) {
  699. i++;
  700. auto image = row["image"];
  701. MS_LOG(INFO) << "Tensor image shape: " << image->shape();
  702. iter->GetNextRow(&row);
  703. }
  704. EXPECT_EQ(i, 20);
  705. // Manually terminate the pipeline
  706. iter->Stop();
  707. }
  708. TEST_F(MindDataTestPipeline, TestRandomRotation) {
  709. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestRandomRotation.";
  710. // Create an ImageFolder Dataset
  711. std::string folder_path = datasets_root_path_ + "/testPK/data/";
  712. std::shared_ptr<Dataset> ds = ImageFolder(folder_path, true, RandomSampler(false, 10));
  713. EXPECT_NE(ds, nullptr);
  714. // Create a Repeat operation on ds
  715. int32_t repeat_num = 2;
  716. ds = ds->Repeat(repeat_num);
  717. EXPECT_NE(ds, nullptr);
  718. // Create objects for the tensor ops
  719. std::shared_ptr<TensorOperation> random_rotation_op = vision::RandomRotation({-180, 180});
  720. EXPECT_NE(random_rotation_op, nullptr);
  721. // Create a Map operation on ds
  722. ds = ds->Map({random_rotation_op});
  723. EXPECT_NE(ds, nullptr);
  724. // Create a Batch operation on ds
  725. int32_t batch_size = 1;
  726. ds = ds->Batch(batch_size);
  727. EXPECT_NE(ds, nullptr);
  728. // Create an iterator over the result of the above dataset
  729. // This will trigger the creation of the Execution Tree and launch it.
  730. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  731. EXPECT_NE(iter, nullptr);
  732. // Iterate the dataset and get each row
  733. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  734. iter->GetNextRow(&row);
  735. uint64_t i = 0;
  736. while (row.size() != 0) {
  737. i++;
  738. auto image = row["image"];
  739. MS_LOG(INFO) << "Tensor image shape: " << image->shape();
  740. iter->GetNextRow(&row);
  741. }
  742. EXPECT_EQ(i, 20);
  743. // Manually terminate the pipeline
  744. iter->Stop();
  745. }
  746. TEST_F(MindDataTestPipeline, TestProjectMap) {
  747. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestProjectMap.";
  748. // Create an ImageFolder Dataset
  749. std::string folder_path = datasets_root_path_ + "/testPK/data/";
  750. std::shared_ptr<Dataset> ds = ImageFolder(folder_path, true, RandomSampler(false, 10));
  751. EXPECT_NE(ds, nullptr);
  752. // Create a Repeat operation on ds
  753. int32_t repeat_num = 2;
  754. ds = ds->Repeat(repeat_num);
  755. EXPECT_NE(ds, nullptr);
  756. // Create objects for the tensor ops
  757. std::shared_ptr<TensorOperation> random_vertical_flip_op = vision::RandomVerticalFlip(0.5);
  758. EXPECT_NE(random_vertical_flip_op, nullptr);
  759. // Create a Map operation on ds
  760. ds = ds->Map({random_vertical_flip_op}, {}, {}, {"image", "label"});
  761. EXPECT_NE(ds, nullptr);
  762. // Create a Project operation on ds
  763. std::vector<std::string> column_project = {"image"};
  764. ds = ds->Project(column_project);
  765. EXPECT_NE(ds, nullptr);
  766. // Create a Batch operation on ds
  767. int32_t batch_size = 1;
  768. ds = ds->Batch(batch_size);
  769. EXPECT_NE(ds, nullptr);
  770. // Create an iterator over the result of the above dataset
  771. // This will trigger the creation of the Execution Tree and launch it.
  772. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  773. EXPECT_NE(iter, nullptr);
  774. // Iterate the dataset and get each row
  775. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  776. iter->GetNextRow(&row);
  777. uint64_t i = 0;
  778. while (row.size() != 0) {
  779. i++;
  780. auto image = row["image"];
  781. MS_LOG(INFO) << "Tensor image shape: " << image->shape();
  782. iter->GetNextRow(&row);
  783. }
  784. EXPECT_EQ(i, 20);
  785. // Manually terminate the pipeline
  786. iter->Stop();
  787. }
  788. TEST_F(MindDataTestPipeline, TestZipSuccess) {
  789. // Testing the member zip() function
  790. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestZipSuccess.";
  791. // Create an ImageFolder Dataset
  792. std::string folder_path = datasets_root_path_ + "/testPK/data/";
  793. std::shared_ptr<Dataset> ds = ImageFolder(folder_path, true, RandomSampler(false, 10));
  794. EXPECT_NE(ds, nullptr);
  795. // Create a Project operation on ds
  796. std::vector<std::string> column_project = {"image"};
  797. ds = ds->Project(column_project);
  798. EXPECT_NE(ds, nullptr);
  799. // Create an ImageFolder Dataset
  800. std::shared_ptr<Dataset> ds1 = ImageFolder(folder_path, true, RandomSampler(false, 10));
  801. EXPECT_NE(ds1, nullptr);
  802. // Create a Rename operation on ds (so that the 3 datasets we are going to zip have distinct column names)
  803. ds1 = ds1->Rename({"image", "label"}, {"col1", "col2"});
  804. EXPECT_NE(ds1, nullptr);
  805. folder_path = datasets_root_path_ + "/testCifar10Data/";
  806. std::shared_ptr<Dataset> ds2 = Cifar10(folder_path, RandomSampler(false, 10));
  807. EXPECT_NE(ds2, nullptr);
  808. // Create a Project operation on ds
  809. column_project = {"label"};
  810. ds2 = ds2->Project(column_project);
  811. EXPECT_NE(ds2, nullptr);
  812. // Create a Zip operation on the datasets
  813. ds = ds->Zip({ds1, ds2});
  814. EXPECT_NE(ds, nullptr);
  815. // Create a Batch operation on ds
  816. int32_t batch_size = 1;
  817. ds = ds->Batch(batch_size);
  818. EXPECT_NE(ds, nullptr);
  819. // Create an iterator over the result of the above dataset
  820. // This will trigger the creation of the Execution Tree and launch it.
  821. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  822. EXPECT_NE(iter, nullptr);
  823. // Iterate the dataset and get each row
  824. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  825. iter->GetNextRow(&row);
  826. // Check zipped column names
  827. EXPECT_EQ(row.size(), 4);
  828. EXPECT_NE(row.find("image"), row.end());
  829. EXPECT_NE(row.find("label"), row.end());
  830. EXPECT_NE(row.find("col1"), row.end());
  831. EXPECT_NE(row.find("col2"), row.end());
  832. uint64_t i = 0;
  833. while (row.size() != 0) {
  834. i++;
  835. auto image = row["image"];
  836. MS_LOG(INFO) << "Tensor image shape: " << image->shape();
  837. iter->GetNextRow(&row);
  838. }
  839. EXPECT_EQ(i, 10);
  840. // Manually terminate the pipeline
  841. iter->Stop();
  842. }
  843. TEST_F(MindDataTestPipeline, TestZipSuccess2) {
  844. // Testing the static zip() function
  845. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestZipSuccess2.";
  846. // Create an ImageFolder Dataset
  847. std::string folder_path = datasets_root_path_ + "/testPK/data/";
  848. std::shared_ptr<Dataset> ds = ImageFolder(folder_path, true, RandomSampler(false, 9));
  849. EXPECT_NE(ds, nullptr);
  850. std::shared_ptr<Dataset> ds2 = ImageFolder(folder_path, true, RandomSampler(false, 10));
  851. EXPECT_NE(ds2, nullptr);
  852. // Create a Rename operation on ds (so that the 2 datasets we are going to zip have distinct column names)
  853. ds = ds->Rename({"image", "label"}, {"col1", "col2"});
  854. EXPECT_NE(ds, nullptr);
  855. // Create a Zip operation on the datasets
  856. ds = Zip({ds, ds2});
  857. EXPECT_NE(ds, nullptr);
  858. // Create a Batch operation on ds
  859. int32_t batch_size = 1;
  860. ds = ds->Batch(batch_size);
  861. EXPECT_NE(ds, nullptr);
  862. // Create an iterator over the result of the above dataset
  863. // This will trigger the creation of the Execution Tree and launch it.
  864. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  865. EXPECT_NE(iter, nullptr);
  866. // Iterate the dataset and get each row
  867. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  868. iter->GetNextRow(&row);
  869. // Check zipped column names
  870. EXPECT_EQ(row.size(), 4);
  871. EXPECT_NE(row.find("image"), row.end());
  872. EXPECT_NE(row.find("label"), row.end());
  873. EXPECT_NE(row.find("col1"), row.end());
  874. EXPECT_NE(row.find("col2"), row.end());
  875. uint64_t i = 0;
  876. while (row.size() != 0) {
  877. i++;
  878. auto image = row["image"];
  879. MS_LOG(INFO) << "Tensor image shape: " << image->shape();
  880. iter->GetNextRow(&row);
  881. }
  882. EXPECT_EQ(i, 9);
  883. // Manually terminate the pipeline
  884. iter->Stop();
  885. }
  886. TEST_F(MindDataTestPipeline, TestZipFail) {
  887. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestZipFail.";
  888. // We expect this test to fail because we are the both datasets we are zipping have "image" and "label" columns
  889. // and zip doesn't accept datasets with same column names
  890. // Create an ImageFolder Dataset
  891. std::string folder_path = datasets_root_path_ + "/testPK/data/";
  892. std::shared_ptr<Dataset> ds = ImageFolder(folder_path, true, RandomSampler(false, 10));
  893. EXPECT_NE(ds, nullptr);
  894. // Create an ImageFolder Dataset
  895. std::shared_ptr<Dataset> ds1 = ImageFolder(folder_path, true, RandomSampler(false, 10));
  896. EXPECT_NE(ds1, nullptr);
  897. // Create a Zip operation on the datasets
  898. ds = Zip({ds, ds1});
  899. EXPECT_NE(ds, nullptr);
  900. // Create a Batch operation on ds
  901. int32_t batch_size = 1;
  902. ds = ds->Batch(batch_size);
  903. EXPECT_NE(ds, nullptr);
  904. // Create an iterator over the result of the above dataset
  905. // This will trigger the creation of the Execution Tree and launch it.
  906. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  907. EXPECT_EQ(iter, nullptr);
  908. }
  909. TEST_F(MindDataTestPipeline, TestZipFail2) {
  910. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestZipFail2.";
  911. // This case is expected to fail because the input dataset is empty.
  912. // Create an ImageFolder Dataset
  913. std::string folder_path = datasets_root_path_ + "/testPK/data/";
  914. std::shared_ptr<Dataset> ds = ImageFolder(folder_path, true, RandomSampler(false, 10));
  915. EXPECT_NE(ds, nullptr);
  916. // Create a Zip operation on the datasets
  917. // Input dataset to zip is empty
  918. ds = Zip({});
  919. EXPECT_EQ(ds, nullptr);
  920. }
  921. TEST_F(MindDataTestPipeline, TestRenameSuccess) {
  922. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestRenameSuccess.";
  923. // Create an ImageFolder Dataset
  924. std::string folder_path = datasets_root_path_ + "/testPK/data/";
  925. std::shared_ptr<Dataset> ds = ImageFolder(folder_path, true, RandomSampler(false, 10));
  926. EXPECT_NE(ds, nullptr);
  927. // Create a Repeat operation on ds
  928. int32_t repeat_num = 2;
  929. ds = ds->Repeat(repeat_num);
  930. EXPECT_NE(ds, nullptr);
  931. // Create a Rename operation on ds
  932. ds = ds->Rename({"image", "label"}, {"col1", "col2"});
  933. EXPECT_NE(ds, nullptr);
  934. // Create a Batch operation on ds
  935. int32_t batch_size = 1;
  936. ds = ds->Batch(batch_size);
  937. EXPECT_NE(ds, nullptr);
  938. // Create an iterator over the result of the above dataset
  939. // This will trigger the creation of the Execution Tree and launch it.
  940. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  941. EXPECT_NE(iter, nullptr);
  942. // Iterate the dataset and get each row
  943. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  944. iter->GetNextRow(&row);
  945. uint64_t i = 0;
  946. EXPECT_NE(row.find("col1"), row.end());
  947. EXPECT_NE(row.find("col2"), row.end());
  948. EXPECT_EQ(row.find("image"), row.end());
  949. EXPECT_EQ(row.find("label"), row.end());
  950. while (row.size() != 0) {
  951. i++;
  952. auto image = row["col1"];
  953. MS_LOG(INFO) << "Tensor image shape: " << image->shape();
  954. iter->GetNextRow(&row);
  955. }
  956. EXPECT_EQ(i, 20);
  957. // Manually terminate the pipeline
  958. iter->Stop();
  959. }
  960. TEST_F(MindDataTestPipeline, TestRenameFail) {
  961. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestRenameFail.";
  962. // We expect this test to fail because input and output in Rename are not the same size
  963. // Create an ImageFolder Dataset
  964. std::string folder_path = datasets_root_path_ + "/testPK/data/";
  965. std::shared_ptr<Dataset> ds = ImageFolder(folder_path, true, RandomSampler(false, 10));
  966. EXPECT_NE(ds, nullptr);
  967. // Create a Repeat operation on ds
  968. int32_t repeat_num = 2;
  969. ds = ds->Repeat(repeat_num);
  970. EXPECT_NE(ds, nullptr);
  971. // Create a Rename operation on ds
  972. ds = ds->Rename({"image", "label"}, {"col2"});
  973. EXPECT_EQ(ds, nullptr);
  974. }
  975. TEST_F(MindDataTestPipeline, TestVOCSegmentation) {
  976. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestVOCSegmentation.";
  977. // Create a VOC Dataset
  978. std::string folder_path = datasets_root_path_ + "/testVOC2012_2";
  979. std::shared_ptr<Dataset> ds = VOC(folder_path, "Segmentation", "train", {}, false, SequentialSampler(0, 3));
  980. EXPECT_NE(ds, nullptr);
  981. // Create a Repeat operation on ds
  982. int32_t repeat_num = 2;
  983. ds = ds->Repeat(repeat_num);
  984. EXPECT_NE(ds, nullptr);
  985. // Create an iterator over the result of the above dataset
  986. // This will trigger the creation of the Execution Tree and launch it.
  987. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  988. EXPECT_NE(iter, nullptr);
  989. // Iterate the dataset and get each row
  990. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  991. iter->GetNextRow(&row);
  992. // Check if VOCOp read correct images/targets
  993. using Tensor = mindspore::dataset::Tensor;
  994. std::string expect_file[] = {"32", "33", "39", "32", "33", "39"};
  995. uint64_t i = 0;
  996. while (row.size() != 0) {
  997. auto image = row["image"];
  998. auto target = row["target"];
  999. MS_LOG(INFO) << "Tensor image shape: " << image->shape();
  1000. MS_LOG(INFO) << "Tensor target shape: " << target->shape();
  1001. std::shared_ptr<Tensor> expect_image;
  1002. Tensor::CreateFromFile(folder_path + "/JPEGImages/" + expect_file[i] + ".jpg", &expect_image);
  1003. EXPECT_EQ(*image, *expect_image);
  1004. std::shared_ptr<Tensor> expect_target;
  1005. Tensor::CreateFromFile(folder_path + "/SegmentationClass/" + expect_file[i] + ".png", &expect_target);
  1006. EXPECT_EQ(*target, *expect_target);
  1007. iter->GetNextRow(&row);
  1008. i++;
  1009. }
  1010. EXPECT_EQ(i, 6);
  1011. // Manually terminate the pipeline
  1012. iter->Stop();
  1013. }
  1014. TEST_F(MindDataTestPipeline, TestVOCSegmentationError1) {
  1015. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestVOCSegmentationError1.";
  1016. // Create a VOC Dataset
  1017. std::map<std::string, int32_t> class_index;
  1018. class_index["car"] = 0;
  1019. std::string folder_path = datasets_root_path_ + "/testVOC2012_2";
  1020. std::shared_ptr<Dataset> ds = VOC(folder_path, "Segmentation", "train", class_index, false, RandomSampler(false, 6));
  1021. // Expect nullptr for segmentation task with class_index
  1022. EXPECT_EQ(ds, nullptr);
  1023. }
  1024. TEST_F(MindDataTestPipeline, TestVOCInvalidTaskOrMode) {
  1025. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestVOCInvalidTaskOrMode.";
  1026. // Create a VOC Dataset
  1027. std::string folder_path = datasets_root_path_ + "/testVOC2012_2";
  1028. std::shared_ptr<Dataset> ds_1 = VOC(folder_path, "Classification", "train", {}, false, SequentialSampler(0, 3));
  1029. // Expect nullptr for invalid task
  1030. EXPECT_EQ(ds_1, nullptr);
  1031. std::shared_ptr<Dataset> ds_2 = VOC(folder_path, "Segmentation", "validation", {}, false, RandomSampler(false, 4));
  1032. // Expect nullptr for invalid mode
  1033. EXPECT_EQ(ds_2, nullptr);
  1034. }
  1035. TEST_F(MindDataTestPipeline, TestVOCDetection) {
  1036. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestVOCDetection.";
  1037. // Create a VOC Dataset
  1038. std::string folder_path = datasets_root_path_ + "/testVOC2012_2";
  1039. std::shared_ptr<Dataset> ds = VOC(folder_path, "Detection", "train", {}, false, SequentialSampler(0, 4));
  1040. EXPECT_NE(ds, nullptr);
  1041. // Create an iterator over the result of the above dataset
  1042. // This will trigger the creation of the Execution Tree and launch it.
  1043. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  1044. EXPECT_NE(iter, nullptr);
  1045. // Iterate the dataset and get each row
  1046. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  1047. iter->GetNextRow(&row);
  1048. // Check if VOCOp read correct images/labels
  1049. std::string expect_file[] = {"15", "32", "33", "39"};
  1050. uint32_t expect_num[] = {5, 5, 4, 3};
  1051. uint64_t i = 0;
  1052. while (row.size() != 0) {
  1053. auto image = row["image"];
  1054. auto label = row["label"];
  1055. MS_LOG(INFO) << "Tensor image shape: " << image->shape();
  1056. MS_LOG(INFO) << "Tensor label shape: " << label->shape();
  1057. std::shared_ptr<Tensor> expect_image;
  1058. Tensor::CreateFromFile(folder_path + "/JPEGImages/" + expect_file[i] + ".jpg", &expect_image);
  1059. EXPECT_EQ(*image, *expect_image);
  1060. std::shared_ptr<Tensor> expect_label;
  1061. Tensor::CreateFromMemory(TensorShape({1, 1}), DataType(DataType::DE_UINT32), nullptr, &expect_label);
  1062. expect_label->SetItemAt({0, 0}, expect_num[i]);
  1063. EXPECT_EQ(*label, *expect_label);
  1064. iter->GetNextRow(&row);
  1065. i++;
  1066. }
  1067. EXPECT_EQ(i, 4);
  1068. // Manually terminate the pipeline
  1069. iter->Stop();
  1070. }
  1071. TEST_F(MindDataTestPipeline, TestVOCClassIndex) {
  1072. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestVOCClassIndex.";
  1073. // Create a VOC Dataset
  1074. std::string folder_path = datasets_root_path_ + "/testVOC2012_2";
  1075. std::map<std::string, int32_t> class_index;
  1076. class_index["car"] = 0;
  1077. class_index["cat"] = 1;
  1078. class_index["train"] = 9;
  1079. std::shared_ptr<Dataset> ds = VOC(folder_path, "Detection", "train", class_index, false, SequentialSampler(0, 6));
  1080. EXPECT_NE(ds, nullptr);
  1081. // Create an iterator over the result of the above dataset
  1082. // This will trigger the creation of the Execution Tree and launch it.
  1083. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  1084. EXPECT_NE(iter, nullptr);
  1085. // Iterate the dataset and get each row
  1086. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  1087. iter->GetNextRow(&row);
  1088. // Check if VOCOp read correct labels
  1089. // When we provide class_index, label of ["car","cat","train"] become [0,1,9]
  1090. std::shared_ptr<Tensor> expect_label;
  1091. Tensor::CreateFromMemory(TensorShape({1, 1}), DataType(DataType::DE_UINT32), nullptr, &expect_label);
  1092. uint32_t expect[] = {9, 9, 9, 1, 1, 0};
  1093. uint64_t i = 0;
  1094. while (row.size() != 0) {
  1095. auto image = row["image"];
  1096. auto label = row["label"];
  1097. MS_LOG(INFO) << "Tensor image shape: " << image->shape();
  1098. MS_LOG(INFO) << "Tensor label shape: " << label->shape();
  1099. expect_label->SetItemAt({0, 0}, expect[i]);
  1100. EXPECT_EQ(*label, *expect_label);
  1101. iter->GetNextRow(&row);
  1102. i++;
  1103. }
  1104. EXPECT_EQ(i, 6);
  1105. // Manually terminate the pipeline
  1106. iter->Stop();
  1107. }
  1108. TEST_F(MindDataTestPipeline, TestCocoDetection) {
  1109. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestCocoDetection.";
  1110. // Create a Coco Dataset
  1111. std::string folder_path = datasets_root_path_ + "/testCOCO/train";
  1112. std::string annotation_file = datasets_root_path_ + "/testCOCO/annotations/train.json";
  1113. std::shared_ptr<Dataset> ds = Coco(folder_path, annotation_file, "Detection", false, SequentialSampler(0, 6));
  1114. EXPECT_NE(ds, nullptr);
  1115. // Create an iterator over the result of the above dataset
  1116. // This will trigger the creation of the Execution Tree and launch it.
  1117. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  1118. EXPECT_NE(iter, nullptr);
  1119. // Iterate the dataset and get each row
  1120. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  1121. iter->GetNextRow(&row);
  1122. std::string expect_file[] = {"000000391895", "000000318219", "000000554625", "000000574769", "000000060623",
  1123. "000000309022"};
  1124. std::vector<std::vector<float>> expect_bbox_vector = {{10.0, 10.0, 10.0, 10.0, 70.0, 70.0, 70.0, 70.0},
  1125. {20.0, 20.0, 20.0, 20.0, 80.0, 80.0, 80.0, 80.0},
  1126. {30.0, 30.0, 30.0, 30.0}, {40.0, 40.0, 40.0, 40.0},
  1127. {50.0, 50.0, 50.0, 50.0}, {60.0, 60.0, 60.0, 60.0}};
  1128. std::vector<std::vector<uint32_t>> expect_catagoryid_list = {{1, 7}, {2, 8}, {3}, {4}, {5}, {6}};
  1129. uint64_t i = 0;
  1130. while (row.size() != 0) {
  1131. auto image = row["image"];
  1132. auto bbox = row["bbox"];
  1133. auto category_id = row["category_id"];
  1134. std::shared_ptr<Tensor> expect_image;
  1135. Tensor::CreateFromFile(folder_path + "/" + expect_file[i] + ".jpg", &expect_image);
  1136. EXPECT_EQ(*image, *expect_image);
  1137. std::shared_ptr<Tensor> expect_bbox;
  1138. dsize_t bbox_num = static_cast<dsize_t>(expect_bbox_vector[i].size() / 4);
  1139. Tensor::CreateFromVector(expect_bbox_vector[i], TensorShape({bbox_num, 4}), &expect_bbox);
  1140. EXPECT_EQ(*bbox, *expect_bbox);
  1141. std::shared_ptr<Tensor> expect_categoryid;
  1142. Tensor::CreateFromVector(expect_catagoryid_list[i], TensorShape({bbox_num, 1}), &expect_categoryid);
  1143. EXPECT_EQ(*category_id, *expect_categoryid);
  1144. iter->GetNextRow(&row);
  1145. i++;
  1146. }
  1147. EXPECT_EQ(i, 6);
  1148. // Manually terminate the pipeline
  1149. iter->Stop();
  1150. }
  1151. TEST_F(MindDataTestPipeline, TestCocoStuff) {
  1152. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestCocoStuff.";
  1153. // Create a Coco Dataset
  1154. std::string folder_path = datasets_root_path_ + "/testCOCO/train";
  1155. std::string annotation_file = datasets_root_path_ + "/testCOCO/annotations/train.json";
  1156. std::shared_ptr<Dataset> ds = Coco(folder_path, annotation_file, "Stuff", false, SequentialSampler(0, 6));
  1157. EXPECT_NE(ds, nullptr);
  1158. // Create an iterator over the result of the above dataset
  1159. // This will trigger the creation of the Execution Tree and launch it.
  1160. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  1161. EXPECT_NE(iter, nullptr);
  1162. // Iterate the dataset and get each row
  1163. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  1164. iter->GetNextRow(&row);
  1165. std::string expect_file[] = {"000000391895", "000000318219", "000000554625", "000000574769", "000000060623",
  1166. "000000309022"};
  1167. std::vector<std::vector<float>> expect_segmentation_vector =
  1168. {{10.0, 12.0, 13.0, 14.0, 15.0, 16.0, 17.0, 18.0, 19.0, 20.0,
  1169. 70.0, 72.0, 73.0, 74.0, 75.0, -1.0, -1.0, -1.0, -1.0, -1.0},
  1170. {20.0, 22.0, 23.0, 24.0, 25.0, 26.0, 27.0, 28.0, 29.0, 30.0, 31.0,
  1171. 10.0, 12.0, 13.0, 14.0, 15.0, 16.0, 17.0, 18.0, 19.0, 20.0, -1.0},
  1172. {40.0, 42.0, 43.0, 44.0, 45.0, 46.0, 47.0, 48.0, 49.0, 40.0, 41.0, 42.0},
  1173. {50.0, 52.0, 53.0, 54.0, 55.0, 56.0, 57.0, 58.0, 59.0, 60.0, 61.0, 62.0, 63.0},
  1174. {60.0, 62.0, 63.0, 64.0, 65.0, 66.0, 67.0, 68.0, 69.0, 70.0, 71.0, 72.0, 73.0, 74.0},
  1175. {60.0, 62.0, 63.0, 64.0, 65.0, 66.0, 67.0, 68.0, 69.0, 70.0, 71.0, 72.0, 73.0, 74.0}};
  1176. std::vector<std::vector<dsize_t>> expect_size = {{2, 10}, {2, 11}, {1, 12}, {1, 13}, {1, 14}, {2, 7}};
  1177. uint64_t i = 0;
  1178. while (row.size() != 0) {
  1179. auto image = row["image"];
  1180. auto segmentation = row["segmentation"];
  1181. auto iscrowd = row["iscrowd"];
  1182. std::shared_ptr<Tensor> expect_image;
  1183. Tensor::CreateFromFile(folder_path + "/" + expect_file[i] + ".jpg", &expect_image);
  1184. EXPECT_EQ(*image, *expect_image);
  1185. std::shared_ptr<Tensor> expect_segmentation;
  1186. Tensor::CreateFromVector(expect_segmentation_vector[i], TensorShape(expect_size[i]), &expect_segmentation);
  1187. EXPECT_EQ(*segmentation, *expect_segmentation);
  1188. iter->GetNextRow(&row);
  1189. i++;
  1190. }
  1191. EXPECT_EQ(i, 6);
  1192. // Manually terminate the pipeline
  1193. iter->Stop();
  1194. }
  1195. TEST_F(MindDataTestPipeline, TestCocoKeypoint) {
  1196. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestCocoKeypoint.";
  1197. // Create a Coco Dataset
  1198. std::string folder_path = datasets_root_path_ + "/testCOCO/train";
  1199. std::string annotation_file = datasets_root_path_ + "/testCOCO/annotations/key_point.json";
  1200. std::shared_ptr<Dataset> ds = Coco(folder_path, annotation_file, "Keypoint", false, SequentialSampler(0, 2));
  1201. EXPECT_NE(ds, nullptr);
  1202. // Create an iterator over the result of the above dataset
  1203. // This will trigger the creation of the Execution Tree and launch it.
  1204. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  1205. EXPECT_NE(iter, nullptr);
  1206. // Iterate the dataset and get each row
  1207. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  1208. iter->GetNextRow(&row);
  1209. std::string expect_file[] = {"000000391895", "000000318219"};
  1210. std::vector<std::vector<float>> expect_keypoint_vector =
  1211. {{368.0, 61.0, 1.0, 369.0, 52.0, 2.0, 0.0, 0.0, 0.0, 382.0, 48.0, 2.0, 0.0, 0.0, 0.0, 368.0, 84.0, 2.0, 435.0,
  1212. 81.0, 2.0, 362.0, 125.0, 2.0, 446.0, 125.0, 2.0, 360.0, 153.0, 2.0, 0.0, 0.0, 0.0, 397.0, 167.0, 1.0, 439.0,
  1213. 166.0, 1.0, 369.0, 193.0, 2.0, 461.0, 234.0, 2.0, 361.0, 246.0, 2.0, 474.0, 287.0, 2.0},
  1214. {244.0, 139.0, 2.0, 0.0, 0.0, 0.0, 226.0, 118.0, 2.0, 0.0, 0.0, 0.0, 154.0, 159.0, 2.0, 143.0, 261.0, 2.0, 135.0,
  1215. 312.0, 2.0, 271.0, 423.0, 2.0, 184.0, 530.0, 2.0, 261.0, 280.0, 2.0, 347.0, 592.0, 2.0, 0.0, 0.0, 0.0, 123.0,
  1216. 596.0, 2.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0}};
  1217. std::vector<std::vector<dsize_t>> expect_size = {{1, 51}, {1, 51}};
  1218. std::vector<std::vector<uint32_t>> expect_num_keypoints_list = {{14}, {10}};
  1219. uint64_t i = 0;
  1220. while (row.size() != 0) {
  1221. auto image = row["image"];
  1222. auto keypoints = row["keypoints"];
  1223. auto num_keypoints = row["num_keypoints"];
  1224. std::shared_ptr<Tensor> expect_image;
  1225. Tensor::CreateFromFile(folder_path + "/" + expect_file[i] + ".jpg", &expect_image);
  1226. EXPECT_EQ(*image, *expect_image);
  1227. std::shared_ptr<Tensor> expect_keypoints;
  1228. dsize_t keypoints_size = expect_size[i][0];
  1229. Tensor::CreateFromVector(expect_keypoint_vector[i], TensorShape(expect_size[i]), &expect_keypoints);
  1230. EXPECT_EQ(*keypoints, *expect_keypoints);
  1231. std::shared_ptr<Tensor> expect_num_keypoints;
  1232. Tensor::CreateFromVector(expect_num_keypoints_list[i], TensorShape({keypoints_size, 1}), &expect_num_keypoints);
  1233. EXPECT_EQ(*num_keypoints, *expect_num_keypoints);
  1234. iter->GetNextRow(&row);
  1235. i++;
  1236. }
  1237. EXPECT_EQ(i, 2);
  1238. // Manually terminate the pipeline
  1239. iter->Stop();
  1240. }
  1241. TEST_F(MindDataTestPipeline, TestCocoPanoptic) {
  1242. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestCocoPanoptic.";
  1243. // Create a Coco Dataset
  1244. std::string folder_path = datasets_root_path_ + "/testCOCO/train";
  1245. std::string annotation_file = datasets_root_path_ + "/testCOCO/annotations/panoptic.json";
  1246. std::shared_ptr<Dataset> ds = Coco(folder_path, annotation_file, "Panoptic", false, SequentialSampler(0, 2));
  1247. EXPECT_NE(ds, nullptr);
  1248. // Create an iterator over the result of the above dataset
  1249. // This will trigger the creation of the Execution Tree and launch it.
  1250. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  1251. EXPECT_NE(iter, nullptr);
  1252. // Iterate the dataset and get each row
  1253. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  1254. iter->GetNextRow(&row);
  1255. std::string expect_file[] = {"000000391895", "000000574769"};
  1256. std::vector<std::vector<float>> expect_bbox_vector = {{472, 173, 36, 48, 340, 22, 154, 301, 486, 183, 30, 35},
  1257. {103, 133, 229, 422, 243, 175, 93, 164}};
  1258. std::vector<std::vector<uint32_t>> expect_categoryid_vector = {{1, 1, 2}, {1, 3}};
  1259. std::vector<std::vector<uint32_t>> expect_iscrowd_vector = {{0, 0, 0}, {0, 0}};
  1260. std::vector<std::vector<uint32_t>> expect_area_vector = {{705, 14062, 626}, {43102, 6079}};
  1261. std::vector<std::vector<dsize_t>> expect_size = {{3, 4}, {2, 4}};
  1262. uint64_t i = 0;
  1263. while (row.size() != 0) {
  1264. auto image = row["image"];
  1265. auto bbox = row["bbox"];
  1266. auto category_id = row["category_id"];
  1267. auto iscrowd = row["iscrowd"];
  1268. auto area = row["area"];
  1269. std::shared_ptr<Tensor> expect_image;
  1270. Tensor::CreateFromFile(folder_path + "/" + expect_file[i] + ".jpg", &expect_image);
  1271. EXPECT_EQ(*image, *expect_image);
  1272. std::shared_ptr<Tensor> expect_bbox;
  1273. dsize_t bbox_size = expect_size[i][0];
  1274. Tensor::CreateFromVector(expect_bbox_vector[i], TensorShape(expect_size[i]), &expect_bbox);
  1275. EXPECT_EQ(*bbox, *expect_bbox);
  1276. std::shared_ptr<Tensor> expect_categoryid;
  1277. Tensor::CreateFromVector(expect_categoryid_vector[i], TensorShape({bbox_size, 1}), &expect_categoryid);
  1278. EXPECT_EQ(*category_id, *expect_categoryid);
  1279. std::shared_ptr<Tensor> expect_iscrowd;
  1280. Tensor::CreateFromVector(expect_iscrowd_vector[i], TensorShape({bbox_size, 1}), &expect_iscrowd);
  1281. EXPECT_EQ(*iscrowd, *expect_iscrowd);
  1282. std::shared_ptr<Tensor> expect_area;
  1283. Tensor::CreateFromVector(expect_area_vector[i], TensorShape({bbox_size, 1}), &expect_area);
  1284. EXPECT_EQ(*area, *expect_area);
  1285. iter->GetNextRow(&row);
  1286. i++;
  1287. }
  1288. EXPECT_EQ(i, 2);
  1289. // Manually terminate the pipeline
  1290. iter->Stop();
  1291. }
  1292. TEST_F(MindDataTestPipeline, TestCocoDefault) {
  1293. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestCocoDetection.";
  1294. // Create a Coco Dataset
  1295. std::string folder_path = datasets_root_path_ + "/testCOCO/train";
  1296. std::string annotation_file = datasets_root_path_ + "/testCOCO/annotations/train.json";
  1297. std::shared_ptr<Dataset> ds = Coco(folder_path, annotation_file);
  1298. EXPECT_NE(ds, nullptr);
  1299. // Create an iterator over the result of the above dataset
  1300. // This will trigger the creation of the Execution Tree and launch it.
  1301. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  1302. EXPECT_NE(iter, nullptr);
  1303. // Iterate the dataset and get each row
  1304. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  1305. iter->GetNextRow(&row);
  1306. uint64_t i = 0;
  1307. while (row.size() != 0) {
  1308. auto image = row["image"];
  1309. auto bbox = row["bbox"];
  1310. auto category_id = row["category_id"];
  1311. MS_LOG(INFO) << "Tensor image shape: " << image->shape();
  1312. MS_LOG(INFO) << "Tensor bbox shape: " << bbox->shape();
  1313. MS_LOG(INFO) << "Tensor category_id shape: " << category_id->shape();
  1314. iter->GetNextRow(&row);
  1315. i++;
  1316. }
  1317. EXPECT_EQ(i, 6);
  1318. // Manually terminate the pipeline
  1319. iter->Stop();
  1320. }
  1321. TEST_F(MindDataTestPipeline, TestCocoException) {
  1322. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestCocoDetection.";
  1323. // Create a Coco Dataset
  1324. std::string folder_path = datasets_root_path_ + "/testCOCO/train";
  1325. std::string annotation_file = datasets_root_path_ + "/testCOCO/annotations/train.json";
  1326. std::string invalid_folder_path = "./NotExist";
  1327. std::string invalid_annotation_file = "./NotExistFile";
  1328. std::shared_ptr<Dataset> ds = Coco(invalid_folder_path, annotation_file);
  1329. EXPECT_EQ(ds, nullptr);
  1330. std::shared_ptr<Dataset> ds1 = Coco(folder_path, invalid_annotation_file);
  1331. EXPECT_EQ(ds1, nullptr);
  1332. std::shared_ptr<Dataset> ds2 = Coco(folder_path, annotation_file, "valid_mode");
  1333. EXPECT_EQ(ds2, nullptr);
  1334. }
  1335. TEST_F(MindDataTestPipeline, TestConcatSuccess) {
  1336. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestConcatSuccess.";
  1337. // Create an ImageFolder Dataset
  1338. // Column names: {"image", "label"}
  1339. std::string folder_path = datasets_root_path_ + "/testPK/data/";
  1340. std::shared_ptr<Dataset> ds = ImageFolder(folder_path, true, RandomSampler(false, 10));
  1341. EXPECT_NE(ds, nullptr);
  1342. // Create a Cifar10 Dataset
  1343. // Column names: {"image", "label"}
  1344. folder_path = datasets_root_path_ + "/testCifar10Data/";
  1345. std::shared_ptr<Dataset> ds2 = Cifar10(folder_path, RandomSampler(false, 9));
  1346. EXPECT_NE(ds2, nullptr);
  1347. // Create a Project operation on ds
  1348. ds = ds->Project({"image"});
  1349. EXPECT_NE(ds, nullptr);
  1350. ds2 = ds2->Project({"image"});
  1351. EXPECT_NE(ds, nullptr);
  1352. // Create a Concat operation on the ds
  1353. ds = ds->Concat({ds2});
  1354. EXPECT_NE(ds, nullptr);
  1355. // Create a Batch operation on ds
  1356. int32_t batch_size = 1;
  1357. ds = ds->Batch(batch_size);
  1358. EXPECT_NE(ds, nullptr);
  1359. // Create an iterator over the result of the above dataset
  1360. // This will trigger the creation of the Execution Tree and launch it.
  1361. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  1362. EXPECT_NE(iter, nullptr);
  1363. // Iterate the dataset and get each row
  1364. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  1365. iter->GetNextRow(&row);
  1366. uint64_t i = 0;
  1367. while (row.size() != 0) {
  1368. i++;
  1369. auto image = row["image"];
  1370. MS_LOG(INFO) << "Tensor image shape: " << image->shape();
  1371. iter->GetNextRow(&row);
  1372. }
  1373. EXPECT_EQ(i, 19);
  1374. // Manually terminate the pipeline
  1375. iter->Stop();
  1376. }
  1377. TEST_F(MindDataTestPipeline, TestConcatSuccess2) {
  1378. // Test "+" operator to concat two datasets
  1379. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestConcatSuccess2.";
  1380. // Create an ImageFolder Dataset
  1381. // Column names: {"image", "label"}
  1382. std::string folder_path = datasets_root_path_ + "/testPK/data/";
  1383. std::shared_ptr<Dataset> ds = ImageFolder(folder_path, true, RandomSampler(false, 10));
  1384. EXPECT_NE(ds, nullptr);
  1385. // Create a Cifar10 Dataset
  1386. // Column names: {"image", "label"}
  1387. folder_path = datasets_root_path_ + "/testCifar10Data/";
  1388. std::shared_ptr<Dataset> ds2 = Cifar10(folder_path, RandomSampler(false, 9));
  1389. EXPECT_NE(ds2, nullptr);
  1390. // Create a Project operation on ds
  1391. ds = ds->Project({"image"});
  1392. EXPECT_NE(ds, nullptr);
  1393. ds2 = ds2->Project({"image"});
  1394. EXPECT_NE(ds, nullptr);
  1395. // Create a Concat operation on the ds
  1396. ds = ds + ds2;
  1397. EXPECT_NE(ds, nullptr);
  1398. // Create a Batch operation on ds
  1399. int32_t batch_size = 1;
  1400. ds = ds->Batch(batch_size);
  1401. EXPECT_NE(ds, nullptr);
  1402. // Create an iterator over the result of the above dataset
  1403. // This will trigger the creation of the Execution Tree and launch it.
  1404. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  1405. EXPECT_NE(iter, nullptr);
  1406. // Iterate the dataset and get each row
  1407. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  1408. iter->GetNextRow(&row);
  1409. uint64_t i = 0;
  1410. while (row.size() != 0) {
  1411. i++;
  1412. auto image = row["image"];
  1413. MS_LOG(INFO) << "Tensor image shape: " << image->shape();
  1414. iter->GetNextRow(&row);
  1415. }
  1416. EXPECT_EQ(i, 19);
  1417. // Manually terminate the pipeline
  1418. iter->Stop();
  1419. }
  1420. TEST_F(MindDataTestPipeline, TestConcatFail1) {
  1421. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestConcatFail1.";
  1422. // This case is expected to fail because the input column names of concatenated datasets are not the same
  1423. // Create an ImageFolder Dataset
  1424. // Column names: {"image", "label"}
  1425. std::string folder_path = datasets_root_path_ + "/testPK/data/";
  1426. std::shared_ptr<Dataset> ds = ImageFolder(folder_path, true, RandomSampler(false, 10));
  1427. EXPECT_NE(ds, nullptr);
  1428. std::shared_ptr<Dataset> ds2 = ImageFolder(folder_path, true, RandomSampler(false, 10));
  1429. EXPECT_NE(ds, nullptr);
  1430. // Create a Rename operation on ds
  1431. ds2 = ds2->Rename({"image", "label"}, {"col1", "col2"});
  1432. EXPECT_NE(ds, nullptr);
  1433. // Create a Project operation on the ds
  1434. // Name of datasets to concat doesn't not match
  1435. ds = ds->Concat({ds2});
  1436. EXPECT_NE(ds, nullptr);
  1437. // Create a Batch operation on ds
  1438. int32_t batch_size = 1;
  1439. ds = ds->Batch(batch_size);
  1440. EXPECT_NE(ds, nullptr);
  1441. // Create an iterator over the result of the above dataset
  1442. // This will trigger the creation of the Execution Tree and launch it.
  1443. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  1444. EXPECT_EQ(iter, nullptr);
  1445. }
  1446. TEST_F(MindDataTestPipeline, TestConcatFail2) {
  1447. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestConcatFail2.";
  1448. // This case is expected to fail because the input dataset is empty.
  1449. // Create an ImageFolder Dataset
  1450. std::string folder_path = datasets_root_path_ + "/testPK/data/";
  1451. std::shared_ptr<Dataset> ds = ImageFolder(folder_path, true, RandomSampler(false, 10));
  1452. EXPECT_NE(ds, nullptr);
  1453. // Create a Project operation on the ds
  1454. // Input dataset to concat is empty
  1455. ds = ds->Concat({});
  1456. EXPECT_EQ(ds, nullptr);
  1457. }