Skip to content
Snippets Groups Projects
Commit 5db89e84 authored by Ludovic Hussonnois's avatar Ludovic Hussonnois
Browse files

ENH: Remove Supervised dependencies on Unsupervised Module.

All Supervised and Unsupervised Classifier should be declared and stay
in Supervised/MachinLearningModelFactory since multiple application
depend on it.
parent 7b2217e9
No related branches found
No related tags found
No related merge requests found
......@@ -30,7 +30,6 @@ namespace otb
* part of a template class (ld error).
*
*
* \ingroup OTBSupervised
*/
class OTBSupervised_EXPORT MachineLearningModelFactoryBase : public itk::Object
{
......
......@@ -6,6 +6,9 @@ otb_module(OTBLearningBase
OTBCommon
OTBITK
OPTIONAL_DEPENDS
OTBShark
TEST_DEPENDS
OTBTestKernel
OTBImageIO
......
......@@ -37,6 +37,7 @@
#ifdef OTB_USE_SHARK
#include "otbSharkRandomForestsMachineLearningModelFactory.h"
#include "otbSharkKMeansMachineLearningModelFactory.h"
#endif
#include "itkMutexLockHolder.h"
......@@ -104,6 +105,7 @@ MachineLearningModelFactory<TInputValue,TOutputValue>
#ifdef OTB_USE_SHARK
RegisterFactory(SharkRandomForestsMachineLearningModelFactory<TInputValue,TOutputValue>::New());
RegisterFactory(SharkKMeansMachineLearningModelFactory<TInputValue,TOutputValue>::New());
#endif
#ifdef OTB_USE_OPENCV
......@@ -160,6 +162,14 @@ MachineLearningModelFactory<TInputValue,TOutputValue>
itk::ObjectFactoryBase::UnRegisterFactory(sharkRFFactory);
continue;
}
SharkKMeansMachineLearningModelFactory<TInputValue,TOutputValue> *sharkKMeansFactory =
dynamic_cast<SharkKMeansMachineLearningModelFactory<TInputValue,TOutputValue> *>(*itFac);
if (sharkKMeansFactory)
{
itk::ObjectFactoryBase::UnRegisterFactory(sharkKMeansFactory);
continue;
}
#endif
#ifdef OTB_USE_OPENCV
......
......@@ -10,6 +10,7 @@ ENABLE_SHARED
OTBITK
OTBImageBase
OTBLearningBase
OTBUnsupervised
OPTIONAL_DEPENDS
OTBOpenCV
......@@ -22,6 +23,7 @@ ENABLE_SHARED
OTBImageBase
OTBLearningBase
OTBBoost
OTBUnsupervised
DESCRIPTION
"${DOCUMENTATION}"
......
/*=========================================================================
Program: ORFEO Toolbox
Language: C++
Date: $Date$
Version: $Revision$
Copyright (c) Centre National d'Etudes Spatiales. All rights reserved.
See OTBCopyright.txt for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notices for more information.
=========================================================================*/
#ifndef otbMachineLearningClusteringModelFactory_h
#define otbMachineLearningClusteringModelFactory_h
#include "otbMachineLearningModel.h"
#include "otbMachineLearningModelFactoryBase.h"
namespace otb
{
/** \class MachineLearningModelFactory
* \brief Creation of object instance using object factory.
*
* \ingroup OTBUnsupervised
*/
template <class TInputValue, class TOutputValue>
class MachineLearningModelFactory : public MachineLearningModelFactoryBase
{
public:
/** Standard class typedefs. */
typedef MachineLearningModelFactory Self;
typedef itk::Object Superclass;
typedef itk::SmartPointer<Self> Pointer;
typedef itk::SmartPointer<const Self> ConstPointer;
/** Class Methods used to interface with the registered factories */
/** Run-time type information (and related methods). */
itkTypeMacro(MachineLearningModelFactory, itk::Object);
/** Convenient typedefs. */
typedef otb::MachineLearningModel<TInputValue,TOutputValue> MachineLearningModelType;
typedef typename MachineLearningModelType::Pointer MachineLearningModelTypePointer;
/** Mode in which the files is intended to be used */
typedef enum { ReadMode, WriteMode } FileModeType;
/** Create the appropriate MachineLearningModel depending on the particulars of the file. */
static MachineLearningModelTypePointer CreateMachineLearningModel(const std::string& path, FileModeType mode);
static void CleanFactories();
protected:
MachineLearningModelFactory();
~MachineLearningModelFactory() ITK_OVERRIDE;
private:
MachineLearningModelFactory(const Self &); //purposely not implemented
void operator =(const Self&); //purposely not implemented
/** Register Built-in factories */
static void RegisterBuiltInFactories();
/** Register a single factory, ensuring it has not been registered
* twice */
static void RegisterFactory(itk::ObjectFactoryBase * factory);
};
} // end namespace otb
#ifndef OTB_MANUAL_INSTANTIATION
#include "otbMachineLearningClusteringModelFactory.txx"
#endif
#endif //otbMachineLearningClusteringModelFactory_h
/*=========================================================================
Program: ORFEO Toolbox
Language: C++
Date: $Date$
Version: $Revision$
Copyright (c) Centre National d'Etudes Spatiales. All rights reserved.
See OTBCopyright.txt for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notices for more information.
=========================================================================*/
#ifndef otbMachineLearningModelFactory_txx
#define otbMachineLearningModelFactory_txx
#include "otbMachineLearningClusteringModelFactory.h"
#include "otbConfigure.h"
#ifdef OTB_USE_SHARK
#include "otbSharkKMeansMachineLearningModelFactory.h"
#endif
#include "itkMutexLockHolder.h"
namespace otb
{
template <class TInputValue, class TOutputValue>
typename MachineLearningModel<TInputValue,TOutputValue>::Pointer
MachineLearningModelFactory<TInputValue,TOutputValue>
::CreateMachineLearningModel(const std::string& path, FileModeType mode)
{
RegisterBuiltInFactories();
std::list<MachineLearningModelTypePointer> possibleMachineLearningModel;
std::list<LightObject::Pointer> allobjects =
itk::ObjectFactoryBase::CreateAllInstance("otbMachineLearningModel");
for(std::list<LightObject::Pointer>::iterator i = allobjects.begin();
i != allobjects.end(); ++i)
{
MachineLearningModel<TInputValue,TOutputValue> * io = dynamic_cast<MachineLearningModel<TInputValue,TOutputValue>*>(i->GetPointer());
if(io)
{
possibleMachineLearningModel.push_back(io);
}
else
{
std::cerr << "Error MachineLearningModel Factory did not return an MachineLearningModel: "
<< (*i)->GetNameOfClass()
<< std::endl;
}
}
for(typename std::list<MachineLearningModelTypePointer>::iterator k = possibleMachineLearningModel.begin();
k != possibleMachineLearningModel.end(); ++k)
{
if( mode == ReadMode )
{
if((*k)->CanReadFile(path))
{
return *k;
}
}
else if( mode == WriteMode )
{
if((*k)->CanWriteFile(path))
{
return *k;
}
}
}
return ITK_NULLPTR;
}
template <class TInputValue, class TOutputValue>
void
MachineLearningModelFactory<TInputValue,TOutputValue>
::RegisterBuiltInFactories()
{
itk::MutexLockHolder<itk::SimpleMutexLock> lockHolder(mutex);
#ifdef OTB_USE_SHARK
RegisterFactory(SharkKMeansMachineLearningModelFactory<TInputValue,TOutputValue>::New());
#endif
}
template <class TInputValue, class TOutputValue>
void
MachineLearningModelFactory<TInputValue,TOutputValue>
::RegisterFactory(itk::ObjectFactoryBase * factory)
{
// Unregister any previously registered factory of the same class
// Might be more intensive but static bool is not an option due to
// ld error.
itk::ObjectFactoryBase::UnRegisterFactory(factory);
itk::ObjectFactoryBase::RegisterFactory(factory);
}
template <class TInputValue, class TOutputValue>
void
MachineLearningModelFactory<TInputValue,TOutputValue>
::CleanFactories()
{
itk::MutexLockHolder<itk::SimpleMutexLock> lockHolder(mutex);
std::list<itk::ObjectFactoryBase*> factories = itk::ObjectFactoryBase::GetRegisteredFactories();
std::list<itk::ObjectFactoryBase*>::iterator itFac;
for (itFac = factories.begin(); itFac != factories.end() ; ++itFac)
{
#ifdef OTB_USE_SHARK
SharkKMeansMachineLearningModelFactory<TInputValue,TOutputValue> *sharkKMeansFactory =
dynamic_cast<SharkKMeansMachineLearningModelFactory<TInputValue,TOutputValue> *>(*itFac);
if (sharkKMeansFactory)
{
itk::ObjectFactoryBase::UnRegisterFactory(sharkKMeansFactory);
continue;
}
#endif
}
}
} // end namespace otb
#endif
......@@ -7,7 +7,6 @@ otb_module(OTBUnsupervised
OTBITK
OTBImageBase
OTBLearningBase
OTBSupervised
OPTIONAL_DEPENDS
OTBShark
......@@ -17,7 +16,6 @@ otb_module(OTBUnsupervised
OTBImageIO
OTBImageBase
OTBLearningBase
OTBSupervised
DESCRIPTION
"${DOCUMENTATION}"
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment