OTB  9.0.0
Orfeo Toolbox
otbMachineLearningModelFactory.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 
21 #ifndef otbMachineLearningModelFactory_hxx
22 #define otbMachineLearningModelFactory_hxx
23 
25 #include "otbConfigure.h"
26 
27 #ifdef OTB_USE_OPENCV
28 #include "otb_opencv_api.h"
36 #endif
37 #ifdef OTB_USE_LIBSVM
39 #endif
40 
41 #ifdef OTB_USE_SHARK
44 #endif
45 
46 #include "itkMutexLockHolder.h"
47 
48 
49 namespace otb
50 {
51 template <class TInputValue, class TOutputValue>
54 {
55  RegisterBuiltInFactories();
56 
57  std::list<MachineLearningModelTypePointer> possibleMachineLearningModel;
58  std::list<LightObject::Pointer> allobjects = itk::ObjectFactoryBase::CreateAllInstance("otbMachineLearningModel");
59  for (std::list<LightObject::Pointer>::iterator i = allobjects.begin(); i != allobjects.end(); ++i)
60  {
62  if (io)
63  {
64  possibleMachineLearningModel.push_back(io);
65  }
66  else
67  {
68  std::cerr << "Error MachineLearningModel Factory did not return an MachineLearningModel: " << (*i)->GetNameOfClass() << std::endl;
69  }
70  }
71  for (typename std::list<MachineLearningModelTypePointer>::iterator k = possibleMachineLearningModel.begin(); k != possibleMachineLearningModel.end(); ++k)
72  {
73  if (mode == ReadMode)
74  {
75  if ((*k)->CanReadFile(path))
76  {
77  return *k;
78  }
79  }
80  else if (mode == WriteMode)
81  {
82  if ((*k)->CanWriteFile(path))
83  {
84  return *k;
85  }
86  }
87  }
88  return nullptr;
89 }
90 
91 template <class TInputValue, class TOutputValue>
93 {
94  itk::MutexLockHolder<itk::SimpleMutexLock> lockHolder(mutex);
95 
96 #ifdef OTB_USE_LIBSVM
98 #endif
99 
100 #ifdef OTB_USE_SHARK
103 #endif
104 
105 #ifdef OTB_USE_OPENCV
113 #endif
114 }
115 
116 template <class TInputValue, class TOutputValue>
118 {
119  // Unregister any previously registered factory of the same class
120  // Might be more intensive but static bool is not an option due to
121  // ld error.
122  itk::ObjectFactoryBase::UnRegisterFactory(factory);
123  itk::ObjectFactoryBase::RegisterFactory(factory);
124 }
125 
126 template <class TInputValue, class TOutputValue>
128 {
129  itk::MutexLockHolder<itk::SimpleMutexLock> lockHolder(mutex);
130 
131  std::list<itk::ObjectFactoryBase*> factories = itk::ObjectFactoryBase::GetRegisteredFactories();
132  std::list<itk::ObjectFactoryBase*>::iterator itFac;
133 
134  for (itFac = factories.begin(); itFac != factories.end(); ++itFac)
135  {
136 #ifdef OTB_USE_LIBSVM
139  if (libsvmFactory)
140  {
141  itk::ObjectFactoryBase::UnRegisterFactory(libsvmFactory);
142  continue;
143  }
144 #endif
145 
146 #ifdef OTB_USE_SHARK
149  if (sharkRFFactory)
150  {
151  itk::ObjectFactoryBase::UnRegisterFactory(sharkRFFactory);
152  continue;
153  }
154 
157  if (sharkKMeansFactory)
158  {
159  itk::ObjectFactoryBase::UnRegisterFactory(sharkKMeansFactory);
160  continue;
161  }
162 #endif
163 
164 #ifdef OTB_USE_OPENCV
165  // RandomForest
168  if (rfFactory)
169  {
170  itk::ObjectFactoryBase::UnRegisterFactory(rfFactory);
171  continue;
172  }
173  // SVM
175  if (svmFactory)
176  {
177  itk::ObjectFactoryBase::UnRegisterFactory(svmFactory);
178  continue;
179  }
180  // Boost
183  if (boostFactory)
184  {
185  itk::ObjectFactoryBase::UnRegisterFactory(boostFactory);
186  continue;
187  }
188  // ANN
191  if (annFactory)
192  {
193  itk::ObjectFactoryBase::UnRegisterFactory(annFactory);
194  continue;
195  }
196  // Bayes
199  if (bayesFactory)
200  {
201  itk::ObjectFactoryBase::UnRegisterFactory(bayesFactory);
202  continue;
203  }
204  // Decision Tree
207  if (dtFactory)
208  {
209  itk::ObjectFactoryBase::UnRegisterFactory(dtFactory);
210  continue;
211  }
212  // KNN
215  if (knnFactory)
216  {
217  itk::ObjectFactoryBase::UnRegisterFactory(knnFactory);
218  continue;
219  }
220 #endif
221  }
222 }
223 
224 } // end namespace otb
225 
226 #endif
otb::MachineLearningModelFactory::CleanFactories
static void CleanFactories()
Definition: otbMachineLearningModelFactory.hxx:127
otbKNearestNeighborsMachineLearningModelFactory.h
otbNeuralNetworkMachineLearningModelFactory.h
otb::SVMMachineLearningModelFactory
Creation d'un instance d'un object SVMMachineLearningModel utilisant les object factory.
Definition: otbSVMMachineLearningModelFactory.h:37
otb::SharkRandomForestsMachineLearningModelFactory
Creation of an instance of a SharkRandomForestsMachineLearningModel object using the object factory.
Definition: otbSharkRandomForestsMachineLearningModelFactory.h:35
otb
The "otb" namespace contains all Orfeo Toolbox (OTB) classes.
Definition: otbJoinContainer.h:32
otb::NeuralNetworkMachineLearningModelFactory
Create an instance of SVMMachineLearningModel through the object factory.
Definition: otbNeuralNetworkMachineLearningModelFactory.h:37
otb::MachineLearningModelFactory::RegisterBuiltInFactories
static void RegisterBuiltInFactories()
Definition: otbMachineLearningModelFactory.hxx:92
otb::MachineLearningModelFactory::CreateMachineLearningModel
static MachineLearningModelTypePointer CreateMachineLearningModel(const std::string &path, FileModeType mode)
Definition: otbMachineLearningModelFactory.hxx:53
otb::BoostMachineLearningModelFactory
Create an instance of SVMMachineLearningModel through the object factory.
Definition: otbBoostMachineLearningModelFactory.h:37
otbNormalBayesMachineLearningModelFactory.h
otbLibSVMMachineLearningModelFactory.h
otbSharkKMeansMachineLearningModelFactory.h
otbRandomForestsMachineLearningModelFactory.h
otbDecisionTreeMachineLearningModelFactory.h
otbSVMMachineLearningModelFactory.h
otb::DecisionTreeMachineLearningModelFactory
Creation d'un instance d'un object SVMMachineLearningModel utilisant les object factory.
Definition: otbDecisionTreeMachineLearningModelFactory.h:37
otb::NormalBayesMachineLearningModelFactory
Creation of an instance of object SVMMachineLearningModel using the objects factory.
Definition: otbNormalBayesMachineLearningModelFactory.h:37
otb::RandomForestsMachineLearningModelFactory
Creation of an instance of a RandomForestsMachineLearningModel object using the object factory.
Definition: otbRandomForestsMachineLearningModelFactory.h:37
otbMachineLearningModelFactory.h
otb::MachineLearningModel::Pointer
itk::SmartPointer< Self > Pointer
Definition: otbMachineLearningModel.h:77
otb::LibSVMMachineLearningModelFactory
Creation of an instance of an object SVMMachineLearningModel using the objects factory.
Definition: otbLibSVMMachineLearningModelFactory.h:35
otb::MachineLearningModel
MachineLearningModel is the base class for all classifier objects (SVM, KNN, Random Forests,...
Definition: otbMachineLearningModel.h:70
otbSharkRandomForestsMachineLearningModelFactory.h
otb::MachineLearningModelFactory::RegisterFactory
static void RegisterFactory(itk::ObjectFactoryBase *factory)
Definition: otbMachineLearningModelFactory.hxx:117
otbBoostMachineLearningModelFactory.h
otb::SharkKMeansMachineLearningModelFactory
Creation of an instance of a SharkKMeansMachineLearningModel object using the object factory.
Definition: otbSharkKMeansMachineLearningModelFactory.h:34
otb::MachineLearningModelFactory::FileModeType
FileModeType
Definition: otbMachineLearningModelFactory.h:54
otb::KNearestNeighborsMachineLearningModelFactory
Create an instance of KNearestNeighborsMachineLearningModel through the object factory.
Definition: otbKNearestNeighborsMachineLearningModelFactory.h:37