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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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, std::shared_ptr<Sampler> sampler = nullptr,
  38. bool decode = false, const std::string &dataset_type="all") {
  39. std::shared_ptr<CelebAOp> so;
  40. CelebAOp::Builder builder;
  41. Status rc = builder.SetNumWorkers(num_workers).SetCelebADir(dir).SetRowsPerBuffer(rows_per_buffer)
  42. .SetOpConnectorSize(queue_size).SetSampler(std::move(sampler)).SetDecode(decode)
  43. .SetDatasetType(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[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},
  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. uint32_t count = 0;
  54. auto tree = Build({Celeba(16, 2, 32, dir)});
  55. tree->Prepare();
  56. Status rc = tree->Launch();
  57. if (rc.IsError()) {
  58. MS_LOG(ERROR) << "Return code error detected during tree launch: " << rc.ToString() << ".";
  59. EXPECT_TRUE(false);
  60. } else {
  61. DatasetIterator di(tree);
  62. TensorMap tersor_map;
  63. di.GetNextAsMap(&tersor_map);
  64. EXPECT_TRUE(rc.IsOk());
  65. while (tersor_map.size() != 0) {
  66. uint32_t label;
  67. for (int index = 0; index < 40; index++) {
  68. tersor_map["attr"]->GetItemAt<uint32_t>(&label, {index});
  69. EXPECT_TRUE(expect_labels[count][index] == label);
  70. }
  71. count++;
  72. di.GetNextAsMap(&tersor_map);
  73. }
  74. EXPECT_TRUE(count == 2);
  75. }
  76. }
  77. TEST_F(MindDataTestCelebaDataset, TestCelebaRepeat) {
  78. std::string dir = datasets_root_path_ + "/testCelebAData/";
  79. 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},
  80. {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},
  81. {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. uint32_t count = 0;
  84. auto tree = Build({Celeba(16, 2, 32, dir), Repeat(2)});
  85. tree->Prepare();
  86. Status rc = tree->Launch();
  87. if (rc.IsError()) {
  88. MS_LOG(ERROR) << "Return code error detected during tree launch: " << rc.ToString() << ".";
  89. EXPECT_TRUE(false);
  90. } else {
  91. DatasetIterator di(tree);
  92. TensorMap tersor_map;
  93. di.GetNextAsMap(&tersor_map);
  94. EXPECT_TRUE(rc.IsOk());
  95. while (tersor_map.size() != 0) {
  96. uint32_t label;
  97. for (int index = 0; index < 40; index++) {
  98. tersor_map["attr"]->GetItemAt<uint32_t>(&label, {index});
  99. EXPECT_TRUE(expect_labels[count][index] == label);
  100. }
  101. count++;
  102. di.GetNextAsMap(&tersor_map);
  103. }
  104. EXPECT_TRUE(count == 4);
  105. }
  106. }
  107. TEST_F(MindDataTestCelebaDataset, TestSubsetRandomSamplerCeleba) {
  108. std::vector<int64_t> indices({1});
  109. int64_t num_samples = 0;
  110. std::shared_ptr<Sampler> sampler = std::make_shared<SubsetRandomSampler>(num_samples, 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, 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. }