Orfeo ToolBox  4.2
Orfeo ToolBox is not a black box
otbSVMModel.h
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: ORFEO Toolbox
4  Language: C++
5  Date: $Date$
6  Version: $Revision$
7 
8 
9  Copyright (c) Centre National d'Etudes Spatiales. All rights reserved.
10  See OTBCopyright.txt for details.
11 
12 
13  This software is distributed WITHOUT ANY WARRANTY; without even
14  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15  PURPOSE. See the above copyright notices for more information.
16 
17 =========================================================================*/
18 #ifndef __otbSVMModel_h
19 #define __otbSVMModel_h
20 
21 #include "itkObjectFactory.h"
22 #include "itkDataObject.h"
24 #include "svm.h"
25 #include "itkTimeProbe.h"
26 
27 namespace otb
28 {
29 
58 template <class TValue, class TLabel>
59 class ITK_EXPORT SVMModel : public itk::DataObject
60 {
61 public:
63  typedef SVMModel Self;
67 
69  typedef TValue ValueType;
71  typedef TLabel LabelType;
72  typedef std::vector<ValueType> MeasurementType;
73  typedef std::pair<MeasurementType, LabelType> SampleType;
74  typedef std::vector<SampleType> SamplesVectorType;
76  typedef std::vector<struct svm_node *> CacheVectorType;
77 
81 
82  typedef struct svm_node * NodeCacheType;
83 
85  itkNewMacro(Self);
86  itkTypeMacro(SVMModel, itk::DataObject);
87 
89  unsigned int GetNumberOfClasses(void) const
90  {
91  return (unsigned int) (m_Model->nr_class);
92  }
93 
95  unsigned int GetNumberOfHyperplane(void) const
96  {
97  return (unsigned int) (m_Model->nr_class * (m_Model->nr_class - 1) / 2);
98  }
99 
102  void SetModel(struct svm_model* aModel);
103 
105  const struct svm_model* GetModel()
106  {
107  return m_Model;
108  }
109 
111  struct svm_parameter& GetParameters()
112  {
113  return m_Parameters;
114  }
116  const struct svm_parameter& GetParameters() const
117  {
118  return m_Parameters;
119  }
120 
122  void SaveModel(const char* model_file_name) const;
123  void SaveModel(const std::string& model_file_name) const
124  {
125  //implemented in term of const char * version
126  this->SaveModel(model_file_name.c_str());
127  }
128 
130  void LoadModel(const char* model_file_name);
131  void LoadModel(const std::string& model_file_name)
132  {
133  //implemented in term of const char * version
134  this->LoadModel(model_file_name.c_str());
135  }
136 
138  Pointer GetCopy() const;
139 
141  void SetSVMType(int svmtype)
142  {
143  m_Parameters.svm_type = svmtype;
144  this->Modified();
145  }
146 
148  int GetSVMType(void) const
149  {
150  return m_Parameters.svm_type;
151  }
152 
158  void SetKernelType(int kerneltype)
159  {
160  m_Parameters.kernel_type = kerneltype;
161  this->Modified();
162  }
163 
165  int GetKernelType(void) const
166  {
167  return m_Parameters.kernel_type;
168  }
169 
171  void SetPolynomialKernelDegree(int degree)
172  {
173  m_Parameters.degree = degree;
174  this->Modified();
175  }
176 
179  {
180  return m_Parameters.degree;
181  }
182 
184  virtual void SetKernelGamma(double gamma)
185  {
186  m_Parameters.gamma = gamma;
187  this->Modified();
188  }
190  double GetKernelGamma(void) const
191  {
192  return m_Parameters.gamma;
193  }
194 
196  void SetKernelCoef0(double coef0)
197  {
198  m_Parameters.coef0 = coef0;
199  this->Modified();
200  }
201 
203  double GetKernelCoef0(void) const
204  {
205  //return m_Parameters.coef0;
206  return m_Parameters.coef0;
207  }
208 
210  void SetNu(double nu)
211  {
212  m_Parameters.nu = nu;
213  this->Modified();
214  }
215 
217  double GetNu(void) const
218  {
219  //return m_Parameters.nu;
220  return m_Parameters.nu;
221  }
222 
224  void SetCacheSize(int cSize)
225  {
226  m_Parameters.cache_size = static_cast<double>(cSize);
227  this->Modified();
228  }
229 
231  int GetCacheSize(void) const
232  {
233  return static_cast<int>(m_Parameters.cache_size);
234  }
235 
237  void SetC(double c)
238  {
239  m_Parameters.C = c;
240  this->Modified();
241  }
242 
244  double GetC(void) const
245  {
246  return m_Parameters.C;
247  }
248 
250  void SetEpsilon(double eps)
251  {
252  m_Parameters.eps = eps;
253  this->Modified();
254  }
255 
257  double GetEpsilon(void) const
258  {
259  return m_Parameters.eps;
260  }
261 
262  /* Set the value of p for EPSILON_SVR */
263  void SetP(double p)
264  {
265  m_Parameters.p = p;
266  this->Modified();
267  }
268 
269  /* Get the value of p for EPSILON_SVR */
270  double GetP(void) const
271  {
272  return m_Parameters.p;
273  }
274 
276  void DoShrinking(bool s)
277  {
278  m_Parameters.shrinking = static_cast<int>(s);
279  this->Modified();
280  }
281 
283  bool GetDoShrinking(void) const
284  {
285  return static_cast<bool>(m_Parameters.shrinking);
286  }
287 
289  void DoProbabilityEstimates(bool prob)
290  {
291  m_Parameters.probability = static_cast<int>(prob);
292  this->Modified();
293  }
294 
296  bool GetDoProbabilityEstimates(void) const
297  {
298  return static_cast<bool>(m_Parameters.probability);
299  }
300 
302  GenericKernelFunctorBase * GetKernelFunctor(void) const
303  {
304  return m_Parameters.kernel_generic;
305  }
306 
307  void SetKernelFunctor(GenericKernelFunctorBase* pGenericKernelFunctor)
308  {
309  if (pGenericKernelFunctor != NULL)
310  m_Parameters.kernel_generic = pGenericKernelFunctor->Clone();
311  this->Modified();
312  }
313 
316  {
317  return m_Model->l;
318  }
319 
321  double * GetRho(void) const
322  {
323  return m_Model->rho;
324  }
326  svm_node ** GetSupportVectors(void)
327  {
328  return m_Model->SV;
329  }
331  void SetSupportVectors(svm_node ** sv, int nbOfSupportVector);
332 
334  double ** GetAlpha(void)
335  {
336  return m_Model->sv_coef;
337  }
339  void SetAlpha(double ** alpha, int nbOfSupportVector);
340 
342  int * GetLabels()
343  {
344  return m_Model->label;
345  }
346 
349  {
350  return m_Model->nSV;
351  }
352 
353  struct svm_problem& GetProblem()
354  {
355  return m_Problem;
356  }
357 
359  virtual void Modified()
360  {
361  Superclass::Modified();
362  m_ModelUpToDate = false;
363  }
364 
366  void BuildProblem();
367 
369  void ConsistencyCheck();
370 
372  void Train();
373 
375  double CrossValidation(unsigned int nbFolders);
376 
381  LabelType EvaluateLabel(const MeasurementType& measure) const;
382 
387  DistancesVectorType EvaluateHyperplanesDistances(const MeasurementType& measure) const;
388 
395  ProbabilitiesVectorType EvaluateProbabilities(const MeasurementType& measure) const;
396 
398  void AddSample(const MeasurementType& measure, const LabelType& label);
399 
401  void ClearSamples();
402 
404  void SetSamples(const SamplesVectorType& samples);
405 
408  void Reset();
409 
410 protected:
412  SVMModel();
414  virtual ~SVMModel();
416  void PrintSelf(std::ostream& os, itk::Indent indent) const;
417 
419  void DeleteProblem();
420 
422  void DeleteModel();
423 
425  void Initialize();
426 
427 private:
428  SVMModel(const Self &); //purposely not implemented
429  void operator =(const Self&); //purposely not implemented
430 
432  struct svm_model* m_Model;
433 
436 
438  struct svm_problem m_Problem;
439 
441  struct svm_parameter m_Parameters;
442 
445 
448 }; // class SVMModel
449 
450 } // namespace otb
451 
452 #ifndef OTB_MANUAL_INSTANTIATION
453 #include "otbSVMModel.txx"
454 #endif
455 
456 #endif
void SetPolynomialKernelDegree(int degree)
Definition: otbSVMModel.h:171
bool GetDoShrinking(void) const
Definition: otbSVMModel.h:283
itk::SmartPointer< const Self > ConstPointer
Definition: otbSVMModel.h:66
itk::VariableLengthVector< double > ProbabilitiesVectorType
Definition: otbSVMModel.h:79
int GetPolynomialKernelDegree(void) const
Definition: otbSVMModel.h:178
virtual void Modified()
Definition: otbSVMModel.h:359
int * GetNumberOfSVPerClasse()
Definition: otbSVMModel.h:348
double GetP(void) const
Definition: otbSVMModel.h:270
int GetSVMType(void) const
Definition: otbSVMModel.h:148
bool GetDoProbabilityEstimates(void) const
Definition: otbSVMModel.h:296
TValue ValueType
Definition: otbSVMModel.h:69
int GetCacheSize(void) const
Definition: otbSVMModel.h:231
void LoadModel(const std::string &model_file_name)
Definition: otbSVMModel.h:131
int * GetLabels()
Definition: otbSVMModel.h:342
double GetEpsilon(void) const
Definition: otbSVMModel.h:257
const struct svm_model * GetModel()
Definition: otbSVMModel.h:105
itk::DataObject Superclass
Definition: otbSVMModel.h:64
unsigned int GetNumberOfHyperplane(void) const
Definition: otbSVMModel.h:95
const struct svm_parameter & GetParameters() const
Definition: otbSVMModel.h:116
itk::SmartPointer< Self > Pointer
Definition: otbSVMModel.h:65
svm_node ** GetSupportVectors(void)
Definition: otbSVMModel.h:326
struct svm_model * m_Model
Definition: otbSVMModel.h:432
void SetKernelCoef0(double coef0)
Definition: otbSVMModel.h:196
void SetCacheSize(int cSize)
Definition: otbSVMModel.h:224
double GetC(void) const
Definition: otbSVMModel.h:244
double GetKernelGamma(void) const
Definition: otbSVMModel.h:190
GenericKernelFunctorBase * GetKernelFunctor(void) const
Definition: otbSVMModel.h:302
unsigned int GetNumberOfClasses(void) const
Definition: otbSVMModel.h:89
void SetC(double c)
Definition: otbSVMModel.h:237
virtual void SetKernelGamma(double gamma)
Definition: otbSVMModel.h:184
Class for SVM models.
Definition: otbSVMModel.h:59
bool m_ProblemUpToDate
Definition: otbSVMModel.h:444
TLabel LabelType
Definition: otbSVMModel.h:71
double GetKernelCoef0(void) const
Definition: otbSVMModel.h:203
double ** GetAlpha(void)
Definition: otbSVMModel.h:334
void SetKernelType(int kerneltype)
Definition: otbSVMModel.h:158
void SetSVMType(int svmtype)
Definition: otbSVMModel.h:141
struct svm_problem & GetProblem()
Definition: otbSVMModel.h:353
SamplesVectorType m_Samples
Definition: otbSVMModel.h:447
struct svm_node * NodeCacheType
Definition: otbSVMModel.h:82
void SetKernelFunctor(GenericKernelFunctorBase *pGenericKernelFunctor)
Definition: otbSVMModel.h:307
void SetEpsilon(double eps)
Definition: otbSVMModel.h:250
void DoProbabilityEstimates(bool prob)
Definition: otbSVMModel.h:289
int GetKernelType(void) const
Definition: otbSVMModel.h:165
itk::VariableLengthVector< double > DistancesVectorType
Definition: otbSVMModel.h:80
void SaveModel(const std::string &model_file_name) const
Definition: otbSVMModel.h:123
double * GetRho(void) const
Definition: otbSVMModel.h:321
struct svm_parameter & GetParameters()
Definition: otbSVMModel.h:111
int GetNumberOfSupportVectors(void) const
Definition: otbSVMModel.h:315
#define NULL
double GetNu(void) const
Definition: otbSVMModel.h:217
SVMModel Self
Definition: otbSVMModel.h:63
std::vector< SampleType > SamplesVectorType
Definition: otbSVMModel.h:74
bool m_ModelUpToDate
Definition: otbSVMModel.h:435
std::vector< struct svm_node * > CacheVectorType
Definition: otbSVMModel.h:76
void SetNu(double nu)
Definition: otbSVMModel.h:210
std::vector< ValueType > MeasurementType
Definition: otbSVMModel.h:72
std::pair< MeasurementType, LabelType > SampleType
Definition: otbSVMModel.h:73
void DoShrinking(bool s)
Definition: otbSVMModel.h:276
void SetP(double p)
Definition: otbSVMModel.h:263