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.5 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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::MsLogLevel::ERROR;
  28. using mindspore::ExceptionType::NoExceptionType;
  29. using mindspore::LogStream;
  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 rows_per_buffer, int32_t queue_size,
  33. const std::string &dir, std::shared_ptr<SamplerRT> sampler = nullptr,
  34. bool decode = false, 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. .SetRowsPerBuffer(rows_per_buffer)
  40. .SetOpConnectorSize(queue_size)
  41. .SetSampler(std::move(sampler))
  42. .SetDecode(decode)
  43. .SetUsage(dataset_type).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,0,1,0,0,1,0,0,1,0,0,0,1,1,0,1,0,1,0,0,1},
  52. {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},
  53. {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},
  54. {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. uint32_t count = 0;
  56. auto tree = Build({Celeba(16, 2, 32, dir)});
  57. tree->Prepare();
  58. Status rc = tree->Launch();
  59. if (rc.IsError()) {
  60. MS_LOG(ERROR) << "Return code error detected during tree launch: " << rc.ToString() << ".";
  61. EXPECT_TRUE(false);
  62. } else {
  63. DatasetIterator di(tree);
  64. TensorMap tersor_map;
  65. di.GetNextAsMap(&tersor_map);
  66. EXPECT_TRUE(rc.IsOk());
  67. while (tersor_map.size() != 0) {
  68. uint32_t label;
  69. for (int index = 0; index < 40; index++) {
  70. tersor_map["attr"]->GetItemAt<uint32_t>(&label, {index});
  71. EXPECT_TRUE(expect_labels[count][index] == label);
  72. }
  73. count++;
  74. di.GetNextAsMap(&tersor_map);
  75. }
  76. EXPECT_TRUE(count == 4);
  77. }
  78. }
  79. TEST_F(MindDataTestCelebaDataset, TestCelebaRepeat) {
  80. std::string dir = datasets_root_path_ + "/testCelebAData/";
  81. 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},
  82. {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},
  83. {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},
  84. {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,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},
  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,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},
  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. uint32_t count = 0;
  90. auto op1 = Celeba(16, 2, 32, dir);
  91. auto op2 = Repeat(2);
  92. auto tree = Build({op1, op2});
  93. op1->set_total_repeats(2);
  94. op1->set_num_repeats_per_epoch(2);
  95. tree->Prepare();
  96. Status rc = tree->Launch();
  97. if (rc.IsError()) {
  98. MS_LOG(ERROR) << "Return code error detected during tree launch: " << rc.ToString() << ".";
  99. EXPECT_TRUE(false);
  100. } else {
  101. DatasetIterator di(tree);
  102. TensorMap tersor_map;
  103. di.GetNextAsMap(&tersor_map);
  104. EXPECT_TRUE(rc.IsOk());
  105. while (tersor_map.size() != 0) {
  106. uint32_t label;
  107. for (int index = 0; index < 40; index++) {
  108. tersor_map["attr"]->GetItemAt<uint32_t>(&label, {index});
  109. EXPECT_TRUE(expect_labels[count][index] == label);
  110. }
  111. count++;
  112. di.GetNextAsMap(&tersor_map);
  113. }
  114. EXPECT_TRUE(count == 8);
  115. }
  116. }
  117. TEST_F(MindDataTestCelebaDataset, TestSubsetRandomSamplerCeleba) {
  118. std::vector<int64_t> indices({1});
  119. int64_t num_samples = 0;
  120. std::shared_ptr<SamplerRT> sampler = std::make_shared<SubsetRandomSamplerRT>(num_samples, indices);
  121. 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,
  122. 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1}};
  123. std::string dir = datasets_root_path_ + "/testCelebAData/";
  124. uint32_t count = 0;
  125. auto tree = Build({Celeba(16, 2, 32, dir, std::move(sampler))});
  126. tree->Prepare();
  127. Status rc = tree->Launch();
  128. if (rc.IsError()) {
  129. MS_LOG(ERROR) << "Return code error detected during tree launch: " << rc.ToString() << ".";
  130. EXPECT_TRUE(false);
  131. } else {
  132. DatasetIterator di(tree);
  133. TensorMap tersor_map;
  134. di.GetNextAsMap(&tersor_map);
  135. EXPECT_TRUE(rc.IsOk());
  136. while (tersor_map.size() != 0) {
  137. uint32_t label;
  138. for (int index = 0; index < 40; index++) {
  139. tersor_map["attr"]->GetItemAt<uint32_t>(&label, {index});
  140. EXPECT_TRUE(expect_labels[count][index] == label);
  141. }
  142. count++;
  143. di.GetNextAsMap(&tersor_map);
  144. }
  145. EXPECT_TRUE(count == 1);
  146. }
  147. }