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

ENH: export centroids using the (new) method in SharkKMeanMachineLearningModel...

ENH: export centroids using the (new) method in SharkKMeanMachineLearningModel instead of the method in the app
No related merge requests found
Showing with 12 additions and 67 deletions
+12 -67
...@@ -81,22 +81,18 @@ protected: ...@@ -81,22 +81,18 @@ protected:
SetDefaultParameterInt("maxit", 1000); SetDefaultParameterInt("maxit", 1000);
MandatoryOff("maxit"); MandatoryOff("maxit");
AddParameter( ParameterType_Group, "inmeans", "Input centroids parameters" ); AddParameter( ParameterType_Group, "incentroids", "Input centroids parameters" );
SetParameterDescription( "inmeans", SetParameterDescription( "incentroids",
"Group of parameters for used defined input centroids." ); "Group of parameters for used defined input centroids." );
AddParameter(ParameterType_InputFilename, "inmeans.in", "input centroids"); AddParameter(ParameterType_InputFilename, "incentroids.in", "input centroids text file");
SetParameterDescription("inmeans.in", "Input text file containing centroid posistions."); SetParameterDescription("incentroids.in", "Input text file containing centroid posistions.");
MandatoryOff("inmeans.in"); MandatoryOff("incentroids.in");
AddParameter(ParameterType_Bool, "inmeans.normalize", "Normalize input centroids"); AddParameter(ParameterType_Bool, "incentroids.normalize", "Normalize input centroids");
SetParameterDescription("inmeans.normalize", "Normalize input centroids using the image statistics" SetParameterDescription("incentroids.normalize", "Normalize input centroids using the image statistics"
" computed during the execution of the application"); " computed during the execution of the application");
SetDefaultParameterInt("inmeans.normalize", true); SetDefaultParameterInt("incentroids.normalize", true);
AddParameter(ParameterType_OutputFilename, "outmeans", "Centroid filename");
SetParameterDescription("outmeans", "Output text file containing centroid positions");
MandatoryOff("outmeans");
ShareKMSamplingParameters(); ShareKMSamplingParameters();
ConnectKMSamplingParams(); ConnectKMSamplingParams();
...@@ -112,6 +108,7 @@ protected: ...@@ -112,6 +108,7 @@ protected:
{ {
ShareParameter("ram", "polystats.ram"); ShareParameter("ram", "polystats.ram");
ShareParameter("sampler", "select.sampler"); ShareParameter("sampler", "select.sampler");
ShareParameter("outcentroids", "training.classifier.sharkkm.outcentroids");
ShareParameter("vm", "polystats.mask", "Validity Mask", ShareParameter("vm", "polystats.mask", "Validity Mask",
"Validity mask, only non-zero pixels will be used to estimate KMeans modes."); "Validity mask, only non-zero pixels will be used to estimate KMeans modes.");
} }
...@@ -261,11 +258,11 @@ protected: ...@@ -261,11 +258,11 @@ protected:
GetParameterInt("maxit")); GetParameterInt("maxit"));
GetInternalApplication("training")->SetParameterInt("classifier.sharkkm.k", GetInternalApplication("training")->SetParameterInt("classifier.sharkkm.k",
GetParameterInt("nc")); GetParameterInt("nc"));
if(IsParameterEnabled("inmeans.in") && HasValue("inmeans.in")) if(IsParameterEnabled("incentroids.in") && HasValue("incentroids.in"))
{ {
GetInternalApplication("training")->SetParameterString("classifier.sharkkm.centroids", GetInternalApplication("training")->SetParameterString("classifier.sharkkm.centroids",
GetParameterString("inmeans.in")); GetParameterString("incentroids.in"));
if(GetParameterInt("inmeans.normalize")) if(GetParameterInt("incentroids.normalize"))
GetInternalApplication("training")->SetParameterString("classifier.sharkkm.centroidstats", GetInternalApplication("training")->SetParameterString("classifier.sharkkm.centroidstats",
GetInternalApplication("imgstats")->GetParameterString("out")); GetInternalApplication("imgstats")->GetParameterString("out"));
} }
...@@ -298,55 +295,6 @@ protected: ...@@ -298,55 +295,6 @@ protected:
ExecuteInternal( "classif" ); ExecuteInternal( "classif" );
} }
void CreateOutMeansFile(FloatVectorImageType *image,
const std::string &modelFileName,
unsigned int nbClasses)
{
if (IsParameterEnabled("outmeans"))
{
unsigned int nbBands = image->GetNumberOfComponentsPerPixel();
unsigned int nbElements = nbClasses * nbBands;
// get the line in model file that contains the centroids positions
std::ifstream infile(modelFileName);
if(!infile)
{
itkExceptionMacro(<< "File: " << modelFileName << " couldn't be opened");
}
// get the line with the centroids (starts with "2 ")
std::string line, centroidLine;
while(std::getline(infile,line))
{
if (line.size() > 2 && line[0] == '2' && line[1] == ' ')
{
centroidLine = line;
break;
}
}
std::vector<std::string> centroidElm;
boost::split(centroidElm,centroidLine,boost::is_any_of(" "));
// remove the first elements, not the centroids positions
int nbWord = centroidElm.size();
int beginCentroid = nbWord-nbElements;
centroidElm.erase(centroidElm.begin(), centroidElm.begin()+beginCentroid);
// write in the output file
std::ofstream outfile;
outfile.open(GetParameterString("outmeans"));
for (unsigned int i = 0; i < nbClasses; i++)
{
for (unsigned int j = 0; j < nbBands; j++)
{
outfile << std::setw(8) << centroidElm[i * nbBands + j] << " ";
}
outfile << std::endl;
}
}
}
class KMeansFileNamesHandler class KMeansFileNamesHandler
{ {
public: public:
...@@ -517,9 +465,6 @@ private: ...@@ -517,9 +465,6 @@ private:
// Compute a classification of the input image according to a model file // Compute a classification of the input image according to a model file
Superclass::KMeansClassif(); Superclass::KMeansClassif();
// Create the output text file containing centroids positions
Superclass::CreateOutMeansFile(GetParameterImage("in"), fileNames.modelFile, GetParameterInt("nc"));
// Remove all tempory files // Remove all tempory files
if( GetParameterInt( "cleanup" ) ) if( GetParameterInt( "cleanup" ) )
{ {
......
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