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

added new parameter tied/untied for autoencoders

parent 039141fc
......@@ -57,7 +57,6 @@ void AutoencoderModel<TInputValue,AutoencoderType>::Train()
optimizer.step(error);
std::cout<<i<<" "<<optimizer.solution().value<<std::endl;
}
//std::cout<<optimizer.solution().value<<std::endl;
m_net.setParameterVector(optimizer.solution().point);
......@@ -111,7 +110,6 @@ void AutoencoderModel<TInputValue,AutoencoderType>::Load(const std::string & fil
m_net.read(ia);
ifs.close();
m_NumberOfHiddenNeurons = m_net.numberOfHiddenNeurons();
//this->m_Size = m_NumberOfHiddenNeurons;
}
......@@ -119,6 +117,7 @@ template <class TInputValue, class AutoencoderType>
typename AutoencoderModel<TInputValue,AutoencoderType>::TargetSampleType
AutoencoderModel<TInputValue,AutoencoderType>::DoPredict(const InputSampleType & value) const
{
std::cout << "SINGLE PIXEL " ;
shark::RealVector samples(value.Size());
for(size_t i = 0; i < value.Size();i++)
{
......@@ -157,17 +156,10 @@ void AutoencoderModel<TInputValue,AutoencoderType>
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;
++id;
}
}
......
......@@ -40,7 +40,7 @@ ImageDimensionalityReductionFilter<TInputImage, TOutputImage, TMaskImage>
this->SetNthOutput(0,TOutputImage::New());
this->SetNthOutput(1,ConfidenceImageType::New());
m_UseConfidenceMap = false;
m_BatchMode = false;
m_BatchMode = true;
}
template <class TInputImage, class TOutputImage, class TMaskImage>
......
......@@ -96,45 +96,32 @@ std::istream & binary_read(std::istream& stream, T& value){
template <class TInputValue, unsigned int MapDimension>
void SOMModel<TInputValue, MapDimension>::Save(const std::string & filename, const std::string & name)
{
//Ecriture
auto kwl = m_SOMMap->GetImageKeywordlist();
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());
inputIterator.GoToBegin();
std::ofstream ofs(filename+"2", std::ios::binary);
binary_write_string(ofs,"som");
binary_write(ofs,static_cast<int>(MapDimension));
SizeType size = m_SOMMap->GetLargestPossibleRegion().GetSize() ;
for (size_t i=0;i<MapDimension;i++){
itk::ImageRegionConstIterator<MapType> inputIterator(m_SOMMap,m_SOMMap->GetLargestPossibleRegion());
inputIterator.GoToBegin();
std::ofstream ofs(filename, std::ios::binary);
binary_write_string(ofs,"som");
binary_write(ofs,static_cast<int>(MapDimension));
SizeType size = m_SOMMap->GetLargestPossibleRegion().GetSize() ;
for (size_t i=0;i<MapDimension;i++){
binary_write(ofs,size[i]);
}
binary_write(ofs,inputIterator.Get().GetNumberOfElements());
while(!inputIterator.IsAtEnd()){
InputSampleType vect = inputIterator.Get();
for (size_t i=0;i<vect.GetNumberOfElements();i++){
binary_write(ofs,vect[i]);
}
binary_write(ofs,inputIterator.Get().GetNumberOfElements());
while(!inputIterator.IsAtEnd()){
InputSampleType vect = inputIterator.Get();
for (size_t i=0;i<vect.GetNumberOfElements();i++){
binary_write(ofs,vect[i]);
}
++inputIterator;
}
ofs.close();
}
ofs.close();
}
template <class TInputValue, unsigned int MapDimension>
void SOMModel<TInputValue, MapDimension>::Load(const std::string & filename, const std::string & name)
{
std::ifstream ifs(filename+"2", std::ios::binary);
std::ifstream ifs(filename, std::ios::binary);
/** Read the model key (should be som) */
char s[]=" ";
......
......@@ -136,9 +136,13 @@ private:
void InitAutoencoderParams();
void InitPCAParams();
void InitSOMParams();
void BeforeTrainAutoencoder(typename ListSampleType::Pointer trainingListSample, std::string modelPath);
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);
void BeforeTrainSOM(typename ListSampleType::Pointer trainingListSample, std::string modelPath);
......
......@@ -83,7 +83,7 @@ cbLearningApplicationBaseDR<TInputValue,TOutputValue>
if(modelName == "autoencoder")
{
#ifdef OTB_USE_SHARK
TrainAutoencoder<AutoencoderModelType>(trainingListSample,modelPath);
BeforeTrainAutoencoder(trainingListSample,modelPath);
#else
otbAppLogFATAL("Module SharkLearning is not installed. You should consider turning OTB_USE_SHARK on during cmake configuration.");
#endif
......
......@@ -21,6 +21,21 @@ cbLearningApplicationBaseDR<TInputValue,TOutputValue>
SetParameterDescription("model.autoencoder",
"This group of parameters allows setting Shark autoencoder parameters. "
);
//Tied Autoencoder
AddParameter(ParameterType_Choice, "model.autoencoder.istied",
"tied weighth <tied/untied>");
SetParameterDescription(
"model.autoencoder.istied",
"Parameter that determine if the weights are tied or not <tied/untied>");
AddChoice("model.autoencoder.istied.yes","Tied weigths");
AddChoice("model.autoencoder.istied.no","Untied weights");
//Number Of Iterations
AddParameter(ParameterType_Int, "model.autoencoder.nbiter",
"Maximum number of iterations during training");
......@@ -38,13 +53,41 @@ cbLearningApplicationBaseDR<TInputValue,TOutputValue>
"model.autoencoder.nbneuron",
"The number of neurons in the hidden layer.");
//normalization
AddParameter(ParameterType_Float, "model.autoencoder.normalizer", "Strength of the normalization");
SetParameterFloat("model.autoencoder.normalizer",0, false);
SetParameterDescription("model.autoencoder.normalizer",
"Strength of the L2 normalization used during training");
//Regularization
AddParameter(ParameterType_Float, "model.autoencoder.regularization", "Strength of the regularization");
SetParameterFloat("model.autoencoder.regularization",0, false);
SetParameterDescription("model.autoencoder.regularization",
"Strength of the L2 regularization used during training");
}
template <class TInputValue, class TOutputValue>
void
cbLearningApplicationBaseDR<TInputValue,TOutputValue>
::BeforeTrainAutoencoder(typename ListSampleType::Pointer trainingListSample,
std::string modelPath)
{
std::string TiedWeigth = GetParameterString("model.autoencoder.istied");
std::cout << TiedWeigth << std::endl;
if(TiedWeigth == "no")
{
TrainAutoencoder<AutoencoderModelType>(trainingListSample,modelPath);
}
if(TiedWeigth == "yes")
{
TrainAutoencoder<TiedAutoencoderModelType>(trainingListSample,modelPath);
}
if(TiedWeigth != "yes" && TiedWeigth != "no")
{
std::cerr << "istied : invalid choice <yes/no>" << std::endl;
}
}
template <class TInputValue, class TOutputValue>
template <typename autoencoderchoice>
void cbLearningApplicationBaseDR<TInputValue,TOutputValue>
......@@ -53,7 +96,7 @@ void cbLearningApplicationBaseDR<TInputValue,TOutputValue>
typename autoencoderchoice::Pointer dimredTrainer = autoencoderchoice::New();
dimredTrainer->SetNumberOfHiddenNeurons(GetParameterInt("model.autoencoder.nbneuron"));
dimredTrainer->SetNumberOfIterations(GetParameterInt("model.autoencoder.nbiter"));
dimredTrainer->SetRegularization(GetParameterFloat("model.autoencoder.normalizer"));
dimredTrainer->SetRegularization(GetParameterFloat("model.autoencoder.regularization"));
dimredTrainer->SetInputListSample(trainingListSample);
dimredTrainer->Train();
dimredTrainer->Save(modelPath);
......
......@@ -18,22 +18,6 @@ cbLearningApplicationBaseDR<TInputValue,TOutputValue>
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. "
);
AddChoice("model.som4d", "OTB SOM");
SetParameterDescription("model.som4d",
"This group of parameters allows setting SOM parameters. "
);
AddChoice("model.som5d", "OTB SOM");
SetParameterDescription("model.som5d",
"This group of parameters allows setting SOM parameters. "
);
AddParameter(ParameterType_Int, "model.som.dim","Dimension of the map");
SetParameterDescription("model.som.dim","Dimension of the SOM map.");
......@@ -120,7 +104,7 @@ cbLearningApplicationBaseDR<TInputValue,TOutputValue>
}
if(SomDim > 5 || SomDim < 2)
{
std::cerr << "invalid dimension" << std::endl;
std::cerr << "k : invalid dimension" << std::endl;
}
}
......
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