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.

epoch_ctrl_op_test.cc 22 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639
  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 "minddata/dataset/core/client.h"
  17. #include "minddata/dataset/engine/datasetops/source/image_folder_op.h"
  18. #include "common/common.h"
  19. #include "gtest/gtest.h"
  20. #include "utils/log_adapter.h"
  21. #include <memory>
  22. using namespace mindspore::dataset;
  23. using mindspore::MsLogLevel::INFO;
  24. using mindspore::ExceptionType::NoExceptionType;
  25. using mindspore::LogStream;
  26. std::shared_ptr<ImageFolderOp> ImageFolder(int64_t num_works, int64_t rows, int64_t conns, std::string path,
  27. bool shuf = false, std::shared_ptr<SamplerRT> sampler = nullptr,
  28. std::map<std::string, int32_t> map = {}, bool decode = false);
  29. std::shared_ptr<ExecutionTree> Build(std::vector<std::shared_ptr<DatasetOp>> ops);
  30. class MindDataTestEpochCtrlOp : public UT::DatasetOpTesting {
  31. public:
  32. void SetUp() override {
  33. DatasetOpTesting::SetUp();
  34. folder_path = datasets_root_path_ + "/testPK/data";
  35. GlobalInit();
  36. // Start with an empty execution tree
  37. my_tree_ = std::make_shared<ExecutionTree>();
  38. my_tree_ = Build({ImageFolder(2, 2, 32, folder_path, false)});
  39. rc = my_tree_->Prepare();
  40. EXPECT_TRUE(rc.IsOk());
  41. rc = my_tree_->Launch();
  42. EXPECT_TRUE(rc.IsOk());
  43. // Start the loop of reading tensors from our pipeline
  44. DatasetIterator di(my_tree_);
  45. TensorMap tensor_map;
  46. rc = di.GetNextAsMap(&tensor_map);
  47. EXPECT_TRUE(rc.IsOk());
  48. int32_t i = 0;
  49. while (tensor_map.size() != 0) {
  50. tensor_map["label"]->GetItemAt<int32_t>(&label, {});
  51. EXPECT_TRUE(img_class[(i % 44) / 11] == label);
  52. // Dump all the image into string, to be used as a comparison later.
  53. golden_imgs.append((char *) tensor_map["image"]->GetBuffer(), (int64_t) tensor_map["image"]->Size());
  54. rc = di.GetNextAsMap(&tensor_map);
  55. EXPECT_TRUE(rc.IsOk());
  56. i++;
  57. }
  58. }
  59. std::shared_ptr<ExecutionTree> my_tree_;
  60. Status rc;
  61. std::string golden_imgs;
  62. std::string folder_path;
  63. int32_t label = 0;
  64. std::string result;
  65. int32_t img_class[4] = {0, 1, 2, 3};
  66. };
  67. TEST_F(MindDataTestEpochCtrlOp, ImageFolder_AutoInjectEpoch) {
  68. MS_LOG(WARNING) << "Doing ImageFolder_AutoInjectEpoch.";
  69. int32_t num_epoch = 2 + std::rand() % 5;
  70. my_tree_ = Build({ImageFolder(2, 2, 32, folder_path, false)});
  71. rc = my_tree_->Prepare();
  72. EXPECT_TRUE(rc.IsOk());
  73. rc = my_tree_->Launch();
  74. EXPECT_TRUE(rc.IsOk());
  75. MS_LOG(DEBUG) << "num_epoch: " << num_epoch;
  76. std::string golden = golden_imgs;
  77. // Start the loop of reading tensors from our pipeline
  78. DatasetIterator di(my_tree_);
  79. TensorMap tensor_map;
  80. uint64_t i = 0;
  81. for (int epoch = 0; epoch < num_epoch; epoch++) {
  82. rc = di.GetNextAsMap(&tensor_map);
  83. EXPECT_TRUE(rc.IsOk());
  84. while (tensor_map.size() != 0) {
  85. tensor_map["label"]->GetItemAt<int32_t>(&label, {});
  86. MS_LOG(DEBUG) << "row:" << i << "\tlabel:" << label << "\n";
  87. EXPECT_TRUE(img_class[(i % 44) / 11] == label);
  88. // Dump all the image into string, to be used as a comparison later.
  89. result.append((char *) tensor_map["image"]->GetBuffer(), (int64_t) tensor_map["image"]->Size());
  90. rc = di.GetNextAsMap(&tensor_map);
  91. EXPECT_TRUE(rc.IsOk());
  92. i++;
  93. }
  94. EXPECT_TRUE(result == golden);
  95. result.clear();
  96. MS_LOG(DEBUG) << "Current epoch: " << epoch << ". Sample count: " << i;
  97. }
  98. EXPECT_TRUE(i == 44 * num_epoch);
  99. // Try to fetch data beyond the specified number of epochs.
  100. rc = di.GetNextAsMap(&tensor_map);
  101. EXPECT_TRUE(rc.IsOk());
  102. }
  103. TEST_F(MindDataTestEpochCtrlOp, ImageFolder_Epoch) {
  104. MS_LOG(WARNING) << "Doing ImageFolder_Epoch.";
  105. int32_t num_epoch = 2 + std::rand() % 5;
  106. my_tree_ = Build({ImageFolder(2, 2, 32, folder_path, false)});
  107. rc = my_tree_->Prepare(num_epoch);
  108. EXPECT_TRUE(rc.IsOk());
  109. rc = my_tree_->Launch();
  110. EXPECT_TRUE(rc.IsOk());
  111. MS_LOG(DEBUG) << "num_epoch: " << num_epoch;
  112. std::string golden = golden_imgs;
  113. // Start the loop of reading tensors from our pipeline
  114. DatasetIterator di(my_tree_);
  115. TensorMap tensor_map;
  116. uint64_t i = 0;
  117. for (int epoch = 0; epoch < num_epoch; epoch++) {
  118. rc = di.GetNextAsMap(&tensor_map);
  119. EXPECT_TRUE(rc.IsOk());
  120. while (tensor_map.size() != 0) {
  121. tensor_map["label"]->GetItemAt<int32_t>(&label, {});
  122. MS_LOG(DEBUG) << "row:" << i << "\tlabel:" << label << "\n";
  123. EXPECT_TRUE(img_class[(i % 44) / 11] == label);
  124. // Dump all the image into string, to be used as a comparison later.
  125. result.append((char *) tensor_map["image"]->GetBuffer(), (int64_t) tensor_map["image"]->Size());
  126. rc = di.GetNextAsMap(&tensor_map);
  127. EXPECT_TRUE(rc.IsOk());
  128. i++;
  129. }
  130. EXPECT_TRUE(result == golden);
  131. result.clear();
  132. MS_LOG(DEBUG) << "Current epoch: " << epoch << ". Sample count: " << i;
  133. }
  134. EXPECT_TRUE(i == 44 * num_epoch);
  135. // Try to fetch data beyond the specified number of epochs.
  136. rc = di.GetNextAsMap(&tensor_map);
  137. EXPECT_FALSE(rc.IsOk());
  138. }
  139. TEST_F(MindDataTestEpochCtrlOp, ImageFolder_Repeat_Epoch) {
  140. MS_LOG(WARNING) << "Doing ImageFolder_Repeat_Epoch.";
  141. int32_t num_epoch = 2 + std::rand() % 5;
  142. int32_t num_repeats = 2;
  143. std::shared_ptr<RepeatOp> repeat_op;
  144. rc = RepeatOp::Builder(num_repeats).Build(&repeat_op);
  145. EXPECT_TRUE(rc.IsOk());
  146. my_tree_ = Build({ImageFolder(2, 2, 32, folder_path, false), repeat_op});
  147. rc = my_tree_->Prepare(num_epoch);
  148. EXPECT_TRUE(rc.IsOk());
  149. rc = my_tree_->Launch();
  150. EXPECT_TRUE(rc.IsOk());
  151. MS_LOG(DEBUG) << "num_epoch: " << num_epoch << ". num_repeat: " << num_repeats;
  152. std::string golden = golden_imgs;
  153. for (int i = 1; i < num_repeats; i++) {
  154. golden += golden_imgs;
  155. }
  156. // Start the loop of reading tensors from our pipeline
  157. DatasetIterator di(my_tree_);
  158. TensorMap tensor_map;
  159. uint64_t i = 0;
  160. for (int epoch = 0; epoch < num_epoch; epoch++) {
  161. rc = di.GetNextAsMap(&tensor_map);
  162. EXPECT_TRUE(rc.IsOk());
  163. while (tensor_map.size() != 0) {
  164. tensor_map["label"]->GetItemAt<int32_t>(&label, {});
  165. MS_LOG(DEBUG) << "row:" << i << "\tlabel:" << label << "\n";
  166. EXPECT_TRUE(img_class[(i % 44) / 11] == label);
  167. // Dump all the image into string, to be used as a comparison later.
  168. result.append((char *) tensor_map["image"]->GetBuffer(), (int64_t) tensor_map["image"]->Size());
  169. rc = di.GetNextAsMap(&tensor_map);
  170. EXPECT_TRUE(rc.IsOk());
  171. i++;
  172. }
  173. EXPECT_TRUE(result == golden);
  174. result.clear();
  175. MS_LOG(DEBUG) << "Current epoch: " << epoch << ". Sample count: " << i;
  176. }
  177. EXPECT_TRUE(i == 44 * num_repeats * num_epoch);
  178. // Try to fetch data beyond the specified number of epochs.
  179. rc = di.GetNextAsMap(&tensor_map);
  180. EXPECT_FALSE(rc.IsOk());
  181. }
  182. TEST_F(MindDataTestEpochCtrlOp, ImageFolder_Repeat_Repeat_Epoch) {
  183. MS_LOG(WARNING) << "Doing ImageFolder_Repeat_Repeat_Epoch.";
  184. int32_t num_epoch = 2 + std::rand() % 5;
  185. int32_t num_repeats = 2;
  186. std::shared_ptr<RepeatOp> repeat_op;
  187. rc = RepeatOp::Builder(num_repeats).Build(&repeat_op);
  188. EXPECT_TRUE(rc.IsOk());
  189. int32_t num_repeats_2 = 3;
  190. std::shared_ptr<RepeatOp> repeat_op_2;
  191. rc = RepeatOp::Builder(num_repeats_2).Build(&repeat_op_2);
  192. EXPECT_TRUE(rc.IsOk());
  193. my_tree_ = Build({ImageFolder(2, 2, 32, folder_path, false), repeat_op, repeat_op_2});
  194. rc = my_tree_->Prepare(num_epoch);
  195. EXPECT_TRUE(rc.IsOk());
  196. rc = my_tree_->Launch();
  197. EXPECT_TRUE(rc.IsOk());
  198. MS_LOG(DEBUG) << "num_epoch: " << num_epoch << ". num_repeat: " << num_repeats << ". num_repeat_2: " << num_repeats_2;
  199. std::string golden;
  200. for (int j = 0; j < num_repeats_2; j++) {
  201. for (int i = 0; i < num_repeats; i++) {
  202. golden += golden_imgs;
  203. }
  204. }
  205. // Start the loop of reading tensors from our pipeline
  206. DatasetIterator di(my_tree_);
  207. TensorMap tensor_map;
  208. uint64_t i = 0;
  209. for (int epoch = 0; epoch < num_epoch; epoch++) {
  210. rc = di.GetNextAsMap(&tensor_map);
  211. EXPECT_TRUE(rc.IsOk());
  212. while (tensor_map.size() != 0) {
  213. tensor_map["label"]->GetItemAt<int32_t>(&label, {});
  214. MS_LOG(DEBUG) << "row:" << i << "\tlabel:" << label << "\n";
  215. EXPECT_TRUE(img_class[(i % 44) / 11] == label);
  216. // Dump all the image into string, to be used as a comparison later.
  217. result.append((char *) tensor_map["image"]->GetBuffer(), (int64_t) tensor_map["image"]->Size());
  218. rc = di.GetNextAsMap(&tensor_map);
  219. EXPECT_TRUE(rc.IsOk());
  220. i++;
  221. }
  222. EXPECT_EQ(result.size(), golden.size());
  223. EXPECT_TRUE(result == golden);
  224. result.clear();
  225. MS_LOG(DEBUG) << "Current epoch: " << epoch << ". Sample count: " << i;
  226. }
  227. EXPECT_EQ(i, 44 * num_epoch * num_repeats * num_repeats_2);
  228. // Try to fetch data beyond the specified number of epochs.
  229. rc = di.GetNextAsMap(&tensor_map);
  230. EXPECT_FALSE(rc.IsOk());
  231. }
  232. TEST_F(MindDataTestEpochCtrlOp, ImageFolder_Epoch_Inf) {
  233. MS_LOG(WARNING) << "Doing ImageFolder_Epoch_Inf.";
  234. // if num_epoch == -1, it means infinity.
  235. int32_t num_epoch = -1;
  236. my_tree_ = Build({ImageFolder(2, 2, 32, folder_path, false)});
  237. rc = my_tree_->Prepare(num_epoch);
  238. EXPECT_TRUE(rc.IsOk());
  239. rc = my_tree_->Launch();
  240. EXPECT_TRUE(rc.IsOk());
  241. // Start the loop of reading tensors from our pipeline
  242. DatasetIterator di(my_tree_);
  243. TensorMap tensor_map;
  244. uint64_t i = 0;
  245. // For this test, we stop at stop_at_epoch number.
  246. int32_t stop_at_epoch = 2 + std::rand() % 6;
  247. MS_LOG(DEBUG) << "num_epoch: " << num_epoch << ". Stop at epoch: " << stop_at_epoch;
  248. for (int epoch = 0; epoch < stop_at_epoch; epoch++) {
  249. rc = di.GetNextAsMap(&tensor_map);
  250. EXPECT_TRUE(rc.IsOk());
  251. while (tensor_map.size() != 0) {
  252. tensor_map["label"]->GetItemAt<int32_t>(&label, {});
  253. MS_LOG(DEBUG) << "row:" << i << "\tlabel:" << label << "\n";
  254. EXPECT_TRUE(img_class[(i % 44) / 11] == label);
  255. // Dump all the image into string, to be used as a comparison later.
  256. result.append((char *) tensor_map["image"]->GetBuffer(), (int64_t) tensor_map["image"]->Size());
  257. rc = di.GetNextAsMap(&tensor_map);
  258. EXPECT_TRUE(rc.IsOk());
  259. i++;
  260. }
  261. EXPECT_EQ(result, golden_imgs);
  262. result.clear();
  263. MS_LOG(DEBUG) << "Current epoch: " << epoch << ". Sample count: " << i;
  264. }
  265. EXPECT_TRUE(i == 44 * stop_at_epoch);
  266. }
  267. TEST_F(MindDataTestEpochCtrlOp, ImageFolder_Repeat_Repeat_Epoch_Inf) {
  268. MS_LOG(WARNING) << "Doing ImageFolder_Repeat_Epoch_Inf.";
  269. // if num_epoch == -1, it means infinity.
  270. int32_t num_epoch = -1;
  271. int32_t num_repeats = 2;
  272. std::shared_ptr<RepeatOp> repeat_op;
  273. rc = RepeatOp::Builder(num_repeats).Build(&repeat_op);
  274. EXPECT_TRUE(rc.IsOk());
  275. int32_t num_repeats_2 = 3;
  276. std::shared_ptr<RepeatOp> repeat_op_2;
  277. rc = RepeatOp::Builder(num_repeats_2).Build(&repeat_op_2);
  278. EXPECT_TRUE(rc.IsOk());
  279. my_tree_ = Build({ImageFolder(2, 2, 32, folder_path, false), repeat_op, repeat_op_2});
  280. rc = my_tree_->Prepare(num_epoch);
  281. EXPECT_TRUE(rc.IsOk());
  282. rc = my_tree_->Launch();
  283. EXPECT_TRUE(rc.IsOk());
  284. MS_LOG(DEBUG) << "num_epoch: " << num_epoch << ". num_repeat: " << num_repeats << ". num_repeat_2: " << num_repeats_2;
  285. std::string golden;
  286. for (int j = 0; j < num_repeats_2; j++) {
  287. for (int i = 0; i < num_repeats; i++) {
  288. golden += golden_imgs;
  289. }
  290. }
  291. // Start the loop of reading tensors from our pipeline
  292. DatasetIterator di(my_tree_);
  293. TensorMap tensor_map;
  294. uint64_t i = 0;
  295. // For this test, we stop at stop_at_epoch number.
  296. int32_t stop_at_epoch = 2 + std::rand() % 6;
  297. MS_LOG(DEBUG) << "num_epoch: " << num_epoch << ". Stop at epoch: " << stop_at_epoch;
  298. for (int epoch = 0; epoch < stop_at_epoch; epoch++) {
  299. rc = di.GetNextAsMap(&tensor_map);
  300. EXPECT_TRUE(rc.IsOk());
  301. while (tensor_map.size() != 0) {
  302. tensor_map["label"]->GetItemAt<int32_t>(&label, {});
  303. MS_LOG(DEBUG) << "row:" << i << "\tlabel:" << label << "\n";
  304. EXPECT_TRUE(img_class[(i % 44) / 11] == label);
  305. // Dump all the image into string, to be used as a comparison later.
  306. result.append((char *) tensor_map["image"]->GetBuffer(), (int64_t) tensor_map["image"]->Size());
  307. rc = di.GetNextAsMap(&tensor_map);
  308. EXPECT_TRUE(rc.IsOk());
  309. i++;
  310. }
  311. EXPECT_EQ(result, golden);
  312. result.clear();
  313. MS_LOG(DEBUG) << "Current epoch: " << epoch << ". Sample count: " << i;
  314. }
  315. EXPECT_TRUE(i == 44 * stop_at_epoch * num_repeats * num_repeats_2);
  316. }
  317. TEST_F(MindDataTestEpochCtrlOp, ImageFolder_Epoch_ChildItr) {
  318. MS_LOG(WARNING) << "Doing ImageFolder_Epoch_ChildItr.";
  319. int32_t num_epoch = 2 + std::rand() % 5;
  320. my_tree_ = Build({ImageFolder(2, 2, 32, folder_path, false)});
  321. rc = my_tree_->Prepare(num_epoch);
  322. EXPECT_TRUE(rc.IsOk());
  323. rc = my_tree_->Launch();
  324. EXPECT_TRUE(rc.IsOk());
  325. MS_LOG(INFO) << "num_epoch: " << num_epoch;
  326. // Start the loop of reading tensors from our pipeline
  327. ChildIterator ci(my_tree_->root().get(), 0, 0);
  328. TensorRow tensor_row;
  329. uint64_t total_sample = 0;
  330. uint64_t i = 0;
  331. uint32_t epoch = 0;
  332. rc = ci.FetchNextTensorRow(&tensor_row);
  333. EXPECT_TRUE(rc.IsOk());
  334. while(!ci.eof_handled()) {
  335. i = 0;
  336. while (tensor_row.size() != 0) {
  337. tensor_row[1]->GetItemAt<int32_t>(&label, {});
  338. MS_LOG(DEBUG) << "row:" << i << "\tlabel:" << label << "\n";
  339. EXPECT_TRUE(img_class[(i % 44) / 11] == label);
  340. // Dump all the image into string, to be used as a comparison later.
  341. result.append((char *) tensor_row[0]->GetBuffer(), (int64_t) tensor_row[0]->Size());
  342. rc = ci.FetchNextTensorRow(&tensor_row);
  343. EXPECT_TRUE(rc.IsOk());
  344. i++;
  345. }
  346. epoch++;
  347. MS_LOG(DEBUG) << "Current epoch: " << epoch << ". Sample count: " << i;
  348. EXPECT_TRUE(result == golden_imgs);
  349. result.clear();
  350. EXPECT_TRUE(i == 44);
  351. total_sample += i;
  352. rc = ci.FetchNextTensorRow(&tensor_row);
  353. EXPECT_TRUE(rc.IsOk());
  354. }
  355. EXPECT_TRUE(total_sample == 44 * num_epoch);
  356. // Try to fetch data after last epoch ends.
  357. rc = ci.FetchNextTensorRow(&tensor_row);
  358. EXPECT_TRUE(tensor_row.empty());
  359. EXPECT_FALSE(rc.IsOk());
  360. }
  361. TEST_F(MindDataTestEpochCtrlOp, ImageFolder_Repeat_Epoch_ChildItr) {
  362. MS_LOG(WARNING) << "Doing ImageFolder_Repeat_Epoch_ChildItr.";
  363. int32_t num_epoch = 2 + std::rand() % 5;
  364. int32_t num_repeats = 2;
  365. std::shared_ptr<RepeatOp> repeat_op;
  366. rc = RepeatOp::Builder(num_repeats).Build(&repeat_op);
  367. EXPECT_TRUE(rc.IsOk());
  368. my_tree_ = Build({ImageFolder(2, 2, 32, folder_path, false), repeat_op});
  369. rc = my_tree_->Prepare(num_epoch);
  370. EXPECT_TRUE(rc.IsOk());
  371. rc = my_tree_->Launch();
  372. EXPECT_TRUE(rc.IsOk());
  373. MS_LOG(DEBUG) << "num_epoch: " << num_epoch << ". num_repeat: " << num_repeats;
  374. std::string golden;
  375. for (int i = 0; i < num_repeats; i++) {
  376. golden += golden_imgs;
  377. }
  378. // Start the loop of reading tensors from our pipeline
  379. ChildIterator ci(my_tree_->root().get(), 0, 0);
  380. TensorRow tensor_row;
  381. uint64_t total_sample = 0;
  382. uint64_t i = 0;
  383. uint32_t epoch = 0;
  384. rc = ci.FetchNextTensorRow(&tensor_row);
  385. EXPECT_TRUE(rc.IsOk());
  386. while(!ci.eof_handled()) {
  387. i = 0;
  388. while (tensor_row.size() != 0) {
  389. tensor_row[1]->GetItemAt<int32_t>(&label, {});
  390. MS_LOG(DEBUG) << "row:" << i << "\tlabel:" << label << "\n";
  391. EXPECT_TRUE(img_class[(i % 44) / 11] == label);
  392. // Dump all the image into string, to be used as a comparison later.
  393. result.append((char *) tensor_row[0]->GetBuffer(), (int64_t) tensor_row[0]->Size());
  394. rc = ci.FetchNextTensorRow(&tensor_row);
  395. EXPECT_TRUE(rc.IsOk());
  396. i++;
  397. }
  398. epoch++;
  399. MS_LOG(DEBUG) << "Current epoch: " << epoch << ". Sample count: " << i;
  400. EXPECT_TRUE(result == golden);
  401. result.clear();
  402. EXPECT_TRUE(i == 44 * num_repeats);
  403. total_sample += i;
  404. rc = ci.FetchNextTensorRow(&tensor_row);
  405. EXPECT_TRUE(rc.IsOk());
  406. }
  407. EXPECT_TRUE(total_sample == 44 * num_epoch * num_repeats);
  408. // Try to fetch data after last epoch ends.
  409. rc = ci.FetchNextTensorRow(&tensor_row);
  410. EXPECT_TRUE(tensor_row.empty());
  411. EXPECT_FALSE(rc.IsOk());
  412. }
  413. TEST_F(MindDataTestEpochCtrlOp, ImageFolder_Repeat_Repeat_Epoch_ChildItr) {
  414. MS_LOG(WARNING) << "Doing ImageFolder_Repeat_Repeat_Epoch_ChildItr.";
  415. int32_t num_epoch = 2 + std::rand() % 5;
  416. int32_t num_repeats = 2;
  417. std::shared_ptr<RepeatOp> repeat_op;
  418. rc = RepeatOp::Builder(num_repeats).Build(&repeat_op);
  419. EXPECT_TRUE(rc.IsOk());
  420. int32_t num_repeats_2 = 3;
  421. std::shared_ptr<RepeatOp> repeat_op_2;
  422. rc = RepeatOp::Builder(num_repeats_2).Build(&repeat_op_2);
  423. EXPECT_TRUE(rc.IsOk());
  424. my_tree_ = Build({ImageFolder(2, 2, 32, folder_path, false), repeat_op, repeat_op_2});
  425. rc = my_tree_->Prepare(num_epoch);
  426. EXPECT_TRUE(rc.IsOk());
  427. rc = my_tree_->Launch();
  428. EXPECT_TRUE(rc.IsOk());
  429. MS_LOG(DEBUG) << "num_epoch: " << num_epoch << ". num_repeat: " << num_repeats << ". num_repeat_2: " << num_repeats_2;
  430. std::string golden;
  431. for (int j = 0; j < num_repeats_2; j++) {
  432. for (int i = 0; i < num_repeats; i++) {
  433. golden += golden_imgs;
  434. }
  435. }
  436. // Start the loop of reading tensors from our pipeline
  437. ChildIterator ci(my_tree_->root().get(), 0, 0);
  438. TensorRow tensor_row;
  439. uint64_t total_sample = 0;
  440. uint64_t i = 0;
  441. uint32_t epoch = 0;
  442. rc = ci.FetchNextTensorRow(&tensor_row);
  443. EXPECT_TRUE(rc.IsOk());
  444. while(!ci.eof_handled()) {
  445. i = 0;
  446. while (tensor_row.size() != 0) {
  447. tensor_row[1]->GetItemAt<int32_t>(&label, {});
  448. MS_LOG(DEBUG) << "row:" << i << "\tlabel:" << label << "\n";
  449. EXPECT_TRUE(img_class[(i % 44) / 11] == label);
  450. // Dump all the image into string, to be used as a comparison later.
  451. result.append((char *) tensor_row[0]->GetBuffer(), (int64_t) tensor_row[0]->Size());
  452. rc = ci.FetchNextTensorRow(&tensor_row);
  453. EXPECT_TRUE(rc.IsOk());
  454. i++;
  455. }
  456. epoch++;
  457. MS_LOG(DEBUG) << "Current epoch: " << epoch << ". Sample count: " << i;
  458. EXPECT_TRUE(result == golden);
  459. result.clear();
  460. EXPECT_TRUE(i == 44 * num_repeats * num_repeats_2);
  461. total_sample += i;
  462. rc = ci.FetchNextTensorRow(&tensor_row);
  463. EXPECT_TRUE(rc.IsOk());
  464. }
  465. EXPECT_TRUE(total_sample == 44 * num_epoch * num_repeats * num_repeats_2);
  466. // Try to fetch data after last epoch ends.
  467. rc = ci.FetchNextTensorRow(&tensor_row);
  468. EXPECT_TRUE(tensor_row.empty());
  469. EXPECT_FALSE(rc.IsOk());
  470. }
  471. TEST_F(MindDataTestEpochCtrlOp, ImageFolder_Epoch_Inf_ChildItr) {
  472. MS_LOG(WARNING) << "Doing ImageFolder_Epoch_Inf_ChildItr.";
  473. // if num_epoch == -1, it means infinity.
  474. int32_t num_epoch = -1;
  475. my_tree_ = Build({ImageFolder(2, 2, 32, folder_path, false)});
  476. rc = my_tree_->Prepare(num_epoch);
  477. EXPECT_TRUE(rc.IsOk());
  478. rc = my_tree_->Launch();
  479. EXPECT_TRUE(rc.IsOk());
  480. // Start the loop of reading tensors from our pipeline
  481. ChildIterator ci(my_tree_->root().get(), 0, 0);
  482. TensorRow tensor_row;
  483. uint64_t i = 0;
  484. // For this test, we stop at a random number between 0 - 100 epochs.
  485. int32_t stop_at_epoch = 2 + std::rand() % 5;
  486. MS_LOG(DEBUG) << "num_epoch: " << num_epoch << ". Stop at epoch: " << stop_at_epoch;
  487. for (int epoch = 0; epoch < stop_at_epoch; epoch++) {
  488. rc = ci.FetchNextTensorRow(&tensor_row);
  489. EXPECT_TRUE(rc.IsOk());
  490. while (tensor_row.size() != 0) {
  491. tensor_row[1]->GetItemAt<int32_t>(&label, {});
  492. MS_LOG(DEBUG) << "row:" << i << "\tlabel:" << label << "\n";
  493. EXPECT_TRUE(img_class[(i % 44) / 11] == label);
  494. // Dump all the image into string, to be used as a comparison later.
  495. result.append((char *) tensor_row[0]->GetBuffer(), (int64_t) tensor_row[0]->Size());
  496. rc = ci.FetchNextTensorRow(&tensor_row);
  497. EXPECT_TRUE(rc.IsOk());
  498. i++;
  499. }
  500. EXPECT_TRUE(result == golden_imgs);
  501. result.clear();
  502. MS_LOG(DEBUG) << "Current epoch: " << epoch << ". Sample count: " << i;
  503. }
  504. EXPECT_TRUE(i == 44 * stop_at_epoch);
  505. }
  506. TEST_F(MindDataTestEpochCtrlOp, ImageFolder_Repeat_Epoch_Inf_ChildItr) {
  507. MS_LOG(WARNING) << "Doing ImageFolder_Repeat_Epoch_Inf_ChildItr.";
  508. // if num_epoch == -1, it means infinity.
  509. int32_t num_epoch = -1;
  510. int32_t num_repeats = 2;
  511. std::shared_ptr<RepeatOp> repeat_op;
  512. rc = RepeatOp::Builder(num_repeats).Build(&repeat_op);
  513. EXPECT_TRUE(rc.IsOk());
  514. my_tree_ = Build({ImageFolder(2, 2, 32, folder_path, false), repeat_op});
  515. rc = my_tree_->Prepare(num_epoch);
  516. EXPECT_TRUE(rc.IsOk());
  517. rc = my_tree_->Launch();
  518. EXPECT_TRUE(rc.IsOk());
  519. MS_LOG(DEBUG) << "num_epoch: " << num_epoch << ". num_repeat: " << num_repeats;
  520. std::string golden;
  521. for (int i = 0; i < num_repeats; i++) {
  522. golden += golden_imgs;
  523. }
  524. // Start the loop of reading tensors from our pipeline
  525. ChildIterator ci(my_tree_->root().get(), 0, 0);
  526. TensorRow tensor_row;
  527. uint64_t i = 0;
  528. // For this test, we stop at a random number between 0 - 100 epochs.
  529. int32_t stop_at_epoch = 2 + std::rand() % 5;
  530. MS_LOG(DEBUG) << "num_epoch: " << num_epoch << ". Stop at epoch: " << stop_at_epoch;
  531. for (int epoch = 0; epoch < stop_at_epoch; epoch++) {
  532. rc = ci.FetchNextTensorRow(&tensor_row);
  533. EXPECT_TRUE(rc.IsOk());
  534. while (tensor_row.size() != 0) {
  535. tensor_row[1]->GetItemAt<int32_t>(&label, {});
  536. MS_LOG(DEBUG) << "row:" << i << "\tlabel:" << label << "\n";
  537. EXPECT_TRUE(img_class[(i % 44) / 11] == label);
  538. // Dump all the image into string, to be used as a comparison later.
  539. result.append((char *) tensor_row[0]->GetBuffer(), (int64_t) tensor_row[0]->Size());
  540. rc = ci.FetchNextTensorRow(&tensor_row);
  541. EXPECT_TRUE(rc.IsOk());
  542. i++;
  543. }
  544. EXPECT_TRUE(result == golden);
  545. result.clear();
  546. MS_LOG(DEBUG) << "Current epoch: " << epoch << ". Sample count: " << i;
  547. }
  548. EXPECT_TRUE(i == 44 * stop_at_epoch * num_repeats);
  549. }