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

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