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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519
  1. /**
  2. * Copyright 2020 Huawei Technologies Co., Ltd
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "common/common.h"
  17. #include "minddata/dataset/include/datasets.h"
  18. #include "minddata/dataset/include/transforms.h"
  19. #include "minddata/dataset/include/vision.h"
  20. using namespace mindspore::dataset::api;
  21. using mindspore::dataset::BorderType;
  22. using mindspore::dataset::Tensor;
  23. class MindDataTestPipeline : public UT::DatasetOpTesting {
  24. protected:
  25. };
  26. TEST_F(MindDataTestPipeline, TestCutMixBatchSuccess1) {
  27. // Testing CutMixBatch on a batch of CHW images
  28. // Create a Cifar10 Dataset
  29. std::string folder_path = datasets_root_path_ + "/testCifar10Data/";
  30. int number_of_classes = 10;
  31. std::shared_ptr<Dataset> ds = Cifar10(folder_path, "all", RandomSampler(false, 10));
  32. EXPECT_NE(ds, nullptr);
  33. // Create objects for the tensor ops
  34. std::shared_ptr<TensorOperation> hwc_to_chw = vision::HWC2CHW();
  35. EXPECT_NE(hwc_to_chw, nullptr);
  36. // Create a Map operation on ds
  37. ds = ds->Map({hwc_to_chw}, {"image"});
  38. EXPECT_NE(ds, nullptr);
  39. // Create a Batch operation on ds
  40. int32_t batch_size = 5;
  41. ds = ds->Batch(batch_size);
  42. EXPECT_NE(ds, nullptr);
  43. // Create objects for the tensor ops
  44. std::shared_ptr<TensorOperation> one_hot_op = transforms::OneHot(number_of_classes);
  45. EXPECT_NE(one_hot_op, nullptr);
  46. // Create a Map operation on ds
  47. ds = ds->Map({one_hot_op}, {"label"});
  48. EXPECT_NE(ds, nullptr);
  49. std::shared_ptr<TensorOperation> cutmix_batch_op =
  50. vision::CutMixBatch(mindspore::dataset::ImageBatchFormat::kNCHW, 1.0, 1.0);
  51. EXPECT_NE(cutmix_batch_op, nullptr);
  52. // Create a Map operation on ds
  53. ds = ds->Map({cutmix_batch_op}, {"image", "label"});
  54. EXPECT_NE(ds, nullptr);
  55. // Create an iterator over the result of the above dataset
  56. // This will trigger the creation of the Execution Tree and launch it.
  57. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  58. EXPECT_NE(iter, nullptr);
  59. // Iterate the dataset and get each row
  60. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  61. iter->GetNextRow(&row);
  62. uint64_t i = 0;
  63. while (row.size() != 0) {
  64. i++;
  65. auto image = row["image"];
  66. auto label = row["label"];
  67. MS_LOG(INFO) << "Tensor image shape: " << image->shape();
  68. MS_LOG(INFO) << "Label shape: " << label->shape();
  69. EXPECT_EQ(image->shape().AsVector().size() == 4 && batch_size == image->shape()[0] && 3 == image->shape()[1] &&
  70. 32 == image->shape()[2] && 32 == image->shape()[3],
  71. true);
  72. EXPECT_EQ(label->shape().AsVector().size() == 2 && batch_size == label->shape()[0] &&
  73. number_of_classes == label->shape()[1],
  74. true);
  75. iter->GetNextRow(&row);
  76. }
  77. EXPECT_EQ(i, 2);
  78. // Manually terminate the pipeline
  79. iter->Stop();
  80. }
  81. TEST_F(MindDataTestPipeline, TestCutMixBatchSuccess2) {
  82. // Calling CutMixBatch on a batch of HWC images with default values of alpha and prob
  83. // Create a Cifar10 Dataset
  84. std::string folder_path = datasets_root_path_ + "/testCifar10Data/";
  85. int number_of_classes = 10;
  86. std::shared_ptr<Dataset> ds = Cifar10(folder_path, "all", RandomSampler(false, 10));
  87. EXPECT_NE(ds, nullptr);
  88. // Create a Batch operation on ds
  89. int32_t batch_size = 5;
  90. ds = ds->Batch(batch_size);
  91. EXPECT_NE(ds, nullptr);
  92. // Create objects for the tensor ops
  93. std::shared_ptr<TensorOperation> one_hot_op = transforms::OneHot(number_of_classes);
  94. EXPECT_NE(one_hot_op, nullptr);
  95. // Create a Map operation on ds
  96. ds = ds->Map({one_hot_op}, {"label"});
  97. EXPECT_NE(ds, nullptr);
  98. std::shared_ptr<TensorOperation> cutmix_batch_op = vision::CutMixBatch(mindspore::dataset::ImageBatchFormat::kNHWC);
  99. EXPECT_NE(cutmix_batch_op, nullptr);
  100. // Create a Map operation on ds
  101. ds = ds->Map({cutmix_batch_op}, {"image", "label"});
  102. EXPECT_NE(ds, nullptr);
  103. // Create an iterator over the result of the above dataset
  104. // This will trigger the creation of the Execution Tree and launch it.
  105. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  106. EXPECT_NE(iter, nullptr);
  107. // Iterate the dataset and get each row
  108. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  109. iter->GetNextRow(&row);
  110. uint64_t i = 0;
  111. while (row.size() != 0) {
  112. i++;
  113. auto image = row["image"];
  114. auto label = row["label"];
  115. MS_LOG(INFO) << "Tensor image shape: " << image->shape();
  116. MS_LOG(INFO) << "Label shape: " << label->shape();
  117. EXPECT_EQ(image->shape().AsVector().size() == 4 && batch_size == image->shape()[0] && 32 == image->shape()[1] &&
  118. 32 == image->shape()[2] && 3 == image->shape()[3],
  119. true);
  120. EXPECT_EQ(label->shape().AsVector().size() == 2 && batch_size == label->shape()[0] &&
  121. number_of_classes == label->shape()[1],
  122. true);
  123. iter->GetNextRow(&row);
  124. }
  125. EXPECT_EQ(i, 2);
  126. // Manually terminate the pipeline
  127. iter->Stop();
  128. }
  129. TEST_F(MindDataTestPipeline, TestCutMixBatchFail1) {
  130. // Must fail because alpha can't be negative
  131. // Create a Cifar10 Dataset
  132. std::string folder_path = datasets_root_path_ + "/testCifar10Data/";
  133. std::shared_ptr<Dataset> ds = Cifar10(folder_path, "all", RandomSampler(false, 10));
  134. EXPECT_NE(ds, nullptr);
  135. // Create a Batch operation on ds
  136. int32_t batch_size = 5;
  137. ds = ds->Batch(batch_size);
  138. EXPECT_NE(ds, nullptr);
  139. // Create objects for the tensor ops
  140. std::shared_ptr<TensorOperation> one_hot_op = transforms::OneHot(10);
  141. EXPECT_NE(one_hot_op, nullptr);
  142. // Create a Map operation on ds
  143. ds = ds->Map({one_hot_op}, {"label"});
  144. EXPECT_NE(ds, nullptr);
  145. std::shared_ptr<TensorOperation> cutmix_batch_op =
  146. vision::CutMixBatch(mindspore::dataset::ImageBatchFormat::kNHWC, -1, 0.5);
  147. EXPECT_EQ(cutmix_batch_op, nullptr);
  148. }
  149. TEST_F(MindDataTestPipeline, TestCutMixBatchFail2) {
  150. // Must fail because prob can't be negative
  151. // Create a Cifar10 Dataset
  152. std::string folder_path = datasets_root_path_ + "/testCifar10Data/";
  153. std::shared_ptr<Dataset> ds = Cifar10(folder_path, "all", RandomSampler(false, 10));
  154. EXPECT_NE(ds, nullptr);
  155. // Create a Batch operation on ds
  156. int32_t batch_size = 5;
  157. ds = ds->Batch(batch_size);
  158. EXPECT_NE(ds, nullptr);
  159. // Create objects for the tensor ops
  160. std::shared_ptr<TensorOperation> one_hot_op = transforms::OneHot(10);
  161. EXPECT_NE(one_hot_op, nullptr);
  162. // Create a Map operation on ds
  163. ds = ds->Map({one_hot_op}, {"label"});
  164. EXPECT_NE(ds, nullptr);
  165. std::shared_ptr<TensorOperation> cutmix_batch_op =
  166. vision::CutMixBatch(mindspore::dataset::ImageBatchFormat::kNHWC, 1, -0.5);
  167. EXPECT_EQ(cutmix_batch_op, nullptr);
  168. }
  169. TEST_F(MindDataTestPipeline, TestCutMixBatchFail3) {
  170. // Must fail because alpha can't be zero
  171. // Create a Cifar10 Dataset
  172. std::string folder_path = datasets_root_path_ + "/testCifar10Data/";
  173. std::shared_ptr<Dataset> ds = Cifar10(folder_path, "all", RandomSampler(false, 10));
  174. EXPECT_NE(ds, nullptr);
  175. // Create a Batch operation on ds
  176. int32_t batch_size = 5;
  177. ds = ds->Batch(batch_size);
  178. EXPECT_NE(ds, nullptr);
  179. // Create objects for the tensor ops
  180. std::shared_ptr<TensorOperation> one_hot_op = transforms::OneHot(10);
  181. EXPECT_NE(one_hot_op, nullptr);
  182. // Create a Map operation on ds
  183. ds = ds->Map({one_hot_op}, {"label"});
  184. EXPECT_NE(ds, nullptr);
  185. std::shared_ptr<TensorOperation> cutmix_batch_op =
  186. vision::CutMixBatch(mindspore::dataset::ImageBatchFormat::kNHWC, 0.0, 0.5);
  187. EXPECT_EQ(cutmix_batch_op, nullptr);
  188. }
  189. TEST_F(MindDataTestPipeline, TestCutOut) {
  190. // Create an ImageFolder Dataset
  191. std::string folder_path = datasets_root_path_ + "/testPK/data/";
  192. std::shared_ptr<Dataset> ds = ImageFolder(folder_path, true, RandomSampler(false, 10));
  193. EXPECT_NE(ds, nullptr);
  194. // Create a Repeat operation on ds
  195. int32_t repeat_num = 2;
  196. ds = ds->Repeat(repeat_num);
  197. EXPECT_NE(ds, nullptr);
  198. // Create objects for the tensor ops
  199. std::shared_ptr<TensorOperation> cut_out1 = vision::CutOut(30, 5);
  200. EXPECT_NE(cut_out1, nullptr);
  201. std::shared_ptr<TensorOperation> cut_out2 = vision::CutOut(30);
  202. EXPECT_NE(cut_out2, nullptr);
  203. // Create a Map operation on ds
  204. ds = ds->Map({cut_out1, cut_out2});
  205. EXPECT_NE(ds, nullptr);
  206. // Create a Batch operation on ds
  207. int32_t batch_size = 1;
  208. ds = ds->Batch(batch_size);
  209. EXPECT_NE(ds, nullptr);
  210. // Create an iterator over the result of the above dataset
  211. // This will trigger the creation of the Execution Tree and launch it.
  212. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  213. EXPECT_NE(iter, nullptr);
  214. // Iterate the dataset and get each row
  215. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  216. iter->GetNextRow(&row);
  217. uint64_t i = 0;
  218. while (row.size() != 0) {
  219. i++;
  220. auto image = row["image"];
  221. MS_LOG(INFO) << "Tensor image shape: " << image->shape();
  222. iter->GetNextRow(&row);
  223. }
  224. EXPECT_EQ(i, 20);
  225. // Manually terminate the pipeline
  226. iter->Stop();
  227. }
  228. TEST_F(MindDataTestPipeline, TestDecode) {
  229. // Create an ImageFolder Dataset
  230. std::string folder_path = datasets_root_path_ + "/testPK/data/";
  231. std::shared_ptr<Dataset> ds = ImageFolder(folder_path, false, RandomSampler(false, 10));
  232. EXPECT_NE(ds, nullptr);
  233. // Create a Repeat operation on ds
  234. int32_t repeat_num = 2;
  235. ds = ds->Repeat(repeat_num);
  236. EXPECT_NE(ds, nullptr);
  237. // Create objects for the tensor ops
  238. std::shared_ptr<TensorOperation> decode = vision::Decode(true);
  239. EXPECT_NE(decode, nullptr);
  240. // Create a Map operation on ds
  241. ds = ds->Map({decode});
  242. EXPECT_NE(ds, nullptr);
  243. // Create a Batch operation on ds
  244. int32_t batch_size = 1;
  245. ds = ds->Batch(batch_size);
  246. EXPECT_NE(ds, nullptr);
  247. // Create an iterator over the result of the above dataset
  248. // This will trigger the creation of the Execution Tree and launch it.
  249. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  250. EXPECT_NE(iter, nullptr);
  251. // Iterate the dataset and get each row
  252. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  253. iter->GetNextRow(&row);
  254. uint64_t i = 0;
  255. while (row.size() != 0) {
  256. i++;
  257. auto image = row["image"];
  258. MS_LOG(INFO) << "Tensor image shape: " << image->shape();
  259. iter->GetNextRow(&row);
  260. }
  261. EXPECT_EQ(i, 20);
  262. // Manually terminate the pipeline
  263. iter->Stop();
  264. }
  265. TEST_F(MindDataTestPipeline, TestHwcToChw) {
  266. // Create an ImageFolder Dataset
  267. std::string folder_path = datasets_root_path_ + "/testPK/data/";
  268. std::shared_ptr<Dataset> ds = ImageFolder(folder_path, true, RandomSampler(false, 10));
  269. EXPECT_NE(ds, nullptr);
  270. // Create a Repeat operation on ds
  271. int32_t repeat_num = 2;
  272. ds = ds->Repeat(repeat_num);
  273. EXPECT_NE(ds, nullptr);
  274. // Create objects for the tensor ops
  275. std::shared_ptr<TensorOperation> channel_swap = vision::HWC2CHW();
  276. EXPECT_NE(channel_swap, nullptr);
  277. // Create a Map operation on ds
  278. ds = ds->Map({channel_swap});
  279. EXPECT_NE(ds, nullptr);
  280. // Create a Batch operation on ds
  281. int32_t batch_size = 1;
  282. ds = ds->Batch(batch_size);
  283. EXPECT_NE(ds, nullptr);
  284. // Create an iterator over the result of the above dataset
  285. // This will trigger the creation of the Execution Tree and launch it.
  286. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  287. EXPECT_NE(iter, nullptr);
  288. // Iterate the dataset and get each row
  289. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  290. iter->GetNextRow(&row);
  291. uint64_t i = 0;
  292. while (row.size() != 0) {
  293. i++;
  294. auto image = row["image"];
  295. MS_LOG(INFO) << "Tensor image shape: " << image->shape();
  296. // check if the image is in NCHW
  297. EXPECT_EQ(batch_size == image->shape()[0] && 3 == image->shape()[1] && 2268 == image->shape()[2] &&
  298. 4032 == image->shape()[3],
  299. true);
  300. iter->GetNextRow(&row);
  301. }
  302. EXPECT_EQ(i, 20);
  303. // Manually terminate the pipeline
  304. iter->Stop();
  305. }
  306. TEST_F(MindDataTestPipeline, TestMixUpBatchFail1) {
  307. // Create a Cifar10 Dataset
  308. std::string folder_path = datasets_root_path_ + "/testCifar10Data/";
  309. std::shared_ptr<Dataset> ds = Cifar10(folder_path, "all", RandomSampler(false, 10));
  310. EXPECT_NE(ds, nullptr);
  311. // Create a Batch operation on ds
  312. int32_t batch_size = 5;
  313. ds = ds->Batch(batch_size);
  314. EXPECT_NE(ds, nullptr);
  315. // Create objects for the tensor ops
  316. std::shared_ptr<TensorOperation> one_hot_op = transforms::OneHot(10);
  317. EXPECT_NE(one_hot_op, nullptr);
  318. // Create a Map operation on ds
  319. ds = ds->Map({one_hot_op}, {"label"});
  320. EXPECT_NE(ds, nullptr);
  321. std::shared_ptr<TensorOperation> mixup_batch_op = vision::MixUpBatch(-1);
  322. EXPECT_EQ(mixup_batch_op, nullptr);
  323. }
  324. TEST_F(MindDataTestPipeline, TestMixUpBatchFail2) {
  325. // This should fail because alpha can't be zero
  326. // Create a Cifar10 Dataset
  327. std::string folder_path = datasets_root_path_ + "/testCifar10Data/";
  328. std::shared_ptr<Dataset> ds = Cifar10(folder_path, "all", RandomSampler(false, 10));
  329. EXPECT_NE(ds, nullptr);
  330. // Create a Batch operation on ds
  331. int32_t batch_size = 5;
  332. ds = ds->Batch(batch_size);
  333. EXPECT_NE(ds, nullptr);
  334. // Create objects for the tensor ops
  335. std::shared_ptr<TensorOperation> one_hot_op = transforms::OneHot(10);
  336. EXPECT_NE(one_hot_op, nullptr);
  337. // Create a Map operation on ds
  338. ds = ds->Map({one_hot_op}, {"label"});
  339. EXPECT_NE(ds, nullptr);
  340. std::shared_ptr<TensorOperation> mixup_batch_op = vision::MixUpBatch(0.0);
  341. EXPECT_EQ(mixup_batch_op, nullptr);
  342. }
  343. TEST_F(MindDataTestPipeline, TestMixUpBatchSuccess1) {
  344. // Create a Cifar10 Dataset
  345. std::string folder_path = datasets_root_path_ + "/testCifar10Data/";
  346. std::shared_ptr<Dataset> ds = Cifar10(folder_path, "all", RandomSampler(false, 10));
  347. EXPECT_NE(ds, nullptr);
  348. // Create a Batch operation on ds
  349. int32_t batch_size = 5;
  350. ds = ds->Batch(batch_size);
  351. EXPECT_NE(ds, nullptr);
  352. // Create objects for the tensor ops
  353. std::shared_ptr<TensorOperation> one_hot_op = transforms::OneHot(10);
  354. EXPECT_NE(one_hot_op, nullptr);
  355. // Create a Map operation on ds
  356. ds = ds->Map({one_hot_op}, {"label"});
  357. EXPECT_NE(ds, nullptr);
  358. std::shared_ptr<TensorOperation> mixup_batch_op = vision::MixUpBatch(2.0);
  359. EXPECT_NE(mixup_batch_op, nullptr);
  360. // Create a Map operation on ds
  361. ds = ds->Map({mixup_batch_op}, {"image", "label"});
  362. EXPECT_NE(ds, nullptr);
  363. // Create an iterator over the result of the above dataset
  364. // This will trigger the creation of the Execution Tree and launch it.
  365. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  366. EXPECT_NE(iter, nullptr);
  367. // Iterate the dataset and get each row
  368. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  369. iter->GetNextRow(&row);
  370. uint64_t i = 0;
  371. while (row.size() != 0) {
  372. i++;
  373. auto image = row["image"];
  374. MS_LOG(INFO) << "Tensor image shape: " << image->shape();
  375. iter->GetNextRow(&row);
  376. }
  377. EXPECT_EQ(i, 2);
  378. // Manually terminate the pipeline
  379. iter->Stop();
  380. }
  381. TEST_F(MindDataTestPipeline, TestMixUpBatchSuccess2) {
  382. // Create a Cifar10 Dataset
  383. std::string folder_path = datasets_root_path_ + "/testCifar10Data/";
  384. std::shared_ptr<Dataset> ds = Cifar10(folder_path, "all", RandomSampler(false, 10));
  385. EXPECT_NE(ds, nullptr);
  386. // Create a Batch operation on ds
  387. int32_t batch_size = 5;
  388. ds = ds->Batch(batch_size);
  389. EXPECT_NE(ds, nullptr);
  390. // Create objects for the tensor ops
  391. std::shared_ptr<TensorOperation> one_hot_op = transforms::OneHot(10);
  392. EXPECT_NE(one_hot_op, nullptr);
  393. // Create a Map operation on ds
  394. ds = ds->Map({one_hot_op}, {"label"});
  395. EXPECT_NE(ds, nullptr);
  396. std::shared_ptr<TensorOperation> mixup_batch_op = vision::MixUpBatch();
  397. EXPECT_NE(mixup_batch_op, nullptr);
  398. // Create a Map operation on ds
  399. ds = ds->Map({mixup_batch_op}, {"image", "label"});
  400. EXPECT_NE(ds, nullptr);
  401. // Create an iterator over the result of the above dataset
  402. // This will trigger the creation of the Execution Tree and launch it.
  403. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  404. EXPECT_NE(iter, nullptr);
  405. // Iterate the dataset and get each row
  406. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  407. iter->GetNextRow(&row);
  408. uint64_t i = 0;
  409. while (row.size() != 0) {
  410. i++;
  411. auto image = row["image"];
  412. MS_LOG(INFO) << "Tensor image shape: " << image->shape();
  413. iter->GetNextRow(&row);
  414. }
  415. EXPECT_EQ(i, 2);
  416. // Manually terminate the pipeline
  417. iter->Stop();
  418. }
  419. TEST_F(MindDataTestPipeline, TestNormalize) {
  420. // Create an ImageFolder Dataset
  421. std::string folder_path = datasets_root_path_ + "/testPK/data/";
  422. std::shared_ptr<Dataset> ds = ImageFolder(folder_path, true, RandomSampler(false, 10));
  423. EXPECT_NE(ds, nullptr);
  424. // Create a Repeat operation on ds
  425. int32_t repeat_num = 2;
  426. ds = ds->Repeat(repeat_num);
  427. EXPECT_NE(ds, nullptr);
  428. // Create objects for the tensor ops
  429. std::shared_ptr<TensorOperation> normalize = vision::Normalize({121.0, 115.0, 100.0}, {70.0, 68.0, 71.0});
  430. EXPECT_NE(normalize, nullptr);
  431. // Create a Map operation on ds
  432. ds = ds->Map({normalize});
  433. EXPECT_NE(ds, nullptr);
  434. // Create a Batch operation on ds
  435. int32_t batch_size = 1;
  436. ds = ds->Batch(batch_size);
  437. EXPECT_NE(ds, nullptr);
  438. // Create an iterator over the result of the above dataset
  439. // This will trigger the creation of the Execution Tree and launch it.
  440. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  441. EXPECT_NE(iter, nullptr);
  442. // Iterate the dataset and get each row
  443. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  444. iter->GetNextRow(&row);
  445. uint64_t i = 0;
  446. while (row.size() != 0) {
  447. i++;
  448. auto image = row["image"];
  449. MS_LOG(INFO) << "Tensor image shape: " << image->shape();
  450. iter->GetNextRow(&row);
  451. }
  452. EXPECT_EQ(i, 20);
  453. // Manually terminate the pipeline
  454. iter->Stop();
  455. }
  456. TEST_F(MindDataTestPipeline, TestPad) {
  457. // Create an ImageFolder Dataset
  458. std::string folder_path = datasets_root_path_ + "/testPK/data/";
  459. std::shared_ptr<Dataset> ds = ImageFolder(folder_path, true, RandomSampler(false, 10));
  460. EXPECT_NE(ds, nullptr);
  461. // Create a Repeat operation on ds
  462. int32_t repeat_num = 2;
  463. ds = ds->Repeat(repeat_num);
  464. EXPECT_NE(ds, nullptr);
  465. // Create objects for the tensor ops
  466. std::shared_ptr<TensorOperation> pad_op1 = vision::Pad({1, 2, 3, 4}, {0}, BorderType::kSymmetric);
  467. EXPECT_NE(pad_op1, nullptr);
  468. std::shared_ptr<TensorOperation> pad_op2 = vision::Pad({1}, {1, 1, 1}, BorderType::kEdge);
  469. EXPECT_NE(pad_op2, nullptr);
  470. std::shared_ptr<TensorOperation> pad_op3 = vision::Pad({1, 4});
  471. EXPECT_NE(pad_op3, nullptr);
  472. // Create a Map operation on ds
  473. ds = ds->Map({pad_op1, pad_op2, pad_op3});
  474. EXPECT_NE(ds, nullptr);
  475. // Create a Batch operation on ds
  476. int32_t batch_size = 1;
  477. ds = ds->Batch(batch_size);
  478. EXPECT_NE(ds, nullptr);
  479. // Create an iterator over the result of the above dataset
  480. // This will trigger the creation of the Execution Tree and launch it.
  481. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  482. EXPECT_NE(iter, nullptr);
  483. // Iterate the dataset and get each row
  484. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  485. iter->GetNextRow(&row);
  486. uint64_t i = 0;
  487. while (row.size() != 0) {
  488. i++;
  489. auto image = row["image"];
  490. MS_LOG(INFO) << "Tensor image shape: " << image->shape();
  491. iter->GetNextRow(&row);
  492. }
  493. EXPECT_EQ(i, 20);
  494. // Manually terminate the pipeline
  495. iter->Stop();
  496. }
  497. TEST_F(MindDataTestPipeline, TestRandomAffineFail) {
  498. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestRandomAffineFail with invalid params.";
  499. // Create objects for the tensor ops
  500. std::shared_ptr<TensorOperation> affine = vision::RandomAffine({0.0, 0.0}, {});
  501. EXPECT_EQ(affine, nullptr);
  502. // Invalid number of values for translate
  503. affine = vision::RandomAffine({0.0, 0.0}, {1, 1, 1, 1, 1});
  504. EXPECT_EQ(affine, nullptr);
  505. // Invalid number of values for shear
  506. affine = vision::RandomAffine({30.0, 30.0}, {0.0, 0.0}, {2.0, 2.0}, {10.0});
  507. EXPECT_EQ(affine, nullptr);
  508. }
  509. TEST_F(MindDataTestPipeline, TestRandomAffineSuccess1) {
  510. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestRandomAffineSuccess1 with non-default params.";
  511. // Create an ImageFolder Dataset
  512. std::string folder_path = datasets_root_path_ + "/testPK/data/";
  513. std::shared_ptr<Dataset> ds = ImageFolder(folder_path, true, RandomSampler(false, 10));
  514. EXPECT_NE(ds, nullptr);
  515. // Create a Repeat operation on ds
  516. int32_t repeat_num = 2;
  517. ds = ds->Repeat(repeat_num);
  518. EXPECT_NE(ds, nullptr);
  519. // Create objects for the tensor ops
  520. std::shared_ptr<TensorOperation> affine =
  521. 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});
  522. EXPECT_NE(affine, nullptr);
  523. // Create a Map operation on ds
  524. ds = ds->Map({affine});
  525. EXPECT_NE(ds, nullptr);
  526. // Create a Batch operation on ds
  527. int32_t batch_size = 1;
  528. ds = ds->Batch(batch_size);
  529. EXPECT_NE(ds, nullptr);
  530. // Create an iterator over the result of the above dataset
  531. // This will trigger the creation of the Execution Tree and launch it.
  532. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  533. EXPECT_NE(iter, nullptr);
  534. // Iterate the dataset and get each row
  535. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  536. iter->GetNextRow(&row);
  537. uint64_t i = 0;
  538. while (row.size() != 0) {
  539. i++;
  540. auto image = row["image"];
  541. MS_LOG(INFO) << "Tensor image shape: " << image->shape();
  542. iter->GetNextRow(&row);
  543. }
  544. EXPECT_EQ(i, 20);
  545. // Manually terminate the pipeline
  546. iter->Stop();
  547. }
  548. TEST_F(MindDataTestPipeline, TestRandomAffineSuccess2) {
  549. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestRandomAffineSuccess2 with default params.";
  550. // Create an ImageFolder Dataset
  551. std::string folder_path = datasets_root_path_ + "/testPK/data/";
  552. std::shared_ptr<Dataset> ds = ImageFolder(folder_path, true, RandomSampler(false, 10));
  553. EXPECT_NE(ds, nullptr);
  554. // Create a Repeat operation on ds
  555. int32_t repeat_num = 2;
  556. ds = ds->Repeat(repeat_num);
  557. EXPECT_NE(ds, nullptr);
  558. // Create objects for the tensor ops
  559. std::shared_ptr<TensorOperation> affine = vision::RandomAffine({0.0, 0.0});
  560. EXPECT_NE(affine, nullptr);
  561. // Create a Map operation on ds
  562. ds = ds->Map({affine});
  563. EXPECT_NE(ds, nullptr);
  564. // Create a Batch operation on ds
  565. int32_t batch_size = 1;
  566. ds = ds->Batch(batch_size);
  567. EXPECT_NE(ds, nullptr);
  568. // Create an iterator over the result of the above dataset
  569. // This will trigger the creation of the Execution Tree and launch it.
  570. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  571. EXPECT_NE(iter, nullptr);
  572. // Iterate the dataset and get each row
  573. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  574. iter->GetNextRow(&row);
  575. uint64_t i = 0;
  576. while (row.size() != 0) {
  577. i++;
  578. auto image = row["image"];
  579. MS_LOG(INFO) << "Tensor image shape: " << image->shape();
  580. iter->GetNextRow(&row);
  581. }
  582. EXPECT_EQ(i, 20);
  583. // Manually terminate the pipeline
  584. iter->Stop();
  585. }
  586. TEST_F(MindDataTestPipeline, TestRandomColor) {
  587. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestRandomColor with non-default params.";
  588. // Create an ImageFolder Dataset
  589. std::string folder_path = datasets_root_path_ + "/testPK/data/";
  590. std::shared_ptr<Dataset> ds = ImageFolder(folder_path, true, RandomSampler(false, 10));
  591. EXPECT_NE(ds, nullptr);
  592. // Create a Repeat operation on ds
  593. int32_t repeat_num = 2;
  594. ds = ds->Repeat(repeat_num);
  595. EXPECT_NE(ds, nullptr);
  596. // Create objects for the tensor ops
  597. std::shared_ptr<TensorOperation> random_color_op_1 = vision::RandomColor(0.0, 0.0);
  598. EXPECT_NE(random_color_op_1, nullptr);
  599. std::shared_ptr<TensorOperation> random_color_op_2 = vision::RandomColor(1.0, 0.1);
  600. EXPECT_EQ(random_color_op_2, nullptr);
  601. std::shared_ptr<TensorOperation> random_color_op_3 = vision::RandomColor(0.0, 1.1);
  602. EXPECT_NE(random_color_op_3, nullptr);
  603. // Create a Map operation on ds
  604. ds = ds->Map({random_color_op_1, random_color_op_3});
  605. EXPECT_NE(ds, nullptr);
  606. // Create a Batch operation on ds
  607. int32_t batch_size = 1;
  608. ds = ds->Batch(batch_size);
  609. EXPECT_NE(ds, nullptr);
  610. // Create an iterator over the result of the above dataset
  611. // This will trigger the creation of the Execution Tree and launch it.
  612. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  613. EXPECT_NE(iter, nullptr);
  614. // Iterate the dataset and get each row
  615. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  616. iter->GetNextRow(&row);
  617. uint64_t i = 0;
  618. while (row.size() != 0) {
  619. i++;
  620. auto image = row["image"];
  621. MS_LOG(INFO) << "Tensor image shape: " << image->shape();
  622. iter->GetNextRow(&row);
  623. }
  624. EXPECT_EQ(i, 20);
  625. // Manually terminate the pipeline
  626. iter->Stop();
  627. }
  628. TEST_F(MindDataTestPipeline, TestRandomColorAdjust) {
  629. // Create an ImageFolder Dataset
  630. std::string folder_path = datasets_root_path_ + "/testPK/data/";
  631. std::shared_ptr<Dataset> ds = ImageFolder(folder_path, true, RandomSampler(false, 10));
  632. EXPECT_NE(ds, nullptr);
  633. // Create a Repeat operation on ds
  634. int32_t repeat_num = 2;
  635. ds = ds->Repeat(repeat_num);
  636. EXPECT_NE(ds, nullptr);
  637. // Create objects for the tensor ops
  638. std::shared_ptr<TensorOperation> random_color_adjust1 = vision::RandomColorAdjust({1.0}, {0.0}, {0.5}, {0.5});
  639. EXPECT_NE(random_color_adjust1, nullptr);
  640. std::shared_ptr<TensorOperation> random_color_adjust2 =
  641. vision::RandomColorAdjust({1.0, 1.0}, {0.0, 0.0}, {0.5, 0.5}, {0.5, 0.5});
  642. EXPECT_NE(random_color_adjust2, nullptr);
  643. std::shared_ptr<TensorOperation> random_color_adjust3 =
  644. vision::RandomColorAdjust({0.5, 1.0}, {0.0, 0.5}, {0.25, 0.5}, {0.25, 0.5});
  645. EXPECT_NE(random_color_adjust3, nullptr);
  646. std::shared_ptr<TensorOperation> random_color_adjust4 = vision::RandomColorAdjust();
  647. EXPECT_NE(random_color_adjust4, nullptr);
  648. // Create a Map operation on ds
  649. ds = ds->Map({random_color_adjust1, random_color_adjust2, random_color_adjust3, random_color_adjust4});
  650. EXPECT_NE(ds, nullptr);
  651. // Create a Batch operation on ds
  652. int32_t batch_size = 1;
  653. ds = ds->Batch(batch_size);
  654. EXPECT_NE(ds, nullptr);
  655. // Create an iterator over the result of the above dataset
  656. // This will trigger the creation of the Execution Tree and launch it.
  657. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  658. EXPECT_NE(iter, nullptr);
  659. // Iterate the dataset and get each row
  660. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  661. iter->GetNextRow(&row);
  662. uint64_t i = 0;
  663. while (row.size() != 0) {
  664. i++;
  665. auto image = row["image"];
  666. MS_LOG(INFO) << "Tensor image shape: " << image->shape();
  667. iter->GetNextRow(&row);
  668. }
  669. EXPECT_EQ(i, 20);
  670. // Manually terminate the pipeline
  671. iter->Stop();
  672. }
  673. TEST_F(MindDataTestPipeline, TestRandomPosterizeFail) {
  674. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestRandomPosterize with invalid params.";
  675. // Create objects for the tensor ops
  676. // Invalid max > 8
  677. std::shared_ptr<TensorOperation> posterize = vision::RandomPosterize({1, 9});
  678. EXPECT_EQ(posterize, nullptr);
  679. // Invalid min < 1
  680. posterize = vision::RandomPosterize({0, 8});
  681. EXPECT_EQ(posterize, nullptr);
  682. // min > max
  683. posterize = vision::RandomPosterize({8, 1});
  684. EXPECT_EQ(posterize, nullptr);
  685. // empty
  686. posterize = vision::RandomPosterize({});
  687. EXPECT_EQ(posterize, nullptr);
  688. }
  689. TEST_F(MindDataTestPipeline, TestRandomPosterizeSuccess1) {
  690. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestRandomPosterizeSuccess1 with non-default params.";
  691. // Create an ImageFolder Dataset
  692. std::string folder_path = datasets_root_path_ + "/testPK/data/";
  693. std::shared_ptr<Dataset> ds = ImageFolder(folder_path, true, RandomSampler(false, 10));
  694. EXPECT_NE(ds, nullptr);
  695. // Create a Repeat operation on ds
  696. int32_t repeat_num = 2;
  697. ds = ds->Repeat(repeat_num);
  698. EXPECT_NE(ds, nullptr);
  699. // Create objects for the tensor ops
  700. std::shared_ptr<TensorOperation> posterize = vision::RandomPosterize({1, 4});
  701. EXPECT_NE(posterize, nullptr);
  702. // Create a Map operation on ds
  703. ds = ds->Map({posterize});
  704. EXPECT_NE(ds, nullptr);
  705. // Create a Batch operation on ds
  706. int32_t batch_size = 1;
  707. ds = ds->Batch(batch_size);
  708. EXPECT_NE(ds, nullptr);
  709. // Create an iterator over the result of the above dataset
  710. // This will trigger the creation of the Execution Tree and launch it.
  711. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  712. EXPECT_NE(iter, nullptr);
  713. // Iterate the dataset and get each row
  714. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  715. iter->GetNextRow(&row);
  716. uint64_t i = 0;
  717. while (row.size() != 0) {
  718. i++;
  719. auto image = row["image"];
  720. MS_LOG(INFO) << "Tensor image shape: " << image->shape();
  721. iter->GetNextRow(&row);
  722. }
  723. EXPECT_EQ(i, 20);
  724. // Manually terminate the pipeline
  725. iter->Stop();
  726. }
  727. TEST_F(MindDataTestPipeline, TestRandomPosterizeSuccess2) {
  728. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestRandomPosterizeSuccess2 with default params.";
  729. // Create an ImageFolder Dataset
  730. std::string folder_path = datasets_root_path_ + "/testPK/data/";
  731. std::shared_ptr<Dataset> ds = ImageFolder(folder_path, true, RandomSampler(false, 10));
  732. EXPECT_NE(ds, nullptr);
  733. // Create a Repeat operation on ds
  734. int32_t repeat_num = 2;
  735. ds = ds->Repeat(repeat_num);
  736. EXPECT_NE(ds, nullptr);
  737. // Create objects for the tensor ops
  738. std::shared_ptr<TensorOperation> posterize = vision::RandomPosterize();
  739. EXPECT_NE(posterize, nullptr);
  740. // Create a Map operation on ds
  741. ds = ds->Map({posterize});
  742. EXPECT_NE(ds, nullptr);
  743. // Create a Batch operation on ds
  744. int32_t batch_size = 1;
  745. ds = ds->Batch(batch_size);
  746. EXPECT_NE(ds, nullptr);
  747. // Create an iterator over the result of the above dataset
  748. // This will trigger the creation of the Execution Tree and launch it.
  749. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  750. EXPECT_NE(iter, nullptr);
  751. // Iterate the dataset and get each row
  752. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  753. iter->GetNextRow(&row);
  754. uint64_t i = 0;
  755. while (row.size() != 0) {
  756. i++;
  757. auto image = row["image"];
  758. MS_LOG(INFO) << "Tensor image shape: " << image->shape();
  759. iter->GetNextRow(&row);
  760. }
  761. EXPECT_EQ(i, 20);
  762. // Manually terminate the pipeline
  763. iter->Stop();
  764. }
  765. TEST_F(MindDataTestPipeline, TestRandomSharpness) {
  766. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestRandomSharpness.";
  767. // Create an ImageFolder Dataset
  768. std::string folder_path = datasets_root_path_ + "/testPK/data/";
  769. std::shared_ptr<Dataset> ds = ImageFolder(folder_path, true, RandomSampler(false, 10));
  770. EXPECT_NE(ds, nullptr);
  771. // Create a Repeat operation on ds
  772. int32_t repeat_num = 2;
  773. ds = ds->Repeat(repeat_num);
  774. EXPECT_NE(ds, nullptr);
  775. // Create objects for the tensor ops
  776. std::shared_ptr<TensorOperation> random_sharpness_op_1 = vision::RandomSharpness({0.4, 2.3});
  777. EXPECT_NE(random_sharpness_op_1, nullptr);
  778. std::shared_ptr<TensorOperation> random_sharpness_op_2 = vision::RandomSharpness({});
  779. EXPECT_EQ(random_sharpness_op_2, nullptr);
  780. std::shared_ptr<TensorOperation> random_sharpness_op_3 = vision::RandomSharpness();
  781. EXPECT_NE(random_sharpness_op_3, nullptr);
  782. std::shared_ptr<TensorOperation> random_sharpness_op_4 = vision::RandomSharpness({0.1});
  783. EXPECT_EQ(random_sharpness_op_4, nullptr);
  784. // Create a Map operation on ds
  785. ds = ds->Map({random_sharpness_op_1, random_sharpness_op_3});
  786. EXPECT_NE(ds, nullptr);
  787. // Create a Batch operation on ds
  788. int32_t batch_size = 1;
  789. ds = ds->Batch(batch_size);
  790. EXPECT_NE(ds, nullptr);
  791. // Create an iterator over the result of the above dataset
  792. // This will trigger the creation of the Execution Tree and launch it.
  793. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  794. EXPECT_NE(iter, nullptr);
  795. // Iterate the dataset and get each row
  796. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  797. iter->GetNextRow(&row);
  798. uint64_t i = 0;
  799. while (row.size() != 0) {
  800. i++;
  801. auto image = row["image"];
  802. MS_LOG(INFO) << "Tensor image shape: " << image->shape();
  803. iter->GetNextRow(&row);
  804. }
  805. EXPECT_EQ(i, 20);
  806. // Manually terminate the pipeline
  807. iter->Stop();
  808. }
  809. TEST_F(MindDataTestPipeline, TestRandomFlip) {
  810. // Create an ImageFolder Dataset
  811. std::string folder_path = datasets_root_path_ + "/testPK/data/";
  812. std::shared_ptr<Dataset> ds = ImageFolder(folder_path, true, RandomSampler(false, 10));
  813. EXPECT_NE(ds, nullptr);
  814. // Create a Repeat operation on ds
  815. int32_t repeat_num = 2;
  816. ds = ds->Repeat(repeat_num);
  817. EXPECT_NE(ds, nullptr);
  818. // Create objects for the tensor ops
  819. std::shared_ptr<TensorOperation> random_vertical_flip_op = vision::RandomVerticalFlip(0.5);
  820. EXPECT_NE(random_vertical_flip_op, nullptr);
  821. std::shared_ptr<TensorOperation> random_horizontal_flip_op = vision::RandomHorizontalFlip(0.5);
  822. EXPECT_NE(random_horizontal_flip_op, nullptr);
  823. // Create a Map operation on ds
  824. ds = ds->Map({random_vertical_flip_op, random_horizontal_flip_op});
  825. EXPECT_NE(ds, nullptr);
  826. // Create a Batch operation on ds
  827. int32_t batch_size = 1;
  828. ds = ds->Batch(batch_size);
  829. EXPECT_NE(ds, nullptr);
  830. // Create an iterator over the result of the above dataset
  831. // This will trigger the creation of the Execution Tree and launch it.
  832. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  833. EXPECT_NE(iter, nullptr);
  834. // Iterate the dataset and get each row
  835. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  836. iter->GetNextRow(&row);
  837. uint64_t i = 0;
  838. while (row.size() != 0) {
  839. i++;
  840. auto image = row["image"];
  841. MS_LOG(INFO) << "Tensor image shape: " << image->shape();
  842. iter->GetNextRow(&row);
  843. }
  844. EXPECT_EQ(i, 20);
  845. // Manually terminate the pipeline
  846. iter->Stop();
  847. }
  848. TEST_F(MindDataTestPipeline, TestRandomRotation) {
  849. // Create an ImageFolder Dataset
  850. std::string folder_path = datasets_root_path_ + "/testPK/data/";
  851. std::shared_ptr<Dataset> ds = ImageFolder(folder_path, true, RandomSampler(false, 10));
  852. EXPECT_NE(ds, nullptr);
  853. // Create a Repeat operation on ds
  854. int32_t repeat_num = 2;
  855. ds = ds->Repeat(repeat_num);
  856. EXPECT_NE(ds, nullptr);
  857. // Create objects for the tensor ops
  858. std::shared_ptr<TensorOperation> random_rotation_op = vision::RandomRotation({-180, 180});
  859. EXPECT_NE(random_rotation_op, nullptr);
  860. // Create a Map operation on ds
  861. ds = ds->Map({random_rotation_op});
  862. EXPECT_NE(ds, nullptr);
  863. // Create a Batch operation on ds
  864. int32_t batch_size = 1;
  865. ds = ds->Batch(batch_size);
  866. EXPECT_NE(ds, nullptr);
  867. // Create an iterator over the result of the above dataset
  868. // This will trigger the creation of the Execution Tree and launch it.
  869. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  870. EXPECT_NE(iter, nullptr);
  871. // Iterate the dataset and get each row
  872. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  873. iter->GetNextRow(&row);
  874. uint64_t i = 0;
  875. while (row.size() != 0) {
  876. i++;
  877. auto image = row["image"];
  878. MS_LOG(INFO) << "Tensor image shape: " << image->shape();
  879. iter->GetNextRow(&row);
  880. }
  881. EXPECT_EQ(i, 20);
  882. // Manually terminate the pipeline
  883. iter->Stop();
  884. }
  885. TEST_F(MindDataTestPipeline, TestUniformAugWithOps) {
  886. // Create a Mnist Dataset
  887. std::string folder_path = datasets_root_path_ + "/testMnistData/";
  888. std::shared_ptr<Dataset> ds = Mnist(folder_path, "all", RandomSampler(false, 20));
  889. EXPECT_NE(ds, nullptr);
  890. // Create a Repeat operation on ds
  891. int32_t repeat_num = 1;
  892. ds = ds->Repeat(repeat_num);
  893. EXPECT_NE(ds, nullptr);
  894. // Create objects for the tensor ops
  895. std::shared_ptr<TensorOperation> resize_op = vision::Resize({30, 30});
  896. EXPECT_NE(resize_op, nullptr);
  897. std::shared_ptr<TensorOperation> random_crop_op = vision::RandomCrop({28, 28});
  898. EXPECT_NE(random_crop_op, nullptr);
  899. std::shared_ptr<TensorOperation> center_crop_op = vision::CenterCrop({16, 16});
  900. EXPECT_NE(center_crop_op, nullptr);
  901. std::shared_ptr<TensorOperation> uniform_aug_op = vision::UniformAugment({random_crop_op, center_crop_op}, 2);
  902. EXPECT_NE(uniform_aug_op, nullptr);
  903. // Create a Map operation on ds
  904. ds = ds->Map({resize_op, uniform_aug_op});
  905. EXPECT_NE(ds, nullptr);
  906. // Create an iterator over the result of the above dataset
  907. // This will trigger the creation of the Execution Tree and launch it.
  908. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  909. EXPECT_NE(iter, nullptr);
  910. // Iterate the dataset and get each row
  911. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  912. iter->GetNextRow(&row);
  913. uint64_t i = 0;
  914. while (row.size() != 0) {
  915. i++;
  916. auto image = row["image"];
  917. MS_LOG(INFO) << "Tensor image shape: " << image->shape();
  918. iter->GetNextRow(&row);
  919. }
  920. EXPECT_EQ(i, 20);
  921. // Manually terminate the pipeline
  922. iter->Stop();
  923. }
  924. TEST_F(MindDataTestPipeline, TestRandomSolarizeSucess1) {
  925. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestRandomSolarize.";
  926. // Create an ImageFolder Dataset
  927. std::string folder_path = datasets_root_path_ + "/testPK/data/";
  928. std::shared_ptr<Dataset> ds = ImageFolder(folder_path, true, RandomSampler(false, 10));
  929. EXPECT_NE(ds, nullptr);
  930. // Create objects for the tensor ops
  931. std::vector<uint8_t> threshold = {10, 100};
  932. std::shared_ptr<TensorOperation> random_solarize = mindspore::dataset::api::vision::RandomSolarize(threshold);
  933. EXPECT_NE(random_solarize, nullptr);
  934. // Create a Map operation on ds
  935. ds = ds->Map({random_solarize});
  936. EXPECT_NE(ds, nullptr);
  937. // Create an iterator over the result of the above dataset
  938. // This will trigger the creation of the Execution Tree and launch it.
  939. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  940. EXPECT_NE(iter, nullptr);
  941. // Iterate the dataset and get each row
  942. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  943. iter->GetNextRow(&row);
  944. uint64_t i = 0;
  945. while (row.size() != 0) {
  946. i++;
  947. auto image = row["image"];
  948. MS_LOG(INFO) << "Tensor image shape: " << image->shape();
  949. iter->GetNextRow(&row);
  950. }
  951. EXPECT_EQ(i, 10);
  952. // Manually terminate the pipeline
  953. iter->Stop();
  954. }
  955. TEST_F(MindDataTestPipeline, TestRandomSolarizeSucess2) {
  956. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestRandomSolarize with default params.";
  957. // Create an ImageFolder Dataset
  958. std::string folder_path = datasets_root_path_ + "/testPK/data/";
  959. std::shared_ptr<Dataset> ds = ImageFolder(folder_path, true, RandomSampler(false, 10));
  960. EXPECT_NE(ds, nullptr);
  961. // Create objects for the tensor ops
  962. std::shared_ptr<TensorOperation> random_solarize = mindspore::dataset::api::vision::RandomSolarize();
  963. EXPECT_NE(random_solarize, nullptr);
  964. // Create a Map operation on ds
  965. ds = ds->Map({random_solarize});
  966. EXPECT_NE(ds, nullptr);
  967. // Create an iterator over the result of the above dataset
  968. // This will trigger the creation of the Execution Tree and launch it.
  969. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  970. EXPECT_NE(iter, nullptr);
  971. // Iterate the dataset and get each row
  972. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  973. iter->GetNextRow(&row);
  974. uint64_t i = 0;
  975. while (row.size() != 0) {
  976. i++;
  977. auto image = row["image"];
  978. MS_LOG(INFO) << "Tensor image shape: " << image->shape();
  979. iter->GetNextRow(&row);
  980. }
  981. EXPECT_EQ(i, 10);
  982. // Manually terminate the pipeline
  983. iter->Stop();
  984. }
  985. TEST_F(MindDataTestPipeline, TestRandomSolarizeFail) {
  986. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestRandomSolarize with invalid params.";
  987. std::vector<uint8_t> threshold = {13, 1};
  988. std::shared_ptr<TensorOperation> random_solarize = mindspore::dataset::api::vision::RandomSolarize(threshold);
  989. EXPECT_EQ(random_solarize, nullptr);
  990. threshold = {1, 2, 3};
  991. random_solarize = mindspore::dataset::api::vision::RandomSolarize(threshold);
  992. EXPECT_EQ(random_solarize, nullptr);
  993. threshold = {1};
  994. random_solarize = mindspore::dataset::api::vision::RandomSolarize(threshold);
  995. EXPECT_EQ(random_solarize, nullptr);
  996. threshold = {};
  997. random_solarize = mindspore::dataset::api::vision::RandomSolarize(threshold);
  998. EXPECT_EQ(random_solarize, nullptr);
  999. }
  1000. TEST_F(MindDataTestPipeline, TestResizeFail) {
  1001. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestResize with invalid params.";
  1002. // negative resize value
  1003. std::shared_ptr<TensorOperation> resize = mindspore::dataset::api::vision::Resize({30, -30});
  1004. EXPECT_EQ(resize, nullptr);
  1005. // zero resize value
  1006. resize = mindspore::dataset::api::vision::Resize({0, 30});
  1007. EXPECT_EQ(resize, nullptr);
  1008. }
  1009. TEST_F(MindDataTestPipeline, TestCropFail) {
  1010. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestCrop with invalid params.";
  1011. // wrong width
  1012. std::shared_ptr<TensorOperation> crop = mindspore::dataset::api::vision::Crop({0, 0}, {32, -32});
  1013. EXPECT_EQ(crop, nullptr);
  1014. // wrong height
  1015. crop = mindspore::dataset::api::vision::Crop({0, 0}, {-32, -32});
  1016. EXPECT_EQ(crop, nullptr);
  1017. // zero height
  1018. crop = mindspore::dataset::api::vision::Crop({0, 0}, {0, 32});
  1019. EXPECT_EQ(crop, nullptr);
  1020. }
  1021. TEST_F(MindDataTestPipeline, TestCenterCropFail) {
  1022. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestCenterCrop with invalid params.";
  1023. // center crop height value negative
  1024. std::shared_ptr<TensorOperation> center_crop = mindspore::dataset::api::vision::CenterCrop({-32, 32});
  1025. EXPECT_EQ(center_crop, nullptr);
  1026. // center crop width value negative
  1027. center_crop = mindspore::dataset::api::vision::CenterCrop({32, -32});
  1028. EXPECT_EQ(center_crop, nullptr);
  1029. // 0 value would result in nullptr
  1030. center_crop = mindspore::dataset::api::vision::CenterCrop({0, 32});
  1031. EXPECT_EQ(center_crop, nullptr);
  1032. }
  1033. TEST_F(MindDataTestPipeline, TestNormalizeFail) {
  1034. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestNormalize with invalid params.";
  1035. // mean value 0.0
  1036. std::shared_ptr<TensorOperation> normalize = mindspore::dataset::api::vision::Normalize({0.0, 115.0, 100.0},
  1037. {70.0, 68.0, 71.0});
  1038. EXPECT_EQ(normalize, nullptr);
  1039. // std value at 0.0
  1040. normalize = mindspore::dataset::api::vision::Normalize({121.0, 115.0, 100.0}, {0.0, 68.0, 71.0});
  1041. EXPECT_EQ(normalize, nullptr);
  1042. // mean value 300.0 greater than 255.0
  1043. normalize = mindspore::dataset::api::vision::Normalize({300.0, 115.0, 100.0}, {70.0, 68.0, 71.0});
  1044. EXPECT_EQ(normalize, nullptr);
  1045. }
  1046. TEST_F(MindDataTestPipeline, TestRandomCropDecodeResizeSucess1) {
  1047. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestRandomCropDecodeResize with default params.";
  1048. // Create an ImageFolder Dataset
  1049. std::string folder_path = datasets_root_path_ + "/testPK/data/";
  1050. std::shared_ptr<Dataset> ds = ImageFolder(folder_path, false, SequentialSampler(0, 2));
  1051. EXPECT_NE(ds, nullptr);
  1052. // Create objects for the tensor ops
  1053. std::shared_ptr<TensorOperation> random_crop_decode_resize =
  1054. mindspore::dataset::api::vision::RandomCropDecodeResize({50, 60});
  1055. EXPECT_NE(random_crop_decode_resize, nullptr);
  1056. // Create a Map operation on ds
  1057. ds = ds->Map({random_crop_decode_resize});
  1058. EXPECT_NE(ds, nullptr);
  1059. // Create an iterator over the result of the above dataset
  1060. // This will trigger the creation of the Execution Tree and launch it.
  1061. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  1062. EXPECT_NE(iter, nullptr);
  1063. // Iterate the dataset and get each row
  1064. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  1065. iter->GetNextRow(&row);
  1066. uint64_t i = 0;
  1067. while (row.size() != 0) {
  1068. i++;
  1069. auto image = row["image"];
  1070. MS_LOG(INFO) << "Tensor image shape: " << image->shape();
  1071. iter->GetNextRow(&row);
  1072. EXPECT_EQ(image->shape()[0], 50);
  1073. EXPECT_EQ(image->shape()[1], 60);
  1074. }
  1075. EXPECT_EQ(i, 2);
  1076. // Manually terminate the pipeline
  1077. iter->Stop();
  1078. }
  1079. TEST_F(MindDataTestPipeline, TestRandomCropDecodeResizeSucess2) {
  1080. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestRandomCropDecodeResize with single size.";
  1081. // Create an ImageFolder Dataset
  1082. std::string folder_path = datasets_root_path_ + "/testPK/data/";
  1083. std::shared_ptr<Dataset> ds = ImageFolder(folder_path, false, RandomSampler(false, 3));
  1084. EXPECT_NE(ds, nullptr);
  1085. // Create objects for the tensor ops
  1086. std::shared_ptr<TensorOperation> random_crop_decode_resize =
  1087. mindspore::dataset::api::vision::RandomCropDecodeResize({100});
  1088. EXPECT_NE(random_crop_decode_resize, nullptr);
  1089. // Create a Map operation on ds
  1090. ds = ds->Map({random_crop_decode_resize});
  1091. EXPECT_NE(ds, nullptr);
  1092. // Create an iterator over the result of the above dataset
  1093. // This will trigger the creation of the Execution Tree and launch it.
  1094. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  1095. EXPECT_NE(iter, nullptr);
  1096. // Iterate the dataset and get each row
  1097. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  1098. iter->GetNextRow(&row);
  1099. uint64_t i = 0;
  1100. while (row.size() != 0) {
  1101. i++;
  1102. auto image = row["image"];
  1103. MS_LOG(INFO) << "Tensor image shape: " << image->shape();
  1104. iter->GetNextRow(&row);
  1105. EXPECT_EQ(image->shape()[0], 100);
  1106. EXPECT_EQ(image->shape()[1], 100);
  1107. }
  1108. EXPECT_EQ(i, 3);
  1109. // Manually terminate the pipeline
  1110. iter->Stop();
  1111. }
  1112. TEST_F(MindDataTestPipeline, TestRandomCropDecodeResizeFail) {
  1113. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestRandomCropDecodeResize with invalid params.";
  1114. // size of size vector is not 1 or 2
  1115. std::shared_ptr<TensorOperation> random_crop_decode_resize_1 =
  1116. mindspore::dataset::api::vision::RandomCropDecodeResize({50, 100, 150});
  1117. EXPECT_EQ(random_crop_decode_resize_1, nullptr);
  1118. // incorrect scale vector
  1119. std::shared_ptr<TensorOperation> random_crop_decode_resize_2 =
  1120. mindspore::dataset::api::vision::RandomCropDecodeResize({50, 50}, {0.5});
  1121. EXPECT_EQ(random_crop_decode_resize_2, nullptr);
  1122. std::shared_ptr<TensorOperation> random_crop_decode_resize_3 =
  1123. mindspore::dataset::api::vision::RandomCropDecodeResize({50, 50}, {0.5, 0.1});
  1124. EXPECT_EQ(random_crop_decode_resize_3, nullptr);
  1125. // incorrect ratio vector
  1126. std::shared_ptr<TensorOperation> random_crop_decode_resize_4 =
  1127. mindspore::dataset::api::vision::RandomCropDecodeResize({50, 50}, {0.5, 0.6}, {0.9});
  1128. EXPECT_EQ(random_crop_decode_resize_4, nullptr);
  1129. std::shared_ptr<TensorOperation> random_crop_decode_resize_5 =
  1130. mindspore::dataset::api::vision::RandomCropDecodeResize({50, 50}, {0.5, 0.6}, {0.9, 0.1});
  1131. EXPECT_EQ(random_crop_decode_resize_5, nullptr);
  1132. // incorrect max_attempts range
  1133. std::shared_ptr<TensorOperation> random_crop_decode_resize_6 =
  1134. mindspore::dataset::api::vision::RandomCropDecodeResize({50, 50}, {0.5, 0.6}, {0.9, 0.9},
  1135. mindspore::dataset::InterpolationMode::kLinear, 0);
  1136. EXPECT_EQ(random_crop_decode_resize_6, nullptr);
  1137. }
  1138. TEST_F(MindDataTestPipeline, TestRescaleSucess1) {
  1139. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestRescaleSucess1.";
  1140. // Create an ImageFolder Dataset
  1141. std::string folder_path = datasets_root_path_ + "/testPK/data/";
  1142. std::shared_ptr<Dataset> ds = ImageFolder(folder_path, true, SequentialSampler(0, 1));
  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. auto image = row["image"];
  1152. // Create objects for the tensor ops
  1153. std::shared_ptr<TensorOperation> rescale = mindspore::dataset::api::vision::Rescale(1.0, 0.0);
  1154. EXPECT_NE(rescale, nullptr);
  1155. // Convert to the same type
  1156. std::shared_ptr<TensorOperation> type_cast = transforms::TypeCast("uint8");
  1157. EXPECT_NE(type_cast, nullptr);
  1158. ds = ds->Map({rescale, type_cast}, {"image"});
  1159. EXPECT_NE(ds, nullptr);
  1160. // Create an iterator over the result of the above dataset
  1161. // This will trigger the creation of the Execution Tree and launch it.
  1162. std::shared_ptr<Iterator> iter1 = ds->CreateIterator();
  1163. EXPECT_NE(iter1, nullptr);
  1164. // Iterate the dataset and get each row1
  1165. std::unordered_map<std::string, std::shared_ptr<Tensor>> row1;
  1166. iter1->GetNextRow(&row1);
  1167. auto image1 = row1["image"];
  1168. EXPECT_EQ(*image, *image1);
  1169. // Manually terminate the pipeline
  1170. iter1->Stop();
  1171. }
  1172. TEST_F(MindDataTestPipeline, TestRescaleSucess2) {
  1173. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestRescaleSucess2 with different params.";
  1174. // Create an ImageFolder Dataset
  1175. std::string folder_path = datasets_root_path_ + "/testPK/data/";
  1176. std::shared_ptr<Dataset> ds = ImageFolder(folder_path, true, RandomSampler(false, 1));
  1177. EXPECT_NE(ds, nullptr);
  1178. // Create objects for the tensor ops
  1179. std::shared_ptr<TensorOperation> rescale = mindspore::dataset::api::vision::Rescale(1.0 / 255, 1.0);
  1180. EXPECT_NE(rescale, nullptr);
  1181. ds = ds->Map({rescale}, {"image"});
  1182. EXPECT_NE(ds, nullptr);
  1183. // Create an iterator over the result of the above dataset
  1184. // This will trigger the creation of the Execution Tree and launch it.
  1185. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  1186. EXPECT_NE(iter, nullptr);
  1187. // Iterate the dataset and get each row
  1188. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  1189. iter->GetNextRow(&row);
  1190. uint64_t i = 0;
  1191. while (row.size() != 0) {
  1192. i++;
  1193. auto image = row["image"];
  1194. MS_LOG(INFO) << "Tensor image shape: " << image->shape();
  1195. iter->GetNextRow(&row);
  1196. }
  1197. EXPECT_EQ(i, 1);
  1198. // Manually terminate the pipeline
  1199. iter->Stop();
  1200. }
  1201. TEST_F(MindDataTestPipeline, TestRescaleFail) {
  1202. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestRescaleSucess3 with invalid params.";
  1203. // incorrect negative rescale parameter
  1204. std::shared_ptr<TensorOperation> rescale = mindspore::dataset::api::vision::Rescale(-1.0, 0.0);
  1205. EXPECT_EQ(rescale, nullptr);
  1206. }