20 #ifndef otbDimensionalityReductionTrainSOM_hxx
21 #define otbDimensionalityReductionTrainSOM_hxx
30 template <
class TInputValue,
class TOutputValue>
33 AddChoice(
"algorithm.som",
"OTB SOM");
34 SetParameterDescription(
"algorithm.som",
"This group of parameters allows setting SOM parameters. ");
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"
41 MandatoryOff(
"algorithm.som.s");
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"
48 MandatoryOff(
"algorithm.som.n");
51 SetParameterDescription(
"algorithm.som.ni",
"Number of iterations for SOM learning");
52 MandatoryOff(
"algorithm.som.ni");
55 SetParameterDescription(
"algorithm.som.bi",
"Initial learning coefficient");
56 MandatoryOff(
"algorithm.som.bi");
59 SetParameterDescription(
"algorithm.som.bf",
"Final learning coefficient");
60 MandatoryOff(
"algorithm.som.bf");
63 SetParameterDescription(
"algorithm.som.iv",
"Maximum initial neuron weight");
64 MandatoryOff(
"algorithm.som.iv");
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");
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);
79 template <
class TInputValue,
class TOutputValue>
81 std::string modelPath)
83 std::vector<std::string> s = GetParameterStringList(
"algorithm.som.s");
84 int SomDim = s.size();
89 TrainSOM<SOM2DModelType>(trainingListSample, modelPath);
95 TrainSOM<SOM3DModelType>(trainingListSample, modelPath);
101 TrainSOM<SOM4DModelType>(trainingListSample, modelPath);
107 TrainSOM<SOM5DModelType>(trainingListSample, modelPath);
109 if (SomDim > 5 || SomDim < 2)
111 otbAppLogFATAL(<<
"Invalid number of dimensions : " << SomDim <<
". Only support 2, 3, 4 or 5 dimensions");
115 template <
class TInputValue,
class TOutputValue>
116 template <
typename TSOM>
118 std::string modelPath)
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++)
130 size[i] = boost::lexical_cast<unsigned int>(s[i]);
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())
138 otbAppLogFATAL(<<
"Wrong number of neighborhood radii : expected " << s.size() <<
" ; got " << n.size());
140 for (
unsigned int i = 0; i < n.size(); i++)
142 radius[i] = boost::lexical_cast<unsigned int>(n[i]);
144 dimredTrainer->SetNeighborhoodSizeInit(radius);
145 dimredTrainer->SetInputListSample(trainingListSample);
146 dimredTrainer->Train();
147 dimredTrainer->Save(modelPath);