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

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