Commit 64af665b authored by Cédric Traizet's avatar Cédric Traizet

bug can't find appli

parent 2f6c48cd
......@@ -93,7 +93,7 @@ private:
FilterType::Pointer filter_dim_reduc;
ExtractROIFilterType::Pointer m_ExtractROIFilter;
//d
};
}
}
......
......@@ -6,6 +6,9 @@
#include "itkVariableLengthVector.h"
#include "otbShiftScaleSampleListFilter.h"
#include "otbStatisticsXMLFileReader.h"
//#include "AutoencoderModel.h"
#include "otbSharkUtils.h"
......@@ -29,39 +32,6 @@
#include "cbLearningApplicationBaseDR.h"
template<class AutoencoderModel>
AutoencoderModel trainAutoencoderModel(
shark::UnlabeledData<shark::RealVector> const& data,//the data to train with
std::size_t numHidden,//number of features in the autoencoder
std::size_t iterations, //number of iterations to optimize
double regularisation//strength of the regularisation
){
//create the model
std::size_t inputs = dataDimension(data);
AutoencoderModel model;
model.setStructure(inputs, numHidden);
initRandomUniform(model,-0.1*std::sqrt(1.0/inputs),0.1*std::sqrt(1.0/inputs));
//create the objective function
shark::LabeledData<shark::RealVector,shark::RealVector> trainSet(data,data);//labels identical to inputs
shark::SquaredLoss<shark::RealVector> loss;
shark::ErrorFunction error(trainSet, &model, &loss);
shark::TwoNormRegularizer regularizer(error.numberOfVariables());
error.setRegularizer(regularisation,&regularizer);
shark::IRpropPlusFull optimizer;
error.init();
optimizer.init(error);
std::cout<<"Optimizing model: "+model.name()<<std::endl;
for(std::size_t i = 0; i != iterations; ++i){
optimizer.step(error);
std::cout<<i<<" "<<optimizer.solution().value<<std::endl;
}
//std::cout<<optimizer.solution().value<<std::endl;
model.setParameterVector(optimizer.solution().point);
return model;
}
shark::Normalizer<shark::RealVector> trainNormalizer(const shark::UnlabeledData<shark::RealVector>& data)
{
bool removeMean = true;
......@@ -88,11 +58,24 @@ public:
itkTypeMacro(CbDimensionalityReductionTrainer, otb::Application);
typedef float ValueType;
typedef itk::VariableLengthVector<ValueType> InputSampleType;
typedef itk::Statistics::ListSample<InputSampleType> ListSampleType;
typedef Superclass::SampleType SampleType;
typedef Superclass::ListSampleType ListSampleType;
typedef Superclass::SampleImageType SampleImageType;
typedef double ValueType;
typedef itk::VariableLengthVector<ValueType> MeasurementType;
typedef otb::StatisticsXMLFileReader<SampleType> StatisticsReader;
typedef otb::Statistics::ShiftScaleSampleListFilter<ListSampleType, ListSampleType> ShiftScaleFilterType;
//typedef float ValueType;
//typedef itk::VariableLengthVector<ValueType> InputSampleType;
//typedef itk::Statistics::ListSample<InputSampleType> ListSampleType;
//typedef itk::VariableLengthVector<ValueType> MeasurementType;
typedef otb::MachineLearningModelFactory<ValueType, ValueType> ModelFactoryType;
......@@ -105,16 +88,7 @@ private:
{
SetName("CbDimensionalityReductionTrainer");
SetDescription("Trainer for the dimensionality reduction algorithms used in the cbDimensionalityReduction application.");
/*
AddParameter(ParameterType_InputVectorData, "train", "Name of the input training vector data");
SetParameterDescription("train","The vector data used for training.");
AddParameter(ParameterType_StringList, "feat", "Field names to be calculated."); //
SetParameterDescription("feat","List of field names in the input vector data used as features for training."); //
AddParameter(ParameterType_Int, "k","target dimension");
SetParameterDescription("k", "Dimension of the output feature vectors");
*/
AddParameter(ParameterType_Group, "io", "Input and output data");
SetParameterDescription("io", "This group of parameters allows setting input and output data.");
......@@ -124,6 +98,9 @@ private:
AddParameter(ParameterType_OutputFilename, "io.out", "Output model");
SetParameterDescription("io.out", "Output file containing the model estimated (.txt format).");
AddParameter(ParameterType_InputFilename, "io.stats", "Input XML image statistics file");
MandatoryOff("io.stats");
SetParameterDescription("io.stats", "XML file containing mean and variance of each feature.");
AddParameter(ParameterType_StringList, "feat", "Field names to be calculated."); //
SetParameterDescription("feat","List of field names in the input vector data used as features for training."); //
......@@ -153,7 +130,7 @@ private:
void DoExecute()
{
std::cout << "Appli !" << std::endl;
std::cout << "Appli Training!" << std::endl;
std::string shapefile = GetParameterString("io.vd");
......@@ -175,51 +152,37 @@ private:
}
input->PushBack(mv);
}
/*
std::cout << input << std::endl;
std::vector<shark::RealVector> features;
otb::Shark::ListSampleToSharkVector<ListSampleType>( input, features);
shark::Data<shark::RealVector> inputSamples = shark::createDataFromRange( features );
std::size_t numHidden= GetParameterInt("k");
std::size_t iterations = 100;
double regularisation = 0;
shark::Normalizer<shark::RealVector> normalizer = trainNormalizer(inputSamples);
inputSamples = normalizer(inputSamples);
// Statistics for shift/scale
MeasurementType meanMeasurementVector;
MeasurementType stddevMeasurementVector;
if (HasValue("io.stats") && IsParameterEnabled("io.stats"))
{
StatisticsReader::Pointer statisticsReader = StatisticsReader::New();
std::string XMLfile = GetParameterString("io.stats");
statisticsReader->SetFileName(XMLfile);
meanMeasurementVector = statisticsReader->GetStatisticVectorByName("mean");
stddevMeasurementVector = statisticsReader->GetStatisticVectorByName("stddev");
}
else
{
meanMeasurementVector.SetSize(nbFeatures);
meanMeasurementVector.Fill(0.);
stddevMeasurementVector.SetSize(nbFeatures);
stddevMeasurementVector.Fill(1.);
}
ShiftScaleFilterType::Pointer trainingShiftScaleFilter = ShiftScaleFilterType::New();
trainingShiftScaleFilter->SetInput(input);
trainingShiftScaleFilter->SetShifts(meanMeasurementVector);
trainingShiftScaleFilter->SetScales(stddevMeasurementVector);
trainingShiftScaleFilter->Update();
ListSampleType::Pointer trainingListSample= trainingShiftScaleFilter->GetOutput();
std::cout << "normalizer trained and training set normalized" << std::endl;
AutoencoderType net = trainAutoencoderModel<AutoencoderType>(inputSamples,numHidden,iterations,regularisation);
std::cout << "autoencoder trained !!!!" << std::endl;
// save the model to the file "net.model"
std::ofstream ofs("net.model");
shark::TextOutArchive oa(ofs);
net.write(oa);
ofs.close();
// save the model to the file "net.model"
std::ofstream norm_ofs("normalizer.model");
boost::archive::polymorphic_text_oarchive onorm(norm_ofs);
normalizer.write(onorm);
norm_ofs.close();
*/
std::cout << "Using a Machine learning model" << std::endl;
/*
AutoencoderModelType::Pointer dimredTrainer = AutoencoderModelType::New();
dimredTrainer->SetNumberOfHiddenNeurons(5);
dimredTrainer->SetNumberOfIterations(50);
dimredTrainer->SetRegularization(0.1);
dimredTrainer->SetInputListSample(input);
dimredTrainer->Train();
dimredTrainer->Save("net.model");
std::cout << "ok" << std::endl;
*/
this->Train(input,GetParameterString("io.out"));
this->Train(trainingListSample,GetParameterString("io.out"));
// d
}
......
......@@ -51,10 +51,14 @@ public:
protected:
AutoencoderModel(){};
private:
AutoencoderModel();
//~AutoencoderModel() 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:
AutoencoderType m_net;
unsigned int m_NumberOfHiddenNeurons;
unsigned int m_NumberOfIterations;
......
......@@ -14,6 +14,15 @@
namespace otb
{
template <class TInputValue, class AutoencoderType>
AutoencoderModel<TInputValue,AutoencoderType>::AutoencoderModel()
{
//this->m_IsRegressionSupported = true;
}
template <class TInputValue, class AutoencoderType>
void AutoencoderModel<TInputValue,AutoencoderType>::Train()
{
......
#ifndef DimensionalityReductionModel_h
#define DimensionalityReductionModel_h
#include "itkObject.h"
#include "itkListSample.h"
namespace otb
{
template <class TInputValue>
class DimensionalityReductionModel: public itk::Object
{
public:
typedef DimensionalityReductionModel Self;
typedef itk::Object Superclass;
typedef itk::SmartPointer<Self> Pointer;
typedef itk::SmartPointer<const Self> ConstPointer;
typedef TInputValue InputValueType;
typedef itk::VariableLengthVector<InputValueType> InputSampleType;
typedef itk::Statistics::ListSample<InputSampleType> InputListSampleType;
itkSetObjectMacro(InputListSample,InputListSampleType);
itkGetObjectMacro(InputListSample,InputListSampleType);
itkGetConstObjectMacro(InputListSample,InputListSampleType);
virtual void Save(const std::string & filename, const std::string & name="") = 0;
virtual void Load(const std::string & filename, const std::string & name="") = 0;
virtual void Train() = 0;
virtual void Dimensionality_reduction() = 0;
protected:
DimensionalityReductionModel(){};
typename InputListSampleType::Pointer m_InputListSample;
};
} // end namespace otb
//#ifndef OTB_MANUAL_INSTANTIATION
//#include "DimensionalityReductionModel.txx"
//#endif
#endif
......@@ -117,7 +117,7 @@ private:
#ifdef OTB_USE_SHARK
void InitAutoencoderParams();
template <class autoencoderchoice>
void TrainAutoencoder(typename ListSampleType::Pointer trainingListSample, std::string modelPath){
void TrainAutoencoder(typename ListSampleType::Pointer trainingListSample, std::string modelPath);/*{
// typename AutoencoderModelType::Pointer dimredTrainer = AutoencoderModelType::New();
typename autoencoderchoice::Pointer dimredTrainer = autoencoderchoice::New();
dimredTrainer->SetNumberOfHiddenNeurons(GetParameterInt("model.autoencoder.nbneuron"));
......@@ -126,7 +126,7 @@ private:
dimredTrainer->SetInputListSample(trainingListSample);
dimredTrainer->Train();
dimredTrainer->Save(modelPath);
}; // !!!!!!!!!!!!!!!!! How to declare this method body in the .txx ? (double template...)
}; // !!!!!!!!!!!!!!!!! How to declare this method body in the .txx ? (double template...) */
#endif
//@}
};
......
......@@ -44,13 +44,13 @@ cbLearningApplicationBaseDR<TInputValue,TOutputValue>
SetParameterDescription("model.autoencoder.normalizer",
"Strength of the L2 normalization used during training");
}
/*
template <class TInputValue, class TOutputValue>
template <typename autoencoderchoice>
void cbLearningApplicationBaseDR<TInputValue,TOutputValue>
::template < autoencoderchoice> TrainAutoencoder(typename ListSampleType::Pointer trainingListSample,std::string modelPath)
::TrainAutoencoder(typename ListSampleType::Pointer trainingListSample,std::string modelPath)
{
// typename AutoencoderModelType::Pointer dimredTrainer = AutoencoderModelType::New();
autoencoderchoice::Pointer dimredTrainer = autoencoderchoice::New();
typename autoencoderchoice::Pointer dimredTrainer = autoencoderchoice::New();
dimredTrainer->SetNumberOfHiddenNeurons(GetParameterInt("model.autoencoder.nbneuron"));
dimredTrainer->SetNumberOfIterations(GetParameterInt("model.autoencoder.nbiter"));
dimredTrainer->SetRegularization(GetParameterFloat("model.autoencoder.normalizer"));
......@@ -58,7 +58,7 @@ void cbLearningApplicationBaseDR<TInputValue,TOutputValue>
dimredTrainer->Train();
dimredTrainer->Save(modelPath);
}
*/
} //end namespace wrapper
} //end namespace otb
......
......@@ -9,15 +9,15 @@ template< class TImage, class AutoencoderModel, class NormalizerModel>
class ITK_EXPORT EncodeFilter:public itk::ImageToImageFilter< TImage, TImage >
{
public:
/** Standard class typedefs. */
// Standard class typedefs.
typedef EncodeFilter Self;
typedef itk::ImageToImageFilter< TImage, TImage > Superclass;
typedef itk::SmartPointer< Self > Pointer;
/** Method for creation through the object factory. */
// Method for creation through the object factory.
itkNewMacro(Self);
/** Run-time type information (and related methods). */
// Run-time type information (and related methods).
itkTypeMacro(EncodeFilter, ImageToImageFilter);
//void SetInputImage(const TImage* image);
......@@ -33,7 +33,7 @@ class ITK_EXPORT EncodeFilter:public itk::ImageToImageFilter< TImage, TImage >
AutoencoderModel GetAutoencoderModel();
NormalizerModel GetNormalizerModel();
/** Does the real work. */
// Does the real work.
virtual void GenerateOutputInformation();
virtual void BeforeThreadedGenerateData();
void ThreadedGenerateData(const typename TImage::RegionType &outputRegionForThread, unsigned int threadId) ITK_OVERRIDE;
......
Markdown is supported
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