diff --git a/include/AutoencoderModel.h b/include/AutoencoderModel.h index eb65fdb1dfd6bf9325b73ed67c54711b28423e96..81b55a5fbffb89b7ff28e1e0e81ee112c51c9c9e 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 dad8324514755e530ec5b48c8983e1908d73b5f6..5c7bc5d97efd7fa56d9c4fdd1ee9e5a86a9aa42a 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 82e21d17b67ff98ef635b5357267eb4c73963008..e120ae5e9b85f2176c867c6ed6f148f3d7d2962f 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);