OTB  9.0.0
Orfeo Toolbox
otbTrainLibSVM.hxx
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 otbTrainLibSVM_hxx
22 #define otbTrainLibSVM_hxx
25 
26 namespace otb
27 {
28 namespace Wrapper
29 {
30 
31 template <class TInputValue, class TOutputValue>
32 void LearningApplicationBase<TInputValue, TOutputValue>::InitLibSVMParams()
33 {
34  AddChoice("classifier.libsvm", "LibSVM classifier");
35  SetParameterDescription("classifier.libsvm", "This group of parameters allows setting SVM classifier parameters.");
36  AddParameter(ParameterType_Choice, "classifier.libsvm.k", "SVM Kernel Type");
37  AddChoice("classifier.libsvm.k.linear", "Linear");
38  SetParameterDescription("classifier.libsvm.k.linear", "Linear Kernel, no mapping is done, this is the fastest option.");
39 
40  AddChoice("classifier.libsvm.k.rbf", "Gaussian radial basis function");
41  SetParameterDescription("classifier.libsvm.k.rbf",
42  "This kernel is a good choice in most of the case. It is "
43  "an exponential function of the euclidean distance between "
44  "the vectors.");
45 
46  AddChoice("classifier.libsvm.k.poly", "Polynomial");
47  SetParameterDescription("classifier.libsvm.k.poly", "Polynomial Kernel, the mapping is a polynomial function.");
48 
49  AddChoice("classifier.libsvm.k.sigmoid", "Sigmoid");
50  SetParameterDescription("classifier.libsvm.k.sigmoid", "The kernel is a hyperbolic tangente function of the vectors.");
51 
52  SetParameterString("classifier.libsvm.k", "linear");
53  SetParameterDescription("classifier.libsvm.k", "SVM Kernel Type.");
54  AddParameter(ParameterType_Choice, "classifier.libsvm.m", "SVM Model Type");
55  SetParameterDescription("classifier.libsvm.m", "Type of SVM formulation.");
56  if (this->m_RegressionFlag)
57  {
58  AddChoice("classifier.libsvm.m.epssvr", "Epsilon Support Vector Regression");
59  SetParameterDescription("classifier.libsvm.m.epssvr",
60  "The distance between feature vectors from the training set and the "
61  "fitting hyper-plane must be less than Epsilon. For outliers the penalty "
62  "multiplier C is used ");
63 
64  AddChoice("classifier.libsvm.m.nusvr", "Nu Support Vector Regression");
65  SetParameterString("classifier.libsvm.m", "epssvr");
66  SetParameterDescription("classifier.libsvm.m.nusvr",
67  "Same as the epsilon regression except that this time the bounded "
68  "parameter nu is used instead of epsilon");
69  }
70  else
71  {
72  AddChoice("classifier.libsvm.m.csvc", "C support vector classification");
73  SetParameterDescription("classifier.libsvm.m.csvc",
74  "This formulation allows imperfect separation of classes. The penalty "
75  "is set through the cost parameter C.");
76 
77  AddChoice("classifier.libsvm.m.nusvc", "Nu support vector classification");
78  SetParameterDescription("classifier.libsvm.m.nusvc",
79  "This formulation allows imperfect separation of classes. The penalty "
80  "is set through the cost parameter Nu. As compared to C, Nu is harder "
81  "to optimize, and may not be as fast.");
82 
83  AddChoice("classifier.libsvm.m.oneclass", "Distribution estimation (One Class SVM)");
84  SetParameterDescription("classifier.libsvm.m.oneclass",
85  "All the training data are from the same class, SVM builds a boundary "
86  "that separates the class from the rest of the feature space.");
87  SetParameterString("classifier.libsvm.m", "csvc");
88  }
89 
90  AddParameter(ParameterType_Float, "classifier.libsvm.c", "Cost parameter C");
91  SetParameterFloat("classifier.libsvm.c", 1.0);
92  SetParameterDescription("classifier.libsvm.c",
93  "SVM models have a cost parameter C (1 by default) to control the "
94  "trade-off between training errors and forcing rigid margins.");
95 
96  AddParameter(ParameterType_Float, "classifier.libsvm.gamma", "Gamma parameter");
97  SetParameterFloat("classifier.libsvm.gamma", 1.0);
98  SetMinimumParameterFloatValue("classifier.libsvm.gamma", 0.0);
99  SetParameterDescription("classifier.libsvm.gamma", "Set gamma parameter in poly/rbf/sigmoid kernel function");
100 
101  AddParameter(ParameterType_Float, "classifier.libsvm.coef0", "Coefficient parameter");
102  SetParameterFloat("classifier.libsvm.coef0", 0.0);
103  SetParameterDescription("classifier.libsvm.coef0", "Set coef0 parameter in poly/sigmoid kernel function");
104 
105  AddParameter(ParameterType_Int, "classifier.libsvm.degree", "Degree parameter");
106  SetParameterInt("classifier.libsvm.degree", 3);
107  SetMinimumParameterIntValue("classifier.libsvm.degree", 1);
108  SetParameterDescription("classifier.libsvm.degree", "Set polynomial degree in poly kernel function");
109 
110  AddParameter(ParameterType_Float, "classifier.libsvm.nu", "Cost parameter Nu");
111  SetParameterFloat("classifier.libsvm.nu", 0.5);
112  SetParameterDescription("classifier.libsvm.nu",
113  "Cost parameter Nu, in the range 0..1, the larger the value, "
114  "the smoother the decision.");
115 
116  // It seems that it miss a nu parameter for the nu-SVM use.
117  AddParameter(ParameterType_Bool, "classifier.libsvm.opt", "Parameters optimization");
118  SetParameterDescription("classifier.libsvm.opt", "SVM parameters optimization flag.");
119 
120  AddParameter(ParameterType_Bool, "classifier.libsvm.prob", "Probability estimation");
121  SetParameterDescription("classifier.libsvm.prob", "Probability estimation flag.");
122 
123  if (this->m_RegressionFlag)
124  {
125  AddParameter(ParameterType_Float, "classifier.libsvm.eps", "Epsilon");
126  SetParameterFloat("classifier.libsvm.eps", 1e-3);
127  SetParameterDescription("classifier.libsvm.eps",
128  "The distance between feature vectors from the training set and "
129  "the fitting hyper-plane must be less than Epsilon. For outliers"
130  "the penalty multiplier is set by C.");
131  }
132 }
133 
134 template <class TInputValue, class TOutputValue>
135 void LearningApplicationBase<TInputValue, TOutputValue>::TrainLibSVM(typename ListSampleType::Pointer trainingListSample,
136  typename TargetListSampleType::Pointer trainingLabeledListSample, std::string modelPath)
137 {
139  typename LibSVMType::Pointer libSVMClassifier = LibSVMType::New();
140  libSVMClassifier->SetRegressionMode(this->m_RegressionFlag);
141  libSVMClassifier->SetInputListSample(trainingListSample);
142  libSVMClassifier->SetTargetListSample(trainingLabeledListSample);
143  // SVM Option
144  // TODO : Add other options ?
145  libSVMClassifier->SetParameterOptimization(GetParameterInt("classifier.libsvm.opt"));
146  libSVMClassifier->SetDoProbabilityEstimates(GetParameterInt("classifier.libsvm.prob"));
147  libSVMClassifier->SetNu(GetParameterFloat("classifier.libsvm.nu"));
148  libSVMClassifier->SetC(GetParameterFloat("classifier.libsvm.c"));
149 
150  switch (GetParameterInt("classifier.libsvm.k"))
151  {
152  case 0: // LINEAR
153  libSVMClassifier->SetKernelType(LINEAR);
154  break;
155  case 1: // RBF
156  libSVMClassifier->SetKernelType(RBF);
157  libSVMClassifier->SetKernelGamma(GetParameterFloat("classifier.libsvm.gamma"));
158  break;
159  case 2: // POLY
160  libSVMClassifier->SetKernelType(POLY);
161  libSVMClassifier->SetKernelGamma(GetParameterFloat("classifier.libsvm.gamma"));
162  libSVMClassifier->SetKernelCoef0(GetParameterFloat("classifier.libsvm.coef0"));
163  libSVMClassifier->SetPolynomialKernelDegree(GetParameterInt("classifier.libsvm.degree"));
164  break;
165  case 3: // SIGMOID
166  libSVMClassifier->SetKernelType(SIGMOID);
167  libSVMClassifier->SetKernelGamma(GetParameterFloat("classifier.libsvm.gamma"));
168  libSVMClassifier->SetKernelCoef0(GetParameterFloat("classifier.libsvm.coef0"));
169  break;
170  default: // DEFAULT = LINEAR
171  libSVMClassifier->SetKernelType(LINEAR);
172  break;
173  }
174  if (this->m_RegressionFlag)
175  {
176  switch (GetParameterInt("classifier.libsvm.m"))
177  {
178  case 0: // EPSILON_SVR
179  libSVMClassifier->SetSVMType(EPSILON_SVR);
180  break;
181  case 1: // NU_SVR
182  libSVMClassifier->SetSVMType(NU_SVR);
183  break;
184  default:
185  libSVMClassifier->SetSVMType(EPSILON_SVR);
186  break;
187  }
188  libSVMClassifier->SetEpsilon(GetParameterFloat("classifier.libsvm.eps"));
189  }
190  else
191  {
192  switch (GetParameterInt("classifier.libsvm.m"))
193  {
194  case 0: // C_SVC
195  libSVMClassifier->SetSVMType(C_SVC);
196  break;
197  case 1: // NU_SVC
198  libSVMClassifier->SetSVMType(NU_SVC);
199  break;
200  case 2: // ONE_CLASS
201  libSVMClassifier->SetSVMType(ONE_CLASS);
202  break;
203  default:
204  libSVMClassifier->SetSVMType(C_SVC);
205  break;
206  }
207  }
208 
209 
210  libSVMClassifier->Train();
211  libSVMClassifier->Save(modelPath);
212 }
213 
214 } // end namespace wrapper
215 } // end namespace otb
216 
217 #endif
otbLibSVMMachineLearningModel.h
otb::Wrapper::ParameterType_Bool
@ ParameterType_Bool
Definition: otbWrapperTypes.h:60
otb::Wrapper::ParameterType_Choice
@ ParameterType_Choice
Definition: otbWrapperTypes.h:47
otb
The "otb" namespace contains all Orfeo Toolbox (OTB) classes.
Definition: otbJoinContainer.h:32
otbLearningApplicationBase.h
otb::LibSVMMachineLearningModel
Definition: otbLibSVMMachineLearningModel.h:33
otb::Wrapper::ParameterType_Int
@ ParameterType_Int
Definition: otbWrapperTypes.h:38
otb::Wrapper::ParameterType_Float
@ ParameterType_Float
Definition: otbWrapperTypes.h:39