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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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[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},
  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. uint32_t count = 0;
  57. auto tree = Build({Celeba(16, 2, 32, dir)});
  58. tree->Prepare();
  59. Status rc = tree->Launch();
  60. if (rc.IsError()) {
  61. MS_LOG(ERROR) << "Return code error detected during tree launch: " << rc.ToString() << ".";
  62. EXPECT_TRUE(false);
  63. } else {
  64. DatasetIterator di(tree);
  65. TensorMap tersor_map;
  66. di.GetNextAsMap(&tersor_map);
  67. EXPECT_TRUE(rc.IsOk());
  68. while (tersor_map.size() != 0) {
  69. uint32_t label;
  70. for (int index = 0; index < 40; index++) {
  71. tersor_map["attr"]->GetItemAt<uint32_t>(&label, {index});
  72. EXPECT_TRUE(expect_labels[count][index] == label);
  73. }
  74. count++;
  75. di.GetNextAsMap(&tersor_map);
  76. }
  77. EXPECT_TRUE(count == 2);
  78. }
  79. }
  80. TEST_F(MindDataTestCelebaDataset, TestCelebaRepeat) {
  81. std::string dir = datasets_root_path_ + "/testCelebAData/";
  82. 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},
  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,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. uint32_t count = 0;
  87. auto tree = Build({Celeba(16, 2, 32, dir), Repeat(2)});
  88. tree->Prepare();
  89. Status rc = tree->Launch();
  90. if (rc.IsError()) {
  91. MS_LOG(ERROR) << "Return code error detected during tree launch: " << rc.ToString() << ".";
  92. EXPECT_TRUE(false);
  93. } else {
  94. DatasetIterator di(tree);
  95. TensorMap tersor_map;
  96. di.GetNextAsMap(&tersor_map);
  97. EXPECT_TRUE(rc.IsOk());
  98. while (tersor_map.size() != 0) {
  99. uint32_t label;
  100. for (int index = 0; index < 40; index++) {
  101. tersor_map["attr"]->GetItemAt<uint32_t>(&label, {index});
  102. EXPECT_TRUE(expect_labels[count][index] == label);
  103. }
  104. count++;
  105. di.GetNextAsMap(&tersor_map);
  106. }
  107. EXPECT_TRUE(count == 4);
  108. }
  109. }
  110. TEST_F(MindDataTestCelebaDataset, TestSubsetRandomSamplerCeleba) {
  111. std::vector<int64_t> indices({1});
  112. int64_t num_samples = 0;
  113. std::shared_ptr<Sampler> sampler = std::make_shared<SubsetRandomSampler>(num_samples, indices);
  114. 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}};
  115. std::string dir = datasets_root_path_ + "/testCelebAData/";
  116. uint32_t count = 0;
  117. auto tree = Build({Celeba(16, 2, 32, dir, std::move(sampler))});
  118. tree->Prepare();
  119. Status rc = tree->Launch();
  120. if (rc.IsError()) {
  121. MS_LOG(ERROR) << "Return code error detected during tree launch: " << rc.ToString() << ".";
  122. EXPECT_TRUE(false);
  123. } else {
  124. DatasetIterator di(tree);
  125. TensorMap tersor_map;
  126. di.GetNextAsMap(&tersor_map);
  127. EXPECT_TRUE(rc.IsOk());
  128. while (tersor_map.size() != 0) {
  129. uint32_t label;
  130. for (int index = 0; index < 40; index++) {
  131. tersor_map["attr"]->GetItemAt<uint32_t>(&label, {index});
  132. EXPECT_TRUE(expect_labels[count][index] == label);
  133. }
  134. count++;
  135. di.GetNextAsMap(&tersor_map);
  136. }
  137. EXPECT_TRUE(count == 1);
  138. }
  139. }