LTI-Lib latest version v1.9 - last update 10 Apr 2010

ltiCrossValidator.h

00001 /*
00002  * Copyright (C) 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-Lib: Image Processing and Computer Vision Library
00026  * file .......: ltiCrossValidator.h
00027  * authors ....: Jens Paustenbach
00028  * organization: LTI, RWTH Aachen
00029  * creation ...: 25.6.2002
00030  * revisions ..: $Id: ltiCrossValidator.h,v 1.7 2006/02/07 18:16:16 ltilib Exp $
00031  */
00032 
00033 #ifndef _LTI_CROSS_VALIDATOR_H_
00034 #define _LTI_CROSS_VALIDATOR_H_
00035 
00036 //TODO: include only those files which are needed in this header!! 
00037 
00038 #include "ltiVector.h"
00039 #include "ltiMatrix.h"
00040 #include "ltiSupervisedInstanceClassifier.h"
00041 #include "ltiUniformDist.h"
00042 
00043 #include "ltiFunctor.h"
00044 
00045 namespace lti {
00046   /**
00047    *  This class does a cross validation on the given dataset and returns
00048    *  the average recognition rates.
00049    *  It divides the data at random in nbOfSplits distinct segments. Then the 
00050    *  network is trained with nbOfSplits-1 of the segments. Then each data 
00051    *  point of the remaining segment is classified. 
00052    *  This process is repeated for every possible choice of the 
00053    *  the segment which is omited from the classification process.
00054    *  The classifier that is used for validation is expected to delete old
00055    *  results from previous trainings, because the classifier is trained with
00056    *  different training data set during validation. 
00057    */
00058   class crossValidator : public functor {
00059   public:
00060     /**
00061      * the parameters for the class crossValidator
00062      */
00063     class parameters : public functor::parameters {
00064     public:
00065       /**
00066        * default constructor
00067        */
00068       parameters();
00069 
00070       /**
00071        * copy constructor
00072        * @param other the parameters object to be copied
00073        */
00074       parameters(const parameters& other);
00075 
00076       /**
00077        * destructor
00078        */
00079       ~parameters();
00080 
00081       /**
00082        * returns name of this type
00083        */
00084       const char* getTypeName() const;
00085 
00086       /**
00087        * copy the contents of a parameters object
00088        * @param other the parameters object to be copied
00089        * @return a reference to this parameters object
00090        */
00091       parameters& copy(const parameters& other);
00092    
00093       /**
00094        * copy the contents of a parameters object
00095        * @param other the parameters object to be copied
00096        * @return a reference to this parameters object
00097        */
00098       parameters& operator=(const parameters& other);
00099 
00100       /**
00101        * returns a pointer to a clone of the parameters
00102        */
00103       virtual functor::parameters* clone() const;
00104 
00105       /**
00106        * write the parameters in the given ioHandler
00107        * @param handler the ioHandler to be used
00108        * @param complete if true (the default) the enclosing begin/end will
00109        *        be also written, otherwise only the data block will be written.
00110        * @return true if write was successful
00111        */
00112       virtual bool write(ioHandler& handler,const bool complete=true) const;
00113 
00114       /**
00115        * read the parameters from the given ioHandler
00116        * @param handler the ioHandler to be used
00117        * @param complete if true (the default) the enclosing begin/end will
00118        *        be also written, otherwise only the data block will be written.
00119        * @return true if write was successful
00120        */
00121       virtual bool read(ioHandler& handler,const bool complete=true);
00122 
00123 #     ifdef _LTI_MSC_6     
00124       /**
00125        * this function is required by MSVC only, as a workaround for a
00126        * very awful bug, which exists since MSVC V.4.0, and still by
00127        * V.6.0 with all bugfixes (so called "service packs") remains
00128        * there...  This method is also public due to another bug, so please
00129        * NEVER EVER call this method directly: use read() instead
00130        */
00131       bool readMS(ioHandler& handler,const bool complete=true);
00132 
00133       /**
00134        * this function is required by MSVC only, as a workaround for a
00135        * very awful bug, which exists since MSVC V.4.0, and still by
00136        * V.6.0 with all bugfixes (so called "service packs") remains
00137        * there...  This method is also public due to another bug, so please
00138        * NEVER EVER call this method directly: use write() instead
00139        */
00140       bool writeMS(ioHandler& handler,const bool complete=true) const;
00141 #     endif
00142 
00143       // ------------------------------------------------
00144       // the parameters
00145       // ------------------------------------------------
00146 
00147       /**
00148        * the data set is divided in this number of splits
00149        */
00150       int nbOfSplits;
00151 
00152       /**
00153        * the classifier that is used for training and classifing
00154        */
00155       supervisedInstanceClassifier* classify;
00156 
00157     };
00158 
00159     /**
00160      * default constructor
00161      */
00162     crossValidator();
00163 
00164     /**
00165      * copy constructor
00166      * @param other the object to be copied
00167      */
00168     crossValidator(const crossValidator& other);
00169 
00170     /**
00171      * destructor
00172      */
00173     virtual ~crossValidator();
00174 
00175     /**
00176      * returns the name of this type ("crossValidator")
00177      */
00178     virtual const char* getTypeName() const;
00179   
00180     /**
00181      * Computes the average the average recognition rate of the given data
00182      * using the specified classifier.
00183      * @param data dmatrix with the source data.
00184      * @param ids ivector with the ids of each data point in data.
00185      * @return the average recogntion rate
00186      */
00187     double apply(const dmatrix& data, const ivector& ids) const;
00188 
00189     /**
00190      * Computes the average the average recognition rate of the given data
00191      * using the specified classifier.
00192      * @param data dmatrix with the source data.
00193      * @param ids ivector with the ids of each data point in data.
00194      * @param avRecogRate the average recognition rate
00195      * @return true if apply successful or false otherwise.
00196      */
00197     bool apply(const dmatrix& data, const ivector& ids,
00198                double& avRecogRate) const;
00199       
00200     /**
00201      * copy data of "other" functor.
00202      * @param other the functor to be copied
00203      * @return a reference to this functor object
00204      */
00205     crossValidator& copy(const crossValidator& other);
00206 
00207     /**
00208      * alias for copy member
00209      * @param other the functor to be copied
00210      * @return a reference to this functor object
00211      */
00212     crossValidator& operator=(const crossValidator& other);
00213 
00214     /**
00215      * returns a pointer to a clone of this functor.
00216      */
00217     virtual functor* clone() const;
00218 
00219     /**
00220      * returns used parameters
00221      */
00222     const parameters& getParameters() const;
00223 
00224   protected: 
00225 
00226     /**
00227      * Splits the given data into parts. The points are randomly drawn from 
00228      * the the src data.
00229      */
00230     bool splitData(const dmatrix& data,std::list<ivector>& splittedData) const;
00231 
00232     static uniformDistribution randomGenerator;
00233 
00234   };
00235 }
00236 
00237 #endif

Generated on Sat Apr 10 15:25:18 2010 for LTI-Lib by Doxygen 1.6.1