Skip to content
Snippets Groups Projects
Commit c3e7e8f5 authored by Ludovic Hussonnois's avatar Ludovic Hussonnois
Browse files

BUG: Add a workaround to check the model name before serialization.

This workaround is added to avoid a crash in application when build
is made with RelWithDebInfo which is caused by a
boost::archive_exception impossible to be catch.
This workaround break the compatibility with shark model direct
read/write.
It is still possible to read old RF model from OTB.
parent c0d7df26
No related branches found
No related tags found
No related merge requests found
......@@ -195,6 +195,8 @@ SharkRandomForestsMachineLearningModel<TInputValue,TOutputValue>
{
itkExceptionMacro(<< "Error opening " << filename.c_str() );
}
// Add comment with model file name
ofs << "#" << m_RFModel.name() << std::endl;
shark::TextOutArchive oa(ofs);
m_RFModel.save(oa,0);
}
......@@ -205,8 +207,25 @@ SharkRandomForestsMachineLearningModel<TInputValue,TOutputValue>
::Load(const std::string & filename, const std::string & itkNotUsed(name))
{
std::ifstream ifs(filename.c_str());
shark::TextInArchive ia(ifs);
m_RFModel.load(ia,0);
if( ifs.good() )
{
// Check if the first line is a comment and verify the name of the model in this case.
std::string line;
getline( ifs, line );
if( line.at( 0 ) == '#' )
{
if( line.find( m_RFModel.name() ) == std::string::npos )
itkExceptionMacro( "The model file : " + filename + " cannot be read." );
}
else
{
// rewind if first line is not a comment
ifs.clear();
ifs.seekg( 0, std::ios::beg );
}
shark::TextInArchive ia( ifs );
m_RFModel.load( ia, 0 );
}
}
template <class TInputValue, class TOutputValue>
......
......@@ -30,6 +30,7 @@
#pragma GCC diagnostic ignored "-Wignored-qualifiers"
#endif
#include "otb_shark.h"
#include "otbSharkUtils.h"
#include "shark/Algorithms/Trainers/NormalizeComponentsUnitVariance.h" //normalize
#include "shark/Algorithms/KMeans.h" //k-means algorithm
......@@ -188,9 +189,8 @@ SharkKMeansMachineLearningModel<TInputValue, TOutputValue>
{
itkExceptionMacro( << "Error opening " << filename.c_str());
}
ofs << "#" << m_ClusteringModel->name() << std::endl;
shark::TextOutArchive oa( ofs );
std::string name = m_ClusteringModel->name();
oa << name;
m_ClusteringModel->save( oa, 1 );
}
......@@ -203,6 +203,7 @@ SharkKMeansMachineLearningModel<TInputValue, TOutputValue>
std::ifstream ifs( filename.c_str());
if(ifs.good())
{
// Check if first line contains model name
std::string line;
std::getline(ifs, line);
m_CanRead = line.find(m_ClusteringModel->name()) != std::string::npos;
......@@ -211,12 +212,8 @@ SharkKMeansMachineLearningModel<TInputValue, TOutputValue>
if(!m_CanRead)
return;
// Go to the start of the file
ifs.seekg(0, std::ios::beg);
shark::TextInArchive ia( ifs );
std::string name;
ia & name;
m_ClusteringModel->load( ia, 1 );
m_ClusteringModel->load( ia, 0 );
ifs.close();
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment