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.

data_helper_test.cc 7.0 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /**
  2. * Copyright 2020 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 "minddata/dataset/core/client.h"
  22. #include "minddata/dataset/core/global_context.h"
  23. #include "minddata/dataset/core/tensor.h"
  24. #include "minddata/dataset/core/tensor_shape.h"
  25. #include "minddata/dataset/core/data_type.h"
  26. #include "minddata/dataset/engine/datasetops/source/sampler/distributed_sampler.h"
  27. #include "minddata/dataset/engine/datasetops/source/sampler/pk_sampler.h"
  28. #include "minddata/dataset/engine/datasetops/source/sampler/random_sampler.h"
  29. #include "minddata/dataset/engine/datasetops/source/sampler/sampler.h"
  30. #include "minddata/dataset/engine/datasetops/source/sampler/sequential_sampler.h"
  31. #include "minddata/dataset/engine/datasetops/source/sampler/subset_random_sampler.h"
  32. #include "minddata/dataset/engine/datasetops/source/sampler/weighted_random_sampler.h"
  33. #include "minddata/dataset/util/data_helper.h"
  34. #include "minddata/dataset/util/path.h"
  35. #include "minddata/dataset/util/status.h"
  36. #include "gtest/gtest.h"
  37. #include "utils/log_adapter.h"
  38. #include "securec.h"
  39. using namespace mindspore::dataset;
  40. using mindspore::MsLogLevel::ERROR;
  41. using mindspore::ExceptionType::NoExceptionType;
  42. using mindspore::LogStream;
  43. class MindDataTestDataHelper : public UT::DatasetOpTesting {
  44. protected:
  45. };
  46. TEST_F(MindDataTestDataHelper, MindDataTestHelper) {
  47. std::string file_path = datasets_root_path_ + "/testAlbum/images/1.json";
  48. DataHelper dh;
  49. std::vector<std::string> new_label = {"3", "4"};
  50. Status rc = dh.UpdateArray(file_path, "label", new_label);
  51. if (rc.IsError()) {
  52. MS_LOG(ERROR) << "Return code error detected during label update: " << ".";
  53. EXPECT_TRUE(false);
  54. }
  55. }
  56. TEST_F(MindDataTestDataHelper, MindDataTestAlbumGen) {
  57. std::string file_path = datasets_root_path_ + "/testAlbum/original";
  58. std::string out_path = datasets_root_path_ + "/testAlbum/testout";
  59. DataHelper dh;
  60. Status rc = dh.CreateAlbum(file_path, out_path);
  61. if (rc.IsError()) {
  62. MS_LOG(ERROR) << "Return code error detected during album generation: " << ".";
  63. EXPECT_TRUE(false);
  64. }
  65. }
  66. TEST_F(MindDataTestDataHelper, MindDataTestTemplateUpdateArrayInt) {
  67. std::string file_path = datasets_root_path_ + "/testAlbum/testout/2.json";
  68. DataHelper dh;
  69. std::vector<int> new_label = {3, 4};
  70. Status rc = dh.UpdateArray(file_path, "label", new_label);
  71. if (rc.IsError()) {
  72. MS_LOG(ERROR) << "Return code error detected during json int array update: " << ".";
  73. EXPECT_TRUE(false);
  74. }
  75. }
  76. TEST_F(MindDataTestDataHelper, MindDataTestTemplateUpdateArrayString) {
  77. std::string file_path = datasets_root_path_ + "/testAlbum/testout/3.json";
  78. DataHelper dh;
  79. std::vector<std::string> new_label = {"3", "4"};
  80. Status rc = dh.UpdateArray(file_path, "label", new_label);
  81. if (rc.IsError()) {
  82. MS_LOG(ERROR) << "Return code error detected during json string array update: " << ".";
  83. EXPECT_TRUE(false);
  84. }
  85. }
  86. TEST_F(MindDataTestDataHelper, MindDataTestTemplateUpdateValueInt) {
  87. std::string file_path = datasets_root_path_ + "/testAlbum/testout/4.json";
  88. DataHelper dh;
  89. int new_label = 3;
  90. Status rc = dh.UpdateValue(file_path, "label", new_label);
  91. if (rc.IsError()) {
  92. MS_LOG(ERROR) << "Return code error detected during json int update: " << ".";
  93. EXPECT_TRUE(false);
  94. }
  95. }
  96. TEST_F(MindDataTestDataHelper, MindDataTestTemplateUpdateString) {
  97. std::string file_path = datasets_root_path_ + "/testAlbum/testout/5.json";
  98. DataHelper dh;
  99. std::string new_label = "new label";
  100. Status rc = dh.UpdateValue(file_path, "label", new_label);
  101. if (rc.IsError()) {
  102. MS_LOG(ERROR) << "Return code error detected during json string update: " << ".";
  103. EXPECT_TRUE(false);
  104. }
  105. }
  106. TEST_F(MindDataTestDataHelper, MindDataTestDeleteKey) {
  107. std::string file_path = datasets_root_path_ + "/testAlbum/testout/5.json";
  108. DataHelper dh;
  109. Status rc = dh.RemoveKey(file_path, "label");
  110. if (rc.IsError()) {
  111. MS_LOG(ERROR) << "Return code error detected during json key remove: " << ".";
  112. EXPECT_TRUE(false);
  113. }
  114. }
  115. TEST_F(MindDataTestDataHelper, MindDataTestBinWrite) {
  116. std::string file_path = datasets_root_path_ + "/testAlbum/1.bin";
  117. DataHelper dh;
  118. std::vector<float> bin_content = {3, 4};
  119. Status rc = dh.WriteBinFile(file_path, bin_content);
  120. if (rc.IsError()) {
  121. MS_LOG(ERROR) << "Return code error detected during bin file write: " << ".";
  122. EXPECT_TRUE(false);
  123. }
  124. }
  125. TEST_F(MindDataTestDataHelper, MindDataTestBinWritePointer) {
  126. std::string file_path = datasets_root_path_ + "/testAlbum/2.bin";
  127. DataHelper dh;
  128. std::vector<float> bin_content = {3, 4};
  129. Status rc = dh.WriteBinFile(file_path, &bin_content[0], bin_content.size());
  130. if (rc.IsError()) {
  131. MS_LOG(ERROR) << "Return code error detected during binfile write: " << ".";
  132. EXPECT_TRUE(false);
  133. }
  134. }
  135. TEST_F(MindDataTestDataHelper, MindDataTestTensorWriteFloat) {
  136. // create tensor
  137. std::vector<float> y = {2.5, 3.0, 3.5, 4.0};
  138. std::shared_ptr<Tensor> t;
  139. Tensor::CreateFromVector(y, &t);
  140. // create buffer using system mempool
  141. DataHelper dh;
  142. void *data = malloc(t->SizeInBytes());
  143. auto bytes_copied = dh.DumpTensor(std::move(t), data, t->SizeInBytes());
  144. if (bytes_copied != t->SizeInBytes()) {
  145. EXPECT_TRUE(false);
  146. }
  147. float *array = static_cast<float *>(data);
  148. if (array[0] != 2.5) { EXPECT_TRUE(false); }
  149. if (array[1] != 3.0) { EXPECT_TRUE(false); }
  150. if (array[2] != 3.5) { EXPECT_TRUE(false); }
  151. if (array[3] != 4.0) { EXPECT_TRUE(false); }
  152. std::free(data);
  153. }
  154. TEST_F(MindDataTestDataHelper, MindDataTestTensorWriteUInt) {
  155. // create tensor
  156. std::vector<uint8_t> y = {1, 2, 3, 4};
  157. std::shared_ptr<Tensor> t;
  158. Tensor::CreateFromVector(y, &t);
  159. uint8_t o;
  160. t->GetItemAt<uint8_t>(&o, {0, 0});
  161. MS_LOG(INFO) << "before op :" << std::to_string(o) << ".";
  162. // create buffer using system mempool
  163. DataHelper dh;
  164. void *data = malloc(t->SizeInBytes());
  165. auto bytes_copied = dh.DumpTensor(t, data, t->SizeInBytes());
  166. if (bytes_copied != t->SizeInBytes()) {
  167. EXPECT_TRUE(false);
  168. }
  169. t->GetItemAt<uint8_t>(&o, {});
  170. MS_LOG(INFO) << "after op :" << std::to_string(o) << ".";
  171. uint8_t *array = static_cast<uint8_t *>(data);
  172. if (array[0] != 1) { EXPECT_TRUE(false); }
  173. if (array[1] != 2) { EXPECT_TRUE(false); }
  174. if (array[2] != 3) { EXPECT_TRUE(false); }
  175. if (array[3] != 4) { EXPECT_TRUE(false); }
  176. std::free(data);
  177. }