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_vision_test.cc 114 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141
  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 "common/common.h"
  17. #include "minddata/dataset/include/datasets.h"
  18. #include "minddata/dataset/include/transforms.h"
  19. #include "minddata/dataset/include/vision.h"
  20. using namespace mindspore::dataset;
  21. using mindspore::dataset::BorderType;
  22. using mindspore::dataset::InterpolationMode;
  23. using mindspore::dataset::Tensor;
  24. class MindDataTestPipeline : public UT::DatasetOpTesting {
  25. protected:
  26. };
  27. // Tests for vision ops (in alphabetical order)
  28. TEST_F(MindDataTestPipeline, TestAutoContrastSuccess1) {
  29. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestAutoContrastSuccess1.";
  30. // Create an ImageFolder Dataset
  31. std::string folder_path = datasets_root_path_ + "/testPK/data/";
  32. std::shared_ptr<Dataset> ds = ImageFolder(folder_path, true, RandomSampler(false, 5));
  33. EXPECT_NE(ds, nullptr);
  34. // Create a Repeat operation on ds
  35. int32_t repeat_num = 3;
  36. ds = ds->Repeat(repeat_num);
  37. EXPECT_NE(ds, nullptr);
  38. // Create auto contrast object with default values
  39. std::shared_ptr<TensorOperation> auto_contrast = vision::AutoContrast();
  40. EXPECT_NE(auto_contrast, nullptr);
  41. // Create a Map operation on ds
  42. ds = ds->Map({auto_contrast});
  43. EXPECT_NE(ds, nullptr);
  44. // Create a Batch operation on ds
  45. int32_t batch_size = 1;
  46. ds = ds->Batch(batch_size);
  47. EXPECT_NE(ds, nullptr);
  48. // Create an iterator over the result of the above dataset
  49. // This will trigger the creation of the Execution Tree and launch it.
  50. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  51. EXPECT_NE(iter, nullptr);
  52. // Iterate the dataset and get each row
  53. std::unordered_map<std::string, mindspore::MSTensor> row;
  54. iter->GetNextRow(&row);
  55. uint64_t i = 0;
  56. while (row.size() != 0) {
  57. i++;
  58. // auto image = row["image"];
  59. // MS_LOG(INFO) << "Tensor image shape: " << image->shape();
  60. iter->GetNextRow(&row);
  61. }
  62. EXPECT_EQ(i, 15);
  63. // Manually terminate the pipeline
  64. iter->Stop();
  65. }
  66. TEST_F(MindDataTestPipeline, TestAutoContrastSuccess2) {
  67. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestAutoContrastSuccess2.";
  68. // Create an ImageFolder Dataset
  69. std::string folder_path = datasets_root_path_ + "/testPK/data/";
  70. std::shared_ptr<Dataset> ds = ImageFolder(folder_path, true, RandomSampler(false, 5));
  71. EXPECT_NE(ds, nullptr);
  72. // Create a Repeat operation on ds
  73. int32_t repeat_num = 3;
  74. ds = ds->Repeat(repeat_num);
  75. EXPECT_NE(ds, nullptr);
  76. // Create auto contrast object
  77. std::shared_ptr<TensorOperation> auto_contrast = vision::AutoContrast(10, {10, 20});
  78. EXPECT_NE(auto_contrast, nullptr);
  79. // Create a Map operation on ds
  80. ds = ds->Map({auto_contrast});
  81. EXPECT_NE(ds, nullptr);
  82. // Create a Batch operation on ds
  83. int32_t batch_size = 1;
  84. ds = ds->Batch(batch_size);
  85. EXPECT_NE(ds, nullptr);
  86. // Create an iterator over the result of the above dataset
  87. // This will trigger the creation of the Execution Tree and launch it.
  88. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  89. EXPECT_NE(iter, nullptr);
  90. // Iterate the dataset and get each row
  91. std::unordered_map<std::string, mindspore::MSTensor> row;
  92. iter->GetNextRow(&row);
  93. uint64_t i = 0;
  94. while (row.size() != 0) {
  95. i++;
  96. // auto image = row["image"];
  97. // MS_LOG(INFO) << "Tensor image shape: " << image->shape();
  98. iter->GetNextRow(&row);
  99. }
  100. EXPECT_EQ(i, 15);
  101. // Manually terminate the pipeline
  102. iter->Stop();
  103. }
  104. TEST_F(MindDataTestPipeline, TestAutoContrastFail) {
  105. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestAutoContrastFail with invalid params.";
  106. // Testing invalid cutoff < 0
  107. std::shared_ptr<TensorOperation> auto_contrast1 = vision::AutoContrast(-1.0);
  108. EXPECT_EQ(auto_contrast1, nullptr);
  109. // Testing invalid cutoff > 100
  110. std::shared_ptr<TensorOperation> auto_contrast2 = vision::AutoContrast(110.0, {10, 20});
  111. EXPECT_EQ(auto_contrast2, nullptr);
  112. }
  113. TEST_F(MindDataTestPipeline, TestBoundingBoxAugmentSuccess) {
  114. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestBoundingBoxAugmentSuccess.";
  115. // Create an VOC Dataset
  116. std::string folder_path = datasets_root_path_ + "/testVOC2012_2";
  117. std::shared_ptr<Dataset> ds = VOC(folder_path, "Detection", "train", {}, true, SequentialSampler(0, 3));
  118. EXPECT_NE(ds, nullptr);
  119. // Create objects for the tensor ops
  120. std::shared_ptr<TensorOperation> bound_box_augment = vision::BoundingBoxAugment(vision::RandomRotation({90.0}), 1.0);
  121. EXPECT_NE(bound_box_augment, nullptr);
  122. // Create a Map operation on ds
  123. ds = ds->Map({bound_box_augment}, {"image", "bbox"}, {"image", "bbox"}, {"image", "bbox"});
  124. EXPECT_NE(ds, nullptr);
  125. // Create an iterator over the result of the above dataset
  126. // This will trigger the creation of the Execution Tree and launch it.
  127. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  128. EXPECT_NE(iter, nullptr);
  129. // Iterate the dataset and get each row
  130. std::unordered_map<std::string, mindspore::MSTensor> row;
  131. iter->GetNextRow(&row);
  132. uint64_t i = 0;
  133. while (row.size() != 0) {
  134. i++;
  135. // auto image = row["image"];
  136. // MS_LOG(INFO) << "Tensor image shape: " << image->shape();
  137. iter->GetNextRow(&row);
  138. }
  139. EXPECT_EQ(i, 3);
  140. // Manually terminate the pipeline
  141. iter->Stop();
  142. }
  143. TEST_F(MindDataTestPipeline, TestBoundingBoxAugmentFail) {
  144. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestBoundingBoxAugmentFail with invalid params.";
  145. // Testing invalid ratio < 0.0
  146. std::shared_ptr<TensorOperation> bound_box_augment = vision::BoundingBoxAugment(vision::RandomRotation({90.0}), -1.0);
  147. EXPECT_EQ(bound_box_augment, nullptr);
  148. // Testing invalid ratio > 1.0
  149. std::shared_ptr<TensorOperation> bound_box_augment1 = vision::BoundingBoxAugment(vision::RandomRotation({90.0}), 2.0);
  150. EXPECT_EQ(bound_box_augment1, nullptr);
  151. // Testing invalid transform
  152. std::shared_ptr<TensorOperation> bound_box_augment2 = vision::BoundingBoxAugment(nullptr, 0.5);
  153. EXPECT_EQ(bound_box_augment2, nullptr);
  154. }
  155. TEST_F(MindDataTestPipeline, TestCenterCrop) {
  156. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestCenterCrop with single integer input.";
  157. // Create an ImageFolder Dataset
  158. std::string folder_path = datasets_root_path_ + "/testPK/data/";
  159. std::shared_ptr<Dataset> ds = ImageFolder(folder_path, true, RandomSampler(false, 5));
  160. EXPECT_NE(ds, nullptr);
  161. // Create a Repeat operation on ds
  162. int32_t repeat_num = 3;
  163. ds = ds->Repeat(repeat_num);
  164. EXPECT_NE(ds, nullptr);
  165. // Create centre crop object with square crop
  166. std::shared_ptr<TensorOperation> centre_out1 = vision::CenterCrop({30});
  167. EXPECT_NE(centre_out1, nullptr);
  168. // Create a Map operation on ds
  169. ds = ds->Map({centre_out1});
  170. EXPECT_NE(ds, nullptr);
  171. // Create a Batch operation on ds
  172. int32_t batch_size = 1;
  173. ds = ds->Batch(batch_size);
  174. EXPECT_NE(ds, nullptr);
  175. // Create an iterator over the result of the above dataset
  176. // This will trigger the creation of the Execution Tree and launch it.
  177. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  178. EXPECT_NE(iter, nullptr);
  179. // Iterate the dataset and get each row
  180. std::unordered_map<std::string, mindspore::MSTensor> row;
  181. iter->GetNextRow(&row);
  182. uint64_t i = 0;
  183. while (row.size() != 0) {
  184. i++;
  185. // auto image = row["image"];
  186. // MS_LOG(INFO) << "Tensor image shape: " << image->shape();
  187. iter->GetNextRow(&row);
  188. }
  189. EXPECT_EQ(i, 15);
  190. // Manually terminate the pipeline
  191. iter->Stop();
  192. }
  193. TEST_F(MindDataTestPipeline, TestCenterCropFail) {
  194. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestCenterCrop with invalid parameters.";
  195. // center crop height value negative
  196. std::shared_ptr<TensorOperation> center_crop = mindspore::dataset::vision::CenterCrop({-32, 32});
  197. EXPECT_EQ(center_crop, nullptr);
  198. // center crop width value negative
  199. center_crop = mindspore::dataset::vision::CenterCrop({32, -32});
  200. EXPECT_EQ(center_crop, nullptr);
  201. // 0 value would result in nullptr
  202. center_crop = mindspore::dataset::vision::CenterCrop({0, 32});
  203. EXPECT_EQ(center_crop, nullptr);
  204. // center crop with 3 values
  205. center_crop = mindspore::dataset::vision::CenterCrop({10, 20, 30});
  206. EXPECT_EQ(center_crop, nullptr);
  207. }
  208. TEST_F(MindDataTestPipeline, TestCropFail) {
  209. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestCrop with invalid parameters.";
  210. // wrong width
  211. std::shared_ptr<TensorOperation> crop = mindspore::dataset::vision::Crop({0, 0}, {32, -32});
  212. EXPECT_EQ(crop, nullptr);
  213. // wrong height
  214. crop = mindspore::dataset::vision::Crop({0, 0}, {-32, -32});
  215. EXPECT_EQ(crop, nullptr);
  216. // zero height
  217. crop = mindspore::dataset::vision::Crop({0, 0}, {0, 32});
  218. EXPECT_EQ(crop, nullptr);
  219. // negative coordinates
  220. crop = mindspore::dataset::vision::Crop({-1, 0}, {32, 32});
  221. EXPECT_EQ(crop, nullptr);
  222. }
  223. TEST_F(MindDataTestPipeline, TestCutMixBatchSuccess1) {
  224. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestCutMixBatchSuccess1.";
  225. // Testing CutMixBatch on a batch of CHW images
  226. // Create a Cifar10 Dataset
  227. std::string folder_path = datasets_root_path_ + "/testCifar10Data/";
  228. int number_of_classes = 10;
  229. std::shared_ptr<Dataset> ds = Cifar10(folder_path, "all", RandomSampler(false, 10));
  230. EXPECT_NE(ds, nullptr);
  231. // Create objects for the tensor ops
  232. std::shared_ptr<TensorOperation> hwc_to_chw = vision::HWC2CHW();
  233. EXPECT_NE(hwc_to_chw, nullptr);
  234. // Create a Map operation on ds
  235. ds = ds->Map({hwc_to_chw}, {"image"});
  236. EXPECT_NE(ds, nullptr);
  237. // Create a Batch operation on ds
  238. int32_t batch_size = 5;
  239. ds = ds->Batch(batch_size);
  240. EXPECT_NE(ds, nullptr);
  241. // Create objects for the tensor ops
  242. std::shared_ptr<TensorOperation> one_hot_op = transforms::OneHot(number_of_classes);
  243. EXPECT_NE(one_hot_op, nullptr);
  244. // Create a Map operation on ds
  245. ds = ds->Map({one_hot_op}, {"label"});
  246. EXPECT_NE(ds, nullptr);
  247. std::shared_ptr<TensorOperation> cutmix_batch_op =
  248. vision::CutMixBatch(mindspore::dataset::ImageBatchFormat::kNCHW, 1.0, 1.0);
  249. EXPECT_NE(cutmix_batch_op, nullptr);
  250. // Create a Map operation on ds
  251. ds = ds->Map({cutmix_batch_op}, {"image", "label"});
  252. EXPECT_NE(ds, nullptr);
  253. // Create an iterator over the result of the above dataset
  254. // This will trigger the creation of the Execution Tree and launch it.
  255. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  256. EXPECT_NE(iter, nullptr);
  257. // Iterate the dataset and get each row
  258. std::unordered_map<std::string, mindspore::MSTensor> row;
  259. iter->GetNextRow(&row);
  260. uint64_t i = 0;
  261. while (row.size() != 0) {
  262. i++;
  263. // auto image = row["image"];
  264. // auto label = row["label"];
  265. // MS_LOG(INFO) << "Tensor image shape: " << image->shape();
  266. // MS_LOG(INFO) << "Label shape: " << label->shape();
  267. // EXPECT_EQ(image->shape().AsVector().size() == 4 && batch_size == image->shape()[0] && 3 == image->shape()[1] &&
  268. // 32 == image->shape()[2] && 32 == image->shape()[3],
  269. // true);
  270. // EXPECT_EQ(label->shape().AsVector().size() == 2 && batch_size == label->shape()[0] &&
  271. // number_of_classes == label->shape()[1],
  272. // true);
  273. iter->GetNextRow(&row);
  274. }
  275. EXPECT_EQ(i, 2);
  276. // Manually terminate the pipeline
  277. iter->Stop();
  278. }
  279. TEST_F(MindDataTestPipeline, TestCutMixBatchSuccess2) {
  280. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestCutMixBatchSuccess2.";
  281. // Calling CutMixBatch on a batch of HWC images with default values of alpha and prob
  282. // Create a Cifar10 Dataset
  283. std::string folder_path = datasets_root_path_ + "/testCifar10Data/";
  284. int number_of_classes = 10;
  285. std::shared_ptr<Dataset> ds = Cifar10(folder_path, "all", RandomSampler(false, 10));
  286. EXPECT_NE(ds, nullptr);
  287. // Create a Batch operation on ds
  288. int32_t batch_size = 5;
  289. ds = ds->Batch(batch_size);
  290. EXPECT_NE(ds, nullptr);
  291. // Create objects for the tensor ops
  292. std::shared_ptr<TensorOperation> one_hot_op = transforms::OneHot(number_of_classes);
  293. EXPECT_NE(one_hot_op, nullptr);
  294. // Create a Map operation on ds
  295. ds = ds->Map({one_hot_op}, {"label"});
  296. EXPECT_NE(ds, nullptr);
  297. std::shared_ptr<TensorOperation> cutmix_batch_op = vision::CutMixBatch(mindspore::dataset::ImageBatchFormat::kNHWC);
  298. EXPECT_NE(cutmix_batch_op, nullptr);
  299. // Create a Map operation on ds
  300. ds = ds->Map({cutmix_batch_op}, {"image", "label"});
  301. EXPECT_NE(ds, nullptr);
  302. // Create an iterator over the result of the above dataset
  303. // This will trigger the creation of the Execution Tree and launch it.
  304. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  305. EXPECT_NE(iter, nullptr);
  306. // Iterate the dataset and get each row
  307. std::unordered_map<std::string, mindspore::MSTensor> row;
  308. iter->GetNextRow(&row);
  309. uint64_t i = 0;
  310. while (row.size() != 0) {
  311. i++;
  312. // auto image = row["image"];
  313. // auto label = row["label"];
  314. // MS_LOG(INFO) << "Tensor image shape: " << image->shape();
  315. // MS_LOG(INFO) << "Label shape: " << label->shape();
  316. // EXPECT_EQ(image->shape().AsVector().size() == 4 && batch_size == image->shape()[0] && 32 == image->shape()[1] &&
  317. // 32 == image->shape()[2] && 3 == image->shape()[3],
  318. // true);
  319. // EXPECT_EQ(label->shape().AsVector().size() == 2 && batch_size == label->shape()[0] &&
  320. // number_of_classes == label->shape()[1],
  321. // true);
  322. iter->GetNextRow(&row);
  323. }
  324. EXPECT_EQ(i, 2);
  325. // Manually terminate the pipeline
  326. iter->Stop();
  327. }
  328. TEST_F(MindDataTestPipeline, TestCutMixBatchFail1) {
  329. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestCutMixBatchFail1 with invalid negative alpha parameter.";
  330. // Create a Cifar10 Dataset
  331. std::string folder_path = datasets_root_path_ + "/testCifar10Data/";
  332. std::shared_ptr<Dataset> ds = Cifar10(folder_path, "all", RandomSampler(false, 10));
  333. EXPECT_NE(ds, nullptr);
  334. // Create a Batch operation on ds
  335. int32_t batch_size = 5;
  336. ds = ds->Batch(batch_size);
  337. EXPECT_NE(ds, nullptr);
  338. // Create objects for the tensor ops
  339. std::shared_ptr<TensorOperation> one_hot_op = transforms::OneHot(10);
  340. EXPECT_NE(one_hot_op, nullptr);
  341. // Create a Map operation on ds
  342. ds = ds->Map({one_hot_op}, {"label"});
  343. EXPECT_NE(ds, nullptr);
  344. std::shared_ptr<TensorOperation> cutmix_batch_op =
  345. vision::CutMixBatch(mindspore::dataset::ImageBatchFormat::kNHWC, -1, 0.5);
  346. EXPECT_EQ(cutmix_batch_op, nullptr);
  347. }
  348. TEST_F(MindDataTestPipeline, TestCutMixBatchFail2) {
  349. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestCutMixBatchFail2 with invalid negative prob parameter.";
  350. // Create a Cifar10 Dataset
  351. std::string folder_path = datasets_root_path_ + "/testCifar10Data/";
  352. std::shared_ptr<Dataset> ds = Cifar10(folder_path, "all", RandomSampler(false, 10));
  353. EXPECT_NE(ds, nullptr);
  354. // Create a Batch operation on ds
  355. int32_t batch_size = 5;
  356. ds = ds->Batch(batch_size);
  357. EXPECT_NE(ds, nullptr);
  358. // Create objects for the tensor ops
  359. std::shared_ptr<TensorOperation> one_hot_op = transforms::OneHot(10);
  360. EXPECT_NE(one_hot_op, nullptr);
  361. // Create a Map operation on ds
  362. ds = ds->Map({one_hot_op}, {"label"});
  363. EXPECT_NE(ds, nullptr);
  364. std::shared_ptr<TensorOperation> cutmix_batch_op =
  365. vision::CutMixBatch(mindspore::dataset::ImageBatchFormat::kNHWC, 1, -0.5);
  366. EXPECT_EQ(cutmix_batch_op, nullptr);
  367. }
  368. TEST_F(MindDataTestPipeline, TestCutMixBatchFail3) {
  369. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestCutMixBatchFail3 with invalid zero alpha parameter.";
  370. // Create a Cifar10 Dataset
  371. std::string folder_path = datasets_root_path_ + "/testCifar10Data/";
  372. std::shared_ptr<Dataset> ds = Cifar10(folder_path, "all", RandomSampler(false, 10));
  373. EXPECT_NE(ds, nullptr);
  374. // Create a Batch operation on ds
  375. int32_t batch_size = 5;
  376. ds = ds->Batch(batch_size);
  377. EXPECT_NE(ds, nullptr);
  378. // Create objects for the tensor ops
  379. std::shared_ptr<TensorOperation> one_hot_op = transforms::OneHot(10);
  380. EXPECT_NE(one_hot_op, nullptr);
  381. // Create a Map operation on ds
  382. ds = ds->Map({one_hot_op}, {"label"});
  383. EXPECT_NE(ds, nullptr);
  384. std::shared_ptr<TensorOperation> cutmix_batch_op =
  385. vision::CutMixBatch(mindspore::dataset::ImageBatchFormat::kNHWC, 0.0, 0.5);
  386. EXPECT_EQ(cutmix_batch_op, nullptr);
  387. }
  388. TEST_F(MindDataTestPipeline, TestCutMixBatchFail4) {
  389. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestCutMixBatchFail4 with invalid greater than 1 prob parameter.";
  390. // Create a Cifar10 Dataset
  391. std::string folder_path = datasets_root_path_ + "/testCifar10Data/";
  392. std::shared_ptr<Dataset> ds = Cifar10(folder_path, "all", RandomSampler(false, 10));
  393. EXPECT_NE(ds, nullptr);
  394. // Create a Batch operation on ds
  395. int32_t batch_size = 10;
  396. ds = ds->Batch(batch_size);
  397. EXPECT_NE(ds, nullptr);
  398. // Create objects for the tensor ops
  399. std::shared_ptr<TensorOperation> one_hot_op = transforms::OneHot(10);
  400. EXPECT_NE(one_hot_op, nullptr);
  401. // Create a Map operation on ds
  402. ds = ds->Map({one_hot_op}, {"label"});
  403. EXPECT_NE(ds, nullptr);
  404. std::shared_ptr<TensorOperation> cutmix_batch_op =
  405. vision::CutMixBatch(mindspore::dataset::ImageBatchFormat::kNHWC, 1, 1.5);
  406. EXPECT_EQ(cutmix_batch_op, nullptr);
  407. }
  408. TEST_F(MindDataTestPipeline, TestCutOutFail1) {
  409. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestCutOutFail1 with invalid parameters.";
  410. // Create object for the tensor op
  411. // Invalid negative length
  412. std::shared_ptr<TensorOperation> cutout_op = vision::CutOut(-10);
  413. EXPECT_EQ(cutout_op, nullptr);
  414. // Invalid negative number of patches
  415. cutout_op = vision::CutOut(10, -1);
  416. EXPECT_EQ(cutout_op, nullptr);
  417. }
  418. TEST_F(MindDataTestPipeline, TestCutOutFail2) {
  419. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestCutOutFail2 with invalid params, boundary cases.";
  420. // Create object for the tensor op
  421. // Invalid zero length
  422. std::shared_ptr<TensorOperation> cutout_op = vision::CutOut(0);
  423. EXPECT_EQ(cutout_op, nullptr);
  424. // Invalid zero number of patches
  425. cutout_op = vision::CutOut(10, 0);
  426. EXPECT_EQ(cutout_op, nullptr);
  427. }
  428. TEST_F(MindDataTestPipeline, TestCutOut) {
  429. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestCutOut.";
  430. // Create an ImageFolder Dataset
  431. std::string folder_path = datasets_root_path_ + "/testPK/data/";
  432. std::shared_ptr<Dataset> ds = ImageFolder(folder_path, true, RandomSampler(false, 10));
  433. EXPECT_NE(ds, nullptr);
  434. // Create a Repeat operation on ds
  435. int32_t repeat_num = 2;
  436. ds = ds->Repeat(repeat_num);
  437. EXPECT_NE(ds, nullptr);
  438. // Create objects for the tensor ops
  439. std::shared_ptr<TensorOperation> cut_out1 = vision::CutOut(30, 5);
  440. EXPECT_NE(cut_out1, nullptr);
  441. std::shared_ptr<TensorOperation> cut_out2 = vision::CutOut(30);
  442. EXPECT_NE(cut_out2, nullptr);
  443. // Create a Map operation on ds
  444. ds = ds->Map({cut_out1, cut_out2});
  445. EXPECT_NE(ds, nullptr);
  446. // Create a Batch operation on ds
  447. int32_t batch_size = 1;
  448. ds = ds->Batch(batch_size);
  449. EXPECT_NE(ds, nullptr);
  450. // Create an iterator over the result of the above dataset
  451. // This will trigger the creation of the Execution Tree and launch it.
  452. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  453. EXPECT_NE(iter, nullptr);
  454. // Iterate the dataset and get each row
  455. std::unordered_map<std::string, mindspore::MSTensor> row;
  456. iter->GetNextRow(&row);
  457. uint64_t i = 0;
  458. while (row.size() != 0) {
  459. i++;
  460. // auto image = row["image"];
  461. // MS_LOG(INFO) << "Tensor image shape: " << image->shape();
  462. iter->GetNextRow(&row);
  463. }
  464. EXPECT_EQ(i, 20);
  465. // Manually terminate the pipeline
  466. iter->Stop();
  467. }
  468. TEST_F(MindDataTestPipeline, TestDecode) {
  469. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestDecode.";
  470. // Create an ImageFolder Dataset
  471. std::string folder_path = datasets_root_path_ + "/testPK/data/";
  472. std::shared_ptr<Dataset> ds = ImageFolder(folder_path, false, RandomSampler(false, 10));
  473. EXPECT_NE(ds, nullptr);
  474. // Create a Repeat operation on ds
  475. int32_t repeat_num = 2;
  476. ds = ds->Repeat(repeat_num);
  477. EXPECT_NE(ds, nullptr);
  478. // Create objects for the tensor ops
  479. std::shared_ptr<TensorOperation> decode = vision::Decode(true);
  480. EXPECT_NE(decode, nullptr);
  481. // Create a Map operation on ds
  482. ds = ds->Map({decode});
  483. EXPECT_NE(ds, nullptr);
  484. // Create a Batch operation on ds
  485. int32_t batch_size = 1;
  486. ds = ds->Batch(batch_size);
  487. EXPECT_NE(ds, nullptr);
  488. // Create an iterator over the result of the above dataset
  489. // This will trigger the creation of the Execution Tree and launch it.
  490. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  491. EXPECT_NE(iter, nullptr);
  492. // Iterate the dataset and get each row
  493. std::unordered_map<std::string, mindspore::MSTensor> row;
  494. iter->GetNextRow(&row);
  495. uint64_t i = 0;
  496. while (row.size() != 0) {
  497. i++;
  498. // auto image = row["image"];
  499. // MS_LOG(INFO) << "Tensor image shape: " << image->shape();
  500. iter->GetNextRow(&row);
  501. }
  502. EXPECT_EQ(i, 20);
  503. // Manually terminate the pipeline
  504. iter->Stop();
  505. }
  506. TEST_F(MindDataTestPipeline, TestHwcToChw) {
  507. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestHwcToChw.";
  508. // Create an ImageFolder Dataset
  509. std::string folder_path = datasets_root_path_ + "/testPK/data/";
  510. std::shared_ptr<Dataset> ds = ImageFolder(folder_path, true, RandomSampler(false, 10));
  511. EXPECT_NE(ds, nullptr);
  512. // Create a Repeat operation on ds
  513. int32_t repeat_num = 2;
  514. ds = ds->Repeat(repeat_num);
  515. EXPECT_NE(ds, nullptr);
  516. // Create objects for the tensor ops
  517. std::shared_ptr<TensorOperation> channel_swap = vision::HWC2CHW();
  518. EXPECT_NE(channel_swap, nullptr);
  519. // Create a Map operation on ds
  520. ds = ds->Map({channel_swap});
  521. EXPECT_NE(ds, nullptr);
  522. // Create a Batch operation on ds
  523. int32_t batch_size = 1;
  524. ds = ds->Batch(batch_size);
  525. EXPECT_NE(ds, nullptr);
  526. // Create an iterator over the result of the above dataset
  527. // This will trigger the creation of the Execution Tree and launch it.
  528. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  529. EXPECT_NE(iter, nullptr);
  530. // Iterate the dataset and get each row
  531. std::unordered_map<std::string, mindspore::MSTensor> row;
  532. iter->GetNextRow(&row);
  533. uint64_t i = 0;
  534. while (row.size() != 0) {
  535. i++;
  536. // auto image = row["image"];
  537. // MS_LOG(INFO) << "Tensor image shape: " << image->shape();
  538. // check if the image is in NCHW
  539. // EXPECT_EQ(batch_size == image->shape()[0] && 3 == image->shape()[1] && 2268 == image->shape()[2] &&
  540. // 4032 == image->shape()[3],
  541. // true);
  542. iter->GetNextRow(&row);
  543. }
  544. EXPECT_EQ(i, 20);
  545. // Manually terminate the pipeline
  546. iter->Stop();
  547. }
  548. TEST_F(MindDataTestPipeline, TestInvert) {
  549. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestInvert.";
  550. // Create an ImageFolder Dataset
  551. std::string folder_path = datasets_root_path_ + "/testPK/data/";
  552. std::shared_ptr<Dataset> ds = ImageFolder(folder_path, true, RandomSampler(false, 20));
  553. EXPECT_NE(ds, nullptr);
  554. // Create objects for the tensor ops
  555. std::shared_ptr<TensorOperation> invert_op = vision::Invert();
  556. EXPECT_NE(invert_op, nullptr);
  557. // Create a Map operation on ds
  558. ds = ds->Map({invert_op});
  559. EXPECT_NE(ds, nullptr);
  560. // Create an iterator over the result of the above dataset
  561. // This will trigger the creation of the Execution Tree and launch it.
  562. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  563. EXPECT_NE(iter, nullptr);
  564. // Iterate the dataset and get each row
  565. std::unordered_map<std::string, mindspore::MSTensor> row;
  566. iter->GetNextRow(&row);
  567. uint64_t i = 0;
  568. while (row.size() != 0) {
  569. i++;
  570. // auto image = row["image"];
  571. // MS_LOG(INFO) << "Tensor image shape: " << image->shape();
  572. iter->GetNextRow(&row);
  573. }
  574. EXPECT_EQ(i, 20);
  575. // Manually terminate the pipeline
  576. iter->Stop();
  577. }
  578. TEST_F(MindDataTestPipeline, TestMixUpBatchFail1) {
  579. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestMixUpBatchFail1 with negative alpha parameter.";
  580. // Create a Cifar10 Dataset
  581. std::string folder_path = datasets_root_path_ + "/testCifar10Data/";
  582. std::shared_ptr<Dataset> ds = Cifar10(folder_path, "all", RandomSampler(false, 10));
  583. EXPECT_NE(ds, nullptr);
  584. // Create a Batch operation on ds
  585. int32_t batch_size = 5;
  586. ds = ds->Batch(batch_size);
  587. EXPECT_NE(ds, nullptr);
  588. // Create objects for the tensor ops
  589. std::shared_ptr<TensorOperation> one_hot_op = transforms::OneHot(10);
  590. EXPECT_NE(one_hot_op, nullptr);
  591. // Create a Map operation on ds
  592. ds = ds->Map({one_hot_op}, {"label"});
  593. EXPECT_NE(ds, nullptr);
  594. std::shared_ptr<TensorOperation> mixup_batch_op = vision::MixUpBatch(-1);
  595. EXPECT_EQ(mixup_batch_op, nullptr);
  596. }
  597. TEST_F(MindDataTestPipeline, TestMixUpBatchFail2) {
  598. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestMixUpBatchFail2 with zero alpha parameter.";
  599. // Create a Cifar10 Dataset
  600. std::string folder_path = datasets_root_path_ + "/testCifar10Data/";
  601. std::shared_ptr<Dataset> ds = Cifar10(folder_path, "all", RandomSampler(false, 10));
  602. EXPECT_NE(ds, nullptr);
  603. // Create a Batch operation on ds
  604. int32_t batch_size = 5;
  605. ds = ds->Batch(batch_size);
  606. EXPECT_NE(ds, nullptr);
  607. // Create objects for the tensor ops
  608. std::shared_ptr<TensorOperation> one_hot_op = transforms::OneHot(10);
  609. EXPECT_NE(one_hot_op, nullptr);
  610. // Create a Map operation on ds
  611. ds = ds->Map({one_hot_op}, {"label"});
  612. EXPECT_NE(ds, nullptr);
  613. std::shared_ptr<TensorOperation> mixup_batch_op = vision::MixUpBatch(0.0);
  614. EXPECT_EQ(mixup_batch_op, nullptr);
  615. }
  616. TEST_F(MindDataTestPipeline, TestMixUpBatchSuccess1) {
  617. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestMixUpBatchSuccess1 with explicit alpha parameter.";
  618. // Create a Cifar10 Dataset
  619. std::string folder_path = datasets_root_path_ + "/testCifar10Data/";
  620. std::shared_ptr<Dataset> ds = Cifar10(folder_path, "all", RandomSampler(false, 10));
  621. EXPECT_NE(ds, nullptr);
  622. // Create a Batch operation on ds
  623. int32_t batch_size = 5;
  624. ds = ds->Batch(batch_size);
  625. EXPECT_NE(ds, nullptr);
  626. // Create objects for the tensor ops
  627. std::shared_ptr<TensorOperation> one_hot_op = transforms::OneHot(10);
  628. EXPECT_NE(one_hot_op, nullptr);
  629. // Create a Map operation on ds
  630. ds = ds->Map({one_hot_op}, {"label"});
  631. EXPECT_NE(ds, nullptr);
  632. std::shared_ptr<TensorOperation> mixup_batch_op = vision::MixUpBatch(2.0);
  633. EXPECT_NE(mixup_batch_op, nullptr);
  634. // Create a Map operation on ds
  635. ds = ds->Map({mixup_batch_op}, {"image", "label"});
  636. EXPECT_NE(ds, nullptr);
  637. // Create an iterator over the result of the above dataset
  638. // This will trigger the creation of the Execution Tree and launch it.
  639. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  640. EXPECT_NE(iter, nullptr);
  641. // Iterate the dataset and get each row
  642. std::unordered_map<std::string, mindspore::MSTensor> row;
  643. iter->GetNextRow(&row);
  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, 2);
  652. // Manually terminate the pipeline
  653. iter->Stop();
  654. }
  655. TEST_F(MindDataTestPipeline, TestMixUpBatchSuccess2) {
  656. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestMixUpBatchSuccess1 with default alpha parameter.";
  657. // Create a Cifar10 Dataset
  658. std::string folder_path = datasets_root_path_ + "/testCifar10Data/";
  659. std::shared_ptr<Dataset> ds = Cifar10(folder_path, "all", RandomSampler(false, 10));
  660. EXPECT_NE(ds, nullptr);
  661. // Create a Batch operation on ds
  662. int32_t batch_size = 5;
  663. ds = ds->Batch(batch_size);
  664. EXPECT_NE(ds, nullptr);
  665. // Create objects for the tensor ops
  666. std::shared_ptr<TensorOperation> one_hot_op = transforms::OneHot(10);
  667. EXPECT_NE(one_hot_op, nullptr);
  668. // Create a Map operation on ds
  669. ds = ds->Map({one_hot_op}, {"label"});
  670. EXPECT_NE(ds, nullptr);
  671. std::shared_ptr<TensorOperation> mixup_batch_op = vision::MixUpBatch();
  672. EXPECT_NE(mixup_batch_op, nullptr);
  673. // Create a Map operation on ds
  674. ds = ds->Map({mixup_batch_op}, {"image", "label"});
  675. EXPECT_NE(ds, nullptr);
  676. // Create an iterator over the result of the above dataset
  677. // This will trigger the creation of the Execution Tree and launch it.
  678. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  679. EXPECT_NE(iter, nullptr);
  680. // Iterate the dataset and get each row
  681. std::unordered_map<std::string, mindspore::MSTensor> row;
  682. iter->GetNextRow(&row);
  683. uint64_t i = 0;
  684. while (row.size() != 0) {
  685. i++;
  686. // auto image = row["image"];
  687. // MS_LOG(INFO) << "Tensor image shape: " << image->shape();
  688. iter->GetNextRow(&row);
  689. }
  690. EXPECT_EQ(i, 2);
  691. // Manually terminate the pipeline
  692. iter->Stop();
  693. }
  694. TEST_F(MindDataTestPipeline, TestNormalize) {
  695. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestNormalize.";
  696. // Create an ImageFolder Dataset
  697. std::string folder_path = datasets_root_path_ + "/testPK/data/";
  698. std::shared_ptr<Dataset> ds = ImageFolder(folder_path, true, RandomSampler(false, 10));
  699. EXPECT_NE(ds, nullptr);
  700. // Create a Repeat operation on ds
  701. int32_t repeat_num = 2;
  702. ds = ds->Repeat(repeat_num);
  703. EXPECT_NE(ds, nullptr);
  704. // Create objects for the tensor ops
  705. std::shared_ptr<TensorOperation> normalize = vision::Normalize({121.0, 115.0, 0.0}, {70.0, 68.0, 71.0});
  706. EXPECT_NE(normalize, nullptr);
  707. // Create a Map operation on ds
  708. ds = ds->Map({normalize});
  709. EXPECT_NE(ds, nullptr);
  710. // Create a Batch operation on ds
  711. int32_t batch_size = 1;
  712. ds = ds->Batch(batch_size);
  713. EXPECT_NE(ds, nullptr);
  714. // Create an iterator over the result of the above dataset
  715. // This will trigger the creation of the Execution Tree and launch it.
  716. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  717. EXPECT_NE(iter, nullptr);
  718. // Iterate the dataset and get each row
  719. std::unordered_map<std::string, mindspore::MSTensor> row;
  720. iter->GetNextRow(&row);
  721. uint64_t i = 0;
  722. while (row.size() != 0) {
  723. i++;
  724. // auto image = row["image"];
  725. // MS_LOG(INFO) << "Tensor image shape: " << image->shape();
  726. iter->GetNextRow(&row);
  727. }
  728. EXPECT_EQ(i, 20);
  729. // Manually terminate the pipeline
  730. iter->Stop();
  731. }
  732. TEST_F(MindDataTestPipeline, TestNormalizeFail) {
  733. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestNormalizeFail with invalid parameters.";
  734. // std value at 0.0
  735. std::shared_ptr<TensorOperation> normalize =
  736. mindspore::dataset::vision::Normalize({121.0, 115.0, 100.0}, {0.0, 68.0, 71.0});
  737. EXPECT_EQ(normalize, nullptr);
  738. // mean out of range
  739. normalize = mindspore::dataset::vision::Normalize({121.0, 0.0, 100.0}, {256.0, 68.0, 71.0});
  740. EXPECT_EQ(normalize, nullptr);
  741. // mean out of range
  742. normalize = mindspore::dataset::vision::Normalize({256.0, 0.0, 100.0}, {70.0, 68.0, 71.0});
  743. EXPECT_EQ(normalize, nullptr);
  744. // mean out of range
  745. normalize = mindspore::dataset::vision::Normalize({-1.0, 0.0, 100.0}, {70.0, 68.0, 71.0});
  746. EXPECT_EQ(normalize, nullptr);
  747. // normalize with 2 values (not 3 values) for mean
  748. normalize = mindspore::dataset::vision::Normalize({121.0, 115.0}, {70.0, 68.0, 71.0});
  749. EXPECT_EQ(normalize, nullptr);
  750. // normalize with 2 values (not 3 values) for standard deviation
  751. normalize = mindspore::dataset::vision::Normalize({121.0, 115.0, 100.0}, {68.0, 71.0});
  752. EXPECT_EQ(normalize, nullptr);
  753. }
  754. TEST_F(MindDataTestPipeline, TestNormalizePad) {
  755. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestNormalizePad.";
  756. // Create an ImageFolder Dataset
  757. std::string folder_path = datasets_root_path_ + "/testPK/data/";
  758. std::shared_ptr<Dataset> ds = ImageFolder(folder_path, true, RandomSampler(false, 10));
  759. EXPECT_NE(ds, nullptr);
  760. // Create a Repeat operation on ds
  761. int32_t repeat_num = 2;
  762. ds = ds->Repeat(repeat_num);
  763. EXPECT_NE(ds, nullptr);
  764. // Create objects for the tensor ops
  765. std::shared_ptr<TensorOperation> normalizepad =
  766. vision::NormalizePad({121.0, 115.0, 100.0}, {70.0, 68.0, 71.0}, "float32");
  767. EXPECT_NE(normalizepad, nullptr);
  768. // Create a Map operation on ds
  769. ds = ds->Map({normalizepad});
  770. EXPECT_NE(ds, nullptr);
  771. // Create an iterator over the result of the above dataset
  772. // This will trigger the creation of the Execution Tree and launch it.
  773. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  774. EXPECT_NE(iter, nullptr);
  775. // Iterate the dataset and get each row
  776. std::unordered_map<std::string, mindspore::MSTensor> row;
  777. iter->GetNextRow(&row);
  778. uint64_t i = 0;
  779. while (row.size() != 0) {
  780. i++;
  781. // auto image = row["image"];
  782. // EXPECT_EQ(image->shape()[2], 4);
  783. // MS_LOG(INFO) << "Tensor image shape: " << image->shape();
  784. iter->GetNextRow(&row);
  785. }
  786. EXPECT_EQ(i, 20);
  787. // Manually terminate the pipeline
  788. iter->Stop();
  789. }
  790. TEST_F(MindDataTestPipeline, TestNormalizePadFail) {
  791. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestNormalizePadFail with invalid parameters.";
  792. // std value at 0.0
  793. std::shared_ptr<TensorOperation> normalizepad =
  794. mindspore::dataset::vision::NormalizePad({121.0, 115.0, 100.0}, {0.0, 68.0, 71.0});
  795. EXPECT_EQ(normalizepad, nullptr);
  796. // normalizepad with 2 values (not 3 values) for mean
  797. normalizepad = mindspore::dataset::vision::NormalizePad({121.0, 115.0}, {70.0, 68.0, 71.0});
  798. EXPECT_EQ(normalizepad, nullptr);
  799. // normalizepad with 2 values (not 3 values) for standard deviation
  800. normalizepad = mindspore::dataset::vision::NormalizePad({121.0, 115.0, 100.0}, {68.0, 71.0});
  801. EXPECT_EQ(normalizepad, nullptr);
  802. // normalizepad with invalid dtype
  803. normalizepad = mindspore::dataset::vision::NormalizePad({121.0, 115.0, 100.0}, {68.0, 71.0, 71.0}, "123");
  804. EXPECT_EQ(normalizepad, nullptr);
  805. }
  806. TEST_F(MindDataTestPipeline, TestPad) {
  807. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestPad.";
  808. // Create an ImageFolder Dataset
  809. std::string folder_path = datasets_root_path_ + "/testPK/data/";
  810. std::shared_ptr<Dataset> ds = ImageFolder(folder_path, true, RandomSampler(false, 10));
  811. EXPECT_NE(ds, nullptr);
  812. // Create a Repeat operation on ds
  813. int32_t repeat_num = 2;
  814. ds = ds->Repeat(repeat_num);
  815. EXPECT_NE(ds, nullptr);
  816. // Create objects for the tensor ops
  817. std::shared_ptr<TensorOperation> pad_op1 = vision::Pad({1, 2, 3, 4}, {0}, BorderType::kSymmetric);
  818. EXPECT_NE(pad_op1, nullptr);
  819. std::shared_ptr<TensorOperation> pad_op2 = vision::Pad({1}, {1, 1, 1}, BorderType::kEdge);
  820. EXPECT_NE(pad_op2, nullptr);
  821. std::shared_ptr<TensorOperation> pad_op3 = vision::Pad({1, 4});
  822. EXPECT_NE(pad_op3, nullptr);
  823. // Create a Map operation on ds
  824. ds = ds->Map({pad_op1, pad_op2, pad_op3});
  825. EXPECT_NE(ds, nullptr);
  826. // Create a Batch operation on ds
  827. int32_t batch_size = 1;
  828. ds = ds->Batch(batch_size);
  829. EXPECT_NE(ds, nullptr);
  830. // Create an iterator over the result of the above dataset
  831. // This will trigger the creation of the Execution Tree and launch it.
  832. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  833. EXPECT_NE(iter, nullptr);
  834. // Iterate the dataset and get each row
  835. std::unordered_map<std::string, mindspore::MSTensor> row;
  836. iter->GetNextRow(&row);
  837. uint64_t i = 0;
  838. while (row.size() != 0) {
  839. i++;
  840. // auto image = row["image"];
  841. // MS_LOG(INFO) << "Tensor image shape: " << image->shape();
  842. iter->GetNextRow(&row);
  843. }
  844. EXPECT_EQ(i, 20);
  845. // Manually terminate the pipeline
  846. iter->Stop();
  847. }
  848. TEST_F(MindDataTestPipeline, TestRandomAffineFail) {
  849. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestRandomAffineFail with invalid parameters.";
  850. // Create objects for the tensor ops
  851. std::shared_ptr<TensorOperation> affine = vision::RandomAffine({0.0, 0.0}, {});
  852. EXPECT_EQ(affine, nullptr);
  853. // Invalid number of values for translate
  854. affine = vision::RandomAffine({0.0, 0.0}, {1, 1, 1, 1, 1});
  855. EXPECT_EQ(affine, nullptr);
  856. // Invalid number of values for shear
  857. affine = vision::RandomAffine({30.0, 30.0}, {0.0, 0.0}, {2.0, 2.0}, {10.0});
  858. EXPECT_EQ(affine, nullptr);
  859. }
  860. TEST_F(MindDataTestPipeline, TestRandomAffineSuccess1) {
  861. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestRandomAffineSuccess1 with non-default parameters.";
  862. // Create an ImageFolder Dataset
  863. std::string folder_path = datasets_root_path_ + "/testPK/data/";
  864. std::shared_ptr<Dataset> ds = ImageFolder(folder_path, true, RandomSampler(false, 10));
  865. EXPECT_NE(ds, nullptr);
  866. // Create a Repeat operation on ds
  867. int32_t repeat_num = 2;
  868. ds = ds->Repeat(repeat_num);
  869. EXPECT_NE(ds, nullptr);
  870. // Create objects for the tensor ops
  871. std::shared_ptr<TensorOperation> affine =
  872. vision::RandomAffine({30.0, 30.0}, {-1.0, 1.0, -1.0, 1.0}, {2.0, 2.0}, {10.0, 10.0, 20.0, 20.0});
  873. EXPECT_NE(affine, nullptr);
  874. // Create a Map operation on ds
  875. ds = ds->Map({affine});
  876. EXPECT_NE(ds, nullptr);
  877. // Create a Batch operation on ds
  878. int32_t batch_size = 1;
  879. ds = ds->Batch(batch_size);
  880. EXPECT_NE(ds, nullptr);
  881. // Create an iterator over the result of the above dataset
  882. // This will trigger the creation of the Execution Tree and launch it.
  883. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  884. EXPECT_NE(iter, nullptr);
  885. // Iterate the dataset and get each row
  886. std::unordered_map<std::string, mindspore::MSTensor> row;
  887. iter->GetNextRow(&row);
  888. uint64_t i = 0;
  889. while (row.size() != 0) {
  890. i++;
  891. // auto image = row["image"];
  892. // MS_LOG(INFO) << "Tensor image shape: " << image->shape();
  893. iter->GetNextRow(&row);
  894. }
  895. EXPECT_EQ(i, 20);
  896. // Manually terminate the pipeline
  897. iter->Stop();
  898. }
  899. TEST_F(MindDataTestPipeline, TestRandomAffineSuccess2) {
  900. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestRandomAffineSuccess2 with default parameters.";
  901. // Create an ImageFolder Dataset
  902. std::string folder_path = datasets_root_path_ + "/testPK/data/";
  903. std::shared_ptr<Dataset> ds = ImageFolder(folder_path, true, RandomSampler(false, 10));
  904. EXPECT_NE(ds, nullptr);
  905. // Create a Repeat operation on ds
  906. int32_t repeat_num = 2;
  907. ds = ds->Repeat(repeat_num);
  908. EXPECT_NE(ds, nullptr);
  909. // Create objects for the tensor ops
  910. std::shared_ptr<TensorOperation> affine = vision::RandomAffine({0.0, 0.0});
  911. EXPECT_NE(affine, nullptr);
  912. // Create a Map operation on ds
  913. ds = ds->Map({affine});
  914. EXPECT_NE(ds, nullptr);
  915. // Create a Batch operation on ds
  916. int32_t batch_size = 1;
  917. ds = ds->Batch(batch_size);
  918. EXPECT_NE(ds, nullptr);
  919. // Create an iterator over the result of the above dataset
  920. // This will trigger the creation of the Execution Tree and launch it.
  921. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  922. EXPECT_NE(iter, nullptr);
  923. // Iterate the dataset and get each row
  924. std::unordered_map<std::string, mindspore::MSTensor> row;
  925. iter->GetNextRow(&row);
  926. uint64_t i = 0;
  927. while (row.size() != 0) {
  928. i++;
  929. // auto image = row["image"];
  930. // MS_LOG(INFO) << "Tensor image shape: " << image->shape();
  931. iter->GetNextRow(&row);
  932. }
  933. EXPECT_EQ(i, 20);
  934. // Manually terminate the pipeline
  935. iter->Stop();
  936. }
  937. TEST_F(MindDataTestPipeline, TestRandomColor) {
  938. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestRandomColor with non-default parameters.";
  939. // Create an ImageFolder Dataset
  940. std::string folder_path = datasets_root_path_ + "/testPK/data/";
  941. std::shared_ptr<Dataset> ds = ImageFolder(folder_path, true, RandomSampler(false, 10));
  942. EXPECT_NE(ds, nullptr);
  943. // Create a Repeat operation on ds
  944. int32_t repeat_num = 2;
  945. ds = ds->Repeat(repeat_num);
  946. EXPECT_NE(ds, nullptr);
  947. // Create objects for the tensor ops
  948. // Valid case: Set lower bound and upper bound to be the same value zero
  949. std::shared_ptr<TensorOperation> random_color_op_1 = vision::RandomColor(0.0, 0.0);
  950. EXPECT_NE(random_color_op_1, nullptr);
  951. // Failure case: Set invalid lower bound greater than upper bound
  952. std::shared_ptr<TensorOperation> random_color_op_2 = vision::RandomColor(1.0, 0.1);
  953. EXPECT_EQ(random_color_op_2, nullptr);
  954. // Valid case: Set lower bound as zero and less than upper bound
  955. std::shared_ptr<TensorOperation> random_color_op_3 = vision::RandomColor(0.0, 1.1);
  956. EXPECT_NE(random_color_op_3, nullptr);
  957. // Failure case: Set invalid negative lower bound
  958. std::shared_ptr<TensorOperation> random_color_op_4 = vision::RandomColor(-0.5, 0.5);
  959. EXPECT_EQ(random_color_op_4, nullptr);
  960. // Create a Map operation on ds
  961. ds = ds->Map({random_color_op_1, random_color_op_3});
  962. EXPECT_NE(ds, nullptr);
  963. // Create a Batch operation on ds
  964. int32_t batch_size = 1;
  965. ds = ds->Batch(batch_size);
  966. EXPECT_NE(ds, nullptr);
  967. // Create an iterator over the result of the above dataset
  968. // This will trigger the creation of the Execution Tree and launch it.
  969. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  970. EXPECT_NE(iter, nullptr);
  971. // Iterate the dataset and get each row
  972. std::unordered_map<std::string, mindspore::MSTensor> row;
  973. iter->GetNextRow(&row);
  974. uint64_t i = 0;
  975. while (row.size() != 0) {
  976. i++;
  977. // auto image = row["image"];
  978. // MS_LOG(INFO) << "Tensor image shape: " << image->shape();
  979. iter->GetNextRow(&row);
  980. }
  981. EXPECT_EQ(i, 20);
  982. // Manually terminate the pipeline
  983. iter->Stop();
  984. }
  985. TEST_F(MindDataTestPipeline, TestRandomColorAdjust) {
  986. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestRandomColorAdjust.";
  987. // Create an ImageFolder Dataset
  988. std::string folder_path = datasets_root_path_ + "/testPK/data/";
  989. std::shared_ptr<Dataset> ds = ImageFolder(folder_path, true, RandomSampler(false, 10));
  990. EXPECT_NE(ds, nullptr);
  991. // Create a Repeat operation on ds
  992. int32_t repeat_num = 2;
  993. ds = ds->Repeat(repeat_num);
  994. EXPECT_NE(ds, nullptr);
  995. // Create objects for the tensor ops
  996. // Use single value for vectors
  997. std::shared_ptr<TensorOperation> random_color_adjust1 = vision::RandomColorAdjust({1.0}, {0.0}, {0.5}, {0.5});
  998. EXPECT_NE(random_color_adjust1, nullptr);
  999. // Use same 2 values for vectors
  1000. std::shared_ptr<TensorOperation> random_color_adjust2 =
  1001. vision::RandomColorAdjust({1.0, 1.0}, {0.0, 0.0}, {0.5, 0.5}, {0.5, 0.5});
  1002. EXPECT_NE(random_color_adjust2, nullptr);
  1003. // Use different 2 value for vectors
  1004. std::shared_ptr<TensorOperation> random_color_adjust3 =
  1005. vision::RandomColorAdjust({0.5, 1.0}, {0.0, 0.5}, {0.25, 0.5}, {0.25, 0.5});
  1006. EXPECT_NE(random_color_adjust3, nullptr);
  1007. // Use default input values
  1008. std::shared_ptr<TensorOperation> random_color_adjust4 = vision::RandomColorAdjust();
  1009. EXPECT_NE(random_color_adjust4, nullptr);
  1010. // Use subset of explicitly set parameters
  1011. std::shared_ptr<TensorOperation> random_color_adjust5 = vision::RandomColorAdjust({0.0, 0.5}, {0.25});
  1012. EXPECT_NE(random_color_adjust5, nullptr);
  1013. // Create a Map operation on ds
  1014. ds = ds->Map(
  1015. {random_color_adjust1, random_color_adjust2, random_color_adjust3, random_color_adjust4, random_color_adjust5});
  1016. EXPECT_NE(ds, nullptr);
  1017. // Create a Batch operation on ds
  1018. int32_t batch_size = 1;
  1019. ds = ds->Batch(batch_size);
  1020. EXPECT_NE(ds, nullptr);
  1021. // Create an iterator over the result of the above dataset
  1022. // This will trigger the creation of the Execution Tree and launch it.
  1023. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  1024. EXPECT_NE(iter, nullptr);
  1025. // Iterate the dataset and get each row
  1026. std::unordered_map<std::string, mindspore::MSTensor> row;
  1027. iter->GetNextRow(&row);
  1028. uint64_t i = 0;
  1029. while (row.size() != 0) {
  1030. i++;
  1031. // auto image = row["image"];
  1032. // MS_LOG(INFO) << "Tensor image shape: " << image->shape();
  1033. iter->GetNextRow(&row);
  1034. }
  1035. EXPECT_EQ(i, 20);
  1036. // Manually terminate the pipeline
  1037. iter->Stop();
  1038. }
  1039. TEST_F(MindDataTestPipeline, TestRandomColorAdjustFail) {
  1040. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestRandomColorAdjustFail.";
  1041. // brightness out of range
  1042. std::shared_ptr<TensorOperation> random_color_adjust1 = vision::RandomColorAdjust({-1.0});
  1043. EXPECT_EQ(random_color_adjust1, nullptr);
  1044. // contrast out of range
  1045. std::shared_ptr<TensorOperation> random_color_adjust2 = vision::RandomColorAdjust({1.0}, {-0.1});
  1046. EXPECT_EQ(random_color_adjust2, nullptr);
  1047. // saturation out of range
  1048. std::shared_ptr<TensorOperation> random_color_adjust3 = vision::RandomColorAdjust({0.0}, {0.0}, {-0.2});
  1049. EXPECT_EQ(random_color_adjust3, nullptr);
  1050. // hue out of range
  1051. std::shared_ptr<TensorOperation> random_color_adjust4 = vision::RandomColorAdjust({0.0}, {0.0}, {0.0}, {-0.6});
  1052. EXPECT_EQ(random_color_adjust4, nullptr);
  1053. std::shared_ptr<TensorOperation> random_color_adjust5 = vision::RandomColorAdjust({0.0}, {0.0}, {0.0}, {-0.5, 0.6});
  1054. EXPECT_EQ(random_color_adjust5, nullptr);
  1055. std::shared_ptr<TensorOperation> random_color_adjust6 = vision::RandomColorAdjust({0.0}, {0.0}, {0.0}, {0.51});
  1056. EXPECT_EQ(random_color_adjust6, nullptr);
  1057. }
  1058. TEST_F(MindDataTestPipeline, TestRandomCropSuccess) {
  1059. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestRandomCropSuccess.";
  1060. // Create an VOC Dataset
  1061. std::string folder_path = datasets_root_path_ + "/testVOC2012_2";
  1062. std::shared_ptr<Dataset> ds = VOC(folder_path, "Detection", "train", {}, true, SequentialSampler(0, 10));
  1063. EXPECT_NE(ds, nullptr);
  1064. // Create objects for the tensor ops
  1065. // Testing siez of size vector is 1
  1066. std::shared_ptr<TensorOperation> random_crop = vision::RandomCrop({20});
  1067. EXPECT_NE(random_crop, nullptr);
  1068. // Testing siez of size vector is 2
  1069. std::shared_ptr<TensorOperation> random_crop1 = vision::RandomCrop({20, 20});
  1070. EXPECT_NE(random_crop1, nullptr);
  1071. // Testing siez of paddiing vector is 1
  1072. std::shared_ptr<TensorOperation> random_crop2 = vision::RandomCrop({20, 20}, {10});
  1073. EXPECT_NE(random_crop2, nullptr);
  1074. // Testing siez of paddiing vector is 2
  1075. std::shared_ptr<TensorOperation> random_crop3 = vision::RandomCrop({20, 20}, {10, 20});
  1076. EXPECT_NE(random_crop3, nullptr);
  1077. // Testing siez of paddiing vector is 2
  1078. std::shared_ptr<TensorOperation> random_crop4 = vision::RandomCrop({20, 20}, {10, 10, 10, 10});
  1079. EXPECT_NE(random_crop4, nullptr);
  1080. // Testing siez of fill_value vector is 1
  1081. std::shared_ptr<TensorOperation> random_crop5 = vision::RandomCrop({20, 20}, {10, 10, 10, 10}, false, {5});
  1082. EXPECT_NE(random_crop5, nullptr);
  1083. // Testing siez of fill_value vector is 3
  1084. std::shared_ptr<TensorOperation> random_crop6 = vision::RandomCrop({20, 20}, {10, 10, 10, 10}, false, {4, 4, 4});
  1085. EXPECT_NE(random_crop6, nullptr);
  1086. // Create a Map operation on ds
  1087. ds = ds->Map({random_crop, random_crop1, random_crop2, random_crop3, random_crop4, random_crop5, random_crop6});
  1088. EXPECT_NE(ds, nullptr);
  1089. // Create an iterator over the result of the above dataset
  1090. // This will trigger the creation of the Execution Tree and launch it.
  1091. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  1092. EXPECT_NE(iter, nullptr);
  1093. // Iterate the dataset and get each row
  1094. std::unordered_map<std::string, mindspore::MSTensor> row;
  1095. iter->GetNextRow(&row);
  1096. uint64_t i = 0;
  1097. while (row.size() != 0) {
  1098. i++;
  1099. // auto image = row["image"];
  1100. // MS_LOG(INFO) << "Tensor image shape: " << image->shape();
  1101. iter->GetNextRow(&row);
  1102. }
  1103. EXPECT_EQ(i, 10);
  1104. // Manually terminate the pipeline
  1105. iter->Stop();
  1106. }
  1107. TEST_F(MindDataTestPipeline, TestRandomCropFail) {
  1108. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestRandomCropFail with invalid parameters.";
  1109. // Create an VOC Dataset
  1110. std::string folder_path = datasets_root_path_ + "/testVOC2012_2";
  1111. std::shared_ptr<Dataset> ds = VOC(folder_path, "Detection", "train", {}, true, SequentialSampler(0, 3));
  1112. EXPECT_NE(ds, nullptr);
  1113. // Create objects for the tensor ops
  1114. // Testing the size parameter is negative.
  1115. std::shared_ptr<TensorOperation> random_crop = vision::RandomCrop({-28, 28});
  1116. EXPECT_EQ(random_crop, nullptr);
  1117. // Testing the size parameter is None.
  1118. std::shared_ptr<TensorOperation> random_crop1 = vision::RandomCrop({});
  1119. EXPECT_EQ(random_crop1, nullptr);
  1120. // Testing the size of size vector is 3.
  1121. std::shared_ptr<TensorOperation> random_crop2 = vision::RandomCrop({28, 28, 28});
  1122. EXPECT_EQ(random_crop2, nullptr);
  1123. // Testing the padding parameter is negative.
  1124. std::shared_ptr<TensorOperation> random_crop3 = vision::RandomCrop({28, 28}, {-5});
  1125. EXPECT_EQ(random_crop3, nullptr);
  1126. // Testing the size of padding vector is empty.
  1127. std::shared_ptr<TensorOperation> random_crop4 = vision::RandomCrop({28, 28}, {});
  1128. EXPECT_EQ(random_crop4, nullptr);
  1129. // Testing the size of padding vector is 3.
  1130. std::shared_ptr<TensorOperation> random_crop5 = vision::RandomCrop({28, 28}, {5, 5, 5});
  1131. EXPECT_EQ(random_crop5, nullptr);
  1132. // Testing the size of padding vector is 5.
  1133. std::shared_ptr<TensorOperation> random_crop6 = vision::RandomCrop({28, 28}, {5, 5, 5, 5, 5});
  1134. EXPECT_EQ(random_crop6, nullptr);
  1135. // Testing the size of fill_value vector is empty.
  1136. std::shared_ptr<TensorOperation> random_crop7 = vision::RandomCrop({28, 28}, {0, 0, 0, 0}, false, {});
  1137. EXPECT_EQ(random_crop7, nullptr);
  1138. // Testing the size of fill_value vector is 2.
  1139. std::shared_ptr<TensorOperation> random_crop8 = vision::RandomCrop({28, 28}, {0, 0, 0, 0}, false, {0, 0});
  1140. EXPECT_EQ(random_crop8, nullptr);
  1141. // Testing the size of fill_value vector is 4.
  1142. std::shared_ptr<TensorOperation> random_crop9 = vision::RandomCrop({28, 28}, {0, 0, 0, 0}, false, {0, 0, 0, 0});
  1143. EXPECT_EQ(random_crop9, nullptr);
  1144. }
  1145. TEST_F(MindDataTestPipeline, TestRandomCropWithBboxSuccess) {
  1146. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestRandomCropWithBboxSuccess.";
  1147. // Create an VOC Dataset
  1148. std::string folder_path = datasets_root_path_ + "/testVOC2012_2";
  1149. std::shared_ptr<Dataset> ds = VOC(folder_path, "Detection", "train", {}, true, SequentialSampler(0, 3));
  1150. EXPECT_NE(ds, nullptr);
  1151. // Create objects for the tensor ops
  1152. std::shared_ptr<TensorOperation> random_crop = mindspore::dataset::vision::RandomCropWithBBox({128, 128});
  1153. EXPECT_NE(random_crop, nullptr);
  1154. // Create a Map operation on ds
  1155. ds = ds->Map({random_crop}, {"image", "bbox"}, {"image", "bbox"}, {"image", "bbox"});
  1156. EXPECT_NE(ds, nullptr);
  1157. // Create an iterator over the result of the above dataset
  1158. // This will trigger the creation of the Execution Tree and launch it.
  1159. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  1160. EXPECT_NE(iter, nullptr);
  1161. // Iterate the dataset and get each row
  1162. std::unordered_map<std::string, mindspore::MSTensor> row;
  1163. iter->GetNextRow(&row);
  1164. uint64_t i = 0;
  1165. while (row.size() != 0) {
  1166. i++;
  1167. // auto image = row["image"];
  1168. // MS_LOG(INFO) << "Tensor image shape: " << image->shape();
  1169. // EXPECT_EQ(image->shape()[0], 128);
  1170. // EXPECT_EQ(image->shape()[1], 128);
  1171. iter->GetNextRow(&row);
  1172. }
  1173. EXPECT_EQ(i, 3);
  1174. // Manually terminate the pipeline
  1175. iter->Stop();
  1176. }
  1177. TEST_F(MindDataTestPipeline, TestRandomCropWithBboxFail) {
  1178. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestRandomCropWithBboxFail with invalid parameters.";
  1179. // Create an VOC Dataset
  1180. std::string folder_path = datasets_root_path_ + "/testVOC2012_2";
  1181. std::shared_ptr<Dataset> ds = VOC(folder_path, "Detection", "train", {}, true, SequentialSampler(0, 3));
  1182. EXPECT_NE(ds, nullptr);
  1183. // Create objects for the tensor ops
  1184. // The size parameter is negative.
  1185. std::shared_ptr<TensorOperation> random_crop = vision::RandomCropWithBBox({-10});
  1186. EXPECT_EQ(random_crop, nullptr);
  1187. // The parameter in the padding vector is negative.
  1188. std::shared_ptr<TensorOperation> random_crop1 = vision::RandomCropWithBBox({10, 10}, {-2, 2, 2, 2});
  1189. EXPECT_EQ(random_crop1, nullptr);
  1190. // The size container is empty.
  1191. std::shared_ptr<TensorOperation> random_crop2 = vision::RandomCropWithBBox({});
  1192. EXPECT_EQ(random_crop2, nullptr);
  1193. // The size of the size container is too large.
  1194. std::shared_ptr<TensorOperation> random_crop3 = vision::RandomCropWithBBox({10, 10, 10});
  1195. EXPECT_EQ(random_crop3, nullptr);
  1196. // The padding container is empty.
  1197. std::shared_ptr<TensorOperation> random_crop4 = vision::RandomCropWithBBox({10, 10}, {});
  1198. EXPECT_EQ(random_crop4, nullptr);
  1199. // The size of the padding container is too large.
  1200. std::shared_ptr<TensorOperation> random_crop5 = vision::RandomCropWithBBox({10, 10}, {5, 5, 5, 5, 5});
  1201. EXPECT_EQ(random_crop5, nullptr);
  1202. // The fill_value container is empty.
  1203. std::shared_ptr<TensorOperation> random_crop6 = vision::RandomCropWithBBox({10, 10}, {5, 5, 5, 5}, false, {});
  1204. EXPECT_EQ(random_crop6, nullptr);
  1205. // The size of the fill_value container is too large.
  1206. std::shared_ptr<TensorOperation> random_crop7 =
  1207. vision::RandomCropWithBBox({10, 10}, {5, 5, 5, 5}, false, {3, 3, 3, 3});
  1208. EXPECT_EQ(random_crop7, nullptr);
  1209. }
  1210. TEST_F(MindDataTestPipeline, TestRandomHorizontalFlipFail) {
  1211. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestRandomHorizontalFlipFail with invalid parameters.";
  1212. // Create object for the tensor op
  1213. // Invalid negative input
  1214. std::shared_ptr<TensorOperation> random_horizontal_flip_op = vision::RandomHorizontalFlip(-0.5);
  1215. EXPECT_EQ(random_horizontal_flip_op, nullptr);
  1216. // Invalid >1 input
  1217. random_horizontal_flip_op = vision::RandomHorizontalFlip(2);
  1218. EXPECT_EQ(random_horizontal_flip_op, nullptr);
  1219. }
  1220. TEST_F(MindDataTestPipeline, TestRandomHorizontalFlipWithBBoxSuccess) {
  1221. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestRandomHorizontalFlipWithBBoxSuccess.";
  1222. // Create an VOC Dataset
  1223. std::string folder_path = datasets_root_path_ + "/testVOC2012_2";
  1224. std::shared_ptr<Dataset> ds = VOC(folder_path, "Detection", "train", {}, true, SequentialSampler(0, 3));
  1225. EXPECT_NE(ds, nullptr);
  1226. // Create objects for the tensor ops
  1227. std::shared_ptr<TensorOperation> random_horizontal_flip_op = vision::RandomHorizontalFlipWithBBox(0.5);
  1228. EXPECT_NE(random_horizontal_flip_op, nullptr);
  1229. // Create a Map operation on ds
  1230. ds = ds->Map({random_horizontal_flip_op}, {"image", "bbox"}, {"image", "bbox"}, {"image", "bbox"});
  1231. EXPECT_NE(ds, nullptr);
  1232. // Create an iterator over the result of the above dataset
  1233. // This will trigger the creation of the Execution Tree and launch it.
  1234. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  1235. EXPECT_NE(iter, nullptr);
  1236. // Iterate the dataset and get each row
  1237. std::unordered_map<std::string, mindspore::MSTensor> row;
  1238. iter->GetNextRow(&row);
  1239. uint64_t i = 0;
  1240. while (row.size() != 0) {
  1241. i++;
  1242. // auto image = row["image"];
  1243. // MS_LOG(INFO) << "Tensor image shape: " << image->shape();
  1244. iter->GetNextRow(&row);
  1245. }
  1246. EXPECT_EQ(i, 3);
  1247. // Manually terminate the pipeline
  1248. iter->Stop();
  1249. }
  1250. TEST_F(MindDataTestPipeline, TestRandomHorizontalFlipWithBBoxFail) {
  1251. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestRandomHorizontalFlipWithBBoxFail with invalid parameters.";
  1252. // Create an VOC Dataset
  1253. std::string folder_path = datasets_root_path_ + "/testVOC2012_2";
  1254. std::shared_ptr<Dataset> ds = VOC(folder_path, "Detection", "train", {}, true, SequentialSampler(0, 3));
  1255. EXPECT_NE(ds, nullptr);
  1256. // Create objects for the tensor ops
  1257. // Incorrect prob parameter.
  1258. std::shared_ptr<TensorOperation> random_horizontal_flip_op = vision::RandomHorizontalFlipWithBBox(-1.0);
  1259. EXPECT_EQ(random_horizontal_flip_op, nullptr);
  1260. // Incorrect prob parameter.
  1261. std::shared_ptr<TensorOperation> random_horizontal_flip_op1 = vision::RandomHorizontalFlipWithBBox(2.0);
  1262. EXPECT_EQ(random_horizontal_flip_op1, nullptr);
  1263. }
  1264. TEST_F(MindDataTestPipeline, TestRandomHorizontalAndVerticalFlip) {
  1265. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestRandomHorizontalAndVerticalFlip for horizontal and vertical flips.";
  1266. // Create an ImageFolder Dataset
  1267. std::string folder_path = datasets_root_path_ + "/testPK/data/";
  1268. std::shared_ptr<Dataset> ds = ImageFolder(folder_path, true, RandomSampler(false, 10));
  1269. EXPECT_NE(ds, nullptr);
  1270. // Create a Repeat operation on ds
  1271. int32_t repeat_num = 2;
  1272. ds = ds->Repeat(repeat_num);
  1273. EXPECT_NE(ds, nullptr);
  1274. // Create objects for the tensor ops
  1275. std::shared_ptr<TensorOperation> random_vertical_flip_op = vision::RandomVerticalFlip(0.75);
  1276. EXPECT_NE(random_vertical_flip_op, nullptr);
  1277. std::shared_ptr<TensorOperation> random_horizontal_flip_op = vision::RandomHorizontalFlip(0.5);
  1278. EXPECT_NE(random_horizontal_flip_op, nullptr);
  1279. // Create a Map operation on ds
  1280. ds = ds->Map({random_vertical_flip_op, random_horizontal_flip_op});
  1281. EXPECT_NE(ds, nullptr);
  1282. // Create a Batch operation on ds
  1283. int32_t batch_size = 1;
  1284. ds = ds->Batch(batch_size);
  1285. EXPECT_NE(ds, nullptr);
  1286. // Create an iterator over the result of the above dataset
  1287. // This will trigger the creation of the Execution Tree and launch it.
  1288. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  1289. EXPECT_NE(iter, nullptr);
  1290. // Iterate the dataset and get each row
  1291. std::unordered_map<std::string, mindspore::MSTensor> row;
  1292. iter->GetNextRow(&row);
  1293. uint64_t i = 0;
  1294. while (row.size() != 0) {
  1295. i++;
  1296. // auto image = row["image"];
  1297. // MS_LOG(INFO) << "Tensor image shape: " << image->shape();
  1298. iter->GetNextRow(&row);
  1299. }
  1300. EXPECT_EQ(i, 20);
  1301. // Manually terminate the pipeline
  1302. iter->Stop();
  1303. }
  1304. TEST_F(MindDataTestPipeline, TestRandomPosterizeFail) {
  1305. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestRandomPosterizeFail with invalid parameters.";
  1306. // Create objects for the tensor ops
  1307. // Invalid max > 8
  1308. std::shared_ptr<TensorOperation> posterize = vision::RandomPosterize({1, 9});
  1309. EXPECT_EQ(posterize, nullptr);
  1310. // Invalid min < 1
  1311. posterize = vision::RandomPosterize({0, 8});
  1312. EXPECT_EQ(posterize, nullptr);
  1313. // min > max
  1314. posterize = vision::RandomPosterize({8, 1});
  1315. EXPECT_EQ(posterize, nullptr);
  1316. // empty
  1317. posterize = vision::RandomPosterize({});
  1318. EXPECT_EQ(posterize, nullptr);
  1319. }
  1320. TEST_F(MindDataTestPipeline, TestRandomPosterizeSuccess1) {
  1321. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestRandomPosterizeSuccess1 with non-default parameters.";
  1322. // Create an ImageFolder Dataset
  1323. std::string folder_path = datasets_root_path_ + "/testPK/data/";
  1324. std::shared_ptr<Dataset> ds = ImageFolder(folder_path, true, RandomSampler(false, 10));
  1325. EXPECT_NE(ds, nullptr);
  1326. // Create a Repeat operation on ds
  1327. int32_t repeat_num = 2;
  1328. ds = ds->Repeat(repeat_num);
  1329. EXPECT_NE(ds, nullptr);
  1330. // Create objects for the tensor ops
  1331. std::shared_ptr<TensorOperation> posterize = vision::RandomPosterize({1, 4});
  1332. EXPECT_NE(posterize, nullptr);
  1333. // Create a Map operation on ds
  1334. ds = ds->Map({posterize});
  1335. EXPECT_NE(ds, nullptr);
  1336. // Create a Batch operation on ds
  1337. int32_t batch_size = 1;
  1338. ds = ds->Batch(batch_size);
  1339. EXPECT_NE(ds, nullptr);
  1340. // Create an iterator over the result of the above dataset
  1341. // This will trigger the creation of the Execution Tree and launch it.
  1342. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  1343. EXPECT_NE(iter, nullptr);
  1344. // Iterate the dataset and get each row
  1345. std::unordered_map<std::string, mindspore::MSTensor> row;
  1346. iter->GetNextRow(&row);
  1347. uint64_t i = 0;
  1348. while (row.size() != 0) {
  1349. i++;
  1350. // auto image = row["image"];
  1351. // MS_LOG(INFO) << "Tensor image shape: " << image->shape();
  1352. iter->GetNextRow(&row);
  1353. }
  1354. EXPECT_EQ(i, 20);
  1355. // Manually terminate the pipeline
  1356. iter->Stop();
  1357. }
  1358. TEST_F(MindDataTestPipeline, TestRandomPosterizeSuccess2) {
  1359. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestRandomPosterizeSuccess2 with default parameters.";
  1360. // Create an ImageFolder Dataset
  1361. std::string folder_path = datasets_root_path_ + "/testPK/data/";
  1362. std::shared_ptr<Dataset> ds = ImageFolder(folder_path, true, RandomSampler(false, 10));
  1363. EXPECT_NE(ds, nullptr);
  1364. // Create a Repeat operation on ds
  1365. int32_t repeat_num = 2;
  1366. ds = ds->Repeat(repeat_num);
  1367. EXPECT_NE(ds, nullptr);
  1368. // Create objects for the tensor ops
  1369. std::shared_ptr<TensorOperation> posterize = vision::RandomPosterize();
  1370. EXPECT_NE(posterize, nullptr);
  1371. // Create a Map operation on ds
  1372. ds = ds->Map({posterize});
  1373. EXPECT_NE(ds, nullptr);
  1374. // Create a Batch operation on ds
  1375. int32_t batch_size = 1;
  1376. ds = ds->Batch(batch_size);
  1377. EXPECT_NE(ds, nullptr);
  1378. // Create an iterator over the result of the above dataset
  1379. // This will trigger the creation of the Execution Tree and launch it.
  1380. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  1381. EXPECT_NE(iter, nullptr);
  1382. // Iterate the dataset and get each row
  1383. std::unordered_map<std::string, mindspore::MSTensor> row;
  1384. iter->GetNextRow(&row);
  1385. uint64_t i = 0;
  1386. while (row.size() != 0) {
  1387. i++;
  1388. // auto image = row["image"];
  1389. // MS_LOG(INFO) << "Tensor image shape: " << image->shape();
  1390. iter->GetNextRow(&row);
  1391. }
  1392. EXPECT_EQ(i, 20);
  1393. // Manually terminate the pipeline
  1394. iter->Stop();
  1395. }
  1396. TEST_F(MindDataTestPipeline, TestRandomResizeSuccess1) {
  1397. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestRandomResizeSuccess1 with single integer input.";
  1398. // Create an ImageFolder Dataset
  1399. std::string folder_path = datasets_root_path_ + "/testPK/data/";
  1400. std::shared_ptr<Dataset> ds = ImageFolder(folder_path, true, RandomSampler(false, 5));
  1401. EXPECT_NE(ds, nullptr);
  1402. // Create objects for the tensor ops
  1403. std::shared_ptr<TensorOperation> random_resize = vision::RandomResize({66});
  1404. EXPECT_NE(random_resize, nullptr);
  1405. // Create a Map operation on ds
  1406. ds = ds->Map({random_resize}, {"image"});
  1407. EXPECT_NE(ds, nullptr);
  1408. // Create an iterator over the result of the above dataset
  1409. // This will trigger the creation of the Execution Tree and launch it.
  1410. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  1411. EXPECT_NE(iter, nullptr);
  1412. // Iterate the dataset and get each row
  1413. std::unordered_map<std::string, mindspore::MSTensor> row;
  1414. iter->GetNextRow(&row);
  1415. uint64_t i = 0;
  1416. while (row.size() != 0) {
  1417. i++;
  1418. // auto image = row["image"];
  1419. // MS_LOG(INFO) << "Tensor image shape: " << image->shape();
  1420. // EXPECT_EQ(image->shape()[0] == 66, true);
  1421. iter->GetNextRow(&row);
  1422. }
  1423. EXPECT_EQ(i, 5);
  1424. // Manually terminate the pipeline
  1425. iter->Stop();
  1426. }
  1427. TEST_F(MindDataTestPipeline, TestRandomResizeSuccess2) {
  1428. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestRandomResizeSuccess2 with (height, width) input.";
  1429. // Create an ImageFolder Dataset
  1430. std::string folder_path = datasets_root_path_ + "/testPK/data/";
  1431. std::shared_ptr<Dataset> ds = ImageFolder(folder_path, true, RandomSampler(false, 3));
  1432. EXPECT_NE(ds, nullptr);
  1433. // Create a Repeat operation on ds
  1434. int32_t repeat_num = 2;
  1435. ds = ds->Repeat(repeat_num);
  1436. EXPECT_NE(ds, nullptr);
  1437. // Create objects for the tensor ops
  1438. std::shared_ptr<TensorOperation> random_resize = vision::RandomResize({66, 77});
  1439. EXPECT_NE(random_resize, nullptr);
  1440. // Create a Map operation on ds
  1441. ds = ds->Map({random_resize}, {"image"});
  1442. EXPECT_NE(ds, nullptr);
  1443. // Create an iterator over the result of the above dataset
  1444. // This will trigger the creation of the Execution Tree and launch it.
  1445. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  1446. EXPECT_NE(iter, nullptr);
  1447. // Iterate the dataset and get each row
  1448. std::unordered_map<std::string, mindspore::MSTensor> row;
  1449. iter->GetNextRow(&row);
  1450. uint64_t i = 0;
  1451. while (row.size() != 0) {
  1452. i++;
  1453. // auto image = row["image"];
  1454. // MS_LOG(INFO) << "Tensor image shape: " << image->shape();
  1455. // EXPECT_EQ(image->shape()[0] == 66 && image->shape()[1] == 77, true);
  1456. iter->GetNextRow(&row);
  1457. }
  1458. EXPECT_EQ(i, 6);
  1459. // Manually terminate the pipeline
  1460. iter->Stop();
  1461. }
  1462. TEST_F(MindDataTestPipeline, TestRandomResizeFail) {
  1463. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestRandomResizeFail incorrect size.";
  1464. // RandomResize : size must only contain positive integers
  1465. std::shared_ptr<TensorOperation> random_resize1 = vision::RandomResize({-66, 77});
  1466. EXPECT_EQ(random_resize1, nullptr);
  1467. // RandomResize : size must only contain positive integers
  1468. std::shared_ptr<TensorOperation> random_resize2 = vision::RandomResize({0, 77});
  1469. EXPECT_EQ(random_resize2, nullptr);
  1470. // RandomResize : size must be a vector of one or two values
  1471. std::shared_ptr<TensorOperation> random_resize3 = vision::RandomResize({1, 2, 3});
  1472. EXPECT_EQ(random_resize3, nullptr);
  1473. // RandomResize : size must be a vector of one or two values
  1474. std::shared_ptr<TensorOperation> random_resize4 = vision::RandomResize({});
  1475. EXPECT_EQ(random_resize4, nullptr);
  1476. }
  1477. TEST_F(MindDataTestPipeline, TestRandomResizeWithBBoxSuccess1) {
  1478. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestRandomResizeWithBBoxSuccess1 with single integer input.";
  1479. // Create an VOC Dataset
  1480. std::string folder_path = datasets_root_path_ + "/testVOC2012_2";
  1481. std::shared_ptr<Dataset> ds = VOC(folder_path, "Detection", "train", {}, true, SequentialSampler(0, 3));
  1482. EXPECT_NE(ds, nullptr);
  1483. // Create objects for the tensor ops
  1484. std::shared_ptr<TensorOperation> random_resize = vision::RandomResizeWithBBox({88});
  1485. EXPECT_NE(random_resize, nullptr);
  1486. // Create a Map operation on ds
  1487. ds = ds->Map({random_resize}, {"image", "bbox"});
  1488. EXPECT_NE(ds, nullptr);
  1489. // Create an iterator over the result of the above dataset
  1490. // This will trigger the creation of the Execution Tree and launch it.
  1491. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  1492. EXPECT_NE(iter, nullptr);
  1493. // Iterate the dataset and get each row
  1494. std::unordered_map<std::string, mindspore::MSTensor> row;
  1495. iter->GetNextRow(&row);
  1496. uint64_t i = 0;
  1497. while (row.size() != 0) {
  1498. i++;
  1499. // auto image = row["image"];
  1500. // MS_LOG(INFO) << "Tensor image shape: " << image->shape();
  1501. // EXPECT_EQ(image->shape()[0] == 88, true);
  1502. iter->GetNextRow(&row);
  1503. }
  1504. EXPECT_EQ(i, 3);
  1505. // Manually terminate the pipeline
  1506. iter->Stop();
  1507. }
  1508. TEST_F(MindDataTestPipeline, TestRandomResizeWithBBoxSuccess2) {
  1509. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestRandomResizeWithBBoxSuccess2 with (height, width) input.";
  1510. // Create an VOC Dataset
  1511. std::string folder_path = datasets_root_path_ + "/testVOC2012_2";
  1512. std::shared_ptr<Dataset> ds = VOC(folder_path, "Detection", "train", {}, true, SequentialSampler(0, 4));
  1513. EXPECT_NE(ds, nullptr);
  1514. // Create a Repeat operation on ds
  1515. int32_t repeat_num = 2;
  1516. ds = ds->Repeat(repeat_num);
  1517. EXPECT_NE(ds, nullptr);
  1518. // Create objects for the tensor ops
  1519. std::shared_ptr<TensorOperation> random_resize = vision::RandomResizeWithBBox({88, 99});
  1520. EXPECT_NE(random_resize, nullptr);
  1521. // Create a Map operation on ds
  1522. ds = ds->Map({random_resize}, {"image", "bbox"});
  1523. EXPECT_NE(ds, nullptr);
  1524. // Create an iterator over the result of the above dataset
  1525. // This will trigger the creation of the Execution Tree and launch it.
  1526. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  1527. EXPECT_NE(iter, nullptr);
  1528. // Iterate the dataset and get each row
  1529. std::unordered_map<std::string, mindspore::MSTensor> row;
  1530. iter->GetNextRow(&row);
  1531. uint64_t i = 0;
  1532. while (row.size() != 0) {
  1533. i++;
  1534. // auto image = row["image"];
  1535. // MS_LOG(INFO) << "Tensor image shape: " << image->shape();
  1536. // EXPECT_EQ(image->shape()[0] == 88 && image->shape()[1] == 99, true);
  1537. iter->GetNextRow(&row);
  1538. }
  1539. EXPECT_EQ(i, 8);
  1540. // Manually terminate the pipeline
  1541. iter->Stop();
  1542. }
  1543. TEST_F(MindDataTestPipeline, TestRandomResizeWithBBoxFail) {
  1544. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestRandomResizeWithBBoxFail incorrect size.";
  1545. // RandomResizeWithBBox : size must only contain positive integers
  1546. std::shared_ptr<TensorOperation> random_resize_with_bbox1 = vision::RandomResizeWithBBox({-66, 77});
  1547. EXPECT_EQ(random_resize_with_bbox1, nullptr);
  1548. // RandomResizeWithBBox : size must be a vector of one or two values
  1549. std::shared_ptr<TensorOperation> random_resize_with_bbox2 = vision::RandomResizeWithBBox({1, 2, 3});
  1550. EXPECT_EQ(random_resize_with_bbox2, nullptr);
  1551. // RandomResizeWithBBox : size must be a vector of one or two values
  1552. std::shared_ptr<TensorOperation> random_resize_with_bbox3 = vision::RandomResizeWithBBox({});
  1553. EXPECT_EQ(random_resize_with_bbox3, nullptr);
  1554. }
  1555. TEST_F(MindDataTestPipeline, TestRandomResizedCropSuccess1) {
  1556. // Testing RandomResizedCrop with default values
  1557. // Create a Cifar10 Dataset
  1558. std::string folder_path = datasets_root_path_ + "/testCifar10Data/";
  1559. std::shared_ptr<Dataset> ds = Cifar10(folder_path, "all", RandomSampler(false, 10));
  1560. EXPECT_NE(ds, nullptr);
  1561. // Create objects for the tensor ops
  1562. std::shared_ptr<TensorOperation> random_resized_crop = vision::RandomResizedCrop({5});
  1563. EXPECT_NE(random_resized_crop, nullptr);
  1564. // Create a Map operation on ds
  1565. ds = ds->Map({random_resized_crop}, {"image"});
  1566. EXPECT_NE(ds, nullptr);
  1567. // Create an iterator over the result of the above dataset
  1568. // This will trigger the creation of the Execution Tree and launch it.
  1569. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  1570. EXPECT_NE(iter, nullptr);
  1571. // Iterate the dataset and get each row
  1572. std::unordered_map<std::string, mindspore::MSTensor> row;
  1573. iter->GetNextRow(&row);
  1574. uint64_t i = 0;
  1575. while (row.size() != 0) {
  1576. i++;
  1577. // auto image = row["image"];
  1578. // MS_LOG(INFO) << "Tensor image shape: " << image->shape();
  1579. // EXPECT_EQ(image->shape()[0] == 5 && image->shape()[1] == 5, true);
  1580. iter->GetNextRow(&row);
  1581. }
  1582. EXPECT_EQ(i, 10);
  1583. // Manually terminate the pipeline
  1584. iter->Stop();
  1585. }
  1586. TEST_F(MindDataTestPipeline, TestRandomResizedCropSuccess2) {
  1587. // Testing RandomResizedCrop with non-default values
  1588. // Create a Cifar10 Dataset
  1589. std::string folder_path = datasets_root_path_ + "/testCifar10Data/";
  1590. std::shared_ptr<Dataset> ds = Cifar10(folder_path, "all", RandomSampler(false, 10));
  1591. EXPECT_NE(ds, nullptr);
  1592. // Create objects for the tensor ops
  1593. std::shared_ptr<TensorOperation> random_resized_crop =
  1594. vision::RandomResizedCrop({5, 10}, {0.25, 0.75}, {0.5, 1.25}, mindspore::dataset::InterpolationMode::kArea, 20);
  1595. EXPECT_NE(random_resized_crop, nullptr);
  1596. // Create a Map operation on ds
  1597. ds = ds->Map({random_resized_crop}, {"image"});
  1598. EXPECT_NE(ds, nullptr);
  1599. // Create an iterator over the result of the above dataset
  1600. // This will trigger the creation of the Execution Tree and launch it.
  1601. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  1602. EXPECT_NE(iter, nullptr);
  1603. // Iterate the dataset and get each row
  1604. std::unordered_map<std::string, mindspore::MSTensor> row;
  1605. iter->GetNextRow(&row);
  1606. uint64_t i = 0;
  1607. while (row.size() != 0) {
  1608. i++;
  1609. // auto image = row["image"];
  1610. // MS_LOG(INFO) << "Tensor image shape: " << image->shape();
  1611. // EXPECT_EQ(image->shape()[0] == 5 && image->shape()[1] == 10, true);
  1612. iter->GetNextRow(&row);
  1613. }
  1614. EXPECT_EQ(i, 10);
  1615. // Manually terminate the pipeline
  1616. iter->Stop();
  1617. }
  1618. TEST_F(MindDataTestPipeline, TestRandomResizedCropFail1) {
  1619. // This should fail because size has negative value
  1620. // Create a Cifar10 Dataset
  1621. std::string folder_path = datasets_root_path_ + "/testCifar10Data/";
  1622. std::shared_ptr<Dataset> ds = Cifar10(folder_path, "all", RandomSampler(false, 10));
  1623. EXPECT_NE(ds, nullptr);
  1624. // Create objects for the tensor ops
  1625. std::shared_ptr<TensorOperation> random_resized_crop = vision::RandomResizedCrop({5, -10});
  1626. EXPECT_EQ(random_resized_crop, nullptr);
  1627. }
  1628. TEST_F(MindDataTestPipeline, TestRandomResizedCropFail2) {
  1629. // This should fail because scale isn't in {min, max} format
  1630. // Create a Cifar10 Dataset
  1631. std::string folder_path = datasets_root_path_ + "/testCifar10Data/";
  1632. std::shared_ptr<Dataset> ds = Cifar10(folder_path, "all", RandomSampler(false, 10));
  1633. EXPECT_NE(ds, nullptr);
  1634. // Create objects for the tensor ops
  1635. std::shared_ptr<TensorOperation> random_resized_crop = vision::RandomResizedCrop({5, 10}, {4, 3});
  1636. EXPECT_EQ(random_resized_crop, nullptr);
  1637. }
  1638. TEST_F(MindDataTestPipeline, TestRandomResizedCropFail3) {
  1639. // This should fail because ratio isn't in {min, max} format
  1640. // Create a Cifar10 Dataset
  1641. std::string folder_path = datasets_root_path_ + "/testCifar10Data/";
  1642. std::shared_ptr<Dataset> ds = Cifar10(folder_path, "all", RandomSampler(false, 10));
  1643. EXPECT_NE(ds, nullptr);
  1644. // Create objects for the tensor ops
  1645. std::shared_ptr<TensorOperation> random_resized_crop = vision::RandomResizedCrop({5, 10}, {4, 5}, {7, 6});
  1646. EXPECT_EQ(random_resized_crop, nullptr);
  1647. }
  1648. TEST_F(MindDataTestPipeline, TestRandomResizedCropFail4) {
  1649. // This should fail because scale has a size of more than 2
  1650. // Create a Cifar10 Dataset
  1651. std::string folder_path = datasets_root_path_ + "/testCifar10Data/";
  1652. std::shared_ptr<Dataset> ds = Cifar10(folder_path, "all", RandomSampler(false, 10));
  1653. EXPECT_NE(ds, nullptr);
  1654. // Create objects for the tensor ops
  1655. std::shared_ptr<TensorOperation> random_resized_crop = vision::RandomResizedCrop({5, 10, 20}, {4, 5}, {7, 6});
  1656. EXPECT_EQ(random_resized_crop, nullptr);
  1657. }
  1658. TEST_F(MindDataTestPipeline, TestRandomResizedCropWithBBoxSuccess1) {
  1659. // Testing RandomResizedCropWithBBox with default values
  1660. // Create an VOC Dataset
  1661. std::string folder_path = datasets_root_path_ + "/testVOC2012_2";
  1662. std::shared_ptr<Dataset> ds = VOC(folder_path, "Detection", "train", {}, true, SequentialSampler(0, 4));
  1663. EXPECT_NE(ds, nullptr);
  1664. // Create objects for the tensor ops
  1665. std::shared_ptr<TensorOperation> random_resized_crop = vision::RandomResizedCropWithBBox({5});
  1666. EXPECT_NE(random_resized_crop, nullptr);
  1667. // Create a Map operation on ds
  1668. ds = ds->Map({random_resized_crop}, {"image", "bbox"});
  1669. EXPECT_NE(ds, nullptr);
  1670. // Create an iterator over the result of the above dataset
  1671. // This will trigger the creation of the Execution Tree and launch it.
  1672. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  1673. EXPECT_NE(iter, nullptr);
  1674. // Iterate the dataset and get each row
  1675. std::unordered_map<std::string, mindspore::MSTensor> row;
  1676. iter->GetNextRow(&row);
  1677. uint64_t i = 0;
  1678. while (row.size() != 0) {
  1679. i++;
  1680. // auto image = row["image"];
  1681. // MS_LOG(INFO) << "Tensor image shape: " << image->shape();
  1682. // EXPECT_EQ(image->shape()[0] == 5 && image->shape()[1] == 5, true);
  1683. iter->GetNextRow(&row);
  1684. }
  1685. EXPECT_EQ(i, 4);
  1686. // Manually terminate the pipeline
  1687. iter->Stop();
  1688. }
  1689. TEST_F(MindDataTestPipeline, TestRandomResizedCropWithBBoxSuccess2) {
  1690. // Testing RandomResizedCropWithBBox with non-default values
  1691. // Create an VOC Dataset
  1692. std::string folder_path = datasets_root_path_ + "/testVOC2012_2";
  1693. std::shared_ptr<Dataset> ds = VOC(folder_path, "Detection", "train", {}, true, SequentialSampler(0, 4));
  1694. EXPECT_NE(ds, nullptr);
  1695. // Create objects for the tensor ops
  1696. std::shared_ptr<TensorOperation> random_resized_crop = vision::RandomResizedCropWithBBox(
  1697. {5, 10}, {0.25, 0.75}, {0.5, 1.25}, mindspore::dataset::InterpolationMode::kArea, 20);
  1698. EXPECT_NE(random_resized_crop, nullptr);
  1699. // Create a Map operation on ds
  1700. ds = ds->Map({random_resized_crop}, {"image", "bbox"});
  1701. EXPECT_NE(ds, nullptr);
  1702. // Create an iterator over the result of the above dataset
  1703. // This will trigger the creation of the Execution Tree and launch it.
  1704. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  1705. EXPECT_NE(iter, nullptr);
  1706. // Iterate the dataset and get each row
  1707. std::unordered_map<std::string, mindspore::MSTensor> row;
  1708. iter->GetNextRow(&row);
  1709. uint64_t i = 0;
  1710. while (row.size() != 0) {
  1711. i++;
  1712. // auto image = row["image"];
  1713. // MS_LOG(INFO) << "Tensor image shape: " << image->shape();
  1714. // EXPECT_EQ(image->shape()[0] == 5 && image->shape()[1] == 10, true);
  1715. iter->GetNextRow(&row);
  1716. }
  1717. EXPECT_EQ(i, 4);
  1718. // Manually terminate the pipeline
  1719. iter->Stop();
  1720. }
  1721. TEST_F(MindDataTestPipeline, TestRandomResizedCropWithBBoxFail1) {
  1722. // This should fail because size has negative value
  1723. // Create a Cifar10 Dataset
  1724. std::string folder_path = datasets_root_path_ + "/testCifar10Data/";
  1725. std::shared_ptr<Dataset> ds = Cifar10(folder_path, "all", RandomSampler(false, 10));
  1726. EXPECT_NE(ds, nullptr);
  1727. // Create objects for the tensor ops
  1728. std::shared_ptr<TensorOperation> random_resized_crop = vision::RandomResizedCropWithBBox({5, -10});
  1729. EXPECT_EQ(random_resized_crop, nullptr);
  1730. }
  1731. TEST_F(MindDataTestPipeline, TestRandomResizedCropWithBBoxFail2) {
  1732. // This should fail because scale isn't in {min, max} format
  1733. // Create a Cifar10 Dataset
  1734. std::string folder_path = datasets_root_path_ + "/testCifar10Data/";
  1735. std::shared_ptr<Dataset> ds = Cifar10(folder_path, "all", RandomSampler(false, 10));
  1736. EXPECT_NE(ds, nullptr);
  1737. // Create objects for the tensor ops
  1738. std::shared_ptr<TensorOperation> random_resized_crop = vision::RandomResizedCropWithBBox({5, 10}, {4, 3});
  1739. EXPECT_EQ(random_resized_crop, nullptr);
  1740. }
  1741. TEST_F(MindDataTestPipeline, TestRandomResizedCropWithBBoxFail3) {
  1742. // This should fail because ratio isn't in {min, max} format
  1743. // Create a Cifar10 Dataset
  1744. std::string folder_path = datasets_root_path_ + "/testCifar10Data/";
  1745. std::shared_ptr<Dataset> ds = Cifar10(folder_path, "all", RandomSampler(false, 10));
  1746. EXPECT_NE(ds, nullptr);
  1747. // Create objects for the tensor ops
  1748. std::shared_ptr<TensorOperation> random_resized_crop = vision::RandomResizedCropWithBBox({5, 10}, {4, 5}, {7, 6});
  1749. EXPECT_EQ(random_resized_crop, nullptr);
  1750. }
  1751. TEST_F(MindDataTestPipeline, TestRandomResizedCropWithBBoxFail4) {
  1752. // This should fail because scale has a size of more than 2
  1753. // Create a Cifar10 Dataset
  1754. std::string folder_path = datasets_root_path_ + "/testCifar10Data/";
  1755. std::shared_ptr<Dataset> ds = Cifar10(folder_path, "all", RandomSampler(false, 10));
  1756. EXPECT_NE(ds, nullptr);
  1757. // Create objects for the tensor ops
  1758. std::shared_ptr<TensorOperation> random_resized_crop = vision::RandomResizedCropWithBBox({5, 10, 20}, {4, 5}, {7, 6});
  1759. EXPECT_EQ(random_resized_crop, nullptr);
  1760. }
  1761. TEST_F(MindDataTestPipeline, TestRandomRotation) {
  1762. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestRandomRotation.";
  1763. // Create an ImageFolder Dataset
  1764. std::string folder_path = datasets_root_path_ + "/testPK/data/";
  1765. std::shared_ptr<Dataset> ds = ImageFolder(folder_path, true, RandomSampler(false, 10));
  1766. EXPECT_NE(ds, nullptr);
  1767. // Create a Repeat operation on ds
  1768. int32_t repeat_num = 2;
  1769. ds = ds->Repeat(repeat_num);
  1770. EXPECT_NE(ds, nullptr);
  1771. // Create objects for the tensor ops
  1772. // Testing the size of degrees is 1
  1773. std::shared_ptr<TensorOperation> random_rotation_op = vision::RandomRotation({180});
  1774. EXPECT_NE(random_rotation_op, nullptr);
  1775. // Testing the size of degrees is 2
  1776. std::shared_ptr<TensorOperation> random_rotation_op1 = vision::RandomRotation({-180, 180});
  1777. EXPECT_NE(random_rotation_op1, nullptr);
  1778. // Testing the size of fill_value is 1
  1779. std::shared_ptr<TensorOperation> random_rotation_op2 =
  1780. vision::RandomRotation({180}, InterpolationMode::kNearestNeighbour, false, {-1, -1}, {2});
  1781. EXPECT_NE(random_rotation_op2, nullptr);
  1782. // Testing the size of fill_value is 3
  1783. std::shared_ptr<TensorOperation> random_rotation_op3 =
  1784. vision::RandomRotation({180}, InterpolationMode::kNearestNeighbour, false, {-1, -1}, {2, 2, 2});
  1785. EXPECT_NE(random_rotation_op3, nullptr);
  1786. // Create a Map operation on ds
  1787. ds = ds->Map({random_rotation_op, random_rotation_op1, random_rotation_op2, random_rotation_op3});
  1788. EXPECT_NE(ds, nullptr);
  1789. // Create a Batch operation on ds
  1790. int32_t batch_size = 1;
  1791. ds = ds->Batch(batch_size);
  1792. EXPECT_NE(ds, nullptr);
  1793. // Create an iterator over the result of the above dataset
  1794. // This will trigger the creation of the Execution Tree and launch it.
  1795. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  1796. EXPECT_NE(iter, nullptr);
  1797. // Iterate the dataset and get each row
  1798. std::unordered_map<std::string, mindspore::MSTensor> row;
  1799. iter->GetNextRow(&row);
  1800. uint64_t i = 0;
  1801. while (row.size() != 0) {
  1802. i++;
  1803. // auto image = row["image"];
  1804. // MS_LOG(INFO) << "Tensor image shape: " << image->shape();
  1805. iter->GetNextRow(&row);
  1806. }
  1807. EXPECT_EQ(i, 20);
  1808. // Manually terminate the pipeline
  1809. iter->Stop();
  1810. }
  1811. TEST_F(MindDataTestPipeline, TestRandomRotationFail) {
  1812. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestRandomRotationFail with invalid parameters.";
  1813. // Create an ImageFolder Dataset
  1814. std::string folder_path = datasets_root_path_ + "/testPK/data/";
  1815. std::shared_ptr<Dataset> ds = ImageFolder(folder_path, true, RandomSampler(false, 10));
  1816. EXPECT_NE(ds, nullptr);
  1817. // Create objects for the tensor ops
  1818. // Testing the size of degrees vector is 0
  1819. std::shared_ptr<TensorOperation> random_rotation_op = vision::RandomRotation({});
  1820. EXPECT_EQ(random_rotation_op, nullptr);
  1821. // Testing the size of degrees vector is 3
  1822. std::shared_ptr<TensorOperation> random_rotation_op1 = vision::RandomRotation({-50.0, 50.0, 100.0});
  1823. EXPECT_EQ(random_rotation_op1, nullptr);
  1824. // Test the case where the first column value of degrees is greater than the second column value
  1825. std::shared_ptr<TensorOperation> random_rotation_op2 = vision::RandomRotation({50.0, -50.0});
  1826. EXPECT_EQ(random_rotation_op2, nullptr);
  1827. // Testing the size of center vector is 1
  1828. std::shared_ptr<TensorOperation> random_rotation_op3 =
  1829. vision::RandomRotation({-50.0, 50.0}, InterpolationMode::kNearestNeighbour, false, {-1.0});
  1830. EXPECT_EQ(random_rotation_op3, nullptr);
  1831. // Testing the size of center vector is 3
  1832. std::shared_ptr<TensorOperation> random_rotation_op4 =
  1833. vision::RandomRotation({-50.0, 50.0}, InterpolationMode::kNearestNeighbour, false, {-1.0, -1.0, -1.0});
  1834. EXPECT_EQ(random_rotation_op4, nullptr);
  1835. // Testing the size of fill_value vector is 2
  1836. std::shared_ptr<TensorOperation> random_rotation_op5 =
  1837. vision::RandomRotation({-50.0, 50.0}, InterpolationMode::kNearestNeighbour, false, {-1.0, -1.0}, {2, 2});
  1838. EXPECT_EQ(random_rotation_op5, nullptr);
  1839. // Testing the size of fill_value vector is 4
  1840. std::shared_ptr<TensorOperation> random_rotation_op6 =
  1841. vision::RandomRotation({-50.0, 50.0}, InterpolationMode::kNearestNeighbour, false, {-1.0, -1.0}, {2, 2, 2, 2});
  1842. EXPECT_EQ(random_rotation_op6, nullptr);
  1843. }
  1844. TEST_F(MindDataTestPipeline, TestRandomSelectSubpolicySuccess) {
  1845. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestRandomSelectSubpolicySuccess.";
  1846. // Create an ImageFolder Dataset
  1847. std::string folder_path = datasets_root_path_ + "/testPK/data/";
  1848. std::shared_ptr<Dataset> ds = ImageFolder(folder_path, true, RandomSampler(false, 7));
  1849. EXPECT_NE(ds, nullptr);
  1850. // Create objects for the tensor ops
  1851. // Valid case: TensorOperation is not null and probability is between (0,1)
  1852. std::shared_ptr<TensorOperation> random_select_subpolicy = vision::RandomSelectSubpolicy(
  1853. {{{vision::Invert(), 0.5}, {vision::Equalize(), 0.5}}, {{vision::Resize({15, 15}), 1}}});
  1854. EXPECT_NE(random_select_subpolicy, nullptr);
  1855. // Create a Map operation on ds
  1856. ds = ds->Map({random_select_subpolicy});
  1857. EXPECT_NE(ds, nullptr);
  1858. // Create an iterator over the result of the above dataset
  1859. // This will trigger the creation of the Execution Tree and launch it.
  1860. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  1861. EXPECT_NE(iter, nullptr);
  1862. // Iterate the dataset and get each row
  1863. std::unordered_map<std::string, mindspore::MSTensor> row;
  1864. iter->GetNextRow(&row);
  1865. uint64_t i = 0;
  1866. while (row.size() != 0) {
  1867. i++;
  1868. // auto image = row["image"];
  1869. // MS_LOG(INFO) << "Tensor image shape: " << image->shape();
  1870. iter->GetNextRow(&row);
  1871. }
  1872. EXPECT_EQ(i, 7);
  1873. // Manually terminate the pipeline
  1874. iter->Stop();
  1875. }
  1876. TEST_F(MindDataTestPipeline, TestRandomSelectSubpolicyFail) {
  1877. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestRandomSelectSubpolicyFail.";
  1878. // RandomSelectSubpolicy : probability of transform must be between 0.0 and 1.0
  1879. std::shared_ptr<TensorOperation> random_select_subpolicy1 = vision::RandomSelectSubpolicy(
  1880. {{{vision::Invert(), 1.5}, {vision::Equalize(), 0.5}}, {{vision::Resize({15, 15}), 1}}});
  1881. EXPECT_EQ(random_select_subpolicy1, nullptr);
  1882. // RandomSelectSubpolicy: policy must not be empty
  1883. std::shared_ptr<TensorOperation> random_select_subpolicy2 =
  1884. vision::RandomSelectSubpolicy({{{vision::Invert(), 0.5}, {vision::Equalize(), 0.5}}, {{nullptr, 1}}});
  1885. EXPECT_EQ(random_select_subpolicy2, nullptr);
  1886. // RandomSelectSubpolicy: policy must not be empty
  1887. std::shared_ptr<TensorOperation> random_select_subpolicy3 = vision::RandomSelectSubpolicy({});
  1888. EXPECT_EQ(random_select_subpolicy3, nullptr);
  1889. // RandomSelectSubpolicy: policy must not be empty
  1890. std::shared_ptr<TensorOperation> random_select_subpolicy4 =
  1891. vision::RandomSelectSubpolicy({{{vision::Invert(), 0.5}, {vision::Equalize(), 0.5}}, {}});
  1892. EXPECT_EQ(random_select_subpolicy4, nullptr);
  1893. // RandomSelectSubpolicy: policy must not be empty
  1894. std::shared_ptr<TensorOperation> random_select_subpolicy5 =
  1895. vision::RandomSelectSubpolicy({{{}, {vision::Equalize(), 0.5}}, {{vision::Resize({15, 15}), 1}}});
  1896. EXPECT_EQ(random_select_subpolicy5, nullptr);
  1897. }
  1898. TEST_F(MindDataTestPipeline, TestRandomSharpness) {
  1899. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestRandomSharpness.";
  1900. // Create an ImageFolder Dataset
  1901. std::string folder_path = datasets_root_path_ + "/testPK/data/";
  1902. std::shared_ptr<Dataset> ds = ImageFolder(folder_path, true, RandomSampler(false, 10));
  1903. EXPECT_NE(ds, nullptr);
  1904. // Create a Repeat operation on ds
  1905. int32_t repeat_num = 2;
  1906. ds = ds->Repeat(repeat_num);
  1907. EXPECT_NE(ds, nullptr);
  1908. // Create objects for the tensor ops
  1909. // Valid case: Input start degree and end degree
  1910. std::shared_ptr<TensorOperation> random_sharpness_op_1 = vision::RandomSharpness({0.4, 2.3});
  1911. EXPECT_NE(random_sharpness_op_1, nullptr);
  1912. // Failure case: Empty degrees vector
  1913. std::shared_ptr<TensorOperation> random_sharpness_op_2 = vision::RandomSharpness({});
  1914. EXPECT_EQ(random_sharpness_op_2, nullptr);
  1915. // Valid case: Use default input values
  1916. std::shared_ptr<TensorOperation> random_sharpness_op_3 = vision::RandomSharpness();
  1917. EXPECT_NE(random_sharpness_op_3, nullptr);
  1918. // Failure case: Single degree value
  1919. std::shared_ptr<TensorOperation> random_sharpness_op_4 = vision::RandomSharpness({0.1});
  1920. EXPECT_EQ(random_sharpness_op_4, nullptr);
  1921. // Create a Map operation on ds
  1922. ds = ds->Map({random_sharpness_op_1, random_sharpness_op_3});
  1923. EXPECT_NE(ds, nullptr);
  1924. // Create a Batch operation on ds
  1925. int32_t batch_size = 1;
  1926. ds = ds->Batch(batch_size);
  1927. EXPECT_NE(ds, nullptr);
  1928. // Create an iterator over the result of the above dataset
  1929. // This will trigger the creation of the Execution Tree and launch it.
  1930. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  1931. EXPECT_NE(iter, nullptr);
  1932. // Iterate the dataset and get each row
  1933. std::unordered_map<std::string, mindspore::MSTensor> row;
  1934. iter->GetNextRow(&row);
  1935. uint64_t i = 0;
  1936. while (row.size() != 0) {
  1937. i++;
  1938. // auto image = row["image"];
  1939. // MS_LOG(INFO) << "Tensor image shape: " << image->shape();
  1940. iter->GetNextRow(&row);
  1941. }
  1942. EXPECT_EQ(i, 20);
  1943. // Manually terminate the pipeline
  1944. iter->Stop();
  1945. }
  1946. TEST_F(MindDataTestPipeline, TestRandomSolarizeSucess1) {
  1947. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestRandomSolarizeSucess1.";
  1948. // Create an ImageFolder Dataset
  1949. std::string folder_path = datasets_root_path_ + "/testPK/data/";
  1950. std::shared_ptr<Dataset> ds = ImageFolder(folder_path, true, RandomSampler(false, 10));
  1951. EXPECT_NE(ds, nullptr);
  1952. // Create objects for the tensor ops
  1953. std::vector<uint8_t> threshold = {10, 100};
  1954. std::shared_ptr<TensorOperation> random_solarize = mindspore::dataset::vision::RandomSolarize(threshold);
  1955. EXPECT_NE(random_solarize, nullptr);
  1956. // Create a Map operation on ds
  1957. ds = ds->Map({random_solarize});
  1958. EXPECT_NE(ds, nullptr);
  1959. // Create an iterator over the result of the above dataset
  1960. // This will trigger the creation of the Execution Tree and launch it.
  1961. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  1962. EXPECT_NE(iter, nullptr);
  1963. // Iterate the dataset and get each row
  1964. std::unordered_map<std::string, mindspore::MSTensor> row;
  1965. iter->GetNextRow(&row);
  1966. uint64_t i = 0;
  1967. while (row.size() != 0) {
  1968. i++;
  1969. // auto image = row["image"];
  1970. // MS_LOG(INFO) << "Tensor image shape: " << image->shape();
  1971. iter->GetNextRow(&row);
  1972. }
  1973. EXPECT_EQ(i, 10);
  1974. // Manually terminate the pipeline
  1975. iter->Stop();
  1976. }
  1977. TEST_F(MindDataTestPipeline, TestRandomSolarizeSucess2) {
  1978. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestRandomSolarizeSuccess2 with default parameters.";
  1979. // Create an ImageFolder Dataset
  1980. std::string folder_path = datasets_root_path_ + "/testPK/data/";
  1981. std::shared_ptr<Dataset> ds = ImageFolder(folder_path, true, RandomSampler(false, 10));
  1982. EXPECT_NE(ds, nullptr);
  1983. // Create objects for the tensor ops
  1984. std::shared_ptr<TensorOperation> random_solarize = mindspore::dataset::vision::RandomSolarize();
  1985. EXPECT_NE(random_solarize, nullptr);
  1986. // Create a Map operation on ds
  1987. ds = ds->Map({random_solarize});
  1988. EXPECT_NE(ds, nullptr);
  1989. // Create an iterator over the result of the above dataset
  1990. // This will trigger the creation of the Execution Tree and launch it.
  1991. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  1992. EXPECT_NE(iter, nullptr);
  1993. // Iterate the dataset and get each row
  1994. std::unordered_map<std::string, mindspore::MSTensor> row;
  1995. iter->GetNextRow(&row);
  1996. uint64_t i = 0;
  1997. while (row.size() != 0) {
  1998. i++;
  1999. // auto image = row["image"];
  2000. // MS_LOG(INFO) << "Tensor image shape: " << image->shape();
  2001. iter->GetNextRow(&row);
  2002. }
  2003. EXPECT_EQ(i, 10);
  2004. // Manually terminate the pipeline
  2005. iter->Stop();
  2006. }
  2007. TEST_F(MindDataTestPipeline, TestRandomSolarizeFail) {
  2008. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestRandomSolarizeFail with invalid parameters.";
  2009. std::vector<uint8_t> threshold = {13, 1};
  2010. std::shared_ptr<TensorOperation> random_solarize = mindspore::dataset::vision::RandomSolarize(threshold);
  2011. EXPECT_EQ(random_solarize, nullptr);
  2012. threshold = {1, 2, 3};
  2013. random_solarize = mindspore::dataset::vision::RandomSolarize(threshold);
  2014. EXPECT_EQ(random_solarize, nullptr);
  2015. threshold = {1};
  2016. random_solarize = mindspore::dataset::vision::RandomSolarize(threshold);
  2017. EXPECT_EQ(random_solarize, nullptr);
  2018. threshold = {};
  2019. random_solarize = mindspore::dataset::vision::RandomSolarize(threshold);
  2020. EXPECT_EQ(random_solarize, nullptr);
  2021. }
  2022. TEST_F(MindDataTestPipeline, TestRandomVerticalFlipFail) {
  2023. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestRandomVerticalFlipFail with invalid parameters.";
  2024. // Create object for the tensor op
  2025. // Invalid negative input
  2026. std::shared_ptr<TensorOperation> random_vertical_flip_op = vision::RandomVerticalFlip(-0.5);
  2027. EXPECT_EQ(random_vertical_flip_op, nullptr);
  2028. // Invalid >1 input
  2029. random_vertical_flip_op = vision::RandomVerticalFlip(1.1);
  2030. EXPECT_EQ(random_vertical_flip_op, nullptr);
  2031. }
  2032. TEST_F(MindDataTestPipeline, TestResizeFail) {
  2033. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestResize with invalid parameters.";
  2034. // negative resize value
  2035. std::shared_ptr<TensorOperation> resize_op = mindspore::dataset::vision::Resize({30, -30});
  2036. EXPECT_EQ(resize_op, nullptr);
  2037. // zero resize value
  2038. resize_op = mindspore::dataset::vision::Resize({0, 30});
  2039. EXPECT_EQ(resize_op, nullptr);
  2040. // resize with 3 values
  2041. resize_op = mindspore::dataset::vision::Resize({30, 20, 10});
  2042. EXPECT_EQ(resize_op, nullptr);
  2043. }
  2044. TEST_F(MindDataTestPipeline, TestResizeWithBBoxSuccess) {
  2045. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestResizeWithBBoxSuccess.";
  2046. // Create an VOC Dataset
  2047. std::string folder_path = datasets_root_path_ + "/testVOC2012_2";
  2048. std::shared_ptr<Dataset> ds = VOC(folder_path, "Detection", "train", {}, true, SequentialSampler(0, 3));
  2049. EXPECT_NE(ds, nullptr);
  2050. // Create objects for the tensor ops
  2051. std::shared_ptr<TensorOperation> resize_with_bbox_op = vision::ResizeWithBBox({30});
  2052. EXPECT_NE(resize_with_bbox_op, nullptr);
  2053. std::shared_ptr<TensorOperation> resize_with_bbox_op1 = vision::ResizeWithBBox({30, 30});
  2054. EXPECT_NE(resize_with_bbox_op1, nullptr);
  2055. // Create a Map operation on ds
  2056. ds = ds->Map({resize_with_bbox_op, resize_with_bbox_op1}, {"image", "bbox"}, {"image", "bbox"}, {"image", "bbox"});
  2057. EXPECT_NE(ds, nullptr);
  2058. // Create an iterator over the result of the above dataset
  2059. // This will trigger the creation of the Execution Tree and launch it.
  2060. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  2061. EXPECT_NE(iter, nullptr);
  2062. // Iterate the dataset and get each row
  2063. std::unordered_map<std::string, mindspore::MSTensor> row;
  2064. iter->GetNextRow(&row);
  2065. uint64_t i = 0;
  2066. while (row.size() != 0) {
  2067. i++;
  2068. // auto image = row["image"];
  2069. // MS_LOG(INFO) << "Tensor image shape: " << image->shape();
  2070. iter->GetNextRow(&row);
  2071. }
  2072. EXPECT_EQ(i, 3);
  2073. // Manually terminate the pipeline
  2074. iter->Stop();
  2075. }
  2076. TEST_F(MindDataTestPipeline, TestResizeWithBBoxFail) {
  2077. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestResizeWithBBoxFail with invalid parameters.";
  2078. // Testing negative resize value
  2079. std::shared_ptr<TensorOperation> resize_with_bbox_op = vision::ResizeWithBBox({10, -10});
  2080. EXPECT_EQ(resize_with_bbox_op, nullptr);
  2081. // Testing negative resize value
  2082. std::shared_ptr<TensorOperation> resize_with_bbox_op1 = vision::ResizeWithBBox({-10});
  2083. EXPECT_EQ(resize_with_bbox_op1, nullptr);
  2084. // Testinig zero resize value
  2085. std::shared_ptr<TensorOperation> resize_with_bbox_op2 = vision::ResizeWithBBox({0, 10});
  2086. EXPECT_EQ(resize_with_bbox_op2, nullptr);
  2087. // Testing resize with 3 values
  2088. std::shared_ptr<TensorOperation> resize_with_bbox_op3 = vision::ResizeWithBBox({10, 10, 10});
  2089. EXPECT_EQ(resize_with_bbox_op3, nullptr);
  2090. }
  2091. TEST_F(MindDataTestPipeline, TestRandomVerticalFlipWithBBoxSuccess) {
  2092. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestRandomVerticalFlipWithBBoxSuccess.";
  2093. // Create an VOC Dataset
  2094. std::string folder_path = datasets_root_path_ + "/testVOC2012_2";
  2095. std::shared_ptr<Dataset> ds = VOC(folder_path, "Detection", "train", {}, true, SequentialSampler(0, 3));
  2096. EXPECT_NE(ds, nullptr);
  2097. // Create objects for the tensor ops
  2098. std::shared_ptr<TensorOperation> random_vertical_flip_op = vision::RandomVerticalFlipWithBBox(0.4);
  2099. EXPECT_NE(random_vertical_flip_op, nullptr);
  2100. // Create a Map operation on ds
  2101. ds = ds->Map({random_vertical_flip_op}, {"image", "bbox"}, {"image", "bbox"}, {"image", "bbox"});
  2102. EXPECT_NE(ds, nullptr);
  2103. // Create an iterator over the result of the above dataset
  2104. // This will trigger the creation of the Execution Tree and launch it.
  2105. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  2106. EXPECT_NE(iter, nullptr);
  2107. // Iterate the dataset and get each row
  2108. std::unordered_map<std::string, mindspore::MSTensor> row;
  2109. iter->GetNextRow(&row);
  2110. uint64_t i = 0;
  2111. while (row.size() != 0) {
  2112. i++;
  2113. // auto image = row["image"];
  2114. // MS_LOG(INFO) << "Tensor image shape: " << image->shape();
  2115. iter->GetNextRow(&row);
  2116. }
  2117. EXPECT_EQ(i, 3);
  2118. // Manually terminate the pipeline
  2119. iter->Stop();
  2120. }
  2121. TEST_F(MindDataTestPipeline, TestRandomVerticalFlipWithBBoxFail) {
  2122. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestRandomVerticalFlipWithBBoxFail with invalid parameters.";
  2123. // Create an VOC Dataset
  2124. std::string folder_path = datasets_root_path_ + "/testVOC2012_2";
  2125. std::shared_ptr<Dataset> ds = VOC(folder_path, "Detection", "train", {}, true, SequentialSampler(0, 3));
  2126. EXPECT_NE(ds, nullptr);
  2127. // Create objects for the tensor ops
  2128. // Incorrect prob parameter.
  2129. std::shared_ptr<TensorOperation> random_vertical_flip_op = vision::RandomVerticalFlipWithBBox(-0.5);
  2130. EXPECT_EQ(random_vertical_flip_op, nullptr);
  2131. // Incorrect prob parameter.
  2132. std::shared_ptr<TensorOperation> random_vertical_flip_op1 = vision::RandomVerticalFlipWithBBox(3.0);
  2133. EXPECT_EQ(random_vertical_flip_op1, nullptr);
  2134. }
  2135. TEST_F(MindDataTestPipeline, TestResize1) {
  2136. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestResize1 with single integer input.";
  2137. // Create an ImageFolder Dataset
  2138. std::string folder_path = datasets_root_path_ + "/testPK/data/";
  2139. std::shared_ptr<Dataset> ds = ImageFolder(folder_path, true, RandomSampler(false, 6));
  2140. EXPECT_NE(ds, nullptr);
  2141. // Create a Repeat operation on ds
  2142. int32_t repeat_num = 4;
  2143. ds = ds->Repeat(repeat_num);
  2144. EXPECT_NE(ds, nullptr);
  2145. // Create resize object with single integer input
  2146. std::shared_ptr<TensorOperation> resize_op = vision::Resize({30});
  2147. EXPECT_NE(resize_op, nullptr);
  2148. // Create a Map operation on ds
  2149. ds = ds->Map({resize_op});
  2150. EXPECT_NE(ds, nullptr);
  2151. // Create a Batch operation on ds
  2152. int32_t batch_size = 1;
  2153. ds = ds->Batch(batch_size);
  2154. EXPECT_NE(ds, nullptr);
  2155. // Create an iterator over the result of the above dataset
  2156. // This will trigger the creation of the Execution Tree and launch it.
  2157. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  2158. EXPECT_NE(iter, nullptr);
  2159. // Iterate the dataset and get each row
  2160. std::unordered_map<std::string, mindspore::MSTensor> row;
  2161. iter->GetNextRow(&row);
  2162. uint64_t i = 0;
  2163. while (row.size() != 0) {
  2164. i++;
  2165. // auto image = row["image"];
  2166. // MS_LOG(INFO) << "Tensor image shape: " << image->shape();
  2167. iter->GetNextRow(&row);
  2168. }
  2169. EXPECT_EQ(i, 24);
  2170. // Manually terminate the pipeline
  2171. iter->Stop();
  2172. }
  2173. TEST_F(MindDataTestPipeline, TestRescaleSucess1) {
  2174. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestRescaleSucess1.";
  2175. // Create an ImageFolder Dataset
  2176. std::string folder_path = datasets_root_path_ + "/testPK/data/";
  2177. std::shared_ptr<Dataset> ds = ImageFolder(folder_path, true, SequentialSampler(0, 1));
  2178. EXPECT_NE(ds, nullptr);
  2179. // Create an iterator over the result of the above dataset
  2180. // This will trigger the creation of the Execution Tree and launch it.
  2181. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  2182. EXPECT_NE(iter, nullptr);
  2183. // Iterate the dataset and get each row
  2184. std::unordered_map<std::string, mindspore::MSTensor> row;
  2185. iter->GetNextRow(&row);
  2186. auto image = row["image"];
  2187. // Create objects for the tensor ops
  2188. std::shared_ptr<TensorOperation> rescale = mindspore::dataset::vision::Rescale(1.0, 0.0);
  2189. EXPECT_NE(rescale, nullptr);
  2190. // Convert to the same type
  2191. std::shared_ptr<TensorOperation> type_cast = transforms::TypeCast("uint8");
  2192. EXPECT_NE(type_cast, nullptr);
  2193. ds = ds->Map({rescale, type_cast}, {"image"});
  2194. EXPECT_NE(ds, nullptr);
  2195. // Create an iterator over the result of the above dataset
  2196. // This will trigger the creation of the Execution Tree and launch it.
  2197. std::shared_ptr<Iterator> iter1 = ds->CreateIterator();
  2198. EXPECT_NE(iter1, nullptr);
  2199. // Iterate the dataset and get each row1
  2200. std::unordered_map<std::string, mindspore::MSTensor> row1;
  2201. iter1->GetNextRow(&row1);
  2202. auto image1 = row1["image"];
  2203. // EXPECT_EQ(*image, *image1);
  2204. // Manually terminate the pipeline
  2205. iter1->Stop();
  2206. }
  2207. TEST_F(MindDataTestPipeline, TestRescaleSucess2) {
  2208. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestRescaleSucess2 with different params.";
  2209. // Create an ImageFolder Dataset
  2210. std::string folder_path = datasets_root_path_ + "/testPK/data/";
  2211. std::shared_ptr<Dataset> ds = ImageFolder(folder_path, true, RandomSampler(false, 1));
  2212. EXPECT_NE(ds, nullptr);
  2213. // Create objects for the tensor ops
  2214. std::shared_ptr<TensorOperation> rescale = mindspore::dataset::vision::Rescale(1.0 / 255, 1.0);
  2215. EXPECT_NE(rescale, nullptr);
  2216. ds = ds->Map({rescale}, {"image"});
  2217. EXPECT_NE(ds, nullptr);
  2218. // Create an iterator over the result of the above dataset
  2219. // This will trigger the creation of the Execution Tree and launch it.
  2220. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  2221. EXPECT_NE(iter, nullptr);
  2222. // Iterate the dataset and get each row
  2223. std::unordered_map<std::string, mindspore::MSTensor> row;
  2224. iter->GetNextRow(&row);
  2225. uint64_t i = 0;
  2226. while (row.size() != 0) {
  2227. i++;
  2228. // auto image = row["image"];
  2229. // MS_LOG(INFO) << "Tensor image shape: " << image->shape();
  2230. iter->GetNextRow(&row);
  2231. }
  2232. EXPECT_EQ(i, 1);
  2233. // Manually terminate the pipeline
  2234. iter->Stop();
  2235. }
  2236. TEST_F(MindDataTestPipeline, TestRescaleFail) {
  2237. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestRescaleFail with invalid params.";
  2238. // incorrect negative rescale parameter
  2239. std::shared_ptr<TensorOperation> rescale = mindspore::dataset::vision::Rescale(-1.0, 0.0);
  2240. EXPECT_EQ(rescale, nullptr);
  2241. }
  2242. TEST_F(MindDataTestPipeline, TestSoftDvppDecodeRandomCropResizeJpegSuccess1) {
  2243. MS_LOG(INFO)
  2244. << "Doing MindDataTestPipeline-TestSoftDvppDecodeRandomCropResizeJpegSuccess1 with single integer input.";
  2245. // Create an ImageFolder Dataset
  2246. std::string folder_path = datasets_root_path_ + "/testPK/data/";
  2247. std::shared_ptr<Dataset> ds = ImageFolder(folder_path, false, RandomSampler(false, 4));
  2248. EXPECT_NE(ds, nullptr);
  2249. // Create objects for the tensor ops
  2250. std::shared_ptr<TensorOperation> soft_dvpp_decode_random_crop_resize_jpeg =
  2251. vision::SoftDvppDecodeRandomCropResizeJpeg({500});
  2252. EXPECT_NE(soft_dvpp_decode_random_crop_resize_jpeg, nullptr);
  2253. // Create a Map operation on ds
  2254. ds = ds->Map({soft_dvpp_decode_random_crop_resize_jpeg}, {"image"});
  2255. EXPECT_NE(ds, nullptr);
  2256. // Create an iterator over the result of the above dataset
  2257. // This will trigger the creation of the Execution Tree and launch it.
  2258. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  2259. EXPECT_NE(iter, nullptr);
  2260. // Iterate the dataset and get each row
  2261. std::unordered_map<std::string, mindspore::MSTensor> row;
  2262. iter->GetNextRow(&row);
  2263. uint64_t i = 0;
  2264. while (row.size() != 0) {
  2265. i++;
  2266. // auto image = row["image"];
  2267. // MS_LOG(INFO) << "Tensor image shape: " << image->shape();
  2268. // EXPECT_EQ(image->shape()[0] == 500 && image->shape()[1] == 500, true);
  2269. iter->GetNextRow(&row);
  2270. }
  2271. EXPECT_EQ(i, 4);
  2272. // Manually terminate the pipeline
  2273. iter->Stop();
  2274. }
  2275. TEST_F(MindDataTestPipeline, TestSoftDvppDecodeRandomCropResizeJpegSuccess2) {
  2276. MS_LOG(INFO)
  2277. << "Doing MindDataTestPipeline-TestSoftDvppDecodeRandomCropResizeJpegSuccess2 with (height, width) input.";
  2278. // Create an ImageFolder Dataset
  2279. std::string folder_path = datasets_root_path_ + "/testPK/data/";
  2280. std::shared_ptr<Dataset> ds = ImageFolder(folder_path, false, RandomSampler(false, 6));
  2281. EXPECT_NE(ds, nullptr);
  2282. // Create objects for the tensor ops
  2283. std::shared_ptr<TensorOperation> soft_dvpp_decode_random_crop_resize_jpeg =
  2284. vision::SoftDvppDecodeRandomCropResizeJpeg({500, 600}, {0.25, 0.75}, {0.5, 1.25}, 20);
  2285. EXPECT_NE(soft_dvpp_decode_random_crop_resize_jpeg, nullptr);
  2286. // Create a Map operation on ds
  2287. ds = ds->Map({soft_dvpp_decode_random_crop_resize_jpeg}, {"image"});
  2288. EXPECT_NE(ds, nullptr);
  2289. // Create an iterator over the result of the above dataset
  2290. // This will trigger the creation of the Execution Tree and launch it.
  2291. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  2292. EXPECT_NE(iter, nullptr);
  2293. // Iterate the dataset and get each row
  2294. std::unordered_map<std::string, mindspore::MSTensor> row;
  2295. iter->GetNextRow(&row);
  2296. uint64_t i = 0;
  2297. while (row.size() != 0) {
  2298. i++;
  2299. // auto image = row["image"];
  2300. // MS_LOG(INFO) << "Tensor image shape: " << image->shape();
  2301. // EXPECT_EQ(image->shape()[0] == 500 && image->shape()[1] == 600, true);
  2302. iter->GetNextRow(&row);
  2303. }
  2304. EXPECT_EQ(i, 6);
  2305. // Manually terminate the pipeline
  2306. iter->Stop();
  2307. }
  2308. TEST_F(MindDataTestPipeline, TestSoftDvppDecodeRandomCropResizeJpegFail) {
  2309. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestSoftDvppDecodeRandomCropResizeJpegFail with incorrect parameters.";
  2310. // SoftDvppDecodeRandomCropResizeJpeg: size must only contain positive integers
  2311. auto soft_dvpp_decode_random_crop_resize_jpeg1 = vision::SoftDvppDecodeRandomCropResizeJpeg({-500, 600});
  2312. EXPECT_EQ(soft_dvpp_decode_random_crop_resize_jpeg1, nullptr);
  2313. // SoftDvppDecodeRandomCropResizeJpeg: size must only contain positive integers
  2314. auto soft_dvpp_decode_random_crop_resize_jpeg2 = vision::SoftDvppDecodeRandomCropResizeJpeg({-500});
  2315. EXPECT_EQ(soft_dvpp_decode_random_crop_resize_jpeg2, nullptr);
  2316. // SoftDvppDecodeRandomCropResizeJpeg: size must be a vector of one or two values
  2317. auto soft_dvpp_decode_random_crop_resize_jpeg3 = vision::SoftDvppDecodeRandomCropResizeJpeg({500, 600, 700});
  2318. EXPECT_EQ(soft_dvpp_decode_random_crop_resize_jpeg3, nullptr);
  2319. // SoftDvppDecodeRandomCropResizeJpeg: scale must be greater than or equal to 0
  2320. auto soft_dvpp_decode_random_crop_resize_jpeg4 = vision::SoftDvppDecodeRandomCropResizeJpeg({500}, {-0.1, 0.9});
  2321. EXPECT_EQ(soft_dvpp_decode_random_crop_resize_jpeg4, nullptr);
  2322. // SoftDvppDecodeRandomCropResizeJpeg: scale must be in the format of (min, max)
  2323. auto soft_dvpp_decode_random_crop_resize_jpeg5 = vision::SoftDvppDecodeRandomCropResizeJpeg({500}, {0.6, 0.2});
  2324. EXPECT_EQ(soft_dvpp_decode_random_crop_resize_jpeg5, nullptr);
  2325. // SoftDvppDecodeRandomCropResizeJpeg: scale must be a vector of two values
  2326. auto soft_dvpp_decode_random_crop_resize_jpeg6 = vision::SoftDvppDecodeRandomCropResizeJpeg({500}, {0.5, 0.6, 0.7});
  2327. EXPECT_EQ(soft_dvpp_decode_random_crop_resize_jpeg6, nullptr);
  2328. // SoftDvppDecodeRandomCropResizeJpeg: ratio must be greater than or equal to 0
  2329. auto soft_dvpp_decode_random_crop_resize_jpeg7 =
  2330. vision::SoftDvppDecodeRandomCropResizeJpeg({500}, {0.5, 0.9}, {-0.2, 0.4});
  2331. EXPECT_EQ(soft_dvpp_decode_random_crop_resize_jpeg7, nullptr);
  2332. // SoftDvppDecodeRandomCropResizeJpeg: ratio must be in the format of (min, max)
  2333. auto soft_dvpp_decode_random_crop_resize_jpeg8 =
  2334. vision::SoftDvppDecodeRandomCropResizeJpeg({500}, {0.5, 0.9}, {0.4, 0.2});
  2335. EXPECT_EQ(soft_dvpp_decode_random_crop_resize_jpeg8, nullptr);
  2336. // SoftDvppDecodeRandomCropResizeJpeg: ratio must be a vector of two values
  2337. auto soft_dvpp_decode_random_crop_resize_jpeg9 =
  2338. vision::SoftDvppDecodeRandomCropResizeJpeg({500}, {0.5, 0.9}, {0.1, 0.2, 0.3});
  2339. EXPECT_EQ(soft_dvpp_decode_random_crop_resize_jpeg9, nullptr);
  2340. // SoftDvppDecodeRandomCropResizeJpeg: max_attempts must be greater than or equal to 1
  2341. auto soft_dvpp_decode_random_crop_resize_jpeg10 =
  2342. vision::SoftDvppDecodeRandomCropResizeJpeg({500}, {0.5, 0.9}, {0.1, 0.2}, 0);
  2343. EXPECT_EQ(soft_dvpp_decode_random_crop_resize_jpeg10, nullptr);
  2344. }
  2345. TEST_F(MindDataTestPipeline, TestSoftDvppDecodeResizeJpegSuccess1) {
  2346. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestSoftDvppDecodeResizeJpegSuccess1 with single integer input.";
  2347. // Create an ImageFolder Dataset
  2348. std::string folder_path = datasets_root_path_ + "/testPK/data/";
  2349. std::shared_ptr<Dataset> ds = ImageFolder(folder_path, false, RandomSampler(false, 4));
  2350. EXPECT_NE(ds, nullptr);
  2351. // Create a Repeat operation on ds
  2352. int32_t repeat_num = 3;
  2353. ds = ds->Repeat(repeat_num);
  2354. EXPECT_NE(ds, nullptr);
  2355. // Create SoftDvppDecodeResizeJpeg object with single integer input
  2356. std::shared_ptr<TensorOperation> soft_dvpp_decode_resize_jpeg_op = vision::SoftDvppDecodeResizeJpeg({1134});
  2357. EXPECT_NE(soft_dvpp_decode_resize_jpeg_op, nullptr);
  2358. // Create a Map operation on ds
  2359. ds = ds->Map({soft_dvpp_decode_resize_jpeg_op});
  2360. EXPECT_NE(ds, nullptr);
  2361. // Create an iterator over the result of the above dataset
  2362. // This will trigger the creation of the Execution Tree and launch it.
  2363. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  2364. EXPECT_NE(iter, nullptr);
  2365. // Iterate the dataset and get each row
  2366. std::unordered_map<std::string, mindspore::MSTensor> row;
  2367. iter->GetNextRow(&row);
  2368. uint64_t i = 0;
  2369. while (row.size() != 0) {
  2370. i++;
  2371. // auto image = row["image"];
  2372. // MS_LOG(INFO) << "Tensor image shape: " << image->shape();
  2373. iter->GetNextRow(&row);
  2374. }
  2375. EXPECT_EQ(i, 12);
  2376. // Manually terminate the pipeline
  2377. iter->Stop();
  2378. }
  2379. TEST_F(MindDataTestPipeline, TestSoftDvppDecodeResizeJpegSuccess2) {
  2380. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestSoftDvppDecodeResizeJpegSuccess2 with (height, width) input.";
  2381. // Create an ImageFolder Dataset
  2382. std::string folder_path = datasets_root_path_ + "/testPK/data/";
  2383. std::shared_ptr<Dataset> ds = ImageFolder(folder_path, false, RandomSampler(false, 2));
  2384. EXPECT_NE(ds, nullptr);
  2385. // Create SoftDvppDecodeResizeJpeg object with single integer input
  2386. std::shared_ptr<TensorOperation> soft_dvpp_decode_resize_jpeg_op = vision::SoftDvppDecodeResizeJpeg({100, 200});
  2387. EXPECT_NE(soft_dvpp_decode_resize_jpeg_op, nullptr);
  2388. // Create a Map operation on ds
  2389. ds = ds->Map({soft_dvpp_decode_resize_jpeg_op});
  2390. EXPECT_NE(ds, nullptr);
  2391. // Create an iterator over the result of the above dataset
  2392. // This will trigger the creation of the Execution Tree and launch it.
  2393. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  2394. EXPECT_NE(iter, nullptr);
  2395. // Iterate the dataset and get each row
  2396. std::unordered_map<std::string, mindspore::MSTensor> row;
  2397. iter->GetNextRow(&row);
  2398. uint64_t i = 0;
  2399. while (row.size() != 0) {
  2400. i++;
  2401. // auto image = row["image"];
  2402. // MS_LOG(INFO) << "Tensor image shape: " << image->shape();
  2403. iter->GetNextRow(&row);
  2404. }
  2405. EXPECT_EQ(i, 2);
  2406. // Manually terminate the pipeline
  2407. iter->Stop();
  2408. }
  2409. TEST_F(MindDataTestPipeline, TestSoftDvppDecodeResizeJpegFail) {
  2410. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestSoftDvppDecodeResizeJpegFail with incorrect size.";
  2411. // CSoftDvppDecodeResizeJpeg: size must be a vector of one or two values
  2412. std::shared_ptr<TensorOperation> soft_dvpp_decode_resize_jpeg_op1 = vision::SoftDvppDecodeResizeJpeg({});
  2413. EXPECT_EQ(soft_dvpp_decode_resize_jpeg_op1, nullptr);
  2414. // SoftDvppDecodeResizeJpeg: size must be a vector of one or two values
  2415. std::shared_ptr<TensorOperation> soft_dvpp_decode_resize_jpeg_op2 = vision::SoftDvppDecodeResizeJpeg({1, 2, 3});
  2416. EXPECT_EQ(soft_dvpp_decode_resize_jpeg_op2, nullptr);
  2417. // SoftDvppDecodeResizeJpeg: size must only contain positive integers
  2418. std::shared_ptr<TensorOperation> soft_dvpp_decode_resize_jpeg_op3 = vision::SoftDvppDecodeResizeJpeg({20, -20});
  2419. EXPECT_EQ(soft_dvpp_decode_resize_jpeg_op3, nullptr);
  2420. // SoftDvppDecodeResizeJpeg: size must only contain positive integers
  2421. std::shared_ptr<TensorOperation> soft_dvpp_decode_resize_jpeg_op4 = vision::SoftDvppDecodeResizeJpeg({0});
  2422. EXPECT_EQ(soft_dvpp_decode_resize_jpeg_op4, nullptr);
  2423. }
  2424. TEST_F(MindDataTestPipeline, TestUniformAugmentFail1) {
  2425. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestUniformAugmentFail1 with invalid num_ops parameter.";
  2426. // Create objects for the tensor ops
  2427. std::shared_ptr<TensorOperation> random_crop_op = vision::RandomCrop({28, 28});
  2428. EXPECT_NE(random_crop_op, nullptr);
  2429. std::shared_ptr<TensorOperation> center_crop_op = vision::CenterCrop({16, 16});
  2430. EXPECT_NE(center_crop_op, nullptr);
  2431. // UniformAug: num_ops must be greater than 0
  2432. std::shared_ptr<TensorOperation> uniform_aug_op1 = vision::UniformAugment({random_crop_op, center_crop_op}, 0);
  2433. EXPECT_EQ(uniform_aug_op1, nullptr);
  2434. // UniformAug: num_ops must be greater than 0
  2435. std::shared_ptr<TensorOperation> uniform_aug_op2 = vision::UniformAugment({random_crop_op, center_crop_op}, -1);
  2436. EXPECT_EQ(uniform_aug_op2, nullptr);
  2437. // UniformAug: num_ops is greater than transforms size
  2438. std::shared_ptr<TensorOperation> uniform_aug_op3 = vision::UniformAugment({random_crop_op, center_crop_op}, 3);
  2439. EXPECT_EQ(uniform_aug_op3, nullptr);
  2440. }
  2441. TEST_F(MindDataTestPipeline, TestUniformAugmentFail2) {
  2442. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestUniformAugmentFail2 with invalid transform.";
  2443. // UniformAug: transform ops must not be null
  2444. std::shared_ptr<TensorOperation> uniform_aug_op1 = vision::UniformAugment({vision::RandomCrop({-28})}, 1);
  2445. EXPECT_EQ(uniform_aug_op1, nullptr);
  2446. // UniformAug: transform ops must not be null
  2447. std::shared_ptr<TensorOperation> uniform_aug_op2 = vision::UniformAugment({vision::RandomCrop({28}), nullptr}, 2);
  2448. EXPECT_EQ(uniform_aug_op2, nullptr);
  2449. // UniformAug: transform list must not be empty
  2450. std::shared_ptr<TensorOperation> uniform_aug_op3 = vision::UniformAugment({}, 1);
  2451. EXPECT_EQ(uniform_aug_op3, nullptr);
  2452. }
  2453. TEST_F(MindDataTestPipeline, TestUniformAugWithOps) {
  2454. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestUniformAugWithOps.";
  2455. // Create a Mnist Dataset
  2456. std::string folder_path = datasets_root_path_ + "/testMnistData/";
  2457. std::shared_ptr<Dataset> ds = Mnist(folder_path, "all", RandomSampler(false, 20));
  2458. EXPECT_NE(ds, nullptr);
  2459. // Create a Repeat operation on ds
  2460. int32_t repeat_num = 1;
  2461. ds = ds->Repeat(repeat_num);
  2462. EXPECT_NE(ds, nullptr);
  2463. // Create objects for the tensor ops
  2464. std::shared_ptr<TensorOperation> resize_op = vision::Resize({30, 30});
  2465. EXPECT_NE(resize_op, nullptr);
  2466. std::shared_ptr<TensorOperation> random_crop_op = vision::RandomCrop({28, 28});
  2467. EXPECT_NE(random_crop_op, nullptr);
  2468. std::shared_ptr<TensorOperation> center_crop_op = vision::CenterCrop({16, 16});
  2469. EXPECT_NE(center_crop_op, nullptr);
  2470. std::shared_ptr<TensorOperation> uniform_aug_op = vision::UniformAugment({random_crop_op, center_crop_op}, 2);
  2471. EXPECT_NE(uniform_aug_op, nullptr);
  2472. // Create a Map operation on ds
  2473. ds = ds->Map({resize_op, uniform_aug_op});
  2474. EXPECT_NE(ds, nullptr);
  2475. // Create an iterator over the result of the above dataset
  2476. // This will trigger the creation of the Execution Tree and launch it.
  2477. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  2478. EXPECT_NE(iter, nullptr);
  2479. // Iterate the dataset and get each row
  2480. std::unordered_map<std::string, mindspore::MSTensor> row;
  2481. iter->GetNextRow(&row);
  2482. uint64_t i = 0;
  2483. while (row.size() != 0) {
  2484. i++;
  2485. // auto image = row["image"];
  2486. // MS_LOG(INFO) << "Tensor image shape: " << image->shape();
  2487. iter->GetNextRow(&row);
  2488. }
  2489. EXPECT_EQ(i, 20);
  2490. // Manually terminate the pipeline
  2491. iter->Stop();
  2492. }
  2493. TEST_F(MindDataTestPipeline, TestVisionOperationName) {
  2494. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestVisionOperationName.";
  2495. std::string correct_name;
  2496. // Create object for the tensor op, and check the name
  2497. std::shared_ptr<TensorOperation> random_vertical_flip_op = vision::RandomVerticalFlip(0.5);
  2498. correct_name = "RandomVerticalFlip";
  2499. EXPECT_EQ(correct_name, random_vertical_flip_op->Name());
  2500. // Create object for the tensor op, and check the name
  2501. std::shared_ptr<TensorOperation> softDvpp_decode_resize_jpeg_op = vision::SoftDvppDecodeResizeJpeg({1, 1});
  2502. correct_name = "SoftDvppDecodeResizeJpeg";
  2503. EXPECT_EQ(correct_name, softDvpp_decode_resize_jpeg_op->Name());
  2504. }