iRoCS Toolbox  1.1.0
BasicSVMAdapter.hh
Go to the documentation of this file.
1 /**************************************************************************
2  *
3  * Copyright (C) 2004-2015 Olaf Ronneberger, Florian Pigorsch, Jörg Mechnich,
4  * Thorsten Falk
5  *
6  * Image Analysis Lab, University of Freiburg, Germany
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software Foundation,
20  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  *
22  **************************************************************************/
23 
24 /**************************************************************************
25  ** Title:
26  ** $RCSfile$
27  ** $Revision: 907 $$Name$
28  ** $Date: 2006-10-06 15:50:05 +0200 (Fri, 06 Oct 2006) $
29  ** Copyright: LGPL $Author: fehr $
30  ** Description:
31  **
32  **
33  **
34  **-------------------------------------------------------------------------
35  **
36  ** $Log$
37  ** Revision 1.12 2006/10/06 13:50:05 fehr
38  ** linear model optimizer added
39  **
40  ** Revision 1.11 2006/01/25 08:53:51 fehr
41  ** added HistIntersect to Kmatrix Kernels and new BasicSVMAdapter interface to TwoClassModels
42  **
43  ** Revision 1.10 2005/10/26 06:57:05 ronneber
44  ** - added set/getStoreClassificationDetailsFlag()
45  ** - instead of classify() that returns a double detailedResultsVec, now
46  ** the details can be requested after classify() with the more general
47  ** saveClassificationDetailsASCII()
48  **
49  ** Revision 1.9 2005/07/19 13:03:59 haasdonk
50  ** removed redundant cout-messages, erroneous filename and added a new function
51  ** for computing a training-kernel matrix in BasicSVMAdapter*
52  **
53  ** Revision 1.8 2005/06/06 21:23:31 haasdonk
54  ** added updateCache() with two FV-lists, required for classification with precomputed kernel-matrices
55  **
56  ** Revision 1.7 2005/03/29 17:50:29 ronneber
57  ** - added updateKernelCache() and clearKernelCache()
58  **
59  ** Revision 1.6 2005/02/24 16:58:17 fehr
60  ** sometimes... it's better to go home: FINAL bugfix
61  **
62  ** Revision 1.5 2005/02/24 13:45:49 fehr
63  ** enable SV access through SVMAdapter
64  **
65  ** Revision 1.4 2005/02/24 12:57:50 fehr
66  ** some more 64-bit netcdf bugfixing
67  **
68  ** Revision 1.3 2004/09/08 14:15:07 ronneber
69  ** - added saveOnlyKernelParameters()
70  **
71  ** Revision 1.2 2004/09/01 14:43:36 ronneber
72  ** changed IterToPointerTraits stuff to
73  ** DirectAccessor and DereferencingAccessor, to make code more
74  ** intuitive understandable
75  **
76  ** Revision 1.1 2004/08/26 08:36:58 ronneber
77  ** initital import
78  **
79  ** Revision 1.3 2003/05/19 11:09:03 ronneber
80  ** - moved all code into templated BasicBasicSVMAdapter class, to be able to
81  ** use it with other and own FV classes
82  **
83  ** Revision 1.2 2002/09/05 13:04:15 pigorsch
84  ** - added typedef MCFV
85  **
86  ** Revision 1.1 2002/09/04 10:29:57 pigorsch
87  ** - initial revision, copied from libpollenpp and modified
88  **
89  **
90  **
91  **************************************************************************/
92 
93 #ifndef BASICSVMADAPTER_HH
94 #define BASICSVMADAPTER_HH
95 
96 #ifdef HAVE_CONFIG_H
97 #include <config.hh>
98 #endif
99 
100 // std includes
101 #include <map>
102 #include <string>
103 #include <vector>
104 
105 // libsvmtl includes
106 #include "GroupedTrainingData.hh"
107 #include "SVM_Problem.hh"
108 #include "SVMError.hh"
109 #include "BasicFV.hh"
110 #include "StDataASCIIFile.hh"
111 #include "StDataCmdLine.hh"
112 #include "ProgressReporter.hh"
113 #include "DirectAccessor.hh"
114 #include "Model.hh"
115 
116 namespace svt
117 {
118 
119  template<typename FV, typename STDATA = StDataASCIIFile>
121  {
122  public:
123 
126  {}
127 
128 
130  {}
131 
132  /*======================================================================*/
142  /*======================================================================*/
143  virtual void setProgressReporter( ProgressReporter* pr) = 0;
144 
145 
146 
147  /*======================================================================*/
161  /*======================================================================*/
162  virtual void loadParameters( STDATA& stData) = 0;
163 
164  virtual void loadParameters( StDataASCII& stData) = 0;
165 
166 
167  /*======================================================================*/
178  /*======================================================================*/
179  virtual void loadParameters( StDataCmdLine& stData) = 0;
180 
181 
182 
183  /*======================================================================*/
194  /*======================================================================*/
195  virtual void loadModel( STDATA& stData) = 0;
196 
197 
198  /*======================================================================*/
206  /*======================================================================*/
207  virtual void saveParameters( STDATA& stData) const = 0;
208 
209 
210  /*======================================================================*/
215  /*======================================================================*/
216  virtual void saveParameters( StDataASCII& stData) const = 0;
217 
218  /*======================================================================*/
223  /*======================================================================*/
224  virtual void saveOnlyKernelParameters( StDataASCII& stData) const = 0;
225 
226  /*======================================================================*/
234  /*======================================================================*/
235  virtual void saveModel( STDATA& stData) const = 0;
236 
237  /*======================================================================*/
247  /*======================================================================*/
248  virtual void saveTrainingInfos( STDATA& stData,
249  int detailLevel = 1) = 0;
250 
251  /*======================================================================*/
256  /*======================================================================*/
257  virtual void saveTrainingInfos( StDataASCII& stData,
258  int detailLevel = 1) = 0;
259 
260 
261  /*======================================================================*/
273  /*======================================================================*/
274  virtual void updateKernelCache( const GroupedTrainingData<FV>& trainData) = 0;
275 
276 
277  /*======================================================================*/
286  /*======================================================================*/
287  virtual void updateKernelCache( const SVM_Problem<FV>& problem) = 0;
288 
289 
290  /*======================================================================*/
298  /*==============================x========================================*/
299  virtual void updateTestKernelCache(typename
300  std::vector<FV>::iterator FV_begin,
301  const typename
302  std::vector<FV>::iterator& FV_end) = 0;
303 
304 
305  /*======================================================================*/
317  /*======================================================================*/
318  virtual void train( const GroupedTrainingData<FV>& trainData) = 0;
319 
320 
321  /*====================================================================*/
328  /*====================================================================*/
329  virtual void train( const SVM_Problem<FV>& problem) = 0;
330 
331  /*======================================================================*/
342  /*======================================================================*/
343  template< typename ForwardIter>
344  void train( ForwardIter FV_begin, const ForwardIter& FV_end)
345  {
346  train( FV_begin, FV_end, DirectAccessor());
347  }
348 
349  /*======================================================================*/
365  /*======================================================================*/
366  template< typename ForwardIter, typename Accessor>
367  void train( ForwardIter FV_begin,
368  const ForwardIter& FV_end,
369  Accessor accessor)
370  {
371  GroupedTrainingData<FV> trainData( FV_begin, FV_end, accessor);
372  train( trainData);
373  }
374 
375  /*======================================================================*/
389  /*======================================================================*/
390  virtual void clearKernelCache() = 0;
391 
392 
394  {
396  }
397 
399  {
401  }
402 
403 
404  /*======================================================================*/
415  /*======================================================================*/
416  virtual double classify( const FV& testObject) = 0;
417 
418 // /*====================================================================*/
419 // /*!
420 // * classify the given Feature Vector.
421 // *
422 // * \param testObject test object.
423 // * \param[out] detailedResultsVec vector with detailed results
424 // * (results of all two class
425 // * classifications)
426 // *
427 // * \return Label of best matching class or predicted value for
428 // * One-class SVM's
429 // */
430 // /*====================================================================*/
431 // virtual double classify( const FV& testObject,
432 // std::vector<double>& detailedResultsVec) = 0;
433 
434 
435  /*======================================================================*/
445  /*======================================================================*/
446  virtual void saveClassificationDetailsASCII( StDataASCII& stData) const =0;
447 
448 
449  /*======================================================================*/
455  /*======================================================================*/
456  virtual void computeTrainKernelMatrix(float** kmatrix,
457  const std::vector<FV>& featureVectors) = 0;
458 
459  /*======================================================================*/
466  /*======================================================================*/
467  virtual int nClasses() const = 0;
468 
469 
470  /*======================================================================*/
479  /*======================================================================*/
480  virtual double classIndexToLabel( unsigned int classIndex) const = 0;
481 
482 
483  /*======================================================================*/
487  /*======================================================================*/
488  virtual size_t nSupportVectors() const = 0;
489 
490  /*======================================================================*/
496  /*======================================================================*/
497  virtual FV* getSupportVector(int i) const = 0;
498 
499 
500  /*======================================================================*/
505  /*======================================================================*/
506  virtual std::vector<const typename svt::Model<FV>* > getTwoClassModels() const =0;
507  virtual std::vector<typename svt::Model<FV>* > getTwoClassModels() =0;
508 
509  /*======================================================================*/
513  /*======================================================================*/
514  virtual void optimizeLinearModel()=0;
515 
516  protected:
518 
519  };
520 
521 }
522 
523 
524 #endif
virtual void saveTrainingInfos(STDATA &stData, int detailLevel=1)=0
save additional trainnig infos to given structured data
The GroupedTrainingData class is a container for feature vectors.
virtual void clearKernelCache()=0
call clearCache() method of selected kernel, e.g., for Kernel_MATRIX this will clear the internal cac...
void setStoreClassificationDetailsFlag(bool f)
virtual FV * getSupportVector(int i) const =0
returns ith SV in model
void train(ForwardIter FV_begin, const ForwardIter &FV_end)
train the Multi Class SVM with the given feature vectors.
virtual size_t nSupportVectors() const =0
returns number of SV in model
virtual double classIndexToLabel(unsigned int classIndex) const =0
map classIndex to label (loadModel() or train() must have been called before)
virtual void saveClassificationDetailsASCII(StDataASCII &stData) const =0
classify the given Feature Vector.
void train(ForwardIter FV_begin, const ForwardIter &FV_end, Accessor accessor)
same as train(), but you can specify an Accessor for accessing the elements of your container...
virtual void computeTrainKernelMatrix(float **kmatrix, const std::vector< FV > &featureVectors)=0
Evaluate kernel matrix of the svm.
virtual void saveModel(STDATA &stData) const =0
save resulting model from training process into given map
virtual void optimizeLinearModel()=0
optimize model for faster classification
bool getStoreClassificationDetailsFlag() const
virtual void saveOnlyKernelParameters(StDataASCII &stData) const =0
save only Kernel Parameters (this is used for user information and to detect in grid search...
virtual void updateKernelCache(const GroupedTrainingData< FV > &trainData)=0
call updateCache() of selected kernel, e.g., for Kernel_MATRIX this will setup the internal cache mat...
virtual void setProgressReporter(ProgressReporter *pr)=0
set progress reporter object.
virtual void train(const GroupedTrainingData< FV > &trainData)=0
train SVM with given training data.
virtual double classify(const FV &testObject)=0
classify the given Feature Vector.
virtual void saveParameters(STDATA &stData) const =0
store all parameters of the SVM to given map
virtual void loadModel(STDATA &stData)=0
load model data from stData into SVM&#39;s.
virtual void updateTestKernelCache(typename std::vector< FV >::iterator FV_begin, const typename std::vector< FV >::iterator &FV_end)=0
Load Test Kernel Cache.
virtual void loadParameters(STDATA &stData)=0
Read all parameters (e.g.
The StDataASCII class is a container for "structured data", that is kept completly in memory...
Definition: StDataASCII.hh:83
virtual std::vector< const typename svt::Model< FV > *> getTwoClassModels() const =0
returns vector containing pointers to all TwoClassModels
virtual int nClasses() const =0
get number of Classes (loadModel() or train() must have been called before)