[mlpack-svn] r16966 - mlpack/trunk/src/mlpack/methods/hmm

fastlab-svn at coffeetalk-1.cc.gatech.edu fastlab-svn at coffeetalk-1.cc.gatech.edu
Tue Aug 5 09:36:49 EDT 2014


Author: michaelfox99
Date: Tue Aug  5 09:36:49 2014
New Revision: 16966

Log:
Legacy file converter util


Added:
   mlpack/trunk/src/mlpack/methods/hmm/hmm_convert_main.cpp
      - copied, changed from r16871, /mlpack/trunk/src/mlpack/methods/hmm/hmm_generate_main.cpp

Copied: mlpack/trunk/src/mlpack/methods/hmm/hmm_convert_main.cpp (from r16871, /mlpack/trunk/src/mlpack/methods/hmm/hmm_generate_main.cpp)
==============================================================================
--- /mlpack/trunk/src/mlpack/methods/hmm/hmm_generate_main.cpp	(original)
+++ mlpack/trunk/src/mlpack/methods/hmm/hmm_convert_main.cpp	Tue Aug  5 09:36:49 2014
@@ -1,9 +1,9 @@
 /**
- * @file hmm_viterbi_main.cpp
+ * @file hmm_convert_main.cpp
  * @author Ryan Curtin
+ * @author Michael Fox
  *
- * Compute the most probably hidden state sequence of a given observation
- * sequence for a given HMM.
+ * Convert an HMM (XML) file from older MLPACK versions to current format.
  */
 #include <mlpack/core.hpp>
 
@@ -12,21 +12,13 @@
 
 #include <mlpack/methods/gmm/gmm.hpp>
 
-PROGRAM_INFO("Hidden Markov Model (HMM) Sequence Generator", "This "
-    "utility takes an already-trained HMM (--model_file) and generates a "
-    "random observation sequence and hidden state sequence based on its "
-    "parameters, saving them to the specified files (--output_file and "
-    "--state_file)");
 
-PARAM_STRING_REQ("model_file", "File containing HMM (XML).", "m");
-PARAM_INT_REQ("length", "Length of sequence to generate.", "l");
+PROGRAM_INFO("Hidden Markov Model (HMM) File Converter", "This utility takes "
+    "an already-trained HMM (--model_file) and converts it to the new format "
+    "(--output_file).");
 
-PARAM_INT("start_state", "Starting state of sequence.", "t", 0);
-PARAM_STRING("output_file", "File to save observation sequence to.", "o",
-    "output.csv");
-PARAM_STRING("state_file", "File to save hidden state sequence to (may be left "
-    "unspecified.", "S", "");
-PARAM_INT("seed", "Random seed.  If 0, 'std::time(NULL)' is used.", "s", 0);
+PARAM_STRING_REQ("model_file", "File containing HMM (XML).", "m");
+PARAM_STRING("output_file", "File to save HMM (XML) to.", "o", "output.xml");
 
 using namespace mlpack;
 using namespace mlpack::hmm;
@@ -42,88 +34,44 @@
   // Parse command line options.
   CLI::ParseCommandLine(argc, argv);
 
-  // Set random seed.
-  if (CLI::GetParam<int>("seed") != 0)
-    RandomSeed((size_t) CLI::GetParam<int>("seed"));
-  else
-    RandomSeed((size_t) time(NULL));
-
-  // Load observations.
+  // Load model
   const string modelFile = CLI::GetParam<string>("model_file");
-  const int length = CLI::GetParam<int>("length");
-  const int startState = CLI::GetParam<int>("start_state");
-
-  if (length <= 0)
-  {
-    Log::Fatal << "Invalid sequence length (" << length << "); must be greater "
-        << "than or equal to 0!" << endl;
-  }
 
   // Load model, but first we have to determine its type.
-  SaveRestoreUtility sr;
+  SaveRestoreUtility sr, sr2;
   sr.ReadFile(modelFile);
-  string type;
-  sr.LoadParameter(type, "hmm_type");
+  string emissionType;
+  sr.LoadParameter(emissionType, "hmm_type");
 
   mat observations;
   Col<size_t> sequence;
-  if (type == "discrete")
+  if (emissionType == "discrete")
   {
     HMM<DiscreteDistribution> hmm(1, DiscreteDistribution(1));
-
-    LoadHMM(hmm, sr);
-
-    if (startState < 0 || startState >= (int) hmm.Transition().n_rows)
-    {
-      Log::Fatal << "Invalid start state (" << startState << "); must be "
-          << "between 0 and number of states (" << hmm.Transition().n_rows
-          << ")!" << endl;
-    }
-
-    hmm.Generate(size_t(length), observations, sequence, size_t(startState));
+    ConvertHMM(hmm, sr);
+		hmm.Save(sr2);
   }
-  else if (type == "gaussian")
+  else if (emissionType == "gaussian")
   {
     HMM<GaussianDistribution> hmm(1, GaussianDistribution(1));
-
-    LoadHMM(hmm, sr);
-
-    if (startState < 0 || startState >= (int) hmm.Transition().n_rows)
-    {
-      Log::Fatal << "Invalid start state (" << startState << "); must be "
-          << "between 0 and number of states (" << hmm.Transition().n_rows
-          << ")!" << endl;
-    }
-
-    hmm.Generate(size_t(length), observations, sequence, size_t(startState));
+    ConvertHMM(hmm, sr);
+		hmm.Save(sr2);
   }
-  else if (type == "gmm")
+  else if (emissionType == "gmm")
   {
     HMM<GMM<> > hmm(1, GMM<>(1, 1));
-
-    LoadHMM(hmm, sr);
-
-    if (startState < 0 || startState >= (int) hmm.Transition().n_rows)
-    {
-      Log::Fatal << "Invalid start state (" << startState << "); must be "
-          << "between 0 and number of states (" << hmm.Transition().n_rows
-          << ")!" << endl;
-    }
-
-    hmm.Generate(size_t(length), observations, sequence, size_t(startState));
+    ConvertHMM(hmm, sr);
+		hmm.Save(sr2);
   }
   else
   {
-    Log::Fatal << "Unknown HMM type '" << type << "' in file '" << modelFile
+    Log::Fatal << "Unknown HMM type '" << emissionType << "' in file '" << modelFile
         << "'!" << endl;
   }
 
-  // Save observations.
+  // Save the converted model.
   const string outputFile = CLI::GetParam<string>("output_file");
-  data::Save(outputFile, observations, true);
-
-  // Do we want to save the hidden sequence?
-  const string sequenceFile = CLI::GetParam<string>("state_file");
-  if (sequenceFile != "")
-    data::Save(sequenceFile, sequence, true);
+  sr2.WriteFile(outputFile);
+  
+	return 0;
 }



More information about the mlpack-svn mailing list