From ee1cb255956c66d7591d94db23e3de93ecf38c4d Mon Sep 17 00:00:00 2001 From: Guillaume Pasero <guillaume.pasero@c-s.fr> Date: Wed, 5 Apr 2017 11:23:06 +0200 Subject: [PATCH] REFAC: remove deprecated application TrainOGRLayersClassifier --- .../AppClassification/app/CMakeLists.txt | 5 - .../otbComputeOGRLayersFeaturesStatistics.cxx | 2 +- .../app/otbOGRLayerClassifier.cxx | 2 +- .../app/otbTrainOGRLayersClassifier.cxx | 224 ------------------ .../AppClassification/test/CMakeLists.txt | 13 - 5 files changed, 2 insertions(+), 244 deletions(-) delete mode 100644 Modules/Applications/AppClassification/app/otbTrainOGRLayersClassifier.cxx diff --git a/Modules/Applications/AppClassification/app/CMakeLists.txt b/Modules/Applications/AppClassification/app/CMakeLists.txt index e28ab734b9..7a039b8224 100644 --- a/Modules/Applications/AppClassification/app/CMakeLists.txt +++ b/Modules/Applications/AppClassification/app/CMakeLists.txt @@ -60,11 +60,6 @@ otb_create_application( SOURCES otbImageClassifier.cxx LINK_LIBRARIES ${${otb-module}_LIBRARIES}) -otb_create_application( - NAME TrainOGRLayersClassifier - SOURCES otbTrainOGRLayersClassifier.cxx - LINK_LIBRARIES ${${otb-module}_LIBRARIES}) - otb_create_application( NAME TrainVectorClassifier SOURCES otbTrainVectorClassifier.cxx diff --git a/Modules/Applications/AppClassification/app/otbComputeOGRLayersFeaturesStatistics.cxx b/Modules/Applications/AppClassification/app/otbComputeOGRLayersFeaturesStatistics.cxx index 4f33d7a562..75469174e2 100644 --- a/Modules/Applications/AppClassification/app/otbComputeOGRLayersFeaturesStatistics.cxx +++ b/Modules/Applications/AppClassification/app/otbComputeOGRLayersFeaturesStatistics.cxx @@ -55,7 +55,7 @@ private: SetDocLongDescription("Compute statistics (mean and standard deviation) of the features in a set of OGR Layers, and write them in an XML file. This XML file can then be used by the training application."); SetDocLimitations("Experimental. For now only shapefiles are supported."); SetDocAuthors("David Youssefi during internship at CNES"); - SetDocSeeAlso("OGRLayerClassifier,TrainOGRLayersClassifier"); + SetDocSeeAlso("OGRLayerClassifier,TrainVectorClassifier"); AddDocTag(Tags::Segmentation); AddParameter(ParameterType_InputVectorData, "inshp", "Name of the input shapefile"); diff --git a/Modules/Applications/AppClassification/app/otbOGRLayerClassifier.cxx b/Modules/Applications/AppClassification/app/otbOGRLayerClassifier.cxx index 4511598633..807459cd48 100644 --- a/Modules/Applications/AppClassification/app/otbOGRLayerClassifier.cxx +++ b/Modules/Applications/AppClassification/app/otbOGRLayerClassifier.cxx @@ -64,7 +64,7 @@ private: SetDocLongDescription("This application will apply a trained machine learning model on the selected feature to get a classification of each geometry contained in an OGR layer. The list of feature must match the list used for training. The predicted label is written in the user defined field for each geometry."); SetDocLimitations("Experimental. Only shapefiles are supported for now."); SetDocAuthors("David Youssefi during internship at CNES"); - SetDocSeeAlso("ComputeOGRLayersFeaturesStatistics,TrainOGRLayersClassifier"); + SetDocSeeAlso("ComputeOGRLayersFeaturesStatistics"); AddDocTag(Tags::Segmentation); AddParameter(ParameterType_InputVectorData, "inshp", "Name of the input shapefile"); diff --git a/Modules/Applications/AppClassification/app/otbTrainOGRLayersClassifier.cxx b/Modules/Applications/AppClassification/app/otbTrainOGRLayersClassifier.cxx deleted file mode 100644 index d576ed661e..0000000000 --- a/Modules/Applications/AppClassification/app/otbTrainOGRLayersClassifier.cxx +++ /dev/null @@ -1,224 +0,0 @@ -/* - * Copyright (C) 2005-2017 Centre National d'Etudes Spatiales (CNES) - * - * This file is part of Orfeo Toolbox - * - * https://www.orfeo-toolbox.org/ - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "otbWrapperApplication.h" -#include "otbWrapperApplicationFactory.h" - -#include "otbOGRDataSourceWrapper.h" -#include "otbOGRFeatureWrapper.h" -#include "otbStatisticsXMLFileWriter.h" - -#include "itkVariableLengthVector.h" -#include "otbStatisticsXMLFileReader.h" - -#include "itkListSample.h" -#include "otbShiftScaleSampleListFilter.h" - -#ifdef OTB_USE_LIBSVM -#include "otbLibSVMMachineLearningModel.h" -#endif - -#include <time.h> - -namespace otb -{ -namespace Wrapper -{ -class TrainOGRLayersClassifier : public Application -{ -public: - typedef TrainOGRLayersClassifier Self; - typedef Application Superclass; - typedef itk::SmartPointer<Self> Pointer; - typedef itk::SmartPointer<const Self> ConstPointer; - itkNewMacro(Self) -; - - itkTypeMacro(TrainOGRLayersClassifier, otb::Application) -; - -private: - void DoInit() ITK_OVERRIDE - { - SetName("TrainOGRLayersClassifier"); - SetDescription("Train a SVM classifier based on labeled geometries and a list of features to consider."); - - 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"); - AddDocTag(Tags::Segmentation); - - AddParameter(ParameterType_InputVectorData, "inshp", "Name of the input shapefile"); - SetParameterDescription("inshp","Name of the input shapefile"); - - AddParameter(ParameterType_InputFilename, "instats", "XML file containing mean and variance of each feature."); - SetParameterDescription("instats", "XML file containing mean and variance of each feature."); - - AddParameter(ParameterType_OutputFilename, "outsvm", "Output model filename."); - SetParameterDescription("outsvm", "Output model filename."); - - AddParameter(ParameterType_ListView, "feat", "List of features to consider for classification."); - SetParameterDescription("feat","List of features to consider for classification."); - - 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", false); - - // Doc example parameter settings - SetDocExampleParameterValue("inshp", "vectorData.shp"); - SetDocExampleParameterValue("instats", "meanVar.xml"); - SetDocExampleParameterValue("outsvm", "svmModel.svm"); - SetDocExampleParameterValue("feat", "perimeter"); - SetDocExampleParameterValue("cfield", "predicted"); - - } - - void DoUpdateParameters() ITK_OVERRIDE - { - if ( HasValue("inshp") ) - { - std::string shapefile = GetParameterString("inshp"); - - otb::ogr::DataSource::Pointer ogrDS; - otb::ogr::Layer layer(ITK_NULLPTR, false); - - OGRSpatialReference oSRS(""); - std::vector<std::string> options; - - ogrDS = otb::ogr::DataSource::New(shapefile, otb::ogr::DataSource::Modes::Read); - std::string layername = itksys::SystemTools::GetFilenameName(shapefile); - layername = layername.substr(0,layername.size()-4); - layer = ogrDS->GetLayer(0); - - otb::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; - key.erase(std::remove(key.begin(), key.end(), ' '), key.end()); - std::transform(key.begin(), key.end(), key.begin(), tolower); - key="feat."+key; - AddChoice(key,item); - } - } - } - - void DoExecute() ITK_OVERRIDE - { - #ifdef OTB_USE_LIBSVM - clock_t tic = clock(); - - std::string shapefile = GetParameterString("inshp"); - std::string XMLfile = GetParameterString("instats"); - std::string modelfile = GetParameterString("outsvm"); - - typedef double ValueType; - typedef itk::VariableLengthVector<ValueType> MeasurementType; - typedef itk::Statistics::ListSample <MeasurementType> ListSampleType; - typedef otb::StatisticsXMLFileReader<MeasurementType> StatisticsReader; - - typedef unsigned int LabelPixelType; - typedef itk::FixedArray<LabelPixelType,1> LabelSampleType; - typedef itk::Statistics::ListSample <LabelSampleType> LabelListSampleType; - - typedef otb::Statistics::ShiftScaleSampleListFilter<ListSampleType, ListSampleType> ShiftScaleFilterType; - - StatisticsReader::Pointer statisticsReader = StatisticsReader::New(); - statisticsReader->SetFileName(XMLfile); - - MeasurementType meanMeasurementVector = statisticsReader->GetStatisticVectorByName("mean"); - MeasurementType stddevMeasurementVector = statisticsReader->GetStatisticVectorByName("stddev"); - - otb::ogr::DataSource::Pointer source = otb::ogr::DataSource::New(shapefile, otb::ogr::DataSource::Modes::Read); - otb::ogr::Layer layer = source->GetLayer(0); - bool goesOn = true; - otb::ogr::Feature feature = layer.ogr().GetNextFeature(); - - ListSampleType::Pointer input = ListSampleType::New(); - LabelListSampleType::Pointer target = LabelListSampleType::New(); - const int nbFeatures = GetSelectedItems("feat").size(); - - input->SetMeasurementVectorSize(nbFeatures); - - if(feature.addr()) - while(goesOn) - { - if(feature.ogr().IsFieldSet(feature.ogr().GetFieldIndex(GetParameterString("cfield").c_str()))) - { - MeasurementType mv; mv.SetSize(nbFeatures); - - for(int idx=0; idx < nbFeatures; ++idx) - mv[idx] = feature.ogr().GetFieldAsDouble(GetSelectedItems("feat")[idx]); - - input->PushBack(mv); - target->PushBack(feature.ogr().GetFieldAsInteger(GetParameterString("cfield").c_str())); - } - feature = layer.ogr().GetNextFeature(); - goesOn = feature.addr() != ITK_NULLPTR; - } - - 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; - - ListSampleType::Pointer trainingListSample = listSample; - LabelListSampleType::Pointer trainingLabeledListSample = labelListSample; - - typedef otb::LibSVMMachineLearningModel<ValueType,LabelPixelType> LibSVMType; - LibSVMType::Pointer libSVMClassifier = LibSVMType::New(); - libSVMClassifier->SetInputListSample(trainingListSample); - libSVMClassifier->SetTargetListSample(trainingLabeledListSample); - libSVMClassifier->SetParameterOptimization(true); - libSVMClassifier->SetC(1.0); - libSVMClassifier->SetKernelType(LINEAR); - libSVMClassifier->Train(); - libSVMClassifier->Save(modelfile); - - clock_t toc = clock(); - - otbAppLogINFO( "Elapsed: "<< ((double)(toc - tic) / CLOCKS_PER_SEC)<<" seconds."); - - #else - otbAppLogFATAL("Module LIBSVM is not installed. You should consider turning OTB_USE_LIBSVM on during cmake configuration."); - #endif - - } - -}; -} -} - -OTB_APPLICATION_EXPORT(otb::Wrapper::TrainOGRLayersClassifier) - - diff --git a/Modules/Applications/AppClassification/test/CMakeLists.txt b/Modules/Applications/AppClassification/test/CMakeLists.txt index 3a340346d8..d8606c5d80 100644 --- a/Modules/Applications/AppClassification/test/CMakeLists.txt +++ b/Modules/Applications/AppClassification/test/CMakeLists.txt @@ -360,19 +360,6 @@ if(OTB_USE_LIBSVM) endif() -#----------- TrainOGRLayersClassifier TESTS ---------------- -otb_test_application(NAME apTvClTrainOGRLayersClassifier - APP TrainOGRLayersClassifier - OPTIONS -inshp ${INPUTDATA}/Classification/apTvClLabeledVector.shp - -feat meanB0 meanB1 meanB2 meanB3 varB0 varB1 varB2 varB3 - -instats ${TEMP}/apTvClComputeOGRLayersFeaturesStatistics.xml - -outsvm ${TEMP}/apTvClModel.svm - VALID --compare-ascii ${NOTOL} - ${OTBAPP_BASELINE_FILES}/apTvClModel.svm - ${TEMP}/apTvClModel.svm) - -set_tests_properties(apTvClTrainOGRLayersClassifier PROPERTIES DEPENDS apTvClComputeOGRLayersFeaturesStatistics) - #----------- ComputeConfusionMatrix TESTS ---------------- otb_test_application(NAME apTvComputeConfusionMatrixV APP ComputeConfusionMatrix -- GitLab