otbTrainLibSVM.txx 5.81 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
/*=========================================================================
 Program:   ORFEO Toolbox
 Language:  C++
 Date:      $Date$
 Version:   $Revision$


 Copyright (c) Centre National d'Etudes Spatiales. All rights reserved.
 See OTBCopyright.txt for details.


 This software is distributed WITHOUT ANY WARRANTY; without even
 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
 PURPOSE.  See the above copyright notices for more information.

 =========================================================================*/
17
18
#ifndef otbTrainLibSVM_txx
#define otbTrainLibSVM_txx
19
#include "otbLearningApplicationBase.h"
20
21
22
23
24

namespace otb
{
namespace Wrapper
{
25
26
27
28
29

  template <class TInputValue, class TOutputValue>
  void
  LearningApplicationBase<TInputValue,TOutputValue>
  ::InitLibSVMParams()
30
31
  {
    AddChoice("classifier.libsvm", "LibSVM classifier");
32
    SetParameterDescription("classifier.libsvm", "This group of parameters allows setting SVM classifier parameters.");
33
34
35
36
37
    AddParameter(ParameterType_Choice, "classifier.libsvm.k", "SVM Kernel Type");
    AddChoice("classifier.libsvm.k.linear", "Linear");
    AddChoice("classifier.libsvm.k.rbf", "Gaussian radial basis function");
    AddChoice("classifier.libsvm.k.poly", "Polynomial");
    AddChoice("classifier.libsvm.k.sigmoid", "Sigmoid");
38
    SetParameterString("classifier.libsvm.k", "linear", false);
39
    SetParameterDescription("classifier.libsvm.k", "SVM Kernel Type.");
40
41
42
43
44
45
    AddParameter(ParameterType_Choice, "classifier.libsvm.m", "SVM Model Type");
    SetParameterDescription("classifier.libsvm.m", "Type of SVM formulation.");
    if (this->m_RegressionFlag)
      {
      AddChoice("classifier.libsvm.m.epssvr", "Epsilon Support Vector Regression");
      AddChoice("classifier.libsvm.m.nusvr", "Nu Support Vector Regression");
46
      SetParameterString("classifier.libsvm.m", "epssvr", false);
47
48
49
50
51
52
      }
    else
      {
      AddChoice("classifier.libsvm.m.csvc", "C support vector classification");
      AddChoice("classifier.libsvm.m.nusvc", "Nu support vector classification");
      AddChoice("classifier.libsvm.m.oneclass", "Distribution estimation (One Class SVM)");
53
      SetParameterString("classifier.libsvm.m", "csvc", false);
54
      }
55
    AddParameter(ParameterType_Float, "classifier.libsvm.c", "Cost parameter C");
56
    SetParameterFloat("classifier.libsvm.c",1.0, false);
57
58
59
    SetParameterDescription(
        "classifier.libsvm.c",
        "SVM models have a cost parameter C (1 by default) to control the trade-off between training errors and forcing rigid margins.");
60
    AddParameter(ParameterType_Empty, "classifier.libsvm.opt", "Parameters optimization");
61
    MandatoryOff("classifier.libsvm.opt");
62
    SetParameterDescription("classifier.libsvm.opt", "SVM parameters optimization flag.");
63
64
65
    AddParameter(ParameterType_Empty, "classifier.libsvm.prob", "Probability estimation");
    MandatoryOff("classifier.libsvm.prob");
    SetParameterDescription("classifier.libsvm.prob", "Probability estimation flag.");
66
67
68
69

    if (this->m_RegressionFlag)
      {
      AddParameter(ParameterType_Float, "classifier.libsvm.eps", "Epsilon");
70
      SetParameterFloat("classifier.libsvm.eps",1e-3, false);
71
      AddParameter(ParameterType_Float, "classifier.libsvm.nu", "Nu");
72
      SetParameterFloat("classifier.libsvm.nu",0.5, false);
73
      }
74
75
  }

76
77
78
  template <class TInputValue, class TOutputValue>
  void
  LearningApplicationBase<TInputValue,TOutputValue>
79
80
  ::TrainLibSVM(typename ListSampleType::Pointer trainingListSample,
                typename TargetListSampleType::Pointer trainingLabeledListSample,
81
                std::string modelPath)
82
  {
83
    typename LibSVMType::Pointer libSVMClassifier = LibSVMType::New();
84
    libSVMClassifier->SetRegressionMode(this->m_RegressionFlag);
85
86
87
88
89
90
91
92
    libSVMClassifier->SetInputListSample(trainingListSample);
    libSVMClassifier->SetTargetListSample(trainingLabeledListSample);
    //SVM Option
    //TODO : Add other options ?
    if (IsParameterEnabled("classifier.libsvm.opt"))
      {
      libSVMClassifier->SetParameterOptimization(true);
      }
93
94
95
96
    if (IsParameterEnabled("classifier.libsvm.prob"))
      {
      libSVMClassifier->SetDoProbabilityEstimates(true);
      }
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
    libSVMClassifier->SetC(GetParameterFloat("classifier.libsvm.c"));

    switch (GetParameterInt("classifier.libsvm.k"))
      {
      case 0: // LINEAR
        libSVMClassifier->SetKernelType(LINEAR);
        break;
      case 1: // RBF
        libSVMClassifier->SetKernelType(RBF);
        break;
      case 2: // POLY
        libSVMClassifier->SetKernelType(POLY);
        break;
      case 3: // SIGMOID
        libSVMClassifier->SetKernelType(SIGMOID);
        break;
      default: // DEFAULT = LINEAR
        libSVMClassifier->SetKernelType(LINEAR);
        break;
      }
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
    if (this->m_RegressionFlag)
      {
      switch (GetParameterInt("classifier.libsvm.m"))
        {
        case 0: // EPSILON_SVR
          libSVMClassifier->SetSVMType(EPSILON_SVR);
          break;
        case 1: // NU_SVR
          libSVMClassifier->SetSVMType(NU_SVR);
          break;
        default:
          libSVMClassifier->SetSVMType(EPSILON_SVR);
          break;
        }
      libSVMClassifier->SetEpsilon(GetParameterFloat("classifier.libsvm.eps"));
      libSVMClassifier->SetNu(GetParameterFloat("classifier.libsvm.nu"));
      }
    else
      {
      switch (GetParameterInt("classifier.libsvm.m"))
        {
        case 0: // C_SVC
          libSVMClassifier->SetSVMType(C_SVC);
          break;
        case 1: // NU_SVC
          libSVMClassifier->SetSVMType(NU_SVC);
          break;
        case 2: // ONE_CLASS
          libSVMClassifier->SetSVMType(ONE_CLASS);
          break;
        default:
          libSVMClassifier->SetSVMType(C_SVC);
          break;
        }
      }

153
    libSVMClassifier->Train();
154
    libSVMClassifier->Save(modelPath);
155
  }
156

157
158
} //end namespace wrapper
} //end namespace otb
159
160

#endif