Orfeo Toolbox  4.0
svm.h
Go to the documentation of this file.
1 #ifndef _LIBSVM_H
2 #define _LIBSVM_H
3 
4 #define LIBSVM_VERSION 300
5 
6 /*** Begin OTB modification ***/
7 #include <map>
8 #include <vector>
9 #include "otbMacro.h"
10 #include <string.h>
11 
12 struct svm_node;
13 struct svm_parameter;
14 
19 {
20 public:
22 
23  typedef std::map<std::string, std::string> MapType;
24  typedef MapType::iterator MapIterator;
25  typedef MapType::const_iterator MapConstIterator;
26 
28 
29  virtual ~GenericKernelFunctorBase();
30 
31  virtual GenericKernelFunctorBase* Clone() const;
32 
33  virtual double operator()(const svm_node * /*x*/, const svm_node * /*y*/, const svm_parameter& /*param*/) const;
34 
35 
36  virtual double derivative(const svm_node * /*x*/, const svm_node * /*y*/, const svm_parameter& /*param*/,
37  int /*degree*/, int /*index*/, bool /*isAtEnd*/, double /*constValue*/) const;
38 
39  virtual int load_parameters(FILE ** pfile);
40 
41  virtual int save_parameters(FILE ** pfile, const char * generic_kernel_parameters_keyword) const;
42 
43  virtual void print_parameters(void) const;
44 
45  virtual double dot(const svm_node *px, const svm_node *py) const;
46 
47  virtual svm_node * sub(const svm_node *px, const svm_node *py) const;
48 
49  virtual svm_node * add(const svm_node *px, const svm_node *py) const;
50 
51  virtual void SetName(std::string name);
52  virtual std::string GetName(void) const;
53 
54  virtual void SetMapParameters(const MapType & map);
55  virtual const MapType & GetMapParameters() const;
56  virtual MapType GetMapParameters();
57 
58  template<class T>
59  T GetValue(const char *option) const
60  {
61  std::string Value = m_MapParameters.find(std::string(option))->second;
62  T lValeur;
63  std::stringstream flux;
64  flux << Value;
65  flux >> lValeur;
66  return lValeur;
67  }
68 
69  template<class T>
70  void SetValue(const char *option, const T & value)
71  {
72  std::string lValeur;
73  std::stringstream flux;
74  flux << value;
75  flux >> lValeur;
76  m_MapParameters[std::string(option)] = lValeur;
77  }
78 
79  // Override this method to to split m_MapParameters into specific variables to speed up kernel evaluations
80  virtual void Update(void);
81 
82 protected:
83  GenericKernelFunctorBase(const Self& copy);
84 
85  Self& operator=(const Self& copy);
86 
87 private:
90 
92  std::string m_Name;
93 };
94 
99 {
100 public:
103 
105 
106  virtual ~ComposedKernelFunctor();
107 
108  virtual ComposedKernelFunctor* Clone() const;
109 
110  typedef std::vector<GenericKernelFunctorBase *> KernelListType;
111 
112  virtual double operator()(const svm_node *x, const svm_node *y, const svm_parameter& param) const;
113 
115  // degree is the developement degree
116  // index is the current value
117  // isAtEnd to indicate that it's the last possible derivation
118  // baseValue is the constant of the formula
119  virtual double derivative(const svm_node *x, const svm_node *y, const svm_parameter& param, int degree, int index, bool isAtEnd, double constValue) const;
120 
121  virtual int load_parameters(FILE ** pfile);
122 
123  virtual int save_parameters(FILE ** pfile, const char * composed_kernel_parameters_keyword) const;
124 
125  virtual void print_parameters(void)const;
126 
129 
131  void SetKernelFunctorList(const KernelListType& kernelFunctorList);
132 
133  // Add 1 element to the end of the list. A clone is made of the kernel
135 
137  std::vector<double> GetPonderationList();
138  void SetPonderationModelList(const std::vector<double> & list);
139  // Add 1 element to the end of the list
140  void AddPonderationToPonderationList(const double & pond);
141 
143  void SetMultiplyKernelFunctor( bool val );
145 
146 protected:
148  ComposedKernelFunctor(const Self& copy);
149 
150  /* Assignment operator */
151  Self& operator=(const Self& copy);
152 
153 private:
154 
155  void ClearFunctorList();
156 
160 
164  std::vector<double> m_PonderationList;
165 };
166 
167 //#ifdef __cplusplus
168 //extern "C" {
169 //#endif
170 /*** End OTB modification ***/
171 
172 extern int libsvm_version;
173 
174 struct svm_node
175 {
176  int index;
177  double value;
178 };
179 
181 {
182  int l;
183  double *y;
184  struct svm_node **x;
185 };
186 
187 enum { C_SVC, NU_SVC, ONE_CLASS, EPSILON_SVR, NU_SVR }; /* svm_type */
188 enum { LINEAR, POLY, RBF, SIGMOID, PRECOMPUTED, GENERIC, COMPOSED }; /* kernel_type */
189 
191 {
192  /*** Begin OTB modification ***/
194  : kernel_generic(NULL),
196  nr_weight(0),
198  weight(NULL)
199  {
200  }
201 
203  : kernel_generic(NULL),
205  nr_weight(0),
207  weight(NULL)
208  {
209  *this = copy;
210  }
211 
213  {
214  delete kernel_generic;
215  delete kernel_composed;
216  free(weight_label);
217  free(weight);
218  }
219 
221  {
222  svm_type = copy.svm_type;
223  kernel_type = copy.kernel_type;
224  degree = copy.degree;
225  gamma = copy.gamma;
226  coef0 = copy.coef0;
227  const_coef = copy.const_coef;
228  lin_coef = copy.lin_coef;
229  memcpy(custom, copy.custom, 500);
230  delete kernel_generic;
231  delete kernel_composed;
234  cache_size = copy.cache_size;
235  eps = copy.eps;
236  C = copy.C;
237 
238  if (nr_weight > 0)
239  {
240  free(weight_label);
241  free(weight);
242  }
243 
244  nr_weight = copy.nr_weight;
245  if (nr_weight > 0)
246  {
247  weight_label = (int *)malloc(nr_weight*sizeof(int));
248  weight = (double *)malloc(nr_weight*sizeof(double));
249  }
250  else
251  {
252  weight_label = NULL;
253  weight = NULL;
254  }
255  memcpy(weight_label, copy.weight_label, nr_weight);
256  memcpy(weight, copy.weight, nr_weight);
257  nu = copy.nu;
258  p = copy.p;
259  shrinking = copy.shrinking;
260  probability = copy.probability;
261  return *this;
262  }
263  /*** End OTB modification ***/
264 
265  int svm_type;
267  int degree; /* for poly */
268  double gamma; /* for poly/rbf/sigmoid */
269  double coef0; /* for poly/sigmoid */
270 
271  /*** Begin OTB modification ***/
272  double const_coef;
273  double lin_coef;
274  char custom[500];
277  /*** End OTB modification ***/
278 
279  /* these are for training only */
280  double cache_size; /* in MB */
281  double eps; /* stopping criteria */
282  double C; /* for C_SVC, EPSILON_SVR and NU_SVR */
283  int nr_weight; /* for C_SVC */
284  int *weight_label; /* for C_SVC */
285  double* weight; /* for C_SVC */
286  double nu; /* for NU_SVC, ONE_CLASS, and NU_SVR */
287  double p; /* for EPSILON_SVR */
288  int shrinking; /* use the shrinking heuristics */
289  int probability; /* do probability estimates */
290 };
291 
292 //
293 // svm_model
294 //
295 struct svm_model
296 {
297  struct svm_parameter param; /* parameter */
298  int nr_class; /* number of classes, = 2 in regression/one class svm */
299  int l; /* total #SV */
300  struct svm_node **SV; /* SVs (SV[l]) */
301  double **sv_coef; /* coefficients for SVs in decision functions (sv_coef[k-1][l]) */
302  double *rho; /* constants in decision functions (rho[k*(k-1)/2]) */
303  double *probA; /* pariwise probability information */
304  double *probB;
305 
306  /* for classification only */
307 
308  int *label; /* label of each class (label[k]) */
309  int *nSV; /* number of SVs for each class (nSV[k]) */
310  /* nSV[0] + nSV[1] + ... + nSV[k-1] = l */
311  /* XXX */
312  int free_sv; /* 1 if svm_model is created by svm_load_model*/
313  /* 0 if svm_model is created by svm_train */
314 };
315 
316 struct svm_model *svm_train(const struct svm_problem *prob, const struct svm_parameter *param);
317 void svm_cross_validation(const struct svm_problem *prob, const struct svm_parameter *param, int nr_fold, double *target);
318 
319 int svm_save_model(const char *model_file_name, const struct svm_model *model);
320 /*** Begin OTB modification ***/
321 struct svm_model *svm_load_model(const char *model_file_name, GenericKernelFunctorBase* generic_kernel_functor = NULL);
322 struct svm_model *svm_copy_model( const svm_model *model );
323 /*** End OTB modification ***/
324 
325 int svm_get_svm_type(const struct svm_model *model);
326 int svm_get_nr_class(const struct svm_model *model);
327 void svm_get_labels(const struct svm_model *model, int *label);
328 double svm_get_svr_probability(const struct svm_model *model);
329 
330 double svm_predict_values(const struct svm_model *model, const struct svm_node *x, double* dec_values);
331 double svm_predict(const struct svm_model *model, const struct svm_node *x);
332 double svm_predict_probability(const struct svm_model *model, const struct svm_node *x, double* prob_estimates);
333 
334 void svm_free_model_content(struct svm_model *model_ptr);
335 void svm_free_and_destroy_model(struct svm_model **model_ptr_ptr);
337 
338 const char *svm_check_parameter(const struct svm_problem *prob, const struct svm_parameter *param);
339 int svm_check_probability_model(const struct svm_model *model);
340 
341 void svm_set_print_string_function(void (*print_func)(const char *));
342 
343 // deprecated
344 // this function will be removed in future release
345 void svm_destroy_model(struct svm_model *model_ptr);
346 
347 /*** Begin OTB modification ***/
348 //#ifdef __cplusplus
349 //}
350 //#endif
351 /*** End OTB modification ***/
352 
353 #endif /* _LIBSVM_H */

Generated at Sat Mar 8 2014 16:28:52 for Orfeo Toolbox with doxygen 1.8.3.1