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

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978
  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/include/samplers.h"
  32. using namespace mindspore::dataset::api;
  33. using mindspore::MsLogLevel::ERROR;
  34. using mindspore::ExceptionType::NoExceptionType;
  35. using mindspore::LogStream;
  36. using mindspore::dataset::Tensor;
  37. using mindspore::dataset::Status;
  38. using mindspore::dataset::BorderType;
  39. class MindDataTestPipeline : public UT::DatasetOpTesting {
  40. protected:
  41. };
  42. TEST_F(MindDataTestPipeline, TestBatchAndRepeat) {
  43. // Create a Mnist Dataset
  44. std::string folder_path = datasets_root_path_ + "/testMnistData/";
  45. std::shared_ptr<Dataset> ds = Mnist(folder_path, RandomSampler(false, 10));
  46. EXPECT_NE(ds, nullptr);
  47. // Create a Repeat operation on ds
  48. int32_t repeat_num = 2;
  49. ds = ds->Repeat(repeat_num);
  50. EXPECT_NE(ds, nullptr);
  51. // Create a Batch operation on ds
  52. int32_t batch_size = 2;
  53. ds = ds->Batch(batch_size);
  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. MS_LOG(INFO) << "Tensor image shape: " << image->shape();
  67. iter->GetNextRow(&row);
  68. }
  69. EXPECT_EQ(i, 10);
  70. // Manually terminate the pipeline
  71. iter->Stop();
  72. }
  73. TEST_F(MindDataTestPipeline, TestTensorOpsAndMap) {
  74. // Create a Mnist Dataset
  75. std::string folder_path = datasets_root_path_ + "/testMnistData/";
  76. std::shared_ptr<Dataset> ds = Mnist(folder_path, RandomSampler(false, 20));
  77. EXPECT_NE(ds, nullptr);
  78. // Create a Repeat operation on ds
  79. int32_t repeat_num = 2;
  80. ds = ds->Repeat(repeat_num);
  81. EXPECT_NE(ds, nullptr);
  82. // Create objects for the tensor ops
  83. std::shared_ptr<TensorOperation> resize_op = vision::Resize({30, 30});
  84. EXPECT_NE(resize_op, nullptr);
  85. std::shared_ptr<TensorOperation> center_crop_op = vision::CenterCrop({16, 16});
  86. EXPECT_NE(center_crop_op, nullptr);
  87. // Create a Map operation on ds
  88. ds = ds->Map({resize_op, center_crop_op});
  89. EXPECT_NE(ds, nullptr);
  90. // Create a Batch operation on ds
  91. int32_t batch_size = 1;
  92. ds = ds->Batch(batch_size);
  93. EXPECT_NE(ds, nullptr);
  94. // Create an iterator over the result of the above dataset
  95. // This will trigger the creation of the Execution Tree and launch it.
  96. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  97. EXPECT_NE(iter, nullptr);
  98. // Iterate the dataset and get each row
  99. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  100. iter->GetNextRow(&row);
  101. uint64_t i = 0;
  102. while (row.size() != 0) {
  103. i++;
  104. auto image = row["image"];
  105. MS_LOG(INFO) << "Tensor image shape: " << image->shape();
  106. iter->GetNextRow(&row);
  107. }
  108. EXPECT_EQ(i, 40);
  109. // Manually terminate the pipeline
  110. iter->Stop();
  111. }
  112. TEST_F(MindDataTestPipeline, TestUniformAugWithOps) {
  113. // Create a Mnist Dataset
  114. std::string folder_path = datasets_root_path_ + "/testMnistData/";
  115. std::shared_ptr<Dataset> ds = Mnist(folder_path, RandomSampler(false, 20));
  116. EXPECT_NE(ds, nullptr);
  117. // Create a Repeat operation on ds
  118. int32_t repeat_num = 1;
  119. ds = ds->Repeat(repeat_num);
  120. EXPECT_NE(ds, nullptr);
  121. // Create objects for the tensor ops
  122. std::shared_ptr<TensorOperation> resize_op = vision::Resize({30, 30});
  123. EXPECT_NE(resize_op, nullptr);
  124. std::shared_ptr<TensorOperation> random_crop_op = vision::RandomCrop({28, 28});
  125. EXPECT_NE(random_crop_op, nullptr);
  126. std::shared_ptr<TensorOperation> center_crop_op = vision::CenterCrop({16, 16});
  127. EXPECT_NE(center_crop_op, nullptr);
  128. std::shared_ptr<TensorOperation> uniform_aug_op = vision::UniformAugment({random_crop_op, center_crop_op}, 2);
  129. EXPECT_NE(uniform_aug_op, nullptr);
  130. // Create a Map operation on ds
  131. ds = ds->Map({resize_op, uniform_aug_op});
  132. EXPECT_NE(ds, nullptr);
  133. // Create an iterator over the result of the above dataset
  134. // This will trigger the creation of the Execution Tree and launch it.
  135. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  136. EXPECT_NE(iter, nullptr);
  137. // Iterate the dataset and get each row
  138. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  139. iter->GetNextRow(&row);
  140. uint64_t i = 0;
  141. while (row.size() != 0) {
  142. i++;
  143. auto image = row["image"];
  144. MS_LOG(INFO) << "Tensor image shape: " << image->shape();
  145. iter->GetNextRow(&row);
  146. }
  147. EXPECT_EQ(i, 20);
  148. // Manually terminate the pipeline
  149. iter->Stop();
  150. }
  151. TEST_F(MindDataTestPipeline, TestRandomFlip) {
  152. // Create an ImageFolder Dataset
  153. std::string folder_path = datasets_root_path_ + "/testPK/data/";
  154. std::shared_ptr<Dataset> ds = ImageFolder(folder_path, true, RandomSampler(false, 10));
  155. EXPECT_NE(ds, nullptr);
  156. // Create a Repeat operation on ds
  157. int32_t repeat_num = 2;
  158. ds = ds->Repeat(repeat_num);
  159. EXPECT_NE(ds, nullptr);
  160. // Create objects for the tensor ops
  161. std::shared_ptr<TensorOperation> random_vertical_flip_op = vision::RandomVerticalFlip(0.5);
  162. EXPECT_NE(random_vertical_flip_op, nullptr);
  163. std::shared_ptr<TensorOperation> random_horizontal_flip_op = vision::RandomHorizontalFlip(0.5);
  164. EXPECT_NE(random_horizontal_flip_op, nullptr);
  165. // Create a Map operation on ds
  166. ds = ds->Map({random_vertical_flip_op, random_horizontal_flip_op});
  167. EXPECT_NE(ds, nullptr);
  168. // Create a Batch operation on ds
  169. int32_t batch_size = 1;
  170. ds = ds->Batch(batch_size);
  171. EXPECT_NE(ds, nullptr);
  172. // Create an iterator over the result of the above dataset
  173. // This will trigger the creation of the Execution Tree and launch it.
  174. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  175. EXPECT_NE(iter, nullptr);
  176. // Iterate the dataset and get each row
  177. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  178. iter->GetNextRow(&row);
  179. uint64_t i = 0;
  180. while (row.size() != 0) {
  181. i++;
  182. auto image = row["image"];
  183. MS_LOG(INFO) << "Tensor image shape: " << image->shape();
  184. iter->GetNextRow(&row);
  185. }
  186. EXPECT_EQ(i, 20);
  187. // Manually terminate the pipeline
  188. iter->Stop();
  189. }
  190. TEST_F(MindDataTestPipeline, TestImageFolderBatchAndRepeat) {
  191. // Create an ImageFolder Dataset
  192. std::string folder_path = datasets_root_path_ + "/testPK/data/";
  193. std::shared_ptr<Dataset> ds = ImageFolder(folder_path, true, RandomSampler(false, 10));
  194. EXPECT_NE(ds, nullptr);
  195. // Create a Repeat operation on ds
  196. int32_t repeat_num = 2;
  197. ds = ds->Repeat(repeat_num);
  198. EXPECT_NE(ds, nullptr);
  199. // Create a Batch operation on ds
  200. int32_t batch_size = 2;
  201. ds = ds->Batch(batch_size);
  202. EXPECT_NE(ds, nullptr);
  203. // Create an iterator over the result of the above dataset
  204. // This will trigger the creation of the Execution Tree and launch it.
  205. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  206. EXPECT_NE(iter, nullptr);
  207. // Iterate the dataset and get each row
  208. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  209. iter->GetNextRow(&row);
  210. uint64_t i = 0;
  211. while (row.size() != 0) {
  212. i++;
  213. auto image = row["image"];
  214. MS_LOG(INFO) << "Tensor image shape: " << image->shape();
  215. iter->GetNextRow(&row);
  216. }
  217. EXPECT_EQ(i, 10);
  218. // Manually terminate the pipeline
  219. iter->Stop();
  220. }
  221. TEST_F(MindDataTestPipeline, TestImageFolderWithSamplers) {
  222. std::shared_ptr<SamplerObj> sampl = DistributedSampler(2, 1);
  223. EXPECT_NE(sampl, nullptr);
  224. sampl = PKSampler(3);
  225. EXPECT_NE(sampl, nullptr);
  226. sampl = RandomSampler(false, 12);
  227. EXPECT_NE(sampl, nullptr);
  228. sampl = SequentialSampler(0, 12);
  229. EXPECT_NE(sampl, nullptr);
  230. 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};
  231. sampl = WeightedRandomSampler(weights, 12);
  232. EXPECT_NE(sampl, nullptr);
  233. std::vector<int64_t> indices = {1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23};
  234. sampl = SubsetRandomSampler(indices);
  235. EXPECT_NE(sampl, nullptr);
  236. // Create an ImageFolder Dataset
  237. std::string folder_path = datasets_root_path_ + "/testPK/data/";
  238. std::shared_ptr<Dataset> ds = ImageFolder(folder_path, false, sampl);
  239. EXPECT_NE(ds, nullptr);
  240. // Create a Repeat operation on ds
  241. int32_t repeat_num = 2;
  242. ds = ds->Repeat(repeat_num);
  243. EXPECT_NE(ds, nullptr);
  244. // Create a Batch operation on ds
  245. int32_t batch_size = 2;
  246. ds = ds->Batch(batch_size);
  247. EXPECT_NE(ds, nullptr);
  248. // Create an iterator over the result of the above dataset
  249. // This will trigger the creation of the Execution Tree and launch it.
  250. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  251. EXPECT_NE(iter, nullptr);
  252. // Iterate the dataset and get each row
  253. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  254. iter->GetNextRow(&row);
  255. uint64_t i = 0;
  256. while (row.size() != 0) {
  257. i++;
  258. auto image = row["image"];
  259. MS_LOG(INFO) << "Tensor image shape: " << image->shape();
  260. iter->GetNextRow(&row);
  261. }
  262. EXPECT_EQ(i, 12);
  263. // Manually terminate the pipeline
  264. iter->Stop();
  265. }
  266. TEST_F(MindDataTestPipeline, TestPad) {
  267. // Create an ImageFolder Dataset
  268. std::string folder_path = datasets_root_path_ + "/testPK/data/";
  269. std::shared_ptr<Dataset> ds = ImageFolder(folder_path, true, RandomSampler(false, 10));
  270. EXPECT_NE(ds, nullptr);
  271. // Create a Repeat operation on ds
  272. int32_t repeat_num = 2;
  273. ds = ds->Repeat(repeat_num);
  274. EXPECT_NE(ds, nullptr);
  275. // Create objects for the tensor ops
  276. std::shared_ptr<TensorOperation> pad_op1 = vision::Pad({1, 2, 3, 4}, {0}, BorderType::kSymmetric);
  277. EXPECT_NE(pad_op1, nullptr);
  278. std::shared_ptr<TensorOperation> pad_op2 = vision::Pad({1}, {1, 1, 1}, BorderType::kEdge);
  279. EXPECT_NE(pad_op2, nullptr);
  280. std::shared_ptr<TensorOperation> pad_op3 = vision::Pad({1, 4});
  281. EXPECT_NE(pad_op3, nullptr);
  282. // Create a Map operation on ds
  283. ds = ds->Map({pad_op1, pad_op2, pad_op3});
  284. EXPECT_NE(ds, nullptr);
  285. // Create a Batch operation on ds
  286. int32_t batch_size = 1;
  287. ds = ds->Batch(batch_size);
  288. EXPECT_NE(ds, nullptr);
  289. // Create an iterator over the result of the above dataset
  290. // This will trigger the creation of the Execution Tree and launch it.
  291. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  292. EXPECT_NE(iter, nullptr);
  293. // Iterate the dataset and get each row
  294. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  295. iter->GetNextRow(&row);
  296. uint64_t i = 0;
  297. while (row.size() != 0) {
  298. i++;
  299. auto image = row["image"];
  300. MS_LOG(INFO) << "Tensor image shape: " << image->shape();
  301. iter->GetNextRow(&row);
  302. }
  303. EXPECT_EQ(i, 20);
  304. // Manually terminate the pipeline
  305. iter->Stop();
  306. }
  307. TEST_F(MindDataTestPipeline, TestCutOut) {
  308. // Create an ImageFolder Dataset
  309. std::string folder_path = datasets_root_path_ + "/testPK/data/";
  310. std::shared_ptr<Dataset> ds = ImageFolder(folder_path, true, RandomSampler(false, 10));
  311. EXPECT_NE(ds, nullptr);
  312. // Create a Repeat operation on ds
  313. int32_t repeat_num = 2;
  314. ds = ds->Repeat(repeat_num);
  315. EXPECT_NE(ds, nullptr);
  316. // Create objects for the tensor ops
  317. std::shared_ptr<TensorOperation> cut_out1 = vision::CutOut(30, 5);
  318. EXPECT_NE(cut_out1, nullptr);
  319. std::shared_ptr<TensorOperation> cut_out2 = vision::CutOut(30);
  320. EXPECT_NE(cut_out2, nullptr);
  321. // Create a Map operation on ds
  322. ds = ds->Map({cut_out1, cut_out2});
  323. EXPECT_NE(ds, nullptr);
  324. // Create a Batch operation on ds
  325. int32_t batch_size = 1;
  326. ds = ds->Batch(batch_size);
  327. EXPECT_NE(ds, nullptr);
  328. // Create an iterator over the result of the above dataset
  329. // This will trigger the creation of the Execution Tree and launch it.
  330. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  331. EXPECT_NE(iter, nullptr);
  332. // Iterate the dataset and get each row
  333. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  334. iter->GetNextRow(&row);
  335. uint64_t i = 0;
  336. while (row.size() != 0) {
  337. i++;
  338. auto image = row["image"];
  339. MS_LOG(INFO) << "Tensor image shape: " << image->shape();
  340. iter->GetNextRow(&row);
  341. }
  342. EXPECT_EQ(i, 20);
  343. // Manually terminate the pipeline
  344. iter->Stop();
  345. }
  346. TEST_F(MindDataTestPipeline, TestNormalize) {
  347. // Create an ImageFolder Dataset
  348. std::string folder_path = datasets_root_path_ + "/testPK/data/";
  349. std::shared_ptr<Dataset> ds = ImageFolder(folder_path, true, RandomSampler(false, 10));
  350. EXPECT_NE(ds, nullptr);
  351. // Create a Repeat operation on ds
  352. int32_t repeat_num = 2;
  353. ds = ds->Repeat(repeat_num);
  354. EXPECT_NE(ds, nullptr);
  355. // Create objects for the tensor ops
  356. std::shared_ptr<TensorOperation> normalize = vision::Normalize({121.0, 115.0, 100.0}, {70.0, 68.0, 71.0});
  357. EXPECT_NE(normalize, nullptr);
  358. // Create a Map operation on ds
  359. ds = ds->Map({normalize});
  360. EXPECT_NE(ds, nullptr);
  361. // Create a Batch operation on ds
  362. int32_t batch_size = 1;
  363. ds = ds->Batch(batch_size);
  364. EXPECT_NE(ds, nullptr);
  365. // Create an iterator over the result of the above dataset
  366. // This will trigger the creation of the Execution Tree and launch it.
  367. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  368. EXPECT_NE(iter, nullptr);
  369. // Iterate the dataset and get each row
  370. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  371. iter->GetNextRow(&row);
  372. uint64_t i = 0;
  373. while (row.size() != 0) {
  374. i++;
  375. auto image = row["image"];
  376. MS_LOG(INFO) << "Tensor image shape: " << image->shape();
  377. iter->GetNextRow(&row);
  378. }
  379. EXPECT_EQ(i, 20);
  380. // Manually terminate the pipeline
  381. iter->Stop();
  382. }
  383. TEST_F(MindDataTestPipeline, TestDecode) {
  384. // Create an ImageFolder Dataset
  385. std::string folder_path = datasets_root_path_ + "/testPK/data/";
  386. std::shared_ptr<Dataset> ds = ImageFolder(folder_path, false, RandomSampler(false, 10));
  387. EXPECT_NE(ds, nullptr);
  388. // Create a Repeat operation on ds
  389. int32_t repeat_num = 2;
  390. ds = ds->Repeat(repeat_num);
  391. EXPECT_NE(ds, nullptr);
  392. // Create objects for the tensor ops
  393. std::shared_ptr<TensorOperation> decode = vision::Decode(true);
  394. EXPECT_NE(decode, nullptr);
  395. // Create a Map operation on ds
  396. ds = ds->Map({decode});
  397. EXPECT_NE(ds, nullptr);
  398. // Create a Batch operation on ds
  399. int32_t batch_size = 1;
  400. ds = ds->Batch(batch_size);
  401. EXPECT_NE(ds, nullptr);
  402. // Create an iterator over the result of the above dataset
  403. // This will trigger the creation of the Execution Tree and launch it.
  404. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  405. EXPECT_NE(iter, nullptr);
  406. // Iterate the dataset and get each row
  407. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  408. iter->GetNextRow(&row);
  409. uint64_t i = 0;
  410. while (row.size() != 0) {
  411. i++;
  412. auto image = row["image"];
  413. MS_LOG(INFO) << "Tensor image shape: " << image->shape();
  414. iter->GetNextRow(&row);
  415. }
  416. EXPECT_EQ(i, 20);
  417. // Manually terminate the pipeline
  418. iter->Stop();
  419. }
  420. TEST_F(MindDataTestPipeline, TestShuffleDataset) {
  421. // Create an ImageFolder Dataset
  422. std::string folder_path = datasets_root_path_ + "/testPK/data/";
  423. std::shared_ptr<Dataset> ds = ImageFolder(folder_path, true, RandomSampler(false, 10));
  424. EXPECT_NE(ds, nullptr);
  425. // Create a Shuffle operation on ds
  426. int32_t shuffle_size = 10;
  427. ds = ds->Shuffle(shuffle_size);
  428. EXPECT_NE(ds, nullptr);
  429. // Create a Repeat operation on ds
  430. int32_t repeat_num = 2;
  431. ds = ds->Repeat(repeat_num);
  432. EXPECT_NE(ds, nullptr);
  433. // Create a Batch operation on ds
  434. int32_t batch_size = 2;
  435. ds = ds->Batch(batch_size);
  436. EXPECT_NE(ds, nullptr);
  437. // Create an iterator over the result of the above dataset
  438. // This will trigger the creation of the Execution Tree and launch it.
  439. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  440. EXPECT_NE(iter, nullptr);
  441. // Iterate the dataset and get each row
  442. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  443. iter->GetNextRow(&row);
  444. uint64_t i = 0;
  445. while (row.size() != 0) {
  446. i++;
  447. auto image = row["image"];
  448. MS_LOG(INFO) << "Tensor image shape: " << image->shape();
  449. iter->GetNextRow(&row);
  450. }
  451. EXPECT_EQ(i, 10);
  452. // Manually terminate the pipeline
  453. iter->Stop();
  454. }
  455. TEST_F(MindDataTestPipeline, TestSkipDataset) {
  456. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestSkipDataset.";
  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 Skip operation on ds
  462. int32_t count = 3;
  463. ds = ds->Skip(count);
  464. EXPECT_NE(ds, nullptr);
  465. // Create an iterator over the result of the above dataset
  466. // This will trigger the creation of the Execution Tree and launch it.
  467. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  468. EXPECT_NE(iter, nullptr);
  469. // Iterate the dataset and get each row
  470. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  471. iter->GetNextRow(&row);
  472. uint64_t i = 0;
  473. while (row.size() != 0) {
  474. i++;
  475. auto image = row["image"];
  476. MS_LOG(INFO) << "Tensor image shape: " << image->shape();
  477. iter->GetNextRow(&row);
  478. }
  479. MS_LOG(INFO) << "Number of rows: " << i;
  480. // Expect 10-3=7 rows
  481. EXPECT_EQ(i, 7);
  482. // Manually terminate the pipeline
  483. iter->Stop();
  484. }
  485. TEST_F(MindDataTestPipeline, TestSkipDatasetError1) {
  486. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestSkipDatasetError1.";
  487. // Create an ImageFolder Dataset
  488. std::string folder_path = datasets_root_path_ + "/testPK/data/";
  489. std::shared_ptr<Dataset> ds = ImageFolder(folder_path, true, RandomSampler(false, 10));
  490. EXPECT_NE(ds, nullptr);
  491. // Create a Skip operation on ds with invalid count input
  492. int32_t count = -1;
  493. ds = ds->Skip(count);
  494. // Expect nullptr for invalid input skip_count
  495. EXPECT_EQ(ds, nullptr);
  496. }
  497. TEST_F(MindDataTestPipeline, TestCifar10Dataset) {
  498. // Create a Cifar10 Dataset
  499. std::string folder_path = datasets_root_path_ + "/testCifar10Data/";
  500. std::shared_ptr<Dataset> ds = Cifar10(folder_path, 0, RandomSampler(false, 10));
  501. EXPECT_NE(ds, nullptr);
  502. // Create a Repeat operation on ds
  503. int32_t repeat_num = 2;
  504. ds = ds->Repeat(repeat_num);
  505. EXPECT_NE(ds, nullptr);
  506. // Create a Batch operation on ds
  507. int32_t batch_size = 2;
  508. ds = ds->Batch(batch_size);
  509. EXPECT_NE(ds, nullptr);
  510. // Create an iterator over the result of the above dataset
  511. // This will trigger the creation of the Execution Tree and launch it.
  512. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  513. EXPECT_NE(iter, nullptr);
  514. // Iterate the dataset and get each row
  515. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  516. iter->GetNextRow(&row);
  517. uint64_t i = 0;
  518. while (row.size() != 0) {
  519. i++;
  520. auto image = row["image"];
  521. MS_LOG(INFO) << "Tensor image shape: " << image->shape();
  522. iter->GetNextRow(&row);
  523. }
  524. EXPECT_EQ(i, 10);
  525. // Manually terminate the pipeline
  526. iter->Stop();
  527. }
  528. TEST_F(MindDataTestPipeline, TestRandomColorAdjust) {
  529. // Create an ImageFolder Dataset
  530. std::string folder_path = datasets_root_path_ + "/testPK/data/";
  531. std::shared_ptr<Dataset> ds = ImageFolder(folder_path, true, RandomSampler(false, 10));
  532. EXPECT_NE(ds, nullptr);
  533. // Create a Repeat operation on ds
  534. int32_t repeat_num = 2;
  535. ds = ds->Repeat(repeat_num);
  536. EXPECT_NE(ds, nullptr);
  537. // Create objects for the tensor ops
  538. std::shared_ptr<TensorOperation> random_color_adjust1 = vision::RandomColorAdjust({1.0}, {0.0}, {0.5}, {0.5});
  539. EXPECT_NE(random_color_adjust1, nullptr);
  540. std::shared_ptr<TensorOperation> random_color_adjust2 = vision::RandomColorAdjust({1.0, 1.0}, {0.0, 0.0}, {0.5, 0.5},
  541. {0.5, 0.5});
  542. EXPECT_NE(random_color_adjust2, nullptr);
  543. std::shared_ptr<TensorOperation> random_color_adjust3 = vision::RandomColorAdjust({0.5, 1.0}, {0.0, 0.5}, {0.25, 0.5},
  544. {0.25, 0.5});
  545. EXPECT_NE(random_color_adjust3, nullptr);
  546. std::shared_ptr<TensorOperation> random_color_adjust4 = vision::RandomColorAdjust();
  547. EXPECT_NE(random_color_adjust4, nullptr);
  548. // Create a Map operation on ds
  549. ds = ds->Map({random_color_adjust1, random_color_adjust2, random_color_adjust3, random_color_adjust4});
  550. EXPECT_NE(ds, nullptr);
  551. // Create a Batch operation on ds
  552. int32_t batch_size = 1;
  553. ds = ds->Batch(batch_size);
  554. EXPECT_NE(ds, nullptr);
  555. // Create an iterator over the result of the above dataset
  556. // This will trigger the creation of the Execution Tree and launch it.
  557. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  558. EXPECT_NE(iter, nullptr);
  559. // Iterate the dataset and get each row
  560. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  561. iter->GetNextRow(&row);
  562. uint64_t i = 0;
  563. while (row.size() != 0) {
  564. i++;
  565. auto image = row["image"];
  566. MS_LOG(INFO) << "Tensor image shape: " << image->shape();
  567. iter->GetNextRow(&row);
  568. }
  569. EXPECT_EQ(i, 20);
  570. // Manually terminate the pipeline
  571. iter->Stop();
  572. }
  573. TEST_F(MindDataTestPipeline, TestRandomRotation) {
  574. // Create an ImageFolder Dataset
  575. std::string folder_path = datasets_root_path_ + "/testPK/data/";
  576. std::shared_ptr<Dataset> ds = ImageFolder(folder_path, true, RandomSampler(false, 10));
  577. EXPECT_NE(ds, nullptr);
  578. // Create a Repeat operation on ds
  579. int32_t repeat_num = 2;
  580. ds = ds->Repeat(repeat_num);
  581. EXPECT_NE(ds, nullptr);
  582. // Create objects for the tensor ops
  583. std::shared_ptr<TensorOperation> random_rotation_op = vision::RandomRotation({-180, 180});
  584. EXPECT_NE(random_rotation_op, nullptr);
  585. // Create a Map operation on ds
  586. ds = ds->Map({random_rotation_op});
  587. EXPECT_NE(ds, nullptr);
  588. // Create a Batch operation on ds
  589. int32_t batch_size = 1;
  590. ds = ds->Batch(batch_size);
  591. EXPECT_NE(ds, nullptr);
  592. // Create an iterator over the result of the above dataset
  593. // This will trigger the creation of the Execution Tree and launch it.
  594. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  595. EXPECT_NE(iter, nullptr);
  596. // Iterate the dataset and get each row
  597. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  598. iter->GetNextRow(&row);
  599. uint64_t i = 0;
  600. while (row.size() != 0) {
  601. i++;
  602. auto image = row["image"];
  603. MS_LOG(INFO) << "Tensor image shape: " << image->shape();
  604. iter->GetNextRow(&row);
  605. }
  606. EXPECT_EQ(i, 20);
  607. // Manually terminate the pipeline
  608. iter->Stop();
  609. }
  610. TEST_F(MindDataTestPipeline, TestProjectMap) {
  611. // Create an ImageFolder Dataset
  612. std::string folder_path = datasets_root_path_ + "/testPK/data/";
  613. std::shared_ptr<Dataset> ds = ImageFolder(folder_path, true, RandomSampler(false, 10));
  614. EXPECT_NE(ds, nullptr);
  615. // Create a Repeat operation on ds
  616. int32_t repeat_num = 2;
  617. ds = ds->Repeat(repeat_num);
  618. EXPECT_NE(ds, nullptr);
  619. // Create objects for the tensor ops
  620. std::shared_ptr<TensorOperation> random_vertical_flip_op = vision::RandomVerticalFlip(0.5);
  621. EXPECT_NE(random_vertical_flip_op, nullptr);
  622. // Create a Map operation on ds
  623. ds = ds->Map({random_vertical_flip_op}, {}, {}, {"image", "label"});
  624. EXPECT_NE(ds, nullptr);
  625. // Create a Project operation on ds
  626. std::vector<std::string> column_project = {"image"};
  627. ds = ds->Project(column_project);
  628. EXPECT_NE(ds, nullptr);
  629. // Create a Batch operation on ds
  630. int32_t batch_size = 1;
  631. ds = ds->Batch(batch_size);
  632. EXPECT_NE(ds, nullptr);
  633. // Create an iterator over the result of the above dataset
  634. // This will trigger the creation of the Execution Tree and launch it.
  635. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  636. EXPECT_NE(iter, nullptr);
  637. // Iterate the dataset and get each row
  638. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  639. iter->GetNextRow(&row);
  640. uint64_t i = 0;
  641. while (row.size() != 0) {
  642. i++;
  643. auto image = row["image"];
  644. MS_LOG(INFO) << "Tensor image shape: " << image->shape();
  645. iter->GetNextRow(&row);
  646. }
  647. EXPECT_EQ(i, 20);
  648. // Manually terminate the pipeline
  649. iter->Stop();
  650. }
  651. TEST_F(MindDataTestPipeline, TestZipSuccess) {
  652. // Create an ImageFolder Dataset
  653. std::string folder_path = datasets_root_path_ + "/testPK/data/";
  654. std::shared_ptr<Dataset> ds = ImageFolder(folder_path, true, RandomSampler(false, 10));
  655. EXPECT_NE(ds, nullptr);
  656. // Create a Project operation on ds
  657. std::vector<std::string> column_project = {"image"};
  658. ds = ds->Project(column_project);
  659. EXPECT_NE(ds, nullptr);
  660. // Create an ImageFolder Dataset
  661. std::shared_ptr<Dataset> ds1 = ImageFolder(folder_path, true, RandomSampler(false, 10));
  662. EXPECT_NE(ds1, nullptr);
  663. // Create a Rename operation on ds (so that the 3 datasets we are going to zip have distinct column names)
  664. ds1 = ds1->Rename({"image", "label"}, {"col1", "col2"});
  665. EXPECT_NE(ds1, nullptr);
  666. folder_path = datasets_root_path_ + "/testCifar10Data/";
  667. std::shared_ptr<Dataset> ds2 = Cifar10(folder_path, 0, RandomSampler(false, 10));
  668. EXPECT_NE(ds2, nullptr);
  669. // Create a Project operation on ds
  670. column_project = {"label"};
  671. ds2 = ds2->Project(column_project);
  672. EXPECT_NE(ds2, nullptr);
  673. // Create a Zip operation on the datasets
  674. ds = ds->Zip({ds, ds1, ds2});
  675. EXPECT_NE(ds, nullptr);
  676. // Create a Batch operation on ds
  677. int32_t batch_size = 1;
  678. ds = ds->Batch(batch_size);
  679. EXPECT_NE(ds, nullptr);
  680. // Create an iterator over the result of the above dataset
  681. // This will trigger the creation of the Execution Tree and launch it.
  682. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  683. EXPECT_NE(iter, nullptr);
  684. // Iterate the dataset and get each row
  685. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  686. iter->GetNextRow(&row);
  687. uint64_t i = 0;
  688. while (row.size() != 0) {
  689. i++;
  690. auto image = row["image"];
  691. MS_LOG(INFO) << "Tensor image shape: " << image->shape();
  692. iter->GetNextRow(&row);
  693. }
  694. EXPECT_EQ(i, 10);
  695. // Manually terminate the pipeline
  696. iter->Stop();
  697. }
  698. TEST_F(MindDataTestPipeline, TestZipFail) {
  699. // We expect this test to fail because we are the both datasets we are zipping have "image" and "label" columns
  700. // and zip doesn't accept datasets with same column names
  701. // Create an ImageFolder Dataset
  702. std::string folder_path = datasets_root_path_ + "/testPK/data/";
  703. std::shared_ptr<Dataset> ds = ImageFolder(folder_path, true, RandomSampler(false, 10));
  704. EXPECT_NE(ds, nullptr);
  705. // Create an ImageFolder Dataset
  706. std::shared_ptr<Dataset> ds1 = ImageFolder(folder_path, true, RandomSampler(false, 10));
  707. EXPECT_NE(ds1, nullptr);
  708. // Create a Zip operation on the datasets
  709. ds = ds->Zip({ds, ds1});
  710. EXPECT_NE(ds, nullptr);
  711. // Create a Batch operation on ds
  712. int32_t batch_size = 1;
  713. ds = ds->Batch(batch_size);
  714. EXPECT_NE(ds, nullptr);
  715. // Create an iterator over the result of the above dataset
  716. // This will trigger the creation of the Execution Tree and launch it.
  717. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  718. EXPECT_EQ(iter, nullptr);
  719. }
  720. TEST_F(MindDataTestPipeline, TestRenameSuccess) {
  721. // Create an ImageFolder Dataset
  722. std::string folder_path = datasets_root_path_ + "/testPK/data/";
  723. std::shared_ptr<Dataset> ds = ImageFolder(folder_path, true, RandomSampler(false, 10));
  724. EXPECT_NE(ds, nullptr);
  725. // Create a Repeat operation on ds
  726. int32_t repeat_num = 2;
  727. ds = ds->Repeat(repeat_num);
  728. EXPECT_NE(ds, nullptr);
  729. // Create a Rename operation on ds
  730. ds = ds->Rename({"image", "label"}, {"col1", "col2"});
  731. EXPECT_NE(ds, nullptr);
  732. // Create a Batch operation on ds
  733. int32_t batch_size = 1;
  734. ds = ds->Batch(batch_size);
  735. EXPECT_NE(ds, nullptr);
  736. // Create an iterator over the result of the above dataset
  737. // This will trigger the creation of the Execution Tree and launch it.
  738. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  739. EXPECT_NE(iter, nullptr);
  740. // Iterate the dataset and get each row
  741. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  742. iter->GetNextRow(&row);
  743. uint64_t i = 0;
  744. EXPECT_NE(row.find("col1"), row.end());
  745. EXPECT_NE(row.find("col2"), row.end());
  746. EXPECT_EQ(row.find("image"), row.end());
  747. EXPECT_EQ(row.find("label"), row.end());
  748. while (row.size() != 0) {
  749. i++;
  750. auto image = row["col1"];
  751. MS_LOG(INFO) << "Tensor image shape: " << image->shape();
  752. iter->GetNextRow(&row);
  753. }
  754. EXPECT_EQ(i, 20);
  755. // Manually terminate the pipeline
  756. iter->Stop();
  757. }
  758. TEST_F(MindDataTestPipeline, TestRenameFail) {
  759. // We expect this test to fail because input and output in Rename are not the same size
  760. // Create an ImageFolder Dataset
  761. std::string folder_path = datasets_root_path_ + "/testPK/data/";
  762. std::shared_ptr<Dataset> ds = ImageFolder(folder_path, true, RandomSampler(false, 10));
  763. EXPECT_NE(ds, nullptr);
  764. // Create a Repeat operation on ds
  765. int32_t repeat_num = 2;
  766. ds = ds->Repeat(repeat_num);
  767. EXPECT_NE(ds, nullptr);
  768. // Create a Rename operation on ds
  769. ds = ds->Rename({"image", "label"}, {"col2"});
  770. EXPECT_EQ(ds, nullptr);
  771. }