File: TrainMachineLearningModelFromSamplesExample.cxx

package info (click to toggle)
otb 5.8.0%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 38,496 kB
  • ctags: 40,282
  • sloc: cpp: 306,573; ansic: 3,575; python: 450; sh: 214; perl: 74; java: 72; makefile: 70
file content (183 lines) | stat: -rw-r--r-- 6,796 bytes parent folder | download
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
/*=========================================================================

  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: {1000}, {4}, {5}, {121212}
//    OUTPUTS: {clSVMModelFromSamples.svm}
//  Software Guide : EndCommandLineArgs

//  Software Guide : BeginLatex
//
// This example illustrates the use of the \doxygen{otb}{SVMMachineLearningModel} class, which inherits from the
// \doxygen{otb}{MachineLearningModel} class. This class allows the
// estimation of a classification model (supervised learning) from samples. In this example, we will train an SVM model
// with 4 output classes, from 1000 randomly generated training samples, each of them having 7 components.
// We start by including the appropriate header files.
//
//  Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
// List sample generator
#include "otbListSampleGenerator.h"

// Random number generator

// SVM model Estimator
#include "otbSVMMachineLearningModel.h"
// Software Guide : EndCodeSnippet


int main(int argc, char* argv[])
{

  /*
  if (argc != 6)
    {
    std::cerr << "Usage: " << argv[0] << " nbSamples nbSampleComponents nbClasses inputSeed outputModelFileName" << std::endl;
    return EXIT_FAILURE;
    }
  */

  if (argc != 2)
    {
    std::cerr << "Usage: " << argv[0] << " outputModelFileName" << std::endl;
    return EXIT_FAILURE;
    }

  // Software Guide : BeginLatex
  // The input parameters of the sample generator and of the SVM classifier are initialized.
  // Software Guide : EndLatex

  /*
  int nbSamples = atoi(argv[1]);
  int nbSampleComponents = atoi(argv[2]);
  int nbClasses = atoi(argv[3]);
  unsigned int inputSeed = atoi(argv[4]);
  */

  // Software Guide : BeginCodeSnippet
  int nbSamples = 1000;
  int nbSampleComponents = 7;
  int nbClasses = 4;
  // Software Guide : EndCodeSnippet

//  unsigned int inputSeed = 121212;
  const char* outputModelFileName = argv[1]; //argv[5];


  // Software Guide : BeginLatex
  // Two lists are generated into a \subdoxygen{itk}{Statistics}{ListSample} which is the structure
  // used to handle both lists of samples and of labels for the machine learning classes derived from
  // \doxygen{otb}{MachineLearningModel}. The first list is composed of feature vectors representing
  // multi-component samples, and the second one is filled with their corresponding class labels. The
  // list of labels is composed of scalar values.
  // Software Guide : EndLatex


  // Software Guide : BeginCodeSnippet
  // Input related typedefs
  typedef float InputValueType;
  typedef itk::VariableLengthVector<InputValueType> InputSampleType;
  typedef itk::Statistics::ListSample<InputSampleType> InputListSampleType;

  // Target related typedefs
  typedef int TargetValueType;
  typedef itk::FixedArray<TargetValueType, 1> TargetSampleType;
  typedef itk::Statistics::ListSample<TargetSampleType> TargetListSampleType;

  InputListSampleType::Pointer InputListSample = InputListSampleType::New();
  TargetListSampleType::Pointer TargetListSample = TargetListSampleType::New();

  InputListSample->SetMeasurementVectorSize(nbSampleComponents);
  // Software Guide : EndCodeSnippet


  // Software Guide : BeginLatex
  // In this example, the list of multi-component training samples is randomly filled with a random number
  // generator based on the \subdoxygen{itk}{Statistics}{MersenneTwisterRandomVariateGenerator} class.
  // Each component's value is generated from a normal law centered around the corresponding class label of
  // each sample multiplied by 100, with a standard deviation of 10.
  // Software Guide : EndLatex


  // Software Guide : BeginCodeSnippet
  itk::Statistics::MersenneTwisterRandomVariateGenerator::Pointer randGen;
  randGen = itk::Statistics::MersenneTwisterRandomVariateGenerator::GetInstance();

  // Filling the two input training lists
  for (int i = 0; i < nbSamples; ++i)
    {
    InputSampleType sample;
    TargetValueType label = (i % nbClasses) + 1;

    // Multi-component sample randomly filled from a normal law for each component
    sample.SetSize(nbSampleComponents);
    for (int itComp = 0; itComp < nbSampleComponents; ++itComp)
      {
      sample[itComp] = randGen->GetNormalVariate(100 * label, 10);
      }

    InputListSample->PushBack(sample);
    TargetListSample->PushBack(label);
    }
  // Software Guide : EndCodeSnippet

  // Displays the corresponding values (not into the Software Guide)
  for (int i = 0; i < nbSamples; ++i)
    {
    std::cout << i + 1 << "-label = " << TargetListSample->GetMeasurementVector(i) << std::endl;
    std::cout << "sample = " << InputListSample->GetMeasurementVector(i) << std::endl << std::endl;
    }

  // Software Guide : BeginLatex
  //Once both sample and label lists are generated, the second step consists in
  //declaring the machine learning classifier. In our case we use an SVM model
  //with the help of the \doxygen{otb}{SVMMachineLearningModel} class which is
  //derived from the \doxygen{otb}{MachineLearningModel} class.
  //This pure virtual class is based on the machine learning framework of the
  //OpenCV library (\cite{opencv_library}) which handles other classifiers than
  //the SVM.
  // Software Guide : EndLatex

  // Software Guide : BeginCodeSnippet
  typedef otb::SVMMachineLearningModel<InputValueType, TargetValueType> SVMType;

  SVMType::Pointer SVMClassifier = SVMType::New();

  SVMClassifier->SetInputListSample(InputListSample);
  SVMClassifier->SetTargetListSample(TargetListSample);

  SVMClassifier->SetKernelType(CvSVM::LINEAR);
  // Software Guide : EndCodeSnippet

  // Software Guide : BeginLatex
  //
  // Once the classifier is parametrized with both input lists and default parameters, except
  // for the kernel type in our example of SVM model estimation, the model
  // training is computed with the \code{Train} method. Finally, the \code{Save} method
  // exports the model to a text file. All the available classifiers based on OpenCV are
  // implemented with these interfaces. Like for the SVM model training, the other classifiers
  // can be parametrized with specific settings.
  //
  // Software Guide : EndLatex

  // Software Guide : BeginCodeSnippet
  SVMClassifier->Train();
  SVMClassifier->Save(outputModelFileName);
  // Software Guide : EndCodeSnippet

}