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

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