Commit 0c0351e2110bad051825f161fdc301774306c2b4

Authored by Cédric Traizet
1 parent 4442d2af

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

include/AutoencoderModel.h
... ... @@ -37,6 +37,9 @@ public:
37 37 itkGetMacro(Regularization,double);
38 38 itkSetMacro(Regularization,double);
39 39  
  40 + itkGetMacro(Noise,double);
  41 + itkSetMacro(Noise,double);
  42 +
40 43 bool CanReadFile(const std::string & filename);
41 44 bool CanWriteFile(const std::string & filename);
42 45  
... ... @@ -60,6 +63,7 @@ private:
60 63 unsigned int m_NumberOfHiddenNeurons;
61 64 unsigned int m_NumberOfIterations;
62 65 double m_Regularization;
  66 + double m_Noise;
63 67 };
64 68 } // end namespace otb
65 69  
... ...
include/AutoencoderModel.txx
... ... @@ -11,6 +11,8 @@
11 11 #include <shark/Algorithms/GradientDescent/Rprop.h>// the RProp optimization algorithm
12 12 #include <shark/ObjectiveFunctions/Loss/SquaredLoss.h> // squared loss used for regression
13 13 #include <shark/ObjectiveFunctions/Regularizer.h> //L2 regulariziation
  14 +#include <shark/Models/ImpulseNoiseModel.h>//noise source to corrupt the inputs
  15 +#include <shark/Models/ConcatenatedModel.h>//to concatenate the noise with the model
14 16  
15 17 namespace otb
16 18 {
... ... @@ -32,7 +34,6 @@ AutoencoderModel&lt;TInputValue,AutoencoderType&gt;::~AutoencoderModel()
32 34 template <class TInputValue, class AutoencoderType>
33 35 void AutoencoderModel<TInputValue,AutoencoderType>::Train()
34 36 {
35   -
36 37 std::vector<shark::RealVector> features;
37 38  
38 39 Shark::ListSampleToSharkVector(this->GetInputListSample(), features);
... ... @@ -42,10 +43,12 @@ void AutoencoderModel&lt;TInputValue,AutoencoderType&gt;::Train()
42 43 std::size_t inputs = dataDimension(inputSamples);
43 44 m_net.setStructure(inputs, m_NumberOfHiddenNeurons);
44 45 initRandomUniform(m_net,-0.1*std::sqrt(1.0/inputs),0.1*std::sqrt(1.0/inputs));
45   -
  46 + shark::ImpulseNoiseModel noise(m_Noise,0.0);//set an input pixel with probability p to 0
  47 + shark::ConcatenatedModel<shark::RealVector,shark::RealVector> model = noise>> m_net;
  48 +
46 49 shark::LabeledData<shark::RealVector,shark::RealVector> trainSet(inputSamples,inputSamples);//labels identical to inputs
47 50 shark::SquaredLoss<shark::RealVector> loss;
48   - shark::ErrorFunction error(trainSet, &m_net, &loss);
  51 + shark::ErrorFunction error(trainSet, &model, &loss);
49 52 shark::TwoNormRegularizer regularizer(error.numberOfVariables());
50 53 error.setRegularizer(m_Regularization,&regularizer);
51 54  
... ... @@ -117,7 +120,6 @@ template &lt;class TInputValue, class AutoencoderType&gt;
117 120 typename AutoencoderModel<TInputValue,AutoencoderType>::TargetSampleType
118 121 AutoencoderModel<TInputValue,AutoencoderType>::DoPredict(const InputSampleType & value) const
119 122 {
120   - std::cout << "SINGLE PIXEL " ;
121 123 shark::RealVector samples(value.Size());
122 124 for(size_t i = 0; i < value.Size();i++)
123 125 {
... ... @@ -144,7 +146,6 @@ template &lt;class TInputValue, class AutoencoderType&gt;
144 146 void AutoencoderModel<TInputValue,AutoencoderType>
145 147 ::DoPredictBatch(const InputListSampleType *input, const unsigned int & startIndex, const unsigned int & size, TargetListSampleType * targets) const
146 148 {
147   - std::cout << "BATCH" << std::endl;
148 149 std::vector<shark::RealVector> features;
149 150 Shark::ListSampleRangeToSharkVector(input, features,startIndex,size);
150 151 shark::Data<shark::RealVector> data = shark::createDataFromRange(features);
... ...
include/cbTrainAutoencoder.txx
... ... @@ -58,6 +58,12 @@ cbLearningApplicationBaseDR&lt;TInputValue,TOutputValue&gt;
58 58 SetParameterFloat("model.autoencoder.regularization",0, false);
59 59 SetParameterDescription("model.autoencoder.regularization",
60 60 "Strength of the L2 regularization used during training");
  61 +
  62 + //Noise strength
  63 + AddParameter(ParameterType_Float, "model.autoencoder.noise", "Strength of the noise");
  64 + SetParameterFloat("model.autoencoder.noise",0, false);
  65 + SetParameterDescription("model.autoencoder.noise",
  66 + "Strength of the noise");
61 67 }
62 68  
63 69  
... ... @@ -97,6 +103,7 @@ void cbLearningApplicationBaseDR&lt;TInputValue,TOutputValue&gt;
97 103 dimredTrainer->SetNumberOfHiddenNeurons(GetParameterInt("model.autoencoder.nbneuron"));
98 104 dimredTrainer->SetNumberOfIterations(GetParameterInt("model.autoencoder.nbiter"));
99 105 dimredTrainer->SetRegularization(GetParameterFloat("model.autoencoder.regularization"));
  106 + dimredTrainer->SetRegularization(GetParameterFloat("model.autoencoder.noise"));
100 107 dimredTrainer->SetInputListSample(trainingListSample);
101 108 dimredTrainer->Train();
102 109 dimredTrainer->Save(modelPath);
... ...