OTB  10.0.0
Orfeo Toolbox
otbDimensionalityReductionModelFactory.hxx
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2005-2024 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 otbDimensionalityReductionModelFactory_hxx
21 #define otbDimensionalityReductionModelFactory_hxx
22 
24 #include "otbConfigure.h"
25 
26 #include "otbSOMModelFactory.h"
27 
28 #ifdef OTB_USE_SHARK
30 #include "otbPCAModelFactory.h"
31 #endif
32 
33 #include <mutex>
34 
35 namespace otb
36 {
37 
38 #ifdef OTB_USE_SHARK
39 template <class TInputValue, class TTargetValue>
40 using LogAutoencoderModelFactory = AutoencoderModelFactory<TInputValue, TTargetValue, shark::LogisticNeuron>;
41 #endif
42 
43 template <class TInputValue, class TTargetValue>
45 
46 template <class TInputValue, class TTargetValue>
48 
49 template <class TInputValue, class TTargetValue>
51 
52 template <class TInputValue, class TTargetValue>
54 
55 
56 template <class TInputValue, class TOutputValue>
59 {
60  RegisterBuiltInFactories();
61 
62  std::list<DimensionalityReductionModelTypePointer> possibleDimensionalityReductionModel;
63  std::list<LightObject::Pointer> allobjects = itk::ObjectFactoryBase::CreateAllInstance("DimensionalityReductionModel");
64 
65  for (std::list<LightObject::Pointer>::iterator i = allobjects.begin(); i != allobjects.end(); ++i)
66  {
67  DimensionalityReductionModelType* io = dynamic_cast<DimensionalityReductionModelType*>(i->GetPointer());
68  if (io)
69  {
70  possibleDimensionalityReductionModel.push_back(io);
71  }
72  else
73  {
74  std::cerr << "Error DimensionalityReductionModel Factory did not return an DimensionalityReductionModel: " << (*i)->GetNameOfClass() << std::endl;
75  }
76  }
77 
78  for (typename std::list<DimensionalityReductionModelTypePointer>::iterator k = possibleDimensionalityReductionModel.begin();
79  k != possibleDimensionalityReductionModel.end(); ++k)
80  {
81  if (mode == ReadMode)
82  {
83  if ((*k)->CanReadFile(path))
84  {
85  return *k;
86  }
87  }
88  else if (mode == WriteMode)
89  {
90  if ((*k)->CanWriteFile(path))
91  {
92  return *k;
93  }
94  }
95  }
96  return nullptr;
97 }
98 
99 template <class TInputValue, class TOutputValue>
100 void
103 {
104  std::lock_guard<std::mutex> lockHolder(mutex);
105 
110 
111 #ifdef OTB_USE_SHARK
113  RegisterFactory(LogAutoencoderModelFactory<TInputValue, TOutputValue>::New());
114 #endif
115 }
116 
117 template <class TInputValue, class TOutputValue>
119 {
120  // Unregister any previously registered factory of the same class
121  // Might be more intensive but static bool is not an option due to
122  // ld error.
123  itk::ObjectFactoryBase::UnRegisterFactory(factory);
124  itk::ObjectFactoryBase::RegisterFactory(factory);
125 }
126 
127 template <class TInputValue, class TOutputValue>
129 {
130  std::lock_guard<std::mutex> lockHolder(mutex);
131 
132  std::list<itk::ObjectFactoryBase*> factories = itk::ObjectFactoryBase::GetRegisteredFactories();
133  std::list<itk::ObjectFactoryBase*>::iterator itFac;
134 
135  for (itFac = factories.begin(); itFac != factories.end(); ++itFac)
136  {
137  // SOM 5D
139  if (som5dFactory)
140  {
141  itk::ObjectFactoryBase::UnRegisterFactory(som5dFactory);
142  continue;
143  }
144  // SOM 4D
146  if (som4dFactory)
147  {
148  itk::ObjectFactoryBase::UnRegisterFactory(som4dFactory);
149  continue;
150  }
151  // SOM 3D
153  if (som3dFactory)
154  {
155  itk::ObjectFactoryBase::UnRegisterFactory(som3dFactory);
156  continue;
157  }
158  // SOM 2D
160  if (som2dFactory)
161  {
162  itk::ObjectFactoryBase::UnRegisterFactory(som2dFactory);
163  continue;
164  }
165 #ifdef OTB_USE_SHARK
166  // Autoencoder
167  LogAutoencoderModelFactory<TInputValue, TOutputValue>* aeFactory = dynamic_cast<LogAutoencoderModelFactory<TInputValue, TOutputValue>*>(*itFac);
168  if (aeFactory)
169  {
170  itk::ObjectFactoryBase::UnRegisterFactory(aeFactory);
171  continue;
172  }
173  // PCA
175  if (pcaFactory)
176  {
177  itk::ObjectFactoryBase::UnRegisterFactory(pcaFactory);
178  continue;
179  }
180 #endif
181  }
182 }
183 
184 } // end namespace otb
185 
186 #endif
static DimensionalityReductionModelTypePointer CreateDimensionalityReductionModel(const std::string &path, FileModeType mode)
DimensionalityReductionModelType::Pointer DimensionalityReductionModelTypePointer
static void RegisterFactory(itk::ObjectFactoryBase *factory)
MachineLearningModel is the base class for all classifier objects (SVM, KNN, Random Forests,...
virtual const char * GetNameOfClass() const
The "otb" namespace contains all Orfeo Toolbox (OTB) classes.