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

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