OTB  6.7.0
Orfeo Toolbox
otbDimensionalityReductionTrainSOM.hxx
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2005-2019 Centre National d'Etudes Spatiales (CNES)
3  *
4  * This file is part of Orfeo Toolbox
5  *
6  * https://www.orfeo-toolbox.org/
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  */
20 #ifndef otbDimensionalityReductionTrainSOM_hxx
21 #define otbDimensionalityReductionTrainSOM_hxx
23 #include "otbSOMModel.h"
24 
25 namespace otb
26 {
27 namespace Wrapper
28 {
29 
30 template <class TInputValue, class TOutputValue>
31 void
34 {
35  AddChoice("algorithm.som", "OTB SOM");
36  SetParameterDescription("algorithm.som",
37  "This group of parameters allows setting SOM parameters. ");
38 
39  AddParameter(ParameterType_StringList , "algorithm.som.s", "Map size");
40  SetParameterDescription("algorithm.som.s", "Sizes of the SOM map (one per "
41  "dimension). For instance, [12;15] means a 2D map of size 12x15. Support"
42  "2D to 5D maps.");
43  MandatoryOff("algorithm.som.s");
44 
45  AddParameter(ParameterType_StringList , "algorithm.som.n", "Neighborhood sizes");
46  SetParameterDescription("algorithm.som.n", "Sizes of the initial neighborhood "
47  "in the SOM map (one per dimension). The number of sizes should be the same"
48  " as the map sizes");
49  MandatoryOff("algorithm.som.n");
50 
51  AddParameter(ParameterType_Int, "algorithm.som.ni", "NumberIteration");
52  SetParameterDescription("algorithm.som.ni", "Number of iterations for SOM learning");
53  MandatoryOff("algorithm.som.ni");
54 
55  AddParameter(ParameterType_Float, "algorithm.som.bi", "BetaInit");
56  SetParameterDescription("algorithm.som.bi", "Initial learning coefficient");
57  MandatoryOff("algorithm.som.bi");
58 
59  AddParameter(ParameterType_Float, "algorithm.som.bf", "BetaFinal");
60  SetParameterDescription("algorithm.som.bf", "Final learning coefficient");
61  MandatoryOff("algorithm.som.bf");
62 
63  AddParameter(ParameterType_Float, "algorithm.som.iv", "InitialValue");
64  SetParameterDescription("algorithm.som.iv", "Maximum initial neuron weight");
65  MandatoryOff("algorithm.som.iv");
66 
67  std::vector<std::string> size(2, std::string("10"));
68  std::vector<std::string> radius(2, std::string("3"));
69  SetParameterStringList("algorithm.som.s", size, false);
70  SetParameterStringList("algorithm.som.n", radius, false);
71  DisableParameter("algorithm.som.s");
72  DisableParameter("algorithm.som.n");
73 
74  SetDefaultParameterInt("algorithm.som.ni", 5);
75  SetDefaultParameterFloat("algorithm.som.bi", 1.0);
76  SetDefaultParameterFloat("algorithm.som.bf", 0.1);
77  SetDefaultParameterFloat("algorithm.som.iv", 10.0);
78 }
79 
80 template <class TInputValue, class TOutputValue>
81 void
83 ::BeforeTrainSOM(typename ListSampleType::Pointer trainingListSample,
84  std::string modelPath)
85 {
86  std::vector<std::string> s = GetParameterStringList("algorithm.som.s");
87  int SomDim = s.size();
88 
89  if(SomDim == 2)
90  {
91  typedef otb::SOMModel<InputValueType, 2> SOM2DModelType;
92  TrainSOM<SOM2DModelType >(trainingListSample,modelPath);
93  }
94 
95  if(SomDim == 3)
96  {
97  typedef otb::SOMModel<InputValueType, 3> SOM3DModelType;
98  TrainSOM<SOM3DModelType >(trainingListSample,modelPath);
99  }
100 
101  if(SomDim == 4)
102  {
103  typedef otb::SOMModel<InputValueType, 4> SOM4DModelType;
104  TrainSOM<SOM4DModelType >(trainingListSample,modelPath);
105  }
106 
107  if(SomDim == 5)
108  {
109  typedef otb::SOMModel<InputValueType, 5> SOM5DModelType;
110  TrainSOM<SOM5DModelType >(trainingListSample,modelPath);
111  }
112  if(SomDim > 5 || SomDim < 2)
113  {
114  otbAppLogFATAL(<< "Invalid number of dimensions : " << SomDim <<
115  ". Only support 2, 3, 4 or 5 dimensions");
116  }
117 }
118 
119 template <class TInputValue, class TOutputValue>
120 template <typename TSOM>
122 ::TrainSOM(typename ListSampleType::Pointer trainingListSample,std::string modelPath)
123 {
124  typename TSOM::Pointer dimredTrainer = TSOM::New();
125  dimredTrainer->SetNumberOfIterations(GetParameterInt("algorithm.som.ni"));
126  dimredTrainer->SetBetaInit(GetParameterFloat("algorithm.som.bi"));
127  dimredTrainer->SetWriteMap(true);
128  dimredTrainer->SetBetaEnd(GetParameterFloat("algorithm.som.bf"));
129  dimredTrainer->SetMaxWeight(GetParameterFloat("algorithm.som.iv"));
130  typename TSOM::SizeType size;
131  std::vector<std::string> s = GetParameterStringList("algorithm.som.s");
132  for (unsigned int i=0; i<s.size(); i++)
133  {
134  size[i]=boost::lexical_cast<unsigned int>(s[i]);
135  }
136 
137  dimredTrainer->SetMapSize(size);
138  typename TSOM::SizeType radius;
139  std::vector<std::string> n = GetParameterStringList("algorithm.som.n");
140  if (n.size() != s.size())
141  {
142  otbAppLogFATAL(<< "Wrong number of neighborhood radii : expected "<< s.size() << " ; got "<< n.size());
143  }
144  for (unsigned int i=0; i < n.size(); i++)
145  {
146  radius[i]=boost::lexical_cast<unsigned int>(n[i]);
147  }
148  dimredTrainer->SetNeighborhoodSizeInit(radius);
149  dimredTrainer->SetInputListSample(trainingListSample);
150  dimredTrainer->Train();
151  dimredTrainer->Save(modelPath);
152 }
153 
154 } //end namespace wrapper
155 } //end namespace otb
156 
157 #endif
itk::Size< Monteverdi_DIMENSION > SizeType
Definition: mvdTypes.h:137
#define otbAppLogFATAL(x)
void TrainSOM(typename ListSampleType::Pointer trainingListSample, std::string modelPath)
void BeforeTrainSOM(typename ListSampleType::Pointer trainingListSample, std::string modelPath)