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

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