Newer
Older
Cédric Traizet
committed
#ifndef cbLearningApplicationBaseDR_h
#define cbLearningApplicationBaseDR_h
#include "otbConfigure.h"
#include "otbWrapperApplication.h"
#include <iostream>
// ListSample
#include "itkListSample.h"
#include "itkVariableLengthVector.h"
//Estimator
Cédric Traizet
committed
#include "SOMModel.h"
Cédric Traizet
committed
#ifdef OTB_USE_SHARK
#include "AutoencoderModel.h"
Cédric Traizet
committed
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#endif
namespace otb
{
namespace Wrapper
{
/** \class LearningApplicationBase
* \brief LearningApplicationBase is the base class for application that
* use machine learning model.
*
* This base class offers a DoInit() method to initialize all the parameters
* related to machine learning models. They will all be in the choice parameter
* named "classifier". The class also offers generic Train() and Classify()
* methods. The classes derived from LearningApplicationBase only need these
* 3 methods to handle the machine learning model.
*
* There are multiple machine learning models in OTB, some imported
* from OpenCV and one imported from LibSVM. They all have
* different parameters. The purpose of this class is to handle the
* creation of all parameters related to machine learning models (in
* DoInit() ), and to dispatch the calls to specific train functions
* in function Train().
*
* This class is templated over scalar types for input and output values.
* Typically, the input value type will be either float of double. The choice
* of an output value type depends on the learning mode. This base class
* supports both classification and regression modes. For classification
* (enabled by default), the output value type corresponds to a class
* identifier so integer types suit well. For regression, the output value
* should not be an integer type, but rather a floating point type. In addition,
* an application deriving this base class for regression should initialize
* the m_RegressionFlag to true in their constructor.
*
* \sa TrainImagesClassifier
* \sa TrainRegression
*
* \ingroup OTBAppClassification
*/
template <class TInputValue, class TOutputValue>
class cbLearningApplicationBaseDR: public Application
{
public:
/** Standard class typedefs. */
typedef cbLearningApplicationBaseDR Self;
typedef Application Superclass;
typedef itk::SmartPointer<Self> Pointer;
typedef itk::SmartPointer<const Self> ConstPointer;
/** Standard macro */
itkTypeMacro(cbLearningApplicationBaseDR, otb::Application)
typedef TInputValue InputValueType;
typedef TOutputValue OutputValueType;
typedef otb::VectorImage<InputValueType> SampleImageType;
typedef typename SampleImageType::PixelType PixelType;
Cédric Traizet
committed
InputValueType, OutputValueType> ModelFactoryType;
typedef typename ModelFactoryType::DimensionalityReductionModelTypePointer ModelPointerType;
typedef typename ModelFactoryType::DimensionalityReductionModelType ModelType;
Cédric Traizet
committed
typedef typename ModelType::InputSampleType SampleType;
typedef typename ModelType::InputListSampleType ListSampleType;
// Dimensionality reduction models
typedef SOMMap<itk::VariableLengthVector<TInputValue>,itk::Statistics::EuclideanDistanceMetric<itk::VariableLengthVector<TInputValue>>, 2> MapType;
typedef otb::SOM<ListSampleType, MapType> EstimatorType;
typedef otb::SOMModel<InputValueType> SOMModelType;
Cédric Traizet
committed
#ifdef OTB_USE_SHARK
typedef shark::Autoencoder< shark::TanhNeuron, shark::LinearNeuron> AutoencoderType;
typedef otb::AutoencoderModel<InputValueType, AutoencoderType> AutoencoderModelType;
typedef shark::TiedAutoencoder< shark::TanhNeuron, shark::LinearNeuron> TiedAutoencoderType;
typedef otb::AutoencoderModel<InputValueType, TiedAutoencoderType> TiedAutoencoderModelType;
Cédric Traizet
committed
typedef otb::PCAModel<InputValueType> PCAModelType;
Cédric Traizet
committed
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#endif
protected:
cbLearningApplicationBaseDR();
~cbLearningApplicationBaseDR() ITK_OVERRIDE;
/** Generic method to train and save the machine learning model. This method
* uses specific train methods depending on the chosen model.*/
void Train(typename ListSampleType::Pointer trainingListSample,
std::string modelPath);
/** Generic method to load a model file and use it to classify a sample list*/
void Reduce(typename ListSampleType::Pointer validationListSample,
std::string modelPath);
/** Init method that creates all the parameters for machine learning models */
void DoInit();
private:
/** Specific Init and Train methods for each machine learning model */
//@{
#ifdef OTB_USE_SHARK
void InitAutoencoderParams();
Cédric Traizet
committed
void InitPCAParams();
void InitSOMParams();
Cédric Traizet
committed
template <class autoencoderchoice>
Cédric Traizet
committed
void TrainAutoencoder(typename ListSampleType::Pointer trainingListSample, std::string modelPath);
Cédric Traizet
committed
void TrainPCA(typename ListSampleType::Pointer trainingListSample, std::string modelPath);
void TrainSOM(typename ListSampleType::Pointer trainingListSample, std::string modelPath);
Cédric Traizet
committed
#endif
//@}
};
}
}
#ifndef OTB_MANUAL_INSTANTIATION
#include "cbLearningApplicationBaseDR.txx"
#include "cbTrainSOM.txx"
Cédric Traizet
committed
#ifdef OTB_USE_SHARK
#include "cbTrainAutoencoder.txx"
Cédric Traizet
committed
#include "cbTrainPCA.txx"
Cédric Traizet
committed
#endif
#endif
#endif