OTB  10.0.0
Orfeo Toolbox
otbMachineLearningModelFactory.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 
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 <mutex>
47 
48 namespace otb
49 {
50 template <class TInputValue, class TOutputValue>
53 {
54  RegisterBuiltInFactories();
55 
56  std::list<MachineLearningModelTypePointer> possibleMachineLearningModel;
57  std::list<LightObject::Pointer> allobjects = itk::ObjectFactoryBase::CreateAllInstance("otbMachineLearningModel");
58  for (std::list<LightObject::Pointer>::iterator i = allobjects.begin(); i != allobjects.end(); ++i)
59  {
61  if (io)
62  {
63  possibleMachineLearningModel.push_back(io);
64  }
65  else
66  {
67  std::cerr << "Error MachineLearningModel Factory did not return an MachineLearningModel: " << (*i)->GetNameOfClass() << std::endl;
68  }
69  }
70  for (typename std::list<MachineLearningModelTypePointer>::iterator k = possibleMachineLearningModel.begin(); k != possibleMachineLearningModel.end(); ++k)
71  {
72  if (mode == ReadMode)
73  {
74  if ((*k)->CanReadFile(path))
75  {
76  return *k;
77  }
78  }
79  else if (mode == WriteMode)
80  {
81  if ((*k)->CanWriteFile(path))
82  {
83  return *k;
84  }
85  }
86  }
87  return nullptr;
88 }
89 
90 template <class TInputValue, class TOutputValue>
92 {
93  std::lock_guard<std::mutex> lockHolder(mutex);
94 
95 #ifdef OTB_USE_LIBSVM
97 #endif
98 
99 #ifdef OTB_USE_SHARK
102 #endif
103 
104 #ifdef OTB_USE_OPENCV
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  std::lock_guard<std::mutex> 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 #ifdef OTB_USE_LIBSVM
138  if (libsvmFactory)
139  {
140  itk::ObjectFactoryBase::UnRegisterFactory(libsvmFactory);
141  continue;
142  }
143 #endif
144 
145 #ifdef OTB_USE_SHARK
148  if (sharkRFFactory)
149  {
150  itk::ObjectFactoryBase::UnRegisterFactory(sharkRFFactory);
151  continue;
152  }
153 
156  if (sharkKMeansFactory)
157  {
158  itk::ObjectFactoryBase::UnRegisterFactory(sharkKMeansFactory);
159  continue;
160  }
161 #endif
162 
163 #ifdef OTB_USE_OPENCV
164  // RandomForest
167  if (rfFactory)
168  {
169  itk::ObjectFactoryBase::UnRegisterFactory(rfFactory);
170  continue;
171  }
172  // SVM
174  if (svmFactory)
175  {
176  itk::ObjectFactoryBase::UnRegisterFactory(svmFactory);
177  continue;
178  }
179  // Boost
182  if (boostFactory)
183  {
184  itk::ObjectFactoryBase::UnRegisterFactory(boostFactory);
185  continue;
186  }
187  // ANN
190  if (annFactory)
191  {
192  itk::ObjectFactoryBase::UnRegisterFactory(annFactory);
193  continue;
194  }
195  // Bayes
198  if (bayesFactory)
199  {
200  itk::ObjectFactoryBase::UnRegisterFactory(bayesFactory);
201  continue;
202  }
203  // Decision Tree
206  if (dtFactory)
207  {
208  itk::ObjectFactoryBase::UnRegisterFactory(dtFactory);
209  continue;
210  }
211  // KNN
214  if (knnFactory)
215  {
216  itk::ObjectFactoryBase::UnRegisterFactory(knnFactory);
217  continue;
218  }
219 #endif
220  }
221 }
222 
223 } // end namespace otb
224 
225 #endif
Create an instance of SVMMachineLearningModel through the object factory.
Creation d'un instance d'un object SVMMachineLearningModel utilisant les object factory.
Create an instance of KNearestNeighborsMachineLearningModel through the object factory.
Creation of an instance of an object SVMMachineLearningModel using the objects factory.
static void RegisterFactory(itk::ObjectFactoryBase *factory)
static MachineLearningModelTypePointer CreateMachineLearningModel(const std::string &path, FileModeType mode)
MachineLearningModel is the base class for all classifier objects (SVM, KNN, Random Forests,...
itk::SmartPointer< Self > Pointer
virtual const char * GetNameOfClass() const
Create an instance of SVMMachineLearningModel through the object factory.
Creation of an instance of object SVMMachineLearningModel using the objects factory.
Creation of an instance of a RandomForestsMachineLearningModel object using the object factory.
Creation d'un instance d'un object SVMMachineLearningModel utilisant les object factory.
Creation of an instance of a SharkKMeansMachineLearningModel object using the object factory.
Creation of an instance of a SharkRandomForestsMachineLearningModel object using the object factory.
The "otb" namespace contains all Orfeo Toolbox (OTB) classes.