Commit 484c5dc7 authored by Charles Peyrega's avatar Charles Peyrega

ENH: Replacing the 3 SVM classification applications to handle the OpenCV...

ENH: Replacing the 3 SVM classification applications to handle the OpenCV machine learning framework
parent 837d6a38
......@@ -2,22 +2,10 @@ OTB_CREATE_APPLICATION(NAME ComputeImagesStatistics
SOURCES otbComputeImagesStatistics.cxx
LINK_LIBRARIES OTBIO;OTBCommon;OTBBasicFilters)
OTB_CREATE_APPLICATION(NAME ImageSVMClassifier
SOURCES otbImageSVMClassifier.cxx
LINK_LIBRARIES OTBIO;OTBCommon;OTBBasicFilters;OTBFeatureExtraction;OTBLearning)
OTB_CREATE_APPLICATION(NAME KMeansClassification
SOURCES otbKMeansClassification.cxx
LINK_LIBRARIES OTBIO;OTBCommon;OTBBasicFilters;OTBFeatureExtraction;OTBLearning)
OTB_CREATE_APPLICATION(NAME TrainSVMImagesClassifier
SOURCES otbTrainSVMImagesClassifier.cxx
LINK_LIBRARIES OTBIO;OTBCommon;OTBBasicFilters;OTBFeatureExtraction;OTBLearning)
OTB_CREATE_APPLICATION(NAME ValidateSVMImagesClassifier
SOURCES otbValidateSVMImagesClassifier.cxx
LINK_LIBRARIES OTBIO;OTBCommon;OTBBasicFilters;OTBFeatureExtraction;OTBLearning)
OTB_CREATE_APPLICATION(NAME SOMClassification
SOURCES otbSOMClassification.cxx
LINK_LIBRARIES OTBIO;OTBCommon;OTBBasicFilters;OTBFeatureExtraction;OTBLearning)
......@@ -35,17 +23,17 @@ OTB_CREATE_APPLICATION(NAME ClassificationMapRegularization
LINK_LIBRARIES OTBIO;OTBCommon;OTBBasicFilters)
IF(OTB_USE_OPENCV)
OTB_CREATE_APPLICATION(NAME TrainImagesClassifier
SOURCES otbTrainImagesClassifier.cxx otbTrainSVM.cxx otbTrainLibSVM.cxx otbTrainBoost.cxx
otbTrainDecisionTree.cxx otbTrainGradientBoostedTree.cxx otbTrainNeuralNetwork.cxx otbTrainNormalBayes.cxx
otbTrainRandomForests.cxx otbTrainKNN.cxx
LINK_LIBRARIES OTBIO;OTBCommon;OTBBasicFilters;OTBFeatureExtraction;OTBLearning;OTBMachineLearning)
OTB_CREATE_APPLICATION(NAME ImageClassifier
SOURCES otbImageClassifier.cxx
LINK_LIBRARIES OTBIO;OTBCommon;OTBBasicFilters;OTBFeatureExtraction;OTBLearning;OTBMachineLearning)
OTB_CREATE_APPLICATION(NAME ValidateImagesClassifier
SOURCES otbValidateImagesClassifier.cxx
LINK_LIBRARIES OTBIO;OTBCommon;OTBBasicFilters;OTBFeatureExtraction;OTBLearning;OTBMachineLearning)
OTB_CREATE_APPLICATION(NAME TrainImagesClassifier
SOURCES otbTrainImagesClassifier.cxx otbTrainSVM.cxx otbTrainLibSVM.cxx otbTrainBoost.cxx
otbTrainDecisionTree.cxx otbTrainGradientBoostedTree.cxx otbTrainNeuralNetwork.cxx otbTrainNormalBayes.cxx
otbTrainRandomForests.cxx otbTrainKNN.cxx
LINK_LIBRARIES OTBIO;OTBCommon;OTBBasicFilters;OTBFeatureExtraction;OTBLearning;OTBMachineLearning)
OTB_CREATE_APPLICATION(NAME ImageClassifier
SOURCES otbImageClassifier.cxx
LINK_LIBRARIES OTBIO;OTBCommon;OTBBasicFilters;OTBFeatureExtraction;OTBLearning;OTBMachineLearning)
OTB_CREATE_APPLICATION(NAME ValidateImagesClassifier
SOURCES otbValidateImagesClassifier.cxx
LINK_LIBRARIES OTBIO;OTBCommon;OTBBasicFilters;OTBFeatureExtraction;OTBLearning;OTBMachineLearning)
ENDIF()
......@@ -46,10 +46,10 @@ private:
SetName("ComputeImagesStatistics");
SetDescription("Computes global mean and standard deviation for each band from a set of images and optionally saves the results in an XML file.");
SetDocName("Compute Images second order statistics");
SetDocLongDescription("This application computes a global mean and standard deviation for each band of a set of images and optionally saves the results in an XML file. The output XML is intended to be used an input for the TrainImagesSVMClassifier application to normalize samples before learning.");
SetDocLongDescription("This application computes a global mean and standard deviation for each band of a set of images and optionally saves the results in an XML file. The output XML is intended to be used an input for the TrainImagesClassifier application to normalize samples before learning.");
SetDocLimitations("Each image of the set must contain the same bands as the others (i.e. same types, in the same order).");
SetDocAuthors("OTB-Team");
SetDocSeeAlso("Documentation of the TrainImagesSVMClassifier application.");
SetDocSeeAlso("Documentation of the TrainImagesClassifier application.");
AddDocTag(Tags::Learning);
AddDocTag(Tags::Analysis);
......
......@@ -110,7 +110,7 @@ private:
"-In case of number of votes equality, the UNDECIDED label is attributed to the pixel.");
SetDocLimitations("None");
SetDocAuthors("OTB-Team");
SetDocSeeAlso("SVMImagesClassifier application");
SetDocSeeAlso("ImageClassifier application");
AddDocTag(Tags::Learning);
AddDocTag(Tags::Analysis);
......
......@@ -69,7 +69,7 @@ private:
SetDocName("Image Classification");
SetDocLongDescription("This application performs an image classification based on a model file (*.txt extension) produced by the TrainImagesClassifier application. Pixels of the output image will contain the class label decided by the classifier. The input pixels can be optionnaly centered and reduced according to the statistics file produced by the ComputeImagesStatistics application. An optional input mask can be provided, in which case only input image pixels whose corresponding mask value is greater than 0 will be classified. The remaining of pixels will be given the label 0 in the output image.");
SetDocLimitations("The input image must have the same type, order and number of bands than the images used to produce the statistics file and the SVM model file. If a statistics file was used during training by the TrainSVMImagesClassifier, it is mandatory to use the same statistics file for classification. If an input mask is used, its size must match the input image size.");
SetDocLimitations("The input image must have the same type, order and number of bands than the images used to produce the statistics file and the SVM model file. If a statistics file was used during training by the TrainImagesClassifier, it is mandatory to use the same statistics file for classification. If an input mask is used, its size must match the input image size.");
SetDocAuthors("OTB-Team");
SetDocSeeAlso("TrainImagesClassifier, ValidateImagesClassifier, ComputeImagesStatistics");
......
/*=========================================================================
Program: ORFEO Toolbox
Language: C++
Date: $Date$
Version: $Revision$
Copyright (c) Centre National d'Etudes Spatiales. All rights reserved.
See OTBCopyright.txt for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notices for more information.
=========================================================================*/
#include "otbWrapperApplication.h"
#include "otbWrapperApplicationFactory.h"
#include "itkVariableLengthVector.h"
#include "otbChangeLabelImageFilter.h"
#include "otbStandardWriterWatcher.h"
#include "otbStatisticsXMLFileReader.h"
#include "otbShiftScaleVectorImageFilter.h"
#include "otbSVMImageClassificationFilter.h"
#include "otbMultiToMonoChannelExtractROI.h"
#include "otbImageToVectorImageCastFilter.h"
namespace otb
{
namespace Wrapper
{
class ImageSVMClassifier : public Application
{
public:
/** Standard class typedefs. */
typedef ImageSVMClassifier Self;
typedef Application Superclass;
typedef itk::SmartPointer<Self> Pointer;
typedef itk::SmartPointer<const Self> ConstPointer;
/** Standard macro */
itkNewMacro(Self);
itkTypeMacro(ImageSVMClassifier, otb::Application);
/** Filters typedef */
typedef itk::VariableLengthVector<FloatVectorImageType::InternalPixelType> MeasurementType;
typedef otb::StatisticsXMLFileReader<MeasurementType> StatisticsReader;
typedef otb::ShiftScaleVectorImageFilter<FloatVectorImageType, FloatVectorImageType> RescalerType;
typedef otb::SVMImageClassificationFilter<FloatVectorImageType, UInt8ImageType> ClassificationFilterType;
typedef ClassificationFilterType::Pointer ClassificationFilterPointerType;
typedef ClassificationFilterType::ModelType ModelType;
typedef ModelType::Pointer ModelPointerType;
private:
void DoInit()
{
SetName("ImageSVMClassifier");
SetDescription("Performs a SVM classification of the input image according to a SVM model file.");
// Documentation
SetDocName("Image SVM Classification");
SetDocLongDescription("This application performs a SVM image classification based on a SVM model file (*.svm extension) produced by the TrainSVMImagesClassifier application. Pixels of the output image will contain the class label decided by the SVM classifier. The input pixels can be optionnaly centered and reduced according to the statistics file produced by the ComputeImagesStatistics application. An optional input mask can be provided, in which case only input image pixels whose corresponding mask value is greater than 0 will be classified. The remaining of pixels will be given the label 0 in the output image.");
SetDocLimitations("The input image must have the same type, order and number of bands than the images used to produce the statistics file and the SVM model file. If a statistics file was used during training by the TrainSVMImagesClassifier, it is mandatory to use the same statistics file for classification. If an input mask is used, its size must match the input image size.");
SetDocAuthors("OTB-Team");
SetDocSeeAlso("TrainSVMImagesClassifier, ValidateSVMImagesClassifier, ComputeImagesStatistics");
AddDocTag(Tags::Learning);
AddParameter(ParameterType_InputImage, "in", "Input Image");
SetParameterDescription( "in", "The input image to classify.");
AddParameter(ParameterType_InputImage, "mask", "Input Mask");
SetParameterDescription( "mask", "The mask allows to restrict classification of the input image to the area where mask pixel values are greater than 0.");
MandatoryOff("mask");
AddParameter(ParameterType_InputFilename, "svm", "SVM Model file");
SetParameterDescription("svm", "A SVM model file (*.svm extension, produced by TrainSVMImagesClassifier application).");
AddParameter(ParameterType_InputFilename, "imstat", "Statistics file");
SetParameterDescription("imstat", "A XML file containing mean and standard deviation to center and reduce samples before classification (produced by ComputeImagesStatistics application).");
MandatoryOff("imstat");
AddParameter(ParameterType_OutputImage, "out", "Output Image");
SetParameterDescription( "out", "Output image containing class labels");
SetParameterOutputImagePixelType( "out", ImagePixelType_uint8);
AddRAMParameter();
// Doc example parameter settings
SetDocExampleParameterValue("in", "QB_1_ortho.tif");
SetDocExampleParameterValue("imstat", "EstimateImageStatisticsQB1.xml");
SetDocExampleParameterValue("svm", "clsvmModelQB1.svm");
SetDocExampleParameterValue("out", "clLabeledImageQB1.tif");
}
void DoUpdateParameters()
{
// Nothing to do here : all parameters are independent
}
void DoExecute()
{
// Load input image
FloatVectorImageType::Pointer inImage = GetParameterImage("in");
inImage->UpdateOutputInformation();
// Load svm model
otbAppLogINFO("Loading SVM model");
m_ModelSVM = ModelType::New();
m_ModelSVM->LoadModel(GetParameterString("svm").c_str());
otbAppLogINFO("SVM model loaded");
// Normalize input image (optional)
StatisticsReader::Pointer statisticsReader = StatisticsReader::New();
MeasurementType meanMeasurementVector;
MeasurementType stddevMeasurementVector;
m_Rescaler = RescalerType::New();
// Classify
m_ClassificationFilter = ClassificationFilterType::New();
m_ClassificationFilter->SetModel(m_ModelSVM);
// Normalize input image if asked
if(IsParameterEnabled("imstat") )
{
otbAppLogINFO("Input image normalization activated.");
// Load input image statistics
statisticsReader->SetFileName(GetParameterString("imstat"));
meanMeasurementVector = statisticsReader->GetStatisticVectorByName("mean");
stddevMeasurementVector = statisticsReader->GetStatisticVectorByName("stddev");
otbAppLogINFO( "mean used: " << meanMeasurementVector );
otbAppLogINFO( "standard deviation used: " << stddevMeasurementVector );
// Rescale vector image
m_Rescaler->SetScale(stddevMeasurementVector);
m_Rescaler->SetShift(meanMeasurementVector);
m_Rescaler->SetInput(inImage);
m_ClassificationFilter->SetInput(m_Rescaler->GetOutput());
}
else
{
otbAppLogINFO("Input image normalization deactivated.");
m_ClassificationFilter->SetInput(inImage);
}
if(IsParameterEnabled("mask"))
{
otbAppLogINFO("Using input mask");
// Load mask image and cast into LabeledImageType
UInt8ImageType::Pointer inMask = GetParameterUInt8Image("mask");
m_ClassificationFilter->SetInputMask(inMask);
}
SetParameterOutputImage<UInt8ImageType>("out", m_ClassificationFilter->GetOutput());
}
ClassificationFilterType::Pointer m_ClassificationFilter;
ModelPointerType m_ModelSVM;
RescalerType::Pointer m_Rescaler;
};
}
}
OTB_APPLICATION_EXPORT(otb::Wrapper::ImageSVMClassifier)
/*=========================================================================
Program: ORFEO Toolbox
Language: C++
Date: $Date$
Version: $Revision$
Copyright (c) Centre National d'Etudes Spatiales. All rights reserved.
See OTBCopyright.txt for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notices for more information.
=========================================================================*/
#include "otbWrapperApplication.h"
#include "otbWrapperApplicationFactory.h"
#include <iostream>
#include "otbConfigurationFile.h"
//Image
#include "otbImage.h"
#include "otbVectorImage.h"
#include "otbVectorData.h"
#include "otbListSampleGenerator.h"
// ListSample
#include "itkListSample.h"
#include "itkVariableLengthVector.h"
#include "itkFixedArray.h"
// SVM estimator
#include "otbSVMSampleListModelEstimator.h"
// Statistic XML Reader
#include "otbStatisticsXMLFileReader.h"
// Validation
#include "otbSVMClassifier.h"
#include "otbConfusionMatrixCalculator.h"
#include "itkTimeProbe.h"
#include "otbStandardFilterWatcher.h"
// Normalize the samples
#include "otbShiftScaleSampleListFilter.h"
// List sample concatenation
#include "otbConcatenateSampleListFilter.h"
// Balancing ListSample
#include "otbListSampleToBalancedListSampleFilter.h"
// VectorData projection filter
#include "otbVectorDataProjectionFilter.h"
// Extract a ROI of the vectordata
#include "otbVectorDataIntoImageProjectionFilter.h"
// Elevation handler
#include "otbWrapperElevationParametersHandler.h"
namespace otb
{
namespace Wrapper
{
class TrainSVMImagesClassifier: public Application
{
public:
/** Standard class typedefs. */
typedef TrainSVMImagesClassifier Self;
typedef Application Superclass;
typedef itk::SmartPointer<Self> Pointer;
typedef itk::SmartPointer<const Self> ConstPointer;
/** Standard macro */
itkNewMacro(Self);
itkTypeMacro(TrainSVMImagesClassifier, otb::Application);
typedef otb::Image<FloatVectorImageType::InternalPixelType, 2> ImageReaderType;
typedef FloatVectorImageType::PixelType PixelType;
typedef FloatVectorImageType VectorImageType;
typedef FloatImageType ImageType;
// Training vectordata
typedef itk::VariableLengthVector<ImageType::PixelType> MeasurementType;
// SampleList manipulation
typedef otb::ListSampleGenerator<VectorImageType, VectorDataType> ListSampleGeneratorType;
typedef ListSampleGeneratorType::ListSampleType ListSampleType;
typedef ListSampleGeneratorType::LabelType LabelType;
typedef ListSampleGeneratorType::ListLabelType LabelListSampleType;
typedef otb::Statistics::ConcatenateSampleListFilter<ListSampleType> ConcatenateListSampleFilterType;
typedef otb::Statistics::ConcatenateSampleListFilter<LabelListSampleType> ConcatenateLabelListSampleFilterType;
// Statistic XML file Reader
typedef otb::StatisticsXMLFileReader<MeasurementType> StatisticsReader;
// Enhance List Sample typedef otb::Statistics::ListSampleToBalancedListSampleFilter<ListSampleType, LabelListSampleType> BalancingListSampleFilterType;
typedef otb::Statistics::ShiftScaleSampleListFilter<ListSampleType, ListSampleType> ShiftScaleFilterType;
// SVM Estimator
typedef otb::Functor::VariableLengthVectorToMeasurementVectorFunctor<MeasurementType> MeasurementVectorFunctorType;
typedef otb::SVMSampleListModelEstimator<ListSampleType, LabelListSampleType, MeasurementVectorFunctorType>
SVMEstimatorType;
typedef otb::SVMClassifier<ListSampleType, LabelType::ValueType> ClassifierType;
// Estimate performance on validation sample
typedef otb::ConfusionMatrixCalculator<LabelListSampleType, LabelListSampleType> ConfusionMatrixCalculatorType;
typedef ClassifierType::OutputType ClassifierOutputType;
// VectorData projection filter
typedef otb::VectorDataProjectionFilter<VectorDataType, VectorDataType> VectorDataProjectionFilterType;
// Extract ROI
typedef otb::VectorDataIntoImageProjectionFilter<VectorDataType, VectorImageType> VectorDataReprojectionType;
private:
void DoInit()
{
SetName("TrainSVMImagesClassifier");
SetDescription("Train a SVM classifier from multiple pairs of images and training vector data.");
// Documentation
SetDocName("Train SVM classifier from multiple images");
SetDocLongDescription("This application performs SVM classifier training from multiple pairs of input images and training vector data. Samples are composed of pixel values in each band optionally centered and reduced using XML statistics file produced by the ComputeImagesStatistics application.\n The training vector data must contain polygons with a positive integer field representing the class label. Name of the field can be set using the \"Class label field\" parameter. Training and validation sample lists are built such that each class is equally represented in both lists. One parameter allows to control the ratio between the number of samples in training and validation sets. Two parameters allow to manage the size of the training and validation sets per class and per image.\n Several SVM classifier parameters can be set, such as the kernel function which defines the feature space (for example if the kernel is a Gaussian radial basis function, the corresponding feature space is an infinite-dimensional space). To allow some flexibility in the classification, SVM models have a cost parameter, C, that controls the trade-off between allowing training errors and forcing rigid margins. It creates a soft margin that permits some misclassifications. Increasing the value of C increases the cost of misclassifying points and forces the creation of a more accurate model that may not generalize well. Classifier parameters can also be optimized. In the validation process, the confusion matrix is organized the following way: rows = reference labels, columns = produced labels.");
SetDocLimitations("None");
SetDocAuthors("OTB-Team");
SetDocSeeAlso(" ");
AddDocTag(Tags::Learning);
//Group IO
AddParameter(ParameterType_Group,"io","Input and output data");
SetParameterDescription("io","This group of parameters allows to set input and output data.");
AddParameter(ParameterType_InputImageList, "io.il", "Input Image List");
SetParameterDescription("io.il", "A list of input images.");
AddParameter(ParameterType_InputVectorDataList, "io.vd", "Vector Data List");
SetParameterDescription("io.vd", "A list of vector data to select the training samples.");
AddParameter(ParameterType_InputFilename, "io.imstat", "XML image statistics file");
MandatoryOff("io.imstat");
SetParameterDescription("io.imstat", "Filename of an XML file containing mean and standard deviation of input images.");
AddParameter(ParameterType_OutputFilename, "io.out", "Output SVM model");
SetParameterDescription("io.out", "Output file containing the SVM model estimated");
// Elevation
ElevationParametersHandler::AddElevationParameters(this, "elev");
//Group Sample list
AddParameter(ParameterType_Group,"sample","Training and validation samples parameters");
SetParameterDescription("sample","This group of parameters allows to set training and validation sample lists parameters.");
AddParameter(ParameterType_Int, "sample.mt", "Maximum training sample size");
//MandatoryOff("mt");
SetDefaultParameterInt("sample.mt", 1000);
SetParameterDescription("sample.mt", "Maximum size of the training sample list (default = 1000).");
AddParameter(ParameterType_Int, "sample.mv", "Maximum validation sample size");
// MandatoryOff("mv");
SetDefaultParameterInt("sample.mv", 1000);
SetParameterDescription("sample.mv", "Maximum size of the validation sample list (default = 1000)");
AddParameter(ParameterType_Empty, "sample.edg", "On edge pixel inclusion");
SetParameterDescription("sample.edg", "Take pixels on polygon edge into consideration when building training and validation samples.");
MandatoryOff("sample.edg");
AddParameter(ParameterType_Float, "sample.vtr", "training and validation sample ratio");
SetParameterDescription("sample.vtr",
"Ratio between training and validation samples (0.0 = all training, 1.0 = all validation) default = 0.5.");
SetParameterFloat("sample.vtr", 0.5);
AddParameter(ParameterType_String, "sample.vfn", "Name of the discrimination field");
SetParameterDescription("sample.vfn", "Name of the field used to discriminate class in the vector data files.");
SetParameterString("sample.vfn", "Class");
//Group SVM
AddParameter(ParameterType_Group,"svm","SVM classifier parameters");
SetParameterDescription("svm","This group of parameters allows to set SVM classifier parameters.");
AddParameter(ParameterType_Choice, "svm.k", "SVM Kernel Type");
AddChoice("svm.k.linear", "Linear");
AddChoice("svm.k.rbf", "Gaussian radial basis function");
AddChoice("svm.k.poly", "Polynomial");
AddChoice("svm.k.sigmoid", "Sigmoid");
SetParameterString("svm.k", "linear");
SetParameterDescription("svm.k", "SVM Kernel Type.");
AddParameter(ParameterType_Float, "svm.c", "Cost parameter C.");
SetParameterFloat("svm.c", 1.0);
SetParameterDescription("svm.c", "SVM models have a cost parameter C (1 by default) to control the trade-off between training errors and forcing rigid margins.");
AddParameter(ParameterType_Empty, "svm.opt", "parameters optimization");
MandatoryOff("svm.opt");
SetParameterDescription("svm.opt", "SVM optimization flag");
AddRANDParameter();
// Doc example parameter settings
SetDocExampleParameterValue("io.il", "QB_1_ortho.tif");
SetDocExampleParameterValue("io.vd", "VectorData_QB1.shp");
SetDocExampleParameterValue("io.imstat", "EstimateImageStatisticsQB1.xml");
SetDocExampleParameterValue("sample.mv", "100");
SetDocExampleParameterValue("sample.mt", "100");
SetDocExampleParameterValue("sample.vtr", "0.5");
SetDocExampleParameterValue("svm.opt", "true");
SetDocExampleParameterValue("io.out", "svmModelQB1.svm");
}
void DoUpdateParameters()
{
// Nothing to do here : all parameters are independent
}
void LogConfusionMatrix(ConfusionMatrixCalculatorType* confMatCalc)
{
ConfusionMatrixCalculatorType::ConfusionMatrixType matrix = confMatCalc->GetConfusionMatrix();
// Compute minimal width
size_t minwidth = 0;
for (unsigned int i = 0; i < matrix.Rows(); i++)
{
for (unsigned int j = 0; j < matrix.Cols(); j++)
{
std::ostringstream os;
os << matrix(i,j);
size_t size = os.str().size();
if (size > minwidth)
{
minwidth = size;
}
}
}
typedef std::map<int, ConfusionMatrixCalculatorType::ClassLabelType> MapOfIndicesType;
MapOfIndicesType mapOfIndices = confMatCalc->GetMapOfIndices();
MapOfIndicesType::const_iterator it = mapOfIndices.begin();
MapOfIndicesType::const_iterator end = mapOfIndices.end();
for(; it != end; ++it)
{
std::ostringstream os;
os << "[" << it->second << "]";
size_t size = os.str().size();
if (size > minwidth)
{
minwidth = size;
}
}
// Generate matrix string, with 'minwidth' as size specifier
std::ostringstream os;
// Header line
for (size_t i = 0; i < minwidth; ++i)
os << " ";
os << " ";
it = mapOfIndices.begin();
end = mapOfIndices.end();
for(; it != end; ++it)
{
os << "[" << it->second << "]" << " ";
}
os << std::endl;
// Each line of confusion matrix
for (unsigned int i = 0; i < matrix.Rows(); i++)
{
ConfusionMatrixCalculatorType::ClassLabelType label = mapOfIndices[i];
os << "[" << std::setw(minwidth - 2) << label << "]" << " ";
for (unsigned int j = 0; j < matrix.Cols(); j++)
{
os << std::setw(minwidth) << matrix(i,j) << " ";
}
os << std::endl;
}
otbAppLogINFO("Confusion matrix (rows = reference labels, columns = produced labels):\n" << os.str());
}
void DoExecute()
{
GetLogger()->Debug("Entering DoExecute\n");
//Create training and validation for list samples and label list samples
ConcatenateLabelListSampleFilterType::Pointer
concatenateTrainingLabels = ConcatenateLabelListSampleFilterType::New();
ConcatenateListSampleFilterType::Pointer concatenateTrainingSamples = ConcatenateListSampleFilterType::New();
ConcatenateLabelListSampleFilterType::Pointer
concatenateValidationLabels = ConcatenateLabelListSampleFilterType::New();
ConcatenateListSampleFilterType::Pointer concatenateValidationSamples = ConcatenateListSampleFilterType::New();
MeasurementType meanMeasurementVector;
MeasurementType stddevMeasurementVector;
//--------------------------
// Load measurements from images
unsigned int nbBands = 0;
//Iterate over all input images
FloatVectorImageListType* imageList = GetParameterImageList("io.il");
VectorDataListType* vectorDataList = GetParameterVectorDataList("io.vd");
vdreproj = VectorDataReprojectionType::New();
//Iterate over all input images
for (unsigned int imgIndex = 0; imgIndex < imageList->Size(); ++imgIndex)
{
FloatVectorImageType::Pointer image = imageList->GetNthElement(imgIndex);
image->UpdateOutputInformation();
if (imgIndex == 0)
{
nbBands = image->GetNumberOfComponentsPerPixel();
}
// read the Vectordata
VectorDataType::Pointer vectorData = vectorDataList->GetNthElement(imgIndex);
vectorData->Update();
vdreproj->SetInputImage(image);
vdreproj->SetInput(vectorData);
vdreproj->SetUseOutputSpacingAndOriginFromImage(false);
// Setup the DEM Handler
otb::Wrapper::ElevationParametersHandler::SetupDEMHandlerFromElevationParameters(this,"elev");
vdreproj->Update();
//Sample list generator
ListSampleGeneratorType::Pointer sampleGenerator = ListSampleGeneratorType::New();
sampleGenerator->SetInput(image);
sampleGenerator->SetInputVectorData(vdreproj->GetOutput());
sampleGenerator->SetClassKey(GetParameterString("sample.vfn"));
sampleGenerator->SetMaxTrainingSize(GetParameterInt("sample.mt"));
sampleGenerator->SetMaxValidationSize(GetParameterInt("sample.mv"));
sampleGenerator->SetValidationTrainingProportion(GetParameterFloat("sample.vtr"));
// take pixel located on polygon edge into consideration
if (IsParameterEnabled("sample.edg"))
{
sampleGenerator->SetPolygonEdgeInclusion(true);
}
sampleGenerator->Update();
//Concatenate training and validation samples from the image
concatenateTrainingLabels->AddInput(sampleGenerator->GetTrainingListLabel());
concatenateTrainingSamples->AddInput(sampleGenerator->GetTrainingListSample());
concatenateValidationLabels->AddInput(sampleGenerator->GetValidationListLabel());
concatenateValidationSamples->AddInput(sampleGenerator->GetValidationListSample());
}
// Update
concatenateTrainingSamples->Update();
concatenateTrainingLabels->Update();
concatenateValidationSamples->Update();
concatenateValidationLabels->Update();
if (concatenateTrainingSamples->GetOutputSampleList()->Size() == 0)
{
otbAppLogFATAL("No training samples, cannot perform SVM training.");
}
if (concatenateValidationSamples->GetOutputSampleList()->Size() == 0)
{
otbAppLogWARNING("No validation samples.");
}
if (IsParameterEnabled("io.imstat"))
{
StatisticsReader::Pointer statisticsReader = StatisticsReader::New();
statisticsReader->SetFileName(GetParameterString("io.imstat"));
meanMeasurementVector = statisticsReader->GetStatisticVectorByName("mean");
stddevMeasurementVector = statisticsReader->GetStatisticVectorByName("stddev");
}
else
{
meanMeasurementVector.SetSize(nbBands);
meanMeasurementVector.Fill(0.);
stddevMeasurementVector.SetSize(nbBands);
stddevMeasurementVector.Fill(1.);
}
// Shift scale the samples
ShiftScaleFilterType::Pointer trainingShiftScaleFilter = ShiftScaleFilterType::New();
trainingShiftScaleFilter->SetInput(concatenateTrainingSamples->GetOutput());
trainingShiftScaleFilter->SetShifts(meanMeasurementVector);
trainingShiftScaleFilter->SetScales(stddevMeasurementVector);
trainingShiftScaleFilter->Update();
ShiftScaleFilterType::Pointer validationShiftScaleFilter = ShiftScaleFilterType::New();
validationShiftScaleFilter->SetInput(concatenateValidationSamples->GetOutput());
validationShiftScaleFilter->SetShifts(meanMeasurementVector);
validationShiftScaleFilter->SetScales(stddevMeasurementVector);
validationShiftScaleFilter->Update();
ListSampleType::Pointer listSample;
LabelListSampleType::Pointer labelListSample;
//--------------------------
// Balancing training sample (if needed)
// if (IsParameterEnabled("sample.b"))
// {
// // Balance the list sample.
// otbAppLogINFO("Number of training samples before balancing: " << concatenateTrainingSamples->GetOutputSampleList()->Size())
// BalancingListSampleFilterType::Pointer balancingFilter = BalancingListSampleFilterType::New();
// balancingFilter->SetInput(trainingShiftScaleFilter->GetOutput()/*GetOutputSampleList()*/);
// balancingFilter->SetInputLabel(concatenateTrainingLabels->GetOutput()/*GetOutputSampleList()*/);
// balancingFilter->SetBalancingFactor(GetParameterInt("sample.b"));
// balancingFilter->Update();
// listSample = balancingFilter->GetOutputSampleList();
// labelListSample = balancingFilter->GetOutputLabelSampleList();
// otbAppLogINFO("Number of samples after balancing: " << balancingFilter->GetOutputSampleList()->Size());
// }
// else
// {
listSample = trainingShiftScaleFilter->GetOutputSampleList();
labelListSample = concatenateTrainingLabels->GetOutputSampleList();
otbAppLogINFO("Number of training samples: " << concatenateTrainingSamples->GetOutputSampleList()->Size());
// }
//--------------------------
// Split the data set into training/validation set
ListSampleType::Pointer trainingListSample = listSample;
ListSampleType::Pointer validationListSample = validationShiftScaleFilter->GetOutputSampleList();
LabelListSampleType::Pointer trainingLabeledListSample = labelListSample;
LabelListSampleType::Pointer validationLabeledListSample = concatenateValidationLabels->GetOutputSampleList();
otbAppLogINFO("Size of training set: " << trainingListSample->Size());
otbAppLogINFO("Size of validation set: " << validationListSample->Size());
otbAppLogINFO("Size of labeled training set: " << trainingLabeledListSample->Size());