iRoCS Toolbox  1.1.0
SVMBase.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: 5096 $$Name$
28 ** $Date: 2013-12-05 16:15:41 +0100 (Thu, 05 Dec 2013) $
29 ** Copyright: LGPL $Author: tschmidt $
30 ** Description:
31 **
32 **
33 **
34 **-------------------------------------------------------------------------
35 **
36 ** $Log$
37 ** Revision 1.3 2005/10/26 07:31:32 ronneber
38 ** - corrected some comments where "decision value" was called "alpha"
39 **
40 ** Revision 1.2 2005/03/29 18:03:41 ronneber
41 ** - added updateKernelCache() and clearKernelCache()
42 **
43 ** Revision 1.1 2004/09/10 11:51:50 ronneber
44 ** renamed class SVM to SVMBase to avoid confusion with SVM template parameters
45 **
46 ** Revision 1.2 2004/09/08 14:35:16 ronneber
47 ** - adapted to new ParamInfo class
48 **
49 ** Revision 1.1 2004/08/26 08:36:59 ronneber
50 ** initital import
51 **
52 **
53 **
54 **************************************************************************/
55 #ifndef SVM_HH
56 #define SVM_HH
57 
58 
59 #ifdef HAVE_CONFIG_H
60 #include <config.hh>
61 #endif
62 
63 // std includes
64 #include <iostream>
65 #include <set>
66 #include <string>
67 
68 // libsvmtl includes
69 
70 
71 #include "ProgressReporter.hh"
72 #include "Model.hh"
73 #include "SVMError.hh"
74 
75 // requirements of template parameters
79 
80 
81 namespace svt
82 {
83  /*-------------------------------------------------------------------------
84  * default values for SVM's (same as original libsvm)
85  *-------------------------------------------------------------------------*/
86  static const double TERMINATION_EPSILON_DEFAULT = 0.001;
87  static const float CACHE_SIZE_MB_DEFAULT = 40;
88  static const bool SHRINKING_FLAG_DEFAULT = true;
89 
90 
91  /*======================================================================*/
99  /*======================================================================*/
100  template< typename KF>
101  class SVMBase
102  {
104 
105 
106  public:
107 
108  typedef KF KF_type;
109  template< typename FV>
110  struct Traits
111  {
113  };
114  /*====================================================================*/
118  /*====================================================================*/
120  : _terminationEpsilon( TERMINATION_EPSILON_DEFAULT),
121  _cacheSizeMB( CACHE_SIZE_MB_DEFAULT),
122  _shrinkingFlag( SHRINKING_FLAG_DEFAULT),
123  _pr( 0)
124  {}
125 
126 
127 
128  /*====================================================================*/
136  /*====================================================================*/
137  SVMBase(const KF& kernel)
138  : p_kernel( kernel),
139  _terminationEpsilon( TERMINATION_EPSILON_DEFAULT),
140  _cacheSizeMB( CACHE_SIZE_MB_DEFAULT),
141  _shrinkingFlag( SHRINKING_FLAG_DEFAULT),
142  _pr( 0)
143  {}
144 
145 
146  /*====================================================================*/
150  /*====================================================================*/
152  {
153 
154  }
155 
156 
157  /*====================================================================*/
163  /*====================================================================*/
164  KF& kernel()
165  {
166  return p_kernel;
167  }
168 
169 
170  const KF& kernel() const
171  {
172  return p_kernel;
173  }
174 
175  /*======================================================================*/
185  /*======================================================================*/
186  template< typename ForwardIter, typename Accessor>
187  void updateKernelCache( const ForwardIter& fvBegin,
188  const ForwardIter& fvEnd,
189  Accessor accessor) const
190  {
191  p_kernel.updateCache( fvBegin, fvEnd, accessor, _pr);
192  }
193 
194 
195  /*======================================================================*/
205  /*======================================================================*/
206  void clearKernelCache() const
207  {
208  p_kernel.clearCache();
209  }
210 
211 
212  /*====================================================================*/
223  /*====================================================================*/
224  template<typename FV>
225  double classify( const FV& testObject, const Model<FV>& model) const;
226 
227 
228  /*======================================================================*/
235  /*======================================================================*/
236  void setTerminationEpsilon( double e)
237  {
239  }
240 
241  double terminationEpsilon() const
242  {
243  return _terminationEpsilon;
244  }
245 
246 
247  /*======================================================================*/
254  /*======================================================================*/
255  void setCacheSizeMB( float s)
256  {
257  _cacheSizeMB = s;
258  }
259 
260  float cacheSizeMB() const
261  {
262  return _cacheSizeMB;
263  }
264 
265 
266  /*======================================================================*/
273  /*======================================================================*/
274  void setShrinkingFlag( bool f)
275  {
276  _shrinkingFlag = f;
277  }
278 
279  bool shrinkingFlag() const
280  {
281  return _shrinkingFlag;
282  }
283 
284  /*======================================================================*/
288  /*======================================================================*/
289  template< typename STDATA>
290  void loadParameters( STDATA& stData)
291  {
293 
294  p_kernel.loadParameters( stData);
295  stData.getValue( "epsilon", _terminationEpsilon);
296  stData.getValue( "cache_size", _cacheSizeMB);
297  stData.getValue( "shrinking", _shrinkingFlag);
298  }
299 
300  /*======================================================================*/
304  /*======================================================================*/
305  template< typename STDATA>
306  void saveParameters( STDATA& stData) const
307  {
309 
310  p_kernel.saveParameters(stData);
311  stData.setValue( "epsilon", terminationEpsilon());
312  stData.setValue( "cache_size", cacheSizeMB());
313  stData.setValue( "shrinking", shrinkingFlag());
314  }
315 
316  /*======================================================================*/
325  /*======================================================================*/
326  static void getParamInfos( std::vector<ParamInfo>& p)
327  {
328  p.push_back(
329  ParamInfo( "epsilon", "e", "value",
330  "tolerance of termination criterion "
331  "(default 0.001)"));
332  p.push_back(
333  ParamInfo( "cache_size", "cs", "size",
334  "cache memory size in MB (default 40)"));
335  p.push_back(ParamInfo( "shrinking", "sh"));
336  p.back().addAlternative("0", "don't use the shrinking heuristics");
337  p.back().addAlternative("1", "use the shrinking heuristics "
338  "(default)");
339  }
340 
341 
342 
343 
344  /*======================================================================*/
357  /*======================================================================*/
359  {
360  _pr = pr;
361  }
362 
363 
364  protected:
365 
367  double _terminationEpsilon; // epsilon as termination criterium
368  float _cacheSizeMB; // cache size in Mega-Bytes
369  bool _shrinkingFlag; // flag, wether to do shrinking during training
371  };
372 
373 }
374 
375 
376 #include "SVMBase.icc"
377 
378 #endif
bool _shrinkingFlag
Definition: SVMBase.hh:369
const KF & kernel() const
Definition: SVMBase.hh:170
static void getParamInfos(std::vector< ParamInfo > &p)
get information about the parameters, that are used in loadParameters() and saveParameters().
Definition: SVMBase.hh:326
#define CHECK_MEMBER_TEMPLATE(c)
double terminationEpsilon() const
Definition: SVMBase.hh:241
void clearKernelCache() const
call the clearCache() method of selected Kernel
Definition: SVMBase.hh:206
SVMBase(const KF &kernel)
Constructor with Kernel function.
Definition: SVMBase.hh:137
#define CHECK_CLASS_TEMPLATE1(c)
static const bool SHRINKING_FLAG_DEFAULT
Definition: SVMBase.hh:88
float _cacheSizeMB
Definition: SVMBase.hh:368
ProgressReporter * _pr
Definition: SVMBase.hh:370
void setProgressReporter(ProgressReporter *pr)
set a progress reporter object.
Definition: SVMBase.hh:358
void setCacheSizeMB(float s)
set cache memory size in MB (default: 40)
Definition: SVMBase.hh:255
Ensure that TESTCLASS provides a loadParameters() and saveParamters() method.
void loadParameters(STDATA &stData)
Load parameters from structured data object.
Definition: SVMBase.hh:290
bool shrinkingFlag() const
Definition: SVMBase.hh:279
The SVMBase class is the the base class for all SVM&#39;s.
Definition: SVMBase.hh:101
float cacheSizeMB() const
Definition: SVMBase.hh:260
double classify(const FV &testObject, const Model< FV > &model) const
standard classification without caching the kernel evaluations.
void setTerminationEpsilon(double e)
set tolerance of termination criterion (default: 0.001)
Definition: SVMBase.hh:236
void saveParameters(STDATA &stData) const
save parameters to structured data object
Definition: SVMBase.hh:306
void setShrinkingFlag(bool f)
whether to use the shrinking heuristics (default: true)
Definition: SVMBase.hh:274
static const float CACHE_SIZE_MB_DEFAULT
Definition: SVMBase.hh:87
static const double TERMINATION_EPSILON_DEFAULT
Definition: SVMBase.hh:86
~SVMBase()
Destructor.
Definition: SVMBase.hh:151
svt::Model< FV > ModelType
Definition: SVMBase.hh:112
SVMBase()
Default Constructor.
Definition: SVMBase.hh:119
The ParamInfo class contains informations about one parameter like key, help text, guiHints etc.
Definition: ParamInfo.hh:82
double _terminationEpsilon
Definition: SVMBase.hh:367
KF & kernel()
access the kernel function
Definition: SVMBase.hh:164
void updateKernelCache(const ForwardIter &fvBegin, const ForwardIter &fvEnd, Accessor accessor) const
call the updateCache() method of selected Kernel
Definition: SVMBase.hh:187