[mlpack] 174/207: Add new file.

Barak A. Pearlmutter barak+git at pearlmutter.net
Thu Mar 23 17:53:51 UTC 2017


This is an automated email from the git hooks/post-receive script.

bap pushed a commit to branch master
in repository mlpack.

commit d092de825262477575417915b646434f89f5a71f
Author: Ryan Curtin <ryan at ratml.org>
Date:   Tue Feb 14 14:12:49 2017 -0500

    Add new file.
---
 src/mlpack/core/data/load_model_impl.hpp | 124 +++++++++++++++++++++++++++++++
 1 file changed, 124 insertions(+)

diff --git a/src/mlpack/core/data/load_model_impl.hpp b/src/mlpack/core/data/load_model_impl.hpp
new file mode 100644
index 0000000..556082c
--- /dev/null
+++ b/src/mlpack/core/data/load_model_impl.hpp
@@ -0,0 +1,124 @@
+/**
+ * @file load_model_impl.hpp
+ * @author Ryan Curtin
+ *
+ * Implementation of model-specific Load() function.
+ *
+ * mlpack is free software; you may redistribute it and/or modify it under the
+ * terms of the 3-clause BSD license.  You should have received a copy of the
+ * 3-clause BSD license along with mlpack.  If not, see
+ * http://www.opensource.org/licenses/BSD-3-Clause for more information.
+ */
+#ifndef MLPACK_CORE_DATA_LOAD_MODEL_IMPL_HPP
+#define MLPACK_CORE_DATA_LOAD_MODEL_IMPL_HPP
+
+// In case it hasn't already been included.
+#include "load.hpp"
+
+#include <algorithm>
+#include <mlpack/core/util/timers.hpp>
+
+#include "extension.hpp"
+
+#include <boost/serialization/serialization.hpp>
+#include <boost/algorithm/string/trim.hpp>
+#include <boost/archive/xml_iarchive.hpp>
+#include <boost/archive/text_iarchive.hpp>
+#include <boost/archive/binary_iarchive.hpp>
+#include <boost/tokenizer.hpp>
+#include <boost/algorithm/string.hpp>
+
+#include "serialization_shim.hpp"
+
+namespace mlpack {
+namespace data {
+
+// Load a model from file.
+template<typename T>
+bool Load(const std::string& filename,
+          const std::string& name,
+          T& t,
+          const bool fatal,
+          format f)
+{
+  if (f == format::autodetect)
+  {
+    std::string extension = Extension(filename);
+
+    if (extension == "xml")
+      f = format::xml;
+    else if (extension == "bin")
+      f = format::binary;
+    else if (extension == "txt")
+      f = format::text;
+    else
+    {
+      if (fatal)
+        Log::Fatal << "Unable to detect type of '" << filename << "'; incorrect"
+            << " extension?" << std::endl;
+      else
+        Log::Warn << "Unable to detect type of '" << filename << "'; load "
+            << "failed.  Incorrect extension?" << std::endl;
+
+      return false;
+    }
+  }
+
+  // Now load the given format.
+  std::ifstream ifs;
+#ifdef _WIN32 // Open non-text in binary mode on Windows.
+  if (f == format::binary)
+    ifs.open(filename, std::ifstream::in | std::ifstream::binary);
+  else
+    ifs.open(filename, std::ifstream::in);
+#else
+  ifs.open(filename, std::ifstream::in);
+#endif
+
+  if (!ifs.is_open())
+  {
+    if (fatal)
+      Log::Fatal << "Unable to open file '" << filename << "' to load object '"
+          << name << "'." << std::endl;
+    else
+      Log::Warn << "Unable to open file '" << filename << "' to load object '"
+          << name << "'." << std::endl;
+
+    return false;
+  }
+
+  try
+  {
+    if (f == format::xml)
+    {
+      boost::archive::xml_iarchive ar(ifs);
+      ar >> CreateNVP(t, name);
+    }
+    else if (f == format::text)
+    {
+      boost::archive::text_iarchive ar(ifs);
+      ar >> CreateNVP(t, name);
+    }
+    else if (f == format::binary)
+    {
+      boost::archive::binary_iarchive ar(ifs);
+      ar >> CreateNVP(t, name);
+    }
+
+    return true;
+  }
+  catch (boost::archive::archive_exception& e)
+  {
+    if (fatal)
+      Log::Fatal << e.what() << std::endl;
+    else
+      Log::Warn << e.what() << std::endl;
+
+    return false;
+  }
+}
+
+} // namespace data
+} // namespace mlpack
+
+#endif

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/debian-science/packages/mlpack.git



More information about the debian-science-commits mailing list