latest version v1.9 - last update 10 Apr 2010 |
00001 /* 00002 * Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006 00003 * Lehrstuhl fuer Technische Informatik, RWTH-Aachen, Germany 00004 * 00005 * This file is part of the LTI-Computer Vision Library (LTI-Lib) 00006 * 00007 * The LTI-Lib is free software; you can redistribute it and/or 00008 * modify it under the terms of the GNU Lesser General Public License (LGPL) 00009 * as published by the Free Software Foundation; either version 2.1 of 00010 * the License, or (at your option) any later version. 00011 * 00012 * The LTI-Lib is distributed in the hope that it will be 00013 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty 00014 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00015 * GNU Lesser General Public License for more details. 00016 * 00017 * You should have received a copy of the GNU Lesser General Public 00018 * License along with the LTI-Lib; see the file LICENSE. If 00019 * not, write to the Free Software Foundation, Inc., 59 Temple Place - 00020 * Suite 330, Boston, MA 02111-1307, USA. 00021 */ 00022 00023 00024 /*---------------------------------------------------------------- 00025 * project ....: LTI Digital Image/Signal Processing Library 00026 * file .......: ltiHiddenMarkovModel.h 00027 * authors ....: Benjamin Winkler 00028 * organization: LTI, RWTH Aachen 00029 * creation ...: 14.12.01 00030 * revisions ..: $Id: ltiHiddenMarkovModel.h,v 1.7 2006/02/08 12:26:22 ltilib Exp $ 00031 */ 00032 00033 #ifndef _LTI_HIDDEN_MARKOV_MODEL_H_ 00034 #define _LTI_HIDDEN_MARKOV_MODEL_H_ 00035 00036 #include <string> 00037 #include <vector> 00038 #include "ltiIoObject.h" 00039 #include "ltiVector.h" 00040 #include "ltiMatrix.h" 00041 #include "ltiSequence.h" 00042 #include "ltiSTLIoInterface.h" 00043 00044 namespace lti { 00045 00046 /** 00047 * Hidden Markov Model (HMM) class. A HMM can be created using the class 00048 * hmmTrainer, which estimates the HMM %parameters <code>A, B, Pi</code>. 00049 * These stand for the transitions the emissions in the model states, and 00050 * the initial probabilities. 00051 * 00052 * For an introduction see: L. Rabiner and B.-H. Juang, "An introduction 00053 * to hidden markov models", IEEE ASSP Magazine no.1, vol.3, pp4-16, 1986. 00054 * 00055 * For computational efficiency probabilities p are not explicitly computed 00056 * but represented by scores (~ -ln(p)). Note that a high score corresponds 00057 * to a low probability, i.e. it rather resembles a cost. 00058 */ 00059 class hiddenMarkovModel : public ioObject { 00060 public: 00061 00062 /** 00063 * This class represents a single multivariate (ie multidimensional ) score 00064 * density function within a state of a hidden markov model. The dimensions 00065 * of the density function are assumed to be independent of each other. 00066 */ 00067 class singleDensity : public ioObject { 00068 public: 00069 00070 /** 00071 * default constructor 00072 */ 00073 singleDensity(); 00074 00075 /** 00076 * copy constructor 00077 * @param other the object to be copied 00078 */ 00079 singleDensity(const singleDensity& other); 00080 00081 /** 00082 * destructor 00083 */ 00084 virtual ~singleDensity(); 00085 00086 /** 00087 * copy data of "other" singleDensity. 00088 * Please note that the status string will _NOT_ be copied! 00089 */ 00090 singleDensity& copy(const singleDensity& other); 00091 00092 /** 00093 * assigment operator (alias for copy(other)). 00094 * @param other the singleDensity to be copied 00095 * @return a reference to the actual ioObject 00096 */ 00097 singleDensity& operator=(const singleDensity& other); 00098 00099 /** 00100 * returns name of this type 00101 */ 00102 const char* getTypeName() const; 00103 00104 /** 00105 * write the parameters in the given ioHandler 00106 * @param handler the ioHandler to be used 00107 * @param complete if true (the default) the enclosing begin/end will 00108 * be also written, otherwise only the data block will be written. 00109 * @return true if write was successful 00110 */ 00111 virtual bool write(ioHandler& handler, const bool complete=true) const; 00112 00113 /** 00114 * read the parameters from the given ioHandler 00115 * @param handler the ioHandler to be used 00116 * @param complete if true (the default) the enclosing begin/end will 00117 * be also written, otherwise only the data block will be written. 00118 * @return true if write was successful 00119 */ 00120 virtual bool read(ioHandler& handler,const bool complete=true); 00121 00122 # ifdef _LTI_MSC_6 00123 /** 00124 * this function is required by MSVC only, as a workaround for a 00125 * very awful bug, which exists since MSVC V.4.0, and still by 00126 * V.6.0 with all bugfixes (so called "service packs") remains 00127 * there... This method is public due to another bug, so please 00128 * NEVER EVER call this method directly: use read() instead! 00129 */ 00130 bool readMS(ioHandler& handler,const bool complete=true); 00131 00132 /** 00133 * this function is required by MSVC only, as a workaround for a 00134 * very awful bug, which exists since MSVC V.4.0, and still by 00135 * V.6.0 with all bugfixes (so called "service packs") remains 00136 * there... This method is public due to another bug, so please 00137 * NEVER EVER call this method directly: use write() instead! 00138 */ 00139 bool writeMS(ioHandler& handler,const bool complete=true) const; 00140 # endif 00141 00142 00143 /** 00144 * The densities weight expressed as score. The weight score is -ln(w), 00145 * where w would be the corresponding probability associated with this 00146 * single density. 00147 * 00148 * The weights are required in mixture densities, which consist of 00149 * several single densities, to rate the contribution of each single 00150 * density. The sum over all weights w in a mixture density must be one. 00151 * 00152 */ 00153 double weightScore; 00154 00155 /** 00156 * The mean vector. In other words the center of this density function. 00157 */ 00158 dvector mean; 00159 00160 /** 00161 * Scaling factor vector. The scaling factor is a generic term for the 00162 * spread of a density function. For example in gaussian functions it 00163 * is known as the standard deviation. 00164 */ 00165 dvector scalingFactor; 00166 00167 }; 00168 00169 /** 00170 * A mixture density. Composed of single densities to represent the 00171 * distribution within one state 00172 */ 00173 typedef std::vector<singleDensity> mixtureDensity; 00174 00175 /** 00176 * States of the model, where each state contains a mixture density 00177 */ 00178 typedef std::vector<mixtureDensity> stateList; 00179 00180 00181 /** 00182 * default constructor 00183 */ 00184 hiddenMarkovModel(); 00185 00186 /** 00187 * copy constructor 00188 * @param other the hiddenMarkovModel to be copied 00189 */ 00190 hiddenMarkovModel(const hiddenMarkovModel& other); 00191 00192 /** 00193 * destructor 00194 */ 00195 virtual ~hiddenMarkovModel(); 00196 00197 /** 00198 * copy data of "other" hiddenMarkovModel. 00199 * Please note that the status string will _NOT_ be copied! 00200 */ 00201 hiddenMarkovModel& copy(const hiddenMarkovModel& other); 00202 00203 /** 00204 * assigment operator (alias for copy(other)). 00205 * @param other the hiddenMarkovModel to be copied 00206 * @return a reference to the actual hiddenMarkovModel 00207 */ 00208 hiddenMarkovModel& operator=(const hiddenMarkovModel& other); 00209 00210 /** 00211 * returns name of this type 00212 */ 00213 const char* getTypeName() const; 00214 00215 /** 00216 * Returns the number of states of this model. 00217 */ 00218 int getNumberOfStates() const; 00219 00220 /** 00221 * write the parameters in the given ioHandler 00222 * @param handler the ioHandler to be used 00223 * @param complete if true (the default) the enclosing begin/end will 00224 * be also written, otherwise only the data block will be written. 00225 * @return true if write was successful 00226 */ 00227 virtual bool write(ioHandler& handler, const bool complete=true) const; 00228 00229 /** 00230 * read the parameters from the given ioHandler 00231 * @param handler the ioHandler to be used 00232 * @param complete if true (the default) the enclosing begin/end will 00233 * be also written, otherwise only the data block will be written. 00234 * @return true if write was successful 00235 */ 00236 virtual bool read(ioHandler& handler,const bool complete=true); 00237 00238 # ifdef _LTI_MSC_6 00239 /** 00240 * this function is required by MSVC only, as a workaround for a 00241 * very awful bug, which exists since MSVC V.4.0, and still by 00242 * V.6.0 with all bugfixes (so called "service packs") remains 00243 * there... This method is public due to another bug, so please 00244 * NEVER EVER call this method directly: use read() instead! 00245 */ 00246 bool readMS(ioHandler& handler,const bool complete=true); 00247 00248 /** 00249 * this function is required by MSVC only, as a workaround for a 00250 * very awful bug, which exists since MSVC V.4.0, and still by 00251 * V.6.0 with all bugfixes (so called "service packs") remains 00252 * there... This method is public due to another bug, so please 00253 * NEVER EVER call this method directly: use write() instead! 00254 */ 00255 bool writeMS(ioHandler& handler,const bool complete=true) const; 00256 # endif 00257 00258 00259 00260 /** 00261 * Density type used. Currently gauss or laplace are available. 00262 */ 00263 enum scoreType { 00264 gaussScore, 00265 laplaceScore 00266 }; 00267 00268 00269 /** 00270 * This number is considered to be the "impossible" score 00271 * (score equivalent to a probability of 0.0) 00272 */ 00273 static const double INFINITE_NUMBER; 00274 00275 00276 // ------------------------------------------------ 00277 // the data 00278 // ------------------------------------------------ 00279 00280 /** @name User defined 00281 * These members may (but don't have to) be specified by the user 00282 */ 00283 //@{ 00284 00285 /** 00286 * Name of the model. Can be used to identify a model by a string. 00287 */ 00288 std::string name; 00289 00290 /** 00291 * Absolute weight of each feature (dimension). 00292 * 00293 * Default is a %vector of 1.0 for all features to indicate 00294 * equal importance (weights are ignored if number of dimensions 00295 * differs from featureDimension). 00296 */ 00297 dvector featureWeights; 00298 00299 /** 00300 * Relative weight of the emission scores compared to the 00301 * initial and transition score. Can be used to compensate the 00302 * magnitude difference between those. 00303 * 00304 * Use a value >1.0, to emphasize the emission or a value <1.0 00305 * to put more weight into the transitions. Default value is 1.0 00306 * 00307 */ 00308 double emissionScoreWeight; 00309 00310 //@} 00311 00312 /** @name Generated by hmmTrainer 00313 * These members are usually estimated over a given data set 00314 * by an automatic training algorithm (see hmmTrainer) 00315 */ 00316 //@{ 00317 00318 /** 00319 * Specifies the score function to be used. 00320 * default: laplaceScore 00321 */ 00322 scoreType scoreFunction; 00323 00324 /** 00325 * List of states of the model <code>s_i</code>. 00326 * Generally the number of states is denoted <code>N</code>. 00327 * The states contain the emission functions <code>b_i</code>, 00328 * known as the HMM %parameter called <code>B</code> 00329 */ 00330 stateList states; 00331 00332 /** 00333 * Number of dimensions for an observation 00334 */ 00335 int featureDimension; 00336 00337 /** 00338 * Score for each state being the starting state. 00339 * This is the HMM %parameter called <code>Pi</code> 00340 */ 00341 dvector initialScore; 00342 00343 /** 00344 * Score for each transition. 00345 * TransitionScore[a][b] is the score for a transition from state a to b. 00346 * This is the HMM %parameter called <code>A</code> 00347 */ 00348 dmatrix transitionScore; 00349 00350 //@} 00351 00352 }; 00353 00354 00355 /** 00356 * write single density 00357 */ 00358 bool write(ioHandler& handler, 00359 const hiddenMarkovModel::singleDensity& p, 00360 const bool complete=true); 00361 00362 /** 00363 * read single density 00364 */ 00365 bool read(ioHandler& handler, hiddenMarkovModel::singleDensity& p, 00366 const bool complete=true); 00367 00368 00369 /** 00370 * write hidden markov model 00371 */ 00372 bool write(ioHandler& handler, const hiddenMarkovModel& p, 00373 const bool complete=true); 00374 00375 /** 00376 * read hidden markov model 00377 */ 00378 bool read(ioHandler& handler, hiddenMarkovModel& p, 00379 const bool complete=true); 00380 00381 } 00382 00383 #endif