00001 /*!@file Learn/HMM.H Hidden Markov Models */ 00002 00003 00004 // //////////////////////////////////////////////////////////////////// // 00005 // The iLab Neuromorphic Vision C++ Toolkit - Copyright (C) 2000-2005 // 00006 // by the University of Southern California (USC) and the iLab at USC. // 00007 // See http://iLab.usc.edu for information about this project. // 00008 // //////////////////////////////////////////////////////////////////// // 00009 // Major portions of the iLab Neuromorphic Vision Toolkit are protected // 00010 // under the U.S. patent ``Computation of Intrinsic Perceptual Saliency // 00011 // in Visual Environments, and Applications'' by Christof Koch and // 00012 // Laurent Itti, California Institute of Technology, 2001 (patent // 00013 // pending; application number 09/912,225 filed July 23, 2001; see // 00014 // http://pair.uspto.gov/cgi-bin/final/home.pl for current status). // 00015 // //////////////////////////////////////////////////////////////////// // 00016 // This file is part of the iLab Neuromorphic Vision C++ Toolkit. // 00017 // // 00018 // The iLab Neuromorphic Vision C++ Toolkit is free software; you can // 00019 // redistribute it and/or modify it under the terms of the GNU General // 00020 // Public License as published by the Free Software Foundation; either // 00021 // version 2 of the License, or (at your option) any later version. // 00022 // // 00023 // The iLab Neuromorphic Vision C++ Toolkit is distributed in the hope // 00024 // that it will be useful, but WITHOUT ANY WARRANTY; without even the // 00025 // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR // 00026 // PURPOSE. See the GNU General Public License for more details. // 00027 // // 00028 // You should have received a copy of the GNU General Public License // 00029 // along with the iLab Neuromorphic Vision C++ Toolkit; if not, write // 00030 // to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, // 00031 // Boston, MA 02111-1307 USA. // 00032 // //////////////////////////////////////////////////////////////////// // 00033 // 00034 // Primary maintainer for this file: Lior Elazary <elazary@usc.edu> 00035 // $HeadURL: $ 00036 // $Id: $ 00037 // 00038 00039 #ifndef LEARN_HMM_H_DEFINED 00040 #define LEARN_HMM_H_DEFINED 00041 00042 #include "Image/Image.H" 00043 #include "Image/Pixels.H" 00044 #include "Image/ColorOps.H" 00045 #include "Util/Types.H" // for uint 00046 #include <vector> 00047 #include <string> 00048 #include <map> 00049 00050 template <class T> 00051 class HMM 00052 { 00053 public: 00054 00055 struct Path 00056 { 00057 double prob; 00058 std::vector<size_t> path; 00059 00060 Path() : 00061 prob(0) 00062 {} 00063 }; 00064 00065 struct SeqInfo 00066 { 00067 std::vector<double> scale; 00068 std::vector< std::vector<double> > alpha; 00069 std::vector< std::vector<double> > beta; 00070 std::vector< std::vector<double> > gamma; 00071 std::vector< std::vector< std::vector<double> > > xi; 00072 00073 double prob; 00074 }; 00075 00076 00077 HMM() {} 00078 00079 HMM(const std::vector<T>& states, 00080 const std::vector<T>& observations, 00081 const std::string& name = ""); 00082 00083 //! Destructor 00084 ~HMM(); 00085 00086 //! Set the state transitions 00087 void setStateTransition(const T fromState, const T toState, double prob); 00088 00089 //! The state emission probability 00090 void setStateEmission(const T state, const T emission, double prob); 00091 00092 //! Set the current prob of which state we are in 00093 void setCurrentState(const T state, double prob); 00094 00095 //! Find the most likely sequence of hidden states given the observations and 00096 //! return the probability of this path. 00097 // This alg is using the Viterbi algorithm to find the most probable path 00098 // and the forward alg to find the prob of this path 00099 std::vector<T> getLikelyStates(const std::vector<T> observations, 00100 double& maxPathProb = double()); 00101 00102 //! Iterate through the path given one observation 00103 void iteratePath(const T observation); 00104 00105 //! Forward alg ie. compute P(Obj | currentHMMModel) 00106 double forward(const std::vector<T> observations); 00107 00108 //! Backward alg 00109 double backward(const std::vector<T> observations); 00110 00111 00112 //! Train the model by changing the state transitions and 00113 //! state emittions for the given observation 00114 void train(const std::vector< std::vector<T> > observations, size_t numIterations); 00115 00116 //! Train the model by changing the state transitions and 00117 //! state emittions for the given observation 00118 //! batch update per Fundamentals of Speech Recognition - 00119 //! by Lawrence Rabiner , Biing-Hwang Juang 00120 void train(const std::vector<T> observations, size_t numIterations); 00121 00122 void computeXi(const std::vector<T> observations); 00123 void computeGamma(const std::vector<T> observations); 00124 00125 00126 //! Get the max path so far 00127 std::vector<T> getMaxPath(double& maxPathProb); 00128 00129 //! Show the internal states and observations of the HMM 00130 void show(); 00131 00132 //! Get the hmm name 00133 std::string getName() { return itsName; } 00134 00135 00136 private: 00137 std::string itsName; 00138 00139 std::vector<T> itsStates; 00140 std::vector<T> itsObservations; 00141 00142 Image<double> itsStateTransitions; 00143 Image<double> itsStateEmissions; 00144 00145 //Map to find the index in the matrix 00146 std::map<T,size_t> itsStatesMap; 00147 std::map<T,size_t> itsObservationsMap; 00148 00149 std::vector<Path> itsCurrentPath; 00150 00151 SeqInfo itsSeqInfo; 00152 00153 00154 }; 00155 00156 // ###################################################################### 00157 /* So things look consistent in everyone's emacs... */ 00158 /* Local Variables: */ 00159 /* indent-tabs-mode: nil */ 00160 /* End: */ 00161 00162 #endif // LEARN_BAYES_H_DEFINED