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

added code for saving a SOM as txt file, + some code cleaning

parent b187c9a6
......@@ -118,9 +118,6 @@ public:
*/
typename TargetListSampleType::Pointer PredictBatch(const InputListSampleType * input) const;
/** THIS METHOD IS DEPRECATED AND SHOULD NOT BE USED. */
void PredictAll();
/** Get the size of the output after dimensionality reduction */
virtual unsigned int GetDimension() = 0;
......@@ -149,13 +146,6 @@ public:
itkGetConstObjectMacro(InputListSample,InputListSampleType);
//@}
/**\name Classification output accessors */
//@{
/** Set the target labels (to be used before training) */
itkSetObjectMacro(TargetListSample,TargetListSampleType);
/** Get the target labels (to be used after PredictAll) */
itkGetObjectMacro(TargetListSample,TargetListSampleType);
//@}
......@@ -172,9 +162,6 @@ protected:
/** Input list sample */
typename InputListSampleType::Pointer m_InputListSample;
/** Target list sample */
typename TargetListSampleType::Pointer m_TargetListSample;
/** Is DoPredictBatch multi-threaded ? */
bool m_IsDoPredictBatchMultiThreaded;
......
......@@ -41,20 +41,6 @@ DimensionalityReductionModel<TInputValue,TOutputValue>
{}
template <class TInputValue, class TOutputValue>
void
DimensionalityReductionModel<TInputValue,TOutputValue>
::PredictAll()
{
itkWarningMacro("DimensionalityReductionModel::PredictAll() has been DEPRECATED. Use DimensionalityReductionModel::PredictBatch() instead.");
typename TargetListSampleType::Pointer targets = this->GetTargetListSample();
targets->Clear();
typename TargetListSampleType::Pointer tmpTargets = this->PredictBatch(this->GetInputListSample());
targets->Graft(tmpTargets);
}
template <class TInputValue, class TOutputValue>
typename DimensionalityReductionModel<TInputValue,TOutputValue>
......
......@@ -43,7 +43,10 @@ using TiedAutoencoderModelFactory = AutoencoderModelFactoryBase<TInputValue, TTa
template <class TInputValue, class TTargetValue>
using SOM3DModelFactory = SOMModelFactory<TInputValue, TTargetValue, 2> ;
using SOM2DModelFactory = SOMModelFactory<TInputValue, TTargetValue, 2> ;
template <class TInputValue, class TTargetValue>
using SOM3DModelFactory = SOMModelFactory<TInputValue, TTargetValue, 3> ;
template <class TInputValue, class TOutputValue>
typename DimensionalityReductionModel<TInputValue,TOutputValue>::Pointer
......@@ -103,6 +106,7 @@ DimensionalityReductionModelFactory<TInputValue,TOutputValue>
RegisterFactory(SOM3DModelFactory<TInputValue,TOutputValue>::New());
RegisterFactory(SOM2DModelFactory<TInputValue,TOutputValue>::New());
#ifdef OTB_USE_SHARK
RegisterFactory(PCAModelFactory<TInputValue,TOutputValue>::New());
......@@ -138,11 +142,19 @@ DimensionalityReductionModelFactory<TInputValue,TOutputValue>
{
// SOM
SOM3DModelFactory<TInputValue,TOutputValue> *somFactory =
SOM3DModelFactory<TInputValue,TOutputValue> *som3dFactory =
dynamic_cast<SOM3DModelFactory<TInputValue,TOutputValue> *>(*itFac);
if (somFactory)
if (som3dFactory)
{
itk::ObjectFactoryBase::UnRegisterFactory(som3dFactory);
continue;
}
SOM2DModelFactory<TInputValue,TOutputValue> *som2dFactory =
dynamic_cast<SOM2DModelFactory<TInputValue,TOutputValue> *>(*itFac);
if (som2dFactory)
{
itk::ObjectFactoryBase::UnRegisterFactory(somFactory);
itk::ObjectFactoryBase::UnRegisterFactory(som2dFactory);
continue;
}
#ifdef OTB_USE_SHARK
......
......@@ -134,6 +134,7 @@ void ImageDimensionalityReductionFilter<TInputImage, TOutputImage, TMaskImage>::
{
Superclass::GenerateOutputInformation();
this->GetOutput()->SetNumberOfComponentsPerPixel( m_Model->GetDimension() );
std::cout << m_Model->GetDimension() << std::endl;
}
......
......@@ -15,7 +15,7 @@
namespace otb
{
template <class TInputValue, unsigned int MapDimension>>
template <class TInputValue, unsigned int MapDimension>
class ITK_EXPORT SOMModel: public DimensionalityReductionModel<TInputValue,TInputValue>
{
......@@ -34,7 +34,7 @@ public:
typedef typename Superclass::TargetSampleType TargetSampleType;
typedef typename Superclass::TargetListSampleType TargetListSampleType;
typedef SOMMap<itk::VariableLengthVector<TInputValue>,itk::Statistics::EuclideanDistanceMetric<itk::VariableLengthVector<TInputValue>>, 3> MapType;
typedef SOMMap<itk::VariableLengthVector<TInputValue>,itk::Statistics::EuclideanDistanceMetric<itk::VariableLengthVector<TInputValue>>, MapDimension> MapType;
typedef typename MapType::SizeType SizeType;
typedef otb::SOM<InputListSampleType, MapType> EstimatorType;
......
......@@ -7,24 +7,30 @@
#include "itkMacro.h"
// test text file
#include "itkImageRegionIterator.h"
#include "itkImageRegionConstIterator.h"
#include <fstream>
namespace otb
{
template <class TInputValue>
SOMModel<TInputValue>::SOMModel()
template <class TInputValue, unsigned int MapDimension>
SOMModel<TInputValue, MapDimension>::SOMModel()
{
}
template <class TInputValue>
SOMModel<TInputValue>::~SOMModel()
template <class TInputValue, unsigned int MapDimension>
SOMModel<TInputValue, MapDimension>::~SOMModel()
{
}
template <class TInputValue>
void SOMModel<TInputValue>::Train()
template <class TInputValue, unsigned int MapDimension>
void SOMModel<TInputValue, MapDimension>::Train()
{
typename EstimatorType::Pointer estimator = EstimatorType::New();
......@@ -45,8 +51,8 @@ void SOMModel<TInputValue>::Train()
}
template <class TInputValue>
bool SOMModel<TInputValue>::CanReadFile(const std::string & filename)
template <class TInputValue, unsigned int MapDimension>
bool SOMModel<TInputValue, MapDimension>::CanReadFile(const std::string & filename)
{
try
{
......@@ -60,42 +66,91 @@ bool SOMModel<TInputValue>::CanReadFile(const std::string & filename)
}
template <class TInputValue>
bool SOMModel<TInputValue>::CanWriteFile(const std::string & filename)
template <class TInputValue, unsigned int MapDimension>
bool SOMModel<TInputValue, MapDimension>::CanWriteFile(const std::string & filename)
{
return true;
}
template <class TInputValue>
void SOMModel<TInputValue>::Save(const std::string & filename, const std::string & name)
template <class TInputValue, unsigned int MapDimension>
void SOMModel<TInputValue, MapDimension>::Save(const std::string & filename, const std::string & name)
{
std::cout << m_SOMMap->GetNumberOfComponentsPerPixel() << std::endl;
//Ecriture
auto kwl = m_SOMMap->GetImageKeywordlist();
//kwl.AddKey("MachineLearningModelType", "SOM");
//m_SOMMap->SetImageKeywordList(kwl);
kwl.AddKey("MachineLearningModelType", "SOM"+std::to_string(MapDimension));
m_SOMMap->SetImageKeywordList(kwl);
auto writer = otb::ImageFileWriter<MapType>::New();
writer->SetInput(m_SOMMap);
writer->SetFileName(filename);
writer->Update();
// test text
itk::ImageRegionConstIterator<MapType> inputIterator(m_SOMMap,m_SOMMap->GetLargestPossibleRegion());
std::ofstream ofs(filename+"2");
ofs << "SOM" << std::endl;
ofs << MapDimension << std::endl;
ofs << m_SOMMap->GetLargestPossibleRegion().GetSize() << std::endl;
ofs << inputIterator.Get().GetNumberOfElements() << std::endl;;
while(!inputIterator.IsAtEnd()){
InputSampleType vect = inputIterator.Get();
for (size_t i=0;i<vect.GetNumberOfElements();i++){
ofs << vect[i] << " " ;
}
ofs << std::endl;
++inputIterator;
}
ofs.close();
}
template <class TInputValue>
void SOMModel<TInputValue>::Load(const std::string & filename, const std::string & name)
template <class TInputValue, unsigned int MapDimension>
void SOMModel<TInputValue, MapDimension>::Load(const std::string & filename, const std::string & name)
{
auto reader = otb::ImageFileReader<MapType>::New();
reader->SetFileName(filename);
reader->Update();
//std::cout << reader->GetOutput()->GetImageKeywordlist().GetMetadataByKey("MachineLearningModelType") << '\n';
if (reader->GetOutput()->GetImageKeywordlist().GetMetadataByKey("MachineLearningModelType") != "SOM"+std::to_string(MapDimension)){
itkExceptionMacro(<< "Error opening " << filename.c_str() );
}
m_SOMMap = reader->GetOutput();
// test text
std::ifstream ifs(filename+"2");
std::string model_type_str;
std::string dimension_str;
std::string size_str;
std::string number_of_elements_str;
SizeType size;
std::getline(ifs,model_type_str);
std::getline(ifs,dimension_str);
std::getline(ifs,size_str);
std::getline(ifs,number_of_elements_str);
for (int i=0 ; i<MapDimension; i++)
{
}
while (!ifs.eof())
{
std::string line;
std::getline(ifs, line);
}
ifs.close();
std::cout << "model type " << model_type_str << std::endl;
std::cout << "dimension " << dimension_str << std::endl;
std::cout << "size " << size_str << std::endl;
}
template <class TInputValue>
typename SOMModel<TInputValue>::TargetSampleType
SOMModel<TInputValue>::DoPredict(const InputSampleType & value) const
template <class TInputValue, unsigned int MapDimension>
typename SOMModel<TInputValue, MapDimension>::TargetSampleType
SOMModel<TInputValue, MapDimension>::DoPredict(const InputSampleType & value) const
{
unsigned int dimension =MapType::ImageDimension;
TargetSampleType target;
......
......@@ -8,7 +8,7 @@
namespace otb
{
template <class TInputValue, class TTargetValue>
template <class TInputValue, class TTargetValue, unsigned int MapDimension>
class ITK_EXPORT SOMModelFactory : public itk::ObjectFactoryBase
{
public:
......
......@@ -28,8 +28,8 @@
namespace otb
{
template <class TInputValue, class TOutputValue>
SOMModelFactory<TInputValue,TOutputValue>::SOMModelFactory()
template <class TInputValue, class TOutputValue, unsigned int MapDimension>
SOMModelFactory<TInputValue,TOutputValue,MapDimension>::SOMModelFactory()
{
std::string classOverride = std::string("DimensionalityReductionModel");
......@@ -40,22 +40,22 @@ SOMModelFactory<TInputValue,TOutputValue>::SOMModelFactory()
"SOM DR Model",
1,
// itk::CreateObjectFunction<AutoencoderModel<TInputValue,TOutputValue> >::New());
itk::CreateObjectFunction<SOMModel<TInputValue>>::New());
itk::CreateObjectFunction<SOMModel<TInputValue, MapDimension>>::New());
}
template <class TInputValue, class TOutputValue>
SOMModelFactory<TInputValue,TOutputValue>::~SOMModelFactory()
template <class TInputValue, class TOutputValue, unsigned int MapDimension>
SOMModelFactory<TInputValue,TOutputValue,MapDimension>::~SOMModelFactory()
{
}
template <class TInputValue, class TOutputValue>
const char* SOMModelFactory<TInputValue,TOutputValue>::GetITKSourceVersion(void) const
template <class TInputValue, class TOutputValue, unsigned int MapDimension>
const char* SOMModelFactory<TInputValue,TOutputValue,MapDimension>::GetITKSourceVersion(void) const
{
return ITK_SOURCE_VERSION;
}
template <class TInputValue, class TOutputValue>
const char* SOMModelFactory<TInputValue,TOutputValue>::GetDescription() const
template <class TInputValue, class TOutputValue, unsigned int MapDimension>
const char* SOMModelFactory<TInputValue,TOutputValue,MapDimension>::GetDescription() const
{
return "SOM model factory";
}
......
......@@ -87,9 +87,11 @@ public:
// Dimensionality reduction models
typedef SOMMap<itk::VariableLengthVector<TInputValue>,itk::Statistics::EuclideanDistanceMetric<itk::VariableLengthVector<TInputValue>>, 3> MapType;
typedef otb::SOM<ListSampleType, MapType> EstimatorType;
typedef otb::SOMModel<InputValueType> SOMModelType;
typedef SOMMap<itk::VariableLengthVector<TInputValue>,itk::Statistics::EuclideanDistanceMetric<itk::VariableLengthVector<TInputValue>>, 3> Map3DType;
typedef otb::SOMModel<InputValueType, 3> SOM3DModelType;
typedef SOMMap<itk::VariableLengthVector<TInputValue>,itk::Statistics::EuclideanDistanceMetric<itk::VariableLengthVector<TInputValue>>, 2> Map2DType;
typedef otb::SOMModel<InputValueType, 2> SOM2DModelType;
#ifdef OTB_USE_SHARK
typedef shark::Autoencoder< shark::TanhNeuron, shark::LinearNeuron> AutoencoderType;
......@@ -130,6 +132,7 @@ private:
template <class autoencoderchoice>
void TrainAutoencoder(typename ListSampleType::Pointer trainingListSample, std::string modelPath);
void TrainPCA(typename ListSampleType::Pointer trainingListSample, std::string modelPath);
template <class somchoice>
void TrainSOM(typename ListSampleType::Pointer trainingListSample, std::string modelPath);
#endif
//@}
......
......@@ -101,9 +101,15 @@ cbLearningApplicationBaseDR<TInputValue,TOutputValue>
if(modelName == "som")
{
TrainSOM(trainingListSample,modelPath);
TrainSOM<SOM2DModelType >(trainingListSample,modelPath);
}
if(modelName == "autoencoder")
if(modelName == "som3d")
{
TrainSOM<SOM3DModelType >(trainingListSample,modelPath);
}
if(modelName == "autoencoder")
{
#ifdef OTB_USE_SHARK
TrainAutoencoder<AutoencoderModelType>(trainingListSample,modelPath);
......
......@@ -14,12 +14,16 @@ cbLearningApplicationBaseDR<TInputValue,TOutputValue>
::InitSOMParams()
{
AddChoice("model.som", "OTB SOM");
SetParameterDescription("model.som",
"This group of parameters allows setting SOM parameters. "
);
AddChoice("model.som3d", "OTB SOM");
SetParameterDescription("model.som3d",
"This group of parameters allows setting SOM parameters. "
);
AddParameter(ParameterType_StringList , "model.som.s", "Size");
SetParameterDescription("model.som.s", "Size of the SOM map");
MandatoryOff("model.som.s");
......@@ -73,31 +77,29 @@ cbLearningApplicationBaseDR<TInputValue,TOutputValue>
}
template <class TInputValue, class TOutputValue>
template <typename somchoice>
void cbLearningApplicationBaseDR<TInputValue,TOutputValue>
::TrainSOM(typename ListSampleType::Pointer trainingListSample,std::string modelPath)
{
//std::cout << std::stoi(s[0]) << std::endl;
typename SOMModelType::Pointer dimredTrainer = SOMModelType::New();
using TemplateEstimatorType = typename somchoice::EstimatorType;
typename somchoice::Pointer dimredTrainer = somchoice::New();
unsigned int dim = dimredTrainer->GetDimension();
dimredTrainer->SetNumberOfIterations(GetParameterInt("model.som.ni"));
dimredTrainer->SetBetaInit(GetParameterFloat("model.som.bi"));
dimredTrainer->SetBetaEnd(GetParameterFloat("model.som.bf"));
dimredTrainer->SetMaxWeight(GetParameterFloat("model.som.iv"));
std::cout << "0" << std::endl;
typename EstimatorType::SizeType size;
typename TemplateEstimatorType::SizeType size;
std::vector<std::basic_string<char>> s= GetParameterStringList("model.som.s");
for (int i=0; i<3; i++){ // This will be templated later (the 3)
for (int i=0; i<dim; i++){ // This will be templated later (the 3)
size[i]=std::stoi(s[i]);
}
dimredTrainer->SetMapSize(size);
std::cout << "1" << std::endl;
typename EstimatorType::SizeType radius;
typename TemplateEstimatorType::SizeType radius;
std::vector<std::basic_string<char>> n= GetParameterStringList("model.som.n");
for (int i=0; i<3; i++){ // This will be templated later (the 3)
for (int i=0; i<dim; i++){ // This will be templated later (the 3)
radius[i]=std::stoi(n[i]);
}
std::cout << "2" << std::endl;
dimredTrainer->SetNeighborhoodSizeInit(radius);
dimredTrainer->SetListSample(trainingListSample);
dimredTrainer->Train();
......
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