pasholnahuy

Untitled

May 18th, 2024
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.81 KB | None | 0 0
  1. #include <filesystem>
  2. #include <fstream>
  3. #include <vector>
  4.  
  5. template <class Matrix>
  6. void WriteMatrix(std::ofstream &out, const Matrix &matrix) {
  7.     typename Matrix::Index rows = matrix.rows();
  8.     typename Matrix::Index cols = matrix.cols();
  9.     out.write((char *)matrix.data(),
  10.               rows * cols * sizeof(typename Matrix::Scalar));
  11. }
  12.  
  13. template <class Matrix> void ReadMatrix(std::ifstream &in, Matrix &matrix) {
  14.     in.read((char *)matrix.data(),
  15.             matrix.rows() * matrix.cols() * sizeof(typename Matrix::Scalar));
  16. }
  17.  
  18. class Layer {};
  19.  
  20. void WrireLayer(std::ofstream &out, const Layer &layer) {
  21.     // store size, type and matrices
  22. }
  23.  
  24. Layer ReadLayer(std::ifstream &in) {
  25.     // read
  26. }
  27.  
  28. class Network {
  29.     std::vector<Layer> layers_;
  30.  
  31.     explicit Network(std::vector<Layer> layers) : layers_(std::move(layers)) {
  32.     }
  33.  
  34.   public:
  35.     void StoreModel(const std::filesystem::path &path) const {
  36.         auto out_file =
  37.             std::ofstream(path, std::ios_base::binary | std::ios_base::out |
  38.                                     std::ios_base::trunc);
  39.         uint32_t layers_count = layers_.size();
  40.         out_file.write((char *)&layers_count, sizeof(layers_count));
  41.         for (const auto &layer : layers_) {
  42.             WrireLayer(out_file, layer);
  43.         }
  44.     }
  45.  
  46.     static Network LoadModel(const std::filesystem::path &path) {
  47.         auto in_file =
  48.             std::ifstream(path, std::ios_base::binary | std::ios_base::in);
  49.         uint32_t layers_count;
  50.         in_file.read((char *)&layers_count, sizeof(layers_count));
  51.         std::vector<Layer> layers;
  52.         layers.reserve(layers_count);
  53.         for (uint32_t i = 0; i < layers_count; ++i) {
  54.             layers.push_back(ReadLayer(in_file));
  55.         }
  56.         return Network(std::move(layers));
  57.     }
  58. };
  59.  
Add Comment
Please, Sign In to add comment