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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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).SetCelebADir(dir).SetRowsPerBuffer(rows_per_buffer)
  41. .SetOpConnectorSize(queue_size).SetSampler(std::move(sampler)).SetDecode(decode)
  42. .SetDatasetType(dataset_type).Build(&so);
  43. return so;
  44. }
  45. class MindDataTestCelebaDataset : public UT::DatasetOpTesting {
  46. protected:
  47. };
  48. TEST_F(MindDataTestCelebaDataset, TestSequentialCeleba) {
  49. std::string dir = datasets_root_path_ + "/testCelebAData/";
  50. 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},
  51. {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}};
  52. uint32_t count = 0;
  53. auto tree = Build({Celeba(16, 2, 32, dir)});
  54. tree->Prepare();
  55. Status rc = tree->Launch();
  56. if (rc.IsError()) {
  57. MS_LOG(ERROR) << "Return code error detected during tree launch: " << rc.ToString() << ".";
  58. EXPECT_TRUE(false);
  59. } else {
  60. DatasetIterator di(tree);
  61. TensorMap tersor_map;
  62. di.GetNextAsMap(&tersor_map);
  63. EXPECT_TRUE(rc.IsOk());
  64. while (tersor_map.size() != 0) {
  65. uint32_t label;
  66. for (int index = 0; index < 40; index++) {
  67. tersor_map["attr"]->GetItemAt<uint32_t>(&label, {index});
  68. EXPECT_TRUE(expect_labels[count][index] == label);
  69. }
  70. count++;
  71. di.GetNextAsMap(&tersor_map);
  72. }
  73. EXPECT_TRUE(count == 2);
  74. }
  75. }
  76. TEST_F(MindDataTestCelebaDataset, TestCelebaRepeat) {
  77. std::string dir = datasets_root_path_ + "/testCelebAData/";
  78. 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},
  79. {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},
  80. {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. uint32_t count = 0;
  83. auto tree = Build({Celeba(16, 2, 32, dir), Repeat(2)});
  84. tree->Prepare();
  85. Status rc = tree->Launch();
  86. if (rc.IsError()) {
  87. MS_LOG(ERROR) << "Return code error detected during tree launch: " << rc.ToString() << ".";
  88. EXPECT_TRUE(false);
  89. } else {
  90. DatasetIterator di(tree);
  91. TensorMap tersor_map;
  92. di.GetNextAsMap(&tersor_map);
  93. EXPECT_TRUE(rc.IsOk());
  94. while (tersor_map.size() != 0) {
  95. uint32_t label;
  96. for (int index = 0; index < 40; index++) {
  97. tersor_map["attr"]->GetItemAt<uint32_t>(&label, {index});
  98. EXPECT_TRUE(expect_labels[count][index] == label);
  99. }
  100. count++;
  101. di.GetNextAsMap(&tersor_map);
  102. }
  103. EXPECT_TRUE(count == 4);
  104. }
  105. }
  106. TEST_F(MindDataTestCelebaDataset, TestSubsetRandomSamplerCeleba) {
  107. std::vector<int64_t> indices({1});
  108. int64_t num_samples = 0;
  109. std::shared_ptr<Sampler> sampler = std::make_shared<SubsetRandomSampler>(num_samples, indices);
  110. 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}};
  111. std::string dir = datasets_root_path_ + "/testCelebAData/";
  112. uint32_t count = 0;
  113. auto tree = Build({Celeba(16, 2, 32, dir, std::move(sampler))});
  114. tree->Prepare();
  115. Status rc = tree->Launch();
  116. if (rc.IsError()) {
  117. MS_LOG(ERROR) << "Return code error detected during tree launch: " << rc.ToString() << ".";
  118. EXPECT_TRUE(false);
  119. } else {
  120. DatasetIterator di(tree);
  121. TensorMap tersor_map;
  122. di.GetNextAsMap(&tersor_map);
  123. EXPECT_TRUE(rc.IsOk());
  124. while (tersor_map.size() != 0) {
  125. uint32_t label;
  126. for (int index = 0; index < 40; index++) {
  127. tersor_map["attr"]->GetItemAt<uint32_t>(&label, {index});
  128. EXPECT_TRUE(expect_labels[count][index] == label);
  129. }
  130. count++;
  131. di.GetNextAsMap(&tersor_map);
  132. }
  133. EXPECT_TRUE(count == 1);
  134. }
  135. }