Skip to content
Snippets Groups Projects
Commit 918ef4e8 authored by Jordi Inglada's avatar Jordi Inglada
Browse files

STYLE: rename new DimensionalityReduction applications

parent a0a00b98
No related branches found
No related tags found
No related merge requests found
...@@ -31,20 +31,20 @@ otb_create_application( ...@@ -31,20 +31,20 @@ otb_create_application(
LINK_LIBRARIES ${${otb-module}_LIBRARIES}) LINK_LIBRARIES ${${otb-module}_LIBRARIES})
OTB_CREATE_APPLICATION( OTB_CREATE_APPLICATION(
NAME CbDimensionalityReductionTrainer NAME TrainDimensionalityReduction
SOURCES cbDimensionalityReductionTrainer.cxx SOURCES otbTrainDimensionalityReduction.cxx
LINK_LIBRARIES ${${otb-module}_LIBRARIES} ${OTBCommon_LIBRARIES} ${OTBITK_LIBRARIES} ${OTBBoost_LIBRARIES} ${OTBShark_LIBRARIES} LINK_LIBRARIES ${${otb-module}_LIBRARIES} ${OTBCommon_LIBRARIES} ${OTBITK_LIBRARIES} ${OTBBoost_LIBRARIES} ${OTBShark_LIBRARIES}
) )
OTB_CREATE_APPLICATION( OTB_CREATE_APPLICATION(
NAME CbDimensionalityReduction NAME ImageDimensionalityReduction
SOURCES cbDimensionalityReduction.cxx SOURCES otbImageDimensionalityReduction.cxx
LINK_LIBRARIES ${${otb-module}_LIBRARIES} ${OTBCommon_LIBRARIES} ${OTBITK_LIBRARIES} ${OTBBoost_LIBRARIES} ${OTBShark_LIBRARIES} LINK_LIBRARIES ${${otb-module}_LIBRARIES} ${OTBCommon_LIBRARIES} ${OTBITK_LIBRARIES} ${OTBBoost_LIBRARIES} ${OTBShark_LIBRARIES}
) )
OTB_CREATE_APPLICATION( OTB_CREATE_APPLICATION(
NAME CbDimensionalityReductionVector NAME VectorDimensionalityReduction
SOURCES cbDimensionalityReductionVector.cxx SOURCES otbVectorDimensionalityReduction.cxx
LINK_LIBRARIES ${${otb-module}_LIBRARIES} ${OTBCommon_LIBRARIES} ${OTBITK_LIBRARIES} ${OTBBoost_LIBRARIES} ${OTBShark_LIBRARIES} LINK_LIBRARIES ${${otb-module}_LIBRARIES} ${OTBCommon_LIBRARIES} ${OTBITK_LIBRARIES} ${OTBBoost_LIBRARIES} ${OTBShark_LIBRARIES}
) )
#include "otbWrapperApplication.h"
#include "otbWrapperApplicationFactory.h"
#include "otbOGRDataSourceWrapper.h"
#include "otbOGRFeatureWrapper.h"
#include "itkVariableLengthVector.h"
#include "otbShiftScaleSampleListFilter.h"
#include "otbStatisticsXMLFileReader.h"
//#include "otbSharkUtils.h"
#include <fstream> // write the model file
#include "DimensionalityReductionModelFactory.h"
#include "cbLearningApplicationBaseDR.h"
namespace otb
{
namespace Wrapper
{
class CbDimensionalityReductionTrainer : public cbLearningApplicationBaseDR<float,float>
{
public:
typedef CbDimensionalityReductionTrainer Self;
typedef cbLearningApplicationBaseDR<float, float> Superclass;
typedef itk::SmartPointer<Self> Pointer;
typedef itk::SmartPointer<const Self> ConstPointer;
itkNewMacro(Self);
itkTypeMacro(CbDimensionalityReductionTrainer, otb::Application);
typedef Superclass::SampleType SampleType;
typedef Superclass::ListSampleType ListSampleType;
typedef Superclass::SampleImageType SampleImageType;
typedef float ValueType;
typedef itk::VariableLengthVector<ValueType> MeasurementType;
typedef otb::StatisticsXMLFileReader<SampleType> StatisticsReader;
typedef otb::Statistics::ShiftScaleSampleListFilter<ListSampleType, ListSampleType> ShiftScaleFilterType;
typedef otb::DimensionalityReductionModelFactory<ValueType, ValueType> ModelFactoryType;
private:
void DoInit()
{
SetName("CbDimensionalityReductionTrainer");
SetDescription("Trainer for the dimensionality reduction algorithms used in the cbDimensionalityReduction application.");
AddParameter(ParameterType_Group, "io", "Input and output data");
SetParameterDescription("io", "This group of parameters allows setting input and output data.");
AddParameter(ParameterType_InputVectorData, "io.vd", "Input Vector Data");
SetParameterDescription("io.vd", "Input geometries used for training (note : all geometries from the layer will be used)");
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."); //
Superclass::DoInit();
AddRAMParameter();
}
void DoUpdateParameters()
{
}
void DoExecute()
{
std::string shapefile = GetParameterString("io.vd");
otb::ogr::DataSource::Pointer source = otb::ogr::DataSource::New(shapefile, otb::ogr::DataSource::Modes::Read);
otb::ogr::Layer layer = source->GetLayer(0);
ListSampleType::Pointer input = ListSampleType::New();
const int nbFeatures = GetParameterStringList("feat").size();
input->SetMeasurementVectorSize(nbFeatures);
otb::ogr::Layer::const_iterator it = layer.cbegin();
otb::ogr::Layer::const_iterator itEnd = layer.cend();
for( ; it!=itEnd ; ++it)
{
MeasurementType mv;
mv.SetSize(nbFeatures);
for(int idx=0; idx < nbFeatures; ++idx)
{
mv[idx] = (*it)[GetParameterStringList("feat")[idx]].GetValue<double>();
}
input->PushBack(mv);
}
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();
this->Train(trainingListSample,GetParameterString("io.out"));
}
};
}
}
OTB_APPLICATION_EXPORT(otb::Wrapper::CbDimensionalityReductionTrainer)
...@@ -71,11 +71,11 @@ private: ...@@ -71,11 +71,11 @@ private:
namespace Wrapper namespace Wrapper
{ {
class CbDimensionalityReduction : public Application class ImageDimensionalityReduction : public Application
{ {
public: public:
/** Standard class typedefs. */ /** Standard class typedefs. */
typedef CbDimensionalityReduction Self; typedef ImageDimensionalityReduction Self;
typedef Application Superclass; typedef Application Superclass;
typedef itk::SmartPointer<Self> Pointer; typedef itk::SmartPointer<Self> Pointer;
typedef itk::SmartPointer<const Self> ConstPointer; typedef itk::SmartPointer<const Self> ConstPointer;
...@@ -83,7 +83,7 @@ public: ...@@ -83,7 +83,7 @@ public:
/** Standard macro */ /** Standard macro */
itkNewMacro(Self); itkNewMacro(Self);
itkTypeMacro(CbDimensionalityReduction, otb::Application); itkTypeMacro(ImageDimensionalityReduction, otb::Application);
/** Filters typedef */ /** Filters typedef */
typedef UInt8ImageType MaskImageType; typedef UInt8ImageType MaskImageType;
...@@ -104,10 +104,10 @@ public: ...@@ -104,10 +104,10 @@ public:
protected: protected:
~CbDimensionalityReduction() ITK_OVERRIDE ~ImageDimensionalityReduction() ITK_OVERRIDE
{ {
DimensionalityReductionModelFactoryType::CleanFactories(); DimensionalityReductionModelFactoryType::CleanFactories();
} }
private: private:
void DoInit() ITK_OVERRIDE void DoInit() ITK_OVERRIDE
...@@ -119,7 +119,7 @@ private: ...@@ -119,7 +119,7 @@ private:
SetDocName("DimensionalityReduction"); SetDocName("DimensionalityReduction");
SetDocLongDescription("This application reduces the dimension of an input" SetDocLongDescription("This application reduces the dimension of an input"
" image, based on a machine learning model file produced by" " image, based on a machine learning model file produced by"
" the DimensionalityReductionTrainer application. Pixels of the " " the TrainDimensionalityReduction application. Pixels of the "
"output image will contain the reduced values from" "output image will contain the reduced values from"
"the model. The input pixels" "the model. The input pixels"
" can be optionally centered and reduced according " " can be optionally centered and reduced according "
...@@ -132,7 +132,7 @@ private: ...@@ -132,7 +132,7 @@ private:
"Training application, it is mandatory to use the same " "Training application, it is mandatory to use the same "
"statistics file for reduction."); "statistics file for reduction.");
SetDocAuthors("OTB-Team"); SetDocAuthors("OTB-Team");
SetDocSeeAlso("DimensionalityReductionTrainer, ComputeImagesStatistics"); SetDocSeeAlso("TrainDimensionalityReduction, ComputeImagesStatistics");
AddDocTag(Tags::Learning); AddDocTag(Tags::Learning);
...@@ -146,27 +146,27 @@ private: ...@@ -146,27 +146,27 @@ private:
MandatoryOff("mask"); MandatoryOff("mask");
AddParameter(ParameterType_InputFilename, "model", "Model file"); AddParameter(ParameterType_InputFilename, "model", "Model file");
SetParameterDescription("model", "A regression model file (produced by " SetParameterDescription("model", "A dimensionality reduction model file (produced by "
"TrainRegression application)."); "TrainRegression application).");
AddParameter(ParameterType_InputFilename, "imstat", "Statistics file"); AddParameter(ParameterType_InputFilename, "imstat", "Statistics file");
SetParameterDescription("imstat", "A XML file containing mean and standard" SetParameterDescription("imstat", "A XML file containing mean and standard"
" deviation to center and reduce samples before prediction " " deviation to center and reduce samples before prediction "
"(produced by ComputeImagesStatistics application). If this file contains" "(produced by ComputeImagesStatistics application). If this file contains"
"one more band than the sample size, the last stat of last band will be" "one more bands than the sample size, the last stat of last band will be"
"applied to expand the output predicted value"); "applied to expand the output predicted value");
MandatoryOff("imstat"); MandatoryOff("imstat");
AddParameter(ParameterType_OutputImage, "out", "Output Image"); AddParameter(ParameterType_OutputImage, "out", "Output Image");
SetParameterDescription( "out", "Output image containing predicted values"); SetParameterDescription( "out", "Output image containing reduced values");
AddRAMParameter(); AddRAMParameter();
// Doc example parameter settings // Doc example parameter settings
SetDocExampleParameterValue("in", "QB_1_ortho.tif"); SetDocExampleParameterValue("in", "QB_1_ortho.tif");
SetDocExampleParameterValue("imstat", "EstimateImageStatisticsQB1.xml"); SetDocExampleParameterValue("imstat", "EstimateImageStatisticsQB1.xml");
SetDocExampleParameterValue("model", "clsvmModelQB1.svm"); SetDocExampleParameterValue("model", "clsvmModelQB1.model");
SetDocExampleParameterValue("out", "clLabeledImageQB1.tif"); SetDocExampleParameterValue("out", "ReducedImageQB1.tif");
} }
void DoUpdateParameters() ITK_OVERRIDE void DoUpdateParameters() ITK_OVERRIDE
...@@ -258,4 +258,4 @@ private: ...@@ -258,4 +258,4 @@ private:
} }
} }
OTB_APPLICATION_EXPORT(otb::Wrapper::CbDimensionalityReduction) OTB_APPLICATION_EXPORT(otb::Wrapper::ImageDimensionalityReduction)
#include "otbWrapperApplication.h"
#include "otbWrapperApplicationFactory.h"
#include "otbOGRDataSourceWrapper.h"
#include "otbOGRFeatureWrapper.h"
#include "itkVariableLengthVector.h"
#include "otbShiftScaleSampleListFilter.h"
#include "otbStatisticsXMLFileReader.h"
//#include "otbSharkUtils.h"
#include <fstream> // write the model file
#include "DimensionalityReductionModelFactory.h"
#include "cbLearningApplicationBaseDR.h"
namespace otb
{
namespace Wrapper
{
class TrainDimensionalityReduction : public cbLearningApplicationBaseDR<float,float>
{
public:
typedef TrainDimensionalityReduction Self;
typedef cbLearningApplicationBaseDR<float, float> Superclass;
typedef itk::SmartPointer<Self> Pointer;
typedef itk::SmartPointer<const Self> ConstPointer;
itkNewMacro(Self);
itkTypeMacro(TrainDimensionalityReduction, otb::Application);
typedef Superclass::SampleType SampleType;
typedef Superclass::ListSampleType ListSampleType;
typedef Superclass::SampleImageType SampleImageType;
typedef float ValueType;
typedef itk::VariableLengthVector<ValueType> MeasurementType;
typedef otb::StatisticsXMLFileReader<SampleType> StatisticsReader;
typedef otb::Statistics::ShiftScaleSampleListFilter<ListSampleType, ListSampleType> ShiftScaleFilterType;
typedef otb::DimensionalityReductionModelFactory<ValueType, ValueType> ModelFactoryType;
private:
void DoInit()
{
SetName("TrainDimensionalityReduction");
SetDescription("Trainer for the dimensionality reduction algorithms used in the ImageDimensionalityReduction and VectorDimensionalityReduction applications.");
AddParameter(ParameterType_Group, "io", "Input and output data");
SetParameterDescription("io", "This group of parameters allows setting input and output data.");
AddParameter(ParameterType_InputVectorData, "io.vd", "Input Vector Data");
SetParameterDescription("io.vd", "Input geometries used for training (note : all geometries from the layer will be used)");
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."); //
Superclass::DoInit();
AddRAMParameter();
}
void DoUpdateParameters()
{
}
void DoExecute()
{
std::string shapefile = GetParameterString("io.vd");
otb::ogr::DataSource::Pointer source = otb::ogr::DataSource::New(shapefile, otb::ogr::DataSource::Modes::Read);
otb::ogr::Layer layer = source->GetLayer(0);
ListSampleType::Pointer input = ListSampleType::New();
const int nbFeatures = GetParameterStringList("feat").size();
input->SetMeasurementVectorSize(nbFeatures);
otb::ogr::Layer::const_iterator it = layer.cbegin();
otb::ogr::Layer::const_iterator itEnd = layer.cend();
for( ; it!=itEnd ; ++it)
{
MeasurementType mv;
mv.SetSize(nbFeatures);
for(int idx=0; idx < nbFeatures; ++idx)
{
mv[idx] = (*it)[GetParameterStringList("feat")[idx]].GetValue<double>();
}
input->PushBack(mv);
}
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();
this->Train(trainingListSample,GetParameterString("io.out"));
}
};
}
}
OTB_APPLICATION_EXPORT(otb::Wrapper::TrainDimensionalityReduction)
...@@ -41,13 +41,13 @@ bool IsNotAlphaNum(char c) ...@@ -41,13 +41,13 @@ bool IsNotAlphaNum(char c)
return !std::isalnum(c); return !std::isalnum(c);
} }
class CbDimensionalityReductionVector : public Application class VectorDimensionalityReduction : public Application
{ {
public: public:
/** Standard class typedefs. */ /** Standard class typedefs. */
typedef CbDimensionalityReductionVector Self; typedef VectorDimensionalityReduction Self;
typedef Application Superclass; typedef Application Superclass;
typedef itk::SmartPointer<Self> Pointer; typedef itk::SmartPointer<Self> Pointer;
typedef itk::SmartPointer<const Self> ConstPointer; typedef itk::SmartPointer<const Self> ConstPointer;
...@@ -68,8 +68,8 @@ class CbDimensionalityReductionVector : public Application ...@@ -68,8 +68,8 @@ class CbDimensionalityReductionVector : public Application
typedef itk::VariableLengthVector<ValueType> MeasurementType; typedef itk::VariableLengthVector<ValueType> MeasurementType;
typedef otb::StatisticsXMLFileReader<MeasurementType> StatisticsReader; typedef otb::StatisticsXMLFileReader<MeasurementType> StatisticsReader;
typedef otb::Statistics::ShiftScaleSampleListFilter<ListSampleType, ListSampleType> ShiftScaleFilterType; typedef otb::Statistics::ShiftScaleSampleListFilter<ListSampleType, ListSampleType> ShiftScaleFilterType;
~CbDimensionalityReductionVector() ITK_OVERRIDE ~VectorDimensionalityReduction() ITK_OVERRIDE
{ {
DimensionalityReductionModelFactoryType::CleanFactories(); DimensionalityReductionModelFactoryType::CleanFactories();
} }
...@@ -81,8 +81,8 @@ class CbDimensionalityReductionVector : public Application ...@@ -81,8 +81,8 @@ class CbDimensionalityReductionVector : public Application
SetDescription("Performs dimensionality reduction of the input vector data according to a model file."); SetDescription("Performs dimensionality reduction of the input vector data according to a model file.");
SetDocName("Vector Dimensionality Reduction"); SetDocName("Vector Dimensionality Reduction");
SetDocAuthors("OTB-Team"); SetDocAuthors("OTB-Team");
SetDocLongDescription("This application performs a vector data dimensionality reduction based on a model file produced by the cbDimensionalityReductionTrainer application."); SetDocLongDescription("This application performs a vector data dimensionality reduction based on a model file produced by the TrainDimensionalityReduction application.");
SetDocSeeAlso("cbDimensionalityReductionTrainer"); SetDocSeeAlso("TrainDimensionalityReduction");
AddDocTag(Tags::Learning); AddDocTag(Tags::Learning);
AddParameter(ParameterType_InputVectorData, "in", "Name of the input vector data"); AddParameter(ParameterType_InputVectorData, "in", "Name of the input vector data");
...@@ -94,13 +94,13 @@ class CbDimensionalityReductionVector : public Application ...@@ -94,13 +94,13 @@ class CbDimensionalityReductionVector : public Application
MandatoryOff("instat"); MandatoryOff("instat");
AddParameter(ParameterType_InputFilename, "model", "Model file"); AddParameter(ParameterType_InputFilename, "model", "Model file");
SetParameterDescription("model", "A model file (produced by cbDimensionalityReduction application,"); SetParameterDescription("model", "A model file (produced by the TrainDimensionalityReduction application,");
AddParameter(ParameterType_ListView, "feat", "Field names to be calculated."); // AddParameter(ParameterType_ListView, "feat", "Field names to be calculated."); //
SetParameterDescription("feat","List of field names in the input vector data used as features for training."); // SetParameterDescription("feat","List of field names in the input vector data used as features for reduction."); //
AddParameter(ParameterType_StringList, "featout", "Field names to be calculated."); // AddParameter(ParameterType_StringList, "featout", "Field names to be calculated."); //
SetParameterDescription("featout","List of field names in the input vector data used as features for training."); // SetParameterDescription("featout","List of field names in the input vector data used as features for reduction."); //
AddParameter(ParameterType_OutputFilename, "out", "Output vector data file containing the reduced vector"); AddParameter(ParameterType_OutputFilename, "out", "Output vector data file containing the reduced vector");
SetParameterDescription("out","Output vector data file storing sample values (OGR format)." SetParameterDescription("out","Output vector data file storing sample values (OGR format)."
...@@ -388,4 +388,4 @@ class CbDimensionalityReductionVector : public Application ...@@ -388,4 +388,4 @@ class CbDimensionalityReductionVector : public Application
}; };
} }
} }
OTB_APPLICATION_EXPORT(otb::Wrapper::CbDimensionalityReductionVector) OTB_APPLICATION_EXPORT(otb::Wrapper::VectorDimensionalityReduction)
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