OTB  9.0.0
Orfeo Toolbox
otbSOM.hxx
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2005-2022 Centre National d'Etudes Spatiales (CNES)
3  * Copyright (C) 2007-2012 Institut Mines Telecom / Telecom Bretagne
4  *
5  * This file is part of Orfeo Toolbox
6  *
7  * https://www.orfeo-toolbox.org/
8  *
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  * http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  */
21 
22 #ifndef otbSOM_hxx
23 #define otbSOM_hxx
24 
25 #include "otbSOM.h"
26 #include "itkImageRegionIteratorWithIndex.h"
27 #include "itkFixedArray.h"
28 #include "otbMacro.h"
29 #include "itkImageRegionIterator.h"
30 #include "itkMersenneTwisterRandomVariateGenerator.h"
31 
32 namespace otb
33 {
37 template <class TListSample, class TMap, class TSOMLearningBehaviorFunctor, class TSOMNeighborhoodBehaviorFunctor>
39 {
40  this->SetNumberOfRequiredInputs(0);
41  this->SetNumberOfRequiredOutputs(1);
43 
44  m_MapSize.Fill(10);
45  m_NumberOfIterations = 10;
46  m_BetaInit = 1.0;
47  m_BetaEnd = 0.2;
48  m_NeighborhoodSizeInit.Fill(3);
49  m_MinWeight = static_cast<ValueType>(0.0);
50  m_MaxWeight = static_cast<ValueType>(128.0);
51  m_RandomInit = false;
52  m_Seed = 123574651;
53 }
57 template <class TListSample, class TMap, class TSOMLearningBehaviorFunctor, class TSOMNeighborhoodBehaviorFunctor>
59 {
60 }
61 
68 template <class TListSample, class TMap, class TSOMLearningBehaviorFunctor, class TSOMNeighborhoodBehaviorFunctor>
70 {
71  // output map pointer
72  MapPointerType map = this->GetOutput(0);
73 
74  // typedefs
75  typedef itk::ImageRegionIteratorWithIndex<MapType> IteratorType;
76  typedef itk::FixedArray<double, MapType::ImageDimension> FixedArrayIndexType;
77  typedef itk::Statistics::EuclideanDistanceMetric<FixedArrayIndexType> DistanceType;
78  typename DistanceType::Pointer distance = DistanceType::New();
79 
80  // winner index in the map
81  IndexType position = map->GetWinner(sample);
82  NeuronType winner = map->GetPixel(position);
83 
84  // Local neighborhood definition
85  RegionType localRegion;
86  IndexType localIndex = position - radius;
87  SizeType localSize;
88  for (unsigned int i = 0; i < MapType::ImageDimension; ++i)
89  {
90  localSize[i] = 2 * radius[i] + 1;
91  }
92  localRegion.SetIndex(localIndex);
93  localRegion.SetSize(localSize);
94  localRegion.Crop(map->GetLargestPossibleRegion());
95  IteratorType it(map, localRegion);
96 
97  // Walk through the map, and evolve each neuron depending on its
98  // distance to the winner.
99  for (it.GoToBegin(); !it.IsAtEnd(); ++it)
100  {
101  NeuronType tempNeuron = it.Get();
102  NeuronType newNeuron(tempNeuron.Size());
103 
104  FixedArrayIndexType positionFA, indexFA;
105  positionFA[0] = position[0];
106  positionFA[1] = position[1];
107 
108  indexFA[0] = it.GetIndex()[0];
109  indexFA[1] = it.GetIndex()[1];
110 
111  double tempBeta = beta / (1 + distance->Evaluate(positionFA, indexFA));
112 
113  for (unsigned int i = 0; i < newNeuron.Size(); ++i)
114  {
115  newNeuron[i] = tempNeuron[i] + static_cast<typename NeuronType::ValueType>((sample[i] - tempNeuron[i]) * tempBeta);
116  }
117 
118  it.Set(newNeuron);
119  }
120 }
124 template <class TListSample, class TMap, class TSOMLearningBehaviorFunctor, class TSOMNeighborhoodBehaviorFunctor>
126 {
127  // Compute the new learning coefficient
128  double newBeta = m_BetaFunctor(currentIteration, m_NumberOfIterations, m_BetaInit, m_BetaEnd);
129 
130  // Compute the new neighborhood size
131  SizeType newSize = m_NeighborhoodSizeFunctor(currentIteration, m_NumberOfIterations, m_NeighborhoodSizeInit);
132 
133  // update the neurons map with each example of the training set.
134  otbMsgDebugMacro(<< "Beta: " << newBeta << ", radius: " << newSize);
135  for (typename ListSampleType::Iterator it = m_ListSample->Begin(); it != m_ListSample->End(); ++it)
136  {
137  UpdateMap(it.GetMeasurementVector(), newBeta, newSize);
138  }
139 }
143 template <class TListSample, class TMap, class TSOMLearningBehaviorFunctor, class TSOMNeighborhoodBehaviorFunctor>
145 {
146  Superclass::GenerateOutputInformation();
147  this->GetOutput()->SetNumberOfComponentsPerPixel(m_ListSample->GetMeasurementVectorSize());
149 
150  IndexType index;
151  index.Fill(0);
152 
153  RegionType region;
154  region.SetIndex(index);
155  region.SetSize(this->GetMapSize());
156  this->GetOutput()->SetRegions(region);
157 }
161 template <class TListSample, class TMap, class TSOMLearningBehaviorFunctor, class TSOMNeighborhoodBehaviorFunctor>
163 {
164  if (this->GetNumberOfOutputs() != 1)
165  itkExceptionMacro(<< "Number of output image should be 1");
166 
167  // output neuron map fill
168  MapPointerType map = this->GetOutput(0);
169  map->Allocate();
170 }
171 
175 template <class TListSample, class TMap, class TSOMLearningBehaviorFunctor, class TSOMNeighborhoodBehaviorFunctor>
177 {
178  this->AllocateOutputs();
179  this->BeforeThreadedGenerateData();
181 
182  MapPointerType map = this->GetOutput(0);
183 
184  if (m_RandomInit)
185  {
186  typedef itk::Statistics::MersenneTwisterRandomVariateGenerator GeneratorType;
187  typedef itk::ImageRegionIterator<MapType> IteratorType;
188  GeneratorType::Pointer generator = GeneratorType::New();
189  generator->Initialize(m_Seed);
190 
191  NeuronType neuronInit(m_ListSample->GetMeasurementVectorSize());
192  IteratorType it(map, map->GetLargestPossibleRegion());
193 
194  for (it.GoToBegin(); !it.IsAtEnd(); ++it)
195  {
196  for (unsigned int i = 0; i < neuronInit.Size(); ++i)
197  {
198  neuronInit[i] =
199  static_cast<typename NeuronType::ValueType>(generator->GetUniformVariate(static_cast<double>(m_MinWeight), static_cast<double>(m_MaxWeight)));
200  }
201  it.Set(neuronInit);
202  }
203  }
204  else
205  {
206  NeuronType neuronInit(m_ListSample->GetMeasurementVectorSize());
207  neuronInit.Fill(m_MaxWeight);
208  map->FillBuffer(neuronInit);
209  }
210 
211  // Step through the iterations
212  for (unsigned int i = 0; i < m_NumberOfIterations; ++i)
213  {
214  // otbMsgDebugMacro(<<"Step "<<i+1<<" / "<<m_NumberOfIterations);
215  std::cerr << "Step " << i + 1 << " / " << m_NumberOfIterations << " \r";
216  Step(i);
217  }
218 
219  this->AfterThreadedGenerateData();
220 }
224 template <class TListSample, class TMap, class TSOMLearningBehaviorFunctor, class TSOMNeighborhoodBehaviorFunctor>
226 {
227  Superclass::PrintSelf(os, indent);
228 }
229 
230 } // end namespace otb
231 
232 #endif
otb::SOM::GenerateOutputInformation
void GenerateOutputInformation() override
Definition: otbSOM.hxx:144
otb::SOM::~SOM
~SOM() override
Definition: otbSOM.hxx:58
otb::SOM::Step
virtual void Step(unsigned int currentIteration)
Definition: otbSOM.hxx:125
otb::SOM::PrintSelf
void PrintSelf(std::ostream &os, itk::Indent indent) const override
Definition: otbSOM.hxx:225
otb::SOM::UpdateMap
virtual void UpdateMap(const NeuronType &sample, double beta, SizeType &radius)
Definition: otbSOM.hxx:69
otb
The "otb" namespace contains all Orfeo Toolbox (OTB) classes.
Definition: otbJoinContainer.h:32
otb::SOM< TListSample, TMap, Functor::CzihoSOMLearningBehaviorFunctor, Functor::CzihoSOMNeighborhoodBehaviorFunctor >::IndexType
MapType::IndexType IndexType
Definition: otbSOM.h:81
otb::SOM< TListSample, TMap, Functor::CzihoSOMLearningBehaviorFunctor, Functor::CzihoSOMNeighborhoodBehaviorFunctor >::SizeType
MapType::SizeType SizeType
Definition: otbSOM.h:82
otbMacro.h
otb::SOM< TListSample, TMap, Functor::CzihoSOMLearningBehaviorFunctor, Functor::CzihoSOMNeighborhoodBehaviorFunctor >::RegionType
MapType::RegionType RegionType
Definition: otbSOM.h:83
otb::SOM< TListSample, TMap, Functor::CzihoSOMLearningBehaviorFunctor, Functor::CzihoSOMNeighborhoodBehaviorFunctor >::ValueType
NeuronType::ValueType ValueType
Definition: otbSOM.h:80
otb::SOM< TListSample, TMap, Functor::CzihoSOMLearningBehaviorFunctor, Functor::CzihoSOMNeighborhoodBehaviorFunctor >::MapPointerType
MapType::Pointer MapPointerType
Definition: otbSOM.h:84
otbMsgDebugMacro
#define otbMsgDebugMacro(x)
Definition: otbMacro.h:62
otbSOM.h
otb::SOM::SOM
SOM()
Definition: otbSOM.hxx:38
otb::SOM::GenerateData
void GenerateData(void) override
Definition: otbSOM.hxx:176
otb::SOM::AllocateOutputs
void AllocateOutputs() override
Definition: otbSOM.hxx:162
otb::SOM< TListSample, TMap, Functor::CzihoSOMLearningBehaviorFunctor, Functor::CzihoSOMNeighborhoodBehaviorFunctor >::NeuronType
MapType::PixelType NeuronType
Definition: otbSOM.h:79