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