diff --git a/Examples/Learning/CMakeLists.txt b/Examples/Learning/CMakeLists.txt index 8d25fa6e2280808f545dda543b621b6165e55cb7..89d15d3743f07ae549c64f8f4901f4386229da47 100644 --- a/Examples/Learning/CMakeLists.txt +++ b/Examples/Learning/CMakeLists.txt @@ -33,23 +33,8 @@ add_executable(SOMExample SOMExample.cxx) target_link_libraries(SOMExample ${OTB_LIBRARIES}) if(OTBLibSVM_LOADED) -add_executable(SVMImageClassificationExample SVMImageClassificationExample.cxx) -target_link_libraries(SVMImageClassificationExample ${OTB_LIBRARIES}) - add_executable(SVMImageEstimatorClassificationMultiExample SVMImageEstimatorClassificationMultiExample.cxx) target_link_libraries(SVMImageEstimatorClassificationMultiExample ${OTB_LIBRARIES}) - -add_executable(SVMImageModelEstimatorExample SVMImageModelEstimatorExample.cxx) -target_link_libraries(SVMImageModelEstimatorExample ${OTB_LIBRARIES}) - -add_executable(SVMPointSetClassificationExample SVMPointSetClassificationExample.cxx) -target_link_libraries(SVMPointSetClassificationExample ${OTB_LIBRARIES}) - -add_executable(SVMPointSetExample SVMPointSetExample.cxx) -target_link_libraries(SVMPointSetExample ${OTB_LIBRARIES}) - -add_executable(SVMPointSetModelEstimatorExample SVMPointSetModelEstimatorExample.cxx) -target_link_libraries(SVMPointSetModelEstimatorExample ${OTB_LIBRARIES}) endif() if(OTBOpenCV_LOADED) diff --git a/Examples/Learning/SVMImageClassificationExample.cxx b/Examples/Learning/SVMImageClassificationExample.cxx deleted file mode 100644 index c73ad425c0f6491abb3414d383808cfac3ec15e5..0000000000000000000000000000000000000000 --- a/Examples/Learning/SVMImageClassificationExample.cxx +++ /dev/null @@ -1,337 +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 <fstream> - -#include "otbImageFileReader.h" -#include "otbImageFileWriter.h" -#include "itkUnaryFunctorImageFilter.h" -#include "itkRescaleIntensityImageFilter.h" -#include "otbImage.h" - -// Software Guide : BeginCommandLineArgs -// INPUTS: {ROI_QB_MUL_1.png} -// OUTPUTS: {ROI_QB_MUL_1_SVN_CLASS.png} -// ${OTB_DATA_ROOT}/Examples/svm_image_model.svm -// Software Guide : EndCommandLineArgs - -// Software Guide : BeginLatex -// This example illustrates the use of the -// \doxygen{otb}{SVMClassifier} class for performing SVM -// classification on images. -// In this example, we will use an SVM model estimated in the example -// of section \ref{sec:LearningWithImages} -// to separate between water and non-water pixels by using the RGB -// values only. The images used for this example are shown in -// figure~\ref{fig:SVMROIS}. -// The first thing to do is include the header file for the -// class. Since the \doxygen{otb}{SVMClassifier} takes -// \doxygen{itk}{ListSample}s as input, the class -// \doxygen{itk}{PointSetToListAdaptor} is needed. -// -// -// Software Guide : EndLatex - -#include "itkImageToListSampleAdaptor.h" - -// Software Guide : BeginCodeSnippet -#include "otbSVMClassifier.h" -// Software Guide : EndCodeSnippet - -int main(int argc, char* argv[]) -{ - - if (argc != 4) - { - std::cout << "Usage : " << argv[0] << " inputImage outputImage modelFile " - << std::endl; - return EXIT_FAILURE; - } - - const char * imageFilename = argv[1]; - const char * modelFilename = argv[3]; - const char * outputFilename = argv[2]; - -// Software Guide : BeginLatex -// -// In the framework of supervised learning and classification, we will -// always use feature vectors for the characterization of the -// classes. On the other hand, the class labels are scalar -// values. Here, we start by defining the type of the features as the -// \code{PixelType}, which will be used to define the feature -// \code{VectorType}. We also declare the type for the labels. -// -// Software Guide : EndLatex - -// Software Guide : BeginCodeSnippet - typedef double PixelType; - typedef int LabelPixelType; -// Software Guide : EndCodeSnippet - const unsigned int Dimension = 2; - -// Software Guide : BeginLatex -// -// We can now proceed to define the image type used for storing the -// features. We also define the reader. -// -// Software Guide : EndLatex - -// Software Guide : BeginCodeSnippet - typedef otb::Image<itk::FixedArray<PixelType, 3>, - Dimension> InputImageType; - - typedef otb::ImageFileReader<InputImageType> ReaderType; -// Software Guide : EndCodeSnippet - -// Software Guide : BeginLatex -// -// We can now read the image by calling the \code{Update} method of the reader. -// -// Software Guide : EndLatex - -// Software Guide : BeginCodeSnippet - ReaderType::Pointer reader = ReaderType::New(); - - reader->SetFileName(imageFilename); - - reader->Update(); -// Software Guide : EndCodeSnippet - -// Software Guide : BeginLatex -// -// The image has now to be transformed to a sample which -// is compatible with the classification framework. We will use a -// \doxygen{itk}{Statistics::ImageToListSampleAdaptor} for this -// task. This class is templated over the image type used for -// storing the measures. -// -// Software Guide : EndLatex - -// Software Guide : BeginCodeSnippet - typedef itk::Statistics::ImageToListSampleAdaptor<InputImageType> SampleType; - SampleType::Pointer sample = SampleType::New(); -// Software Guide : EndCodeSnippet - -// Software Guide : BeginLatex -// -// After instantiation, we can set the image as an imput of our -// sample adaptor. -// -// Software Guide : EndLatex - -// Software Guide : BeginCodeSnippet - sample->SetImage(reader->GetOutput()); -// Software Guide : EndCodeSnippet - -// Software Guide : BeginLatex -// -// Now, we need to declare the SVM model which is to be used by the -// classifier. The SVM model is templated over the type of value used -// for the measures and the type of pixel used for the labels. -// -// Software Guide : EndLatex - -// Software Guide : BeginCodeSnippet - typedef otb::SVMModel<PixelType, LabelPixelType> ModelType; - - ModelType::Pointer model = ModelType::New(); -// Software Guide : EndCodeSnippet - -// Software Guide : BeginLatex -// -// After instantiation, we can load a model saved to a file (see -// section \ref{sec:LearningWithImages} for an example of model -// estimation and storage to a file. -// -// Software Guide : EndLatex - -// Software Guide : BeginCodeSnippet - model->LoadModel(modelFilename); -// Software Guide : EndCodeSnippet - -// Software Guide : BeginLatex -// -// We have now all the elements to create a classifier. The classifier -// is templated over the sample type (the type of the data to be -// classified) and the label type (the type of the output of the classifier). -// -// Software Guide : EndLatex - -// Software Guide : BeginCodeSnippet - typedef otb::SVMClassifier<SampleType, LabelPixelType> ClassifierType; - - ClassifierType::Pointer classifier = ClassifierType::New(); -// Software Guide : EndCodeSnippet - -// Software Guide : BeginLatex -// -// We set the classifier parameters : number of classes, SVM model, -// the sample data. And we trigger the classification process by -// calling the \code{Update} method. -// -// Software Guide : EndLatex - -// Software Guide : BeginCodeSnippet - int numberOfClasses = model->GetNumberOfClasses(); - classifier->SetNumberOfClasses(numberOfClasses); - classifier->SetModel(model); - classifier->SetInput(sample.GetPointer()); - classifier->Update(); -// Software Guide : EndCodeSnippet - -// Software Guide : BeginLatex -// -// After the classification step, we usually want to get the -// results. The classifier gives an output under the form of a sample -// list. This list supports the classical STL iterators. Therefore, we -// will create an output image and fill it up with the results of the -// classification. The pixel type of the output image is the same as -// the one used for the labels. -// -// Software Guide : EndLatex - -// Software Guide : BeginCodeSnippet - typedef ClassifierType::ClassLabelType OutputPixelType; - typedef otb::Image<OutputPixelType, Dimension> OutputImageType; - - OutputImageType::Pointer outputImage = OutputImageType::New(); -// Software Guide : EndCodeSnippet - -// Software Guide : BeginLatex -// -// We allocate the memory for the output image using the information -// from the input image. -// -// Software Guide : EndLatex - -// Software Guide : BeginCodeSnippet - typedef itk::Index<Dimension> myIndexType; - typedef itk::Size<Dimension> mySizeType; - typedef itk::ImageRegion<Dimension> myRegionType; - - mySizeType size; - size[0] = reader->GetOutput()->GetRequestedRegion().GetSize()[0]; - size[1] = reader->GetOutput()->GetRequestedRegion().GetSize()[1]; - - myIndexType start; - start[0] = 0; - start[1] = 0; - - myRegionType region; - region.SetIndex(start); - region.SetSize(size); - - outputImage->SetRegions(region); - outputImage->Allocate(); -// Software Guide : EndCodeSnippet - -// Software Guide : BeginLatex -// -// We can now declare the iterators on the list that we get at the -// output of the classifier as well as the iterator to fill the output image. -// -// Software Guide : EndLatex - -// Software Guide : BeginCodeSnippet - ClassifierType::OutputType* membershipSample = - classifier->GetOutput(); - ClassifierType::OutputType::ConstIterator m_iter = - membershipSample->Begin(); - ClassifierType::OutputType::ConstIterator m_last = - membershipSample->End(); - - typedef itk::ImageRegionIterator<OutputImageType> OutputIteratorType; - OutputIteratorType outIt(outputImage, - outputImage->GetBufferedRegion()); - - outIt.GoToBegin(); -// Software Guide : EndCodeSnippet - -// Software Guide : BeginLatex -// -// We will iterate through the list, get the labels and assign pixel -// values to the output image. -// -// Software Guide : EndLatex - -// Software Guide : BeginCodeSnippet - while (m_iter != m_last && !outIt.IsAtEnd()) - { - outIt.Set(m_iter.GetClassLabel()); - ++m_iter; - ++outIt; - } -// Software Guide : EndCodeSnippet - -// Software Guide : BeginLatex -// -// Only for visualization purposes, we choose to rescale the image of -// classes before saving it to a file. We will use the -// \doxygen{itk}{RescaleIntensityImageFilter} for this purpose. -// -// Software Guide : EndLatex - -// Software Guide : BeginCodeSnippet - typedef otb::Image<unsigned char, Dimension> FileImageType; - - typedef itk::RescaleIntensityImageFilter<OutputImageType, - FileImageType> RescalerType; - - RescalerType::Pointer rescaler = RescalerType::New(); - - rescaler->SetOutputMinimum(itk::NumericTraits<unsigned char>::min()); - rescaler->SetOutputMaximum(itk::NumericTraits<unsigned char>::max()); - - rescaler->SetInput(outputImage); -// Software Guide : EndCodeSnippet - -// Software Guide : BeginLatex -// -// We can now create an image file writer and save the image. -// -// Software Guide : EndLatex - -// Software Guide : BeginCodeSnippet - typedef otb::ImageFileWriter<FileImageType> WriterType; - - WriterType::Pointer writer = WriterType::New(); - - writer->SetFileName(outputFilename); - writer->SetInput(rescaler->GetOutput()); - - writer->Update(); -// Software Guide : EndCodeSnippet - -// Software Guide : BeginLatex -// Figure \ref{fig:SVMCLASS} shows the result of the SVM classification. -// \begin{figure} -// \center -// \includegraphics[width=0.45\textwidth]{ROI_QB_MUL_1.eps} -// \includegraphics[width=0.45\textwidth]{ROI_QB_MUL_1_SVN_CLASS.eps} -// \itkcaption[SVM Image Classification]{Result of the SVM -// classification . Left: RGB image. Right: image of classes.} -// \label{fig:SVMCLASS} -// \end{figure} -// Software Guide : EndLatex - - return EXIT_SUCCESS; -} diff --git a/Examples/Learning/SVMImageModelEstimatorExample.cxx b/Examples/Learning/SVMImageModelEstimatorExample.cxx deleted file mode 100644 index 9ee96db859d87531a99b575f86758e05c6e4e360..0000000000000000000000000000000000000000 --- a/Examples/Learning/SVMImageModelEstimatorExample.cxx +++ /dev/null @@ -1,161 +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. - */ - - - -// Software Guide : BeginCommandLineArgs -// INPUTS: {ROI_QB_MUL_1.png}, {ROI_mask.png} -// OUTPUTS: {svm_image_model.svn} -// Software Guide : EndCommandLineArgs - -// Software Guide : BeginLatex -// This example illustrates the use of the -// \doxygen{otb}{SVMImageModelEstimator} class. This class allows the -// estimation of a SVM model (supervised learning) from a feature -// image and an image of labels. In this example, we will train an SVM -// to separate between water and non-water pixels by using the RGB -// values only. The images used for this example are shown in -// figure~\ref{fig:SVMROIS}. -// \begin{figure} -// \center -// \includegraphics[width=0.45\textwidth]{ROI_QB_MUL_1.eps} -// \includegraphics[width=0.45\textwidth]{ROI_mask.eps} -// \itkcaption[SVM Image Model Estimation]{Images used for the -// estimation of the SVM model. Left: RGB image. Right: image of labels.} -// \label{fig:SVMROIS} -// \end{figure} -// The first thing to do is include the header file for the class. -// -// Software Guide : EndLatex - -#include "itkMacro.h" -#include "otbImage.h" -#include "otbVectorImage.h" -#include <iostream> - -// Software Guide : BeginCodeSnippet -#include "otbSVMImageModelEstimator.h" -// Software Guide : EndCodeSnippet - -#include "otbImageFileReader.h" - -int main(int itkNotUsed(argc), char* argv[]) -{ - - const char* inputImageFileName = argv[1]; - const char* trainingImageFileName = argv[2]; - const char* outputModelFileName = argv[3]; - -// Software Guide : BeginLatex -// -// We define the types for the input and training images. Even if the -// input image will be an RGB image, we can read it as a 3 component -// vector image. This simplifies the interfacing with OTB's SVM -// framework. -// -// Software Guide : EndLatex -// Software Guide : BeginCodeSnippet - typedef unsigned char InputPixelType; - const unsigned int Dimension = 2; - - typedef otb::VectorImage<InputPixelType, Dimension> InputImageType; - - typedef otb::Image<InputPixelType, Dimension> TrainingImageType; -// Software Guide : EndCodeSnippet - -// Software Guide : BeginLatex -// -// The \doxygen{otb}{SVMImageModelEstimator} class is templated over -// the input (features) and the training (labels) images. -// -// Software Guide : EndLatex -// Software Guide : BeginCodeSnippet - typedef otb::SVMImageModelEstimator<InputImageType, - TrainingImageType> EstimatorType; - -// Software Guide : EndCodeSnippet - -// Software Guide : BeginLatex -// -// As usual, we define the readers for the images. -// -// Software Guide : EndLatex -// Software Guide : BeginCodeSnippet - typedef otb::ImageFileReader<InputImageType> InputReaderType; - typedef otb::ImageFileReader<TrainingImageType> TrainingReaderType; - - InputReaderType::Pointer inputReader = InputReaderType::New(); - TrainingReaderType::Pointer trainingReader = TrainingReaderType::New(); - -// Software Guide : EndCodeSnippet - -// Software Guide : BeginLatex -// -// We read the images. It is worth to note that, in order to ensure -// the pipeline coherence, the output of the objects which precede the -// model estimator in the pipeline, must be up to date, so we call -// the corresponding \code{Update} methods. -// -// Software Guide : EndLatex -// Software Guide : BeginCodeSnippet - inputReader->SetFileName(inputImageFileName); - trainingReader->SetFileName(trainingImageFileName); - - inputReader->Update(); - trainingReader->Update(); - -// Software Guide : EndCodeSnippet - -// Software Guide : BeginLatex -// -// We can now instantiate the model estimator and set its parameters. -// -// Software Guide : EndLatex -// Software Guide : BeginCodeSnippet - EstimatorType::Pointer svmEstimator = EstimatorType::New(); - - svmEstimator->SetInputImage(inputReader->GetOutput()); - svmEstimator->SetTrainingImage(trainingReader->GetOutput()); - -// Software Guide : EndCodeSnippet - -// Software Guide : BeginLatex -// -// The model estimation procedure is triggered by calling the -// estimator's \code{Update} method. -// -// Software Guide : EndLatex -// Software Guide : BeginCodeSnippet - svmEstimator->Update(); - -// Software Guide : EndCodeSnippet - -// Software Guide : BeginLatex -// -// Finally, the estimated model can be saved to a file for later use. -// -// Software Guide : EndLatex -// Software Guide : BeginCodeSnippet - svmEstimator->SaveModel(outputModelFileName); - -// Software Guide : EndCodeSnippet - - return EXIT_SUCCESS; -} diff --git a/Examples/Learning/SVMPointSetClassificationExample.cxx b/Examples/Learning/SVMPointSetClassificationExample.cxx deleted file mode 100644 index e092f3ec4e9036e3a0daf5a561c896806019403d..0000000000000000000000000000000000000000 --- a/Examples/Learning/SVMPointSetClassificationExample.cxx +++ /dev/null @@ -1,322 +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. - */ - - - -// Software Guide : BeginCommandLineArgs -// INPUTS: {svm_model.svn} -// OUTPUTS: -// Software Guide : EndCommandLineArgs - -#include "itkMacro.h" -#include <iostream> -#include <cstdlib> - -// Software Guide : BeginLatex -// This example illustrates the use of the -// \doxygen{otb}{SVMClassifier} class for performing SVM -// classification on pointsets. -// The first thing to do is include the header file for the -// class. Since the \doxygen{otb}{SVMClassifier} takes -// \doxygen{itk}{ListSample}s as input, the class -// \doxygen{itk}{PointSetToListSampleAdaptor} is needed. -// -// We start by including the needed header files. -// -// Software Guide : EndLatex - -// Software Guide : BeginCodeSnippet -#include "itkPointSetToListSampleAdaptor.h" -#include "otbSVMClassifier.h" -// Software Guide : EndCodeSnippet - -int main(int itkNotUsed(argc), char* argv[]) -{ -// Software Guide : BeginLatex -// -// In the framework of supervised learning and classification, we will -// always use feature vectors for the characterization of the -// classes. On the other hand, the class labels are scalar -// values. Here, we start by defining the type of the features as the -// \code{PixelType}, which will be used to define the feature -// \code{VectorType}. We also declare the type for the labels. -// -// Software Guide : EndLatex - -// Software Guide : BeginCodeSnippet - typedef float InputPixelType; - - typedef std::vector<InputPixelType> InputVectorType; - typedef int LabelPixelType; -// Software Guide : EndCodeSnippet - const unsigned int Dimension = 2; - -// Software Guide : BeginLatex -// -// We can now proceed to define the point sets used for storing the -// features and the labels. -// -// Software Guide : EndLatex - -// Software Guide : BeginCodeSnippet - typedef itk::PointSet<InputVectorType, Dimension> MeasurePointSetType; -// Software Guide : EndCodeSnippet - -// Software Guide : BeginLatex -// -// We will need to get access to the data stored in the point sets, so -// we define the appropriate for the points and the points containers -// used by the point sets (see the section \ref{sec:PointSetSection} -// for more information on how to use point sets). -// -// Software Guide : EndLatex - -// Software Guide : BeginCodeSnippet - typedef MeasurePointSetType::PointType MeasurePointType; - typedef MeasurePointSetType::PointsContainer MeasurePointsContainer; - - MeasurePointSetType::Pointer tPSet = MeasurePointSetType::New(); - MeasurePointsContainer::Pointer tCont = MeasurePointsContainer::New(); -// Software Guide : EndCodeSnippet - -// Software Guide : BeginLatex -// -// We need now to build the test set for the SVM. In this -// simple example, we will build a SVM who classes points depending on -// which side of the line $x=y$ they are located. We start by -// generating 500 random points. -// -// Software Guide : EndLatex - - srand(0); - - unsigned int pointId; -// Software Guide : BeginCodeSnippet - int lowest = 0; - int range = 1000; - - for (pointId = 0; pointId < 100; pointId++) - { - - MeasurePointType tP; - - int x_coord = lowest + static_cast<int>(range * (rand() / (RAND_MAX + 1.0))); - int y_coord = lowest + static_cast<int>(range * (rand() / (RAND_MAX + 1.0))); - - std::cout << "coords : " << x_coord << " " << y_coord << std::endl; - tP[0] = x_coord; - tP[1] = y_coord; -// Software Guide : EndCodeSnippet - -// Software Guide : BeginLatex -// -// We push the features in the vector after a normalization which is -// useful for SVM convergence. -// -// Software Guide : EndLatex - -// Software Guide : BeginCodeSnippet - InputVectorType measure; - measure.push_back(static_cast<InputPixelType>((x_coord * 1.0 - - lowest) / range)); - measure.push_back(static_cast<InputPixelType>((y_coord * 1.0 - - lowest) / range)); -// Software Guide : EndCodeSnippet - -// Software Guide : BeginLatex -// -// And we insert the points in the points container. -// -// Software Guide : EndLatex - -// Software Guide : BeginCodeSnippet - tCont->InsertElement(pointId, tP); - tPSet->SetPointData(pointId, measure); - - } -// Software Guide : EndCodeSnippet - -// Software Guide : BeginLatex -// -// After the loop, we set the points container to the point set. -// -// Software Guide : EndLatex - -// Software Guide : BeginCodeSnippet - tPSet->SetPoints(tCont); -// Software Guide : EndCodeSnippet - -// Software Guide : BeginLatex -// -// Once the pointset is ready, we must transform it to a sample which -// is compatible with the classification framework. We will use a -// \doxygen{itk}{Statistics::PointSetToListSampleAdaptor} for this -// task. This class is templated over the point set type used for -// storing the measures. -// -// Software Guide : EndLatex - -// Software Guide : BeginCodeSnippet - typedef itk::Statistics::PointSetToListSampleAdaptor<MeasurePointSetType> - SampleType; - SampleType::Pointer sample = SampleType::New(); -// Software Guide : EndCodeSnippet - -// Software Guide : BeginLatex -// -// After instantiation, we can set the point set as an imput of our -// sample adaptor. -// -// Software Guide : EndLatex - -// Software Guide : BeginCodeSnippet - sample->SetPointSet(tPSet); -// Software Guide : EndCodeSnippet - -// Software Guide : BeginLatex -// -// Now, we need to declare the SVM model which is to be used by the -// classifier. The SVM model is templated over the type of value used -// for the measures and the type of pixel used for the labels. -// -// Software Guide : EndLatex - -// Software Guide : BeginCodeSnippet - typedef otb::SVMModel<SampleType::MeasurementVectorType::ValueType, - LabelPixelType> ModelType; - - ModelType::Pointer model = ModelType::New(); -// Software Guide : EndCodeSnippet - -// Software Guide : BeginLatex -// -// After instantiation, we can load a model saved to a file (see -// section \ref{sec:LearningWithPointSets} for an example of model -// estimation and storage to a file). -// -// Software Guide : EndLatex - -// Software Guide : BeginCodeSnippet - model->LoadModel(argv[1]); -// Software Guide : EndCodeSnippet - -// Software Guide : BeginLatex -// -// We have now all the elements to create a classifier. The classifier -// is templated over the sample type (the type of the data to be -// classified) and the label type (the type of the output of the classifier). -// -// Software Guide : EndLatex - -// Software Guide : BeginCodeSnippet - typedef otb::SVMClassifier<SampleType, LabelPixelType> ClassifierType; - - ClassifierType::Pointer classifier = ClassifierType::New(); -// Software Guide : EndCodeSnippet - -// Software Guide : BeginLatex -// -// We set the classifier parameters : number of classes, SVM model, -// the sample data. And we trigger the classification process by -// calling the \code{Update} method. -// -// Software Guide : EndLatex - -// Software Guide : BeginCodeSnippet - int numberOfClasses = model->GetNumberOfClasses(); - classifier->SetNumberOfClasses(numberOfClasses); - classifier->SetModel(model); - classifier->SetInput(sample.GetPointer()); - classifier->Update(); -// Software Guide : EndCodeSnippet - -// Software Guide : BeginLatex -// -// After the classification step, we usually want to get the -// results. The classifier gives an output under the form of a sample -// list. This list supports the classical STL iterators. -// -// Software Guide : EndLatex - -// Software Guide : BeginCodeSnippet - ClassifierType::OutputType* membershipSample = - classifier->GetOutput(); - - ClassifierType::OutputType::ConstIterator m_iter = - membershipSample->Begin(); - ClassifierType::OutputType::ConstIterator m_last = - membershipSample->End(); -// Software Guide : EndCodeSnippet - -// Software Guide : BeginLatex -// -// We will iterate through the list, get the labels and compute the -// classification error. -// -// Software Guide : EndLatex - -// Software Guide : BeginCodeSnippet - double error = 0.0; - pointId = 0; - while (m_iter != m_last) - { -// Software Guide : EndCodeSnippet - -// Software Guide : BeginLatex -// -// We get the label for each point. -// -// Software Guide : EndLatex - -// Software Guide : BeginCodeSnippet - ClassifierType::ClassLabelType label = m_iter.GetClassLabel(); -// Software Guide : EndCodeSnippet - -// Software Guide : BeginLatex -// -// And we compare it to the corresponding one of the test set. -// -// Software Guide : EndLatex - -// Software Guide : BeginCodeSnippet - InputVectorType measure; - - tPSet->GetPointData(pointId, &measure); - - ClassifierType::ClassLabelType expectedLabel; - if (measure[0] < measure[1]) expectedLabel = -1; - else expectedLabel = 1; - - double dist = fabs(measure[0] - measure[1]); - - if (label != expectedLabel) error++; - - std::cout << int(label) << "/" << int(expectedLabel) << " --- " << dist << - std::endl; - - ++pointId; - ++m_iter; - } - - std::cout << "Error = " << error / pointId << " % " << std::endl; -// Software Guide : EndCodeSnippet - - return EXIT_SUCCESS; -} diff --git a/Examples/Learning/SVMPointSetExample.cxx b/Examples/Learning/SVMPointSetExample.cxx deleted file mode 100644 index 6c60460cc3211434eb089be045569299c6c48b88..0000000000000000000000000000000000000000 --- a/Examples/Learning/SVMPointSetExample.cxx +++ /dev/null @@ -1,219 +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 "itkMacro.h" -#include <iostream> -#include <cstdlib> - -#include "otbSVMPointSetModelEstimator.h" -#include "itkPointSetToListSampleAdaptor.h" -#include "otbSVMClassifier.h" - -int main(int itkNotUsed(argc), char* itkNotUsed(argv)[]) -{ - - typedef float InputPixelType; - - typedef std::vector<InputPixelType> InputVectorType; - typedef int LabelPixelType; - const unsigned int Dimension = 2; - - typedef itk::PointSet<InputVectorType, Dimension> MeasurePointSetType; - - typedef itk::PointSet<LabelPixelType, Dimension> LabelPointSetType; - - MeasurePointSetType::Pointer mPSet = MeasurePointSetType::New(); - LabelPointSetType::Pointer lPSet = LabelPointSetType::New(); - - typedef MeasurePointSetType::PointType MeasurePointType; - typedef LabelPointSetType::PointType LabelPointType; - - typedef MeasurePointSetType::PointsContainer MeasurePointsContainer; - typedef LabelPointSetType::PointsContainer LabelPointsContainer; - - MeasurePointsContainer::Pointer mCont = MeasurePointsContainer::New(); - LabelPointsContainer::Pointer lCont = LabelPointsContainer::New(); - - /* We learn the y>x | y<x boundary*/ -// srand((unsigned)time(0)); - srand(0); - int lowest = 0; - int range = 1000; - - unsigned int pointId; - - for (pointId = 0; pointId < 500; pointId++) - { - - MeasurePointType mP; - LabelPointType lP; - - int x_coord = lowest + static_cast<int>(range * (rand() / (RAND_MAX + 1.0))); - int y_coord = lowest + static_cast<int>(range * (rand() / (RAND_MAX + 1.0))); - - std::cout << "coords : " << x_coord << " " << y_coord << std::endl; - mP[0] = x_coord; - mP[1] = y_coord; - - lP[0] = x_coord; - lP[1] = y_coord; - - InputVectorType measure; - measure.push_back(static_cast<InputPixelType>((x_coord * 1.0 - - lowest) / range)); - measure.push_back(static_cast<InputPixelType>((y_coord * 1.0 - - lowest) / range)); - - LabelPixelType label; - - if (x_coord < y_coord) label = 0; - else label = 1; - - std::cout << "Label : " << label << std::endl; - std::cout << "Measures : " << measure[0] << " " << measure[1] << std::endl; - - mCont->InsertElement(pointId, mP); - mPSet->SetPointData(pointId, measure); - - lCont->InsertElement(pointId, lP); - lPSet->SetPointData(pointId, label); - - } - - mPSet->SetPoints(mCont); - lPSet->SetPoints(lCont); - - typedef otb::SVMPointSetModelEstimator<MeasurePointSetType, - LabelPointSetType> EstimatorType; - - EstimatorType::Pointer estimator = EstimatorType::New(); - - estimator->SetInputPointSet(mPSet); - estimator->SetTrainingPointSet(lPSet); - - estimator->Update(); - - std::cout << "Saving model" << std::endl; - estimator->SaveModel("model.svm"); - - // Build the test set - - MeasurePointSetType::Pointer tPSet = MeasurePointSetType::New(); - MeasurePointsContainer::Pointer tCont = MeasurePointsContainer::New(); - - for (pointId = 0; pointId < 100; pointId++) - { - - MeasurePointType tP; - - int x_coord = lowest + static_cast<int>(range * (rand() / (RAND_MAX + 1.0))); - int y_coord = lowest + static_cast<int>(range * (rand() / (RAND_MAX + 1.0))); - - std::cout << "coords : " << x_coord << " " << y_coord << std::endl; - tP[0] = x_coord; - tP[1] = y_coord; - - InputVectorType measure; - measure.push_back(static_cast<InputPixelType>((x_coord * 1.0 - - lowest) / range)); - measure.push_back(static_cast<InputPixelType>((y_coord * 1.0 - - lowest) / range)); - - std::cout << "Measures : " << measure[0] << " " << measure[1] << std::endl; - - tCont->InsertElement(pointId, tP); - tPSet->SetPointData(pointId, measure); - - } - - tPSet->SetPoints(tCont); - - // Classify - - typedef itk::Statistics::PointSetToListSampleAdaptor<MeasurePointSetType> - SampleType; - SampleType::Pointer sample = SampleType::New(); - sample->SetPointSet(tPSet); - - std::cout << "Sample set to Adaptor" << std::endl; - - /** preparing classifier and decision rule object */ - typedef otb::SVMModel<SampleType::MeasurementVectorType::ValueType, - LabelPixelType> ModelType; - - ModelType::Pointer model = estimator->GetModel(); - - int numberOfClasses = model->GetNumberOfClasses(); - - std::cout << "Classification for " << numberOfClasses << " classes " << - std::endl; - - typedef otb::SVMClassifier<SampleType, LabelPixelType> ClassifierType; - - ClassifierType::Pointer classifier = ClassifierType::New(); - - classifier->SetNumberOfClasses(numberOfClasses); - classifier->SetModel(model); - classifier->SetInput(sample.GetPointer()); - classifier->Update(); - - /* Build the class map */ - std::cout << "Output image creation" << std::endl; - - std::cout << "classifier get output" << std::endl; - ClassifierType::OutputType* membershipSample = - classifier->GetOutput(); - std::cout << "Sample iterators" << std::endl; - ClassifierType::OutputType::ConstIterator m_iter = - membershipSample->Begin(); - ClassifierType::OutputType::ConstIterator m_last = - membershipSample->End(); - - double error = 0.0; - pointId = 0; - while (m_iter != m_last) - { - ClassifierType::ClassLabelType label = m_iter.GetClassLabel(); - - InputVectorType measure; - - tPSet->GetPointData(pointId, &measure); - - ClassifierType::ClassLabelType expectedLabel; - if (measure[0] < measure[1]) expectedLabel = 0; - else expectedLabel = 1; - - double dist = fabs(measure[0] - measure[1]); - - if (label != expectedLabel) error++; - - std::cout << int(label) << "/" << int(expectedLabel) << " --- " << dist << - std::endl; - - ++pointId; - ++m_iter; - } - - std::cout << "Error = " << error / pointId << std::endl; - - return EXIT_SUCCESS; -} diff --git a/Examples/Learning/SVMPointSetModelEstimatorExample.cxx b/Examples/Learning/SVMPointSetModelEstimatorExample.cxx deleted file mode 100644 index a351194730eb35b0c6103dde6d00df644cb72a63..0000000000000000000000000000000000000000 --- a/Examples/Learning/SVMPointSetModelEstimatorExample.cxx +++ /dev/null @@ -1,264 +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 "itkMacro.h" -#include "itkPointSet.h" -#include <iostream> -#include <cstdlib> - -// Software Guide : BeginLatex -// -// This example illustrates the use of the -// \doxygen{otb}{SVMPointSetModelEstimator} in order to perform the -// SVM learning from an \doxygen{itk}{PointSet} data structure. -// -// The first step required to use this filter is to include its header file. -// -// Software Guide : EndLatex - -// Software Guide : BeginCodeSnippet -#include "otbSVMPointSetModelEstimator.h" -// Software Guide : EndCodeSnippet - -int main(int itkNotUsed(argc), char* itkNotUsed(argv)[]) -{ - -// Software Guide : BeginLatex -// -// In the framework of supervised learning and classification, we will -// always use feature vectors for the characterization of the -// classes. On the other hand, the class labels are scalar -// values. Here, we start by defining the type of the features as the -// \code{PixelType}, which will be used to define the feature -// \code{VectorType}. We also declare the type for the labels. -// -// Software Guide : EndLatex - -// Software Guide : BeginCodeSnippet - typedef float PixelType; - typedef std::vector<PixelType> VectorType; - typedef int LabelPixelType; -// Software Guide : EndCodeSnippet - const unsigned int Dimension = 2; - -// Software Guide : BeginLatex -// -// We can now proceed to define the point sets used for storing the -// features and the labels. -// -// Software Guide : EndLatex - -// Software Guide : BeginCodeSnippet - typedef itk::PointSet<VectorType, Dimension> FeaturePointSetType; - - typedef itk::PointSet<LabelPixelType, Dimension> LabelPointSetType; - - FeaturePointSetType::Pointer fPSet = FeaturePointSetType::New(); - LabelPointSetType::Pointer lPSet = LabelPointSetType::New(); -// Software Guide : EndCodeSnippet - -// Software Guide : BeginLatex -// -// We will need to get access to the data stored in the point sets, so -// we define the appropriate for the points and the points containers -// used by the point sets (see the section \ref{sec:PointSetSection} -// for more information oin haw to use point sets). -// -// Software Guide : EndLatex - -// Software Guide : BeginCodeSnippet - typedef FeaturePointSetType::PointType FeaturePointType; - typedef LabelPointSetType::PointType LabelPointType; - - typedef FeaturePointSetType::PointsContainer FeaturePointsContainer; - typedef LabelPointSetType::PointsContainer LabelPointsContainer; - - FeaturePointsContainer::Pointer fCont = FeaturePointsContainer::New(); - LabelPointsContainer::Pointer lCont = LabelPointsContainer::New(); -// Software Guide : EndCodeSnippet - -// Software Guide : BeginLatex -// -// We need now to build the training set for the SVM learning. In this -// simple example, we will build a SVM who classes points depending on -// which side of the line $x=y$ they are located. We start by -// generating 500 random points. -// -// Software Guide : EndLatex - - /* We learn the y>x | y<x boundary*/ - srand(0); - -// Software Guide : BeginCodeSnippet - int lowest = 0; - int range = 1000; - - for (unsigned int pointId = 0; pointId < 500; pointId++) - { - - FeaturePointType fP; - LabelPointType lP; - - int x_coord = lowest + static_cast<int>(range * (rand() / (RAND_MAX + 1.0))); - int y_coord = lowest + static_cast<int>(range * (rand() / (RAND_MAX + 1.0))); -// Software Guide : EndCodeSnippet - -// Software Guide : BeginLatex -// -// We set the coordinates of the points. They are the same for the -// feature vector and for the label. -// -// Software Guide : EndLatex - -// Software Guide : BeginCodeSnippet - fP[0] = x_coord; - fP[1] = y_coord; - - lP[0] = x_coord; - lP[1] = y_coord; -// Software Guide : EndCodeSnippet - -// Software Guide : BeginLatex -// -// We push the features in the vector after a normalization which is -// useful for SVM convergence. -// -// Software Guide : EndLatex - -// Software Guide : BeginCodeSnippet - VectorType feature; - feature.push_back(static_cast<PixelType>((x_coord * 1.0 - lowest) / range)); - feature.push_back(static_cast<PixelType>((y_coord * 1.0 - lowest) / range)); -// Software Guide : EndCodeSnippet - -// Software Guide : BeginLatex -// -// We decide on the label for each point. -// -// Software Guide : EndLatex - -// Software Guide : BeginCodeSnippet - LabelPixelType label; - - if (x_coord < y_coord) label = -1; - else label = 1; -// Software Guide : EndCodeSnippet - -// Software Guide : BeginLatex -// -// And we insert the points in the points containers. -// -// Software Guide : EndLatex - -// Software Guide : BeginCodeSnippet - fCont->InsertElement(pointId, fP); - fPSet->SetPointData(pointId, feature); - - lCont->InsertElement(pointId, lP); - lPSet->SetPointData(pointId, label); - - } -// Software Guide : EndCodeSnippet - -// Software Guide : BeginLatex -// -// After the loop, we set the points containers to the point sets. -// -// Software Guide : EndLatex - -// Software Guide : BeginCodeSnippet - fPSet->SetPoints(fCont); - lPSet->SetPoints(lCont); -// Software Guide : EndCodeSnippet - -// Software Guide : BeginLatex -// -// Up to now, we have only prepared the data for the SVM learning. We -// can now create the SVM model estimator. This class is templated -// over the feature and the label point set types. -// \index{otb::SVMPointSetModelEstimator} -// -// Software Guide : EndLatex - -// Software Guide : BeginCodeSnippet - typedef otb::SVMPointSetModelEstimator<FeaturePointSetType, - LabelPointSetType> EstimatorType; - - EstimatorType::Pointer estimator = EstimatorType::New(); -// Software Guide : EndCodeSnippet - -// Software Guide : BeginLatex -// -// The next step consists in setting the point sets for the estimator -// and the number of classes for the model. The feture point set is -// set using the \code{SetInputPointSet} and the label point set is -// set with the \code{SetTrainingPointSet} method. -// -// Software Guide : EndLatex - -// Software Guide : BeginCodeSnippet - estimator->SetInputPointSet(fPSet); - estimator->SetTrainingPointSet(lPSet); -// Software Guide : EndCodeSnippet - -// Software Guide : BeginLatex -// -// The model estimation is triggered by calling the \code{Update} -// method. -// -// Software Guide : EndLatex - -// Software Guide : BeginCodeSnippet - estimator->Update(); -// Software Guide : EndCodeSnippet - -// Software Guide : BeginLatex -// -// Finally, we can save the result of the learning to a file. -// -// Software Guide : EndLatex - -// Software Guide : BeginCodeSnippet - estimator->SaveModel("svm_model.svm"); -// Software Guide : EndCodeSnippet - -// Software Guide : BeginLatex -// -// The \doxygen{otb}{otbSVMModel} class provides several accessors in -// order to get some information about the result of the learning -// step. For instance, one can get the number of support vectors kept -// to define the separation surface by using the -// \code{GetNumberOfSupportVectors()}. This can be very useful to -// detect some kind of overlearning (the number of support vectors is -// close to the number of examples). One can also get the SVs -// themselves by calling the \code {GetSupportVectors()}. The $\alpha$ -// values for the support vectors can be accessed by using the -// \code{GetAlpha()} method. Finally the \code{Evaluate()} method will -// return the result of the classification of a sample and the -// \code{EvaluateHyperplaneDistance()} will return the distance of -// the sample to the separating surface (or surfaces in the case of -// multi-class problems). -// -// Software Guide : EndLatex - - return EXIT_SUCCESS; -} diff --git a/Examples/Learning/test/CMakeLists.txt b/Examples/Learning/test/CMakeLists.txt index b5d2b6f1f83f91122ae15f7026ca6023e5e7b2fb..92581e8f1318ed2aa8b0575aae8dc2ee4be2d026 100644 --- a/Examples/Learning/test/CMakeLists.txt +++ b/Examples/Learning/test/CMakeLists.txt @@ -21,19 +21,7 @@ set(BASELINE ${OTB_DATA_ROOT}/Baseline/Examples/Learning) set(INPUTDATA ${OTB_DATA_ROOT}/Examples) -# ------- SVMImageClassificationExampleTest---------- - if(OTBLibSVM_LOADED) -otb_add_test(NAME leTeSVMImageClassificationExampleTest COMMAND ${OTB_TEST_DRIVER} - --compare-n-images ${NOTOL} 1 - ${BASELINE}/ROI_QB_MUL_1_SVN_CLASS.png - ${TEMP}/ROI_QB_MUL_1_SVN_CLASS.png - Execute $<TARGET_FILE:SVMImageClassificationExample> - ${INPUTDATA}/ROI_QB_MUL_1.png - ${TEMP}/ROI_QB_MUL_1_SVN_CLASS.png - ${OTB_DATA_ROOT}/Examples/svm_image_model.svm -) - # ------- SVMImageEstimatorClassificationMultiExampleTest---------- otb_add_test(NAME leTeSVMImageEstimatorClassificationMultiExampleTest COMMAND ${OTB_TEST_DRIVER} @@ -47,19 +35,6 @@ otb_add_test(NAME leTeSVMImageEstimatorClassificationMultiExampleTest COMMAND ${ ${TEMP}/ROI_QB_MUL_1_SVN_CLASS_MULTI_Rescaled.png ) -# ------- SVMImageModelEstimatorExampleTest---------- - -otb_add_test(NAME leTeSVMImageModelEstimatorExampleTest COMMAND ${OTB_TEST_DRIVER} - --compare-ascii ${EPSILON_3} - ${BASELINE}/svm_image_model.svn - ${TEMP}/svm_image_model.svn - --ignore-lines-with 2 probA probB - Execute $<TARGET_FILE:SVMImageModelEstimatorExample> - ${INPUTDATA}/ROI_QB_MUL_1.png - ${INPUTDATA}/ROI_mask.png - ${TEMP}/svm_image_model.svn -) - endif() # ------- GenerateTrainingImageExampleTest----------