OTB  9.0.0
Orfeo Toolbox
otbSOMModel.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 otbSOMModel_hxx
21 #define otbSOMModel_hxx
22 
23 #include "otbSOMModel.h"
24 #include "otbSOM.h"
25 
26 #include "itkMacro.h"
27 #include "itkImageRegionIterator.h"
28 #include "itkImageRegionConstIterator.h"
29 #include "itkImage.h"
30 
31 #include <fstream>
32 
33 namespace otb
34 {
35 
36 namespace internal
37 {
38 template <typename T>
39 std::ostream& BinaryWrite(std::ostream& stream, const T& value)
40 {
41  return stream.write(reinterpret_cast<const char*>(&value), sizeof(T));
42 }
43 
44 std::ostream& BinaryWriteString(std::ofstream& stream, const std::string& value)
45 {
46  return stream.write(value.c_str(), value.length());
47 }
48 
49 template <typename T>
50 std::istream& BinaryRead(std::istream& stream, T& value)
51 {
52  return stream.read(reinterpret_cast<char*>(&value), sizeof(T));
53 }
54 } // end of namespace internal
55 
56 template <class TInputValue, unsigned int MapDimension>
58 {
59  this->m_Dimension = MapType::ImageDimension;
60 }
61 
62 template <class TInputValue, unsigned int MapDimension>
64 {
65 }
66 
67 template <class TInputValue, unsigned int MapDimension>
69 {
70  typedef otb::SOM<InputListSampleType, MapType> EstimatorType;
71  typename EstimatorType::Pointer estimator = EstimatorType::New();
72  estimator->SetListSample(this->GetInputListSample());
73  estimator->SetMapSize(m_MapSize);
74  estimator->SetNeighborhoodSizeInit(m_NeighborhoodSizeInit);
75  estimator->SetNumberOfIterations(m_NumberOfIterations);
76  estimator->SetBetaInit(m_BetaInit);
77  estimator->SetBetaEnd(m_BetaEnd);
78  estimator->SetMaxWeight(m_MaxWeight);
79  estimator->Update();
80  m_SOMMap = estimator->GetOutput();
81 }
82 
83 template <class TInputValue, unsigned int MapDimension>
84 bool SOMModel<TInputValue, MapDimension>::CanReadFile(const std::string& filename)
85 {
86  try
87  {
88  this->Load(filename);
89  }
90  catch (...)
91  {
92  return false;
93  }
94  return true;
95 }
96 
97 template <class TInputValue, unsigned int MapDimension>
98 bool SOMModel<TInputValue, MapDimension>::CanWriteFile(const std::string& /*filename*/)
99 {
100  return true;
101 }
102 
103 template <class TInputValue, unsigned int MapDimension>
104 void SOMModel<TInputValue, MapDimension>::Save(const std::string& filename, const std::string& /*name*/)
105 {
106  itk::ImageRegionConstIterator<MapType> inputIterator(m_SOMMap, m_SOMMap->GetLargestPossibleRegion());
107  inputIterator.GoToBegin();
108  std::ofstream ofs(filename, std::ios::binary);
109  internal::BinaryWriteString(ofs, "som");
110  internal::BinaryWrite(ofs, static_cast<unsigned int>(MapDimension));
111  SizeType size = m_SOMMap->GetLargestPossibleRegion().GetSize();
112  for (size_t i = 0; i < MapDimension; i++)
113  {
114  internal::BinaryWrite(ofs, size[i]);
115  }
116 
117  internal::BinaryWrite(ofs, inputIterator.Get().GetNumberOfElements());
118  while (!inputIterator.IsAtEnd())
119  {
120  InputSampleType vect = inputIterator.Get();
121  for (size_t i = 0; i < vect.GetNumberOfElements(); i++)
122  {
123  internal::BinaryWrite(ofs, vect[i]);
124  }
125  ++inputIterator;
126  }
127  ofs.close();
128 
129  // output the map vectors in a txt file
130  if (this->m_WriteMap == true)
131  {
132  std::ofstream otxt(filename + ".txt");
133  inputIterator.GoToBegin();
134  while (!inputIterator.IsAtEnd())
135  {
136  InputSampleType vect = inputIterator.Get();
137  for (size_t i = 0; i < vect.GetNumberOfElements(); i++)
138  {
139  otxt << vect[i] << " ";
140  }
141  otxt << std::endl;
142  ++inputIterator;
143  }
144  otxt.close();
145  }
146 }
147 
148 template <class TInputValue, unsigned int MapDimension>
149 void SOMModel<TInputValue, MapDimension>::Load(const std::string& filename, const std::string& /*name*/)
150 {
151  std::ifstream ifs(filename, std::ios::binary);
152 
154  char s[] = " ";
155  for (int i = 0; i < 3; i++)
156  {
157  internal::BinaryRead(ifs, s[i]);
158  }
159  std::string modelType(s);
160 
163  unsigned int dimension;
164  internal::BinaryRead(ifs, dimension);
165  if (modelType != "som" || dimension != MapDimension)
166  {
167  itkExceptionMacro(<< "Error opening " << filename.c_str());
168  }
169 
170  SizeType size;
171  itk::Index<MapDimension> index;
172  for (unsigned int i = 0; i < MapDimension; i++)
173  {
174  internal::BinaryRead(ifs, size[i]);
175  index[i] = 0;
176  }
177  unsigned int numberOfElements;
178  internal::BinaryRead(ifs, numberOfElements);
179  m_SOMMap = MapType::New();
180  typename MapType::RegionType region;
181  region.SetSize(size);
182  m_SOMMap->SetNumberOfComponentsPerPixel(numberOfElements);
183  region.SetIndex(index);
184  m_SOMMap->SetRegions(region);
185  m_SOMMap->Allocate();
186 
187  itk::ImageRegionIterator<MapType> outputIterator(m_SOMMap, region);
188  outputIterator.GoToBegin();
189  std::string value;
190  while (!outputIterator.IsAtEnd())
191  {
192  InputSampleType vect(numberOfElements);
193  for (unsigned int i = 0; i < numberOfElements; i++)
194  {
195  // InputValue type is not the same during training anddimredvector.
196  float v;
197  internal::BinaryRead(ifs, v);
198  vect[i] = static_cast<double>(v);
199  }
200  outputIterator.Set(vect);
201  ++outputIterator;
202  }
203  ifs.close();
204  this->m_Dimension = MapType::ImageDimension;
205 }
206 
207 template <class TInputValue, unsigned int MapDimension>
210 {
211  TargetSampleType target;
212  target.SetSize(this->m_Dimension);
213 
214  auto winner = m_SOMMap->GetWinner(value);
215  for (unsigned int i = 0; i < this->m_Dimension; i++)
216  {
217  target[i] = winner.GetElement(i);
218  }
219  return target;
220 }
221 
222 } // namespace otb
223 #endif
otb::SOMModel::ConfidenceValueType
Superclass::ConfidenceValueType ConfidenceValueType
Definition: otbSOMModel.h:59
otb::SOM
This class is responsible for the learning of a self organizing map from a set of vector represented ...
Definition: otbSOM.h:60
otb::SOMModel::Load
void Load(const std::string &filename, const std::string &name="") override
Definition: otbSOMModel.hxx:149
otb::SOMModel::Train
void Train() override
Definition: otbSOMModel.hxx:68
otb::internal::BinaryWriteString
std::ostream & BinaryWriteString(std::ofstream &stream, const std::string &value)
Definition: otbSOMModel.hxx:44
otb
The "otb" namespace contains all Orfeo Toolbox (OTB) classes.
Definition: otbJoinContainer.h:32
otb::SOMMap::RegionType
Superclass::RegionType RegionType
Definition: otbSOMMap.h:81
otb::SOMModel::SizeType
MapType::SizeType SizeType
Definition: otbSOMModel.h:66
otb::SOMModel::Save
void Save(const std::string &filename, const std::string &name="") override
Definition: otbSOMModel.hxx:104
otb::SOMModel::CanReadFile
bool CanReadFile(const std::string &filename) override
Definition: otbSOMModel.hxx:84
otb::SOMModel::InputSampleType
Superclass::InputSampleType InputSampleType
Definition: otbSOMModel.h:51
otb::SOMModel::CanWriteFile
bool CanWriteFile(const std::string &filename) override
Definition: otbSOMModel.hxx:98
otb::SOMModel::ProbaSampleType
Superclass::ProbaSampleType ProbaSampleType
Definition: otbSOMModel.h:62
otbSOM.h
otb::SOMModel::TargetSampleType
Superclass::TargetSampleType TargetSampleType
Definition: otbSOMModel.h:55
otbSOMModel.h
otb::internal::BinaryWrite
std::ostream & BinaryWrite(std::ostream &stream, const T &value)
Definition: otbSOMModel.hxx:39
otb::internal::BinaryRead
std::istream & BinaryRead(std::istream &stream, T &value)
Definition: otbSOMModel.hxx:50
otb::SOMModel::DoPredict
virtual TargetSampleType DoPredict(const InputSampleType &input, ConfidenceValueType *quality=nullptr, ProbaSampleType *proba=nullptr) const override
Definition: otbSOMModel.hxx:209
otb::SOMModel::~SOMModel
~SOMModel() override
Definition: otbSOMModel.hxx:63
otb::SOMModel::SOMModel
SOMModel()
Definition: otbSOMModel.hxx:57