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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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 "common/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<Sampler> sampler = nullptr, bool cifar10 = true) {
  42. std::shared_ptr<CifarOp> so;
  43. CifarOp::Builder builder;
  44. Status rc = builder.SetNumWorkers(num_works).SetCifarDir(path).SetRowsPerBuffer(rows)
  45. .SetOpConnectorSize(conns).SetSampler(std::move(sampler)).SetCifarType(cifar10)
  46. .Build(&so);
  47. return so;
  48. }
  49. class MindDataTestCifarOp : public UT::DatasetOpTesting {
  50. protected:
  51. };
  52. TEST_F(MindDataTestCifarOp, TestSequentialSamplerCifar10) {
  53. //Note: CIFAR and Mnist datasets are not included
  54. //as part of the build tree.
  55. //Download datasets and rebuild if data doesn't
  56. //appear in this dataset
  57. //Example: python tests/dataset/data/prep_data.py
  58. std::string folder_path = datasets_root_path_ + "/testCifar10Data/";
  59. auto tree = Build({Cifarop(16, 2, 32, folder_path, nullptr)});
  60. tree->Prepare();
  61. Status rc = tree->Launch();
  62. if (rc.IsError()) {
  63. MS_LOG(ERROR) << "Return code error detected during tree launch: " << common::SafeCStr(rc.ToString()) << ".";
  64. EXPECT_TRUE(false);
  65. } else {
  66. DatasetIterator di(tree);
  67. TensorMap tensor_map;
  68. di.GetNextAsMap(&tensor_map);
  69. EXPECT_TRUE(rc.IsOk());
  70. uint64_t i = 0;
  71. uint32_t label = 0;
  72. // Note: only iterating first 100 rows then break out.
  73. while (tensor_map.size() != 0 && i < 100) {
  74. tensor_map["label"]->GetItemAt<uint32_t>(&label, {});
  75. MS_LOG(DEBUG) << "row: " << i << "\t" << tensor_map["image"]->shape() << "label:" << label << "\n";
  76. i++;
  77. di.GetNextAsMap(&tensor_map);
  78. }
  79. EXPECT_TRUE(i == 100);
  80. }
  81. }
  82. TEST_F(MindDataTestCifarOp, TestRandomSamplerCifar10) {
  83. uint32_t original_seed = GlobalContext::config_manager()->seed();
  84. GlobalContext::config_manager()->set_seed(0);
  85. std::shared_ptr<Sampler> sampler = std::make_unique<RandomSampler>(12, true, true);
  86. std::string folder_path = datasets_root_path_ + "/testCifar10Data/";
  87. auto tree = Build({Cifarop(16, 2, 32, folder_path, std::move(sampler))});
  88. tree->Prepare();
  89. Status rc = tree->Launch();
  90. if (rc.IsError()) {
  91. MS_LOG(ERROR) << "Return code error detected during tree launch: " << common::SafeCStr(rc.ToString()) << ".";
  92. EXPECT_TRUE(false);
  93. } else {
  94. DatasetIterator di(tree);
  95. TensorMap tensor_map;
  96. di.GetNextAsMap(&tensor_map);
  97. EXPECT_TRUE(rc.IsOk());
  98. uint64_t i = 0;
  99. uint32_t label = 0;
  100. while (tensor_map.size() != 0) {
  101. tensor_map["label"]->GetItemAt<uint32_t>(&label, {});
  102. MS_LOG(DEBUG) << "row: " << i << "\t" << tensor_map["image"]->shape() << "label:" << label << "\n";
  103. i++;
  104. di.GetNextAsMap(&tensor_map);
  105. }
  106. EXPECT_TRUE(i == 12);
  107. }
  108. GlobalContext::config_manager()->set_seed(original_seed);
  109. }
  110. TEST_F(MindDataTestCifarOp, TestSequentialSamplerCifar100) {
  111. std::string folder_path = datasets_root_path_ + "/testCifar100Data/";
  112. auto tree = Build({Cifarop(16, 2, 32, folder_path, nullptr, false)});
  113. tree->Prepare();
  114. Status rc = tree->Launch();
  115. if (rc.IsError()) {
  116. MS_LOG(ERROR) << "Return code error detected during tree launch: " << common::SafeCStr(rc.ToString()) << ".";
  117. EXPECT_TRUE(false);
  118. } else {
  119. DatasetIterator di(tree);
  120. TensorMap tensor_map;
  121. di.GetNextAsMap(&tensor_map);
  122. EXPECT_TRUE(rc.IsOk());
  123. uint64_t i = 0;
  124. uint32_t coarse = 0;
  125. uint32_t fine = 0;
  126. // only iterate to 100 then break out of loop
  127. while (tensor_map.size() != 0 && i < 100) {
  128. tensor_map["coarse_label"]->GetItemAt<uint32_t>(&coarse, {});
  129. tensor_map["fine_label"]->GetItemAt<uint32_t>(&fine, {});
  130. MS_LOG(DEBUG) << "row: " << i << "\t" << tensor_map["image"]->shape() << " coarse:"
  131. << coarse << " fine:" << fine << "\n";
  132. i++;
  133. di.GetNextAsMap(&tensor_map);
  134. }
  135. EXPECT_TRUE(i == 100);
  136. }
  137. }