Commit ae4197ee authored by Julien Michel's avatar Julien Michel
Browse files

MRG

parents c83cfc90 2ea07dea
if(NOT EXISTS "@CMAKE_BINARY_DIR@/install_manifest.txt")
message(FATAL_ERROR "Cannot find install manifest: @CMAKE_BINARY_DIR@/install_manifest.txt")
return()
endif()
file(READ "@CMAKE_BINARY_DIR@/install_manifest.txt" files)
string(REGEX REPLACE "\n" ";" files "${files}")
foreach(file ${files})
message(STATUS "Uninstalling $ENV{DESTDIR}${file}")
if(IS_SYMLINK "$ENV{DESTDIR}${file}" OR EXISTS "$ENV{DESTDIR}${file}")
execute_process(COMMAND
"@CMAKE_COMMAND@" -E remove "$ENV{DESTDIR}${file}"
OUTPUT_VARIABLE out_var
RESULT_VARIABLE res_var
)
if(NOT "${res_var}" STREQUAL 0)
message(FATAL_ERROR "Problem when removing $ENV{DESTDIR}${file}")
endif()
else()
message(STATUS "File $ENV{DESTDIR}${file} does not exist.")
endif()
endforeach(file)
......@@ -364,6 +364,15 @@ endif()
# Create target to download data from the OTBData group. This must come after
# all tests have been added that reference the group, so we put it last.
# uninstall target
configure_file(
"${CMAKE_SOURCE_DIR}/CMake/cmake_uninstall.cmake.in"
"${CMAKE_BINARY_DIR}/cmake_uninstall.cmake"
IMMEDIATE @ONLY)
add_custom_target(uninstall
COMMAND ${CMAKE_COMMAND} -P ${CMAKE_BINARY_DIR}/cmake_uninstall.cmake)
#macro to put a fixed space between key, value in summary
macro(get_white_spaces var res)
string(LENGTH "${var}" len)
......
......@@ -45,6 +45,12 @@ otb_create_application(
SOURCES otbTrainOGRLayersClassifier.cxx
LINK_LIBRARIES ${${otb-module}_LIBRARIES})
otb_create_application(
NAME TrainVectorClassifier
SOURCES otbTrainVectorClassifier.cxx
LINK_LIBRARIES ${${otb-module}_LIBRARIES})
otb_create_application(
NAME ComputeConfusionMatrix
SOURCES otbComputeConfusionMatrix.cxx
......@@ -109,8 +115,13 @@ otb_create_application(
NAME PolygonClassStatistics
SOURCES otbPolygonClassStatistics.cxx
LINK_LIBRARIES ${${otb-module}_LIBRARIES})
otb_create_application(
NAME SampleSelection
SOURCES otbSampleSelection.cxx
LINK_LIBRARIES ${${otb-module}_LIBRARIES})
otb_create_application(
NAME SampleExtraction
SOURCES otbSampleExtraction.cxx
LINK_LIBRARIES ${${otb-module}_LIBRARIES})
/*=========================================================================
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 "otbImageSampleExtractorFilter.h"
namespace otb
{
namespace Wrapper
{
class SampleExtraction : public Application
{
public:
/** Standard class typedefs. */
typedef SampleExtraction Self;
typedef Application Superclass;
typedef itk::SmartPointer<Self> Pointer;
typedef itk::SmartPointer<const Self> ConstPointer;
/** Standard macro */
itkNewMacro(Self);
itkTypeMacro(SampleExtraction, otb::Application);
/** Filters typedef */
typedef otb::ImageSampleExtractorFilter<FloatVectorImageType> FilterType;
private:
SampleExtraction() {}
void DoInit()
{
SetName("SampleExtraction");
SetDescription("Extracts samples values from an image.");
// Documentation
SetDocName("Sample Extraction");
SetDocLongDescription("The application extracts samples values from an"
"image using positions contained in a vector data file. ");
SetDocLimitations("None");
SetDocAuthors("OTB-Team");
SetDocSeeAlso(" ");
AddDocTag(Tags::Learning);
AddParameter(ParameterType_InputImage, "in", "InputImage");
SetParameterDescription("in", "Support image");
AddParameter(ParameterType_InputFilename, "vec", "Input sampling positions");
SetParameterDescription("vec","Vector data file containing sampling"
"positions. (OGR format)");
AddParameter(ParameterType_OutputFilename, "out", "Output samples");
SetParameterDescription("out","Output vector data file storing sample"
"values (OGR format). If not given, the input vector data file is updated");
MandatoryOff("out");
AddParameter(ParameterType_Choice, "outfield", "Output field names");
SetParameterDescription("outfield", "Choice between naming method for output fields");
AddChoice("outfield.prefix","Use a prefix and an incremental counter");
SetParameterDescription("outfield.prefix","Use a prefix and an incremental counter");
AddParameter(ParameterType_String, "outfield.prefix.name", "Output field prefix");
SetParameterDescription("outfield.prefix.name","Prefix used to form the field names that"
"will contain the extracted values.");
SetParameterString("outfield.prefix.name", "value_");
AddChoice("outfield.list","Use the given name list");
SetParameterDescription("outfield.list","Use the given name list");
AddParameter(ParameterType_StringList, "outfield.list.names", "Output field names");
SetParameterDescription("outfield.list.names","Full list of output field names.");
AddParameter(ParameterType_String, "field", "Field Name");
SetParameterDescription("field","Name of the field carrying the class"
"name in the input vectors. This field is copied to output.");
MandatoryOff("field");
SetParameterString("field", "class");
AddParameter(ParameterType_Int, "layer", "Layer Index");
SetParameterDescription("layer", "Layer index to read in the input vector file.");
MandatoryOff("layer");
SetDefaultParameterInt("layer",0);
AddRAMParameter();
// Doc example parameter settings
SetDocExampleParameterValue("in", "support_image.tif");
SetDocExampleParameterValue("vec", "sample_positions.sqlite");
SetDocExampleParameterValue("pre","band_");
SetDocExampleParameterValue("field", "label");
SetDocExampleParameterValue("out","sample_values.sqlite");
}
void DoUpdateParameters()
{
// Nothing to do
}
void DoExecute()
{
ogr::DataSource::Pointer vectors;
ogr::DataSource::Pointer output;
if (IsParameterEnabled("out") && HasValue("out"))
{
vectors = ogr::DataSource::New(this->GetParameterString("vec"));
output = ogr::DataSource::New(this->GetParameterString("out"),
ogr::DataSource::Modes::Overwrite);
}
else
{
// Update mode
vectors = ogr::DataSource::New(this->GetParameterString("vec"),
ogr::DataSource::Modes::Update_LayerUpdate);
output = vectors;
}
std::vector<std::string> nameList;
std::string namePrefix("");
if (this->GetParameterString("outfield").compare("prefix") == 0)
{
namePrefix = this->GetParameterString("outfield.prefix.name");
}
else if (this->GetParameterString("outfield").compare("list") == 0)
{
nameList = this->GetParameterStringList("outfield.list.names");
}
else
{
otbAppLogFATAL("Unkown output field option : " << this->GetParameterString("outfield"));
}
FilterType::Pointer filter = FilterType::New();
filter->SetInput(this->GetParameterImage("in"));
filter->SetLayerIndex(this->GetParameterInt("layer"));
filter->SetSamplePositions(vectors);
filter->SetOutputSamples(output);
filter->SetClassFieldName(this->GetParameterString("field"));
filter->SetOutputFieldPrefix(namePrefix);
filter->SetOutputFieldNames(nameList);
AddProcess(filter->GetStreamer(),"Extracting sample values...");
filter->Update();
output->SyncToDisk();
}
};
} // end of namespace Wrapper
} // end of namespace otb
OTB_APPLICATION_EXPORT(otb::Wrapper::SampleExtraction)
......@@ -56,8 +56,11 @@ private:
SetName("TrainOGRLayersClassifier");
SetDescription("Train a SVM classifier based on labeled geometries and a list of features to consider.");
SetDocName("TrainOGRLayersClassifier");
SetDocLongDescription("This application trains a SVM classifier based on labeled geometries and a list of features to consider for classification.");
SetDocName("TrainOGRLayersClassifier (DEPRECATED)");
SetDocLongDescription("This application trains a SVM classifier based on "
"labeled geometries and a list of features to consider for classification."
" This application is deprecated, prefer using TrainVectorClassifier which"
" offers access to all the classifiers.");
SetDocLimitations("Experimental. For now only shapefiles are supported. Tuning of SVM classifier is not available.");
SetDocAuthors("David Youssefi during internship at CNES");
SetDocSeeAlso("OGRLayerClassifier,ComputeOGRLayersFeaturesStatistics");
......
/*=========================================================================
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 "otbLearningApplicationBase.h"
#include "otbOGRDataSourceWrapper.h"
#include "otbOGRFeatureWrapper.h"
#include "otbStatisticsXMLFileWriter.h"
#include "itkVariableLengthVector.h"
#include "otbStatisticsXMLFileReader.h"
#include "itkListSample.h"
#include "otbShiftScaleSampleListFilter.h"
// Validation
#include "otbConfusionMatrixCalculator.h"
#include <algorithm>
#include <locale>
namespace otb
{
namespace Wrapper
{
/** Utility function to negate std::isalnum */
bool IsNotAlphaNum(char c)
{
return !std::isalnum(c);
}
class TrainVectorClassifier : public LearningApplicationBase<float,int>
{
public:
typedef TrainVectorClassifier Self;
typedef LearningApplicationBase<float, int> Superclass;
typedef itk::SmartPointer<Self> Pointer;
typedef itk::SmartPointer<const Self> ConstPointer;
itkNewMacro(Self)
itkTypeMacro(Self, Superclass)
typedef Superclass::SampleType SampleType;
typedef Superclass::ListSampleType ListSampleType;
typedef Superclass::TargetListSampleType TargetListSampleType;
typedef Superclass::SampleImageType SampleImageType;
typedef double ValueType;
typedef itk::VariableLengthVector<ValueType> MeasurementType;
typedef otb::StatisticsXMLFileReader<SampleType> StatisticsReader;
typedef otb::Statistics::ShiftScaleSampleListFilter<ListSampleType, ListSampleType> ShiftScaleFilterType;
// Estimate performance on validation sample
typedef otb::ConfusionMatrixCalculator<TargetListSampleType, TargetListSampleType> ConfusionMatrixCalculatorType;
typedef ConfusionMatrixCalculatorType::ConfusionMatrixType ConfusionMatrixType;
typedef ConfusionMatrixCalculatorType::MapOfIndicesType MapOfIndicesType;
typedef ConfusionMatrixCalculatorType::ClassLabelType ClassLabelType;
private:
void DoInit()
{
SetName("TrainVectorClassifier");
SetDescription("Train a classifier based on labeled geometries and a list of features to consider.");
SetDocName("Train Vector Classifier");
SetDocLongDescription("This application trains a classifier based on "
"labeled geometries and a list of features to consider for classification.");
SetDocLimitations(" ");
SetDocAuthors("OTB Team");
SetDocSeeAlso(" ");
//Group IO
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_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_OutputFilename, "io.confmatout", "Output confusion matrix");
SetParameterDescription("io.confmatout", "Output file containing the confusion matrix (.csv format).");
MandatoryOff("io.confmatout");
AddParameter(ParameterType_OutputFilename, "io.out", "Output model");
SetParameterDescription("io.out", "Output file containing the model estimated (.txt format).");
AddParameter(ParameterType_ListView, "feat", "Field names for training features.");
SetParameterDescription("feat","List of field names in the input vector data to be used as features for training.");
AddParameter(ParameterType_String,"cfield","Field containing the class id for supervision");
SetParameterDescription("cfield","Field containing the class id for supervision. "
"Only geometries with this field available will be taken into account.");
SetParameterString("cfield","class");
AddParameter(ParameterType_Int, "layer", "Layer Index");
SetParameterDescription("layer", "Index of the layer to use in the input vector file.");
MandatoryOff("layer");
SetDefaultParameterInt("layer",0);
AddParameter(ParameterType_Group, "valid", "Validation data");
SetParameterDescription("valid", "This group of parameters defines validation data.");
AddParameter(ParameterType_InputVectorData, "valid.vd", "Validation Vector Data");
SetParameterDescription("valid.vd", "Geometries used for validation "
"(must contain the same fields used for training, all geometries from the layer will be used)");
MandatoryOff("valid.vd");
AddParameter(ParameterType_Int, "valid.layer", "Layer Index");
SetParameterDescription("valid.layer", "Index of the layer to use in the validation vector file.");
MandatoryOff("valid.layer");
SetDefaultParameterInt("valid.layer",0);
// Add parameters for the classifier choice
Superclass::DoInit();
AddRANDParameter();
// Doc example parameter settings
SetDocExampleParameterValue("io.vd", "vectorData.shp");
SetDocExampleParameterValue("io.stats", "meanVar.xml");
SetDocExampleParameterValue("io.out", "svmModel.svm");
SetDocExampleParameterValue("feat", "perimeter area width");
SetDocExampleParameterValue("cfield", "predicted");
}
void DoUpdateParameters()
{
if ( HasValue("io.vd") )
{
std::string vectorFile = GetParameterString("io.vd");
ogr::DataSource::Pointer ogrDS =
ogr::DataSource::New(vectorFile, ogr::DataSource::Modes::Read);
ogr::Layer layer = ogrDS->GetLayer(this->GetParameterInt("layer"));
ogr::Feature feature = layer.ogr().GetNextFeature();
ClearChoices("feat");
for(int iField=0; iField<feature.ogr().GetFieldCount(); iField++)
{
std::string key, item = feature.ogr().GetFieldDefnRef(iField)->GetNameRef();
key = item;
std::string::iterator end = std::remove_if(key.begin(),key.end(),IsNotAlphaNum);
std::transform(key.begin(), end, key.begin(), tolower);
key="feat."+key.substr(0, end - key.begin());
AddChoice(key,item);
}
}
}
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;
}
}
}
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()
{
std::string shapefile = GetParameterString("io.vd");
std::string modelfile = GetParameterString("io.out");
typedef int LabelPixelType;
typedef itk::FixedArray<LabelPixelType,1> LabelSampleType;
typedef itk::Statistics::ListSample <LabelSampleType> LabelListSampleType;
const int nbFeatures = GetSelectedItems("feat").size();
// Statistics for shift/scale
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.);
}
ogr::DataSource::Pointer source = ogr::DataSource::New(shapefile, ogr::DataSource::Modes::Read);
ogr::Layer layer = source->GetLayer(this->GetParameterInt("layer"));
ogr::Feature feature = layer.ogr().GetNextFeature();
bool goesOn = feature.addr() != 0;
ListSampleType::Pointer input = ListSampleType::New();
LabelListSampleType::Pointer target = LabelListSampleType::New();
input->SetMeasurementVectorSize(nbFeatures);
int cFieldIndex=-1;
std::vector<int> featureFieldIndex = GetSelectedItems("feat");
if (feature.addr())
{
cFieldIndex = feature.ogr().GetFieldIndex(GetParameterString("cfield").c_str());
}
// Check that the class field exists
if (cFieldIndex < 0)
{
std::ostringstream oss;
std::vector<std::string> names = GetChoiceNames("feat");
for (unsigned int i=0 ; i<names.size() ; i++)
{
if (i) oss << ", ";
oss << names[i];
}
otbAppLogFATAL("The field name for class label ("<<GetParameterString("cfield")
<<") has not been found in the input vector file! Choices are "<< oss.str());
}
while(goesOn)
{
if(feature.ogr().IsFieldSet(cFieldIndex))
{
MeasurementType mv;
mv.SetSize(nbFeatures);
for(int idx=0; idx < nbFeatures; ++idx)
mv[idx] = feature.ogr().GetFieldAsDouble(featureFieldIndex[idx]);
input->PushBack(mv);
target->PushBack(feature.ogr().GetFieldAsInteger(cFieldIndex));
}
feature = layer.ogr().GetNextFeature();
goesOn = feature.addr() != 0;
}
ShiftScaleFilterType::Pointer trainingShiftScaleFilter = ShiftScaleFilterType::New();
trainingShiftScaleFilter->SetInput(input);
trainingShiftScaleFilter->SetShifts(meanMeasurementVector);
trainingShiftScaleFilter->SetScales(stddevMeasurementVector);
trainingShiftScaleFilter->Update();
ListSampleType::Pointer listSample;
LabelListSampleType::Pointer labelListSample;
listSample = trainingShiftScaleFilter->GetOutput();
labelListSample = target;