1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319
|
/*=========================================================================
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.
=========================================================================*/
// 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;
}
|