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

c_api_test.cc 40 kB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291
  1. /**
  2. * Copyright 2020 Huawei Technologies Co., Ltd
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include <fstream>
  17. #include <iostream>
  18. #include <memory>
  19. #include <vector>
  20. #include <string>
  21. #include "utils/log_adapter.h"
  22. #include "common/utils.h"
  23. #include "common/common.h"
  24. #include "gtest/gtest.h"
  25. #include "securec.h"
  26. #include "minddata/dataset/include/datasets.h"
  27. #include "minddata/dataset/include/status.h"
  28. #include "minddata/dataset/include/transforms.h"
  29. #include "minddata/dataset/include/iterator.h"
  30. #include "minddata/dataset/core/constants.h"
  31. #include "minddata/dataset/core/tensor_shape.h"
  32. #include "minddata/dataset/core/tensor.h"
  33. #include "minddata/dataset/include/samplers.h"
  34. #include "minddata/dataset/engine/datasetops/source/voc_op.h"
  35. using namespace mindspore::dataset::api;
  36. using mindspore::MsLogLevel::ERROR;
  37. using mindspore::ExceptionType::NoExceptionType;
  38. using mindspore::LogStream;
  39. using mindspore::dataset::Tensor;
  40. using mindspore::dataset::TensorShape;
  41. using mindspore::dataset::TensorImpl;
  42. using mindspore::dataset::DataType;
  43. using mindspore::dataset::Status;
  44. using mindspore::dataset::BorderType;
  45. class MindDataTestPipeline : public UT::DatasetOpTesting {
  46. protected:
  47. };
  48. TEST_F(MindDataTestPipeline, TestBatchAndRepeat) {
  49. // Create a Mnist Dataset
  50. std::string folder_path = datasets_root_path_ + "/testMnistData/";
  51. std::shared_ptr<Dataset> ds = Mnist(folder_path, RandomSampler(false, 10));
  52. EXPECT_NE(ds, nullptr);
  53. // Create a Repeat operation on ds
  54. int32_t repeat_num = 2;
  55. ds = ds->Repeat(repeat_num);
  56. EXPECT_NE(ds, nullptr);
  57. // Create a Batch operation on ds
  58. int32_t batch_size = 2;
  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, 10);
  76. // Manually terminate the pipeline
  77. iter->Stop();
  78. }
  79. TEST_F(MindDataTestPipeline, TestMnistFail1) {
  80. // Create a Mnist Dataset
  81. std::shared_ptr<Dataset> ds = Mnist("", RandomSampler(false, 10));
  82. EXPECT_EQ(ds, nullptr);
  83. }
  84. TEST_F(MindDataTestPipeline, TestTensorOpsAndMap) {
  85. // Create a Mnist Dataset
  86. std::string folder_path = datasets_root_path_ + "/testMnistData/";
  87. std::shared_ptr<Dataset> ds = Mnist(folder_path, RandomSampler(false, 20));
  88. EXPECT_NE(ds, nullptr);
  89. // Create a Repeat operation on ds
  90. int32_t repeat_num = 2;
  91. ds = ds->Repeat(repeat_num);
  92. EXPECT_NE(ds, nullptr);
  93. // Create objects for the tensor ops
  94. std::shared_ptr<TensorOperation> resize_op = vision::Resize({30, 30});
  95. EXPECT_NE(resize_op, nullptr);
  96. std::shared_ptr<TensorOperation> center_crop_op = vision::CenterCrop({16, 16});
  97. EXPECT_NE(center_crop_op, nullptr);
  98. // Create a Map operation on ds
  99. ds = ds->Map({resize_op, center_crop_op});
  100. EXPECT_NE(ds, nullptr);
  101. // Create a Batch operation on ds
  102. int32_t batch_size = 1;
  103. ds = ds->Batch(batch_size);
  104. EXPECT_NE(ds, nullptr);
  105. // Create an iterator over the result of the above dataset
  106. // This will trigger the creation of the Execution Tree and launch it.
  107. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  108. EXPECT_NE(iter, nullptr);
  109. // Iterate the dataset and get each row
  110. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  111. iter->GetNextRow(&row);
  112. uint64_t i = 0;
  113. while (row.size() != 0) {
  114. i++;
  115. auto image = row["image"];
  116. MS_LOG(INFO) << "Tensor image shape: " << image->shape();
  117. iter->GetNextRow(&row);
  118. }
  119. EXPECT_EQ(i, 40);
  120. // Manually terminate the pipeline
  121. iter->Stop();
  122. }
  123. TEST_F(MindDataTestPipeline, TestUniformAugWithOps) {
  124. // Create a Mnist Dataset
  125. std::string folder_path = datasets_root_path_ + "/testMnistData/";
  126. std::shared_ptr<Dataset> ds = Mnist(folder_path, RandomSampler(false, 20));
  127. EXPECT_NE(ds, nullptr);
  128. // Create a Repeat operation on ds
  129. int32_t repeat_num = 1;
  130. ds = ds->Repeat(repeat_num);
  131. EXPECT_NE(ds, nullptr);
  132. // Create objects for the tensor ops
  133. std::shared_ptr<TensorOperation> resize_op = vision::Resize({30, 30});
  134. EXPECT_NE(resize_op, nullptr);
  135. std::shared_ptr<TensorOperation> random_crop_op = vision::RandomCrop({28, 28});
  136. EXPECT_NE(random_crop_op, nullptr);
  137. std::shared_ptr<TensorOperation> center_crop_op = vision::CenterCrop({16, 16});
  138. EXPECT_NE(center_crop_op, nullptr);
  139. std::shared_ptr<TensorOperation> uniform_aug_op = vision::UniformAugment({random_crop_op, center_crop_op}, 2);
  140. EXPECT_NE(uniform_aug_op, nullptr);
  141. // Create a Map operation on ds
  142. ds = ds->Map({resize_op, uniform_aug_op});
  143. EXPECT_NE(ds, nullptr);
  144. // Create an iterator over the result of the above dataset
  145. // This will trigger the creation of the Execution Tree and launch it.
  146. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  147. EXPECT_NE(iter, nullptr);
  148. // Iterate the dataset and get each row
  149. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  150. iter->GetNextRow(&row);
  151. uint64_t i = 0;
  152. while (row.size() != 0) {
  153. i++;
  154. auto image = row["image"];
  155. MS_LOG(INFO) << "Tensor image shape: " << image->shape();
  156. iter->GetNextRow(&row);
  157. }
  158. EXPECT_EQ(i, 20);
  159. // Manually terminate the pipeline
  160. iter->Stop();
  161. }
  162. TEST_F(MindDataTestPipeline, TestRandomFlip) {
  163. // Create an ImageFolder Dataset
  164. std::string folder_path = datasets_root_path_ + "/testPK/data/";
  165. std::shared_ptr<Dataset> ds = ImageFolder(folder_path, true, RandomSampler(false, 10));
  166. EXPECT_NE(ds, nullptr);
  167. // Create a Repeat operation on ds
  168. int32_t repeat_num = 2;
  169. ds = ds->Repeat(repeat_num);
  170. EXPECT_NE(ds, nullptr);
  171. // Create objects for the tensor ops
  172. std::shared_ptr<TensorOperation> random_vertical_flip_op = vision::RandomVerticalFlip(0.5);
  173. EXPECT_NE(random_vertical_flip_op, nullptr);
  174. std::shared_ptr<TensorOperation> random_horizontal_flip_op = vision::RandomHorizontalFlip(0.5);
  175. EXPECT_NE(random_horizontal_flip_op, nullptr);
  176. // Create a Map operation on ds
  177. ds = ds->Map({random_vertical_flip_op, random_horizontal_flip_op});
  178. EXPECT_NE(ds, nullptr);
  179. // Create a Batch operation on ds
  180. int32_t batch_size = 1;
  181. ds = ds->Batch(batch_size);
  182. EXPECT_NE(ds, nullptr);
  183. // Create an iterator over the result of the above dataset
  184. // This will trigger the creation of the Execution Tree and launch it.
  185. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  186. EXPECT_NE(iter, nullptr);
  187. // Iterate the dataset and get each row
  188. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  189. iter->GetNextRow(&row);
  190. uint64_t i = 0;
  191. while (row.size() != 0) {
  192. i++;
  193. auto image = row["image"];
  194. MS_LOG(INFO) << "Tensor image shape: " << image->shape();
  195. iter->GetNextRow(&row);
  196. }
  197. EXPECT_EQ(i, 20);
  198. // Manually terminate the pipeline
  199. iter->Stop();
  200. }
  201. TEST_F(MindDataTestPipeline, TestImageFolderBatchAndRepeat) {
  202. // Create an ImageFolder Dataset
  203. std::string folder_path = datasets_root_path_ + "/testPK/data/";
  204. std::shared_ptr<Dataset> ds = ImageFolder(folder_path, true, RandomSampler(false, 10));
  205. EXPECT_NE(ds, nullptr);
  206. // Create a Repeat operation on ds
  207. int32_t repeat_num = 2;
  208. ds = ds->Repeat(repeat_num);
  209. EXPECT_NE(ds, nullptr);
  210. // Create a Batch operation on ds
  211. int32_t batch_size = 2;
  212. ds = ds->Batch(batch_size);
  213. EXPECT_NE(ds, nullptr);
  214. // Create an iterator over the result of the above dataset
  215. // This will trigger the creation of the Execution Tree and launch it.
  216. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  217. EXPECT_NE(iter, nullptr);
  218. // Iterate the dataset and get each row
  219. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  220. iter->GetNextRow(&row);
  221. uint64_t i = 0;
  222. while (row.size() != 0) {
  223. i++;
  224. auto image = row["image"];
  225. MS_LOG(INFO) << "Tensor image shape: " << image->shape();
  226. iter->GetNextRow(&row);
  227. }
  228. EXPECT_EQ(i, 10);
  229. // Manually terminate the pipeline
  230. iter->Stop();
  231. }
  232. TEST_F(MindDataTestPipeline, TestImageFolderFail1) {
  233. // Create an ImageFolder Dataset
  234. std::shared_ptr<Dataset> ds = ImageFolder("", true, nullptr);
  235. EXPECT_EQ(ds, nullptr);
  236. }
  237. TEST_F(MindDataTestPipeline, TestImageFolderWithSamplers) {
  238. std::shared_ptr<SamplerObj> sampl = DistributedSampler(2, 1);
  239. EXPECT_NE(sampl, nullptr);
  240. sampl = PKSampler(3);
  241. EXPECT_NE(sampl, nullptr);
  242. sampl = RandomSampler(false, 12);
  243. EXPECT_NE(sampl, nullptr);
  244. sampl = SequentialSampler(0, 12);
  245. EXPECT_NE(sampl, nullptr);
  246. std::vector<double> weights = {0.9, 0.8, 0.68, 0.7, 0.71, 0.6, 0.5, 0.4, 0.3, 0.5, 0.2, 0.1};
  247. sampl = WeightedRandomSampler(weights, 12);
  248. EXPECT_NE(sampl, nullptr);
  249. std::vector<int64_t> indices = {1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23};
  250. sampl = SubsetRandomSampler(indices);
  251. EXPECT_NE(sampl, nullptr);
  252. // Create an ImageFolder Dataset
  253. std::string folder_path = datasets_root_path_ + "/testPK/data/";
  254. std::shared_ptr<Dataset> ds = ImageFolder(folder_path, false, sampl);
  255. EXPECT_NE(ds, nullptr);
  256. // Create a Repeat operation on ds
  257. int32_t repeat_num = 2;
  258. ds = ds->Repeat(repeat_num);
  259. EXPECT_NE(ds, nullptr);
  260. // Create a Batch operation on ds
  261. int32_t batch_size = 2;
  262. ds = ds->Batch(batch_size);
  263. EXPECT_NE(ds, nullptr);
  264. // Create an iterator over the result of the above dataset
  265. // This will trigger the creation of the Execution Tree and launch it.
  266. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  267. EXPECT_NE(iter, nullptr);
  268. // Iterate the dataset and get each row
  269. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  270. iter->GetNextRow(&row);
  271. uint64_t i = 0;
  272. while (row.size() != 0) {
  273. i++;
  274. auto image = row["image"];
  275. MS_LOG(INFO) << "Tensor image shape: " << image->shape();
  276. iter->GetNextRow(&row);
  277. }
  278. EXPECT_EQ(i, 12);
  279. // Manually terminate the pipeline
  280. iter->Stop();
  281. }
  282. TEST_F(MindDataTestPipeline, TestPad) {
  283. // Create an ImageFolder Dataset
  284. std::string folder_path = datasets_root_path_ + "/testPK/data/";
  285. std::shared_ptr<Dataset> ds = ImageFolder(folder_path, true, RandomSampler(false, 10));
  286. EXPECT_NE(ds, nullptr);
  287. // Create a Repeat operation on ds
  288. int32_t repeat_num = 2;
  289. ds = ds->Repeat(repeat_num);
  290. EXPECT_NE(ds, nullptr);
  291. // Create objects for the tensor ops
  292. std::shared_ptr<TensorOperation> pad_op1 = vision::Pad({1, 2, 3, 4}, {0}, BorderType::kSymmetric);
  293. EXPECT_NE(pad_op1, nullptr);
  294. std::shared_ptr<TensorOperation> pad_op2 = vision::Pad({1}, {1, 1, 1}, BorderType::kEdge);
  295. EXPECT_NE(pad_op2, nullptr);
  296. std::shared_ptr<TensorOperation> pad_op3 = vision::Pad({1, 4});
  297. EXPECT_NE(pad_op3, nullptr);
  298. // Create a Map operation on ds
  299. ds = ds->Map({pad_op1, pad_op2, pad_op3});
  300. EXPECT_NE(ds, nullptr);
  301. // Create a Batch operation on ds
  302. int32_t batch_size = 1;
  303. ds = ds->Batch(batch_size);
  304. EXPECT_NE(ds, nullptr);
  305. // Create an iterator over the result of the above dataset
  306. // This will trigger the creation of the Execution Tree and launch it.
  307. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  308. EXPECT_NE(iter, nullptr);
  309. // Iterate the dataset and get each row
  310. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  311. iter->GetNextRow(&row);
  312. uint64_t i = 0;
  313. while (row.size() != 0) {
  314. i++;
  315. auto image = row["image"];
  316. MS_LOG(INFO) << "Tensor image shape: " << image->shape();
  317. iter->GetNextRow(&row);
  318. }
  319. EXPECT_EQ(i, 20);
  320. // Manually terminate the pipeline
  321. iter->Stop();
  322. }
  323. TEST_F(MindDataTestPipeline, TestCutOut) {
  324. // Create an ImageFolder Dataset
  325. std::string folder_path = datasets_root_path_ + "/testPK/data/";
  326. std::shared_ptr<Dataset> ds = ImageFolder(folder_path, true, RandomSampler(false, 10));
  327. EXPECT_NE(ds, nullptr);
  328. // Create a Repeat operation on ds
  329. int32_t repeat_num = 2;
  330. ds = ds->Repeat(repeat_num);
  331. EXPECT_NE(ds, nullptr);
  332. // Create objects for the tensor ops
  333. std::shared_ptr<TensorOperation> cut_out1 = vision::CutOut(30, 5);
  334. EXPECT_NE(cut_out1, nullptr);
  335. std::shared_ptr<TensorOperation> cut_out2 = vision::CutOut(30);
  336. EXPECT_NE(cut_out2, nullptr);
  337. // Create a Map operation on ds
  338. ds = ds->Map({cut_out1, cut_out2});
  339. EXPECT_NE(ds, nullptr);
  340. // Create a Batch operation on ds
  341. int32_t batch_size = 1;
  342. ds = ds->Batch(batch_size);
  343. EXPECT_NE(ds, nullptr);
  344. // Create an iterator over the result of the above dataset
  345. // This will trigger the creation of the Execution Tree and launch it.
  346. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  347. EXPECT_NE(iter, nullptr);
  348. // Iterate the dataset and get each row
  349. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  350. iter->GetNextRow(&row);
  351. uint64_t i = 0;
  352. while (row.size() != 0) {
  353. i++;
  354. auto image = row["image"];
  355. MS_LOG(INFO) << "Tensor image shape: " << image->shape();
  356. iter->GetNextRow(&row);
  357. }
  358. EXPECT_EQ(i, 20);
  359. // Manually terminate the pipeline
  360. iter->Stop();
  361. }
  362. TEST_F(MindDataTestPipeline, TestNormalize) {
  363. // Create an ImageFolder Dataset
  364. std::string folder_path = datasets_root_path_ + "/testPK/data/";
  365. std::shared_ptr<Dataset> ds = ImageFolder(folder_path, true, RandomSampler(false, 10));
  366. EXPECT_NE(ds, nullptr);
  367. // Create a Repeat operation on ds
  368. int32_t repeat_num = 2;
  369. ds = ds->Repeat(repeat_num);
  370. EXPECT_NE(ds, nullptr);
  371. // Create objects for the tensor ops
  372. std::shared_ptr<TensorOperation> normalize = vision::Normalize({121.0, 115.0, 100.0}, {70.0, 68.0, 71.0});
  373. EXPECT_NE(normalize, nullptr);
  374. // Create a Map operation on ds
  375. ds = ds->Map({normalize});
  376. EXPECT_NE(ds, nullptr);
  377. // Create a Batch operation on ds
  378. int32_t batch_size = 1;
  379. ds = ds->Batch(batch_size);
  380. EXPECT_NE(ds, nullptr);
  381. // Create an iterator over the result of the above dataset
  382. // This will trigger the creation of the Execution Tree and launch it.
  383. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  384. EXPECT_NE(iter, nullptr);
  385. // Iterate the dataset and get each row
  386. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  387. iter->GetNextRow(&row);
  388. uint64_t i = 0;
  389. while (row.size() != 0) {
  390. i++;
  391. auto image = row["image"];
  392. MS_LOG(INFO) << "Tensor image shape: " << image->shape();
  393. iter->GetNextRow(&row);
  394. }
  395. EXPECT_EQ(i, 20);
  396. // Manually terminate the pipeline
  397. iter->Stop();
  398. }
  399. TEST_F(MindDataTestPipeline, TestDecode) {
  400. // Create an ImageFolder Dataset
  401. std::string folder_path = datasets_root_path_ + "/testPK/data/";
  402. std::shared_ptr<Dataset> ds = ImageFolder(folder_path, false, RandomSampler(false, 10));
  403. EXPECT_NE(ds, nullptr);
  404. // Create a Repeat operation on ds
  405. int32_t repeat_num = 2;
  406. ds = ds->Repeat(repeat_num);
  407. EXPECT_NE(ds, nullptr);
  408. // Create objects for the tensor ops
  409. std::shared_ptr<TensorOperation> decode = vision::Decode(true);
  410. EXPECT_NE(decode, nullptr);
  411. // Create a Map operation on ds
  412. ds = ds->Map({decode});
  413. EXPECT_NE(ds, nullptr);
  414. // Create a Batch operation on ds
  415. int32_t batch_size = 1;
  416. ds = ds->Batch(batch_size);
  417. EXPECT_NE(ds, nullptr);
  418. // Create an iterator over the result of the above dataset
  419. // This will trigger the creation of the Execution Tree and launch it.
  420. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  421. EXPECT_NE(iter, nullptr);
  422. // Iterate the dataset and get each row
  423. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  424. iter->GetNextRow(&row);
  425. uint64_t i = 0;
  426. while (row.size() != 0) {
  427. i++;
  428. auto image = row["image"];
  429. MS_LOG(INFO) << "Tensor image shape: " << image->shape();
  430. iter->GetNextRow(&row);
  431. }
  432. EXPECT_EQ(i, 20);
  433. // Manually terminate the pipeline
  434. iter->Stop();
  435. }
  436. TEST_F(MindDataTestPipeline, TestShuffleDataset) {
  437. // Create an ImageFolder Dataset
  438. std::string folder_path = datasets_root_path_ + "/testPK/data/";
  439. std::shared_ptr<Dataset> ds = ImageFolder(folder_path, true, RandomSampler(false, 10));
  440. EXPECT_NE(ds, nullptr);
  441. // Create a Shuffle operation on ds
  442. int32_t shuffle_size = 10;
  443. ds = ds->Shuffle(shuffle_size);
  444. EXPECT_NE(ds, nullptr);
  445. // Create a Repeat operation on ds
  446. int32_t repeat_num = 2;
  447. ds = ds->Repeat(repeat_num);
  448. EXPECT_NE(ds, nullptr);
  449. // Create a Batch operation on ds
  450. int32_t batch_size = 2;
  451. ds = ds->Batch(batch_size);
  452. EXPECT_NE(ds, nullptr);
  453. // Create an iterator over the result of the above dataset
  454. // This will trigger the creation of the Execution Tree and launch it.
  455. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  456. EXPECT_NE(iter, nullptr);
  457. // Iterate the dataset and get each row
  458. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  459. iter->GetNextRow(&row);
  460. uint64_t i = 0;
  461. while (row.size() != 0) {
  462. i++;
  463. auto image = row["image"];
  464. MS_LOG(INFO) << "Tensor image shape: " << image->shape();
  465. iter->GetNextRow(&row);
  466. }
  467. EXPECT_EQ(i, 10);
  468. // Manually terminate the pipeline
  469. iter->Stop();
  470. }
  471. TEST_F(MindDataTestPipeline, TestSkipDataset) {
  472. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestSkipDataset.";
  473. // Create an ImageFolder Dataset
  474. std::string folder_path = datasets_root_path_ + "/testPK/data/";
  475. std::shared_ptr<Dataset> ds = ImageFolder(folder_path, true, RandomSampler(false, 10));
  476. EXPECT_NE(ds, nullptr);
  477. // Create a Skip operation on ds
  478. int32_t count = 3;
  479. ds = ds->Skip(count);
  480. EXPECT_NE(ds, nullptr);
  481. // Create an iterator over the result of the above dataset
  482. // This will trigger the creation of the Execution Tree and launch it.
  483. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  484. EXPECT_NE(iter, nullptr);
  485. // Iterate the dataset and get each row
  486. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  487. iter->GetNextRow(&row);
  488. uint64_t i = 0;
  489. while (row.size() != 0) {
  490. i++;
  491. auto image = row["image"];
  492. MS_LOG(INFO) << "Tensor image shape: " << image->shape();
  493. iter->GetNextRow(&row);
  494. }
  495. MS_LOG(INFO) << "Number of rows: " << i;
  496. // Expect 10-3=7 rows
  497. EXPECT_EQ(i, 7);
  498. // Manually terminate the pipeline
  499. iter->Stop();
  500. }
  501. TEST_F(MindDataTestPipeline, TestSkipDatasetError1) {
  502. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestSkipDatasetError1.";
  503. // Create an ImageFolder Dataset
  504. std::string folder_path = datasets_root_path_ + "/testPK/data/";
  505. std::shared_ptr<Dataset> ds = ImageFolder(folder_path, true, RandomSampler(false, 10));
  506. EXPECT_NE(ds, nullptr);
  507. // Create a Skip operation on ds with invalid count input
  508. int32_t count = -1;
  509. ds = ds->Skip(count);
  510. // Expect nullptr for invalid input skip_count
  511. EXPECT_EQ(ds, nullptr);
  512. }
  513. TEST_F(MindDataTestPipeline, TestTakeDatasetDefault) {
  514. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestTakeDatasetDefault.";
  515. // Create an ImageFolder Dataset
  516. std::string folder_path = datasets_root_path_ + "/testPK/data/";
  517. std::shared_ptr<Dataset> ds = ImageFolder(folder_path, true, RandomSampler(false, 7));
  518. EXPECT_NE(ds, nullptr);
  519. // Create a Take operation on ds, dafault count = -1
  520. ds = ds->Take();
  521. EXPECT_NE(ds, nullptr);
  522. // Create an iterator over the result of the above dataset
  523. // This will trigger the creation of the Execution Tree and launch it.
  524. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  525. EXPECT_NE(iter, nullptr);
  526. // Iterate the dataset and get each row
  527. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  528. iter->GetNextRow(&row);
  529. uint64_t i = 0;
  530. while (row.size() != 0) {
  531. i++;
  532. auto image = row["image"];
  533. MS_LOG(INFO) << "Tensor image shape: " << image->shape();
  534. iter->GetNextRow(&row);
  535. }
  536. MS_LOG(INFO) << "Number of rows: " << i;
  537. // Expect 7 rows
  538. EXPECT_EQ(i, 7);
  539. // Manually terminate the pipeline
  540. iter->Stop();
  541. }
  542. TEST_F(MindDataTestPipeline, TestTakeDatasetNormal) {
  543. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestTakeDatasetNormal.";
  544. // Create an ImageFolder Dataset
  545. std::string folder_path = datasets_root_path_ + "/testPK/data/";
  546. std::shared_ptr<Dataset> ds = ImageFolder(folder_path, true, RandomSampler(false, 8));
  547. EXPECT_NE(ds, nullptr);
  548. // Create a Take operation on ds
  549. ds = ds->Take(5);
  550. EXPECT_NE(ds, nullptr);
  551. // Create an iterator over the result of the above dataset
  552. // This will trigger the creation of the Execution Tree and launch it.
  553. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  554. EXPECT_NE(iter, nullptr);
  555. // Iterate the dataset and get each row
  556. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  557. iter->GetNextRow(&row);
  558. uint64_t i = 0;
  559. while (row.size() != 0) {
  560. i++;
  561. auto image = row["image"];
  562. MS_LOG(INFO) << "Tensor image shape: " << image->shape();
  563. iter->GetNextRow(&row);
  564. }
  565. MS_LOG(INFO) << "Number of rows: " << i;
  566. // Expect 5 rows
  567. EXPECT_EQ(i, 5);
  568. // Manually terminate the pipeline
  569. iter->Stop();
  570. }
  571. TEST_F(MindDataTestPipeline, TestTakeDatasetError1) {
  572. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestTakeDatasetError1.";
  573. // Create an ImageFolder Dataset
  574. std::string folder_path = datasets_root_path_ + "/testPK/data/";
  575. std::shared_ptr<Dataset> ds = ImageFolder(folder_path, true, RandomSampler(false, 10));
  576. EXPECT_NE(ds, nullptr);
  577. // Create a Take operation on ds with invalid count input
  578. int32_t count = -5;
  579. ds = ds->Take(count);
  580. // Expect nullptr for invalid input take_count
  581. EXPECT_EQ(ds, nullptr);
  582. }
  583. TEST_F(MindDataTestPipeline, TestCifar10Dataset) {
  584. // Create a Cifar10 Dataset
  585. std::string folder_path = datasets_root_path_ + "/testCifar10Data/";
  586. std::shared_ptr<Dataset> ds = Cifar10(folder_path, RandomSampler(false, 10));
  587. EXPECT_NE(ds, nullptr);
  588. // Create an iterator over the result of the above dataset
  589. // This will trigger the creation of the Execution Tree and launch it.
  590. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  591. EXPECT_NE(iter, nullptr);
  592. // Iterate the dataset and get each row
  593. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  594. iter->GetNextRow(&row);
  595. EXPECT_NE(row.find("image"), row.end());
  596. EXPECT_NE(row.find("label"), row.end());
  597. uint64_t i = 0;
  598. while (row.size() != 0) {
  599. i++;
  600. auto image = row["image"];
  601. MS_LOG(INFO) << "Tensor image shape: " << image->shape();
  602. iter->GetNextRow(&row);
  603. }
  604. EXPECT_EQ(i, 10);
  605. // Manually terminate the pipeline
  606. iter->Stop();
  607. }
  608. TEST_F(MindDataTestPipeline, TestCifar10DatasetFail1) {
  609. // Create a Cifar10 Dataset
  610. std::shared_ptr<Dataset> ds = Cifar10("", RandomSampler(false, 10));
  611. EXPECT_EQ(ds, nullptr);
  612. }
  613. TEST_F(MindDataTestPipeline, TestCifar100Dataset) {
  614. // Create a Cifar100 Dataset
  615. std::string folder_path = datasets_root_path_ + "/testCifar100Data/";
  616. std::shared_ptr<Dataset> ds = Cifar100(folder_path, RandomSampler(false, 10));
  617. EXPECT_NE(ds, nullptr);
  618. // Create an iterator over the result of the above dataset
  619. // This will trigger the creation of the Execution Tree and launch it.
  620. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  621. EXPECT_NE(iter, nullptr);
  622. // Iterate the dataset and get each row
  623. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  624. iter->GetNextRow(&row);
  625. EXPECT_NE(row.find("image"), row.end());
  626. EXPECT_NE(row.find("coarse_label"), row.end());
  627. EXPECT_NE(row.find("fine_label"), row.end());
  628. uint64_t i = 0;
  629. while (row.size() != 0) {
  630. i++;
  631. auto image = row["image"];
  632. MS_LOG(INFO) << "Tensor image shape: " << image->shape();
  633. iter->GetNextRow(&row);
  634. }
  635. EXPECT_EQ(i, 10);
  636. // Manually terminate the pipeline
  637. iter->Stop();
  638. }
  639. TEST_F(MindDataTestPipeline, TestCifar100DatasetFail1) {
  640. // Create a Cifar100 Dataset
  641. std::shared_ptr<Dataset> ds = Cifar100("", RandomSampler(false, 10));
  642. EXPECT_EQ(ds, nullptr);
  643. }
  644. TEST_F(MindDataTestPipeline, TestRandomColorAdjust) {
  645. // Create an ImageFolder Dataset
  646. std::string folder_path = datasets_root_path_ + "/testPK/data/";
  647. std::shared_ptr<Dataset> ds = ImageFolder(folder_path, true, RandomSampler(false, 10));
  648. EXPECT_NE(ds, nullptr);
  649. // Create a Repeat operation on ds
  650. int32_t repeat_num = 2;
  651. ds = ds->Repeat(repeat_num);
  652. EXPECT_NE(ds, nullptr);
  653. // Create objects for the tensor ops
  654. std::shared_ptr<TensorOperation> random_color_adjust1 = vision::RandomColorAdjust({1.0}, {0.0}, {0.5}, {0.5});
  655. EXPECT_NE(random_color_adjust1, nullptr);
  656. std::shared_ptr<TensorOperation> random_color_adjust2 = vision::RandomColorAdjust({1.0, 1.0}, {0.0, 0.0}, {0.5, 0.5},
  657. {0.5, 0.5});
  658. EXPECT_NE(random_color_adjust2, nullptr);
  659. std::shared_ptr<TensorOperation> random_color_adjust3 = vision::RandomColorAdjust({0.5, 1.0}, {0.0, 0.5}, {0.25, 0.5},
  660. {0.25, 0.5});
  661. EXPECT_NE(random_color_adjust3, nullptr);
  662. std::shared_ptr<TensorOperation> random_color_adjust4 = vision::RandomColorAdjust();
  663. EXPECT_NE(random_color_adjust4, nullptr);
  664. // Create a Map operation on ds
  665. ds = ds->Map({random_color_adjust1, random_color_adjust2, random_color_adjust3, random_color_adjust4});
  666. EXPECT_NE(ds, nullptr);
  667. // Create a Batch operation on ds
  668. int32_t batch_size = 1;
  669. ds = ds->Batch(batch_size);
  670. EXPECT_NE(ds, nullptr);
  671. // Create an iterator over the result of the above dataset
  672. // This will trigger the creation of the Execution Tree and launch it.
  673. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  674. EXPECT_NE(iter, nullptr);
  675. // Iterate the dataset and get each row
  676. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  677. iter->GetNextRow(&row);
  678. uint64_t i = 0;
  679. while (row.size() != 0) {
  680. i++;
  681. auto image = row["image"];
  682. MS_LOG(INFO) << "Tensor image shape: " << image->shape();
  683. iter->GetNextRow(&row);
  684. }
  685. EXPECT_EQ(i, 20);
  686. // Manually terminate the pipeline
  687. iter->Stop();
  688. }
  689. TEST_F(MindDataTestPipeline, TestRandomRotation) {
  690. // Create an ImageFolder Dataset
  691. std::string folder_path = datasets_root_path_ + "/testPK/data/";
  692. std::shared_ptr<Dataset> ds = ImageFolder(folder_path, true, RandomSampler(false, 10));
  693. EXPECT_NE(ds, nullptr);
  694. // Create a Repeat operation on ds
  695. int32_t repeat_num = 2;
  696. ds = ds->Repeat(repeat_num);
  697. EXPECT_NE(ds, nullptr);
  698. // Create objects for the tensor ops
  699. std::shared_ptr<TensorOperation> random_rotation_op = vision::RandomRotation({-180, 180});
  700. EXPECT_NE(random_rotation_op, nullptr);
  701. // Create a Map operation on ds
  702. ds = ds->Map({random_rotation_op});
  703. EXPECT_NE(ds, nullptr);
  704. // Create a Batch operation on ds
  705. int32_t batch_size = 1;
  706. ds = ds->Batch(batch_size);
  707. EXPECT_NE(ds, nullptr);
  708. // Create an iterator over the result of the above dataset
  709. // This will trigger the creation of the Execution Tree and launch it.
  710. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  711. EXPECT_NE(iter, nullptr);
  712. // Iterate the dataset and get each row
  713. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  714. iter->GetNextRow(&row);
  715. uint64_t i = 0;
  716. while (row.size() != 0) {
  717. i++;
  718. auto image = row["image"];
  719. MS_LOG(INFO) << "Tensor image shape: " << image->shape();
  720. iter->GetNextRow(&row);
  721. }
  722. EXPECT_EQ(i, 20);
  723. // Manually terminate the pipeline
  724. iter->Stop();
  725. }
  726. TEST_F(MindDataTestPipeline, TestProjectMap) {
  727. // Create an ImageFolder Dataset
  728. std::string folder_path = datasets_root_path_ + "/testPK/data/";
  729. std::shared_ptr<Dataset> ds = ImageFolder(folder_path, true, RandomSampler(false, 10));
  730. EXPECT_NE(ds, nullptr);
  731. // Create a Repeat operation on ds
  732. int32_t repeat_num = 2;
  733. ds = ds->Repeat(repeat_num);
  734. EXPECT_NE(ds, nullptr);
  735. // Create objects for the tensor ops
  736. std::shared_ptr<TensorOperation> random_vertical_flip_op = vision::RandomVerticalFlip(0.5);
  737. EXPECT_NE(random_vertical_flip_op, nullptr);
  738. // Create a Map operation on ds
  739. ds = ds->Map({random_vertical_flip_op}, {}, {}, {"image", "label"});
  740. EXPECT_NE(ds, nullptr);
  741. // Create a Project operation on ds
  742. std::vector<std::string> column_project = {"image"};
  743. ds = ds->Project(column_project);
  744. EXPECT_NE(ds, nullptr);
  745. // Create a Batch operation on ds
  746. int32_t batch_size = 1;
  747. ds = ds->Batch(batch_size);
  748. EXPECT_NE(ds, nullptr);
  749. // Create an iterator over the result of the above dataset
  750. // This will trigger the creation of the Execution Tree and launch it.
  751. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  752. EXPECT_NE(iter, nullptr);
  753. // Iterate the dataset and get each row
  754. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  755. iter->GetNextRow(&row);
  756. uint64_t i = 0;
  757. while (row.size() != 0) {
  758. i++;
  759. auto image = row["image"];
  760. MS_LOG(INFO) << "Tensor image shape: " << image->shape();
  761. iter->GetNextRow(&row);
  762. }
  763. EXPECT_EQ(i, 20);
  764. // Manually terminate the pipeline
  765. iter->Stop();
  766. }
  767. TEST_F(MindDataTestPipeline, TestZipSuccess) {
  768. // Create an ImageFolder Dataset
  769. std::string folder_path = datasets_root_path_ + "/testPK/data/";
  770. std::shared_ptr<Dataset> ds = ImageFolder(folder_path, true, RandomSampler(false, 10));
  771. EXPECT_NE(ds, nullptr);
  772. // Create a Project operation on ds
  773. std::vector<std::string> column_project = {"image"};
  774. ds = ds->Project(column_project);
  775. EXPECT_NE(ds, nullptr);
  776. // Create an ImageFolder Dataset
  777. std::shared_ptr<Dataset> ds1 = ImageFolder(folder_path, true, RandomSampler(false, 10));
  778. EXPECT_NE(ds1, nullptr);
  779. // Create a Rename operation on ds (so that the 3 datasets we are going to zip have distinct column names)
  780. ds1 = ds1->Rename({"image", "label"}, {"col1", "col2"});
  781. EXPECT_NE(ds1, nullptr);
  782. folder_path = datasets_root_path_ + "/testCifar10Data/";
  783. std::shared_ptr<Dataset> ds2 = Cifar10(folder_path, RandomSampler(false, 10));
  784. EXPECT_NE(ds2, nullptr);
  785. // Create a Project operation on ds
  786. column_project = {"label"};
  787. ds2 = ds2->Project(column_project);
  788. EXPECT_NE(ds2, nullptr);
  789. // Create a Zip operation on the datasets
  790. ds = ds->Zip({ds, ds1, ds2});
  791. EXPECT_NE(ds, nullptr);
  792. // Create a Batch operation on ds
  793. int32_t batch_size = 1;
  794. ds = ds->Batch(batch_size);
  795. EXPECT_NE(ds, nullptr);
  796. // Create an iterator over the result of the above dataset
  797. // This will trigger the creation of the Execution Tree and launch it.
  798. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  799. EXPECT_NE(iter, nullptr);
  800. // Iterate the dataset and get each row
  801. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  802. iter->GetNextRow(&row);
  803. uint64_t i = 0;
  804. while (row.size() != 0) {
  805. i++;
  806. auto image = row["image"];
  807. MS_LOG(INFO) << "Tensor image shape: " << image->shape();
  808. iter->GetNextRow(&row);
  809. }
  810. EXPECT_EQ(i, 10);
  811. // Manually terminate the pipeline
  812. iter->Stop();
  813. }
  814. TEST_F(MindDataTestPipeline, TestZipFail) {
  815. // We expect this test to fail because we are the both datasets we are zipping have "image" and "label" columns
  816. // and zip doesn't accept datasets with same column names
  817. // Create an ImageFolder Dataset
  818. std::string folder_path = datasets_root_path_ + "/testPK/data/";
  819. std::shared_ptr<Dataset> ds = ImageFolder(folder_path, true, RandomSampler(false, 10));
  820. EXPECT_NE(ds, nullptr);
  821. // Create an ImageFolder Dataset
  822. std::shared_ptr<Dataset> ds1 = ImageFolder(folder_path, true, RandomSampler(false, 10));
  823. EXPECT_NE(ds1, nullptr);
  824. // Create a Zip operation on the datasets
  825. ds = ds->Zip({ds, ds1});
  826. EXPECT_NE(ds, nullptr);
  827. // Create a Batch operation on ds
  828. int32_t batch_size = 1;
  829. ds = ds->Batch(batch_size);
  830. EXPECT_NE(ds, nullptr);
  831. // Create an iterator over the result of the above dataset
  832. // This will trigger the creation of the Execution Tree and launch it.
  833. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  834. EXPECT_EQ(iter, nullptr);
  835. }
  836. TEST_F(MindDataTestPipeline, TestRenameSuccess) {
  837. // Create an ImageFolder Dataset
  838. std::string folder_path = datasets_root_path_ + "/testPK/data/";
  839. std::shared_ptr<Dataset> ds = ImageFolder(folder_path, true, RandomSampler(false, 10));
  840. EXPECT_NE(ds, nullptr);
  841. // Create a Repeat operation on ds
  842. int32_t repeat_num = 2;
  843. ds = ds->Repeat(repeat_num);
  844. EXPECT_NE(ds, nullptr);
  845. // Create a Rename operation on ds
  846. ds = ds->Rename({"image", "label"}, {"col1", "col2"});
  847. EXPECT_NE(ds, nullptr);
  848. // Create a Batch operation on ds
  849. int32_t batch_size = 1;
  850. ds = ds->Batch(batch_size);
  851. EXPECT_NE(ds, nullptr);
  852. // Create an iterator over the result of the above dataset
  853. // This will trigger the creation of the Execution Tree and launch it.
  854. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  855. EXPECT_NE(iter, nullptr);
  856. // Iterate the dataset and get each row
  857. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  858. iter->GetNextRow(&row);
  859. uint64_t i = 0;
  860. EXPECT_NE(row.find("col1"), row.end());
  861. EXPECT_NE(row.find("col2"), row.end());
  862. EXPECT_EQ(row.find("image"), row.end());
  863. EXPECT_EQ(row.find("label"), row.end());
  864. while (row.size() != 0) {
  865. i++;
  866. auto image = row["col1"];
  867. MS_LOG(INFO) << "Tensor image shape: " << image->shape();
  868. iter->GetNextRow(&row);
  869. }
  870. EXPECT_EQ(i, 20);
  871. // Manually terminate the pipeline
  872. iter->Stop();
  873. }
  874. TEST_F(MindDataTestPipeline, TestRenameFail) {
  875. // We expect this test to fail because input and output in Rename are not the same size
  876. // Create an ImageFolder Dataset
  877. std::string folder_path = datasets_root_path_ + "/testPK/data/";
  878. std::shared_ptr<Dataset> ds = ImageFolder(folder_path, true, RandomSampler(false, 10));
  879. EXPECT_NE(ds, nullptr);
  880. // Create a Repeat operation on ds
  881. int32_t repeat_num = 2;
  882. ds = ds->Repeat(repeat_num);
  883. EXPECT_NE(ds, nullptr);
  884. // Create a Rename operation on ds
  885. ds = ds->Rename({"image", "label"}, {"col2"});
  886. EXPECT_EQ(ds, nullptr);
  887. }
  888. TEST_F(MindDataTestPipeline, TestVOCSegmentation) {
  889. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestVOCSegmentation.";
  890. // Create a VOC Dataset
  891. std::string folder_path = datasets_root_path_ + "/testVOC2012_2";
  892. std::shared_ptr<Dataset> ds = VOC(folder_path, "Segmentation", "train", {}, false, SequentialSampler(0, 3));
  893. EXPECT_NE(ds, nullptr);
  894. // Create a Repeat operation on ds
  895. int32_t repeat_num = 2;
  896. ds = ds->Repeat(repeat_num);
  897. EXPECT_NE(ds, nullptr);
  898. // Create an iterator over the result of the above dataset
  899. // This will trigger the creation of the Execution Tree and launch it.
  900. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  901. EXPECT_NE(iter, nullptr);
  902. // Iterate the dataset and get each row
  903. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  904. iter->GetNextRow(&row);
  905. // Check if VOCOp read correct images/targets
  906. using Tensor = mindspore::dataset::Tensor;
  907. std::string expect_file[] = {"32", "33", "39", "32", "33", "39"};
  908. uint64_t i = 0;
  909. while (row.size() != 0) {
  910. auto image = row["image"];
  911. auto target = row["target"];
  912. MS_LOG(INFO) << "Tensor image shape: " << image->shape();
  913. MS_LOG(INFO) << "Tensor target shape: " << target->shape();
  914. std::shared_ptr<Tensor> expect_image;
  915. Tensor::CreateFromFile(folder_path + "/JPEGImages/" + expect_file[i] + ".jpg", &expect_image);
  916. EXPECT_EQ(*image, *expect_image);
  917. std::shared_ptr<Tensor> expect_target;
  918. Tensor::CreateFromFile(folder_path + "/SegmentationClass/" + expect_file[i] + ".png", &expect_target);
  919. EXPECT_EQ(*target, *expect_target);
  920. iter->GetNextRow(&row);
  921. i++;
  922. }
  923. EXPECT_EQ(i, 6);
  924. // Manually terminate the pipeline
  925. iter->Stop();
  926. }
  927. TEST_F(MindDataTestPipeline, TestVOCSegmentationError1) {
  928. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestVOCSegmentationError1.";
  929. // Create a VOC Dataset
  930. std::map<std::string, int32_t> class_index;
  931. class_index["car"] = 0;
  932. std::string folder_path = datasets_root_path_ + "/testVOC2012_2";
  933. std::shared_ptr<Dataset> ds = VOC(folder_path, "Segmentation", "train", class_index, false, RandomSampler(false, 6));
  934. // Expect nullptr for segmentation task with class_index
  935. EXPECT_EQ(ds, nullptr);
  936. }
  937. TEST_F(MindDataTestPipeline, TestVOCInvalidTaskOrMode) {
  938. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestVOCInvalidTaskOrMode.";
  939. // Create a VOC Dataset
  940. std::string folder_path = datasets_root_path_ + "/testVOC2012_2";
  941. std::shared_ptr<Dataset> ds_1 = VOC(folder_path, "Classification", "train", {}, false, SequentialSampler(0, 3));
  942. // Expect nullptr for invalid task
  943. EXPECT_EQ(ds_1, nullptr);
  944. std::shared_ptr<Dataset> ds_2 = VOC(folder_path, "Segmentation", "validation", {}, false, RandomSampler(false, 4));
  945. // Expect nullptr for invalid mode
  946. EXPECT_EQ(ds_2, nullptr);
  947. }
  948. TEST_F(MindDataTestPipeline, TestVOCDetection) {
  949. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestVOCDetection.";
  950. // Create a VOC Dataset
  951. std::string folder_path = datasets_root_path_ + "/testVOC2012_2";
  952. std::shared_ptr<Dataset> ds = VOC(folder_path, "Detection", "train", {}, false, SequentialSampler(0, 4));
  953. EXPECT_NE(ds, nullptr);
  954. // Create an iterator over the result of the above dataset
  955. // This will trigger the creation of the Execution Tree and launch it.
  956. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  957. EXPECT_NE(iter, nullptr);
  958. // Iterate the dataset and get each row
  959. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  960. iter->GetNextRow(&row);
  961. // Check if VOCOp read correct images/labels
  962. std::string expect_file[] = {"15", "32", "33", "39"};
  963. uint32_t expect_num[] = {5, 5, 4, 3};
  964. uint64_t i = 0;
  965. while (row.size() != 0) {
  966. auto image = row["image"];
  967. auto label = row["label"];
  968. MS_LOG(INFO) << "Tensor image shape: " << image->shape();
  969. MS_LOG(INFO) << "Tensor label shape: " << label->shape();
  970. std::shared_ptr<Tensor> expect_image;
  971. Tensor::CreateFromFile(folder_path + "/JPEGImages/" + expect_file[i] + ".jpg", &expect_image);
  972. EXPECT_EQ(*image, *expect_image);
  973. std::shared_ptr<Tensor> expect_label;
  974. Tensor::CreateFromMemory(TensorShape({1, 1}), DataType(DataType::DE_UINT32), nullptr, &expect_label);
  975. expect_label->SetItemAt({0, 0}, expect_num[i]);
  976. EXPECT_EQ(*label, *expect_label);
  977. iter->GetNextRow(&row);
  978. i++;
  979. }
  980. EXPECT_EQ(i, 4);
  981. // Manually terminate the pipeline
  982. iter->Stop();
  983. }
  984. TEST_F(MindDataTestPipeline, TestVOCClassIndex) {
  985. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestVOCClassIndex.";
  986. // Create a VOC Dataset
  987. std::string folder_path = datasets_root_path_ + "/testVOC2012_2";
  988. std::map<std::string, int32_t> class_index;
  989. class_index["car"] = 0;
  990. class_index["cat"] = 1;
  991. class_index["train"] = 9;
  992. std::shared_ptr<Dataset> ds = VOC(folder_path, "Detection", "train", class_index, false, SequentialSampler(0, 6));
  993. EXPECT_NE(ds, nullptr);
  994. // Create an iterator over the result of the above dataset
  995. // This will trigger the creation of the Execution Tree and launch it.
  996. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  997. EXPECT_NE(iter, nullptr);
  998. // Iterate the dataset and get each row
  999. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  1000. iter->GetNextRow(&row);
  1001. // Check if VOCOp read correct labels
  1002. // When we provide class_index, label of ["car","cat","train"] become [0,1,9]
  1003. std::shared_ptr<Tensor> expect_label;
  1004. Tensor::CreateFromMemory(TensorShape({1, 1}), DataType(DataType::DE_UINT32), nullptr, &expect_label);
  1005. uint32_t expect[] = {9, 9, 9, 1, 1, 0};
  1006. uint64_t i = 0;
  1007. while (row.size() != 0) {
  1008. auto image = row["image"];
  1009. auto label = row["label"];
  1010. MS_LOG(INFO) << "Tensor image shape: " << image->shape();
  1011. MS_LOG(INFO) << "Tensor label shape: " << label->shape();
  1012. expect_label->SetItemAt({0, 0}, expect[i]);
  1013. EXPECT_EQ(*label, *expect_label);
  1014. iter->GetNextRow(&row);
  1015. i++;
  1016. }
  1017. EXPECT_EQ(i, 6);
  1018. // Manually terminate the pipeline
  1019. iter->Stop();
  1020. }