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

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