Skip to content
Snippets Groups Projects
Commit ae2ac84c authored by Cédric Traizet's avatar Cédric Traizet
Browse files

creation of drmodel and drmodelfactory

parent c9051b54
No related branches found
No related tags found
No related merge requests found
#ifndef AutoencoderModel_h
#define AutoencoderModel_h
#include "otbMachineLearningModel.h"
#include "DimensionalityReductionModel.h"
namespace otb
{
template <class TInputValue, class AutoencoderType>
class ITK_EXPORT AutoencoderModel: public MachineLearningModel<TInputValue,TInputValue>
class ITK_EXPORT AutoencoderModel: public DimensionalityReductionModel<TInputValue,TInputValue>
{
public:
typedef AutoencoderModel Self;
typedef MachineLearningModel<TInputValue,TInputValue> Superclass;
typedef DimensionalityReductionModel<TInputValue,TInputValue> Superclass;
typedef itk::SmartPointer<Self> Pointer;
typedef itk::SmartPointer<const Self> ConstPointer;
......@@ -28,7 +28,7 @@ public:
typedef typename Superclass::ConfidenceListSampleType ConfidenceListSampleType;
itkNewMacro(Self);
itkTypeMacro(AutoencoderModel, MachineLearningModel);
itkTypeMacro(AutoencoderModel, DimensionalityReductionModel);
itkGetMacro(NumberOfHiddenNeurons,unsigned int);
......
......@@ -31,12 +31,12 @@ template <class TInputValue, class TOutputValue, class AutoencoderType>
AutoencoderModelFactoryBase<TInputValue,TOutputValue, AutoencoderType>::AutoencoderModelFactoryBase()
{
std::string classOverride = std::string("otbMachineLearningModel");
std::string classOverride = std::string("DimensionalityReductionModel");
std::string subclass = std::string("AutoencoderModel");
this->RegisterOverride(classOverride.c_str(),
subclass.c_str(),
"Shark RF ML Model",
"Shark AE ML Model",
1,
// itk::CreateObjectFunction<AutoencoderModel<TInputValue,TOutputValue> >::New());
itk::CreateObjectFunction<AutoencoderModel<TInputValue,AutoencoderType > >::New());
......
/*=========================================================================
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 DimensionalityReductionModel_h
#define DimensionalityReductionModel_h
#include "itkObject.h"
#include "itkVariableLengthVector.h"
#include "itkListSample.h"
namespace otb
{
/** \class MachineLearningModel
* \brief MachineLearningModel is the base class for all classifier objects (SVM, KNN,
* Random Forests, Artificial Neural Network, ...) implemented in the supervised classification framework of the OTB.
*
* MachineLearningModel is an abstract object that specifies behavior and
* interface of supervised classifiers (SVM, KNN, Random Forests, Artificial
* Neural Network, ...) in the generic supervised classification framework of the OTB.
* The main generic virtual methods specifically implemented in each classifier
* derived from the MachineLearningModel class are two learning-related methods:
* Train() and Save(), and three classification-related methods: Load(),
* DoPredict() and optionnaly DoPredictBatch().
*
* Thus, each classifier derived from the MachineLearningModel class
* computes its corresponding model with Train() and exports it with
* the help of the Save() method.
*
* It is also possible to classify any input sample composed of several
* features (or any number of bands in the case of a pixel extracted
* from a multi-band image) with the help of the Predict() method which
* needs a previous loading of the classification model with the Load() method.
*
* \sa MachineLearningModelFactory
* \sa LibSVMMachineLearningModel
* \sa SVMMachineLearningModel
* \sa BoostMachineLearningModel
* \sa KNearestNeighborsMachineLearningModel
* \sa DecisionTreeMachineLearningModel
* \sa RandomForestsMachineLearningModel
* \sa GradientBoostedTreeMachineLearningModel
* \sa NormalBayesMachineLearningModel
* \sa NeuralNetworkMachineLearningModel
* \sa SharkRandomForestsMachineLearningModel
* \sa ImageClassificationFilter
*
*
* \ingroup OTBSupervised
*/
template <class TInputValue, class TTargetValue, class TConfidenceValue = double >
class ITK_EXPORT DimensionalityReductionModel
: public itk::Object
{
public:
/**\name Standard ITK typedefs */
//@{
typedef DimensionalityReductionModel Self;
typedef itk::Object Superclass;
typedef itk::SmartPointer<Self> Pointer;
typedef itk::SmartPointer<const Self> ConstPointer;
//@}
/**\name Input related typedefs */
//@{
typedef TInputValue InputValueType;
typedef itk::VariableLengthVector<InputValueType> InputSampleType;
typedef itk::Statistics::ListSample<InputSampleType> InputListSampleType;
//@}
/**\name Target related typedefs */
//@{
typedef TTargetValue TargetValueType;
typedef itk::VariableLengthVector<TargetValueType> TargetSampleType;
typedef itk::Statistics::ListSample<TargetSampleType> TargetListSampleType;
//@}
/**\name Confidence value typedef */
typedef TConfidenceValue ConfidenceValueType;
typedef itk::FixedArray<ConfidenceValueType,1> ConfidenceSampleType;
typedef itk::Statistics::ListSample<ConfidenceSampleType> ConfidenceListSampleType;
/**\name Standard macros */
//@{
/** Run-time type information (and related methods). */
itkTypeMacro(DimensionalityReductionModel, itk::Object);
//@}
/** Train the machine learning model */
virtual void Train() =0;
/** Predict a single sample
* \param input The sample
* \param quality A pointer to the quality variable were to store
* quality value, or NULL
* \return The predicted label
*/
TargetSampleType Predict(const InputSampleType& input, ConfidenceValueType *quality = ITK_NULLPTR) const;
/** Predict a batch of samples (InputListSampleType)
* \param input The batch of sample to predict
* \param quality A pointer to the list were to store
* quality value, or NULL
* \return The predicted labels
* Note that this method will be multi-threaded if OTB is built
* with OpenMP.
*/
typename TargetListSampleType::Pointer PredictBatch(const InputListSampleType * input, ConfidenceListSampleType * quality = ITK_NULLPTR) const;
/** THIS METHOD IS DEPRECATED AND SHOULD NOT BE USED. */
void PredictAll();
/**\name Classification model file manipulation */
//@{
/** Save the model to file */
virtual void Save(const std::string & filename, const std::string & name="") = 0;
/** Load the model from file */
virtual void Load(const std::string & filename, const std::string & name="") = 0;
//@}
/**\name Classification model file compatibility tests */
//@{
/** Is the input model file readable and compatible with the corresponding classifier ? */
virtual bool CanReadFile(const std::string &) = 0;
/** Is the input model file writable and compatible with the corresponding classifier ? */
virtual bool CanWriteFile(const std::string &) = 0;
//@}
/** Query capacity to produce a confidence index */
bool HasConfidenceIndex() const {return m_ConfidenceIndex;}
/**\name Input list of samples accessors */
//@{
itkSetObjectMacro(InputListSample,InputListSampleType);
itkGetObjectMacro(InputListSample,InputListSampleType);
itkGetConstObjectMacro(InputListSample,InputListSampleType);
//@}
/**\name Classification output accessors */
//@{
/** Set the target labels (to be used before training) */
itkSetObjectMacro(TargetListSample,TargetListSampleType);
/** Get the target labels (to be used after PredictAll) */
itkGetObjectMacro(TargetListSample,TargetListSampleType);
//@}
itkGetObjectMacro(ConfidenceListSample,ConfidenceListSampleType);
/**\name Use model in regression mode */
//@{
itkGetMacro(RegressionMode,bool);
void SetRegressionMode(bool flag);
//@}
protected:
/** Constructor */
DimensionalityReductionModel();
/** Destructor */
~DimensionalityReductionModel() ITK_OVERRIDE;
/** PrintSelf method */
void PrintSelf(std::ostream& os, itk::Indent indent) const ITK_OVERRIDE;
/** Input list sample */
typename InputListSampleType::Pointer m_InputListSample;
/** Target list sample */
typename TargetListSampleType::Pointer m_TargetListSample;
typename ConfidenceListSampleType::Pointer m_ConfidenceListSample;
/** flag to choose between classification and regression modes */
bool m_RegressionMode;
/** flag that indicates if the model supports regression, child
* classes should modify it in their constructor if they support
* regression mode */
bool m_IsRegressionSupported;
/** flag that tells if the model support confidence index output */
bool m_ConfidenceIndex;
/** Is DoPredictBatch multi-threaded ? */
bool m_IsDoPredictBatchMultiThreaded;
private:
/** Actual implementation of BatchPredicition
* Default implementation will call DoPredict iteratively
* \param input The input batch
* \param startIndex Index of the first sample to predict
* \param size Number of samples to predict
* \param target Pointer to the list of produced labels
* \param quality Pointer to the list of produced confidence
* values, or NULL
*
* Override me if internal implementation allows for batch
* prediction.
*
* Also set m_IsDoPredictBatchMultiThreaded to true if internal
* implementation allows for parallel batch prediction.
*/
virtual void DoPredictBatch(const InputListSampleType * input, const unsigned int & startIndex, const unsigned int & size, TargetListSampleType * target, ConfidenceListSampleType * quality = ITK_NULLPTR) const;
/** Actual implementation of single sample prediction
* \param input sample to predict
* \param quality Pointer to a variable to store confidence value,
* or NULL
* \return The predicted label
*/
virtual TargetSampleType DoPredict(const InputSampleType& input, ConfidenceValueType * quality= ITK_NULLPTR) const = 0;
DimensionalityReductionModel(const Self &); //purposely not implemented
void operator =(const Self&); //purposely not implemented
};
} // end namespace otb
#ifndef OTB_MANUAL_INSTANTIATION
#include "DimensionalityReductionModel.txx"
#endif
#endif
/*=========================================================================
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 DimensionalityReductionModel_txx
#define DimensionalityReductionModel_txx
#ifdef _OPENMP
# include <omp.h>
#endif
#include "DimensionalityReductionModel.h"
#include "itkMultiThreader.h"
namespace otb
{
template <class TInputValue, class TOutputValue, class TConfidenceValue>
DimensionalityReductionModel<TInputValue,TOutputValue,TConfidenceValue>
::DimensionalityReductionModel() :
m_RegressionMode(false),
m_IsRegressionSupported(false),
m_ConfidenceIndex(false),
m_IsDoPredictBatchMultiThreaded(false)
{}
template <class TInputValue, class TOutputValue, class TConfidenceValue>
DimensionalityReductionModel<TInputValue,TOutputValue,TConfidenceValue>
::~DimensionalityReductionModel()
{}
template <class TInputValue, class TOutputValue, class TConfidenceValue>
void
DimensionalityReductionModel<TInputValue,TOutputValue,TConfidenceValue>
::SetRegressionMode(bool flag)
{
if (flag && !m_IsRegressionSupported)
{
itkGenericExceptionMacro(<< "Regression mode not implemented.");
}
if (m_RegressionMode != flag)
{
m_RegressionMode = flag;
this->Modified();
}
}
template <class TInputValue, class TOutputValue, class TConfidenceValue>
void
DimensionalityReductionModel<TInputValue,TOutputValue,TConfidenceValue>
::PredictAll()
{
itkWarningMacro("DimensionalityReductionModel::PredictAll() has been DEPRECATED. Use DimensionalityReductionModel::PredictBatch() instead.");
typename TargetListSampleType::Pointer targets = this->GetTargetListSample();
targets->Clear();
typename TargetListSampleType::Pointer tmpTargets = this->PredictBatch(this->GetInputListSample());
targets->Graft(tmpTargets);
}
template <class TInputValue, class TOutputValue, class TConfidenceValue>
typename DimensionalityReductionModel<TInputValue,TOutputValue,TConfidenceValue>
::TargetSampleType
DimensionalityReductionModel<TInputValue,TOutputValue,TConfidenceValue>
::Predict(const InputSampleType& input, ConfidenceValueType *quality) const
{
// Call protected specialization entry point
return this->DoPredict(input,quality);
}
template <class TInputValue, class TOutputValue, class TConfidenceValue>
typename DimensionalityReductionModel<TInputValue,TOutputValue,TConfidenceValue>
::TargetListSampleType::Pointer
DimensionalityReductionModel<TInputValue,TOutputValue,TConfidenceValue>
::PredictBatch(const InputListSampleType * input, ConfidenceListSampleType * quality) const
{
typename TargetListSampleType::Pointer targets = TargetListSampleType::New();
targets->Resize(input->Size());
if(quality!=ITK_NULLPTR)
{
quality->Clear();
quality->Resize(input->Size());
}
if(m_IsDoPredictBatchMultiThreaded)
{
// Simply calls DoPredictBatch
this->DoPredictBatch(input,0,input->Size(),targets,quality);
return targets;
}
else
{
#ifdef _OPENMP
// OpenMP threading here
unsigned int nb_threads(0), threadId(0), nb_batches(0);
#pragma omp parallel shared(nb_threads,nb_batches) private(threadId)
{
// Get number of threads configured with ITK
omp_set_num_threads(itk::MultiThreader::GetGlobalDefaultNumberOfThreads());
nb_threads = omp_get_num_threads();
threadId = omp_get_thread_num();
nb_batches = std::min(nb_threads,(unsigned int)input->Size());
// Ensure that we do not spawn unncessary threads
if(threadId<nb_batches)
{
unsigned int batch_size = ((unsigned int)input->Size()/nb_batches);
unsigned int batch_start = threadId*batch_size;
if(threadId == nb_threads-1)
{
batch_size+=input->Size()%nb_batches;
}
this->DoPredictBatch(input,batch_start,batch_size,targets,quality);
}
}
#else
this->DoPredictBatch(input,0,input->Size(),targets,quality);
#endif
return targets;
}
}
template <class TInputValue, class TOutputValue, class TConfidenceValue>
void
DimensionalityReductionModel<TInputValue,TOutputValue,TConfidenceValue>
::DoPredictBatch(const InputListSampleType * input, const unsigned int & startIndex, const unsigned int & size, TargetListSampleType * targets, ConfidenceListSampleType * quality) const
{
assert(input != ITK_NULLPTR);
assert(targets != ITK_NULLPTR);
assert(input->Size()==targets->Size()&&"Input sample list and target label list do not have the same size.");
assert(((quality==ITK_NULLPTR)||(quality->Size()==input->Size()))&&"Quality samples list is not null and does not have the same size as input samples list");
if(startIndex+size>input->Size())
{
itkExceptionMacro(<<"requested range ["<<startIndex<<", "<<startIndex+size<<"[ partially outside input sample list range.[0,"<<input->Size()<<"[");
}
if(quality != ITK_NULLPTR)
{
for(unsigned int id = startIndex;id<startIndex+size;++id)
{
ConfidenceValueType confidence = 0;
const TargetSampleType target = this->DoPredict(input->GetMeasurementVector(id),&confidence);
quality->SetMeasurementVector(id,confidence);
targets->SetMeasurementVector(id,target);
}
}
else
{
for(unsigned int id = startIndex;id<startIndex+size;++id)
{
const TargetSampleType target = this->DoPredict(input->GetMeasurementVector(id));
targets->SetMeasurementVector(id,target);
}
}
}
template <class TInputValue, class TOutputValue, class TConfidenceValue>
void
DimensionalityReductionModel<TInputValue,TOutputValue,TConfidenceValue>
::PrintSelf(std::ostream& os, itk::Indent indent) const
{
// Call superclass implementation
Superclass::PrintSelf(os,indent);
}
}
#endif
/*=========================================================================
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 DimensionalityReductionModelFactory_h
#define DimensionalityReductionModelFactory_h
#include "DimensionalityReductionModel.h"
#include "otbMachineLearningModelFactoryBase.h"
namespace otb
{
/** \class MachineLearningModelFactory
* \brief Creation of object instance using object factory.
*
* \ingroup OTBSupervised
*/
template <class TInputValue, class TOutputValue>
class DimensionalityReductionModelFactory : public MachineLearningModelFactoryBase
{
public:
/** Standard class typedefs. */
typedef DimensionalityReductionModelFactory 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(DimensionalityReductionModelFactory, itk::Object);
/** Convenient typedefs. */
typedef otb::DimensionalityReductionModel<TInputValue,TOutputValue> DimensionalityReductionModelType;
typedef typename DimensionalityReductionModelType::Pointer DimensionalityReductionModelTypePointer;
/** 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 DimensionalityReductionModelTypePointer CreateDimensionalityReductionModel(const std::string& path, FileModeType mode);
static void CleanFactories();
protected:
DimensionalityReductionModelFactory();
~DimensionalityReductionModelFactory() ITK_OVERRIDE;
private:
DimensionalityReductionModelFactory(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 "DimensionalityReductionModelFactory.txx"
#endif
#endif
/*=========================================================================
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 DimensionalityReductionModelFactory_txx
#define DimensionalityReductionFactory_txx
#include "DimensionalityReductionModelFactory.h"
#include "otbConfigure.h"
#ifdef OTB_USE_SHARK
#include "AutoencoderModelFactory.h"
#endif
#include "itkMutexLockHolder.h"
namespace otb
{
template <class TInputValue, class TOutputValue>
typename DimensionalityReductionModel<TInputValue,TOutputValue>::Pointer
DimensionalityReductionModelFactory<TInputValue,TOutputValue>
::CreateDimensionalityReductionModel(const std::string& path, FileModeType mode)
{
RegisterBuiltInFactories();
std::list<DimensionalityReductionModelTypePointer> possibleDimensionalityReductionModel;
std::list<LightObject::Pointer> allobjects =
itk::ObjectFactoryBase::CreateAllInstance("DimensionalityReductionModel");
for(std::list<LightObject::Pointer>::iterator i = allobjects.begin();
i != allobjects.end(); ++i)
{
DimensionalityReductionModel<TInputValue,TOutputValue> * io = dynamic_cast<DimensionalityReductionModel<TInputValue,TOutputValue>*>(i->GetPointer());
if(io)
{
possibleDimensionalityReductionModel.push_back(io);
}
else
{
std::cerr << "Error DimensionalityReductionModel Factory did not return an DimensionalityReductionModel: "
<< (*i)->GetNameOfClass()
<< std::endl;
}
}
for(typename std::list<DimensionalityReductionModelTypePointer>::iterator k = possibleDimensionalityReductionModel.begin();
k != possibleDimensionalityReductionModel.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
DimensionalityReductionModelFactory<TInputValue,TOutputValue>
::RegisterBuiltInFactories()
{
itk::MutexLockHolder<itk::SimpleMutexLock> lockHolder(mutex);
#ifdef OTB_USE_SHARK
RegisterFactory(AutoencoderModelFactory<TInputValue,TOutputValue>::New());
RegisterFactory(TiedAutoencoderModelFactory<TInputValue,TOutputValue>::New());
#endif
}
template <class TInputValue, class TOutputValue>
void
DimensionalityReductionModelFactory<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
DimensionalityReductionModelFactory<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
// Autoencoder
AutoencoderModelFactory<TInputValue,TOutputValue> *aeFactory =
dynamic_cast<AutoencoderModelFactory<TInputValue,TOutputValue> *>(*itFac);
if (aeFactory)
{
itk::ObjectFactoryBase::UnRegisterFactory(aeFactory);
continue;
}
TiedAutoencoderModelFactory<TInputValue,TOutputValue> *taeFactory =
dynamic_cast<TiedAutoencoderModelFactory<TInputValue,TOutputValue> *>(*itFac);
if (taeFactory)
{
itk::ObjectFactoryBase::UnRegisterFactory(taeFactory);
continue;
}
#endif
}
}
} // end namespace otb
#endif
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