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.

image_folder_op_test.cc 19 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  1. /**
  2. * Copyright 2019 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 <string>
  20. #include "common/common.h"
  21. #include "common/utils.h"
  22. #include "dataset/core/client.h"
  23. #include "dataset/core/global_context.h"
  24. #include "dataset/engine/datasetops/source/image_folder_op.h"
  25. #include "dataset/engine/datasetops/source/sampler/distributed_sampler.h"
  26. #include "dataset/engine/datasetops/source/sampler/pk_sampler.h"
  27. #include "dataset/engine/datasetops/source/sampler/random_sampler.h"
  28. #include "dataset/engine/datasetops/source/sampler/sampler.h"
  29. #include "dataset/engine/datasetops/source/sampler/sequential_sampler.h"
  30. #include "dataset/engine/datasetops/source/sampler/subset_random_sampler.h"
  31. #include "dataset/engine/datasetops/source/sampler/weighted_random_sampler.h"
  32. #include "dataset/util/de_error.h"
  33. #include "dataset/util/path.h"
  34. #include "dataset/util/status.h"
  35. #include "gtest/gtest.h"
  36. #include "utils/log_adapter.h"
  37. #include "securec.h"
  38. namespace common = mindspore::common;
  39. using namespace mindspore::dataset;
  40. using mindspore::MsLogLevel::ERROR;
  41. using mindspore::ExceptionType::NoExceptionType;
  42. using mindspore::LogStream;
  43. std::shared_ptr<BatchOp> Batch(int batch_size = 1, bool drop = false, int rows_per_buf = 2);
  44. std::shared_ptr<RepeatOp> Repeat(int repeat_cnt);
  45. std::shared_ptr<ExecutionTree> Build(std::vector<std::shared_ptr<DatasetOp>> ops);
  46. std::shared_ptr<ImageFolderOp> ImageFolder(int64_t num_works, int64_t rows, int64_t conns, std::string path,
  47. bool shuf = false, std::unique_ptr<Sampler> sampler = nullptr,
  48. std::map<std::string, int32_t> map = {}, int64_t num_samples = 0,
  49. bool decode = false) {
  50. std::shared_ptr<ImageFolderOp> so;
  51. ImageFolderOp::Builder builder;
  52. Status rc = builder.SetNumWorkers(num_works)
  53. .SetImageFolderDir(path)
  54. .SetRowsPerBuffer(rows)
  55. .SetOpConnectorSize(conns)
  56. .SetExtensions({".jpg", ".JPEG"})
  57. .SetSampler(std::move(sampler))
  58. .SetClassIndex(map)
  59. .SetDecode(decode)
  60. .SetNumSamples(num_samples)
  61. .Build(&so);
  62. return so;
  63. }
  64. Status Create1DTensor(std::shared_ptr<Tensor> *sample_ids, int64_t num_elements, unsigned char *data = nullptr,
  65. DataType::Type data_type = DataType::DE_UINT32) {
  66. TensorShape shape(std::vector<int64_t>(1, num_elements));
  67. RETURN_IF_NOT_OK(
  68. Tensor::CreateTensor(sample_ids, TensorImpl::kFlexible, shape, DataType(data_type), data));
  69. if (data == nullptr) {
  70. (*sample_ids)->GetMutableBuffer(); // allocate memory in case user forgets!
  71. }
  72. return Status::OK();
  73. }
  74. class MindDataTestImageFolderSampler : public UT::DatasetOpTesting {
  75. protected:
  76. };
  77. TEST_F(MindDataTestImageFolderSampler, TestSequentialImageFolderWithRepeat) {
  78. std::string folder_path = datasets_root_path_ + "/testPK/data";
  79. auto tree = Build({ImageFolder(16, 2, 32, folder_path, false), Repeat(2)});
  80. tree->Prepare();
  81. int32_t res[] = {0, 1, 2, 3};
  82. Status rc = tree->Launch();
  83. if (rc.IsError()) {
  84. MS_LOG(ERROR) << "Return code error detected during tree launch: " << common::SafeCStr(rc.ToString()) << ".";
  85. EXPECT_TRUE(false);
  86. } else {
  87. DatasetIterator di(tree);
  88. TensorMap tensor_map;
  89. di.GetNextAsMap(&tensor_map);
  90. EXPECT_TRUE(rc.IsOk());
  91. uint64_t i = 0;
  92. int32_t label = 0;
  93. while (tensor_map.size() != 0) {
  94. tensor_map["label"]->GetItemAt<int32_t>(&label, {});
  95. EXPECT_TRUE(res[(i % 44) / 11] == label);
  96. MS_LOG(DEBUG) << "row: " << i << "\t" << tensor_map["image"]->shape() << "label:" << label << "\n";
  97. i++;
  98. di.GetNextAsMap(&tensor_map);
  99. }
  100. EXPECT_TRUE(i == 88);
  101. }
  102. }
  103. TEST_F(MindDataTestImageFolderSampler, TestRandomImageFolder) {
  104. std::string folder_path = datasets_root_path_ + "/testPK/data";
  105. auto tree = Build({ImageFolder(16, 2, 32, folder_path, true, nullptr)});
  106. tree->Prepare();
  107. Status rc = tree->Launch();
  108. if (rc.IsError()) {
  109. MS_LOG(ERROR) << "Return code error detected during tree launch: " << common::SafeCStr(rc.ToString()) << ".";
  110. EXPECT_TRUE(false);
  111. } else {
  112. DatasetIterator di(tree);
  113. TensorMap tensor_map;
  114. di.GetNextAsMap(&tensor_map);
  115. EXPECT_TRUE(rc.IsOk());
  116. uint64_t i = 0;
  117. int32_t label = 0;
  118. while (tensor_map.size() != 0) {
  119. tensor_map["label"]->GetItemAt<int32_t>(&label, {});
  120. MS_LOG(DEBUG) << "row: " << i << "\t" << tensor_map["image"]->shape() << "label:" << label << "\n";
  121. i++;
  122. di.GetNextAsMap(&tensor_map);
  123. }
  124. EXPECT_TRUE(i == 44);
  125. }
  126. }
  127. TEST_F(MindDataTestImageFolderSampler, TestRandomSamplerImageFolder) {
  128. int32_t original_seed = GlobalContext::config_manager()->seed();
  129. GlobalContext::config_manager()->set_seed(0);
  130. std::unique_ptr<Sampler> sampler = std::make_unique<RandomSampler>(true, true, 12);
  131. int32_t res[] = {2, 2, 2, 3, 2, 3, 2, 3, 1, 2, 2, 1}; // ground truth label
  132. std::string folder_path = datasets_root_path_ + "/testPK/data";
  133. auto tree = Build({ImageFolder(16, 2, 32, folder_path, false, std::move(sampler))});
  134. tree->Prepare();
  135. Status rc = tree->Launch();
  136. if (rc.IsError()) {
  137. MS_LOG(ERROR) << "Return code error detected during tree launch: " << common::SafeCStr(rc.ToString()) << ".";
  138. EXPECT_TRUE(false);
  139. } else {
  140. DatasetIterator di(tree);
  141. TensorMap tensor_map;
  142. di.GetNextAsMap(&tensor_map);
  143. EXPECT_TRUE(rc.IsOk());
  144. uint64_t i = 0;
  145. int32_t label = 0;
  146. while (tensor_map.size() != 0) {
  147. tensor_map["label"]->GetItemAt<int32_t>(&label, {});
  148. EXPECT_TRUE(res[i] == label);
  149. MS_LOG(DEBUG) << "row: " << i << "\t" << tensor_map["image"]->shape() << "label:" << label << "\n";
  150. i++;
  151. di.GetNextAsMap(&tensor_map);
  152. }
  153. EXPECT_TRUE(i == 12);
  154. }
  155. GlobalContext::config_manager()->set_seed(original_seed);
  156. }
  157. TEST_F(MindDataTestImageFolderSampler, TestSequentialImageFolderWithRepeatBatch) {
  158. std::string folder_path = datasets_root_path_ + "/testPK/data";
  159. auto tree = Build({ImageFolder(16, 2, 32, folder_path, false), Repeat(2), Batch(11)});
  160. tree->Prepare();
  161. int32_t res[4][11] = {{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
  162. {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
  163. {2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2},
  164. {3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3}};
  165. Status rc = tree->Launch();
  166. if (rc.IsError()) {
  167. MS_LOG(ERROR) << "Return code error detected during tree launch: " << common::SafeCStr(rc.ToString()) << ".";
  168. EXPECT_TRUE(false);
  169. } else {
  170. DatasetIterator di(tree);
  171. TensorMap tensor_map;
  172. di.GetNextAsMap(&tensor_map);
  173. EXPECT_TRUE(rc.IsOk());
  174. uint64_t i = 0;
  175. while (tensor_map.size() != 0) {
  176. std::shared_ptr<Tensor> label;
  177. Create1DTensor(&label, 11, reinterpret_cast<unsigned char *>(res[i % 4]), DataType::DE_INT32);
  178. EXPECT_TRUE((*label) == (*tensor_map["label"]));
  179. MS_LOG(DEBUG) << "row: " << i << " " << tensor_map["image"]->shape() << " (*label):" << (*label)
  180. << " *tensor_map[label]: " << *tensor_map["label"] << std::endl;
  181. i++;
  182. di.GetNextAsMap(&tensor_map);
  183. }
  184. EXPECT_TRUE(i == 8);
  185. }
  186. }
  187. TEST_F(MindDataTestImageFolderSampler, TestSubsetRandomSamplerImageFolder) {
  188. // id range 0 - 10 is label 0, and id range 11 - 21 is label 1
  189. std::vector<int64_t> indices({0, 1, 2, 3, 4, 5, 12, 13, 14, 15, 16, 11});
  190. std::unique_ptr<Sampler> sampler = std::make_unique<SubsetRandomSampler>(indices);
  191. std::string folder_path = datasets_root_path_ + "/testPK/data";
  192. // Expect 6 samples for label 0 and 1
  193. int res[2] = {6, 6};
  194. auto tree = Build({ImageFolder(16, 2, 32, folder_path, false, std::move(sampler))});
  195. tree->Prepare();
  196. Status rc = tree->Launch();
  197. if (rc.IsError()) {
  198. MS_LOG(ERROR) << "Return code error detected during tree launch: " << common::SafeCStr(rc.ToString()) << ".";
  199. EXPECT_TRUE(false);
  200. } else {
  201. DatasetIterator di(tree);
  202. TensorMap tensor_map;
  203. rc = di.GetNextAsMap(&tensor_map);
  204. EXPECT_TRUE(rc.IsOk());
  205. uint64_t i = 0;
  206. int32_t label = 0;
  207. while (tensor_map.size() != 0) {
  208. tensor_map["label"]->GetItemAt<int32_t>(&label, {});
  209. res[label]--;
  210. i++;
  211. di.GetNextAsMap(&tensor_map);
  212. }
  213. EXPECT_EQ(res[0], 0);
  214. EXPECT_EQ(res[1], 0);
  215. EXPECT_TRUE(i == 12);
  216. }
  217. }
  218. TEST_F(MindDataTestImageFolderSampler, TestWeightedRandomSamplerImageFolder) {
  219. // num samples to draw.
  220. int64_t num_samples = 12;
  221. int64_t total_samples = 44;
  222. int64_t samples_per_buffer = 10;
  223. std::vector<double> weights(total_samples, std::rand() % 100);
  224. // create sampler with replacement = replacement
  225. std::unique_ptr<Sampler> sampler =
  226. std::make_unique<WeightedRandomSampler>(weights, num_samples, true, samples_per_buffer);
  227. std::string folder_path = datasets_root_path_ + "/testPK/data";
  228. auto tree = Build({ImageFolder(16, 2, 32, folder_path, false, std::move(sampler))});
  229. tree->Prepare();
  230. Status rc = tree->Launch();
  231. if (rc.IsError()) {
  232. MS_LOG(ERROR) << "Return code error detected during tree launch: " << common::SafeCStr(rc.ToString()) << ".";
  233. EXPECT_TRUE(false);
  234. } else {
  235. DatasetIterator di(tree);
  236. TensorMap tensor_map;
  237. rc = di.GetNextAsMap(&tensor_map);
  238. EXPECT_TRUE(rc.IsOk());
  239. uint64_t i = 0;
  240. int32_t label = 0;
  241. while (tensor_map.size() != 0) {
  242. tensor_map["label"]->GetItemAt<int32_t>(&label, {});
  243. i++;
  244. di.GetNextAsMap(&tensor_map);
  245. }
  246. EXPECT_TRUE(i == 12);
  247. }
  248. }
  249. TEST_F(MindDataTestImageFolderSampler, TestImageFolderClassIndex) {
  250. std::string folder_path = datasets_root_path_ + "/testPK/data";
  251. std::map<std::string, int32_t> map;
  252. map["class3"] = 333;
  253. map["class1"] = 111;
  254. map["wrong folder name"] = 1234; // this is skipped
  255. auto tree = Build({ImageFolder(16, 2, 32, folder_path, false, nullptr, map)});
  256. int64_t res[2] = {111, 333};
  257. tree->Prepare();
  258. Status rc = tree->Launch();
  259. if (rc.IsError()) {
  260. MS_LOG(ERROR) << "Return code error detected during tree launch: " << common::SafeCStr(rc.ToString()) << ".";
  261. EXPECT_TRUE(false);
  262. } else {
  263. DatasetIterator di(tree);
  264. TensorMap tensor_map;
  265. di.GetNextAsMap(&tensor_map);
  266. EXPECT_TRUE(rc.IsOk());
  267. uint64_t i = 0;
  268. int32_t label = 0;
  269. while (tensor_map.size() != 0) {
  270. tensor_map["label"]->GetItemAt<int32_t>(&label, {});
  271. EXPECT_TRUE(label == res[i / 11]);
  272. MS_LOG(DEBUG) << "row: " << i << "\t" << tensor_map["image"]->shape() << "label:" << label << "\n";
  273. i++;
  274. di.GetNextAsMap(&tensor_map);
  275. }
  276. EXPECT_TRUE(i == 22);
  277. }
  278. }
  279. TEST_F(MindDataTestImageFolderSampler, TestDistributedSampler) {
  280. std::unique_ptr<Sampler> sampler = std::make_unique<DistributedSampler>(11, 10, false);
  281. std::string folder_path = datasets_root_path_ + "/testPK/data";
  282. auto tree = Build({ImageFolder(16, 2, 32, folder_path, false, std::move(sampler)), Repeat(4)});
  283. tree->Prepare();
  284. Status rc = tree->Launch();
  285. if (rc.IsError()) {
  286. MS_LOG(ERROR) << "Return code error detected during tree launch: " << common::SafeCStr(rc.ToString()) << ".";
  287. EXPECT_TRUE(false);
  288. } else {
  289. DatasetIterator di(tree);
  290. TensorMap tensor_map;
  291. rc = di.GetNextAsMap(&tensor_map);
  292. EXPECT_TRUE(rc.IsOk());
  293. uint64_t i = 0;
  294. int32_t label = 0;
  295. while (tensor_map.size() != 0) {
  296. tensor_map["label"]->GetItemAt<int32_t>(&label, {});
  297. EXPECT_EQ(i % 4, label);
  298. MS_LOG(DEBUG) << "row:" << i << "\tlabel:" << label << "\n";
  299. i++;
  300. di.GetNextAsMap(&tensor_map);
  301. }
  302. EXPECT_TRUE(i == 16);
  303. }
  304. }
  305. TEST_F(MindDataTestImageFolderSampler, TestPKSamplerImageFolder) {
  306. std::unique_ptr<Sampler> sampler = std::make_unique<PKSampler>(3, false, 4);
  307. int32_t res[] = {0, 0, 0, 1, 1, 1, 2, 2, 2, 3, 3, 3}; // ground truth label
  308. std::string folder_path = datasets_root_path_ + "/testPK/data";
  309. auto tree = Build({ImageFolder(16, 2, 32, folder_path, false, std::move(sampler))});
  310. tree->Prepare();
  311. Status rc = tree->Launch();
  312. if (rc.IsError()) {
  313. MS_LOG(ERROR) << "Return code error detected during tree launch: " << common::SafeCStr(rc.ToString()) << ".";
  314. EXPECT_TRUE(false);
  315. } else {
  316. DatasetIterator di(tree);
  317. TensorMap tensor_map;
  318. di.GetNextAsMap(&tensor_map);
  319. EXPECT_TRUE(rc.IsOk());
  320. uint64_t i = 0;
  321. int32_t label = 0;
  322. while (tensor_map.size() != 0) {
  323. tensor_map["label"]->GetItemAt<int32_t>(&label, {});
  324. EXPECT_TRUE(res[i] == label);
  325. MS_LOG(DEBUG) << "row: " << i << "\t" << tensor_map["image"]->shape() << "label:" << label << "\n";
  326. i++;
  327. di.GetNextAsMap(&tensor_map);
  328. }
  329. EXPECT_TRUE(i == 12);
  330. }
  331. }
  332. TEST_F(MindDataTestImageFolderSampler, TestImageFolderNumSamples) {
  333. std::string folder_path = datasets_root_path_ + "/testPK/data";
  334. auto tree = Build({ImageFolder(16, 2, 32, folder_path, false, nullptr, {}, 11), Repeat(2)});
  335. tree->Prepare();
  336. Status rc = tree->Launch();
  337. if (rc.IsError()) {
  338. MS_LOG(ERROR) << "Return code error detected during tree launch: " << common::SafeCStr(rc.ToString()) << ".";
  339. EXPECT_TRUE(false);
  340. } else {
  341. DatasetIterator di(tree);
  342. TensorMap tensor_map;
  343. di.GetNextAsMap(&tensor_map);
  344. EXPECT_TRUE(rc.IsOk());
  345. uint64_t i = 0;
  346. int32_t label = 0;
  347. while (tensor_map.size() != 0) {
  348. tensor_map["label"]->GetItemAt<int32_t>(&label, {});
  349. EXPECT_TRUE(0 == label);
  350. MS_LOG(DEBUG) << "row: " << i << "\t" << tensor_map["image"]->shape() << "label:" << label << "\n";
  351. i++;
  352. di.GetNextAsMap(&tensor_map);
  353. }
  354. EXPECT_TRUE(i == 22);
  355. }
  356. }
  357. TEST_F(MindDataTestImageFolderSampler, TestImageFolderDecode) {
  358. std::string folder_path = datasets_root_path_ + "/testPK/data";
  359. std::map<std::string, int32_t> map;
  360. map["class3"] = 333;
  361. map["class1"] = 111;
  362. map["wrong folder name"] = 1234; // this is skipped
  363. auto tree = Build({ImageFolder(16, 2, 32, folder_path, false, nullptr, map, 20, true)});
  364. int64_t res[2] = {111, 333};
  365. tree->Prepare();
  366. Status rc = tree->Launch();
  367. if (rc.IsError()) {
  368. MS_LOG(ERROR) << "Return code error detected during tree launch: " << common::SafeCStr(rc.ToString()) << ".";
  369. EXPECT_TRUE(false);
  370. } else {
  371. DatasetIterator di(tree);
  372. TensorMap tensor_map;
  373. di.GetNextAsMap(&tensor_map);
  374. EXPECT_TRUE(rc.IsOk());
  375. uint64_t i = 0;
  376. int32_t label = 0;
  377. while (tensor_map.size() != 0) {
  378. tensor_map["label"]->GetItemAt<int32_t>(&label, {});
  379. EXPECT_TRUE(label == res[i / 11]);
  380. EXPECT_TRUE(
  381. tensor_map["image"]->shape() == TensorShape({2268, 4032, 3})); // verify shapes are correct after decode
  382. MS_LOG(DEBUG) << "row: " << i << "\t" << tensor_map["image"]->shape() << "label:" << label << "\n";
  383. i++;
  384. di.GetNextAsMap(&tensor_map);
  385. }
  386. EXPECT_TRUE(i == 20);
  387. }
  388. }
  389. TEST_F(MindDataTestImageFolderSampler, TestImageFolderDatasetSize) {
  390. std::string folder_path = datasets_root_path_ + "/testPK/data";
  391. int64_t num_rows = 0;
  392. int64_t num_classes = 0;
  393. ImageFolderOp::CountRowsAndClasses(folder_path, 15, {}, &num_rows, &num_classes);
  394. EXPECT_TRUE(num_rows == 15 && num_classes == 4);
  395. ImageFolderOp::CountRowsAndClasses(folder_path, 44, {}, &num_rows, &num_classes);
  396. EXPECT_TRUE(num_rows == 44 && num_classes == 4);
  397. ImageFolderOp::CountRowsAndClasses(folder_path, 0, {}, &num_rows, &num_classes);
  398. EXPECT_TRUE(num_rows == 44 && num_classes == 4);
  399. ImageFolderOp::CountRowsAndClasses(folder_path, 55, {}, &num_rows, &num_classes);
  400. EXPECT_TRUE(num_rows == 44 && num_classes == 4);
  401. ImageFolderOp::CountRowsAndClasses(folder_path, 44, {}, &num_rows, &num_classes, 2, 3);
  402. EXPECT_TRUE(num_rows == 15 && num_classes == 4);
  403. ImageFolderOp::CountRowsAndClasses(folder_path, 33, {}, &num_rows, &num_classes, 0, 3);
  404. EXPECT_TRUE(num_rows == 15 && num_classes == 4);
  405. ImageFolderOp::CountRowsAndClasses(folder_path, 13, {}, &num_rows, &num_classes, 0, 11);
  406. EXPECT_TRUE(num_rows == 4 && num_classes == 4);
  407. ImageFolderOp::CountRowsAndClasses(folder_path, 3, {}, &num_rows, &num_classes, 0, 11);
  408. EXPECT_TRUE(num_rows == 3 && num_classes == 4);
  409. }
  410. TEST_F(MindDataTestImageFolderSampler, TestImageFolderSharding1) {
  411. std::unique_ptr<Sampler> sampler = std::make_unique<DistributedSampler>(4, 0, false);
  412. std::string folder_path = datasets_root_path_ + "/testPK/data";
  413. // numWrks, rows, conns, path, shuffle, sampler, map, numSamples, decode
  414. auto tree = Build({ImageFolder(16, 2, 32, folder_path, false, std::move(sampler), {}, 5)});
  415. tree->Prepare();
  416. Status rc = tree->Launch();
  417. int32_t labels[5] = {0, 0, 0, 1, 1};
  418. if (rc.IsError()) {
  419. MS_LOG(ERROR) << "Return code error detected during tree launch: " << common::SafeCStr(rc.ToString()) << ".";
  420. EXPECT_TRUE(false);
  421. } else {
  422. DatasetIterator di(tree);
  423. TensorMap tensor_map;
  424. rc = di.GetNextAsMap(&tensor_map);
  425. EXPECT_TRUE(rc.IsOk());
  426. uint64_t i = 0;
  427. int32_t label = 0;
  428. while (tensor_map.size() != 0) {
  429. tensor_map["label"]->GetItemAt<int32_t>(&label, {});
  430. EXPECT_EQ(labels[i], label);
  431. MS_LOG(DEBUG) << "row:" << i << "\tlabel:" << label << "\n";
  432. i++;
  433. di.GetNextAsMap(&tensor_map);
  434. }
  435. EXPECT_TRUE(i == 5);
  436. }
  437. }
  438. TEST_F(MindDataTestImageFolderSampler, TestImageFolderSharding2) {
  439. std::unique_ptr<Sampler> sampler = std::make_unique<DistributedSampler>(4, 3, false);
  440. std::string folder_path = datasets_root_path_ + "/testPK/data";
  441. // numWrks, rows, conns, path, shuffle, sampler, map, numSamples, decode
  442. auto tree = Build({ImageFolder(16, 16, 32, folder_path, false, std::move(sampler), {}, 12)});
  443. tree->Prepare();
  444. Status rc = tree->Launch();
  445. uint32_t labels[11] = {0, 0, 1, 1, 1, 2, 2, 2, 3, 3, 3};
  446. if (rc.IsError()) {
  447. MS_LOG(ERROR) << "Return code error detected during tree launch: " << common::SafeCStr(rc.ToString()) << ".";
  448. EXPECT_TRUE(false);
  449. } else {
  450. DatasetIterator di(tree);
  451. TensorMap tensor_map;
  452. rc = di.GetNextAsMap(&tensor_map);
  453. EXPECT_TRUE(rc.IsOk());
  454. uint64_t i = 0;
  455. int32_t label = 0;
  456. while (tensor_map.size() != 0) {
  457. tensor_map["label"]->GetItemAt<int32_t>(&label, {});
  458. EXPECT_EQ(labels[i], label);
  459. MS_LOG(DEBUG) << "row:" << i << "\tlabel:" << label << "\n";
  460. i++;
  461. di.GetNextAsMap(&tensor_map);
  462. }
  463. EXPECT_TRUE(i == 11);
  464. }
  465. }