OTB  9.0.0
Orfeo Toolbox
otbLibSVMMachineLearningModel.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2005-2022 Centre National d'Etudes Spatiales (CNES)
3  *
4  * This file is part of Orfeo Toolbox
5  *
6  * https://www.orfeo-toolbox.org/
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  */
20 
21 #ifndef otbLibSVMMachineLearningModel_h
22 #define otbLibSVMMachineLearningModel_h
23 
24 #include "itkLightObject.h"
25 #include "itkFixedArray.h"
27 
28 #include "svm.h"
29 
30 namespace otb
31 {
32 template <class TInputValue, class TTargetValue>
33 class ITK_EXPORT LibSVMMachineLearningModel : public MachineLearningModel<TInputValue, TTargetValue>
34 {
35 public:
39  typedef itk::SmartPointer<Self> Pointer;
40  typedef itk::SmartPointer<const Self> ConstPointer;
41 
43  typedef typename Superclass::InputSampleType InputSampleType;
48  typedef typename Superclass::ConfidenceValueType ConfidenceValueType;
49  typedef typename Superclass::ProbaSampleType ProbaSampleType;
59  typedef enum { CM_INDEX, CM_PROBA, CM_HYPER } ConfidenceMode;
60 
62  itkNewMacro(Self);
65 
67  void Train() override;
68 
70  void Save(const std::string& filename, const std::string& name = "") override;
71 
73  void Load(const std::string& filename, const std::string& name = "") override;
74 
77 
79  bool CanReadFile(const std::string&) override;
80 
82  bool CanWriteFile(const std::string&) override;
84 
85 #define otbSetSVMParameterMacro(name, alias, type) \
86  void Set##name(const type _arg) \
87  { \
88  itkDebugMacro("setting " #name " to " << _arg); \
89  if (this->m_Parameters.alias != _arg) \
90  { \
91  this->m_Parameters.alias = _arg; \
92  this->Modified(); \
93  } \
94  }
95 
97  otbSetSVMParameterMacro(SVMType, svm_type, int);
98 
100  int GetSVMType(void) const
101  {
102  return m_Parameters.svm_type;
103  }
104 
110  otbSetSVMParameterMacro(KernelType, kernel_type, int);
111 
113  int GetKernelType(void) const
114  {
115  return m_Parameters.kernel_type;
116  }
117 
119  otbSetSVMParameterMacro(PolynomialKernelDegree, degree, int);
120 
123  {
124  return m_Parameters.degree;
125  }
126 
128  otbSetSVMParameterMacro(KernelGamma, gamma, double);
129 
131  double GetKernelGamma(void) const
132  {
133  return m_Parameters.gamma;
134  }
135 
137  otbSetSVMParameterMacro(KernelCoef0, coef0, double);
138 
140  double GetKernelCoef0(void) const
141  {
142  return m_Parameters.coef0;
143  }
144 
146  otbSetSVMParameterMacro(C, C, double);
147 
149  double GetC(void) const
150  {
151  return m_Parameters.C;
152  }
153 
154  itkSetMacro(ParameterOptimization, bool);
155  itkGetMacro(ParameterOptimization, bool);
156 
159  {
160  m_Parameters.probability = static_cast<int>(prob);
161  }
162 
164  bool GetDoProbabilityEstimates(void) const
165  {
166  return static_cast<bool>(m_Parameters.probability);
167  }
168 
170  bool HasProbabilities(void) const;
171 
173  otbSetSVMParameterMacro(Epsilon, eps, double);
174 
176  double GetEpsilon(void) const
177  {
178  return m_Parameters.eps;
179  }
180 
182  otbSetSVMParameterMacro(P, p, double);
183 
185  double GetP(void) const
186  {
187  return m_Parameters.p;
188  }
189 
191  otbSetSVMParameterMacro(Nu, nu, double);
192 
194  double GetNu(void) const
195  {
196  return m_Parameters.nu;
197  }
198 
199 #undef otbSetSVMParameterMacro
200 
202  void DoShrinking(bool s)
203  {
204  m_Parameters.shrinking = static_cast<int>(s);
205  this->Modified();
206  }
208 
210  bool GetDoShrinking(void) const
211  {
212  return static_cast<bool>(m_Parameters.shrinking);
213  }
214 
216  void SetCacheSize(int cSize)
217  {
218  m_Parameters.cache_size = static_cast<double>(cSize);
219  this->Modified();
220  }
222 
224  int GetCacheSize(void) const
225  {
226  return static_cast<int>(m_Parameters.cache_size);
227  }
228 
229  itkSetMacro(CVFolders, unsigned int);
230  itkGetMacro(CVFolders, unsigned int);
231 
232  itkGetMacro(InitialCrossValidationAccuracy, double);
233 
234  itkGetMacro(FinalCrossValidationAccuracy, double);
235 
236  itkSetMacro(CoarseOptimizationNumberOfSteps, unsigned int);
237  itkGetMacro(CoarseOptimizationNumberOfSteps, unsigned int);
238 
239  itkSetMacro(FineOptimizationNumberOfSteps, unsigned int);
240  itkGetMacro(FineOptimizationNumberOfSteps, unsigned int);
241 
242  void SetConfidenceMode(unsigned int mode)
243  {
244  if (m_ConfidenceMode != static_cast<ConfidenceMode>(mode))
245  {
246  m_ConfidenceMode = static_cast<ConfidenceMode>(mode);
247  this->m_ConfidenceIndex = this->HasProbabilities();
248  this->Modified();
249  }
250  }
251  itkGetMacro(ConfidenceMode, unsigned int);
252 
253  unsigned int GetNumberOfKernelParameters();
254 
255  double CrossValidation(void);
256 
258  unsigned int GetNumberOfSupportVectors(void) const
259  {
260  if (m_Model)
261  return m_Model->l;
262  return 0;
263  }
265 
266  unsigned int GetNumberOfClasses(void) const
267  {
268  if (m_Model)
269  return m_Model->nr_class;
270  return 0;
271  }
272 
273 protected:
276 
278  ~LibSVMMachineLearningModel() override;
279 
281  TargetSampleType DoPredict(const InputSampleType& input, ConfidenceValueType* quality = nullptr, ProbaSampleType* proba = nullptr) const override;
282 
284  void PrintSelf(std::ostream& os, itk::Indent indent) const override;
285 
286 private:
287  LibSVMMachineLearningModel(const Self&) = delete;
288  void operator=(const Self&) = delete;
289 
290  void BuildProblem(void);
291 
292  void ConsistencyCheck(void);
293 
294  void DeleteProblem(void);
295 
296  void DeleteModel(void);
297 
298  void OptimizeParameters(void);
299 
301  struct svm_model* m_Model;
302 
304  struct svm_problem m_Problem;
305 
307  struct svm_parameter m_Parameters;
308 
311 
313  unsigned int m_CVFolders;
314 
317 
320 
323 
326 
329 
331  std::vector<double> m_TmpTarget;
332 };
333 } // end namespace otb
334 
335 #ifndef OTB_MANUAL_INSTANTIATION
337 #endif
338 
339 #endif
otb::LibSVMMachineLearningModel::GetSVMType
int GetSVMType(void) const
Definition: otbLibSVMMachineLearningModel.h:100
otb::LibSVMMachineLearningModel::m_CVFolders
unsigned int m_CVFolders
Definition: otbLibSVMMachineLearningModel.h:313
otb::MachineLearningModel< TInputValue, TTargetValue >::TargetListSampleType
itk::Statistics::ListSample< TargetSampleType > TargetListSampleType
Definition: otbMachineLearningModel.h:92
otb::LibSVMMachineLearningModel::GetDoProbabilityEstimates
bool GetDoProbabilityEstimates(void) const
Definition: otbLibSVMMachineLearningModel.h:164
otb::LibSVMMachineLearningModel::ConstPointer
itk::SmartPointer< const Self > ConstPointer
Definition: otbLibSVMMachineLearningModel.h:40
otb::LibSVMMachineLearningModel::GetKernelType
int GetKernelType(void) const
Definition: otbLibSVMMachineLearningModel.h:113
otb::LibSVMMachineLearningModel::DoShrinking
void DoShrinking(bool s)
Definition: otbLibSVMMachineLearningModel.h:202
otb::LibSVMMachineLearningModel::ProbaSampleType
Superclass::ProbaSampleType ProbaSampleType
Definition: otbLibSVMMachineLearningModel.h:49
otb::LibSVMMachineLearningModel::GetPolynomialKernelDegree
int GetPolynomialKernelDegree(void) const
Definition: otbLibSVMMachineLearningModel.h:122
otb::MachineLearningModel< TInputValue, TTargetValue >::InputListSampleType
itk::Statistics::ListSample< InputSampleType > InputListSampleType
Definition: otbMachineLearningModel.h:85
otb::LibSVMMachineLearningModel::m_Model
struct svm_model * m_Model
Definition: otbLibSVMMachineLearningModel.h:301
otb::MachineLearningModel< TInputValue, TTargetValue >::InputValueType
MLMSampleTraits< TInputValue >::ValueType InputValueType
Definition: otbMachineLearningModel.h:83
otb::LibSVMMachineLearningModel::m_CoarseOptimizationNumberOfSteps
unsigned int m_CoarseOptimizationNumberOfSteps
Definition: otbLibSVMMachineLearningModel.h:322
otb::LibSVMMachineLearningModel::InputValueType
Superclass::InputValueType InputValueType
Definition: otbLibSVMMachineLearningModel.h:42
otb::LibSVMMachineLearningModel::Superclass
MachineLearningModel< TInputValue, TTargetValue > Superclass
Definition: otbLibSVMMachineLearningModel.h:38
otb
The "otb" namespace contains all Orfeo Toolbox (OTB) classes.
Definition: otbJoinContainer.h:32
otb::LibSVMMachineLearningModel::Self
LibSVMMachineLearningModel Self
Definition: otbLibSVMMachineLearningModel.h:37
otb::LibSVMMachineLearningModel::GetDoShrinking
bool GetDoShrinking(void) const
Definition: otbLibSVMMachineLearningModel.h:210
otb::LibSVMMachineLearningModel::GetCacheSize
int GetCacheSize(void) const
Definition: otbLibSVMMachineLearningModel.h:224
otb::LibSVMMachineLearningModel
Definition: otbLibSVMMachineLearningModel.h:33
otb::LibSVMMachineLearningModel::TargetSampleType
Superclass::TargetSampleType TargetSampleType
Definition: otbLibSVMMachineLearningModel.h:46
otb::LibSVMMachineLearningModel::m_FinalCrossValidationAccuracy
double m_FinalCrossValidationAccuracy
Definition: otbLibSVMMachineLearningModel.h:319
otb::LibSVMMachineLearningModel::SetCacheSize
void SetCacheSize(int cSize)
Definition: otbLibSVMMachineLearningModel.h:216
otb::LibSVMMachineLearningModel::m_FineOptimizationNumberOfSteps
unsigned int m_FineOptimizationNumberOfSteps
Definition: otbLibSVMMachineLearningModel.h:325
otb::LibSVMMachineLearningModel::ConfidenceValueType
Superclass::ConfidenceValueType ConfidenceValueType
Definition: otbLibSVMMachineLearningModel.h:48
otb::LibSVMMachineLearningModel::ConfidenceMode
ConfidenceMode
Definition: otbLibSVMMachineLearningModel.h:59
otb::LibSVMMachineLearningModel::GetP
double GetP(void) const
Definition: otbLibSVMMachineLearningModel.h:185
otbSetSVMParameterMacro
#define otbSetSVMParameterMacro(name, alias, type)
Definition: otbLibSVMMachineLearningModel.h:85
otb::LibSVMMachineLearningModel::InputListSampleType
Superclass::InputListSampleType InputListSampleType
Definition: otbLibSVMMachineLearningModel.h:44
otbLibSVMMachineLearningModel.hxx
otb::LibSVMMachineLearningModel::TargetValueType
Superclass::TargetValueType TargetValueType
Definition: otbLibSVMMachineLearningModel.h:45
otb::LibSVMMachineLearningModel::GetNumberOfClasses
unsigned int GetNumberOfClasses(void) const
Definition: otbLibSVMMachineLearningModel.h:266
otbMachineLearningModel.h
otb::LibSVMMachineLearningModel::GetNu
double GetNu(void) const
Definition: otbLibSVMMachineLearningModel.h:194
otb::LibSVMMachineLearningModel::SetDoProbabilityEstimates
void SetDoProbabilityEstimates(bool prob)
Definition: otbLibSVMMachineLearningModel.h:158
otb::LibSVMMachineLearningModel::GetNumberOfSupportVectors
unsigned int GetNumberOfSupportVectors(void) const
Definition: otbLibSVMMachineLearningModel.h:258
otb::LibSVMMachineLearningModel::m_ParameterOptimization
bool m_ParameterOptimization
Definition: otbLibSVMMachineLearningModel.h:310
otb::LibSVMMachineLearningModel::m_ConfidenceMode
ConfidenceMode m_ConfidenceMode
Definition: otbLibSVMMachineLearningModel.h:328
otb::LibSVMMachineLearningModel::m_InitialCrossValidationAccuracy
double m_InitialCrossValidationAccuracy
Definition: otbLibSVMMachineLearningModel.h:316
otb::LibSVMMachineLearningModel::InputSampleType
Superclass::InputSampleType InputSampleType
Definition: otbLibSVMMachineLearningModel.h:43
otb::MachineLearningModel
MachineLearningModel is the base class for all classifier objects (SVM, KNN, Random Forests,...
Definition: otbMachineLearningModel.h:70
otb::LibSVMMachineLearningModel::GetKernelGamma
double GetKernelGamma(void) const
Definition: otbLibSVMMachineLearningModel.h:131
otb::MachineLearningModel< TInputValue, TTargetValue >::TargetValueType
MLMTargetTraits< TTargetValue >::ValueType TargetValueType
Definition: otbMachineLearningModel.h:90
otb::LibSVMMachineLearningModel::GetKernelCoef0
double GetKernelCoef0(void) const
Definition: otbLibSVMMachineLearningModel.h:140
otb::LibSVMMachineLearningModel::Pointer
itk::SmartPointer< Self > Pointer
Definition: otbLibSVMMachineLearningModel.h:39
otb::LibSVMMachineLearningModel::GetEpsilon
double GetEpsilon(void) const
Definition: otbLibSVMMachineLearningModel.h:176
otb::LibSVMMachineLearningModel::m_TmpTarget
std::vector< double > m_TmpTarget
Definition: otbLibSVMMachineLearningModel.h:331
otb::SVMMachineLearningModel
OpenCV implementation of SVM algorithm.
Definition: otbSVMMachineLearningModel.h:42
otb::MachineLearningModel::TargetSampleType
MLMTargetTraits< TTargetValue >::SampleType TargetSampleType
Definition: otbMachineLearningModel.h:91
otb::LibSVMMachineLearningModel::GetC
double GetC(void) const
Definition: otbLibSVMMachineLearningModel.h:149
otb::LibSVMMachineLearningModel::TargetListSampleType
Superclass::TargetListSampleType TargetListSampleType
Definition: otbLibSVMMachineLearningModel.h:47
otb::LibSVMMachineLearningModel::SetConfidenceMode
void SetConfidenceMode(unsigned int mode)
Definition: otbLibSVMMachineLearningModel.h:242