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