OTB  9.0.0
Orfeo Toolbox
otbDimensionalityReductionModelFactory.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 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 "itkMutexLockHolder.h"
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>
101 {
102  itk::MutexLockHolder<itk::SimpleMutexLock> lockHolder(mutex);
103 
108 
109 #ifdef OTB_USE_SHARK
111  RegisterFactory(LogAutoencoderModelFactory<TInputValue, TOutputValue>::New());
112 #endif
113 }
114 
115 template <class TInputValue, class TOutputValue>
117 {
118  // Unregister any previously registered factory of the same class
119  // Might be more intensive but static bool is not an option due to
120  // ld error.
121  itk::ObjectFactoryBase::UnRegisterFactory(factory);
122  itk::ObjectFactoryBase::RegisterFactory(factory);
123 }
124 
125 template <class TInputValue, class TOutputValue>
127 {
128  itk::MutexLockHolder<itk::SimpleMutexLock> lockHolder(mutex);
129 
130  std::list<itk::ObjectFactoryBase*> factories = itk::ObjectFactoryBase::GetRegisteredFactories();
131  std::list<itk::ObjectFactoryBase*>::iterator itFac;
132 
133  for (itFac = factories.begin(); itFac != factories.end(); ++itFac)
134  {
135  // SOM 5D
137  if (som5dFactory)
138  {
139  itk::ObjectFactoryBase::UnRegisterFactory(som5dFactory);
140  continue;
141  }
142  // SOM 4D
144  if (som4dFactory)
145  {
146  itk::ObjectFactoryBase::UnRegisterFactory(som4dFactory);
147  continue;
148  }
149  // SOM 3D
151  if (som3dFactory)
152  {
153  itk::ObjectFactoryBase::UnRegisterFactory(som3dFactory);
154  continue;
155  }
156  // SOM 2D
158  if (som2dFactory)
159  {
160  itk::ObjectFactoryBase::UnRegisterFactory(som2dFactory);
161  continue;
162  }
163 #ifdef OTB_USE_SHARK
164  // Autoencoder
165  LogAutoencoderModelFactory<TInputValue, TOutputValue>* aeFactory = dynamic_cast<LogAutoencoderModelFactory<TInputValue, TOutputValue>*>(*itFac);
166  if (aeFactory)
167  {
168  itk::ObjectFactoryBase::UnRegisterFactory(aeFactory);
169  continue;
170  }
171  // PCA
173  if (pcaFactory)
174  {
175  itk::ObjectFactoryBase::UnRegisterFactory(pcaFactory);
176  continue;
177  }
178 #endif
179  }
180 }
181 
182 } // end namespace otb
183 
184 #endif
otb::DimensionalityReductionModelFactory::DimensionalityReductionModelTypePointer
DimensionalityReductionModelType::Pointer DimensionalityReductionModelTypePointer
Definition: otbDimensionalityReductionModelFactory.h:50
otb::DimensionalityReductionModelFactory::FileModeType
FileModeType
Definition: otbDimensionalityReductionModelFactory.h:53
otbDimensionalityReductionModelFactory.h
otb::PCAModelFactory
Definition: otbPCAModelFactory.h:36
otb
The "otb" namespace contains all Orfeo Toolbox (OTB) classes.
Definition: otbJoinContainer.h:32
otb::DimensionalityReductionModelFactory::CreateDimensionalityReductionModel
static DimensionalityReductionModelTypePointer CreateDimensionalityReductionModel(const std::string &path, FileModeType mode)
Definition: otbDimensionalityReductionModelFactory.hxx:58
otb::DimensionalityReductionModelFactory::RegisterBuiltInFactories
static void RegisterBuiltInFactories()
Definition: otbDimensionalityReductionModelFactory.hxx:100
otb::DimensionalityReductionModelFactory::CleanFactories
static void CleanFactories()
Definition: otbDimensionalityReductionModelFactory.hxx:126
otb::SOMModelFactory
Definition: otbSOMModelFactory.h:36
otbSOMModelFactory.h
otb::MachineLearningModel
MachineLearningModel is the base class for all classifier objects (SVM, KNN, Random Forests,...
Definition: otbMachineLearningModel.h:70
otbAutoencoderModelFactory.h
otbPCAModelFactory.h
otb::DimensionalityReductionModelFactory::RegisterFactory
static void RegisterFactory(itk::ObjectFactoryBase *factory)
Definition: otbDimensionalityReductionModelFactory.hxx:116