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.

celeba_op_test.cc 6.4 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /**
  2. * Copyright 2020 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 "minddata/dataset/core/client.h"
  22. #include "minddata/dataset/core/global_context.h"
  23. #include "minddata/dataset/engine/datasetops/source/celeba_op.h"
  24. #include "minddata/dataset/engine/datasetops/source/sampler/subset_random_sampler.h"
  25. #include "minddata/dataset/util/status.h"
  26. #include "gtest/gtest.h"
  27. #include "utils/log_adapter.h"
  28. #include "securec.h"
  29. using namespace mindspore::dataset;
  30. using mindspore::MsLogLevel::ERROR;
  31. using mindspore::ExceptionType::NoExceptionType;
  32. using mindspore::LogStream;
  33. std::shared_ptr<RepeatOp> Repeat(int repeat_cnt);
  34. std::shared_ptr<ExecutionTree> Build(std::vector<std::shared_ptr<DatasetOp>> ops);
  35. std::shared_ptr<CelebAOp> Celeba(int32_t num_workers, int32_t rows_per_buffer, int32_t queue_size,
  36. const std::string &dir, std::shared_ptr<Sampler> sampler = nullptr,
  37. bool decode = false, const std::string &dataset_type="all") {
  38. std::shared_ptr<CelebAOp> so;
  39. CelebAOp::Builder builder;
  40. Status rc = builder.SetNumWorkers(num_workers)
  41. .SetCelebADir(dir)
  42. .SetRowsPerBuffer(rows_per_buffer)
  43. .SetOpConnectorSize(queue_size)
  44. .SetSampler(std::move(sampler))
  45. .SetDecode(decode)
  46. .SetUsage(dataset_type).Build(&so);
  47. return so;
  48. }
  49. class MindDataTestCelebaDataset : public UT::DatasetOpTesting {
  50. protected:
  51. };
  52. TEST_F(MindDataTestCelebaDataset, TestSequentialCeleba) {
  53. std::string dir = datasets_root_path_ + "/testCelebAData/";
  54. uint32_t expect_labels[4][40] = {{0,1,1,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,1,1,0,1,0,0,1,0,0,1,0,0,0,1,1,0,1,0,1,0,0,1},
  55. {0,0,0,1,0,0,0,1,0,0,0,1,0,0,0,0,0,0,0,1,0,1,0,0,1,0,0,0,0,0,0,1,0,0,0,0,0,0,0,1},
  56. {0,0,0,1,0,0,0,1,0,0,0,1,0,0,0,0,0,0,0,1,0,1,0,0,1,0,0,0,0,0,0,1,0,0,0,0,0,0,0,1},
  57. {0,1,1,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,1,1,0,1,0,0,1,0,0,1,0,0,0,1,1,0,1,0,1,0,0,1}};
  58. uint32_t count = 0;
  59. auto tree = Build({Celeba(16, 2, 32, dir)});
  60. tree->Prepare();
  61. Status rc = tree->Launch();
  62. if (rc.IsError()) {
  63. MS_LOG(ERROR) << "Return code error detected during tree launch: " << rc.ToString() << ".";
  64. EXPECT_TRUE(false);
  65. } else {
  66. DatasetIterator di(tree);
  67. TensorMap tersor_map;
  68. di.GetNextAsMap(&tersor_map);
  69. EXPECT_TRUE(rc.IsOk());
  70. while (tersor_map.size() != 0) {
  71. uint32_t label;
  72. for (int index = 0; index < 40; index++) {
  73. tersor_map["attr"]->GetItemAt<uint32_t>(&label, {index});
  74. EXPECT_TRUE(expect_labels[count][index] == label);
  75. }
  76. count++;
  77. di.GetNextAsMap(&tersor_map);
  78. }
  79. EXPECT_TRUE(count == 4);
  80. }
  81. }
  82. TEST_F(MindDataTestCelebaDataset, TestCelebaRepeat) {
  83. std::string dir = datasets_root_path_ + "/testCelebAData/";
  84. uint32_t expect_labels[8][40] = {{0,1,1,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,1,1,0,1,0,0,1,0,0,1,0,0,0,1,1,0,1,0,1,0,0,1},
  85. {0,0,0,1,0,0,0,1,0,0,0,1,0,0,0,0,0,0,0,1,0,1,0,0,1,0,0,0,0,0,0,1,0,0,0,0,0,0,0,1},
  86. {0,0,0,1,0,0,0,1,0,0,0,1,0,0,0,0,0,0,0,1,0,1,0,0,1,0,0,0,0,0,0,1,0,0,0,0,0,0,0,1},
  87. {0,1,1,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,1,1,0,1,0,0,1,0,0,1,0,0,0,1,1,0,1,0,1,0,0,1},
  88. {0,1,1,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,1,1,0,1,0,0,1,0,0,1,0,0,0,1,1,0,1,0,1,0,0,1},
  89. {0,0,0,1,0,0,0,1,0,0,0,1,0,0,0,0,0,0,0,1,0,1,0,0,1,0,0,0,0,0,0,1,0,0,0,0,0,0,0,1},
  90. {0,0,0,1,0,0,0,1,0,0,0,1,0,0,0,0,0,0,0,1,0,1,0,0,1,0,0,0,0,0,0,1,0,0,0,0,0,0,0,1},
  91. {0,1,1,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,1,1,0,1,0,0,1,0,0,1,0,0,0,1,1,0,1,0,1,0,0,1}};
  92. uint32_t count = 0;
  93. auto tree = Build({Celeba(16, 2, 32, dir), Repeat(2)});
  94. tree->Prepare();
  95. Status rc = tree->Launch();
  96. if (rc.IsError()) {
  97. MS_LOG(ERROR) << "Return code error detected during tree launch: " << rc.ToString() << ".";
  98. EXPECT_TRUE(false);
  99. } else {
  100. DatasetIterator di(tree);
  101. TensorMap tersor_map;
  102. di.GetNextAsMap(&tersor_map);
  103. EXPECT_TRUE(rc.IsOk());
  104. while (tersor_map.size() != 0) {
  105. uint32_t label;
  106. for (int index = 0; index < 40; index++) {
  107. tersor_map["attr"]->GetItemAt<uint32_t>(&label, {index});
  108. EXPECT_TRUE(expect_labels[count][index] == label);
  109. }
  110. count++;
  111. di.GetNextAsMap(&tersor_map);
  112. }
  113. EXPECT_TRUE(count == 8);
  114. }
  115. }
  116. TEST_F(MindDataTestCelebaDataset, TestSubsetRandomSamplerCeleba) {
  117. std::vector<int64_t> indices({1});
  118. int64_t num_samples = 0;
  119. std::shared_ptr<Sampler> sampler = std::make_shared<SubsetRandomSampler>(num_samples, indices);
  120. uint32_t expect_labels[1][40] = {{0,0,0,1,0,0,0,1,0,0,0,1,0,0,0,0,0,0,0,1,0,1,0,0,1,0,0,0,0,0,0,1,0,0,0,0,0,0,0,1}};
  121. std::string dir = datasets_root_path_ + "/testCelebAData/";
  122. uint32_t count = 0;
  123. auto tree = Build({Celeba(16, 2, 32, dir, std::move(sampler))});
  124. tree->Prepare();
  125. Status rc = tree->Launch();
  126. if (rc.IsError()) {
  127. MS_LOG(ERROR) << "Return code error detected during tree launch: " << rc.ToString() << ".";
  128. EXPECT_TRUE(false);
  129. } else {
  130. DatasetIterator di(tree);
  131. TensorMap tersor_map;
  132. di.GetNextAsMap(&tersor_map);
  133. EXPECT_TRUE(rc.IsOk());
  134. while (tersor_map.size() != 0) {
  135. uint32_t label;
  136. for (int index = 0; index < 40; index++) {
  137. tersor_map["attr"]->GetItemAt<uint32_t>(&label, {index});
  138. EXPECT_TRUE(expect_labels[count][index] == label);
  139. }
  140. count++;
  141. di.GetNextAsMap(&tersor_map);
  142. }
  143. EXPECT_TRUE(count == 1);
  144. }
  145. }