PCAModel.txx 4.71 KiB
#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
#include <shark/ObjectiveFunctions/ErrorFunction.h>
namespace otb
template <class TInputValue>
PCAModel<TInputValue>::PCAModel()
	this->m_IsDoPredictBatchMultiThreaded = true;
	this->m_Dimension = 0;
template <class TInputValue>
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 );
	m_pca.setData(inputSamples);
	m_pca.encoder(m_encoder, this->m_Dimension);
	m_pca.decoder(m_decoder, this->m_Dimension);
template <class TInputValue>
bool PCAModel<TInputValue>::CanReadFile(const std::string & filename)
	try
		this->Load(filename);
		m_encoder.name();
	catch(...)
	return false;
	return true;
template <class TInputValue>
bool PCAModel<TInputValue>::CanWriteFile(const std::string & filename)
	return true;
7172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
} template <class TInputValue> void PCAModel<TInputValue>::Save(const std::string & filename, const std::string & name) { std::ofstream ofs(filename); //ofs << m_encoder.name() << std::endl; //first line ofs << "pca" << std::endl; //first line boost::archive::polymorphic_text_oarchive oa(ofs); m_encoder.write(oa); ofs.close(); if (this->m_WriteEigenvectors == true) // output the map vectors in a txt file { std::ofstream otxt(filename+".txt"); otxt << "Eigenvectors : " << m_pca.eigenvectors() << std::endl; otxt << "Eigenvalues : " << m_pca.eigenvalues() << std::endl; std::vector<shark::RealVector> features; shark::SquaredLoss<shark::RealVector> loss; Shark::ListSampleToSharkVector(this->GetInputListSample(), features); shark::Data<shark::RealVector> inputSamples = shark::createDataFromRange( features ); otxt << "Reconstruction error : " << loss.eval(inputSamples,m_decoder(m_encoder(inputSamples))) << std::endl; std::cout << "Reconstruction error : " << loss.eval(inputSamples,m_decoder(m_encoder(inputSamples))) << std::endl; otxt.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); //if (encoderstr != m_encoder.name()){ if (encoderstr != "pca"){ itkExceptionMacro(<< "Error opening " << filename.c_str() ); } boost::archive::polymorphic_text_iarchive ia(ifs); m_encoder.read(ia); ifs.close(); if (this->m_Dimension ==0) { this->m_Dimension = m_encoder.outputSize(); } auto eigenvectors = m_encoder.matrix(); eigenvectors.resize(this->m_Dimension,m_encoder.inputSize()); m_encoder.setStructure(eigenvectors, m_encoder.offset() ); } template <class TInputValue> typename PCAModel<TInputValue>::TargetSampleType PCAModel<TInputValue>::DoPredict(const InputSampleType & value, ConfidenceValueType * quality) const { shark::RealVector samples(value.Size()); for(size_t i = 0; i < value.Size();i++) { samples[i]=value[i]; }
141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
std::vector<shark::RealVector> features; features.push_back(samples); shark::Data<shark::RealVector> data = shark::createDataFromRange(features); data = m_encoder(data); TargetSampleType target; target.SetSize(this->m_Dimension); for(unsigned int a = 0; a < this->m_Dimension; ++a){ target[a]=data.element(0)[a]; } return target; } template <class TInputValue> void PCAModel<TInputValue> ::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; data = m_encoder(data); unsigned int id = startIndex; target.SetSize(this->m_Dimension); for(const auto& p : data.elements()){ for(unsigned int a = 0; a < this->m_Dimension; ++a){ target[a]=p[a]; //target[a]=1; //target.SetElement(a,p[a]); } //std::cout << p << std::endl; targets->SetMeasurementVector(id,target); ++id; } } } // namespace otb #endif