mlpack
load_model_impl.hpp
Go to the documentation of this file.
1 
12 #ifndef MLPACK_CORE_DATA_LOAD_MODEL_IMPL_HPP
13 #define MLPACK_CORE_DATA_LOAD_MODEL_IMPL_HPP
14 
15 // In case it hasn't already been included.
16 #include "load.hpp"
17 
18 #include "extension.hpp"
19 
20 #include <cereal/archives/xml.hpp>
21 #include <cereal/archives/binary.hpp>
22 #include <cereal/archives/json.hpp>
23 
24 namespace mlpack {
25 namespace data {
26 
27 // Load a model from file.
28 template<typename T>
29 bool Load(const std::string& filename,
30  const std::string& name,
31  T& t,
32  const bool fatal,
33  format f)
34 {
35  if (f == format::autodetect)
36  {
37  std::string extension = Extension(filename);
38 
39  if (extension == "xml")
40  f = format::xml;
41  else if (extension == "bin")
42  f = format::binary;
43  else if (extension == "json")
44  f = format::json;
45  else
46  {
47  if (fatal)
48  Log::Fatal << "Unable to detect type of '" << filename << "'; incorrect"
49  << " extension?" << std::endl;
50  else
51  Log::Warn << "Unable to detect type of '" << filename << "'; load "
52  << "failed. Incorrect extension?" << std::endl;
53 
54  return false;
55  }
56  }
57 
58  // Now load the given format.
59  std::ifstream ifs;
60 #ifdef _WIN32 // Open non-text in binary mode on Windows.
61  if (f == format::binary)
62  ifs.open(filename, std::ifstream::in | std::ifstream::binary);
63  else
64  ifs.open(filename, std::ifstream::in);
65 #else
66  ifs.open(filename, std::ifstream::in);
67 #endif
68 
69  if (!ifs.is_open())
70  {
71  if (fatal)
72  Log::Fatal << "Unable to open file '" << filename << "' to load object '"
73  << name << "'." << std::endl;
74  else
75  Log::Warn << "Unable to open file '" << filename << "' to load object '"
76  << name << "'." << std::endl;
77 
78  return false;
79  }
80  try
81  {
82  if (f == format::xml)
83  {
84  cereal::XMLInputArchive ar(ifs);
85  ar(cereal::make_nvp(name.c_str(), t));
86  }
87  else if (f == format::json)
88  {
89  cereal::JSONInputArchive ar(ifs);
90  ar(cereal::make_nvp(name.c_str(), t));
91  }
92  else if (f == format::binary)
93  {
94  cereal::BinaryInputArchive ar(ifs);
95  ar(cereal::make_nvp(name.c_str(), t));
96  }
97 
98  return true;
99  }
100  catch (cereal::Exception& e)
101  {
102  if (fatal)
103  Log::Fatal << e.what() << std::endl;
104  else
105  Log::Warn << e.what() << std::endl;
106 
107  return false;
108  }
109 }
110 
111 } // namespace data
112 } // namespace mlpack
113 
114 #endif
static MLPACK_EXPORT util::PrefixedOutStream Fatal
Prints fatal messages prefixed with [FATAL], then terminates the program.
Definition: log.hpp:90
Linear algebra utility functions, generally performed on matrices or vectors.
Definition: cv.hpp:1
static MLPACK_EXPORT util::PrefixedOutStream Warn
Prints warning messages prefixed with [WARN ].
Definition: log.hpp:87
bool Load(const std::string &filename, arma::Mat< eT > &matrix, const bool fatal=false, const bool transpose=true, const arma::file_type inputLoadType=arma::auto_detect)
Loads a matrix from file, guessing the filetype from the extension.
Definition: load_impl.hpp:89
format
Define the formats we can read through cereal.
Definition: format.hpp:20