Newer
Older
Cédric Traizet
committed
Cédric Traizet
committed
#ifndef AutoencoderModel_txx
#define AutoencoderModel_txx
Cédric Traizet
committed
#include <fstream>
#include <shark/Data/Dataset.h>
Cédric Traizet
committed
#include "itkMacro.h"
Cédric Traizet
committed
#include "otbSharkUtils.h"
//include train function
#include <shark/ObjectiveFunctions/ErrorFunction.h>
#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
Cédric Traizet
committed
namespace otb
{
template <class TInputValue, class AutoencoderType>
AutoencoderModel<TInputValue,AutoencoderType>::AutoencoderModel()
{
Cédric Traizet
committed
this->m_IsRegressionSupported = true;
Cédric Traizet
committed
template <class TInputValue, class AutoencoderType>
AutoencoderModel<TInputValue,AutoencoderType>::~AutoencoderModel()
{
}
Cédric Traizet
committed
template <class TInputValue, class AutoencoderType>
void AutoencoderModel<TInputValue,AutoencoderType>::Train()
{
Cédric Traizet
committed
std::vector<shark::RealVector> features;
Shark::ListSampleToSharkVector(this->GetInputListSample(), features);
shark::Data<shark::RealVector> inputSamples = shark::createDataFromRange( features );
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::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::TwoNormRegularizer regularizer(error.numberOfVariables());
error.setRegularizer(m_Regularization,®ularizer);
shark::IRpropPlusFull optimizer;
error.init();
optimizer.init(error);
std::cout<<"Optimizing model: "+m_net.name()<<std::endl;
for(std::size_t i = 0; i != m_NumberOfIterations; ++i){
optimizer.step(error);
std::cout<<i<<" "<<optimizer.solution().value<<std::endl;
}
//std::cout<<optimizer.solution().value<<std::endl;
m_net.setParameterVector(optimizer.solution().point);
Cédric Traizet
committed
Cédric Traizet
committed
}
template <class TInputValue, class AutoencoderType>
bool AutoencoderModel<TInputValue,AutoencoderType>::CanReadFile(const std::string & filename)
{
try
{
this->Load(filename);
m_net.name();
}
catch(...)
{
return false;
}
return true;
}
Cédric Traizet
committed
Cédric Traizet
committed
template <class TInputValue, class AutoencoderType>
bool AutoencoderModel<TInputValue,AutoencoderType>::CanWriteFile(const std::string & filename)
{
return true;
Cédric Traizet
committed
}
Cédric Traizet
committed
template <class TInputValue, class AutoencoderType>
void AutoencoderModel<TInputValue,AutoencoderType>::Save(const std::string & filename, const std::string & name)
{
std::ofstream ofs(filename);
Cédric Traizet
committed
ofs << m_net.name() << std::endl; //first line
Cédric Traizet
committed
boost::archive::polymorphic_text_oarchive oa(ofs);
m_net.write(oa);
ofs.close();
}
Cédric Traizet
committed
Cédric Traizet
committed
template <class TInputValue, class AutoencoderType>
void AutoencoderModel<TInputValue,AutoencoderType>::Load(const std::string & filename, const std::string & name)
{
std::ifstream ifs(filename);
Cédric Traizet
committed
char autoencoder[256];
ifs.getline(autoencoder,256);
std::string autoencoderstr(autoencoder);
if (autoencoderstr != m_net.name()){
itkExceptionMacro(<< "Error opening " << filename.c_str() );
}
Cédric Traizet
committed
boost::archive::polymorphic_text_iarchive ia(ifs);
m_net.read(ia);
ifs.close();
m_NumberOfHiddenNeurons = m_net.numberOfHiddenNeurons();
Cédric Traizet
committed
}
Cédric Traizet
committed
Cédric Traizet
committed
template <class TInputValue, class AutoencoderType>
typename AutoencoderModel<TInputValue,AutoencoderType>::TargetSampleType
AutoencoderModel<TInputValue,AutoencoderType>::DoPredict(const InputSampleType & value, ConfidenceValueType *quality) const
Cédric Traizet
committed
{
shark::RealVector samples(value.Size());
for(size_t i = 0; i < value.Size();i++)
{
Cédric Traizet
committed
samples[i]=value[i];
Cédric Traizet
committed
}
Cédric Traizet
committed
std::vector<shark::RealVector> features;
features.push_back(samples);
shark::Data<shark::RealVector> data = shark::createDataFromRange(features);
data = m_net.encode(data);
Cédric Traizet
committed
TargetSampleType target;
Cédric Traizet
committed
target.SetSize(m_NumberOfHiddenNeurons);
Cédric Traizet
committed
for(unsigned int a = 0; a < m_NumberOfHiddenNeurons; ++a){
Cédric Traizet
committed
target[a]=data.element(0)[a];
Cédric Traizet
committed
}
return target;
}
template <class TInputValue, class AutoencoderType>
void AutoencoderModel<TInputValue,AutoencoderType>
::DoPredictBatch(const InputListSampleType *input, const unsigned int & startIndex, const unsigned int & size, TargetListSampleType * targets, ConfidenceListSampleType * quality) const
Cédric Traizet
committed
{
Cédric Traizet
committed
std::cout << "BATCH" << std::endl;
Cédric Traizet
committed
std::vector<shark::RealVector> features;
Shark::ListSampleRangeToSharkVector(input, features,startIndex,size);
shark::Data<shark::RealVector> data = shark::createDataFromRange(features);
Cédric Traizet
committed
TargetSampleType target;
Cédric Traizet
committed
data = m_net.encode(data);
unsigned int id = startIndex;
Cédric Traizet
committed
target.SetSize(m_NumberOfHiddenNeurons);
Cédric Traizet
committed
for(const auto& p : data.elements()){
for(unsigned int a = 0; a < m_NumberOfHiddenNeurons; ++a){
Cédric Traizet
committed
target[a]=p[a];
//target.SetElement(a,p[a]);
Cédric Traizet
committed
}
Cédric Traizet
committed
//std::cout << p << std::endl;
Cédric Traizet
committed
targets->SetMeasurementVector(id,target);
++id;
Cédric Traizet
committed
Cédric Traizet
committed
}
Cédric Traizet
committed
Cédric Traizet
committed
}
Cédric Traizet
committed
Cédric Traizet
committed
Cédric Traizet
committed
} // namespace otb
#endif