Newer
Older
Cédric Traizet
committed
#ifndef PCAModel_txx
#define PCAModel_txx
#include <fstream>
#include <shark/Data/Dataset.h>
#include "itkMacro.h"
#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
namespace otb
{
template <class TInputValue>
PCAModel<TInputValue>::PCAModel()
{
Cédric Traizet
committed
this->m_Dimension = 0;
Cédric Traizet
committed
PCAModel<TInputValue>::~PCAModel()
{
}
template <class TInputValue>
void PCAModel<TInputValue>::Train()
{
std::vector<shark::RealVector> features;
Shark::ListSampleToSharkVector(this->GetInputListSample(), features);
shark::Data<shark::RealVector> inputSamples = shark::createDataFromRange( features );
Cédric Traizet
committed
m_pca.setData(inputSamples);
Cédric Traizet
committed
m_pca.encoder(m_encoder, this->m_Dimension);
Cédric Traizet
committed
std::cout << m_encoder.matrix() << std::endl;
Cédric Traizet
committed
m_pca.decoder(m_decoder, this->m_Dimension);
}
template <class TInputValue>
bool PCAModel<TInputValue>::CanReadFile(const std::string & filename)
{
try
{
this->Load(filename);
}
catch(...)
{
return false;
}
return true;
}
template <class TInputValue>
bool PCAModel<TInputValue>::CanWriteFile(const std::string & filename)
{
return true;
}
template <class TInputValue>
void PCAModel<TInputValue>::Save(const std::string & filename, const std::string & name)
{
std::ofstream ofs(filename);
Cédric Traizet
committed
//ofs << m_encoder.name() << std::endl; //first line
ofs << "pca" << std::endl; //first line
boost::archive::polymorphic_text_oarchive oa(ofs);
ofs.close();
}
template <class TInputValue>
void PCAModel<TInputValue>::Load(const std::string & filename, const std::string & name)
{
std::ifstream ifs(filename);
char encoder[256];
ifs.getline(encoder,256);
std::string encoderstr(encoder);
Cédric Traizet
committed
//if (encoderstr != m_encoder.name()){
if (encoderstr != "pca"){
itkExceptionMacro(<< "Error opening " << filename.c_str() );
}
boost::archive::polymorphic_text_iarchive ia(ifs);
Cédric Traizet
committed
m_encoder.read(ia);
Cédric Traizet
committed
if (this->m_Dimension ==0)
{
this->m_Dimension = m_encoder.outputSize();
}
else
{
std::cout << "yo" << std::endl;
}
Cédric Traizet
committed
auto eigenvectors = m_encoder.matrix();
Cédric Traizet
committed
eigenvectors.resize(this->m_Dimension,m_encoder.inputSize());
Cédric Traizet
committed
m_encoder.setStructure(eigenvectors, m_encoder.offset() );
Cédric Traizet
committed
std::cout << m_encoder.matrix() << "end" << std::endl;
//this->m_Size = m_NumberOfHiddenNeurons;
}
template <class TInputValue>
typename PCAModel<TInputValue>::TargetSampleType
Cédric Traizet
committed
PCAModel<TInputValue>::DoPredict(const InputSampleType & value, ConfidenceValueType * quality) const
{
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
std::vector<shark::RealVector> features;
features.push_back(samples);
shark::Data<shark::RealVector> data = shark::createDataFromRange(features);
data = m_encoder(data);
Cédric Traizet
committed
target.SetSize(this->m_Dimension);
Cédric Traizet
committed
for(unsigned int a = 0; a < this->m_Dimension; ++a){
Cédric Traizet
committed
target[a]=data.element(0)[a];
}
return target;
}
template <class TInputValue>
void PCAModel<TInputValue>
Cédric Traizet
committed
::DoPredictBatch(const InputListSampleType *input, const unsigned int & startIndex, const unsigned int & size, TargetListSampleType * targets, ConfidenceListSampleType * quality) const
{
std::vector<shark::RealVector> features;
Shark::ListSampleRangeToSharkVector(input, features,startIndex,size);
shark::Data<shark::RealVector> data = shark::createDataFromRange(features);
TargetSampleType target;
Cédric Traizet
committed
target.SetSize(this->m_Dimension);
Cédric Traizet
committed
for(unsigned int a = 0; a < this->m_Dimension; ++a){
Cédric Traizet
committed
//target[a]=1;