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.

cifar_op_test.cc 5.6 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /**
  2. * Copyright 2019 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 "utils/ms_utils.h"
  22. #include "minddata/dataset/core/client.h"
  23. #include "minddata/dataset/core/global_context.h"
  24. #include "minddata/dataset/engine/datasetops/source/cifar_op.h"
  25. #include "minddata/dataset/engine/datasetops/source/sampler/sampler.h"
  26. #include "minddata/dataset/engine/datasetops/source/sampler/random_sampler.h"
  27. #include "minddata/dataset/engine/datasetops/source/sampler/subset_random_sampler.h"
  28. #include "minddata/dataset/util/path.h"
  29. #include "minddata/dataset/util/status.h"
  30. #include "gtest/gtest.h"
  31. #include "utils/log_adapter.h"
  32. #include "securec.h"
  33. namespace common = mindspore::common;
  34. using namespace mindspore::dataset;
  35. using mindspore::MsLogLevel::ERROR;
  36. using mindspore::ExceptionType::NoExceptionType;
  37. using mindspore::LogStream;
  38. std::shared_ptr<RepeatOp> Repeat(int repeatCnt);
  39. std::shared_ptr<ExecutionTree> Build(std::vector<std::shared_ptr<DatasetOp>> ops);
  40. std::shared_ptr<CifarOp> Cifarop(uint64_t num_works, uint64_t rows, uint64_t conns, std::string path,
  41. std::shared_ptr<SamplerRT> sampler = nullptr, bool cifar10 = true) {
  42. std::shared_ptr<CifarOp> so;
  43. CifarOp::Builder builder;
  44. Status rc = builder.SetNumWorkers(num_works)
  45. .SetCifarDir(path)
  46. .SetRowsPerBuffer(rows)
  47. .SetOpConnectorSize(conns)
  48. .SetSampler(std::move(sampler))
  49. .SetCifarType(cifar10)
  50. .Build(&so);
  51. return so;
  52. }
  53. class MindDataTestCifarOp : public UT::DatasetOpTesting {
  54. protected:
  55. };
  56. TEST_F(MindDataTestCifarOp, TestSequentialSamplerCifar10) {
  57. //Note: CIFAR and Mnist datasets are not included
  58. //as part of the build tree.
  59. //Download datasets and rebuild if data doesn't
  60. //appear in this dataset
  61. //Example: python tests/dataset/data/prep_data.py
  62. std::string folder_path = datasets_root_path_ + "/testCifar10Data/";
  63. auto tree = Build({Cifarop(16, 2, 32, folder_path, nullptr)});
  64. tree->Prepare();
  65. Status rc = tree->Launch();
  66. if (rc.IsError()) {
  67. MS_LOG(ERROR) << "Return code error detected during tree launch: " << common::SafeCStr(rc.ToString()) << ".";
  68. EXPECT_TRUE(false);
  69. } else {
  70. DatasetIterator di(tree);
  71. TensorMap tensor_map;
  72. di.GetNextAsMap(&tensor_map);
  73. EXPECT_TRUE(rc.IsOk());
  74. uint64_t i = 0;
  75. uint32_t label = 0;
  76. // Note: only iterating first 100 rows then break out.
  77. while (tensor_map.size() != 0 && i < 100) {
  78. tensor_map["label"]->GetItemAt<uint32_t>(&label, {});
  79. MS_LOG(DEBUG) << "row: " << i << "\t" << tensor_map["image"]->shape() << "label:" << label << "\n";
  80. i++;
  81. di.GetNextAsMap(&tensor_map);
  82. }
  83. EXPECT_TRUE(i == 100);
  84. }
  85. }
  86. TEST_F(MindDataTestCifarOp, TestRandomSamplerCifar10) {
  87. uint32_t original_seed = GlobalContext::config_manager()->seed();
  88. GlobalContext::config_manager()->set_seed(0);
  89. std::shared_ptr<SamplerRT> sampler = std::make_unique<RandomSamplerRT>(12, true, true);
  90. std::string folder_path = datasets_root_path_ + "/testCifar10Data/";
  91. auto tree = Build({Cifarop(16, 2, 32, folder_path, std::move(sampler))});
  92. tree->Prepare();
  93. Status rc = tree->Launch();
  94. if (rc.IsError()) {
  95. MS_LOG(ERROR) << "Return code error detected during tree launch: " << common::SafeCStr(rc.ToString()) << ".";
  96. EXPECT_TRUE(false);
  97. } else {
  98. DatasetIterator di(tree);
  99. TensorMap tensor_map;
  100. di.GetNextAsMap(&tensor_map);
  101. EXPECT_TRUE(rc.IsOk());
  102. uint64_t i = 0;
  103. uint32_t label = 0;
  104. while (tensor_map.size() != 0) {
  105. tensor_map["label"]->GetItemAt<uint32_t>(&label, {});
  106. MS_LOG(DEBUG) << "row: " << i << "\t" << tensor_map["image"]->shape() << "label:" << label << "\n";
  107. i++;
  108. di.GetNextAsMap(&tensor_map);
  109. }
  110. EXPECT_TRUE(i == 12);
  111. }
  112. GlobalContext::config_manager()->set_seed(original_seed);
  113. }
  114. TEST_F(MindDataTestCifarOp, TestSequentialSamplerCifar100) {
  115. std::string folder_path = datasets_root_path_ + "/testCifar100Data/";
  116. auto tree = Build({Cifarop(16, 2, 32, folder_path, nullptr, false)});
  117. tree->Prepare();
  118. Status rc = tree->Launch();
  119. if (rc.IsError()) {
  120. MS_LOG(ERROR) << "Return code error detected during tree launch: " << common::SafeCStr(rc.ToString()) << ".";
  121. EXPECT_TRUE(false);
  122. } else {
  123. DatasetIterator di(tree);
  124. TensorMap tensor_map;
  125. di.GetNextAsMap(&tensor_map);
  126. EXPECT_TRUE(rc.IsOk());
  127. uint64_t i = 0;
  128. uint32_t coarse = 0;
  129. uint32_t fine = 0;
  130. // only iterate to 100 then break out of loop
  131. while (tensor_map.size() != 0 && i < 100) {
  132. tensor_map["coarse_label"]->GetItemAt<uint32_t>(&coarse, {});
  133. tensor_map["fine_label"]->GetItemAt<uint32_t>(&fine, {});
  134. MS_LOG(DEBUG) << "row: " << i << "\t" << tensor_map["image"]->shape() << " coarse:"
  135. << coarse << " fine:" << fine << "\n";
  136. i++;
  137. di.GetNextAsMap(&tensor_map);
  138. }
  139. EXPECT_TRUE(i == 100);
  140. }
  141. }