You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

c_api_test.cc 65 kB

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