OTB  6.7.0
Orfeo Toolbox
otbMachineLearningModelFactory.hxx
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2005-2019 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>
52 typename MachineLearningModel<TInputValue,TOutputValue>::Pointer
54 ::CreateMachineLearningModel(const std::string& path, FileModeType mode)
55 {
56  RegisterBuiltInFactories();
57 
58  std::list<MachineLearningModelTypePointer> possibleMachineLearningModel;
59  std::list<LightObject::Pointer> allobjects =
60  itk::ObjectFactoryBase::CreateAllInstance("otbMachineLearningModel");
61  for(std::list<LightObject::Pointer>::iterator i = allobjects.begin();
62  i != allobjects.end(); ++i)
63  {
65  if(io)
66  {
67  possibleMachineLearningModel.push_back(io);
68  }
69  else
70  {
71  std::cerr << "Error MachineLearningModel Factory did not return an MachineLearningModel: "
72  << (*i)->GetNameOfClass()
73  << std::endl;
74  }
75  }
76 for(typename std::list<MachineLearningModelTypePointer>::iterator k = possibleMachineLearningModel.begin();
77  k != possibleMachineLearningModel.end(); ++k)
78  {
79  if( mode == ReadMode )
80  {
81  if((*k)->CanReadFile(path))
82  {
83  return *k;
84  }
85  }
86  else if( mode == WriteMode )
87  {
88  if((*k)->CanWriteFile(path))
89  {
90  return *k;
91  }
92 
93  }
94  }
95  return nullptr;
96 }
97 
98 template <class TInputValue, class TOutputValue>
99 void
102 {
104 
105 #ifdef OTB_USE_LIBSVM
107 #endif
108 
109 #ifdef OTB_USE_SHARK
112 #endif
113 
114 #ifdef OTB_USE_OPENCV
122 #endif
123 }
124 
125 template <class TInputValue, class TOutputValue>
126 void
129 {
130  // Unregister any previously registered factory of the same class
131  // Might be more intensive but static bool is not an option due to
132  // ld error.
135 }
136 
137 template <class TInputValue, class TOutputValue>
138 void
141 {
143 
144  std::list<itk::ObjectFactoryBase*> factories = itk::ObjectFactoryBase::GetRegisteredFactories();
145  std::list<itk::ObjectFactoryBase*>::iterator itFac;
146 
147  for (itFac = factories.begin(); itFac != factories.end() ; ++itFac)
148  {
149 #ifdef OTB_USE_LIBSVM
152  if (libsvmFactory)
153  {
155  continue;
156  }
157 #endif
158 
159 #ifdef OTB_USE_SHARK
162  if (sharkRFFactory)
163  {
165  continue;
166  }
167 
170  if (sharkKMeansFactory)
171  {
172  itk::ObjectFactoryBase::UnRegisterFactory(sharkKMeansFactory);
173  continue;
174  }
175 #endif
176 
177 #ifdef OTB_USE_OPENCV
178  // RandomForest
181  if (rfFactory)
182  {
184  continue;
185  }
186  // SVM
189  if (svmFactory)
190  {
192  continue;
193  }
194  // Boost
197  if (boostFactory)
198  {
200  continue;
201  }
202  // ANN
205  if (annFactory)
206  {
208  continue;
209  }
210  // Bayes
213  if (bayesFactory)
214  {
216  continue;
217  }
218  // Decision Tree
221  if (dtFactory)
222  {
224  continue;
225  }
226  // KNN
229  if (knnFactory)
230  {
232  continue;
233  }
234 #endif
235  }
236 
237 }
238 
239 } // end namespace otb
240 
241 #endif
static std::list< LightObject::Pointer > CreateAllInstance(const char *itkclassname)
Creation of an instance of a SharkRandomForestsMachineLearningModel object using the object factory...
Creation d'un instance d'un objet SVMMachineLearningModel utilisant les object factory.
MachineLearningModel is the base class for all classifier objects (SVM, KNN, Random Forests...
Creation d'un instance d'un objet SVMMachineLearningModel utilisant les object factory.
static void UnRegisterFactory(ObjectFactoryBase *)
Creation d'un instance d'un objet SVMMachineLearningModel utilisant les object factory.
Creation d'un instance d'un objet KNearestNeighborsMachineLearningModel utilisant les object factory...
Creation of an instance of a SharkKMeansMachineLearningModel object using the object factory...
Creation d'un instance d'un objet SVMMachineLearningModel utilisant les object factory.
static void RegisterFactory(itk::ObjectFactoryBase *factory)
Creation of an instance of a RandomForestsMachineLearningModel object using the object factory...
static MachineLearningModelTypePointer CreateMachineLearningModel(const std::string &path, FileModeType mode)
Creation d'un instance d'un objet SVMMachineLearningModel utilisant les object factory.
Creation d'un instance d'un objet SVMMachineLearningModel utilisant les object factory.
static bool RegisterFactory(ObjectFactoryBase *, InsertionPositionType where=INSERT_AT_BACK, vcl_size_t position=0)
static std::list< ObjectFactoryBase * > GetRegisteredFactories()