iRoCS Toolbox  1.1.0
TwoClassSVMc.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: two class C-SVM
26 ** $RCSfile$
27 ** $Revision: 2824 $$Name$
28 ** $Date: 2009-09-14 09:30:46 +0200 (Mon, 14 Sep 2009) $
29 ** Copyright: GPL $Author: ronneber $
30 ** Description:
31 **
32 **
33 **
34 **-------------------------------------------------------------------------
35 **
36 ** $Log$
37 ** Revision 1.3 2004/09/08 14:40:16 ronneber
38 ** - changed key for two class svm selection from "svm_type" to "two_class_type"
39 ** - adapted to new ParamInfo class
40 **
41 ** Revision 1.2 2004/09/01 14:43:36 ronneber
42 ** changed IterToPointerTraits stuff to
43 ** DirectAccessor and DereferencingAccessor, to make code more
44 ** intuitive understandable
45 **
46 ** Revision 1.1 2004/08/26 08:36:59 ronneber
47 ** initital import
48 **
49 ** Revision 1.8 2003/05/19 11:27:01 ronneber
50 ** - converted from MapTools to ParamMapWrapper
51 **
52 ** Revision 1.7 2002/09/05 13:10:54 pigorsch
53 ** - modified to use new MapTools
54 **
55 ** Revision 1.6 2002/06/07 11:41:17 ronneber
56 ** - made ModelType in train() an own template parameter to make
57 ** it more flexible (e.g. when using a slightly different but
58 ** compatible Feature Vector class)
59 **
60 ** - added two additional interfaces for train() method
61 **
62 ** - removed obsolete train_one() and solve_c_svc() method
63 **
64 ** Revision 1.5 2002/06/05 17:18:31 mechnich
65 ** Made minor corrections for compilation under Win32
66 **
67 ** Revision 1.4 2002/05/21 18:31:31 ronneber
68 ** - now all parameters can be queried
69 **
70 ** Revision 1.3 2002/05/10 11:07:03 ronneber
71 ** - removed FV template for all public classes, because Feature Vector type
72 ** can be extracted automatically from passed iterators or other
73 ** parameters -- this makes the public interface much more intuitive
74 **
75 ** Revision 1.2 2002/05/06 12:28:17 ronneber
76 ** - removed Parameters struct and added setCost() and setWeightOfPositiveClass() instead
77 **
78 ** Revision 1.1 2002/03/26 12:44:02 ronneber
79 ** restructured for autoconf
80 **
81 ** Revision 1.1 2002/03/13 14:07:21 pigorsch
82 ** * initial revision
83 **
84 **
85 **
86 **************************************************************************/
87 
88 #ifndef TWOCLASSSVMC_HH
89 #define TWOCLASSSVMC_HH
90 
91 #ifdef HAVE_CONFIG_H
92 #include <config.hh>
93 #endif
94 
95 // std includes
96 #include <map>
97 #include <set>
98 #include <string>
99 
100 // libsvmtl includes
101 
102 #include "TwoClassSVM.hh"
103 #include "SVC_Q.hh"
104 #include "Solver.hh"
105 #include "SVM_Problem.hh"
106 #include "SolutionInfo.hh"
107 #include "StDataASCII.hh"
108 #include "DirectAccessor.hh"
109 
110 // requirements of template parameters
113 
114 
115 namespace svt
116 {
117  /*-------------------------------------------------------------------------
118  * default values for C-SVM (same as original libsvm)
119  *-------------------------------------------------------------------------*/
120  static const double COST_DEFAULT = 1.0;
121  static const double POSITIVE_CLASS_WEIGHT_DEFAULT = 1.0;
122 
123  /*======================================================================*/
130  /*======================================================================*/
131  template< typename KF>
132  class TwoClassSVMc: public TwoClassSVM< KF>
133  {
134  public:
135  template< typename FV>
136  struct Traits
137  {
139  };
140 
141 
142 
143  /*====================================================================*/
147  /*====================================================================*/
149  :TwoClassSVM<KF>(),
150  _cost( COST_DEFAULT),
151  _positiveClassWeight( POSITIVE_CLASS_WEIGHT_DEFAULT)
152  {}
153 
154 
155 
156  /*====================================================================*/
162  /*====================================================================*/
163  TwoClassSVMc(const KF& kernel)
164  :TwoClassSVM<KF>(kernel),
165  _cost( COST_DEFAULT),
166  _positiveClassWeight( POSITIVE_CLASS_WEIGHT_DEFAULT)
167  {}
168 
169 
170  /*====================================================================*/
174  /*====================================================================*/
176  {}
177 
178 
179  /*====================================================================*/
190  /*====================================================================*/
191  template<typename FV>
192  void train( const SVM_Problem<FV>& problem,
193  Model<FV>& model) const;
194 
195  /*====================================================================*/
212  /*====================================================================*/
213  template<typename ForwardIter>
214  void train( ForwardIter FV_begin, const ForwardIter& FV_end,
215  svt::Model<typename std::iterator_traits<ForwardIter>::value_type>& model) const
216  {
217  train( FV_begin, FV_end, model, DirectAccessor());
218  }
219 
220 
221  /*====================================================================*/
243  /*====================================================================*/
244  template<typename ForwardIter, typename Accessor>
245  void train( ForwardIter FV_begin, const ForwardIter& FV_end,
246  svt::Model<typename Accessor::template Traits<ForwardIter>::value_type>& model,
247  Accessor accessor) const;
248 
249 
250 
251  /*======================================================================*/
258  /*======================================================================*/
259  void setCost( double c)
260  {
261  _cost = c;
262  }
263 
264  double cost() const
265  {
266  return _cost;
267  }
268 
269 
270  /*======================================================================*/
278  /*======================================================================*/
279  void setWeightOfPositiveClass( double weight)
280  {
281  _positiveClassWeight = weight;
282  }
283 
284  double weightOfPositiveClass() const
285  {
286  return _positiveClassWeight;
287  }
288 
289 
290  /*======================================================================*/
294  /*======================================================================*/
295  template<typename STDATA>
296  void loadParameters( STDATA& stData)
297  {
299 
301  stData.getValue( "cost", _cost);
302  stData.getValue( "weight", _positiveClassWeight);
303  }
304 
305  template<typename STDATA>
306  void saveParameters( STDATA& stData) const
307  {
309 
311  stData.setValue( "two_class_type", name());
312  stData.setValue( "cost", _cost);
313  stData.setValue( "weight", _positiveClassWeight);
314  }
315 
316  static const char* name()
317  {
318  return "c_svc";
319  }
320 
321  static const char* description()
322  {
323  return "Two class SVM using C-SVC algorithm for training";
324  }
325 
326  /*======================================================================*/
335  /*======================================================================*/
336  static void getParamInfos( std::vector<ParamInfo>& p)
337  {
338  p.push_back(
339  ParamInfo( "cost", "c", "value",
340  "penalty cost for wrong training vectors "
341  "in C-SVC. (default 1)"));
342  p.push_back(
343  ParamInfo( "weight", "w", "value",
344  "weight for positive class samples in "
345  "two-class C-SVC. (default 1)"));
346 
348 
349  }
350 
351  protected:
352  template< typename FV>
353  void solve_c_svc( const SVM_Problem<FV> *prob,
354  double *alpha, SolutionInfo* si,
355  Model<FV>& model) const;
356 
357 
358  private:
359  double _cost; // parameter 'C': Cost for outliers
360  double _positiveClassWeight; // weight for positive class
361 
362 
363  };
364 
365 }
366 
367 #include "TwoClassSVMc.icc"
368 
369 #endif
static const double POSITIVE_CLASS_WEIGHT_DEFAULT
static void getParamInfos(std::vector< ParamInfo > &p)
get information about the parameters, that are used in loadParameters() and saveParameters().
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)
void setCost(double c)
set the parameter C for C-SVM (cost for outliers).
void train(ForwardIter FV_begin, const ForwardIter &FV_end, svt::Model< typename std::iterator_traits< ForwardIter >::value_type > &model) const
train (STL-like interface) Your feature vector class must provide a method double getLabel()...
double weightOfPositiveClass() const
void loadParameters(STDATA &stData)
Load and save parameters cost and weight from map.
void setWeightOfPositiveClass(double weight)
set the parameter C of positive class (labeled with +1) to weight*C.
TwoClassSVMc(const KF &kernel)
(description)
static const double COST_DEFAULT
void loadParameters(STDATA &stData)
Load parameters from structured data object.
Definition: SVMBase.hh:290
static const char * name()
The TwoClassSVMc class (is/provides/specifies/contains)
~TwoClassSVMc()
(description)
void saveParameters(STDATA &stData) const
save parameters to structured data object
Definition: SVMBase.hh:306
The TwoClassSVM class is the basic class for TwoClassSVMc and TwoClassSVMnu and maybe some more later...
Definition: TwoClassSVM.hh:133
double cost() const
TwoClassSVMc()
(description)
void saveParameters(STDATA &stData) const
void train(const SVM_Problem< FV > &problem, Model< FV > &model) const
train SVM with given Feature Vectors.
static const char * description()
void solve_c_svc(const SVM_Problem< FV > *prob, double *alpha, SolutionInfo *si, Model< FV > &model) const
The ParamInfo class contains informations about one parameter like key, help text, guiHints etc.
Definition: ParamInfo.hh:82
KF & kernel()
access the kernel function
Definition: SVMBase.hh:164