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

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