OTB  10.0.0
Orfeo Toolbox
otbSVMMachineLearningModel.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2005-2024 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 otbSVMMachineLearningModel_h
22 #define otbSVMMachineLearningModel_h
23 
24 #include "otbRequiresOpenCVCheck.h"
25 #include "itkLightObject.h"
26 #include "itkFixedArray.h"
28 
29 #include "otbOpenCVUtils.h"
30 
31 namespace otb
32 {
41 template <class TInputValue, class TTargetValue>
42 class ITK_EXPORT SVMMachineLearningModel : public MachineLearningModel<TInputValue, TTargetValue>
43 {
44 public:
45 
49  typedef itk::SmartPointer<Self> Pointer;
50  typedef itk::SmartPointer<const Self> ConstPointer;
51 
52  typedef typename Superclass::InputValueType InputValueType;
53  typedef typename Superclass::InputSampleType InputSampleType;
54  typedef typename Superclass::InputListSampleType InputListSampleType;
55  typedef typename Superclass::TargetValueType TargetValueType;
57  typedef typename Superclass::TargetListSampleType TargetListSampleType;
58  typedef typename Superclass::ConfidenceValueType ConfidenceValueType;
59  typedef typename Superclass::ProbaSampleType ProbaSampleType;
61  itkNewMacro(Self);
64 
66  void Train() override;
67 
69  void Save(const std::string& filename, const std::string& name = "") override;
70 
72  void Load(const std::string& filename, const std::string& name = "") override;
73 
76 
78  bool CanReadFile(const std::string&) override;
79 
81  bool CanWriteFile(const std::string&) override;
83 
84  // Setters/Getters to SVM model
85  itkGetMacro(SVMType, int);
86  itkSetMacro(SVMType, int);
87 
88  itkGetMacro(KernelType, int);
89  itkSetMacro(KernelType, int);
90 
91  // CV_TERMCRIT_ITER or CV_TERMCRIT_EPS
92  itkGetMacro(TermCriteriaType, int);
93  itkSetMacro(TermCriteriaType, int);
94 
95  itkGetMacro(MaxIter, int);
96  itkSetMacro(MaxIter, int);
97 
98  itkGetMacro(Epsilon, double);
99  itkSetMacro(Epsilon, double);
100 
101  // for poly
102  itkGetMacro(Degree, double);
103  itkSetMacro(Degree, double);
104  itkGetMacro(OutputDegree, double);
105 
106  // for poly/rbf/sigmoid
107  itkGetMacro(Gamma, double);
108  itkSetMacro(Gamma, double);
109  itkGetMacro(OutputGamma, double);
110 
111  // for poly/sigmoid
112  itkGetMacro(Coef0, double);
113  itkSetMacro(Coef0, double);
114  itkGetMacro(OutputCoef0, double);
115 
116  // for CV_SVM_C_SVC, CV_SVM_EPS_SVR and CV_SVM_NU_SVR
117  itkGetMacro(C, double);
118  itkSetMacro(C, double);
119  itkGetMacro(OutputC, double);
120 
121  // for CV_SVM_NU_SVC, CV_SVM_ONE_CLASS, and CV_SVM_NU_SVR
122  itkGetMacro(Nu, double);
123  itkSetMacro(Nu, double);
124  itkGetMacro(OutputNu, double);
125 
126  // for CV_SVM_EPS_SVR
127  itkGetMacro(P, double);
128  itkSetMacro(P, double);
129  itkGetMacro(OutputP, double);
130 
131  itkGetMacro(ParameterOptimization, bool);
132  itkSetMacro(ParameterOptimization, bool);
133 
134 protected:
137 
139  ~SVMMachineLearningModel() override = default;
140 
142  TargetSampleType DoPredict(const InputSampleType& input, ConfidenceValueType* quality = nullptr, ProbaSampleType* proba = nullptr) const override;
143 
145  void PrintSelf(std::ostream& os, itk::Indent indent) const override;
146 
147 private:
148  SVMMachineLearningModel(const Self&) = delete;
149  void operator=(const Self&) = delete;
150  cv::Ptr<cv::ml::SVM> m_SVMModel;
153  double m_Degree;
154  double m_Gamma;
155  double m_Coef0;
156  double m_C;
157  double m_Nu;
158  double m_P;
161  double m_Epsilon;
163  // Output parameters
167  double m_OutputC;
168  double m_OutputNu;
169  double m_OutputP;
170 };
171 } // end namespace otb
172 
173 #ifndef OTB_MANUAL_INSTANTIATION
175 #endif
176 
177 #endif
MachineLearningModel is the base class for all classifier objects (SVM, KNN, Random Forests,...
MLMTargetTraits< TConfidenceValue >::ValueType ConfidenceValueType
itk::VariableLengthVector< double > ProbaSampleType
MLMSampleTraits< TInputValue >::SampleType InputSampleType
MLMTargetTraits< TTargetValue >::SampleType TargetSampleType
OpenCV implementation of SVM algorithm.
Superclass::InputSampleType InputSampleType
Superclass::ConfidenceValueType ConfidenceValueType
MachineLearningModel< TInputValue, TTargetValue > Superclass
SVMMachineLearningModel(const Self &)=delete
~SVMMachineLearningModel() override=default
Superclass::InputValueType InputValueType
Superclass::InputListSampleType InputListSampleType
Superclass::TargetValueType TargetValueType
Superclass::TargetSampleType TargetSampleType
itk::SmartPointer< const Self > ConstPointer
Superclass::ProbaSampleType ProbaSampleType
void operator=(const Self &)=delete
Superclass::TargetListSampleType TargetListSampleType
The "otb" namespace contains all Orfeo Toolbox (OTB) classes.