20 #ifndef otbDimensionalityReductionTrainAutoencoder_hxx
21 #define otbDimensionalityReductionTrainAutoencoder_hxx
31 template <
class TInputValue,
class TOutputValue>
32 void TrainDimensionalityReductionApplicationBase<TInputValue, TOutputValue>::InitAutoencoderParams()
34 AddChoice(
"algorithm.autoencoder",
"Shark Autoencoder");
35 SetParameterDescription(
"algorithm.autoencoder",
"This group of parameters allows setting Shark autoencoder parameters. ");
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.");
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.");
47 SetParameterFloat(
"algorithm.autoencoder.epsilon", 0,
false);
48 SetParameterDescription(
"algorithm.autoencoder.epsilon",
"Epsilon");
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");
56 SetParameterDescription(
"algorithm.autoencoder.nbneuron",
"The number of neurons in each hidden layer.");
60 SetParameterDescription(
"algorithm.autoencoder.regularization",
"Strength of the L2 regularization used during training");
64 SetParameterDescription(
"algorithm.autoencoder.noise",
"Strength of the noise");
68 SetParameterDescription(
"algorithm.autoencoder.rho",
"Sparsity parameter");
72 SetParameterDescription(
"algorithm.autoencoder.beta",
"Sparsity regularization strength");
75 SetParameterDescription(
"algorithm.autoencoder.learningcurve",
"Learning error values");
76 MandatoryOff(
"algorithm.autoencoder.learningcurve");
79 template <
class TInputValue,
class TOutputValue>
80 void TrainDimensionalityReductionApplicationBase<TInputValue, TOutputValue>::BeforeTrainAutoencoder(
typename ListSampleType::Pointer trainingListSample,
81 std::string modelPath)
83 typedef shark::LogisticNeuron NeuronType;
85 TrainAutoencoder<AutoencoderModelType>(trainingListSample, modelPath);
88 template <
class TInputValue,
class TOutputValue>
89 template <
typename autoencoderchoice>
90 void TrainDimensionalityReductionApplicationBase<TInputValue, TOutputValue>::TrainAutoencoder(
typename ListSampleType::Pointer trainingListSample,
91 std::string modelPath)
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++)
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]);
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"))
129 dimredTrainer->SetWriteLearningCurve(
true);
130 dimredTrainer->SetLearningCurveFileName(GetParameterString(
"algorithm.autoencoder.learningcurve"));
133 dimredTrainer->SetInputListSample(trainingListSample);
134 dimredTrainer->Train();
135 dimredTrainer->Save(modelPath);