otbTrainNeuralNetwork.txx 9.36 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 otbTrainNeuralNetwork_txx
#define otbTrainNeuralNetwork_txx
19
#include <boost/lexical_cast.hpp>
20
#include "otbLearningApplicationBase.h"
21
22
23
24
25

namespace otb
{
namespace Wrapper
{
26
27
28
29
30

template <class TInputValue, class TOutputValue>
void
LearningApplicationBase<TInputValue,TOutputValue>
::InitNeuralNetworkParams()
31
32
33
{
  AddChoice("classifier.ann", "Artificial Neural Network classifier");
  SetParameterDescription("classifier.ann",
34
                          "This group of parameters allows setting Artificial Neural Network classifier parameters. "
35
                          "See complete documentation here \\url{http://docs.opencv.org/modules/ml/doc/neural_networks.html}.");
36
37
38

  //TrainMethod
  AddParameter(ParameterType_Choice, "classifier.ann.t", "Train Method Type");
39
40
  AddChoice("classifier.ann.t.reg", "RPROP algorithm");
  AddChoice("classifier.ann.t.back", "Back-propagation algorithm");
41
  SetParameterString("classifier.ann.t", "reg", false);
42
  SetParameterDescription("classifier.ann.t", "Type of training method for the multilayer perceptron (MLP) neural network.");
43
44
45
46
47

  //LayerSizes
  //There is no ParameterType_IntList, so i use a ParameterType_StringList and convert it.
  /*std::vector<std::string> layerSizes;
   layerSizes.push_back("100");
OTB Bot's avatar
STYLE    
OTB Bot committed
48
   layerSizes.push_back("100"); */
49
50
51
  AddParameter(ParameterType_StringList, "classifier.ann.sizes", "Number of neurons in each intermediate layer");
  //SetParameterStringList("classifier.ann.sizes", layerSizes);
  SetParameterDescription("classifier.ann.sizes",
52
                          "The number of neurons in each intermediate layer (excluding input and output layers).");
53
54
55

  //ActivateFunction
  AddParameter(ParameterType_Choice, "classifier.ann.f", "Neuron activation function type");
56
  AddChoice("classifier.ann.f.ident", "Identity function");
57
  AddChoice("classifier.ann.f.sig", "Symmetrical Sigmoid function");
58
  AddChoice("classifier.ann.f.gau", "Gaussian function (Not completely supported)");
59
  SetParameterString("classifier.ann.f", "sig", false);
60
61
62
  SetParameterDescription("classifier.ann.f", "Neuron activation function.");

  //Alpha
63
  AddParameter(ParameterType_Float, "classifier.ann.a", "Alpha parameter of the activation function");
64
  SetParameterFloat("classifier.ann.a",1., false);
65
  SetParameterDescription("classifier.ann.a",
66
                          "Alpha parameter of the activation function (used only with sigmoid and gaussian functions).");
67
68

  //Beta
69
  AddParameter(ParameterType_Float, "classifier.ann.b", "Beta parameter of the activation function");
70
  SetParameterFloat("classifier.ann.b",1., false);
71
  SetParameterDescription("classifier.ann.b",
72
                          "Beta parameter of the activation function (used only with sigmoid and gaussian functions).");
73
74
75

  //BackPropDWScale
  AddParameter(ParameterType_Float, "classifier.ann.bpdw",
76
               "Strength of the weight gradient term in the BACKPROP method");
77
  SetParameterFloat("classifier.ann.bpdw",0.1, false);
78
79
  SetParameterDescription(
      "classifier.ann.bpdw",
80
      "Strength of the weight gradient term in the BACKPROP method. The recommended value is about 0.1.");
81
82
83

  //BackPropMomentScale
  AddParameter(ParameterType_Float, "classifier.ann.bpms",
84
               "Strength of the momentum term (the difference between weights on the 2 previous iterations)");
85
  SetParameterFloat("classifier.ann.bpms",0.1, false);
86
87
88
89
  SetParameterDescription(
      "classifier.ann.bpms",
      "Strength of the momentum term (the difference between weights on the 2 previous iterations). "
      "This parameter provides some inertia to smooth the random fluctuations of the weights. "
90
      "It can vary from 0 (the feature is disabled) to 1 and beyond. The value 0.1 or so is good enough.");
91
92
93

  //RegPropDW0
  AddParameter(ParameterType_Float, "classifier.ann.rdw",
94
               "Initial value Delta_0 of update-values Delta_{ij} in RPROP method");
95
  SetParameterFloat("classifier.ann.rdw",0.1, false);
96
  SetParameterDescription("classifier.ann.rdw", "Initial value Delta_0 of update-values Delta_{ij} in RPROP method (default = 0.1).");
97
98

  //RegPropDWMin
99
  AddParameter(ParameterType_Float, "classifier.ann.rdwm", "Update-values lower limit Delta_{min} in RPROP method");
100
  SetParameterFloat("classifier.ann.rdwm",1e-7, false);
101
102
  SetParameterDescription(
      "classifier.ann.rdwm",
103
      "Update-values lower limit Delta_{min} in RPROP method. It must be positive (default = 1e-7).");
104
105
106
107

  //TermCriteriaType
  AddParameter(ParameterType_Choice, "classifier.ann.term", "Termination criteria");
  AddChoice("classifier.ann.term.iter", "Maximum number of iterations");
108
  AddChoice("classifier.ann.term.eps", "Epsilon");
109
  AddChoice("classifier.ann.term.all", "Max. iterations + Epsilon");
110
  SetParameterString("classifier.ann.term", "all", false);
111
112
113
114
  SetParameterDescription("classifier.ann.term", "Termination criteria.");

  //Epsilon
  AddParameter(ParameterType_Float, "classifier.ann.eps", "Epsilon value used in the Termination criteria");
115
  SetParameterFloat("classifier.ann.eps",0.01, false);
116
  SetParameterDescription("classifier.ann.eps", "Epsilon value used in the Termination criteria.");
117
118
119

  //MaxIter
  AddParameter(ParameterType_Int, "classifier.ann.iter",
120
               "Maximum number of iterations used in the Termination criteria");
121
  SetParameterInt("classifier.ann.iter",1000, false);
122
  SetParameterDescription("classifier.ann.iter", "Maximum number of iterations used in the Termination criteria.");
123
124
125

}

126
127
128
template <class TInputValue, class TOutputValue>
void
LearningApplicationBase<TInputValue,TOutputValue>
129
130
::TrainNeuralNetwork(typename ListSampleType::Pointer trainingListSample,
                     typename TargetListSampleType::Pointer trainingLabeledListSample,
131
                     std::string modelPath)
132
{
133
  typename NeuralNetworkType::Pointer classifier = NeuralNetworkType::New();
134
  classifier->SetRegressionMode(this->m_RegressionFlag);
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
  classifier->SetInputListSample(trainingListSample);
  classifier->SetTargetListSample(trainingLabeledListSample);

  switch (GetParameterInt("classifier.ann.t"))
    {
    case 0: // RPROP
      classifier->SetTrainMethod(CvANN_MLP_TrainParams::RPROP);
      break;
    case 1: // BACKPROP
      classifier->SetTrainMethod(CvANN_MLP_TrainParams::BACKPROP);
      break;
    default: // DEFAULT = RPROP
      classifier->SetTrainMethod(CvANN_MLP_TrainParams::RPROP);
      break;
    }

  std::vector<unsigned int> layerSizes;
  std::vector<std::string> sizes = GetParameterStringList("classifier.ann.sizes");

154
155
156

  unsigned int nbImageBands = trainingListSample->GetMeasurementVectorSize();
  layerSizes.push_back(nbImageBands);
157
158
159
160
161
162
  for (unsigned int i = 0; i < sizes.size(); i++)
    {
    unsigned int nbNeurons = boost::lexical_cast<unsigned int>(sizes[i]);
    layerSizes.push_back(nbNeurons);
    }

163
164

  unsigned int nbClasses = 0;
165
  if (this->m_RegressionFlag)
166
    {
167
168
169
170
    layerSizes.push_back(1);
    }
  else
    {
171
172
    std::set<TargetValueType> labelSet;
    TargetSampleType currentLabel;
173
    for (unsigned int itLab = 0; itLab < trainingLabeledListSample->Size(); ++itLab)
174
      {
175
      currentLabel = trainingLabeledListSample->GetMeasurementVector(itLab);
176
      labelSet.insert(currentLabel[0]);
177
      }
178
    nbClasses = labelSet.size();
179
    layerSizes.push_back(nbClasses);
180
181
    }

182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
  classifier->SetLayerSizes(layerSizes);

  switch (GetParameterInt("classifier.ann.f"))
    {
    case 0: // ident
      classifier->SetActivateFunction(CvANN_MLP::IDENTITY);
      break;
    case 1: // sig
      classifier->SetActivateFunction(CvANN_MLP::SIGMOID_SYM);
      break;
    case 2: // gaussian
      classifier->SetActivateFunction(CvANN_MLP::GAUSSIAN);
      break;
    default: // DEFAULT = RPROP
      classifier->SetActivateFunction(CvANN_MLP::SIGMOID_SYM);
      break;
    }

  classifier->SetAlpha(GetParameterFloat("classifier.ann.a"));
  classifier->SetBeta(GetParameterFloat("classifier.ann.b"));
  classifier->SetBackPropDWScale(GetParameterFloat("classifier.ann.bpdw"));
  classifier->SetBackPropMomentScale(GetParameterFloat("classifier.ann.bpms"));
  classifier->SetRegPropDW0(GetParameterFloat("classifier.ann.rdw"));
  classifier->SetRegPropDWMin(GetParameterFloat("classifier.ann.rdwm"));

  switch (GetParameterInt("classifier.ann.term"))
    {
    case 0: // CV_TERMCRIT_ITER
      classifier->SetTermCriteriaType(CV_TERMCRIT_ITER);
      break;
    case 1: // CV_TERMCRIT_EPS
      classifier->SetTermCriteriaType(CV_TERMCRIT_EPS);
      break;
    case 2: // CV_TERMCRIT_ITER + CV_TERMCRIT_EPS
      classifier->SetTermCriteriaType(CV_TERMCRIT_ITER + CV_TERMCRIT_EPS);
      break;
    default: // DEFAULT = CV_TERMCRIT_ITER + CV_TERMCRIT_EPS
      classifier->SetTermCriteriaType(CV_TERMCRIT_ITER + CV_TERMCRIT_EPS);
      break;
    }
  classifier->SetEpsilon(GetParameterFloat("classifier.ann.eps"));
  classifier->SetMaxIter(GetParameterInt("classifier.ann.iter"));
  classifier->Train();
225
  classifier->Save(modelPath);
226
}
227

228
229
} //end namespace wrapper
} //end namespace otb
230
231

#endif