Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <filesystem>
- #include <fstream>
- #include <vector>
- template <class Matrix>
- void WriteMatrix(std::ofstream &out, const Matrix &matrix) {
- typename Matrix::Index rows = matrix.rows();
- typename Matrix::Index cols = matrix.cols();
- out.write((char *)matrix.data(),
- rows * cols * sizeof(typename Matrix::Scalar));
- }
- template <class Matrix> void ReadMatrix(std::ifstream &in, Matrix &matrix) {
- in.read((char *)matrix.data(),
- matrix.rows() * matrix.cols() * sizeof(typename Matrix::Scalar));
- }
- class Layer {};
- void WrireLayer(std::ofstream &out, const Layer &layer) {
- // store size, type and matrices
- }
- Layer ReadLayer(std::ifstream &in) {
- // read
- }
- class Network {
- std::vector<Layer> layers_;
- explicit Network(std::vector<Layer> layers) : layers_(std::move(layers)) {
- }
- public:
- void StoreModel(const std::filesystem::path &path) const {
- auto out_file =
- std::ofstream(path, std::ios_base::binary | std::ios_base::out |
- std::ios_base::trunc);
- uint32_t layers_count = layers_.size();
- out_file.write((char *)&layers_count, sizeof(layers_count));
- for (const auto &layer : layers_) {
- WrireLayer(out_file, layer);
- }
- }
- static Network LoadModel(const std::filesystem::path &path) {
- auto in_file =
- std::ifstream(path, std::ios_base::binary | std::ios_base::in);
- uint32_t layers_count;
- in_file.read((char *)&layers_count, sizeof(layers_count));
- std::vector<Layer> layers;
- layers.reserve(layers_count);
- for (uint32_t i = 0; i < layers_count; ++i) {
- layers.push_back(ReadLayer(in_file));
- }
- return Network(std::move(layers));
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement