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.h 8.1 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. #ifndef MINDSPORE_CCSRC_MINDDATA_DATASET_UTIL_DATA_HELPER_H_
  17. #define MINDSPORE_CCSRC_MINDDATA_DATASET_UTIL_DATA_HELPER_H_
  18. #include <fstream>
  19. #include <iostream>
  20. #include <map>
  21. #include <memory>
  22. #include <sstream>
  23. #include <string>
  24. #include <unordered_map>
  25. #include <vector>
  26. #include <nlohmann/json.hpp>
  27. #include "./securec.h"
  28. #include "minddata/dataset/util/log_adapter.h"
  29. #include "minddata/dataset/util/path.h"
  30. #include "minddata/dataset/util/status.h"
  31. namespace mindspore {
  32. namespace dataset {
  33. /// \brief Simple class to do data manipulation, contains helper function to update json files in dataset
  34. class DataHelper {
  35. public:
  36. /// \brief constructor
  37. DataHelper() {}
  38. /// \brief Destructor
  39. ~DataHelper() = default;
  40. /// \brief Create an Album dataset while taking in a path to a image folder
  41. /// Creates the output directory if doesn't exist
  42. /// \param[in] in_dir Image folder directory that takes in images
  43. /// \param[in] out_dir Directory containing output json files
  44. Status CreateAlbum(const std::string &in_dir, const std::string &out_dir);
  45. /// \brief Update a json file field with a vector of integers
  46. /// \param in_file The input file name to read in
  47. /// \param key Key of field to write to
  48. /// \param value Value array to write to file
  49. /// \param out_file Optional input for output file path, will write to input file if not specified
  50. /// \return Status The status code returned
  51. Status UpdateArray(const std::string &in_file, const std::string &key, const std::vector<std::string> &value,
  52. const std::string &out_file = "");
  53. /// \brief Update a json file field with a vector of type T values
  54. /// \param in_file The input file name to read in
  55. /// \param key Key of field to write to
  56. /// \param value Value array to write to file
  57. /// \param out_file Optional parameter for output file path, will write to input file if not specified
  58. /// \return Status The status code returned
  59. template <typename T>
  60. Status UpdateArray(const std::string &in_file, const std::string &key, const std::vector<T> &value,
  61. const std::string &out_file = "") {
  62. try {
  63. Path in = Path(in_file);
  64. nlohmann::json js;
  65. if (in.Exists()) {
  66. std::ifstream in(in_file);
  67. MS_LOG(INFO) << "Filename: " << in_file << ".";
  68. in >> js;
  69. in.close();
  70. }
  71. js[key] = value;
  72. MS_LOG(INFO) << "Write outfile is: " << js << ".";
  73. if (out_file == "") {
  74. std::ofstream o(in_file, std::ofstream::trunc);
  75. o << js;
  76. o.close();
  77. } else {
  78. std::ofstream o(out_file, std::ofstream::trunc);
  79. o << js;
  80. o.close();
  81. }
  82. }
  83. // Catch any exception and convert to Status return code
  84. catch (const std::exception &err) {
  85. RETURN_STATUS_UNEXPECTED("Update json failed ");
  86. }
  87. return Status::OK();
  88. }
  89. /// \brief Update a json file field with a single value of of type T
  90. /// \param in_file The input file name to read in
  91. /// \param key Key of field to write to
  92. /// \param value Value to write to file
  93. /// \param out_file Optional parameter for output file path, will write to input file if not specified
  94. /// \return Status The status code returned
  95. template <typename T>
  96. Status UpdateValue(const std::string &in_file, const std::string &key, const T &value,
  97. const std::string &out_file = "") {
  98. try {
  99. Path in = Path(in_file);
  100. nlohmann::json js;
  101. if (in.Exists()) {
  102. std::ifstream in(in_file);
  103. MS_LOG(INFO) << "Filename: " << in_file << ".";
  104. in >> js;
  105. in.close();
  106. }
  107. js[key] = value;
  108. MS_LOG(INFO) << "Write outfile is: " << js << ".";
  109. if (out_file == "") {
  110. std::ofstream o(in_file, std::ofstream::trunc);
  111. o << js;
  112. o.close();
  113. } else {
  114. std::ofstream o(out_file, std::ofstream::trunc);
  115. o << js;
  116. o.close();
  117. }
  118. }
  119. // Catch any exception and convert to Status return code
  120. catch (const std::exception &err) {
  121. RETURN_STATUS_UNEXPECTED("Update json failed ");
  122. }
  123. return Status::OK();
  124. }
  125. /// \brief Template function to write tensor to file
  126. /// \param[in] in_file File to write to
  127. /// \param[in] data Array of type T values
  128. /// \return Status The status code returned
  129. template <typename T>
  130. Status WriteBinFile(const std::string &in_file, const std::vector<T> &data) {
  131. try {
  132. std::ofstream o(in_file, std::ios::binary | std::ios::out);
  133. if (!o.is_open()) {
  134. RETURN_STATUS_UNEXPECTED("Error opening Bin file to write");
  135. }
  136. size_t length = data.size();
  137. o.write(reinterpret_cast<const char *>(&data[0]), std::streamsize(length * sizeof(T)));
  138. o.close();
  139. }
  140. // Catch any exception and convert to Status return code
  141. catch (const std::exception &err) {
  142. RETURN_STATUS_UNEXPECTED("Write bin file failed ");
  143. }
  144. return Status::OK();
  145. }
  146. /// \brief Write pointer to bin, use pointer to avoid memcpy
  147. /// \param[in] in_file File name to write to
  148. /// \param[in] data Pointer to data
  149. /// \param[in] length Length of values to write from pointer
  150. /// \return Status The status code returned
  151. template <typename T>
  152. Status WriteBinFile(const std::string &in_file, T *data, size_t length) {
  153. try {
  154. std::ofstream o(in_file, std::ios::binary | std::ios::out);
  155. if (!o.is_open()) {
  156. RETURN_STATUS_UNEXPECTED("Error opening Bin file to write");
  157. }
  158. o.write(reinterpret_cast<const char *>(data), std::streamsize(length * sizeof(T)));
  159. o.close();
  160. }
  161. // Catch any exception and convert to Status return code
  162. catch (const std::exception &err) {
  163. RETURN_STATUS_UNEXPECTED("Write bin file failed ");
  164. }
  165. return Status::OK();
  166. }
  167. /// \brief Helper function to copy content of a tensor to buffer
  168. /// \note This function iterates over the tensor in bytes, since
  169. /// \param[in] tensor_addr The memory held by a tensor, e.g. tensor->GetBuffer()
  170. /// \param[in] tensor_size The amount of data in bytes in tensor_addr, e.g. tensor->SizeInBytes()
  171. /// \param[out] addr The address to copy tensor data to
  172. /// \param[in] buffer_size The buffer size of addr
  173. /// \return The size of the tensor (bytes copied
  174. size_t DumpData(const unsigned char *tensor_addr, const size_t &tensor_size, void *addr, const size_t &buffer_size);
  175. /// \brief Helper function to delete key in json file
  176. /// note This function will return okay even if key not found
  177. /// \param[in] in_file Json file to remove key from
  178. /// \param[in] key The key to remove
  179. /// \return Status The status code returned
  180. Status RemoveKey(const std::string &in_file, const std::string &key, const std::string &out_file = "");
  181. /// \brief A print method typically used for debugging
  182. /// \param out - The output stream to write output to
  183. void Print(std::ostream &out) const;
  184. /// \brief << Stream output operator overload
  185. /// \notes This allows you to write the debug print info using stream operators
  186. /// \param out Reference to the output stream being overloaded
  187. /// \param ds Reference to the DataSchema to display
  188. /// \return The output stream must be returned
  189. friend std::ostream &operator<<(std::ostream &out, const DataHelper &dh) {
  190. dh.Print(out);
  191. return out;
  192. }
  193. };
  194. } // namespace dataset
  195. } // namespace mindspore
  196. #endif // MINDSPORE_CCSRC_MINDDATA_DATASET_UTIL_DATA_HELPER_H_