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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771
  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_TRUE(ds != nullptr);
  47. // Create a Repeat operation on ds
  48. int32_t repeat_num = 2;
  49. ds = ds->Repeat(repeat_num);
  50. EXPECT_TRUE(ds != nullptr);
  51. // Create a Batch operation on ds
  52. int32_t batch_size = 2;
  53. ds = ds->Batch(batch_size);
  54. EXPECT_TRUE(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_TRUE(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_TRUE(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_TRUE(ds != nullptr);
  78. // Create a Repeat operation on ds
  79. int32_t repeat_num = 2;
  80. ds = ds->Repeat(repeat_num);
  81. EXPECT_TRUE(ds != nullptr);
  82. // Create objects for the tensor ops
  83. std::shared_ptr<TensorOperation> resize_op = vision::Resize({30, 30});
  84. EXPECT_TRUE(resize_op != nullptr);
  85. std::shared_ptr<TensorOperation> center_crop_op = vision::CenterCrop({16, 16});
  86. EXPECT_TRUE(center_crop_op != nullptr);
  87. // Create a Map operation on ds
  88. ds = ds->Map({resize_op, center_crop_op});
  89. EXPECT_TRUE(ds != nullptr);
  90. // Create a Batch operation on ds
  91. int32_t batch_size = 1;
  92. ds = ds->Batch(batch_size);
  93. EXPECT_TRUE(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_TRUE(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_TRUE(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_TRUE(ds != nullptr);
  117. // Create a Repeat operation on ds
  118. int32_t repeat_num = 1;
  119. ds = ds->Repeat(repeat_num);
  120. EXPECT_TRUE(ds != nullptr);
  121. // Create objects for the tensor ops
  122. std::shared_ptr<TensorOperation> resize_op = vision::Resize({30, 30});
  123. EXPECT_TRUE(resize_op != nullptr);
  124. std::shared_ptr<TensorOperation> random_crop_op = vision::RandomCrop({28, 28});
  125. EXPECT_TRUE(random_crop_op != nullptr);
  126. std::shared_ptr<TensorOperation> center_crop_op = vision::CenterCrop({16, 16});
  127. EXPECT_TRUE(center_crop_op != nullptr);
  128. std::shared_ptr<TensorOperation> uniform_aug_op = vision::UniformAugment({random_crop_op, center_crop_op}, 2);
  129. EXPECT_TRUE(uniform_aug_op != nullptr);
  130. // Create a Map operation on ds
  131. ds = ds->Map({resize_op, uniform_aug_op});
  132. EXPECT_TRUE(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_TRUE(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_TRUE(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_TRUE(ds != nullptr);
  156. // Create a Repeat operation on ds
  157. int32_t repeat_num = 2;
  158. ds = ds->Repeat(repeat_num);
  159. EXPECT_TRUE(ds != nullptr);
  160. // Create objects for the tensor ops
  161. std::shared_ptr<TensorOperation> random_vertical_flip_op = vision::RandomVerticalFlip(0.5);
  162. EXPECT_TRUE(random_vertical_flip_op != nullptr);
  163. std::shared_ptr<TensorOperation> random_horizontal_flip_op = vision::RandomHorizontalFlip(0.5);
  164. EXPECT_TRUE(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_TRUE(ds != nullptr);
  168. // Create a Batch operation on ds
  169. int32_t batch_size = 1;
  170. ds = ds->Batch(batch_size);
  171. EXPECT_TRUE(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_TRUE(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_TRUE(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_TRUE(ds != nullptr);
  195. // Create a Repeat operation on ds
  196. int32_t repeat_num = 2;
  197. ds = ds->Repeat(repeat_num);
  198. EXPECT_TRUE(ds != nullptr);
  199. // Create a Batch operation on ds
  200. int32_t batch_size = 2;
  201. ds = ds->Batch(batch_size);
  202. EXPECT_TRUE(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_TRUE(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_TRUE(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_TRUE(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_TRUE(ds != nullptr);
  271. // Create a Repeat operation on ds
  272. int32_t repeat_num = 2;
  273. ds = ds->Repeat(repeat_num);
  274. EXPECT_TRUE(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_TRUE(pad_op1 != nullptr);
  278. std::shared_ptr<TensorOperation> pad_op2 = vision::Pad({1}, {1, 1, 1}, BorderType::kEdge);
  279. EXPECT_TRUE(pad_op2 != nullptr);
  280. std::shared_ptr<TensorOperation> pad_op3 = vision::Pad({1, 4});
  281. EXPECT_TRUE(pad_op3 != nullptr);
  282. // Create a Map operation on ds
  283. ds = ds->Map({pad_op1, pad_op2, pad_op3});
  284. EXPECT_TRUE(ds != nullptr);
  285. // Create a Batch operation on ds
  286. int32_t batch_size = 1;
  287. ds = ds->Batch(batch_size);
  288. EXPECT_TRUE(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_TRUE(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_TRUE(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_TRUE(ds != nullptr);
  312. // Create a Repeat operation on ds
  313. int32_t repeat_num = 2;
  314. ds = ds->Repeat(repeat_num);
  315. EXPECT_TRUE(ds != nullptr);
  316. // Create objects for the tensor ops
  317. std::shared_ptr<TensorOperation> cut_out1 = vision::CutOut(30, 5);
  318. EXPECT_TRUE(cut_out1!= nullptr);
  319. std::shared_ptr<TensorOperation> cut_out2 = vision::CutOut(30);
  320. EXPECT_TRUE(cut_out2 != nullptr);
  321. // Create a Map operation on ds
  322. ds = ds->Map({cut_out1, cut_out2});
  323. EXPECT_TRUE(ds != nullptr);
  324. // Create a Batch operation on ds
  325. int32_t batch_size = 1;
  326. ds = ds->Batch(batch_size);
  327. EXPECT_TRUE(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_TRUE(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_TRUE(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_TRUE(ds != nullptr);
  351. // Create a Repeat operation on ds
  352. int32_t repeat_num = 2;
  353. ds = ds->Repeat(repeat_num);
  354. EXPECT_TRUE(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_TRUE(normalize != nullptr);
  358. // Create a Map operation on ds
  359. ds = ds->Map({normalize});
  360. EXPECT_TRUE(ds != nullptr);
  361. // Create a Batch operation on ds
  362. int32_t batch_size = 1;
  363. ds = ds->Batch(batch_size);
  364. EXPECT_TRUE(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_TRUE(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_TRUE(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_TRUE(ds != nullptr);
  388. // Create a Repeat operation on ds
  389. int32_t repeat_num = 2;
  390. ds = ds->Repeat(repeat_num);
  391. EXPECT_TRUE(ds != nullptr);
  392. // Create objects for the tensor ops
  393. std::shared_ptr<TensorOperation> decode = vision::Decode(true);
  394. EXPECT_TRUE(decode != nullptr);
  395. // Create a Map operation on ds
  396. ds = ds->Map({decode});
  397. EXPECT_TRUE(ds != nullptr);
  398. // Create a Batch operation on ds
  399. int32_t batch_size = 1;
  400. ds = ds->Batch(batch_size);
  401. EXPECT_TRUE(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_TRUE(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_TRUE(ds != nullptr);
  425. // Create a Shuffle operation on ds
  426. int32_t shuffle_size = 10;
  427. ds = ds->Shuffle(shuffle_size);
  428. EXPECT_TRUE(ds != nullptr);
  429. // Create a Repeat operation on ds
  430. int32_t repeat_num = 2;
  431. ds = ds->Repeat(repeat_num);
  432. EXPECT_TRUE(ds != nullptr);
  433. // Create a Batch operation on ds
  434. int32_t batch_size = 2;
  435. ds = ds->Batch(batch_size);
  436. EXPECT_TRUE(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_TRUE(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_TRUE(i == 10);
  452. // Manually terminate the pipeline
  453. iter->Stop();
  454. }
  455. TEST_F(MindDataTestPipeline, TestCifar10Dataset) {
  456. // Create a Cifar10 Dataset
  457. std::string folder_path = datasets_root_path_ + "/testCifar10Data/";
  458. std::shared_ptr<Dataset> ds = Cifar10(folder_path, 0, RandomSampler(false, 10));
  459. EXPECT_TRUE(ds != nullptr);
  460. // Create a Repeat operation on ds
  461. int32_t repeat_num = 2;
  462. ds = ds->Repeat(repeat_num);
  463. EXPECT_TRUE(ds != nullptr);
  464. // Create a Batch operation on ds
  465. int32_t batch_size = 2;
  466. ds = ds->Batch(batch_size);
  467. EXPECT_TRUE(ds != nullptr);
  468. // Create an iterator over the result of the above dataset
  469. // This will trigger the creation of the Execution Tree and launch it.
  470. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  471. EXPECT_TRUE(iter != nullptr);
  472. // Iterate the dataset and get each row
  473. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  474. iter->GetNextRow(&row);
  475. uint64_t i = 0;
  476. while (row.size() != 0) {
  477. i++;
  478. auto image = row["image"];
  479. MS_LOG(INFO) << "Tensor image shape: " << image->shape();
  480. iter->GetNextRow(&row);
  481. }
  482. EXPECT_TRUE(i == 10);
  483. // Manually terminate the pipeline
  484. iter->Stop();
  485. }
  486. TEST_F(MindDataTestPipeline, TestRandomColorAdjust) {
  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_TRUE(ds != nullptr);
  491. // Create a Repeat operation on ds
  492. int32_t repeat_num = 2;
  493. ds = ds->Repeat(repeat_num);
  494. EXPECT_TRUE(ds != nullptr);
  495. // Create objects for the tensor ops
  496. std::shared_ptr<TensorOperation> random_color_adjust1 = vision::RandomColorAdjust({1.0}, {0.0}, {0.5}, {0.5});
  497. EXPECT_TRUE(random_color_adjust1 != nullptr);
  498. std::shared_ptr<TensorOperation> random_color_adjust2 = vision::RandomColorAdjust({1.0, 1.0}, {0.0, 0.0}, {0.5, 0.5},
  499. {0.5, 0.5});
  500. EXPECT_TRUE(random_color_adjust2 != nullptr);
  501. std::shared_ptr<TensorOperation> random_color_adjust3 = vision::RandomColorAdjust({0.5, 1.0}, {0.0, 0.5}, {0.25, 0.5},
  502. {0.25, 0.5});
  503. EXPECT_TRUE(random_color_adjust3 != nullptr);
  504. std::shared_ptr<TensorOperation> random_color_adjust4 = vision::RandomColorAdjust();
  505. EXPECT_TRUE(random_color_adjust4 != nullptr);
  506. // Create a Map operation on ds
  507. ds = ds->Map({random_color_adjust1, random_color_adjust2, random_color_adjust3, random_color_adjust4});
  508. EXPECT_TRUE(ds != nullptr);
  509. // Create a Batch operation on ds
  510. int32_t batch_size = 1;
  511. ds = ds->Batch(batch_size);
  512. EXPECT_TRUE(ds != nullptr);
  513. // Create an iterator over the result of the above dataset
  514. // This will trigger the creation of the Execution Tree and launch it.
  515. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  516. EXPECT_TRUE(iter != nullptr);
  517. // Iterate the dataset and get each row
  518. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  519. iter->GetNextRow(&row);
  520. uint64_t i = 0;
  521. while (row.size() != 0) {
  522. i++;
  523. auto image = row["image"];
  524. MS_LOG(INFO) << "Tensor image shape: " << image->shape();
  525. iter->GetNextRow(&row);
  526. }
  527. EXPECT_TRUE(i == 20);
  528. // Manually terminate the pipeline
  529. iter->Stop();
  530. }
  531. TEST_F(MindDataTestPipeline, TestRandomRotation) {
  532. // Create an ImageFolder Dataset
  533. std::string folder_path = datasets_root_path_ + "/testPK/data/";
  534. std::shared_ptr<Dataset> ds = ImageFolder(folder_path, true, RandomSampler(false, 10));
  535. EXPECT_TRUE(ds != nullptr);
  536. // Create a Repeat operation on ds
  537. int32_t repeat_num = 2;
  538. ds = ds->Repeat(repeat_num);
  539. EXPECT_TRUE(ds != nullptr);
  540. // Create objects for the tensor ops
  541. std::shared_ptr<TensorOperation> random_rotation_op = vision::RandomRotation({-180, 180});
  542. EXPECT_TRUE(random_rotation_op != nullptr);
  543. // Create a Map operation on ds
  544. ds = ds->Map({random_rotation_op});
  545. EXPECT_TRUE(ds != nullptr);
  546. // Create a Batch operation on ds
  547. int32_t batch_size = 1;
  548. ds = ds->Batch(batch_size);
  549. EXPECT_TRUE(ds != nullptr);
  550. // Create an iterator over the result of the above dataset
  551. // This will trigger the creation of the Execution Tree and launch it.
  552. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  553. EXPECT_TRUE(iter != nullptr);
  554. // Iterate the dataset and get each row
  555. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  556. iter->GetNextRow(&row);
  557. uint64_t i = 0;
  558. while (row.size() != 0) {
  559. i++;
  560. auto image = row["image"];
  561. MS_LOG(INFO) << "Tensor image shape: " << image->shape();
  562. iter->GetNextRow(&row);
  563. }
  564. EXPECT_TRUE(i == 20);
  565. // Manually terminate the pipeline
  566. iter->Stop();
  567. }
  568. TEST_F(MindDataTestPipeline, TestProjectMap) {
  569. // Create an ImageFolder Dataset
  570. std::string folder_path = datasets_root_path_ + "/testPK/data/";
  571. std::shared_ptr<Dataset> ds = ImageFolder(folder_path, true, RandomSampler(false, 10));
  572. EXPECT_TRUE(ds != nullptr);
  573. // Create a Repeat operation on ds
  574. int32_t repeat_num = 2;
  575. ds = ds->Repeat(repeat_num);
  576. EXPECT_TRUE(ds != nullptr);
  577. // Create objects for the tensor ops
  578. std::shared_ptr<TensorOperation> random_vertical_flip_op = vision::RandomVerticalFlip(0.5);
  579. EXPECT_TRUE(random_vertical_flip_op != nullptr);
  580. // Create a Map operation on ds
  581. ds = ds->Map({random_vertical_flip_op}, {}, {}, {"image", "label"});
  582. EXPECT_TRUE(ds != nullptr);
  583. // Create a Project operation on ds
  584. std::vector<std::string> column_project = {"image"};
  585. ds = ds->Project(column_project);
  586. EXPECT_TRUE(ds != nullptr);
  587. // Create a Batch operation on ds
  588. int32_t batch_size = 1;
  589. ds = ds->Batch(batch_size);
  590. EXPECT_TRUE(ds != nullptr);
  591. // Create an iterator over the result of the above dataset
  592. // This will trigger the creation of the Execution Tree and launch it.
  593. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  594. EXPECT_TRUE(iter != nullptr);
  595. // Iterate the dataset and get each row
  596. std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
  597. iter->GetNextRow(&row);
  598. uint64_t i = 0;
  599. while (row.size() != 0) {
  600. i++;
  601. auto image = row["image"];
  602. MS_LOG(INFO) << "Tensor image shape: " << image->shape();
  603. iter->GetNextRow(&row);
  604. }
  605. EXPECT_TRUE(i == 20);
  606. // Manually terminate the pipeline
  607. iter->Stop();
  608. }