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

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