OTB  9.0.0
Orfeo Toolbox
otbDimensionalityReductionTrainSOM.hxx
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2005-2022 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>
32 {
33  AddChoice("algorithm.som", "OTB SOM");
34  SetParameterDescription("algorithm.som", "This group of parameters allows setting SOM parameters. ");
35 
36  AddParameter(ParameterType_StringList, "algorithm.som.s", "Map size");
37  SetParameterDescription("algorithm.som.s",
38  "Sizes of the SOM map (one per "
39  "dimension). For instance, [12;15] means a 2D map of size 12x15. Support"
40  "2D to 5D maps.");
41  MandatoryOff("algorithm.som.s");
42 
43  AddParameter(ParameterType_StringList, "algorithm.som.n", "Neighborhood sizes");
44  SetParameterDescription("algorithm.som.n",
45  "Sizes of the initial neighborhood "
46  "in the SOM map (one per dimension). The number of sizes should be the same"
47  " as the map sizes");
48  MandatoryOff("algorithm.som.n");
49 
50  AddParameter(ParameterType_Int, "algorithm.som.ni", "NumberIteration");
51  SetParameterDescription("algorithm.som.ni", "Number of iterations for SOM learning");
52  MandatoryOff("algorithm.som.ni");
53 
54  AddParameter(ParameterType_Float, "algorithm.som.bi", "BetaInit");
55  SetParameterDescription("algorithm.som.bi", "Initial learning coefficient");
56  MandatoryOff("algorithm.som.bi");
57 
58  AddParameter(ParameterType_Float, "algorithm.som.bf", "BetaFinal");
59  SetParameterDescription("algorithm.som.bf", "Final learning coefficient");
60  MandatoryOff("algorithm.som.bf");
61 
62  AddParameter(ParameterType_Float, "algorithm.som.iv", "InitialValue");
63  SetParameterDescription("algorithm.som.iv", "Maximum initial neuron weight");
64  MandatoryOff("algorithm.som.iv");
65 
66  std::vector<std::string> size(2, std::string("10"));
67  std::vector<std::string> radius(2, std::string("3"));
68  SetParameterStringList("algorithm.som.s", size, false);
69  SetParameterStringList("algorithm.som.n", radius, false);
70  DisableParameter("algorithm.som.s");
71  DisableParameter("algorithm.som.n");
72 
73  SetDefaultParameterInt("algorithm.som.ni", 5);
74  SetDefaultParameterFloat("algorithm.som.bi", 1.0);
75  SetDefaultParameterFloat("algorithm.som.bf", 0.1);
76  SetDefaultParameterFloat("algorithm.som.iv", 10.0);
77 }
78 
79 template <class TInputValue, class TOutputValue>
80 void TrainDimensionalityReductionApplicationBase<TInputValue, TOutputValue>::BeforeTrainSOM(typename ListSampleType::Pointer trainingListSample,
81  std::string modelPath)
82 {
83  std::vector<std::string> s = GetParameterStringList("algorithm.som.s");
84  int SomDim = s.size();
85 
86  if (SomDim == 2)
87  {
88  typedef otb::SOMModel<InputValueType, 2> SOM2DModelType;
89  TrainSOM<SOM2DModelType>(trainingListSample, modelPath);
90  }
91 
92  if (SomDim == 3)
93  {
94  typedef otb::SOMModel<InputValueType, 3> SOM3DModelType;
95  TrainSOM<SOM3DModelType>(trainingListSample, modelPath);
96  }
97 
98  if (SomDim == 4)
99  {
100  typedef otb::SOMModel<InputValueType, 4> SOM4DModelType;
101  TrainSOM<SOM4DModelType>(trainingListSample, modelPath);
102  }
103 
104  if (SomDim == 5)
105  {
106  typedef otb::SOMModel<InputValueType, 5> SOM5DModelType;
107  TrainSOM<SOM5DModelType>(trainingListSample, modelPath);
108  }
109  if (SomDim > 5 || SomDim < 2)
110  {
111  otbAppLogFATAL(<< "Invalid number of dimensions : " << SomDim << ". Only support 2, 3, 4 or 5 dimensions");
112  }
113 }
114 
115 template <class TInputValue, class TOutputValue>
116 template <typename TSOM>
117 void TrainDimensionalityReductionApplicationBase<TInputValue, TOutputValue>::TrainSOM(typename ListSampleType::Pointer trainingListSample,
118  std::string modelPath)
119 {
120  typename TSOM::Pointer dimredTrainer = TSOM::New();
121  dimredTrainer->SetNumberOfIterations(GetParameterInt("algorithm.som.ni"));
122  dimredTrainer->SetBetaInit(GetParameterFloat("algorithm.som.bi"));
123  dimredTrainer->SetWriteMap(true);
124  dimredTrainer->SetBetaEnd(GetParameterFloat("algorithm.som.bf"));
125  dimredTrainer->SetMaxWeight(GetParameterFloat("algorithm.som.iv"));
126  typename TSOM::SizeType size;
127  std::vector<std::string> s = GetParameterStringList("algorithm.som.s");
128  for (unsigned int i = 0; i < s.size(); i++)
129  {
130  size[i] = boost::lexical_cast<unsigned int>(s[i]);
131  }
132 
133  dimredTrainer->SetMapSize(size);
134  typename TSOM::SizeType radius;
135  std::vector<std::string> n = GetParameterStringList("algorithm.som.n");
136  if (n.size() != s.size())
137  {
138  otbAppLogFATAL(<< "Wrong number of neighborhood radii : expected " << s.size() << " ; got " << n.size());
139  }
140  for (unsigned int i = 0; i < n.size(); i++)
141  {
142  radius[i] = boost::lexical_cast<unsigned int>(n[i]);
143  }
144  dimredTrainer->SetNeighborhoodSizeInit(radius);
145  dimredTrainer->SetInputListSample(trainingListSample);
146  dimredTrainer->Train();
147  dimredTrainer->Save(modelPath);
148 }
149 
150 } // end namespace wrapper
151 } // end namespace otb
152 
153 #endif
otb::Wrapper::TrainDimensionalityReductionApplicationBase::InitSOMParams
void InitSOMParams()
Definition: otbDimensionalityReductionTrainSOM.hxx:31
otbAppLogFATAL
#define otbAppLogFATAL(x)
Definition: otbWrapperMacros.h:25
otb::SOMModel
Definition: otbSOMModel.h:42
otb::Wrapper::TrainDimensionalityReductionApplicationBase::BeforeTrainSOM
void BeforeTrainSOM(typename ListSampleType::Pointer trainingListSample, std::string modelPath)
Definition: otbDimensionalityReductionTrainSOM.hxx:80
otb
The "otb" namespace contains all Orfeo Toolbox (OTB) classes.
Definition: otbJoinContainer.h:32
otb::Wrapper::ParameterType_Int
@ ParameterType_Int
Definition: otbWrapperTypes.h:38
otbTrainDimensionalityReductionApplicationBase.h
otb::Wrapper::ParameterType_Float
@ ParameterType_Float
Definition: otbWrapperTypes.h:39
otbSOMModel.h
otb::Wrapper::ParameterType_StringList
@ ParameterType_StringList
Definition: otbWrapperTypes.h:42
otb::Wrapper::TrainDimensionalityReductionApplicationBase::TrainSOM
void TrainSOM(typename ListSampleType::Pointer trainingListSample, std::string modelPath)
Definition: otbDimensionalityReductionTrainSOM.hxx:117