Skip to content
Snippets Groups Projects
Commit 0c0351e2 authored by Cédric Traizet's avatar Cédric Traizet
Browse files

added denoising autoencoder (noise strength is now a parameter of cbdimensionalityreductiontrainer

parent 4442d2af
No related branches found
No related tags found
No related merge requests found
......@@ -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
......
......@@ -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,&regularizer);
......@@ -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);
......
......@@ -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);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment