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 6.3 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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::unique_ptr<Sampler> sampler = nullptr,
  43. uint64_t num_samples = 0, bool cifar10 = true) {
  44. std::shared_ptr<CifarOp> so;
  45. CifarOp::Builder builder;
  46. Status rc = builder.SetNumWorkers(num_works).SetCifarDir(path).SetRowsPerBuffer(rows)
  47. .SetOpConnectorSize(conns).SetSampler(std::move(sampler)).SetCifarType(cifar10)
  48. .SetNumSamples(num_samples).Build(&so);
  49. return so;
  50. }
  51. class MindDataTestCifarOp : public UT::DatasetOpTesting {
  52. protected:
  53. };
  54. TEST_F(MindDataTestCifarOp, TestSequentialSamplerCifar10) {
  55. //Note: CIFAR and Mnist datasets are not included
  56. //as part of the build tree.
  57. //Download datasets and rebuild if data doesn't
  58. //appear in this dataset
  59. //Example: python tests/dataset/data/prep_data.py
  60. std::string folder_path = datasets_root_path_ + "/testCifar10Data/";
  61. auto tree = Build({Cifarop(16, 2, 32, folder_path, nullptr, 100)});
  62. tree->Prepare();
  63. Status rc = tree->Launch();
  64. if (rc.IsError()) {
  65. MS_LOG(ERROR) << "Return code error detected during tree launch: " << common::SafeCStr(rc.ToString()) << ".";
  66. EXPECT_TRUE(false);
  67. } else {
  68. DatasetIterator di(tree);
  69. TensorMap tensor_map;
  70. di.GetNextAsMap(&tensor_map);
  71. EXPECT_TRUE(rc.IsOk());
  72. uint64_t i = 0;
  73. uint32_t label = 0;
  74. while (tensor_map.size() != 0) {
  75. tensor_map["label"]->GetItemAt<uint32_t>(&label, {});
  76. std::cout << "row: " << i++ << "\t" << tensor_map["image"]->shape() << "label:" << label << "\n";
  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::unique_ptr<Sampler> sampler = mindspore::make_unique<RandomSampler>(true, 12);
  86. std::string folder_path = datasets_root_path_ + "/testCifar10Data/";
  87. auto tree = Build({Cifarop(16, 2, 32, folder_path, std::move(sampler), 100)});
  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. std::cout << "row: " << i++ << "\t" << tensor_map["image"]->shape() << "label:" << label << "\n";
  103. di.GetNextAsMap(&tensor_map);
  104. }
  105. EXPECT_TRUE(i == 12);
  106. }
  107. GlobalContext::config_manager()->set_seed(original_seed);
  108. }
  109. TEST_F(MindDataTestCifarOp, TestCifar10NumSample) {
  110. std::string folder_path = datasets_root_path_ + "/testCifar10Data/";
  111. auto tree = Build({Cifarop(16, 2, 32, folder_path, nullptr, 100)});
  112. tree->Prepare();
  113. Status rc = tree->Launch();
  114. if (rc.IsError()) {
  115. MS_LOG(ERROR) << "Return code error detected during tree launch: " << common::SafeCStr(rc.ToString()) << ".";
  116. EXPECT_TRUE(false);
  117. } else {
  118. DatasetIterator di(tree);
  119. TensorMap tensor_map;
  120. di.GetNextAsMap(&tensor_map);
  121. EXPECT_TRUE(rc.IsOk());
  122. uint64_t i = 0;
  123. uint32_t label = 0;
  124. while (tensor_map.size() != 0) {
  125. tensor_map["label"]->GetItemAt<uint32_t>(&label, {});
  126. std::cout << "row: " << i++ << "\t" << tensor_map["image"]->shape() << "label:" << label << "\n";
  127. di.GetNextAsMap(&tensor_map);
  128. }
  129. EXPECT_TRUE(i == 100);
  130. }
  131. }
  132. TEST_F(MindDataTestCifarOp, TestSequentialSamplerCifar100) {
  133. std::string folder_path = datasets_root_path_ + "/testCifar100Data/";
  134. auto tree = Build({Cifarop(16, 2, 32, folder_path, nullptr, 100, false)});
  135. tree->Prepare();
  136. Status rc = tree->Launch();
  137. if (rc.IsError()) {
  138. MS_LOG(ERROR) << "Return code error detected during tree launch: " << common::SafeCStr(rc.ToString()) << ".";
  139. EXPECT_TRUE(false);
  140. } else {
  141. DatasetIterator di(tree);
  142. TensorMap tensor_map;
  143. di.GetNextAsMap(&tensor_map);
  144. EXPECT_TRUE(rc.IsOk());
  145. uint64_t i = 0;
  146. uint32_t coarse = 0;
  147. uint32_t fine = 0;
  148. while (tensor_map.size() != 0) {
  149. tensor_map["coarse_label"]->GetItemAt<uint32_t>(&coarse, {});
  150. tensor_map["fine_label"]->GetItemAt<uint32_t>(&fine, {});
  151. std::cout << "row: " << i++ << "\t" << tensor_map["image"]->shape() << " coarse:"
  152. << coarse << " fine:" << fine << "\n";
  153. di.GetNextAsMap(&tensor_map);
  154. }
  155. EXPECT_TRUE(i == 100);
  156. }
  157. }

MindSpore is a new open source deep learning training/inference framework that could be used for mobile, edge and cloud scenarios.