Commit b2b0fc4e authored by Cédric Traizet's avatar Cédric Traizet
Browse files

autoencodermodel's attribute "m_net" is now a vector (works for a vector of size 1)

No related merge requests found
Showing with 24 additions and 18 deletions
+24 -18
...@@ -58,7 +58,7 @@ protected: ...@@ -58,7 +58,7 @@ protected:
private: private:
/** Network attributes */ /** Network attributes */
AutoencoderType m_net; std::vector<AutoencoderType> m_net;
unsigned int m_NumberOfHiddenNeurons; unsigned int m_NumberOfHiddenNeurons;
/** Training parameters */ /** Training parameters */
......
...@@ -35,6 +35,7 @@ AutoencoderModel<TInputValue,AutoencoderType>::~AutoencoderModel() ...@@ -35,6 +35,7 @@ AutoencoderModel<TInputValue,AutoencoderType>::~AutoencoderModel()
template <class TInputValue, class AutoencoderType> template <class TInputValue, class AutoencoderType>
void AutoencoderModel<TInputValue,AutoencoderType>::Train() void AutoencoderModel<TInputValue,AutoencoderType>::Train()
{ {
AutoencoderType net;
std::vector<shark::RealVector> features; std::vector<shark::RealVector> features;
Shark::ListSampleToSharkVector(this->GetInputListSample(), features); Shark::ListSampleToSharkVector(this->GetInputListSample(), features);
...@@ -42,11 +43,10 @@ void AutoencoderModel<TInputValue,AutoencoderType>::Train() ...@@ -42,11 +43,10 @@ void AutoencoderModel<TInputValue,AutoencoderType>::Train()
shark::Data<shark::RealVector> inputSamples = shark::createDataFromRange( features ); shark::Data<shark::RealVector> inputSamples = shark::createDataFromRange( features );
std::size_t inputs = dataDimension(inputSamples); std::size_t inputs = dataDimension(inputSamples);
m_net.setStructure(inputs, m_NumberOfHiddenNeurons); net.setStructure(inputs, m_NumberOfHiddenNeurons);
initRandomUniform(m_net,-0.1*std::sqrt(1.0/inputs),0.1*std::sqrt(1.0/inputs)); initRandomUniform(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 m_Noise to 0 shark::ImpulseNoiseModel noise(m_Noise,0.0); //set an input pixel with probability m_Noise to 0
shark::ConcatenatedModel<shark::RealVector,shark::RealVector> model = noise>> m_net; shark::ConcatenatedModel<shark::RealVector,shark::RealVector> model = noise>> net;
shark::LabeledData<shark::RealVector,shark::RealVector> trainSet(inputSamples,inputSamples);//labels identical to inputs shark::LabeledData<shark::RealVector,shark::RealVector> trainSet(inputSamples,inputSamples);//labels identical to inputs
shark::SquaredLoss<shark::RealVector> loss; shark::SquaredLoss<shark::RealVector> loss;
shark::ErrorFunction error(trainSet, &model, &loss); shark::ErrorFunction error(trainSet, &model, &loss);
...@@ -56,13 +56,13 @@ void AutoencoderModel<TInputValue,AutoencoderType>::Train() ...@@ -56,13 +56,13 @@ void AutoencoderModel<TInputValue,AutoencoderType>::Train()
shark::IRpropPlusFull optimizer; shark::IRpropPlusFull optimizer;
error.init(); error.init();
optimizer.init(error); optimizer.init(error);
std::cout<<"Optimizing model: "+m_net.name()<<std::endl; std::cout<<"Optimizing model: "+net.name()<<std::endl;
for(std::size_t i = 0; i != m_NumberOfIterations; ++i){ for(std::size_t i = 0; i != m_NumberOfIterations; ++i){
optimizer.step(error); optimizer.step(error);
std::cout<<i<<" "<<optimizer.solution().value<<std::endl; std::cout<<i<<" "<<optimizer.solution().value<<std::endl;
} }
m_net.setParameterVector(optimizer.solution().point); net.setParameterVector(optimizer.solution().point);
m_net.push_back(net);
} }
...@@ -73,7 +73,7 @@ bool AutoencoderModel<TInputValue,AutoencoderType>::CanReadFile(const std::strin ...@@ -73,7 +73,7 @@ bool AutoencoderModel<TInputValue,AutoencoderType>::CanReadFile(const std::strin
try try
{ {
this->Load(filename); this->Load(filename);
m_net.name(); m_net[0].name();
} }
catch(...) catch(...)
{ {
...@@ -93,27 +93,31 @@ template <class TInputValue, class AutoencoderType> ...@@ -93,27 +93,31 @@ template <class TInputValue, class AutoencoderType>
void AutoencoderModel<TInputValue,AutoencoderType>::Save(const std::string & filename, const std::string & name) void AutoencoderModel<TInputValue,AutoencoderType>::Save(const std::string & filename, const std::string & name)
{ {
std::ofstream ofs(filename); std::ofstream ofs(filename);
ofs << m_net.name() << std::endl; // the first line of the model file contains a key ofs << m_net[0].name() << std::endl; // the first line of the model file contains a key
boost::archive::polymorphic_text_oarchive oa(ofs); boost::archive::polymorphic_text_oarchive oa(ofs);
m_net.write(oa); //m_net.write(oa);
oa << m_net;
ofs.close(); ofs.close();
} }
template <class TInputValue, class AutoencoderType> template <class TInputValue, class AutoencoderType>
void AutoencoderModel<TInputValue,AutoencoderType>::Load(const std::string & filename, const std::string & name) void AutoencoderModel<TInputValue,AutoencoderType>::Load(const std::string & filename, const std::string & name)
{ {
AutoencoderType net;
std::ifstream ifs(filename); std::ifstream ifs(filename);
char autoencoder[256]; char autoencoder[256];
ifs.getline(autoencoder,256); ifs.getline(autoencoder,256);
std::string autoencoderstr(autoencoder); std::string autoencoderstr(autoencoder);
std::cout << "oy" << std::endl;
if (autoencoderstr != m_net.name()){ if (autoencoderstr != net.name()){
itkExceptionMacro(<< "Error opening " << filename.c_str() ); itkExceptionMacro(<< "Error opening " << filename.c_str() );
} }
std::cout << "yo" << std::endl;
boost::archive::polymorphic_text_iarchive ia(ifs); boost::archive::polymorphic_text_iarchive ia(ifs);
m_net.read(ia); //m_net.read(ia);
ia >> m_net;
ifs.close(); ifs.close();
m_NumberOfHiddenNeurons = m_net.numberOfHiddenNeurons(); m_NumberOfHiddenNeurons = m_net[0].numberOfHiddenNeurons();
} }
...@@ -132,7 +136,7 @@ AutoencoderModel<TInputValue,AutoencoderType>::DoPredict(const InputSampleType & ...@@ -132,7 +136,7 @@ AutoencoderModel<TInputValue,AutoencoderType>::DoPredict(const InputSampleType &
shark::Data<shark::RealVector> data = shark::createDataFromRange(features); shark::Data<shark::RealVector> data = shark::createDataFromRange(features);
data = m_net.encode(data); data = m_net[0].encode(data);
TargetSampleType target; TargetSampleType target;
target.SetSize(m_NumberOfHiddenNeurons); target.SetSize(m_NumberOfHiddenNeurons);
...@@ -151,7 +155,7 @@ void AutoencoderModel<TInputValue,AutoencoderType> ...@@ -151,7 +155,7 @@ void AutoencoderModel<TInputValue,AutoencoderType>
Shark::ListSampleRangeToSharkVector(input, features,startIndex,size); Shark::ListSampleRangeToSharkVector(input, features,startIndex,size);
shark::Data<shark::RealVector> data = shark::createDataFromRange(features); shark::Data<shark::RealVector> data = shark::createDataFromRange(features);
TargetSampleType target; TargetSampleType target;
data = m_net.encode(data); data = m_net[0].encode(data);
unsigned int id = startIndex; unsigned int id = startIndex;
target.SetSize(m_NumberOfHiddenNeurons); target.SetSize(m_NumberOfHiddenNeurons);
for(const auto& p : data.elements()){ for(const auto& p : data.elements()){
......
...@@ -35,7 +35,7 @@ namespace otb ...@@ -35,7 +35,7 @@ namespace otb
{ {
template <class TInputValue, class TTargetValue> template <class TInputValue, class TTargetValue>
using AutoencoderModelFactory = AutoencoderModelFactoryBase<TInputValue, TTargetValue, shark::Autoencoder< shark::TanhNeuron, shark::LinearNeuron>> ; using AutoencoderModelFactory = AutoencoderModelFactoryBase<TInputValue, TTargetValue, shark::Autoencoder<shark::TanhNeuron, shark::LinearNeuron>> ;
template <class TInputValue, class TTargetValue> template <class TInputValue, class TTargetValue>
......
...@@ -105,7 +105,9 @@ void cbLearningApplicationBaseDR<TInputValue,TOutputValue> ...@@ -105,7 +105,9 @@ void cbLearningApplicationBaseDR<TInputValue,TOutputValue>
dimredTrainer->SetRegularization(GetParameterFloat("model.autoencoder.regularization")); dimredTrainer->SetRegularization(GetParameterFloat("model.autoencoder.regularization"));
dimredTrainer->SetRegularization(GetParameterFloat("model.autoencoder.noise")); dimredTrainer->SetRegularization(GetParameterFloat("model.autoencoder.noise"));
dimredTrainer->SetInputListSample(trainingListSample); dimredTrainer->SetInputListSample(trainingListSample);
std::cout << "before train" << std::endl;
dimredTrainer->Train(); dimredTrainer->Train();
std::cout << "after train" << std::endl;
dimredTrainer->Save(modelPath); dimredTrainer->Save(modelPath);
} }
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment