From 0c0351e2110bad051825f161fdc301774306c2b4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Traizet?= <traizetc@cesbio.cnes.fr> Date: Wed, 24 May 2017 17:07:10 +0200 Subject: [PATCH] added denoising autoencoder (noise strength is now a parameter of cbdimensionalityreductiontrainer --- include/AutoencoderModel.h | 4 ++++ include/AutoencoderModel.txx | 11 ++++++----- include/cbTrainAutoencoder.txx | 7 +++++++ 3 files changed, 17 insertions(+), 5 deletions(-) diff --git a/include/AutoencoderModel.h b/include/AutoencoderModel.h index eb65fdb1df..81b55a5fbf 100644 --- a/include/AutoencoderModel.h +++ b/include/AutoencoderModel.h @@ -37,6 +37,9 @@ public: itkGetMacro(Regularization,double); itkSetMacro(Regularization,double); + itkGetMacro(Noise,double); + itkSetMacro(Noise,double); + bool CanReadFile(const std::string & filename); bool CanWriteFile(const std::string & filename); @@ -60,6 +63,7 @@ private: unsigned int m_NumberOfHiddenNeurons; unsigned int m_NumberOfIterations; double m_Regularization; + double m_Noise; }; } // end namespace otb diff --git a/include/AutoencoderModel.txx b/include/AutoencoderModel.txx index dad8324514..5c7bc5d97e 100644 --- a/include/AutoencoderModel.txx +++ b/include/AutoencoderModel.txx @@ -11,6 +11,8 @@ #include <shark/Algorithms/GradientDescent/Rprop.h>// the RProp optimization algorithm #include <shark/ObjectiveFunctions/Loss/SquaredLoss.h> // squared loss used for regression #include <shark/ObjectiveFunctions/Regularizer.h> //L2 regulariziation +#include <shark/Models/ImpulseNoiseModel.h>//noise source to corrupt the inputs +#include <shark/Models/ConcatenatedModel.h>//to concatenate the noise with the model namespace otb { @@ -32,7 +34,6 @@ AutoencoderModel<TInputValue,AutoencoderType>::~AutoencoderModel() template <class TInputValue, class AutoencoderType> void AutoencoderModel<TInputValue,AutoencoderType>::Train() { - std::vector<shark::RealVector> features; Shark::ListSampleToSharkVector(this->GetInputListSample(), features); @@ -42,10 +43,12 @@ void AutoencoderModel<TInputValue,AutoencoderType>::Train() std::size_t inputs = dataDimension(inputSamples); m_net.setStructure(inputs, m_NumberOfHiddenNeurons); initRandomUniform(m_net,-0.1*std::sqrt(1.0/inputs),0.1*std::sqrt(1.0/inputs)); - + shark::ImpulseNoiseModel noise(m_Noise,0.0);//set an input pixel with probability p to 0 + shark::ConcatenatedModel<shark::RealVector,shark::RealVector> model = noise>> m_net; + shark::LabeledData<shark::RealVector,shark::RealVector> trainSet(inputSamples,inputSamples);//labels identical to inputs shark::SquaredLoss<shark::RealVector> loss; - shark::ErrorFunction error(trainSet, &m_net, &loss); + shark::ErrorFunction error(trainSet, &model, &loss); shark::TwoNormRegularizer regularizer(error.numberOfVariables()); error.setRegularizer(m_Regularization,®ularizer); @@ -117,7 +120,6 @@ template <class TInputValue, class AutoencoderType> typename AutoencoderModel<TInputValue,AutoencoderType>::TargetSampleType AutoencoderModel<TInputValue,AutoencoderType>::DoPredict(const InputSampleType & value) const { - std::cout << "SINGLE PIXEL " ; shark::RealVector samples(value.Size()); for(size_t i = 0; i < value.Size();i++) { @@ -144,7 +146,6 @@ template <class TInputValue, class AutoencoderType> void AutoencoderModel<TInputValue,AutoencoderType> ::DoPredictBatch(const InputListSampleType *input, const unsigned int & startIndex, const unsigned int & size, TargetListSampleType * targets) const { - std::cout << "BATCH" << std::endl; std::vector<shark::RealVector> features; Shark::ListSampleRangeToSharkVector(input, features,startIndex,size); shark::Data<shark::RealVector> data = shark::createDataFromRange(features); diff --git a/include/cbTrainAutoencoder.txx b/include/cbTrainAutoencoder.txx index 82e21d17b6..e120ae5e9b 100644 --- a/include/cbTrainAutoencoder.txx +++ b/include/cbTrainAutoencoder.txx @@ -58,6 +58,12 @@ cbLearningApplicationBaseDR<TInputValue,TOutputValue> SetParameterFloat("model.autoencoder.regularization",0, false); SetParameterDescription("model.autoencoder.regularization", "Strength of the L2 regularization used during training"); + + //Noise strength + AddParameter(ParameterType_Float, "model.autoencoder.noise", "Strength of the noise"); + SetParameterFloat("model.autoencoder.noise",0, false); + SetParameterDescription("model.autoencoder.noise", + "Strength of the noise"); } @@ -97,6 +103,7 @@ void cbLearningApplicationBaseDR<TInputValue,TOutputValue> dimredTrainer->SetNumberOfHiddenNeurons(GetParameterInt("model.autoencoder.nbneuron")); dimredTrainer->SetNumberOfIterations(GetParameterInt("model.autoencoder.nbiter")); dimredTrainer->SetRegularization(GetParameterFloat("model.autoencoder.regularization")); + dimredTrainer->SetRegularization(GetParameterFloat("model.autoencoder.noise")); dimredTrainer->SetInputListSample(trainingListSample); dimredTrainer->Train(); dimredTrainer->Save(modelPath); -- GitLab