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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /**
  2. * Copyright 2020-2021 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 <memory>
  17. #include <string>
  18. #include "common/common.h"
  19. #include "minddata/dataset/core/client.h"
  20. #include "minddata/dataset/engine/datasetops/source/celeba_op.h"
  21. #include "minddata/dataset/engine/datasetops/source/sampler/sequential_sampler.h"
  22. #include "minddata/dataset/engine/datasetops/source/sampler/subset_random_sampler.h"
  23. #include "minddata/dataset/util/status.h"
  24. #include "gtest/gtest.h"
  25. #include "utils/log_adapter.h"
  26. #include "securec.h"
  27. using namespace mindspore::dataset;
  28. using mindspore::LogStream;
  29. using mindspore::ExceptionType::NoExceptionType;
  30. using mindspore::MsLogLevel::ERROR;
  31. // std::shared_ptr<RepeatOp> Repeat(int repeat_cnt);
  32. // std::shared_ptr<ExecutionTree> Build(std::vector<std::shared_ptr<DatasetOp>> ops);
  33. std::shared_ptr<CelebAOp> Celeba(int32_t num_workers, int32_t queue_size, const std::string &dir,
  34. std::shared_ptr<SamplerRT> sampler = nullptr, bool decode = false,
  35. const std::string &dataset_type = "all") {
  36. if (sampler == nullptr) {
  37. const int64_t num_samples = 0;
  38. const int64_t start_index = 0;
  39. sampler = std::make_shared<SequentialSamplerRT>(start_index, num_samples);
  40. }
  41. std::unique_ptr<DataSchema> schema = std::make_unique<DataSchema>();
  42. (void)schema->AddColumn(ColDescriptor("image", DataType(DataType::DE_UINT8), TensorImpl::kFlexible, 1));
  43. (void)schema->AddColumn(ColDescriptor("attr", DataType(DataType::DE_UINT32), TensorImpl::kFlexible, 1));
  44. std::shared_ptr<ConfigManager> config_manager = GlobalContext::config_manager();
  45. auto op_connector_size = config_manager->op_connector_size();
  46. std::set<std::string> extensions = {};
  47. std::shared_ptr<CelebAOp> so = std::make_shared<CelebAOp>(num_workers, dir, op_connector_size, decode, dataset_type,
  48. extensions, std::move(schema), std::move(sampler));
  49. return so;
  50. }
  51. class MindDataTestCelebaDataset : public UT::DatasetOpTesting {
  52. protected:
  53. };
  54. TEST_F(MindDataTestCelebaDataset, TestSequentialCeleba) {
  55. std::string dir = datasets_root_path_ + "/testCelebAData/";
  56. 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,
  57. 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 1, 1, 0, 1, 0, 1, 0, 0, 1},
  58. {0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1,
  59. 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1},
  60. {0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1,
  61. 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1},
  62. {0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1,
  63. 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 1, 1, 0, 1, 0, 1, 0, 0, 1}};
  64. uint32_t count = 0;
  65. auto tree = Build({Celeba(16, 2, dir)});
  66. tree->Prepare();
  67. Status rc = tree->Launch();
  68. if (rc.IsError()) {
  69. MS_LOG(ERROR) << "Return code error detected during tree launch: " << rc.ToString() << ".";
  70. EXPECT_TRUE(false);
  71. } else {
  72. DatasetIterator di(tree);
  73. TensorMap tensor_map;
  74. ASSERT_OK(di.GetNextAsMap(&tensor_map));
  75. EXPECT_TRUE(rc.IsOk());
  76. while (tensor_map.size() != 0) {
  77. uint32_t label;
  78. for (int index = 0; index < 40; index++) {
  79. tensor_map["attr"]->GetItemAt<uint32_t>(&label, {index});
  80. EXPECT_TRUE(expect_labels[count][index] == label);
  81. }
  82. count++;
  83. ASSERT_OK(di.GetNextAsMap(&tensor_map));
  84. }
  85. EXPECT_TRUE(count == 4);
  86. }
  87. }
  88. TEST_F(MindDataTestCelebaDataset, TestCelebaRepeat) {
  89. std::string dir = datasets_root_path_ + "/testCelebAData/";
  90. 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,
  91. 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 1, 1, 0, 1, 0, 1, 0, 0, 1},
  92. {0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1,
  93. 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1},
  94. {0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1,
  95. 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1},
  96. {0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1,
  97. 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 1, 1, 0, 1, 0, 1, 0, 0, 1},
  98. {0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1,
  99. 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 1, 1, 0, 1, 0, 1, 0, 0, 1},
  100. {0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1,
  101. 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1},
  102. {0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1,
  103. 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1},
  104. {0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1,
  105. 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 1, 1, 0, 1, 0, 1, 0, 0, 1}};
  106. uint32_t count = 0;
  107. auto op1 = Celeba(16, 2, dir);
  108. auto op2 = Repeat(2);
  109. auto tree = Build({op1, op2});
  110. op1->set_total_repeats(2);
  111. op1->set_num_repeats_per_epoch(2);
  112. tree->Prepare();
  113. Status rc = tree->Launch();
  114. if (rc.IsError()) {
  115. MS_LOG(ERROR) << "Return code error detected during tree launch: " << rc.ToString() << ".";
  116. EXPECT_TRUE(false);
  117. } else {
  118. DatasetIterator di(tree);
  119. TensorMap tensor_map;
  120. ASSERT_OK(di.GetNextAsMap(&tensor_map));
  121. EXPECT_TRUE(rc.IsOk());
  122. while (tensor_map.size() != 0) {
  123. uint32_t label;
  124. for (int index = 0; index < 40; index++) {
  125. tensor_map["attr"]->GetItemAt<uint32_t>(&label, {index});
  126. EXPECT_TRUE(expect_labels[count][index] == label);
  127. }
  128. count++;
  129. ASSERT_OK(di.GetNextAsMap(&tensor_map));
  130. }
  131. EXPECT_TRUE(count == 8);
  132. }
  133. }
  134. TEST_F(MindDataTestCelebaDataset, TestSubsetRandomSamplerCeleba) {
  135. std::vector<int64_t> indices({1});
  136. int64_t num_samples = 0;
  137. std::shared_ptr<SamplerRT> sampler = std::make_shared<SubsetRandomSamplerRT>(indices, num_samples);
  138. 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,
  139. 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1}};
  140. std::string dir = datasets_root_path_ + "/testCelebAData/";
  141. uint32_t count = 0;
  142. auto tree = Build({Celeba(16, 2, dir, std::move(sampler))});
  143. tree->Prepare();
  144. Status rc = tree->Launch();
  145. if (rc.IsError()) {
  146. MS_LOG(ERROR) << "Return code error detected during tree launch: " << rc.ToString() << ".";
  147. EXPECT_TRUE(false);
  148. } else {
  149. DatasetIterator di(tree);
  150. TensorMap tensor_map;
  151. ASSERT_OK(di.GetNextAsMap(&tensor_map));
  152. EXPECT_TRUE(rc.IsOk());
  153. while (tensor_map.size() != 0) {
  154. uint32_t label;
  155. for (int index = 0; index < 40; index++) {
  156. tensor_map["attr"]->GetItemAt<uint32_t>(&label, {index});
  157. EXPECT_TRUE(expect_labels[count][index] == label);
  158. }
  159. count++;
  160. ASSERT_OK(di.GetNextAsMap(&tensor_map));
  161. }
  162. EXPECT_TRUE(count == 1);
  163. }
  164. }