diff --git a/Modules/Applications/AppDimensionalityReduction/app/CMakeLists.txt b/Modules/Applications/AppDimensionalityReduction/app/CMakeLists.txt index 92ad8038f0e8b451ccdcbea0a7eae081014833d5..75baabb957bec12e0c5b4a7f3b20351bf49d6266 100644 --- a/Modules/Applications/AppDimensionalityReduction/app/CMakeLists.txt +++ b/Modules/Applications/AppDimensionalityReduction/app/CMakeLists.txt @@ -31,20 +31,20 @@ otb_create_application( LINK_LIBRARIES ${${otb-module}_LIBRARIES}) OTB_CREATE_APPLICATION( - NAME CbDimensionalityReductionTrainer - SOURCES cbDimensionalityReductionTrainer.cxx + NAME TrainDimensionalityReduction + SOURCES otbTrainDimensionalityReduction.cxx LINK_LIBRARIES ${${otb-module}_LIBRARIES} ${OTBCommon_LIBRARIES} ${OTBITK_LIBRARIES} ${OTBBoost_LIBRARIES} ${OTBShark_LIBRARIES} ) OTB_CREATE_APPLICATION( - NAME CbDimensionalityReduction - SOURCES cbDimensionalityReduction.cxx + NAME ImageDimensionalityReduction + SOURCES otbImageDimensionalityReduction.cxx LINK_LIBRARIES ${${otb-module}_LIBRARIES} ${OTBCommon_LIBRARIES} ${OTBITK_LIBRARIES} ${OTBBoost_LIBRARIES} ${OTBShark_LIBRARIES} ) OTB_CREATE_APPLICATION( - NAME CbDimensionalityReductionVector - SOURCES cbDimensionalityReductionVector.cxx + NAME VectorDimensionalityReduction + SOURCES otbVectorDimensionalityReduction.cxx LINK_LIBRARIES ${${otb-module}_LIBRARIES} ${OTBCommon_LIBRARIES} ${OTBITK_LIBRARIES} ${OTBBoost_LIBRARIES} ${OTBShark_LIBRARIES} ) diff --git a/Modules/Applications/AppDimensionalityReduction/app/cbDimensionalityReductionTrainer.cxx b/Modules/Applications/AppDimensionalityReduction/app/cbDimensionalityReductionTrainer.cxx deleted file mode 100644 index 4cb042427b0288b1da73b4149afbab4b152fecce..0000000000000000000000000000000000000000 --- a/Modules/Applications/AppDimensionalityReduction/app/cbDimensionalityReductionTrainer.cxx +++ /dev/null @@ -1,146 +0,0 @@ -#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) diff --git a/Modules/Applications/AppDimensionalityReduction/app/cbDimensionalityReduction.cxx b/Modules/Applications/AppDimensionalityReduction/app/otbImageDimensionalityReduction.cxx similarity index 89% rename from Modules/Applications/AppDimensionalityReduction/app/cbDimensionalityReduction.cxx rename to Modules/Applications/AppDimensionalityReduction/app/otbImageDimensionalityReduction.cxx index de6408ebcb313fc3badfa27a585b125ecbb5964d..a4d00030febf68d7e1489f637371f6474ae2fcc6 100644 --- a/Modules/Applications/AppDimensionalityReduction/app/cbDimensionalityReduction.cxx +++ b/Modules/Applications/AppDimensionalityReduction/app/otbImageDimensionalityReduction.cxx @@ -71,11 +71,11 @@ private: namespace Wrapper { -class CbDimensionalityReduction : public Application +class ImageDimensionalityReduction : public Application { public: /** Standard class typedefs. */ - typedef CbDimensionalityReduction Self; + typedef ImageDimensionalityReduction Self; typedef Application Superclass; typedef itk::SmartPointer<Self> Pointer; typedef itk::SmartPointer<const Self> ConstPointer; @@ -83,7 +83,7 @@ public: /** Standard macro */ itkNewMacro(Self); - itkTypeMacro(CbDimensionalityReduction, otb::Application); + itkTypeMacro(ImageDimensionalityReduction, otb::Application); /** Filters typedef */ typedef UInt8ImageType MaskImageType; @@ -104,10 +104,10 @@ public: protected: - ~CbDimensionalityReduction() ITK_OVERRIDE - { + ~ImageDimensionalityReduction() ITK_OVERRIDE + { DimensionalityReductionModelFactoryType::CleanFactories(); - } + } private: void DoInit() ITK_OVERRIDE @@ -119,7 +119,7 @@ private: SetDocName("DimensionalityReduction"); SetDocLongDescription("This application reduces the dimension of an input" " 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" "the model. The input pixels" " can be optionally centered and reduced according " @@ -132,7 +132,7 @@ private: "Training application, it is mandatory to use the same " "statistics file for reduction."); SetDocAuthors("OTB-Team"); - SetDocSeeAlso("DimensionalityReductionTrainer, ComputeImagesStatistics"); + SetDocSeeAlso("TrainDimensionalityReduction, ComputeImagesStatistics"); AddDocTag(Tags::Learning); @@ -146,27 +146,27 @@ private: MandatoryOff("mask"); AddParameter(ParameterType_InputFilename, "model", "Model file"); - SetParameterDescription("model", "A regression model file (produced by " - "TrainRegression application)."); + SetParameterDescription("model", "A dimensionality reduction model file (produced by " + "TrainRegression application)."); AddParameter(ParameterType_InputFilename, "imstat", "Statistics file"); SetParameterDescription("imstat", "A XML file containing mean and standard" " deviation to center and reduce samples before prediction " "(produced by ComputeImagesStatistics application). If this file contains" - "one more band than the sample size, the last stat of last band will be" - "applied to expand the output predicted value"); + "one more bands than the sample size, the last stat of last band will be" + "applied to expand the output predicted value"); MandatoryOff("imstat"); AddParameter(ParameterType_OutputImage, "out", "Output Image"); - SetParameterDescription( "out", "Output image containing predicted values"); + SetParameterDescription( "out", "Output image containing reduced values"); AddRAMParameter(); // Doc example parameter settings SetDocExampleParameterValue("in", "QB_1_ortho.tif"); SetDocExampleParameterValue("imstat", "EstimateImageStatisticsQB1.xml"); - SetDocExampleParameterValue("model", "clsvmModelQB1.svm"); - SetDocExampleParameterValue("out", "clLabeledImageQB1.tif"); + SetDocExampleParameterValue("model", "clsvmModelQB1.model"); + SetDocExampleParameterValue("out", "ReducedImageQB1.tif"); } void DoUpdateParameters() ITK_OVERRIDE @@ -258,4 +258,4 @@ private: } } -OTB_APPLICATION_EXPORT(otb::Wrapper::CbDimensionalityReduction) +OTB_APPLICATION_EXPORT(otb::Wrapper::ImageDimensionalityReduction) diff --git a/Modules/Applications/AppDimensionalityReduction/app/otbTrainDimensionalityReduction.cxx b/Modules/Applications/AppDimensionalityReduction/app/otbTrainDimensionalityReduction.cxx new file mode 100644 index 0000000000000000000000000000000000000000..5f94e918b360dbc1cf3245ccbcc90d70ef9bfca9 --- /dev/null +++ b/Modules/Applications/AppDimensionalityReduction/app/otbTrainDimensionalityReduction.cxx @@ -0,0 +1,146 @@ +#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) diff --git a/Modules/Applications/AppDimensionalityReduction/app/cbDimensionalityReductionVector.cxx b/Modules/Applications/AppDimensionalityReduction/app/otbVectorDimensionalityReduction.cxx similarity index 95% rename from Modules/Applications/AppDimensionalityReduction/app/cbDimensionalityReductionVector.cxx rename to Modules/Applications/AppDimensionalityReduction/app/otbVectorDimensionalityReduction.cxx index 12e1307ad112a8e3a71252a352f029fa0dfd72ca..8036bcb32f3d852f1d3f11a9f45f58f1e3a2853b 100644 --- a/Modules/Applications/AppDimensionalityReduction/app/cbDimensionalityReductionVector.cxx +++ b/Modules/Applications/AppDimensionalityReduction/app/otbVectorDimensionalityReduction.cxx @@ -41,13 +41,13 @@ bool IsNotAlphaNum(char c) return !std::isalnum(c); } -class CbDimensionalityReductionVector : public Application +class VectorDimensionalityReduction : public Application { public: /** Standard class typedefs. */ - typedef CbDimensionalityReductionVector Self; - typedef Application Superclass; + typedef VectorDimensionalityReduction Self; + typedef Application Superclass; typedef itk::SmartPointer<Self> Pointer; typedef itk::SmartPointer<const Self> ConstPointer; @@ -68,8 +68,8 @@ class CbDimensionalityReductionVector : public Application typedef itk::VariableLengthVector<ValueType> MeasurementType; typedef otb::StatisticsXMLFileReader<MeasurementType> StatisticsReader; typedef otb::Statistics::ShiftScaleSampleListFilter<ListSampleType, ListSampleType> ShiftScaleFilterType; - ~CbDimensionalityReductionVector() ITK_OVERRIDE - { + ~VectorDimensionalityReduction() ITK_OVERRIDE + { DimensionalityReductionModelFactoryType::CleanFactories(); } @@ -81,8 +81,8 @@ class CbDimensionalityReductionVector : public Application SetDescription("Performs dimensionality reduction of the input vector data according to a model file."); SetDocName("Vector Dimensionality Reduction"); SetDocAuthors("OTB-Team"); - SetDocLongDescription("This application performs a vector data dimensionality reduction based on a model file produced by the cbDimensionalityReductionTrainer application."); - SetDocSeeAlso("cbDimensionalityReductionTrainer"); + SetDocLongDescription("This application performs a vector data dimensionality reduction based on a model file produced by the TrainDimensionalityReduction application."); + SetDocSeeAlso("TrainDimensionalityReduction"); AddDocTag(Tags::Learning); AddParameter(ParameterType_InputVectorData, "in", "Name of the input vector data"); @@ -94,13 +94,13 @@ class CbDimensionalityReductionVector : public Application MandatoryOff("instat"); 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."); // - 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."); // - 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"); SetParameterDescription("out","Output vector data file storing sample values (OGR format)." @@ -388,4 +388,4 @@ class CbDimensionalityReductionVector : public Application }; } } -OTB_APPLICATION_EXPORT(otb::Wrapper::CbDimensionalityReductionVector) +OTB_APPLICATION_EXPORT(otb::Wrapper::VectorDimensionalityReduction)