OTB  6.7.0
Orfeo Toolbox
otbDimensionalityReductionTrainAutoencoder.hxx
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2005-2019 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 #ifndef otbDimensionalityReductionTrainAutoencoder_hxx
21 #define otbDimensionalityReductionTrainAutoencoder_hxx
22 
24 #include "otbAutoencoderModel.h"
25 
26 namespace otb
27 {
28 namespace Wrapper
29 {
30 
31 template <class TInputValue, class TOutputValue>
32 void
33 TrainDimensionalityReductionApplicationBase<TInputValue,TOutputValue>
34 ::InitAutoencoderParams()
35 {
36  AddChoice("algorithm.autoencoder", "Shark Autoencoder");
37  SetParameterDescription("algorithm.autoencoder",
38  "This group of parameters allows setting Shark autoencoder parameters. "
39  );
40 
41  //Number Of Iterations
42  AddParameter(ParameterType_Int, "algorithm.autoencoder.nbiter",
43  "Maximum number of iterations during training");
44  SetParameterInt("algorithm.autoencoder.nbiter",100, false);
45  SetParameterDescription(
46  "algorithm.autoencoder.nbiter",
47  "The maximum number of iterations used during training.");
48 
49  AddParameter(ParameterType_Int, "algorithm.autoencoder.nbiterfinetuning",
50  "Maximum number of iterations during training");
51  SetParameterInt("algorithm.autoencoder.nbiterfinetuning",0, false);
52  SetParameterDescription(
53  "algorithm.autoencoder.nbiterfinetuning",
54  "The maximum number of iterations used during fine tuning of the whole network.");
55 
56  AddParameter(ParameterType_Float, "algorithm.autoencoder.epsilon",
57  "Epsilon");
58  SetParameterFloat("algorithm.autoencoder.epsilon",0, false);
59  SetParameterDescription(
60  "algorithm.autoencoder.epsilon",
61  "Epsilon");
62 
63  AddParameter(ParameterType_Float, "algorithm.autoencoder.initfactor",
64  "Weight initialization factor");
65  SetParameterFloat("algorithm.autoencoder.initfactor",1, false);
66  SetParameterDescription(
67  "algorithm.autoencoder.initfactor", "Parameter that control the weight initialization of the autoencoder");
68 
69  //Number Of Hidden Neurons
70  AddParameter(ParameterType_StringList, "algorithm.autoencoder.nbneuron", "Size");
71  SetParameterDescription(
72  "algorithm.autoencoder.nbneuron",
73  "The number of neurons in each hidden layer.");
74 
75  //Regularization
76  AddParameter(ParameterType_StringList, "algorithm.autoencoder.regularization", "Strength of the regularization");
77  SetParameterDescription("algorithm.autoencoder.regularization",
78  "Strength of the L2 regularization used during training");
79 
80  //Noise strength
81  AddParameter(ParameterType_StringList, "algorithm.autoencoder.noise", "Strength of the noise");
82  SetParameterDescription("algorithm.autoencoder.noise",
83  "Strength of the noise");
84 
85  // Sparsity parameter
86  AddParameter(ParameterType_StringList, "algorithm.autoencoder.rho", "Sparsity parameter");
87  SetParameterDescription("algorithm.autoencoder.rho",
88  "Sparsity parameter");
89 
90  // Sparsity regularization strength
91  AddParameter(ParameterType_StringList, "algorithm.autoencoder.beta", "Sparsity regularization strength");
92  SetParameterDescription("algorithm.autoencoder.beta",
93  "Sparsity regularization strength");
94 
95  AddParameter(ParameterType_OutputFilename, "algorithm.autoencoder.learningcurve", "Learning curve");
96  SetParameterDescription("algorithm.autoencoder.learningcurve", "Learning error values");
97  MandatoryOff("algorithm.autoencoder.learningcurve");
98 }
99 
100 template <class TInputValue, class TOutputValue>
101 void
102 TrainDimensionalityReductionApplicationBase<TInputValue,TOutputValue>
103 ::BeforeTrainAutoencoder(typename ListSampleType::Pointer trainingListSample,
104  std::string modelPath)
105 {
106  typedef shark::LogisticNeuron NeuronType;
107  typedef otb::AutoencoderModel<InputValueType, NeuronType> AutoencoderModelType;
108  TrainAutoencoder<AutoencoderModelType>(trainingListSample,modelPath);
109 }
110 
111 template <class TInputValue, class TOutputValue>
112 template <typename autoencoderchoice>
113 void TrainDimensionalityReductionApplicationBase<TInputValue,TOutputValue>::TrainAutoencoder(typename ListSampleType::Pointer trainingListSample,std::string modelPath)
114 {
115  typename autoencoderchoice::Pointer dimredTrainer = autoencoderchoice::New();
116  itk::Array<unsigned int> nb_neuron;
117  itk::Array<float> noise;
118  itk::Array<float> regularization;
119  itk::Array<float> rho;
120  itk::Array<float> beta;
121  std::vector<std::basic_string<char>> s_nbneuron= GetParameterStringList("algorithm.autoencoder.nbneuron");
122  std::vector<std::basic_string<char>> s_noise= GetParameterStringList("algorithm.autoencoder.noise");
123  std::vector<std::basic_string<char>> s_regularization= GetParameterStringList("algorithm.autoencoder.regularization");
124  std::vector<std::basic_string<char>> s_rho= GetParameterStringList("algorithm.autoencoder.rho");
125  std::vector<std::basic_string<char>> s_beta= GetParameterStringList("algorithm.autoencoder.beta");
126  nb_neuron.SetSize(s_nbneuron.size());
127  noise.SetSize(s_nbneuron.size());
128  regularization.SetSize(s_nbneuron.size());
129  rho.SetSize(s_nbneuron.size());
130  beta.SetSize(s_nbneuron.size());
131  for (unsigned int i=0; i<s_nbneuron.size(); i++)
132  {
133  nb_neuron[i]=std::stoi(s_nbneuron[i]);
134  noise[i]=std::stof(s_noise[i]);
135  regularization[i]=std::stof(s_regularization[i]);
136  rho[i]=std::stof(s_rho[i]);
137  beta[i]=std::stof(s_beta[i]);
138  }
139  dimredTrainer->SetNumberOfHiddenNeurons(nb_neuron);
140  dimredTrainer->SetNumberOfIterations(GetParameterInt("algorithm.autoencoder.nbiter"));
141  dimredTrainer->SetNumberOfIterationsFineTuning(GetParameterInt("algorithm.autoencoder.nbiterfinetuning"));
142  dimredTrainer->SetEpsilon(GetParameterFloat("algorithm.autoencoder.epsilon"));
143  dimredTrainer->SetInitFactor(GetParameterFloat("algorithm.autoencoder.initfactor"));
144  dimredTrainer->SetRegularization(regularization);
145  dimredTrainer->SetNoise(noise);
146  dimredTrainer->SetRho(rho);
147  dimredTrainer->SetBeta(beta);
148  dimredTrainer->SetWriteWeights(true);
149  if (HasValue("algorithm.autoencoder.learningcurve") &&
150  IsParameterEnabled("algorithm.autoencoder.learningcurve"))
151  {
152  dimredTrainer->SetWriteLearningCurve(true);
153  dimredTrainer->SetLearningCurveFileName(GetParameterString("algorithm.autoencoder.learningcurve"));
154  }
155 
156  dimredTrainer->SetInputListSample(trainingListSample);
157  dimredTrainer->Train();
158  dimredTrainer->Save(modelPath);
159 }
160 
161 } //end namespace wrapper
162 } //end namespace otb
163 
164 #endif
void SetSize(SizeValueType sz)