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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  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::LogStream;
  40. using mindspore::ExceptionType::NoExceptionType;
  41. using mindspore::MsLogLevel::ERROR;
  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<SamplerRT> 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. if (rc.IsError()) {
  60. MS_LOG(ERROR) << "Fail to build ImageFolderOp: " << rc.ToString() << "\n";
  61. }
  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(Tensor::CreateFromMemory(shape, DataType(data_type), data, sample_ids));
  68. return Status::OK();
  69. }
  70. class MindDataTestImageFolderSampler : public UT::DatasetOpTesting {
  71. protected:
  72. };
  73. TEST_F(MindDataTestImageFolderSampler, TestSequentialImageFolderWithRepeat) {
  74. std::string folder_path = datasets_root_path_ + "/testPK/data";
  75. auto tree = Build({ImageFolder(16, 2, 32, folder_path, false), Repeat(2)});
  76. tree->Prepare();
  77. int32_t res[] = {0, 1, 2, 3};
  78. Status rc = tree->Launch();
  79. if (rc.IsError()) {
  80. MS_LOG(ERROR) << "Return code error detected during tree launch: " << common::SafeCStr(rc.ToString()) << ".";
  81. EXPECT_TRUE(false);
  82. } else {
  83. DatasetIterator di(tree);
  84. TensorMap tensor_map;
  85. di.GetNextAsMap(&tensor_map);
  86. EXPECT_TRUE(rc.IsOk());
  87. uint64_t i = 0;
  88. int32_t label = 0;
  89. while (tensor_map.size() != 0) {
  90. tensor_map["label"]->GetItemAt<int32_t>(&label, {});
  91. EXPECT_TRUE(res[(i % 44) / 11] == label);
  92. MS_LOG(DEBUG) << "row: " << i << "\t" << tensor_map["image"]->shape() << "label:" << label << "\n";
  93. i++;
  94. di.GetNextAsMap(&tensor_map);
  95. }
  96. EXPECT_TRUE(i == 88);
  97. }
  98. }
  99. TEST_F(MindDataTestImageFolderSampler, TestRandomImageFolder) {
  100. std::string folder_path = datasets_root_path_ + "/testPK/data";
  101. auto tree = Build({ImageFolder(16, 2, 32, folder_path, true, nullptr)});
  102. tree->Prepare();
  103. Status rc = tree->Launch();
  104. if (rc.IsError()) {
  105. MS_LOG(ERROR) << "Return code error detected during tree launch: " << common::SafeCStr(rc.ToString()) << ".";
  106. EXPECT_TRUE(false);
  107. } else {
  108. DatasetIterator di(tree);
  109. TensorMap tensor_map;
  110. di.GetNextAsMap(&tensor_map);
  111. EXPECT_TRUE(rc.IsOk());
  112. uint64_t i = 0;
  113. int32_t label = 0;
  114. while (tensor_map.size() != 0) {
  115. tensor_map["label"]->GetItemAt<int32_t>(&label, {});
  116. MS_LOG(DEBUG) << "row: " << i << "\t" << tensor_map["image"]->shape() << "label:" << label << "\n";
  117. i++;
  118. di.GetNextAsMap(&tensor_map);
  119. }
  120. EXPECT_TRUE(i == 44);
  121. }
  122. }
  123. TEST_F(MindDataTestImageFolderSampler, TestRandomSamplerImageFolder) {
  124. int32_t original_seed = GlobalContext::config_manager()->seed();
  125. GlobalContext::config_manager()->set_seed(0);
  126. int64_t num_samples = 12;
  127. std::shared_ptr<SamplerRT> sampler = std::make_unique<RandomSamplerRT>(num_samples, true, true);
  128. int32_t res[] = {2, 2, 2, 3, 2, 3, 2, 3, 1, 2, 2, 1}; // ground truth label
  129. std::string folder_path = datasets_root_path_ + "/testPK/data";
  130. auto tree = Build({ImageFolder(16, 2, 32, folder_path, false, std::move(sampler))});
  131. tree->Prepare();
  132. Status rc = tree->Launch();
  133. if (rc.IsError()) {
  134. MS_LOG(ERROR) << "Return code error detected during tree launch: " << common::SafeCStr(rc.ToString()) << ".";
  135. EXPECT_TRUE(false);
  136. } else {
  137. DatasetIterator di(tree);
  138. TensorMap tensor_map;
  139. di.GetNextAsMap(&tensor_map);
  140. EXPECT_TRUE(rc.IsOk());
  141. uint64_t i = 0;
  142. int32_t label = 0;
  143. while (tensor_map.size() != 0) {
  144. tensor_map["label"]->GetItemAt<int32_t>(&label, {});
  145. EXPECT_TRUE(res[i] == label);
  146. MS_LOG(DEBUG) << "row: " << i << "\t" << tensor_map["image"]->shape() << "label:" << label << "\n";
  147. i++;
  148. di.GetNextAsMap(&tensor_map);
  149. }
  150. EXPECT_TRUE(i == 12);
  151. }
  152. GlobalContext::config_manager()->set_seed(original_seed);
  153. }
  154. TEST_F(MindDataTestImageFolderSampler, TestSequentialImageFolderWithRepeatBatch) {
  155. std::string folder_path = datasets_root_path_ + "/testPK/data";
  156. auto tree = Build({ImageFolder(16, 2, 32, folder_path, false), Repeat(2), Batch(11)});
  157. tree->Prepare();
  158. int32_t res[4][11] = {{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
  159. {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
  160. {2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2},
  161. {3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3}};
  162. Status rc = tree->Launch();
  163. if (rc.IsError()) {
  164. MS_LOG(ERROR) << "Return code error detected during tree launch: " << common::SafeCStr(rc.ToString()) << ".";
  165. EXPECT_TRUE(false);
  166. } else {
  167. DatasetIterator di(tree);
  168. TensorMap tensor_map;
  169. di.GetNextAsMap(&tensor_map);
  170. EXPECT_TRUE(rc.IsOk());
  171. uint64_t i = 0;
  172. while (tensor_map.size() != 0) {
  173. std::shared_ptr<Tensor> label;
  174. Create1DTensor(&label, 11, reinterpret_cast<unsigned char *>(res[i % 4]), DataType::DE_INT32);
  175. EXPECT_TRUE((*label) == (*tensor_map["label"]));
  176. MS_LOG(DEBUG) << "row: " << i << " " << tensor_map["image"]->shape() << " (*label):" << (*label)
  177. << " *tensor_map[label]: " << *tensor_map["label"] << std::endl;
  178. i++;
  179. di.GetNextAsMap(&tensor_map);
  180. }
  181. EXPECT_TRUE(i == 8);
  182. }
  183. }
  184. TEST_F(MindDataTestImageFolderSampler, TestSubsetRandomSamplerImageFolder) {
  185. // id range 0 - 10 is label 0, and id range 11 - 21 is label 1
  186. std::vector<int64_t> indices({0, 1, 2, 3, 4, 5, 12, 13, 14, 15, 16, 11});
  187. int64_t num_samples = 0;
  188. std::shared_ptr<SamplerRT> sampler = std::make_shared<SubsetRandomSamplerRT>(num_samples, indices);
  189. std::string folder_path = datasets_root_path_ + "/testPK/data";
  190. // Expect 6 samples for label 0 and 1
  191. int res[2] = {6, 6};
  192. auto tree = Build({ImageFolder(16, 2, 32, folder_path, false, std::move(sampler))});
  193. tree->Prepare();
  194. Status rc = tree->Launch();
  195. if (rc.IsError()) {
  196. MS_LOG(ERROR) << "Return code error detected during tree launch: " << common::SafeCStr(rc.ToString()) << ".";
  197. EXPECT_TRUE(false);
  198. } else {
  199. DatasetIterator di(tree);
  200. TensorMap tensor_map;
  201. rc = di.GetNextAsMap(&tensor_map);
  202. EXPECT_TRUE(rc.IsOk());
  203. uint64_t i = 0;
  204. int32_t label = 0;
  205. while (tensor_map.size() != 0) {
  206. tensor_map["label"]->GetItemAt<int32_t>(&label, {});
  207. res[label]--;
  208. i++;
  209. di.GetNextAsMap(&tensor_map);
  210. }
  211. EXPECT_EQ(res[0], 0);
  212. EXPECT_EQ(res[1], 0);
  213. EXPECT_TRUE(i == 12);
  214. }
  215. }
  216. TEST_F(MindDataTestImageFolderSampler, TestWeightedRandomSamplerImageFolder) {
  217. // num samples to draw.
  218. int64_t num_samples = 12;
  219. int64_t total_samples = 44;
  220. int64_t samples_per_buffer = 10;
  221. std::vector<double> weights(total_samples, std::rand() % 100);
  222. // create sampler with replacement = replacement
  223. std::shared_ptr<SamplerRT> sampler =
  224. std::make_shared<WeightedRandomSamplerRT>(num_samples, weights, true, samples_per_buffer);
  225. std::string folder_path = datasets_root_path_ + "/testPK/data";
  226. auto tree = Build({ImageFolder(16, 2, 32, folder_path, false, std::move(sampler))});
  227. tree->Prepare();
  228. Status rc = tree->Launch();
  229. if (rc.IsError()) {
  230. MS_LOG(ERROR) << "Return code error detected during tree launch: " << common::SafeCStr(rc.ToString()) << ".";
  231. EXPECT_TRUE(false);
  232. } else {
  233. DatasetIterator di(tree);
  234. TensorMap tensor_map;
  235. rc = di.GetNextAsMap(&tensor_map);
  236. EXPECT_TRUE(rc.IsOk());
  237. uint64_t i = 0;
  238. int32_t label = 0;
  239. while (tensor_map.size() != 0) {
  240. tensor_map["label"]->GetItemAt<int32_t>(&label, {});
  241. i++;
  242. di.GetNextAsMap(&tensor_map);
  243. }
  244. EXPECT_TRUE(i == 12);
  245. }
  246. }
  247. TEST_F(MindDataTestImageFolderSampler, TestImageFolderClassIndex) {
  248. std::string folder_path = datasets_root_path_ + "/testPK/data";
  249. std::map<std::string, int32_t> map;
  250. map["class3"] = 333;
  251. map["class1"] = 111;
  252. map["wrong folder name"] = 1234; // this is skipped
  253. auto tree = Build({ImageFolder(16, 2, 32, folder_path, false, nullptr, map)});
  254. int64_t res[2] = {111, 333};
  255. tree->Prepare();
  256. Status rc = tree->Launch();
  257. if (rc.IsError()) {
  258. MS_LOG(ERROR) << "Return code error detected during tree launch: " << common::SafeCStr(rc.ToString()) << ".";
  259. EXPECT_TRUE(false);
  260. } else {
  261. DatasetIterator di(tree);
  262. TensorMap tensor_map;
  263. di.GetNextAsMap(&tensor_map);
  264. EXPECT_TRUE(rc.IsOk());
  265. uint64_t i = 0;
  266. int32_t label = 0;
  267. while (tensor_map.size() != 0) {
  268. tensor_map["label"]->GetItemAt<int32_t>(&label, {});
  269. EXPECT_TRUE(label == res[i / 11]);
  270. MS_LOG(DEBUG) << "row: " << i << "\t" << tensor_map["image"]->shape() << "label:" << label << "\n";
  271. i++;
  272. di.GetNextAsMap(&tensor_map);
  273. }
  274. EXPECT_TRUE(i == 22);
  275. }
  276. }
  277. TEST_F(MindDataTestImageFolderSampler, TestDistributedSampler) {
  278. int64_t num_samples = 0;
  279. std::shared_ptr<SamplerRT> sampler = std::make_shared<DistributedSamplerRT>(num_samples, 11, 10, false);
  280. std::string folder_path = datasets_root_path_ + "/testPK/data";
  281. auto tree = Build({ImageFolder(16, 2, 32, folder_path, false, std::move(sampler)), Repeat(4)});
  282. tree->Prepare();
  283. Status rc = tree->Launch();
  284. if (rc.IsError()) {
  285. MS_LOG(ERROR) << "Return code error detected during tree launch: " << common::SafeCStr(rc.ToString()) << ".";
  286. EXPECT_TRUE(false);
  287. } else {
  288. DatasetIterator di(tree);
  289. TensorMap tensor_map;
  290. rc = di.GetNextAsMap(&tensor_map);
  291. EXPECT_TRUE(rc.IsOk());
  292. uint64_t i = 0;
  293. int32_t label = 0;
  294. while (tensor_map.size() != 0) {
  295. tensor_map["label"]->GetItemAt<int32_t>(&label, {});
  296. EXPECT_EQ(i % 4, label);
  297. MS_LOG(DEBUG) << "row:" << i << "\tlabel:" << label << "\n";
  298. i++;
  299. di.GetNextAsMap(&tensor_map);
  300. }
  301. EXPECT_TRUE(i == 16);
  302. }
  303. }
  304. TEST_F(MindDataTestImageFolderSampler, TestPKSamplerImageFolder) {
  305. int64_t num_samples = 0;
  306. std::shared_ptr<SamplerRT> sampler = std::make_shared<PKSamplerRT>(num_samples, 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, TestImageFolderDecode) {
  333. std::string folder_path = datasets_root_path_ + "/testPK/data";
  334. std::map<std::string, int32_t> map;
  335. map["class3"] = 333;
  336. map["class1"] = 111;
  337. map["wrong folder name"] = 1234; // this is skipped
  338. int64_t num_samples = 20;
  339. int64_t start_index = 0;
  340. auto seq_sampler = std::make_shared<SequentialSamplerRT>(num_samples, start_index);
  341. auto tree = Build({ImageFolder(16, 2, 32, folder_path, false, std::move(seq_sampler), map, true)});
  342. int64_t res[2] = {111, 333};
  343. tree->Prepare();
  344. Status rc = tree->Launch();
  345. if (rc.IsError()) {
  346. MS_LOG(ERROR) << "Return code error detected during tree launch: " << common::SafeCStr(rc.ToString()) << ".";
  347. EXPECT_TRUE(false);
  348. } else {
  349. DatasetIterator di(tree);
  350. TensorMap tensor_map;
  351. di.GetNextAsMap(&tensor_map);
  352. EXPECT_TRUE(rc.IsOk());
  353. uint64_t i = 0;
  354. int32_t label = 0;
  355. while (tensor_map.size() != 0) {
  356. tensor_map["label"]->GetItemAt<int32_t>(&label, {});
  357. EXPECT_TRUE(label == res[i / 11]);
  358. EXPECT_TRUE(tensor_map["image"]->shape() ==
  359. TensorShape({2268, 4032, 3})); // verify shapes are correct after decode
  360. MS_LOG(DEBUG) << "row: " << i << "\t" << tensor_map["image"]->shape() << "label:" << label << "\n";
  361. i++;
  362. di.GetNextAsMap(&tensor_map);
  363. }
  364. EXPECT_TRUE(i == 20);
  365. }
  366. }
  367. TEST_F(MindDataTestImageFolderSampler, TestImageFolderSharding1) {
  368. int64_t num_samples = 5;
  369. std::shared_ptr<SamplerRT> sampler = std::make_shared<DistributedSamplerRT>(num_samples, 4, 0, false);
  370. std::string folder_path = datasets_root_path_ + "/testPK/data";
  371. // numWrks, rows, conns, path, shuffle, sampler, map, numSamples, decode
  372. auto tree = Build({ImageFolder(16, 2, 32, folder_path, false, std::move(sampler), {})});
  373. tree->Prepare();
  374. Status rc = tree->Launch();
  375. int32_t labels[5] = {0, 0, 0, 1, 1};
  376. if (rc.IsError()) {
  377. MS_LOG(ERROR) << "Return code error detected during tree launch: " << common::SafeCStr(rc.ToString()) << ".";
  378. EXPECT_TRUE(false);
  379. } else {
  380. DatasetIterator di(tree);
  381. TensorMap tensor_map;
  382. rc = di.GetNextAsMap(&tensor_map);
  383. EXPECT_TRUE(rc.IsOk());
  384. uint64_t i = 0;
  385. int32_t label = 0;
  386. while (tensor_map.size() != 0) {
  387. tensor_map["label"]->GetItemAt<int32_t>(&label, {});
  388. EXPECT_EQ(labels[i], label);
  389. MS_LOG(DEBUG) << "row:" << i << "\tlabel:" << label << "\n";
  390. i++;
  391. di.GetNextAsMap(&tensor_map);
  392. }
  393. EXPECT_TRUE(i == 5);
  394. }
  395. }
  396. TEST_F(MindDataTestImageFolderSampler, TestImageFolderSharding2) {
  397. int64_t num_samples = 12;
  398. std::shared_ptr<SamplerRT> sampler = std::make_shared<DistributedSamplerRT>(num_samples, 4, 3, false);
  399. std::string folder_path = datasets_root_path_ + "/testPK/data";
  400. // numWrks, rows, conns, path, shuffle, sampler, map, numSamples, decode
  401. auto tree = Build({ImageFolder(16, 16, 32, folder_path, false, std::move(sampler), {})});
  402. tree->Prepare();
  403. Status rc = tree->Launch();
  404. uint32_t labels[11] = {0, 0, 1, 1, 1, 2, 2, 2, 3, 3, 3};
  405. if (rc.IsError()) {
  406. MS_LOG(ERROR) << "Return code error detected during tree launch: " << common::SafeCStr(rc.ToString()) << ".";
  407. EXPECT_TRUE(false);
  408. } else {
  409. DatasetIterator di(tree);
  410. TensorMap tensor_map;
  411. rc = di.GetNextAsMap(&tensor_map);
  412. EXPECT_TRUE(rc.IsOk());
  413. uint64_t i = 0;
  414. int32_t label = 0;
  415. while (tensor_map.size() != 0) {
  416. tensor_map["label"]->GetItemAt<int32_t>(&label, {});
  417. EXPECT_EQ(labels[i], label);
  418. MS_LOG(DEBUG) << "row:" << i << "\tlabel:" << label << "\n";
  419. i++;
  420. di.GetNextAsMap(&tensor_map);
  421. }
  422. EXPECT_TRUE(i == 11);
  423. }
  424. }