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

pca added (work in progress)

No related merge requests found
Showing with 224 additions and 0 deletions
+224 -0
#ifndef PCAModel_h
#define PCAModel_h
#include "DimensionalityReductionModel.h"
#include <shark/Algorithms/Trainers/PCA.h>
namespace otb
{
template <class TInputValue>
class ITK_EXPORT PCAModel: public DimensionalityReductionModel<TInputValue,TInputValue>
{
public:
typedef PCAModel Self;
typedef DimensionalityReductionModel<TInputValue,TInputValue> Superclass;
typedef itk::SmartPointer<Self> Pointer;
typedef itk::SmartPointer<const Self> ConstPointer;
typedef typename Superclass::InputValueType InputValueType;
typedef typename Superclass::InputSampleType InputSampleType;
typedef typename Superclass::InputListSampleType InputListSampleType;
typedef typename Superclass::TargetValueType TargetValueType;
typedef typename Superclass::TargetSampleType TargetSampleType;
typedef typename Superclass::TargetListSampleType TargetListSampleType;
typedef typename Superclass::ConfidenceValueType ConfidenceValueType;
typedef typename Superclass::ConfidenceSampleType ConfidenceSampleType;
typedef typename Superclass::ConfidenceListSampleType ConfidenceListSampleType;
itkNewMacro(Self);
itkTypeMacro(AutoencoderModel, DimensionalityReductionModel);
unsigned int GetDimension() {return m_Dimension;};
itkGetMacro(Dimension,unsigned int);
bool CanReadFile(const std::string & filename);
bool CanWriteFile(const std::string & filename);
void Save(const std::string & filename, const std::string & name="") ITK_OVERRIDE;
void Load(const std::string & filename, const std::string & name="") ITK_OVERRIDE;
void Train() ITK_OVERRIDE;
//void Dimensionality_reduction() {}; // Dimensionality reduction is done by DoPredict
protected:
PCAModel();
~PCAModel() ITK_OVERRIDE;
virtual TargetSampleType DoPredict(const InputSampleType& input, ConfidenceValueType *quality=ITK_NULLPTR) const ITK_OVERRIDE;
virtual void DoPredictBatch(const InputListSampleType *, const unsigned int & startIndex, const unsigned int & size, TargetListSampleType *, ConfidenceListSampleType * = ITK_NULLPTR) const ITK_OVERRIDE;
private:
LinearModel<> m_encoder
LinearModel<> m_decoder
PCA m_pca;
unsigned int m_Dimension;
};
} // end namespace otb
#ifndef OTB_MANUAL_INSTANTIATION
#include "PCAModel.txx"
#endif
#endif
#ifndef AutoencoderModel_txx
#define AutoencoderModel_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()
{
this->m_IsRegressionSupported = true;
}
template <class TInputValue>
PCAModel<TInputValue,AutoencoderType>::~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(inputSamples);
pca.encoder(m_encoder, m_Dimension);
pca.decoder(m_decoder, m_Dimension);
}
template <class TInputValue>
bool PCAModel<TInputValue>::CanReadFile(const std::string & filename)
{
try
{
this->Load(filename);
m_net.name();
}
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);
ofs << m_net.name() << std::endl; //first line
boost::archive::polymorphic_text_oarchive oa(ofs);
m_net.write(oa);
ofs.close();
}
template <class TInputValue>
void PCAModel<TInputValue>::Load(const std::string & filename, const std::string & name)
{
std::ifstream ifs(filename);
char autoencoder[256];
ifs.getline(autoencoder,256);
std::string autoencoderstr(autoencoder);
if (autoencoderstr != m_net.name()){
itkExceptionMacro(<< "Error opening " << filename.c_str() );
}
boost::archive::polymorphic_text_iarchive ia(ifs);
m_net.read(ia);
ifs.close();
m_NumberOfHiddenNeurons = m_net.numberOfHiddenNeurons();
//this->m_Size = m_NumberOfHiddenNeurons;
}
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.push_back(value[i]);
}
shark::Data<shark::RealVector> data;
data.element(0)=samples;
data = m_net.encode(data);
TargetSampleType target;
//target.SetSize(m_NumberOfHiddenNeurons);
for(unsigned int a = 0; a < m_NumberOfHiddenNeurons; ++a){
//target[a]=data.element(0)[a];
target=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_net.encode(data);
unsigned int id = startIndex;
target.SetSize(m_NumberOfHiddenNeurons);
for(const auto& p : data.elements()){
for(unsigned int a = 0; a < m_NumberOfHiddenNeurons; ++a){
target[a]=p[a];
//target.SetElement(a,p[a]);
}
//std::cout << p << std::endl;
targets->SetMeasurementVector(id,target);
++id;
}
}
} // namespace otb
#endif
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