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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734
  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. // IR non-leaf nodes
  21. #include "minddata/dataset/engine/ir/datasetops/batch_node.h"
  22. #include "minddata/dataset/engine/ir/datasetops/bucket_batch_by_length_node.h"
  23. #include "minddata/dataset/engine/ir/datasetops/concat_node.h"
  24. #include "minddata/dataset/engine/ir/datasetops/map_node.h"
  25. #include "minddata/dataset/engine/ir/datasetops/project_node.h"
  26. #include "minddata/dataset/engine/ir/datasetops/rename_node.h"
  27. #include "minddata/dataset/engine/ir/datasetops/shuffle_node.h"
  28. #include "minddata/dataset/engine/ir/datasetops/skip_node.h"
  29. #include "minddata/dataset/engine/ir/datasetops/zip_node.h"
  30. // IR leaf nodes
  31. #include "minddata/dataset/engine/ir/datasetops/source/cifar10_node.h"
  32. #include "minddata/dataset/engine/ir/datasetops/source/image_folder_node.h"
  33. #include "minddata/dataset/engine/ir/datasetops/source/mnist_node.h"
  34. using namespace mindspore::dataset::api;
  35. using mindspore::dataset::BorderType;
  36. using mindspore::dataset::Tensor;
  37. class MindDataTestPipeline : public UT::DatasetOpTesting {
  38. protected:
  39. };
  40. // Tests for vision ops (in alphabetical order)
  41. TEST_F(MindDataTestPipeline, TestCenterCrop) {
  42. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestCenterCrop with single integer input.";
  43. // Create an ImageFolder Dataset
  44. std::string folder_path = datasets_root_path_ + "/testPK/data/";
  45. std::shared_ptr<Dataset> ds = ImageFolder(folder_path, true, RandomSampler(false, 5));
  46. EXPECT_NE(ds, nullptr);
  47. // Create a Repeat operation on ds
  48. int32_t repeat_num = 3;
  49. ds = ds->Repeat(repeat_num);
  50. EXPECT_NE(ds, nullptr);
  51. // Create centre crop object with square crop
  52. std::shared_ptr<TensorOperation> centre_out1 = vision::CenterCrop({30});
  53. EXPECT_NE(centre_out1, nullptr);
  54. // Create a Map operation on ds
  55. ds = ds->Map({centre_out1});
  56. EXPECT_NE(ds, nullptr);
  57. // Create a Batch operation on ds
  58. int32_t batch_size = 1;
  59. ds = ds->Batch(batch_size);
  60. EXPECT_NE(ds, nullptr);
  61. // Create an iterator over the result of the above dataset
  62. // This will trigger the creation of the Execution Tree and launch it.
  63. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  64. EXPECT_NE(iter, nullptr);
  65. // Iterate the dataset and get each row
  66. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  67. iter->GetNextRow(&row);
  68. uint64_t i = 0;
  69. while (row.size() != 0) {
  70. i++;
  71. auto image = row["image"];
  72. MS_LOG(INFO) << "Tensor image shape: " << image->shape();
  73. iter->GetNextRow(&row);
  74. }
  75. EXPECT_EQ(i, 15);
  76. // Manually terminate the pipeline
  77. iter->Stop();
  78. }
  79. TEST_F(MindDataTestPipeline, TestCenterCropFail) {
  80. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestCenterCrop with invalid parameters.";
  81. // center crop height value negative
  82. std::shared_ptr<TensorOperation> center_crop = mindspore::dataset::api::vision::CenterCrop({-32, 32});
  83. EXPECT_EQ(center_crop, nullptr);
  84. // center crop width value negative
  85. center_crop = mindspore::dataset::api::vision::CenterCrop({32, -32});
  86. EXPECT_EQ(center_crop, nullptr);
  87. // 0 value would result in nullptr
  88. center_crop = mindspore::dataset::api::vision::CenterCrop({0, 32});
  89. EXPECT_EQ(center_crop, nullptr);
  90. // center crop with 3 values
  91. center_crop = mindspore::dataset::api::vision::CenterCrop({10, 20, 30});
  92. EXPECT_EQ(center_crop, nullptr);
  93. }
  94. TEST_F(MindDataTestPipeline, TestCropFail) {
  95. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestCrop with invalid parameters.";
  96. // wrong width
  97. std::shared_ptr<TensorOperation> crop = mindspore::dataset::api::vision::Crop({0, 0}, {32, -32});
  98. EXPECT_EQ(crop, nullptr);
  99. // wrong height
  100. crop = mindspore::dataset::api::vision::Crop({0, 0}, {-32, -32});
  101. EXPECT_EQ(crop, nullptr);
  102. // zero height
  103. crop = mindspore::dataset::api::vision::Crop({0, 0}, {0, 32});
  104. EXPECT_EQ(crop, nullptr);
  105. }
  106. TEST_F(MindDataTestPipeline, TestCutMixBatchSuccess1) {
  107. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestCutMixBatchSuccess1.";
  108. // Testing CutMixBatch on a batch of CHW images
  109. // Create a Cifar10 Dataset
  110. std::string folder_path = datasets_root_path_ + "/testCifar10Data/";
  111. int number_of_classes = 10;
  112. std::shared_ptr<Dataset> ds = Cifar10(folder_path, "all", RandomSampler(false, 10));
  113. EXPECT_NE(ds, nullptr);
  114. // Create objects for the tensor ops
  115. std::shared_ptr<TensorOperation> hwc_to_chw = vision::HWC2CHW();
  116. EXPECT_NE(hwc_to_chw, nullptr);
  117. // Create a Map operation on ds
  118. ds = ds->Map({hwc_to_chw}, {"image"});
  119. EXPECT_NE(ds, nullptr);
  120. // Create a Batch operation on ds
  121. int32_t batch_size = 5;
  122. ds = ds->Batch(batch_size);
  123. EXPECT_NE(ds, nullptr);
  124. // Create objects for the tensor ops
  125. std::shared_ptr<TensorOperation> one_hot_op = transforms::OneHot(number_of_classes);
  126. EXPECT_NE(one_hot_op, nullptr);
  127. // Create a Map operation on ds
  128. ds = ds->Map({one_hot_op}, {"label"});
  129. EXPECT_NE(ds, nullptr);
  130. std::shared_ptr<TensorOperation> cutmix_batch_op =
  131. vision::CutMixBatch(mindspore::dataset::ImageBatchFormat::kNCHW, 1.0, 1.0);
  132. EXPECT_NE(cutmix_batch_op, nullptr);
  133. // Create a Map operation on ds
  134. ds = ds->Map({cutmix_batch_op}, {"image", "label"});
  135. EXPECT_NE(ds, nullptr);
  136. // Create an iterator over the result of the above dataset
  137. // This will trigger the creation of the Execution Tree and launch it.
  138. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  139. EXPECT_NE(iter, nullptr);
  140. // Iterate the dataset and get each row
  141. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  142. iter->GetNextRow(&row);
  143. uint64_t i = 0;
  144. while (row.size() != 0) {
  145. i++;
  146. auto image = row["image"];
  147. auto label = row["label"];
  148. MS_LOG(INFO) << "Tensor image shape: " << image->shape();
  149. MS_LOG(INFO) << "Label shape: " << label->shape();
  150. EXPECT_EQ(image->shape().AsVector().size() == 4 && batch_size == image->shape()[0] && 3 == image->shape()[1] &&
  151. 32 == image->shape()[2] && 32 == image->shape()[3],
  152. true);
  153. EXPECT_EQ(label->shape().AsVector().size() == 2 && batch_size == label->shape()[0] &&
  154. number_of_classes == label->shape()[1],
  155. true);
  156. iter->GetNextRow(&row);
  157. }
  158. EXPECT_EQ(i, 2);
  159. // Manually terminate the pipeline
  160. iter->Stop();
  161. }
  162. TEST_F(MindDataTestPipeline, TestCutMixBatchSuccess2) {
  163. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestCutMixBatchSuccess2.";
  164. // Calling CutMixBatch on a batch of HWC images with default values of alpha and prob
  165. // Create a Cifar10 Dataset
  166. std::string folder_path = datasets_root_path_ + "/testCifar10Data/";
  167. int number_of_classes = 10;
  168. std::shared_ptr<Dataset> ds = Cifar10(folder_path, "all", RandomSampler(false, 10));
  169. EXPECT_NE(ds, nullptr);
  170. // Create a Batch operation on ds
  171. int32_t batch_size = 5;
  172. ds = ds->Batch(batch_size);
  173. EXPECT_NE(ds, nullptr);
  174. // Create objects for the tensor ops
  175. std::shared_ptr<TensorOperation> one_hot_op = transforms::OneHot(number_of_classes);
  176. EXPECT_NE(one_hot_op, nullptr);
  177. // Create a Map operation on ds
  178. ds = ds->Map({one_hot_op}, {"label"});
  179. EXPECT_NE(ds, nullptr);
  180. std::shared_ptr<TensorOperation> cutmix_batch_op = vision::CutMixBatch(mindspore::dataset::ImageBatchFormat::kNHWC);
  181. EXPECT_NE(cutmix_batch_op, nullptr);
  182. // Create a Map operation on ds
  183. ds = ds->Map({cutmix_batch_op}, {"image", "label"});
  184. EXPECT_NE(ds, nullptr);
  185. // Create an iterator over the result of the above dataset
  186. // This will trigger the creation of the Execution Tree and launch it.
  187. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  188. EXPECT_NE(iter, nullptr);
  189. // Iterate the dataset and get each row
  190. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  191. iter->GetNextRow(&row);
  192. uint64_t i = 0;
  193. while (row.size() != 0) {
  194. i++;
  195. auto image = row["image"];
  196. auto label = row["label"];
  197. MS_LOG(INFO) << "Tensor image shape: " << image->shape();
  198. MS_LOG(INFO) << "Label shape: " << label->shape();
  199. EXPECT_EQ(image->shape().AsVector().size() == 4 && batch_size == image->shape()[0] && 32 == image->shape()[1] &&
  200. 32 == image->shape()[2] && 3 == image->shape()[3],
  201. true);
  202. EXPECT_EQ(label->shape().AsVector().size() == 2 && batch_size == label->shape()[0] &&
  203. number_of_classes == label->shape()[1],
  204. true);
  205. iter->GetNextRow(&row);
  206. }
  207. EXPECT_EQ(i, 2);
  208. // Manually terminate the pipeline
  209. iter->Stop();
  210. }
  211. TEST_F(MindDataTestPipeline, TestCutMixBatchFail1) {
  212. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestCutMixBatchFail1 with invalid negative alpha parameter.";
  213. // Create a Cifar10 Dataset
  214. std::string folder_path = datasets_root_path_ + "/testCifar10Data/";
  215. std::shared_ptr<Dataset> ds = Cifar10(folder_path, "all", RandomSampler(false, 10));
  216. EXPECT_NE(ds, nullptr);
  217. // Create a Batch operation on ds
  218. int32_t batch_size = 5;
  219. ds = ds->Batch(batch_size);
  220. EXPECT_NE(ds, nullptr);
  221. // Create objects for the tensor ops
  222. std::shared_ptr<TensorOperation> one_hot_op = transforms::OneHot(10);
  223. EXPECT_NE(one_hot_op, nullptr);
  224. // Create a Map operation on ds
  225. ds = ds->Map({one_hot_op}, {"label"});
  226. EXPECT_NE(ds, nullptr);
  227. std::shared_ptr<TensorOperation> cutmix_batch_op =
  228. vision::CutMixBatch(mindspore::dataset::ImageBatchFormat::kNHWC, -1, 0.5);
  229. EXPECT_EQ(cutmix_batch_op, nullptr);
  230. }
  231. TEST_F(MindDataTestPipeline, TestCutMixBatchFail2) {
  232. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestCutMixBatchFail2 with invalid negative prob parameter.";
  233. // Create a Cifar10 Dataset
  234. std::string folder_path = datasets_root_path_ + "/testCifar10Data/";
  235. std::shared_ptr<Dataset> ds = Cifar10(folder_path, "all", RandomSampler(false, 10));
  236. EXPECT_NE(ds, nullptr);
  237. // Create a Batch operation on ds
  238. int32_t batch_size = 5;
  239. ds = ds->Batch(batch_size);
  240. EXPECT_NE(ds, nullptr);
  241. // Create objects for the tensor ops
  242. std::shared_ptr<TensorOperation> one_hot_op = transforms::OneHot(10);
  243. EXPECT_NE(one_hot_op, nullptr);
  244. // Create a Map operation on ds
  245. ds = ds->Map({one_hot_op}, {"label"});
  246. EXPECT_NE(ds, nullptr);
  247. std::shared_ptr<TensorOperation> cutmix_batch_op =
  248. vision::CutMixBatch(mindspore::dataset::ImageBatchFormat::kNHWC, 1, -0.5);
  249. EXPECT_EQ(cutmix_batch_op, nullptr);
  250. }
  251. TEST_F(MindDataTestPipeline, TestCutMixBatchFail3) {
  252. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestCutMixBatchFail3 with invalid zero alpha parameter.";
  253. // Create a Cifar10 Dataset
  254. std::string folder_path = datasets_root_path_ + "/testCifar10Data/";
  255. std::shared_ptr<Dataset> ds = Cifar10(folder_path, "all", RandomSampler(false, 10));
  256. EXPECT_NE(ds, nullptr);
  257. // Create a Batch operation on ds
  258. int32_t batch_size = 5;
  259. ds = ds->Batch(batch_size);
  260. EXPECT_NE(ds, nullptr);
  261. // Create objects for the tensor ops
  262. std::shared_ptr<TensorOperation> one_hot_op = transforms::OneHot(10);
  263. EXPECT_NE(one_hot_op, nullptr);
  264. // Create a Map operation on ds
  265. ds = ds->Map({one_hot_op}, {"label"});
  266. EXPECT_NE(ds, nullptr);
  267. std::shared_ptr<TensorOperation> cutmix_batch_op =
  268. vision::CutMixBatch(mindspore::dataset::ImageBatchFormat::kNHWC, 0.0, 0.5);
  269. EXPECT_EQ(cutmix_batch_op, nullptr);
  270. }
  271. TEST_F(MindDataTestPipeline, TestCutMixBatchFail4) {
  272. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestCutMixBatchFail4 with invalid greater than 1 prob parameter.";
  273. // Create a Cifar10 Dataset
  274. std::string folder_path = datasets_root_path_ + "/testCifar10Data/";
  275. std::shared_ptr<Dataset> ds = Cifar10(folder_path, "all", RandomSampler(false, 10));
  276. EXPECT_NE(ds, nullptr);
  277. // Create a Batch operation on ds
  278. int32_t batch_size = 10;
  279. ds = ds->Batch(batch_size);
  280. EXPECT_NE(ds, nullptr);
  281. // Create objects for the tensor ops
  282. std::shared_ptr<TensorOperation> one_hot_op = transforms::OneHot(10);
  283. EXPECT_NE(one_hot_op, nullptr);
  284. // Create a Map operation on ds
  285. ds = ds->Map({one_hot_op}, {"label"});
  286. EXPECT_NE(ds, nullptr);
  287. std::shared_ptr<TensorOperation> cutmix_batch_op =
  288. vision::CutMixBatch(mindspore::dataset::ImageBatchFormat::kNHWC, 1, 1.5);
  289. EXPECT_EQ(cutmix_batch_op, nullptr);
  290. }
  291. TEST_F(MindDataTestPipeline, TestCutOutFail1) {
  292. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestCutOutFail1 with invalid parameters.";
  293. // Create object for the tensor op
  294. // Invalid negative length
  295. std::shared_ptr<TensorOperation> cutout_op = vision::CutOut(-10);
  296. EXPECT_EQ(cutout_op, nullptr);
  297. // Invalid negative number of patches
  298. cutout_op = vision::CutOut(10, -1);
  299. EXPECT_EQ(cutout_op, nullptr);
  300. }
  301. TEST_F(MindDataTestPipeline, DISABLED_TestCutOutFail2) {
  302. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestCutOutFail2 with invalid params, boundary cases.";
  303. // Create object for the tensor op
  304. // Invalid zero length
  305. std::shared_ptr<TensorOperation> cutout_op = vision::CutOut(0);
  306. EXPECT_EQ(cutout_op, nullptr);
  307. // Invalid zero number of patches
  308. cutout_op = vision::CutOut(10, 0);
  309. EXPECT_EQ(cutout_op, nullptr);
  310. }
  311. TEST_F(MindDataTestPipeline, TestCutOut) {
  312. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestCutOut.";
  313. // Create an ImageFolder Dataset
  314. std::string folder_path = datasets_root_path_ + "/testPK/data/";
  315. std::shared_ptr<Dataset> ds = ImageFolder(folder_path, true, RandomSampler(false, 10));
  316. EXPECT_NE(ds, nullptr);
  317. // Create a Repeat operation on ds
  318. int32_t repeat_num = 2;
  319. ds = ds->Repeat(repeat_num);
  320. EXPECT_NE(ds, nullptr);
  321. // Create objects for the tensor ops
  322. std::shared_ptr<TensorOperation> cut_out1 = vision::CutOut(30, 5);
  323. EXPECT_NE(cut_out1, nullptr);
  324. std::shared_ptr<TensorOperation> cut_out2 = vision::CutOut(30);
  325. EXPECT_NE(cut_out2, nullptr);
  326. // Create a Map operation on ds
  327. ds = ds->Map({cut_out1, cut_out2});
  328. EXPECT_NE(ds, nullptr);
  329. // Create a Batch operation on ds
  330. int32_t batch_size = 1;
  331. ds = ds->Batch(batch_size);
  332. EXPECT_NE(ds, nullptr);
  333. // Create an iterator over the result of the above dataset
  334. // This will trigger the creation of the Execution Tree and launch it.
  335. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  336. EXPECT_NE(iter, nullptr);
  337. // Iterate the dataset and get each row
  338. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  339. iter->GetNextRow(&row);
  340. uint64_t i = 0;
  341. while (row.size() != 0) {
  342. i++;
  343. auto image = row["image"];
  344. MS_LOG(INFO) << "Tensor image shape: " << image->shape();
  345. iter->GetNextRow(&row);
  346. }
  347. EXPECT_EQ(i, 20);
  348. // Manually terminate the pipeline
  349. iter->Stop();
  350. }
  351. TEST_F(MindDataTestPipeline, TestDecode) {
  352. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestDecode.";
  353. // Create an ImageFolder Dataset
  354. std::string folder_path = datasets_root_path_ + "/testPK/data/";
  355. std::shared_ptr<Dataset> ds = ImageFolder(folder_path, false, RandomSampler(false, 10));
  356. EXPECT_NE(ds, nullptr);
  357. // Create a Repeat operation on ds
  358. int32_t repeat_num = 2;
  359. ds = ds->Repeat(repeat_num);
  360. EXPECT_NE(ds, nullptr);
  361. // Create objects for the tensor ops
  362. std::shared_ptr<TensorOperation> decode = vision::Decode(true);
  363. EXPECT_NE(decode, nullptr);
  364. // Create a Map operation on ds
  365. ds = ds->Map({decode});
  366. EXPECT_NE(ds, nullptr);
  367. // Create a Batch operation on ds
  368. int32_t batch_size = 1;
  369. ds = ds->Batch(batch_size);
  370. EXPECT_NE(ds, nullptr);
  371. // Create an iterator over the result of the above dataset
  372. // This will trigger the creation of the Execution Tree and launch it.
  373. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  374. EXPECT_NE(iter, nullptr);
  375. // Iterate the dataset and get each row
  376. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  377. iter->GetNextRow(&row);
  378. uint64_t i = 0;
  379. while (row.size() != 0) {
  380. i++;
  381. auto image = row["image"];
  382. MS_LOG(INFO) << "Tensor image shape: " << image->shape();
  383. iter->GetNextRow(&row);
  384. }
  385. EXPECT_EQ(i, 20);
  386. // Manually terminate the pipeline
  387. iter->Stop();
  388. }
  389. TEST_F(MindDataTestPipeline, TestHwcToChw) {
  390. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestHwcToChw.";
  391. // Create an ImageFolder Dataset
  392. std::string folder_path = datasets_root_path_ + "/testPK/data/";
  393. std::shared_ptr<Dataset> ds = ImageFolder(folder_path, true, RandomSampler(false, 10));
  394. EXPECT_NE(ds, nullptr);
  395. // Create a Repeat operation on ds
  396. int32_t repeat_num = 2;
  397. ds = ds->Repeat(repeat_num);
  398. EXPECT_NE(ds, nullptr);
  399. // Create objects for the tensor ops
  400. std::shared_ptr<TensorOperation> channel_swap = vision::HWC2CHW();
  401. EXPECT_NE(channel_swap, nullptr);
  402. // Create a Map operation on ds
  403. ds = ds->Map({channel_swap});
  404. EXPECT_NE(ds, nullptr);
  405. // Create a Batch operation on ds
  406. int32_t batch_size = 1;
  407. ds = ds->Batch(batch_size);
  408. EXPECT_NE(ds, nullptr);
  409. // Create an iterator over the result of the above dataset
  410. // This will trigger the creation of the Execution Tree and launch it.
  411. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  412. EXPECT_NE(iter, nullptr);
  413. // Iterate the dataset and get each row
  414. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  415. iter->GetNextRow(&row);
  416. uint64_t i = 0;
  417. while (row.size() != 0) {
  418. i++;
  419. auto image = row["image"];
  420. MS_LOG(INFO) << "Tensor image shape: " << image->shape();
  421. // check if the image is in NCHW
  422. EXPECT_EQ(batch_size == image->shape()[0] && 3 == image->shape()[1] && 2268 == image->shape()[2] &&
  423. 4032 == image->shape()[3],
  424. true);
  425. iter->GetNextRow(&row);
  426. }
  427. EXPECT_EQ(i, 20);
  428. // Manually terminate the pipeline
  429. iter->Stop();
  430. }
  431. TEST_F(MindDataTestPipeline, TestMixUpBatchFail1) {
  432. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestMixUpBatchFail1 with negative alpha parameter.";
  433. // Create a Cifar10 Dataset
  434. std::string folder_path = datasets_root_path_ + "/testCifar10Data/";
  435. std::shared_ptr<Dataset> ds = Cifar10(folder_path, "all", RandomSampler(false, 10));
  436. EXPECT_NE(ds, nullptr);
  437. // Create a Batch operation on ds
  438. int32_t batch_size = 5;
  439. ds = ds->Batch(batch_size);
  440. EXPECT_NE(ds, nullptr);
  441. // Create objects for the tensor ops
  442. std::shared_ptr<TensorOperation> one_hot_op = transforms::OneHot(10);
  443. EXPECT_NE(one_hot_op, nullptr);
  444. // Create a Map operation on ds
  445. ds = ds->Map({one_hot_op}, {"label"});
  446. EXPECT_NE(ds, nullptr);
  447. std::shared_ptr<TensorOperation> mixup_batch_op = vision::MixUpBatch(-1);
  448. EXPECT_EQ(mixup_batch_op, nullptr);
  449. }
  450. TEST_F(MindDataTestPipeline, TestMixUpBatchFail2) {
  451. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestMixUpBatchFail2 with zero alpha parameter.";
  452. // Create a Cifar10 Dataset
  453. std::string folder_path = datasets_root_path_ + "/testCifar10Data/";
  454. std::shared_ptr<Dataset> ds = Cifar10(folder_path, "all", RandomSampler(false, 10));
  455. EXPECT_NE(ds, nullptr);
  456. // Create a Batch operation on ds
  457. int32_t batch_size = 5;
  458. ds = ds->Batch(batch_size);
  459. EXPECT_NE(ds, nullptr);
  460. // Create objects for the tensor ops
  461. std::shared_ptr<TensorOperation> one_hot_op = transforms::OneHot(10);
  462. EXPECT_NE(one_hot_op, nullptr);
  463. // Create a Map operation on ds
  464. ds = ds->Map({one_hot_op}, {"label"});
  465. EXPECT_NE(ds, nullptr);
  466. std::shared_ptr<TensorOperation> mixup_batch_op = vision::MixUpBatch(0.0);
  467. EXPECT_EQ(mixup_batch_op, nullptr);
  468. }
  469. TEST_F(MindDataTestPipeline, TestMixUpBatchSuccess1) {
  470. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestMixUpBatchSuccess1 with explicit alpha parameter.";
  471. // Create a Cifar10 Dataset
  472. std::string folder_path = datasets_root_path_ + "/testCifar10Data/";
  473. std::shared_ptr<Dataset> ds = Cifar10(folder_path, "all", RandomSampler(false, 10));
  474. EXPECT_NE(ds, nullptr);
  475. // Create a Batch operation on ds
  476. int32_t batch_size = 5;
  477. ds = ds->Batch(batch_size);
  478. EXPECT_NE(ds, nullptr);
  479. // Create objects for the tensor ops
  480. std::shared_ptr<TensorOperation> one_hot_op = transforms::OneHot(10);
  481. EXPECT_NE(one_hot_op, nullptr);
  482. // Create a Map operation on ds
  483. ds = ds->Map({one_hot_op}, {"label"});
  484. EXPECT_NE(ds, nullptr);
  485. std::shared_ptr<TensorOperation> mixup_batch_op = vision::MixUpBatch(2.0);
  486. EXPECT_NE(mixup_batch_op, nullptr);
  487. // Create a Map operation on ds
  488. ds = ds->Map({mixup_batch_op}, {"image", "label"});
  489. EXPECT_NE(ds, nullptr);
  490. // Create an iterator over the result of the above dataset
  491. // This will trigger the creation of the Execution Tree and launch it.
  492. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  493. EXPECT_NE(iter, nullptr);
  494. // Iterate the dataset and get each row
  495. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  496. iter->GetNextRow(&row);
  497. uint64_t i = 0;
  498. while (row.size() != 0) {
  499. i++;
  500. auto image = row["image"];
  501. MS_LOG(INFO) << "Tensor image shape: " << image->shape();
  502. iter->GetNextRow(&row);
  503. }
  504. EXPECT_EQ(i, 2);
  505. // Manually terminate the pipeline
  506. iter->Stop();
  507. }
  508. TEST_F(MindDataTestPipeline, TestMixUpBatchSuccess2) {
  509. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestMixUpBatchSuccess1 with default alpha parameter.";
  510. // Create a Cifar10 Dataset
  511. std::string folder_path = datasets_root_path_ + "/testCifar10Data/";
  512. std::shared_ptr<Dataset> ds = Cifar10(folder_path, "all", RandomSampler(false, 10));
  513. EXPECT_NE(ds, nullptr);
  514. // Create a Batch operation on ds
  515. int32_t batch_size = 5;
  516. ds = ds->Batch(batch_size);
  517. EXPECT_NE(ds, nullptr);
  518. // Create objects for the tensor ops
  519. std::shared_ptr<TensorOperation> one_hot_op = transforms::OneHot(10);
  520. EXPECT_NE(one_hot_op, nullptr);
  521. // Create a Map operation on ds
  522. ds = ds->Map({one_hot_op}, {"label"});
  523. EXPECT_NE(ds, nullptr);
  524. std::shared_ptr<TensorOperation> mixup_batch_op = vision::MixUpBatch();
  525. EXPECT_NE(mixup_batch_op, nullptr);
  526. // Create a Map operation on ds
  527. ds = ds->Map({mixup_batch_op}, {"image", "label"});
  528. EXPECT_NE(ds, nullptr);
  529. // Create an iterator over the result of the above dataset
  530. // This will trigger the creation of the Execution Tree and launch it.
  531. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  532. EXPECT_NE(iter, nullptr);
  533. // Iterate the dataset and get each row
  534. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  535. iter->GetNextRow(&row);
  536. uint64_t i = 0;
  537. while (row.size() != 0) {
  538. i++;
  539. auto image = row["image"];
  540. MS_LOG(INFO) << "Tensor image shape: " << image->shape();
  541. iter->GetNextRow(&row);
  542. }
  543. EXPECT_EQ(i, 2);
  544. // Manually terminate the pipeline
  545. iter->Stop();
  546. }
  547. TEST_F(MindDataTestPipeline, TestNormalize) {
  548. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestNormalize.";
  549. // Create an ImageFolder Dataset
  550. std::string folder_path = datasets_root_path_ + "/testPK/data/";
  551. std::shared_ptr<Dataset> ds = ImageFolder(folder_path, true, RandomSampler(false, 10));
  552. EXPECT_NE(ds, nullptr);
  553. // Create a Repeat operation on ds
  554. int32_t repeat_num = 2;
  555. ds = ds->Repeat(repeat_num);
  556. EXPECT_NE(ds, nullptr);
  557. // Create objects for the tensor ops
  558. std::shared_ptr<TensorOperation> normalize = vision::Normalize({121.0, 115.0, 100.0}, {70.0, 68.0, 71.0});
  559. EXPECT_NE(normalize, nullptr);
  560. // Create a Map operation on ds
  561. ds = ds->Map({normalize});
  562. EXPECT_NE(ds, nullptr);
  563. // Create a Batch operation on ds
  564. int32_t batch_size = 1;
  565. ds = ds->Batch(batch_size);
  566. EXPECT_NE(ds, nullptr);
  567. // Create an iterator over the result of the above dataset
  568. // This will trigger the creation of the Execution Tree and launch it.
  569. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  570. EXPECT_NE(iter, nullptr);
  571. // Iterate the dataset and get each row
  572. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  573. iter->GetNextRow(&row);
  574. uint64_t i = 0;
  575. while (row.size() != 0) {
  576. i++;
  577. auto image = row["image"];
  578. MS_LOG(INFO) << "Tensor image shape: " << image->shape();
  579. iter->GetNextRow(&row);
  580. }
  581. EXPECT_EQ(i, 20);
  582. // Manually terminate the pipeline
  583. iter->Stop();
  584. }
  585. TEST_F(MindDataTestPipeline, TestNormalizeFail) {
  586. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestNormalizeFail with invalid parameters.";
  587. // std value at 0.0
  588. std::shared_ptr<TensorOperation> normalize =
  589. mindspore::dataset::api::vision::Normalize({121.0, 115.0, 100.0}, {0.0, 68.0, 71.0});
  590. EXPECT_EQ(normalize, nullptr);
  591. // normalize with 2 values (not 3 values) for mean
  592. normalize = mindspore::dataset::api::vision::Normalize({121.0, 115.0}, {70.0, 68.0, 71.0});
  593. EXPECT_EQ(normalize, nullptr);
  594. // normalize with 2 values (not 3 values) for standard deviation
  595. normalize = mindspore::dataset::api::vision::Normalize({121.0, 115.0, 100.0}, {68.0, 71.0});
  596. EXPECT_EQ(normalize, nullptr);
  597. }
  598. TEST_F(MindDataTestPipeline, TestPad) {
  599. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestPad.";
  600. // Create an ImageFolder Dataset
  601. std::string folder_path = datasets_root_path_ + "/testPK/data/";
  602. std::shared_ptr<Dataset> ds = ImageFolder(folder_path, true, RandomSampler(false, 10));
  603. EXPECT_NE(ds, nullptr);
  604. // Create a Repeat operation on ds
  605. int32_t repeat_num = 2;
  606. ds = ds->Repeat(repeat_num);
  607. EXPECT_NE(ds, nullptr);
  608. // Create objects for the tensor ops
  609. std::shared_ptr<TensorOperation> pad_op1 = vision::Pad({1, 2, 3, 4}, {0}, BorderType::kSymmetric);
  610. EXPECT_NE(pad_op1, nullptr);
  611. std::shared_ptr<TensorOperation> pad_op2 = vision::Pad({1}, {1, 1, 1}, BorderType::kEdge);
  612. EXPECT_NE(pad_op2, nullptr);
  613. std::shared_ptr<TensorOperation> pad_op3 = vision::Pad({1, 4});
  614. EXPECT_NE(pad_op3, nullptr);
  615. // Create a Map operation on ds
  616. ds = ds->Map({pad_op1, pad_op2, pad_op3});
  617. EXPECT_NE(ds, nullptr);
  618. // Create a Batch operation on ds
  619. int32_t batch_size = 1;
  620. ds = ds->Batch(batch_size);
  621. EXPECT_NE(ds, nullptr);
  622. // Create an iterator over the result of the above dataset
  623. // This will trigger the creation of the Execution Tree and launch it.
  624. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  625. EXPECT_NE(iter, nullptr);
  626. // Iterate the dataset and get each row
  627. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  628. iter->GetNextRow(&row);
  629. uint64_t i = 0;
  630. while (row.size() != 0) {
  631. i++;
  632. auto image = row["image"];
  633. MS_LOG(INFO) << "Tensor image shape: " << image->shape();
  634. iter->GetNextRow(&row);
  635. }
  636. EXPECT_EQ(i, 20);
  637. // Manually terminate the pipeline
  638. iter->Stop();
  639. }
  640. TEST_F(MindDataTestPipeline, TestRandomAffineFail) {
  641. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestRandomAffineFail with invalid parameters.";
  642. // Create objects for the tensor ops
  643. std::shared_ptr<TensorOperation> affine = vision::RandomAffine({0.0, 0.0}, {});
  644. EXPECT_EQ(affine, nullptr);
  645. // Invalid number of values for translate
  646. affine = vision::RandomAffine({0.0, 0.0}, {1, 1, 1, 1, 1});
  647. EXPECT_EQ(affine, nullptr);
  648. // Invalid number of values for shear
  649. affine = vision::RandomAffine({30.0, 30.0}, {0.0, 0.0}, {2.0, 2.0}, {10.0});
  650. EXPECT_EQ(affine, nullptr);
  651. }
  652. TEST_F(MindDataTestPipeline, TestRandomAffineSuccess1) {
  653. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestRandomAffineSuccess1 with non-default parameters.";
  654. // Create an ImageFolder Dataset
  655. std::string folder_path = datasets_root_path_ + "/testPK/data/";
  656. std::shared_ptr<Dataset> ds = ImageFolder(folder_path, true, RandomSampler(false, 10));
  657. EXPECT_NE(ds, nullptr);
  658. // Create a Repeat operation on ds
  659. int32_t repeat_num = 2;
  660. ds = ds->Repeat(repeat_num);
  661. EXPECT_NE(ds, nullptr);
  662. // Create objects for the tensor ops
  663. std::shared_ptr<TensorOperation> affine =
  664. 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});
  665. EXPECT_NE(affine, nullptr);
  666. // Create a Map operation on ds
  667. ds = ds->Map({affine});
  668. EXPECT_NE(ds, nullptr);
  669. // Create a Batch operation on ds
  670. int32_t batch_size = 1;
  671. ds = ds->Batch(batch_size);
  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, 20);
  688. // Manually terminate the pipeline
  689. iter->Stop();
  690. }
  691. TEST_F(MindDataTestPipeline, TestRandomAffineSuccess2) {
  692. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestRandomAffineSuccess2 with default parameters.";
  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> affine = vision::RandomAffine({0.0, 0.0});
  703. EXPECT_NE(affine, nullptr);
  704. // Create a Map operation on ds
  705. ds = ds->Map({affine});
  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, TestRandomColor) {
  730. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestRandomColor with non-default parameters.";
  731. // Create an ImageFolder Dataset
  732. std::string folder_path = datasets_root_path_ + "/testPK/data/";
  733. std::shared_ptr<Dataset> ds = ImageFolder(folder_path, true, RandomSampler(false, 10));
  734. EXPECT_NE(ds, nullptr);
  735. // Create a Repeat operation on ds
  736. int32_t repeat_num = 2;
  737. ds = ds->Repeat(repeat_num);
  738. EXPECT_NE(ds, nullptr);
  739. // Create objects for the tensor ops
  740. // Valid case: Set lower bound and upper bound to be the same value zero
  741. std::shared_ptr<TensorOperation> random_color_op_1 = vision::RandomColor(0.0, 0.0);
  742. EXPECT_NE(random_color_op_1, nullptr);
  743. // Failure case: Set invalid lower bound greater than upper bound
  744. std::shared_ptr<TensorOperation> random_color_op_2 = vision::RandomColor(1.0, 0.1);
  745. EXPECT_EQ(random_color_op_2, nullptr);
  746. // Valid case: Set lower bound as zero and less than upper bound
  747. std::shared_ptr<TensorOperation> random_color_op_3 = vision::RandomColor(0.0, 1.1);
  748. EXPECT_NE(random_color_op_3, nullptr);
  749. // Failure case: Set invalid negative lower bound
  750. std::shared_ptr<TensorOperation> random_color_op_4 = vision::RandomColor(-0.5, 0.5);
  751. EXPECT_EQ(random_color_op_2, nullptr);
  752. // Create a Map operation on ds
  753. ds = ds->Map({random_color_op_1, random_color_op_3});
  754. EXPECT_NE(ds, nullptr);
  755. // Create a Batch operation on ds
  756. int32_t batch_size = 1;
  757. ds = ds->Batch(batch_size);
  758. EXPECT_NE(ds, nullptr);
  759. // Create an iterator over the result of the above dataset
  760. // This will trigger the creation of the Execution Tree and launch it.
  761. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  762. EXPECT_NE(iter, nullptr);
  763. // Iterate the dataset and get each row
  764. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  765. iter->GetNextRow(&row);
  766. uint64_t i = 0;
  767. while (row.size() != 0) {
  768. i++;
  769. auto image = row["image"];
  770. MS_LOG(INFO) << "Tensor image shape: " << image->shape();
  771. iter->GetNextRow(&row);
  772. }
  773. EXPECT_EQ(i, 20);
  774. // Manually terminate the pipeline
  775. iter->Stop();
  776. }
  777. TEST_F(MindDataTestPipeline, TestRandomColorAdjust) {
  778. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestRandomColorAdjust.";
  779. // Create an ImageFolder Dataset
  780. std::string folder_path = datasets_root_path_ + "/testPK/data/";
  781. std::shared_ptr<Dataset> ds = ImageFolder(folder_path, true, RandomSampler(false, 10));
  782. EXPECT_NE(ds, nullptr);
  783. // Create a Repeat operation on ds
  784. int32_t repeat_num = 2;
  785. ds = ds->Repeat(repeat_num);
  786. EXPECT_NE(ds, nullptr);
  787. // Create objects for the tensor ops
  788. // Use single value for vectors
  789. std::shared_ptr<TensorOperation> random_color_adjust1 = vision::RandomColorAdjust({1.0}, {0.0}, {0.5}, {0.5});
  790. EXPECT_NE(random_color_adjust1, nullptr);
  791. // Use same 2 values for vectors
  792. std::shared_ptr<TensorOperation> random_color_adjust2 =
  793. vision::RandomColorAdjust({1.0, 1.0}, {0.0, 0.0}, {0.5, 0.5}, {0.5, 0.5});
  794. EXPECT_NE(random_color_adjust2, nullptr);
  795. // Use different 2 value for vectors
  796. std::shared_ptr<TensorOperation> random_color_adjust3 =
  797. vision::RandomColorAdjust({0.5, 1.0}, {0.0, 0.5}, {0.25, 0.5}, {0.25, 0.5});
  798. EXPECT_NE(random_color_adjust3, nullptr);
  799. // Use default input values
  800. std::shared_ptr<TensorOperation> random_color_adjust4 = vision::RandomColorAdjust();
  801. EXPECT_NE(random_color_adjust4, nullptr);
  802. // Use subset of explictly set parameters
  803. std::shared_ptr<TensorOperation> random_color_adjust5 = vision::RandomColorAdjust({0.0, 0.5}, {0.25});
  804. EXPECT_NE(random_color_adjust5, nullptr);
  805. // Create a Map operation on ds
  806. ds = ds->Map(
  807. {random_color_adjust1, random_color_adjust2, random_color_adjust3, random_color_adjust4, random_color_adjust5});
  808. EXPECT_NE(ds, nullptr);
  809. // Create a Batch operation on ds
  810. int32_t batch_size = 1;
  811. ds = ds->Batch(batch_size);
  812. EXPECT_NE(ds, nullptr);
  813. // Create an iterator over the result of the above dataset
  814. // This will trigger the creation of the Execution Tree and launch it.
  815. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  816. EXPECT_NE(iter, nullptr);
  817. // Iterate the dataset and get each row
  818. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  819. iter->GetNextRow(&row);
  820. uint64_t i = 0;
  821. while (row.size() != 0) {
  822. i++;
  823. auto image = row["image"];
  824. MS_LOG(INFO) << "Tensor image shape: " << image->shape();
  825. iter->GetNextRow(&row);
  826. }
  827. EXPECT_EQ(i, 20);
  828. // Manually terminate the pipeline
  829. iter->Stop();
  830. }
  831. TEST_F(MindDataTestPipeline, DISABLED_TestRandomHorizontalFlipFail) {
  832. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestRandomHorizontalFlipFail with invalid parameters.";
  833. // Create object for the tensor op
  834. // Invalid zero input
  835. std::shared_ptr<TensorOperation> random_horizontal_flip_op = vision::RandomHorizontalFlip(0);
  836. EXPECT_EQ(random_horizontal_flip_op, nullptr);
  837. // Invalid >1 input
  838. random_horizontal_flip_op = vision::RandomHorizontalFlip(2);
  839. EXPECT_EQ(random_horizontal_flip_op, nullptr);
  840. }
  841. TEST_F(MindDataTestPipeline, TestRandomHorizontalAndVerticalFlip) {
  842. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestRandomHorizontalAndVerticalFlip for horizontal and vertical flips.";
  843. // Create an ImageFolder Dataset
  844. std::string folder_path = datasets_root_path_ + "/testPK/data/";
  845. std::shared_ptr<Dataset> ds = ImageFolder(folder_path, true, RandomSampler(false, 10));
  846. EXPECT_NE(ds, nullptr);
  847. // Create a Repeat operation on ds
  848. int32_t repeat_num = 2;
  849. ds = ds->Repeat(repeat_num);
  850. EXPECT_NE(ds, nullptr);
  851. // Create objects for the tensor ops
  852. std::shared_ptr<TensorOperation> random_vertical_flip_op = vision::RandomVerticalFlip(0.75);
  853. EXPECT_NE(random_vertical_flip_op, nullptr);
  854. std::shared_ptr<TensorOperation> random_horizontal_flip_op = vision::RandomHorizontalFlip(0.5);
  855. EXPECT_NE(random_horizontal_flip_op, nullptr);
  856. // Create a Map operation on ds
  857. ds = ds->Map({random_vertical_flip_op, random_horizontal_flip_op});
  858. EXPECT_NE(ds, nullptr);
  859. // Create a Batch operation on ds
  860. int32_t batch_size = 1;
  861. ds = ds->Batch(batch_size);
  862. EXPECT_NE(ds, nullptr);
  863. // Create an iterator over the result of the above dataset
  864. // This will trigger the creation of the Execution Tree and launch it.
  865. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  866. EXPECT_NE(iter, nullptr);
  867. // Iterate the dataset and get each row
  868. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  869. iter->GetNextRow(&row);
  870. uint64_t i = 0;
  871. while (row.size() != 0) {
  872. i++;
  873. auto image = row["image"];
  874. MS_LOG(INFO) << "Tensor image shape: " << image->shape();
  875. iter->GetNextRow(&row);
  876. }
  877. EXPECT_EQ(i, 20);
  878. // Manually terminate the pipeline
  879. iter->Stop();
  880. }
  881. TEST_F(MindDataTestPipeline, TestRandomPosterizeFail) {
  882. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestRandomPosterizeFail with invalid parameters.";
  883. // Create objects for the tensor ops
  884. // Invalid max > 8
  885. std::shared_ptr<TensorOperation> posterize = vision::RandomPosterize({1, 9});
  886. EXPECT_EQ(posterize, nullptr);
  887. // Invalid min < 1
  888. posterize = vision::RandomPosterize({0, 8});
  889. EXPECT_EQ(posterize, nullptr);
  890. // min > max
  891. posterize = vision::RandomPosterize({8, 1});
  892. EXPECT_EQ(posterize, nullptr);
  893. // empty
  894. posterize = vision::RandomPosterize({});
  895. EXPECT_EQ(posterize, nullptr);
  896. }
  897. TEST_F(MindDataTestPipeline, TestRandomPosterizeSuccess1) {
  898. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestRandomPosterizeSuccess1 with non-default parameters.";
  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 a Repeat operation on ds
  904. int32_t repeat_num = 2;
  905. ds = ds->Repeat(repeat_num);
  906. EXPECT_NE(ds, nullptr);
  907. // Create objects for the tensor ops
  908. std::shared_ptr<TensorOperation> posterize = vision::RandomPosterize({1, 4});
  909. EXPECT_NE(posterize, nullptr);
  910. // Create a Map operation on ds
  911. ds = ds->Map({posterize});
  912. EXPECT_NE(ds, nullptr);
  913. // Create a Batch operation on ds
  914. int32_t batch_size = 1;
  915. ds = ds->Batch(batch_size);
  916. EXPECT_NE(ds, nullptr);
  917. // Create an iterator over the result of the above dataset
  918. // This will trigger the creation of the Execution Tree and launch it.
  919. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  920. EXPECT_NE(iter, nullptr);
  921. // Iterate the dataset and get each row
  922. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  923. iter->GetNextRow(&row);
  924. uint64_t i = 0;
  925. while (row.size() != 0) {
  926. i++;
  927. auto image = row["image"];
  928. MS_LOG(INFO) << "Tensor image shape: " << image->shape();
  929. iter->GetNextRow(&row);
  930. }
  931. EXPECT_EQ(i, 20);
  932. // Manually terminate the pipeline
  933. iter->Stop();
  934. }
  935. TEST_F(MindDataTestPipeline, TestRandomPosterizeSuccess2) {
  936. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestRandomPosterizeSuccess2 with default parameters.";
  937. // Create an ImageFolder Dataset
  938. std::string folder_path = datasets_root_path_ + "/testPK/data/";
  939. std::shared_ptr<Dataset> ds = ImageFolder(folder_path, true, RandomSampler(false, 10));
  940. EXPECT_NE(ds, nullptr);
  941. // Create a Repeat operation on ds
  942. int32_t repeat_num = 2;
  943. ds = ds->Repeat(repeat_num);
  944. EXPECT_NE(ds, nullptr);
  945. // Create objects for the tensor ops
  946. std::shared_ptr<TensorOperation> posterize = vision::RandomPosterize();
  947. EXPECT_NE(posterize, nullptr);
  948. // Create a Map operation on ds
  949. ds = ds->Map({posterize});
  950. EXPECT_NE(ds, nullptr);
  951. // Create a Batch operation on ds
  952. int32_t batch_size = 1;
  953. ds = ds->Batch(batch_size);
  954. EXPECT_NE(ds, nullptr);
  955. // Create an iterator over the result of the above dataset
  956. // This will trigger the creation of the Execution Tree and launch it.
  957. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  958. EXPECT_NE(iter, nullptr);
  959. // Iterate the dataset and get each row
  960. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  961. iter->GetNextRow(&row);
  962. uint64_t i = 0;
  963. while (row.size() != 0) {
  964. i++;
  965. auto image = row["image"];
  966. MS_LOG(INFO) << "Tensor image shape: " << image->shape();
  967. iter->GetNextRow(&row);
  968. }
  969. EXPECT_EQ(i, 20);
  970. // Manually terminate the pipeline
  971. iter->Stop();
  972. }
  973. TEST_F(MindDataTestPipeline, TestRandomResizedCropSuccess1) {
  974. // Testing RandomResizedCrop with default values
  975. // Create a Cifar10 Dataset
  976. std::string folder_path = datasets_root_path_ + "/testCifar10Data/";
  977. std::shared_ptr<Dataset> ds = Cifar10(folder_path, "all", RandomSampler(false, 10));
  978. EXPECT_NE(ds, nullptr);
  979. // Create objects for the tensor ops
  980. std::shared_ptr<TensorOperation> random_resized_crop = vision::RandomResizedCrop({5});
  981. EXPECT_NE(random_resized_crop, nullptr);
  982. // Create a Map operation on ds
  983. ds = ds->Map({random_resized_crop}, {"image"});
  984. EXPECT_NE(ds, nullptr);
  985. // Create an iterator over the result of the above dataset
  986. // This will trigger the creation of the Execution Tree and launch it.
  987. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  988. EXPECT_NE(iter, nullptr);
  989. // Iterate the dataset and get each row
  990. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  991. iter->GetNextRow(&row);
  992. uint64_t i = 0;
  993. while (row.size() != 0) {
  994. i++;
  995. auto image = row["image"];
  996. MS_LOG(INFO) << "Tensor image shape: " << image->shape();
  997. EXPECT_EQ(image->shape()[0] == 5 && image->shape()[1] == 5, true);
  998. iter->GetNextRow(&row);
  999. }
  1000. EXPECT_EQ(i, 10);
  1001. // Manually terminate the pipeline
  1002. iter->Stop();
  1003. }
  1004. TEST_F(MindDataTestPipeline, TestRandomResizedCropSuccess2) {
  1005. // Testing RandomResizedCrop with non-default values
  1006. // Create a Cifar10 Dataset
  1007. std::string folder_path = datasets_root_path_ + "/testCifar10Data/";
  1008. std::shared_ptr<Dataset> ds = Cifar10(folder_path, "all", RandomSampler(false, 10));
  1009. EXPECT_NE(ds, nullptr);
  1010. // Create objects for the tensor ops
  1011. std::shared_ptr<TensorOperation> random_resized_crop =
  1012. vision::RandomResizedCrop({5, 10}, {0.25, 0.75}, {0.5, 1.25}, mindspore::dataset::InterpolationMode::kArea, 20);
  1013. EXPECT_NE(random_resized_crop, nullptr);
  1014. // Create a Map operation on ds
  1015. ds = ds->Map({random_resized_crop}, {"image"});
  1016. EXPECT_NE(ds, nullptr);
  1017. // Create an iterator over the result of the above dataset
  1018. // This will trigger the creation of the Execution Tree and launch it.
  1019. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  1020. EXPECT_NE(iter, nullptr);
  1021. // Iterate the dataset and get each row
  1022. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  1023. iter->GetNextRow(&row);
  1024. uint64_t i = 0;
  1025. while (row.size() != 0) {
  1026. i++;
  1027. auto image = row["image"];
  1028. MS_LOG(INFO) << "Tensor image shape: " << image->shape();
  1029. EXPECT_EQ(image->shape()[0] == 5 && image->shape()[1] == 10, true);
  1030. iter->GetNextRow(&row);
  1031. }
  1032. EXPECT_EQ(i, 10);
  1033. // Manually terminate the pipeline
  1034. iter->Stop();
  1035. }
  1036. TEST_F(MindDataTestPipeline, TestRandomResizedCropFail1) {
  1037. // This should fail because size has negative value
  1038. // Create a Cifar10 Dataset
  1039. std::string folder_path = datasets_root_path_ + "/testCifar10Data/";
  1040. std::shared_ptr<Dataset> ds = Cifar10(folder_path, "all", RandomSampler(false, 10));
  1041. EXPECT_NE(ds, nullptr);
  1042. // Create objects for the tensor ops
  1043. std::shared_ptr<TensorOperation> random_resized_crop = vision::RandomResizedCrop({5, -10});
  1044. EXPECT_EQ(random_resized_crop, nullptr);
  1045. }
  1046. TEST_F(MindDataTestPipeline, TestRandomResizedCropFail2) {
  1047. // This should fail because scale isn't in {min, max} format
  1048. // Create a Cifar10 Dataset
  1049. std::string folder_path = datasets_root_path_ + "/testCifar10Data/";
  1050. std::shared_ptr<Dataset> ds = Cifar10(folder_path, "all", RandomSampler(false, 10));
  1051. EXPECT_NE(ds, nullptr);
  1052. // Create objects for the tensor ops
  1053. std::shared_ptr<TensorOperation> random_resized_crop = vision::RandomResizedCrop({5, 10}, {4, 3});
  1054. EXPECT_EQ(random_resized_crop, nullptr);
  1055. }
  1056. TEST_F(MindDataTestPipeline, TestRandomResizedCropFail3) {
  1057. // This should fail because ratio isn't in {min, max} format
  1058. // Create a Cifar10 Dataset
  1059. std::string folder_path = datasets_root_path_ + "/testCifar10Data/";
  1060. std::shared_ptr<Dataset> ds = Cifar10(folder_path, "all", RandomSampler(false, 10));
  1061. EXPECT_NE(ds, nullptr);
  1062. // Create objects for the tensor ops
  1063. std::shared_ptr<TensorOperation> random_resized_crop = vision::RandomResizedCrop({5, 10}, {4, 5}, {7, 6});
  1064. EXPECT_EQ(random_resized_crop, nullptr);
  1065. }
  1066. TEST_F(MindDataTestPipeline, TestRandomResizedCropFail4) {
  1067. // This should fail because scale has a size of more than 2
  1068. // Create a Cifar10 Dataset
  1069. std::string folder_path = datasets_root_path_ + "/testCifar10Data/";
  1070. std::shared_ptr<Dataset> ds = Cifar10(folder_path, "all", RandomSampler(false, 10));
  1071. EXPECT_NE(ds, nullptr);
  1072. // Create objects for the tensor ops
  1073. std::shared_ptr<TensorOperation> random_resized_crop = vision::RandomResizedCrop({5, 10, 20}, {4, 5}, {7, 6});
  1074. EXPECT_EQ(random_resized_crop, nullptr);
  1075. }
  1076. TEST_F(MindDataTestPipeline, TestRandomRotation) {
  1077. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestRandomRotation.";
  1078. // Create an ImageFolder Dataset
  1079. std::string folder_path = datasets_root_path_ + "/testPK/data/";
  1080. std::shared_ptr<Dataset> ds = ImageFolder(folder_path, true, RandomSampler(false, 10));
  1081. EXPECT_NE(ds, nullptr);
  1082. // Create a Repeat operation on ds
  1083. int32_t repeat_num = 2;
  1084. ds = ds->Repeat(repeat_num);
  1085. EXPECT_NE(ds, nullptr);
  1086. // Create objects for the tensor ops
  1087. std::shared_ptr<TensorOperation> random_rotation_op = vision::RandomRotation({-180, 180});
  1088. EXPECT_NE(random_rotation_op, nullptr);
  1089. // Create a Map operation on ds
  1090. ds = ds->Map({random_rotation_op});
  1091. EXPECT_NE(ds, nullptr);
  1092. // Create a Batch operation on ds
  1093. int32_t batch_size = 1;
  1094. ds = ds->Batch(batch_size);
  1095. EXPECT_NE(ds, nullptr);
  1096. // Create an iterator over the result of the above dataset
  1097. // This will trigger the creation of the Execution Tree and launch it.
  1098. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  1099. EXPECT_NE(iter, nullptr);
  1100. // Iterate the dataset and get each row
  1101. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  1102. iter->GetNextRow(&row);
  1103. uint64_t i = 0;
  1104. while (row.size() != 0) {
  1105. i++;
  1106. auto image = row["image"];
  1107. MS_LOG(INFO) << "Tensor image shape: " << image->shape();
  1108. iter->GetNextRow(&row);
  1109. }
  1110. EXPECT_EQ(i, 20);
  1111. // Manually terminate the pipeline
  1112. iter->Stop();
  1113. }
  1114. TEST_F(MindDataTestPipeline, TestRandomSharpness) {
  1115. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestRandomSharpness.";
  1116. // Create an ImageFolder Dataset
  1117. std::string folder_path = datasets_root_path_ + "/testPK/data/";
  1118. std::shared_ptr<Dataset> ds = ImageFolder(folder_path, true, RandomSampler(false, 10));
  1119. EXPECT_NE(ds, nullptr);
  1120. // Create a Repeat operation on ds
  1121. int32_t repeat_num = 2;
  1122. ds = ds->Repeat(repeat_num);
  1123. EXPECT_NE(ds, nullptr);
  1124. // Create objects for the tensor ops
  1125. // Valid case: Input start degree and end degree
  1126. std::shared_ptr<TensorOperation> random_sharpness_op_1 = vision::RandomSharpness({0.4, 2.3});
  1127. EXPECT_NE(random_sharpness_op_1, nullptr);
  1128. // Failure case: Empty degrees vector
  1129. std::shared_ptr<TensorOperation> random_sharpness_op_2 = vision::RandomSharpness({});
  1130. EXPECT_EQ(random_sharpness_op_2, nullptr);
  1131. // Valid case: Use default input values
  1132. std::shared_ptr<TensorOperation> random_sharpness_op_3 = vision::RandomSharpness();
  1133. EXPECT_NE(random_sharpness_op_3, nullptr);
  1134. // Failure case: Single degree value
  1135. std::shared_ptr<TensorOperation> random_sharpness_op_4 = vision::RandomSharpness({0.1});
  1136. EXPECT_EQ(random_sharpness_op_4, nullptr);
  1137. // Create a Map operation on ds
  1138. ds = ds->Map({random_sharpness_op_1, random_sharpness_op_3});
  1139. EXPECT_NE(ds, nullptr);
  1140. // Create a Batch operation on ds
  1141. int32_t batch_size = 1;
  1142. ds = ds->Batch(batch_size);
  1143. EXPECT_NE(ds, nullptr);
  1144. // Create an iterator over the result of the above dataset
  1145. // This will trigger the creation of the Execution Tree and launch it.
  1146. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  1147. EXPECT_NE(iter, nullptr);
  1148. // Iterate the dataset and get each row
  1149. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  1150. iter->GetNextRow(&row);
  1151. uint64_t i = 0;
  1152. while (row.size() != 0) {
  1153. i++;
  1154. auto image = row["image"];
  1155. MS_LOG(INFO) << "Tensor image shape: " << image->shape();
  1156. iter->GetNextRow(&row);
  1157. }
  1158. EXPECT_EQ(i, 20);
  1159. // Manually terminate the pipeline
  1160. iter->Stop();
  1161. }
  1162. TEST_F(MindDataTestPipeline, TestRandomSolarizeSucess1) {
  1163. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestRandomSolarizeSucess1.";
  1164. // Create an ImageFolder Dataset
  1165. std::string folder_path = datasets_root_path_ + "/testPK/data/";
  1166. std::shared_ptr<Dataset> ds = ImageFolder(folder_path, true, RandomSampler(false, 10));
  1167. EXPECT_NE(ds, nullptr);
  1168. // Create objects for the tensor ops
  1169. std::vector<uint8_t> threshold = {10, 100};
  1170. std::shared_ptr<TensorOperation> random_solarize = mindspore::dataset::api::vision::RandomSolarize(threshold);
  1171. EXPECT_NE(random_solarize, nullptr);
  1172. // Create a Map operation on ds
  1173. ds = ds->Map({random_solarize});
  1174. EXPECT_NE(ds, nullptr);
  1175. // Create an iterator over the result of the above dataset
  1176. // This will trigger the creation of the Execution Tree and launch it.
  1177. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  1178. EXPECT_NE(iter, nullptr);
  1179. // Iterate the dataset and get each row
  1180. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  1181. iter->GetNextRow(&row);
  1182. uint64_t i = 0;
  1183. while (row.size() != 0) {
  1184. i++;
  1185. auto image = row["image"];
  1186. MS_LOG(INFO) << "Tensor image shape: " << image->shape();
  1187. iter->GetNextRow(&row);
  1188. }
  1189. EXPECT_EQ(i, 10);
  1190. // Manually terminate the pipeline
  1191. iter->Stop();
  1192. }
  1193. TEST_F(MindDataTestPipeline, TestRandomSolarizeSucess2) {
  1194. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestRandomSolarizeSuccess2 with default parameters.";
  1195. // Create an ImageFolder Dataset
  1196. std::string folder_path = datasets_root_path_ + "/testPK/data/";
  1197. std::shared_ptr<Dataset> ds = ImageFolder(folder_path, true, RandomSampler(false, 10));
  1198. EXPECT_NE(ds, nullptr);
  1199. // Create objects for the tensor ops
  1200. std::shared_ptr<TensorOperation> random_solarize = mindspore::dataset::api::vision::RandomSolarize();
  1201. EXPECT_NE(random_solarize, nullptr);
  1202. // Create a Map operation on ds
  1203. ds = ds->Map({random_solarize});
  1204. EXPECT_NE(ds, nullptr);
  1205. // Create an iterator over the result of the above dataset
  1206. // This will trigger the creation of the Execution Tree and launch it.
  1207. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  1208. EXPECT_NE(iter, nullptr);
  1209. // Iterate the dataset and get each row
  1210. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  1211. iter->GetNextRow(&row);
  1212. uint64_t i = 0;
  1213. while (row.size() != 0) {
  1214. i++;
  1215. auto image = row["image"];
  1216. MS_LOG(INFO) << "Tensor image shape: " << image->shape();
  1217. iter->GetNextRow(&row);
  1218. }
  1219. EXPECT_EQ(i, 10);
  1220. // Manually terminate the pipeline
  1221. iter->Stop();
  1222. }
  1223. TEST_F(MindDataTestPipeline, TestRandomSolarizeFail) {
  1224. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestRandomSolarizeFail with invalid parameters.";
  1225. std::vector<uint8_t> threshold = {13, 1};
  1226. std::shared_ptr<TensorOperation> random_solarize = mindspore::dataset::api::vision::RandomSolarize(threshold);
  1227. EXPECT_EQ(random_solarize, nullptr);
  1228. threshold = {1, 2, 3};
  1229. random_solarize = mindspore::dataset::api::vision::RandomSolarize(threshold);
  1230. EXPECT_EQ(random_solarize, nullptr);
  1231. threshold = {1};
  1232. random_solarize = mindspore::dataset::api::vision::RandomSolarize(threshold);
  1233. EXPECT_EQ(random_solarize, nullptr);
  1234. threshold = {};
  1235. random_solarize = mindspore::dataset::api::vision::RandomSolarize(threshold);
  1236. EXPECT_EQ(random_solarize, nullptr);
  1237. }
  1238. TEST_F(MindDataTestPipeline, DISABLED_TestRandomVerticalFlipFail) {
  1239. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestRandomVerticalFlipFail with invalid parameters.";
  1240. // Create object for the tensor op
  1241. // Invalid zero input
  1242. std::shared_ptr<TensorOperation> random_vertical_flip_op = vision::RandomVerticalFlip(0);
  1243. EXPECT_EQ(random_vertical_flip_op, nullptr);
  1244. // Invalid >1 input
  1245. random_vertical_flip_op = vision::RandomVerticalFlip(1.1);
  1246. EXPECT_EQ(random_vertical_flip_op, nullptr);
  1247. }
  1248. TEST_F(MindDataTestPipeline, TestResizeFail) {
  1249. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestResize with invalid parameters.";
  1250. // negative resize value
  1251. std::shared_ptr<TensorOperation> resize_op = mindspore::dataset::api::vision::Resize({30, -30});
  1252. EXPECT_EQ(resize_op, nullptr);
  1253. // zero resize value
  1254. resize_op = mindspore::dataset::api::vision::Resize({0, 30});
  1255. EXPECT_EQ(resize_op, nullptr);
  1256. // resize with 3 values
  1257. resize_op = mindspore::dataset::api::vision::Resize({30, 20, 10});
  1258. EXPECT_EQ(resize_op, nullptr);
  1259. }
  1260. TEST_F(MindDataTestPipeline, TestResize1) {
  1261. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestResize1 with single integer input.";
  1262. // Create an ImageFolder Dataset
  1263. std::string folder_path = datasets_root_path_ + "/testPK/data/";
  1264. std::shared_ptr<Dataset> ds = ImageFolder(folder_path, true, RandomSampler(false, 6));
  1265. EXPECT_NE(ds, nullptr);
  1266. // Create a Repeat operation on ds
  1267. int32_t repeat_num = 4;
  1268. ds = ds->Repeat(repeat_num);
  1269. EXPECT_NE(ds, nullptr);
  1270. // Create resize object with single integer input
  1271. std::shared_ptr<TensorOperation> resize_op = vision::Resize({30});
  1272. EXPECT_NE(resize_op, nullptr);
  1273. // Create a Map operation on ds
  1274. ds = ds->Map({resize_op});
  1275. EXPECT_NE(ds, nullptr);
  1276. // Create a Batch operation on ds
  1277. int32_t batch_size = 1;
  1278. ds = ds->Batch(batch_size);
  1279. EXPECT_NE(ds, nullptr);
  1280. // Create an iterator over the result of the above dataset
  1281. // This will trigger the creation of the Execution Tree and launch it.
  1282. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  1283. EXPECT_NE(iter, nullptr);
  1284. // Iterate the dataset and get each row
  1285. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  1286. iter->GetNextRow(&row);
  1287. uint64_t i = 0;
  1288. while (row.size() != 0) {
  1289. i++;
  1290. auto image = row["image"];
  1291. MS_LOG(INFO) << "Tensor image shape: " << image->shape();
  1292. iter->GetNextRow(&row);
  1293. }
  1294. EXPECT_EQ(i, 24);
  1295. // Manually terminate the pipeline
  1296. iter->Stop();
  1297. }
  1298. TEST_F(MindDataTestPipeline, DISABLED_TestUniformAugmentFail1) {
  1299. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestUniformAugmentFail1 with invalid zero num_ops parameter.";
  1300. // Create a Mnist Dataset
  1301. std::string folder_path = datasets_root_path_ + "/testMnistData/";
  1302. std::shared_ptr<Dataset> ds = Mnist(folder_path, "all", RandomSampler(false, 20));
  1303. EXPECT_NE(ds, nullptr);
  1304. // Create objects for the tensor ops
  1305. std::shared_ptr<TensorOperation> random_crop_op = vision::RandomCrop({28, 28});
  1306. EXPECT_NE(random_crop_op, nullptr);
  1307. std::shared_ptr<TensorOperation> center_crop_op = vision::CenterCrop({16, 16});
  1308. EXPECT_NE(center_crop_op, nullptr);
  1309. // Try UniformAugment with invalid zero num_ops value
  1310. std::shared_ptr<TensorOperation> uniform_aug_op = vision::UniformAugment({random_crop_op, center_crop_op}, 0);
  1311. EXPECT_EQ(uniform_aug_op, nullptr);
  1312. }
  1313. TEST_F(MindDataTestPipeline, DISABLED_TestUniformAugmentFail2) {
  1314. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestUniformAugmentFail2 with invalid negative num_ops parameter.";
  1315. // Create a Mnist Dataset
  1316. std::string folder_path = datasets_root_path_ + "/testMnistData/";
  1317. std::shared_ptr<Dataset> ds = Mnist(folder_path, "all", RandomSampler(false, 20));
  1318. EXPECT_NE(ds, nullptr);
  1319. // Create objects for the tensor ops
  1320. std::shared_ptr<TensorOperation> random_crop_op = vision::RandomCrop({28, 28});
  1321. EXPECT_NE(random_crop_op, nullptr);
  1322. std::shared_ptr<TensorOperation> center_crop_op = vision::CenterCrop({16, 16});
  1323. EXPECT_NE(center_crop_op, nullptr);
  1324. // Try UniformAugment with invalid negative num_ops value
  1325. std::shared_ptr<TensorOperation> uniform_aug_op = vision::UniformAugment({random_crop_op, center_crop_op}, -1);
  1326. EXPECT_EQ(uniform_aug_op, nullptr);
  1327. }
  1328. TEST_F(MindDataTestPipeline, TestUniformAugWithOps) {
  1329. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestUniformAugWithOps.";
  1330. // Create a Mnist Dataset
  1331. std::string folder_path = datasets_root_path_ + "/testMnistData/";
  1332. std::shared_ptr<Dataset> ds = Mnist(folder_path, "all", RandomSampler(false, 20));
  1333. EXPECT_NE(ds, nullptr);
  1334. // Create a Repeat operation on ds
  1335. int32_t repeat_num = 1;
  1336. ds = ds->Repeat(repeat_num);
  1337. EXPECT_NE(ds, nullptr);
  1338. // Create objects for the tensor ops
  1339. std::shared_ptr<TensorOperation> resize_op = vision::Resize({30, 30});
  1340. EXPECT_NE(resize_op, nullptr);
  1341. std::shared_ptr<TensorOperation> random_crop_op = vision::RandomCrop({28, 28});
  1342. EXPECT_NE(random_crop_op, nullptr);
  1343. std::shared_ptr<TensorOperation> center_crop_op = vision::CenterCrop({16, 16});
  1344. EXPECT_NE(center_crop_op, nullptr);
  1345. std::shared_ptr<TensorOperation> uniform_aug_op = vision::UniformAugment({random_crop_op, center_crop_op}, 2);
  1346. EXPECT_NE(uniform_aug_op, nullptr);
  1347. // Create a Map operation on ds
  1348. ds = ds->Map({resize_op, uniform_aug_op});
  1349. EXPECT_NE(ds, nullptr);
  1350. // Create an iterator over the result of the above dataset
  1351. // This will trigger the creation of the Execution Tree and launch it.
  1352. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  1353. EXPECT_NE(iter, nullptr);
  1354. // Iterate the dataset and get each row
  1355. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  1356. iter->GetNextRow(&row);
  1357. uint64_t i = 0;
  1358. while (row.size() != 0) {
  1359. i++;
  1360. auto image = row["image"];
  1361. MS_LOG(INFO) << "Tensor image shape: " << image->shape();
  1362. iter->GetNextRow(&row);
  1363. }
  1364. EXPECT_EQ(i, 20);
  1365. // Manually terminate the pipeline
  1366. iter->Stop();
  1367. }