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.

mnist_op_test.cc 5.7 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /**
  2. * Copyright 2019-2021 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 "utils/ms_utils.h"
  21. #include "common/common.h"
  22. #include "minddata/dataset/core/client.h"
  23. #include "minddata/dataset/core/global_context.h"
  24. #include "minddata/dataset/engine/datasetops/source/mnist_op.h"
  25. #include "minddata/dataset/engine/datasetops/source/sampler/distributed_sampler.h"
  26. #include "minddata/dataset/engine/datasetops/source/sampler/pk_sampler.h"
  27. #include "minddata/dataset/engine/datasetops/source/sampler/random_sampler.h"
  28. #include "minddata/dataset/engine/datasetops/source/sampler/sampler.h"
  29. #include "minddata/dataset/engine/datasetops/source/sampler/sequential_sampler.h"
  30. #include "minddata/dataset/engine/datasetops/source/sampler/subset_random_sampler.h"
  31. #include "minddata/dataset/engine/datasetops/source/sampler/weighted_random_sampler.h"
  32. #include "minddata/dataset/util/path.h"
  33. #include "minddata/dataset/util/status.h"
  34. #include "gtest/gtest.h"
  35. #include "utils/log_adapter.h"
  36. #include "securec.h"
  37. namespace common = mindspore::common;
  38. using namespace mindspore::dataset;
  39. using mindspore::MsLogLevel::ERROR;
  40. using mindspore::ExceptionType::NoExceptionType;
  41. using mindspore::LogStream;
  42. std::shared_ptr<BatchOp> Batch(int batch_size = 1, bool drop = false, int rows_per_buf = 2);
  43. std::shared_ptr<RepeatOp> Repeat(int repeat_cnt);
  44. std::shared_ptr<ExecutionTree> Build(std::vector<std::shared_ptr<DatasetOp>> ops);
  45. Status Create1DTensor(std::shared_ptr<Tensor> *sample_ids, int64_t num_elements, unsigned char *data = nullptr,
  46. DataType::Type data_type = DataType::DE_UINT32);
  47. std::shared_ptr<MnistOp> CreateMnist(int64_t num_wrks, int64_t rows, int64_t conns, std::string path, bool shuf = false,
  48. std::shared_ptr<SamplerRT> sampler = nullptr) {
  49. std::shared_ptr<MnistOp> so;
  50. MnistOp::Builder builder;
  51. Status rc = builder.SetNumWorkers(num_wrks)
  52. .SetDir(path)
  53. .SetRowsPerBuffer(rows)
  54. .SetOpConnectorSize(conns)
  55. .SetSampler(std::move(sampler))
  56. .Build(&so);
  57. return so;
  58. }
  59. class MindDataTestMnistSampler : public UT::DatasetOpTesting {
  60. protected:
  61. };
  62. TEST_F(MindDataTestMnistSampler, TestSequentialMnistWithRepeat) {
  63. // Note: Mnist datasets are not included
  64. // as part of the build tree.
  65. // Download datasets and rebuild if data doesn't
  66. // appear in this dataset
  67. // Example: python tests/dataset/data/prep_data.py
  68. std::string folder_path = datasets_root_path_ + "/testMnistData/";
  69. int64_t num_samples = 10;
  70. int64_t start_index = 0;
  71. auto seq_sampler = std::make_shared<SequentialSamplerRT>(num_samples, start_index);
  72. auto op1 = CreateMnist(16, 2, 32, folder_path, false, std::move(seq_sampler));
  73. auto op2 = Repeat(2);
  74. op1->set_total_repeats(2);
  75. op1->set_num_repeats_per_epoch(2);
  76. auto tree = Build({op1, op2});
  77. tree->Prepare();
  78. uint32_t res[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
  79. Status rc = tree->Launch();
  80. if (rc.IsError()) {
  81. MS_LOG(ERROR) << "Return code error detected during tree launch: " << rc.ToString() << ".";
  82. EXPECT_TRUE(false);
  83. } else {
  84. DatasetIterator di(tree);
  85. TensorMap tensor_map;
  86. di.GetNextAsMap(&tensor_map);
  87. EXPECT_TRUE(rc.IsOk());
  88. uint64_t i = 0;
  89. uint32_t label = 0;
  90. while (tensor_map.size() != 0) {
  91. tensor_map["label"]->GetItemAt<uint32_t>(&label, {});
  92. EXPECT_TRUE(res[i % 10] == label);
  93. MS_LOG(DEBUG) << "row: " << i << "\t" << tensor_map["image"]->shape() << "label:" << label << "\n";
  94. i++;
  95. di.GetNextAsMap(&tensor_map);
  96. }
  97. EXPECT_TRUE(i == 20);
  98. }
  99. }
  100. TEST_F(MindDataTestMnistSampler, TestSequentialImageFolderWithRepeatBatch) {
  101. std::string folder_path = datasets_root_path_ + "/testMnistData/";
  102. int64_t num_samples = 10;
  103. int64_t start_index = 0;
  104. auto seq_sampler = std::make_shared<SequentialSamplerRT>(num_samples, start_index);
  105. auto op1 = CreateMnist(16, 2, 32, folder_path, false, std::move(seq_sampler));
  106. auto op2 = Repeat(2);
  107. auto op3 = Batch(5);
  108. op1->set_total_repeats(2);
  109. op1->set_num_repeats_per_epoch(2);
  110. auto tree = Build({op1, op2, op3});
  111. tree->Prepare();
  112. uint32_t res[4][5] = { {0, 0, 0, 0, 0 },
  113. {0, 0, 0, 0, 0 },
  114. {0, 0, 0, 0, 0 },
  115. {0, 0, 0, 0, 0 } };
  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 tensor_map;
  123. di.GetNextAsMap(&tensor_map);
  124. EXPECT_TRUE(rc.IsOk());
  125. uint64_t i = 0;
  126. while (tensor_map.size() != 0) {
  127. std::shared_ptr<Tensor> label;
  128. Create1DTensor(&label, 5, reinterpret_cast<unsigned char *>(res[i % 4]));
  129. EXPECT_TRUE((*label) == (*tensor_map["label"]));
  130. MS_LOG(DEBUG) << "row: " << i << "\t" << tensor_map["image"]->shape() << "label:" << *tensor_map["label"] << "\n";
  131. i++;
  132. di.GetNextAsMap(&tensor_map);
  133. }
  134. EXPECT_TRUE(i == 4);
  135. }
  136. }