Commit 19bc8096 authored by Arnaud Jaen's avatar Arnaud Jaen

ADD: Application for Training multiple types of classifier.

parent 33d0fc6e
......@@ -33,3 +33,7 @@ OTB_CREATE_APPLICATION(NAME ComputeConfusionMatrix
OTB_CREATE_APPLICATION(NAME ClassificationMapRegularization
SOURCES otbClassificationMapRegularization.cxx
LINK_LIBRARIES OTBIO;OTBCommon;OTBBasicFilters)
OTB_CREATE_APPLICATION(NAME TrainMachineLearningImagesClassifier
SOURCES otbTrainMachineLearningImagesClassifier.cxx
LINK_LIBRARIES OTBIO;OTBCommon;OTBBasicFilters;OTBFeatureExtraction;OTBLearning;OTBMachineLearning)
\ No newline at end of file
......@@ -32,6 +32,7 @@ SET(OTB_INCLUDE_DIRS_BUILD_TREE ${OTB_INCLUDE_DIRS_BUILD_TREE}
${OTB_SOURCE_DIR}/Code/UtilitiesAdapters/OssimAdapters
${OTB_SOURCE_DIR}/Code/UtilitiesAdapters/TinyXMLAdapters
${OTB_SOURCE_DIR}/Code/UtilitiesAdapters/OGRAdapters
${OTB_SOURCE_DIR}/Code/UtilitiesAdapters/OpenCV
${OTB_SOURCE_DIR}/Code/UtilitiesAdapters/ITKPendingPatches
${OTB_SOURCE_DIR}/Code/UtilitiesAdapters
${OTB_SOURCE_DIR}/Code/Visu
......@@ -283,6 +284,7 @@ SET(OTB_INCLUDE_RELATIVE_DIRS ${OTB_INCLUDE_RELATIVE_DIRS}
UtilitiesAdapters/OssimAdapters
UtilitiesAdapters/TinyXMLAdapters
UtilitiesAdapters/OGRAdapters
UtilitiesAdapters/OpenCV
UtilitiesAdapters/ITKPendingPatches
UtilitiesAdapters
OBIA
......
......@@ -230,6 +230,15 @@ INCLUDE(ImportTinyXML)
INCLUDE(ImportMuParser)
INCLUDE(ImportLibKML)
OPTION(OTB_USE_OPENCV "Use OpenCV library for classification." OFF)
MARK_AS_ADVANCED(OTB_USE_OPENCV)
IF(OTB_USE_OPENCV)
FIND_PACKAGE(OpenCV)
STRING(REPLACE "opencv" "opencv2" OpenCV2_INCLUDE_DIRS "${OpenCV_INCLUDE_DIRS}")
MESSAGE("Found OpenCV ${OpenCV_VERSION_MAJOR}.${OpenCV_VERSION_MINOR}.${OpenCV_VERSION_PATCH}")
MESSAGE("Include directory: ${OpenCV2_INCLUDE_DIRS}")
INCLUDE_DIRECTORIES(${OpenCV2_INCLUDE_DIRS})
ENDIF(OTB_USE_OPENCV)
INCLUDE_DIRECTORIES (
${OTB_BINARY_DIR}
......
......@@ -4,6 +4,7 @@ ADD_SUBDIRECTORY(TinyXMLAdapters)
ADD_SUBDIRECTORY(OGRAdapters)
ADD_SUBDIRECTORY(boost)
ADD_SUBDIRECTORY(ITKPendingPatches)
ADD_SUBDIRECTORY(OpenCV)
IF(NOT OTB_INSTALL_NO_DEVELOPMENT)
FILE(GLOB __files1 "${CMAKE_CURRENT_SOURCE_DIR}/*.h")
......
# Sources of non-templated classes.
FILE(GLOB OTBMachineLearning_SRCS "*.cxx" )
ADD_LIBRARY(OTBMachineLearning ${OTBMachineLearning_SRCS})
......@@ -9,3 +7,21 @@ TARGET_LINK_LIBRARIES (OTBMachineLearning OTBCommon OTBLearning ${OpenCV_LIBS})
IF(OTB_LIBRARY_PROPERTIES)
SET_TARGET_PROPERTIES(OTBMachineLearning PROPERTIES ${OTB_LIBRARY_PROPERTIES})
ENDIF(OTB_LIBRARY_PROPERTIES)
IF(NOT OTB_INSTALL_NO_LIBRARIES)
INSTALL(TARGETS OTBMachineLearning
RUNTIME DESTINATION ${OTB_INSTALL_BIN_DIR} COMPONENT RuntimeLibraries
LIBRARY DESTINATION ${OTB_INSTALL_LIB_DIR} COMPONENT RuntimeLibraries
ARCHIVE DESTINATION ${OTB_INSTALL_LIB_DIR} COMPONENT Development)
ENDIF(NOT OTB_INSTALL_NO_LIBRARIES)
IF(NOT OTB_INSTALL_NO_DEVELOPMENT)
FILE(GLOB __files1 "${CMAKE_CURRENT_SOURCE_DIR}/*.h")
FILE(GLOB __files2 "${CMAKE_CURRENT_SOURCE_DIR}/*.txx")
FILE(GLOB __files3 "${CMAKE_CURRENT_BINARY_DIR}/*.h")
# INSTALL(FILES ${__files1}
# INSTALL(FILES ${__files1} ${__files2}
INSTALL(FILES ${__files1} ${__files2} ${__files3}
DESTINATION ${OTB_INSTALL_INCLUDE_DIR}/UtilitiesAdapters/OGRAdapters
COMPONENT Development)
ENDIF(NOT OTB_INSTALL_NO_DEVELOPMENT)
\ No newline at end of file
......@@ -62,18 +62,18 @@ public:
virtual TargetSampleType Predict(const InputSampleType & input) const;
/** Save the model to file */
virtual void Save(char * filename, const char * name=0);
virtual void Save(const std::string & filename, const std::string & name="");
/** Load the model from file */
virtual void Load(char * filename, const char * name=0);
virtual void Load(const std::string & filename, const std::string & name="");
/** Determine the file type. Returns true if this ImageIO can read the
* file specified. */
virtual bool CanReadFile(const char*);
virtual bool CanReadFile(const std::string &);
/** Determine the file type. Returns true if this ImageIO can write the
* file specified. */
virtual bool CanWriteFile(const char*);
virtual bool CanWriteFile(const std::string &);
protected:
/** Constructor */
......
......@@ -79,23 +79,23 @@ KNearestNeighborsMachineLearningModel<TInputValue,TOutputValue>
template <class TInputValue, class TOutputValue>
void
KNearestNeighborsMachineLearningModel<TInputValue,TOutputValue>
::Save(char * filename, const char * name)
::Save(const std::string & filename, const std::string & name)
{
m_KNearestModel->save(filename, name);
m_KNearestModel->save(filename.c_str(), name.c_str());
}
template <class TInputValue, class TOutputValue>
void
KNearestNeighborsMachineLearningModel<TInputValue,TOutputValue>
::Load(char * filename, const char * name)
::Load(const std::string & filename, const std::string & name)
{
m_KNearestModel->load(filename, name);
m_KNearestModel->load(filename.c_str(), name.c_str());
}
template <class TInputValue, class TOutputValue>
bool
KNearestNeighborsMachineLearningModel<TInputValue,TOutputValue>
::CanReadFile(const char * file)
::CanReadFile(const std::string & file)
{
return false;
}
......@@ -103,7 +103,7 @@ KNearestNeighborsMachineLearningModel<TInputValue,TOutputValue>
template <class TInputValue, class TOutputValue>
bool
KNearestNeighborsMachineLearningModel<TInputValue,TOutputValue>
::CanWriteFile(const char * file)
::CanWriteFile(const std::string & file)
{
return false;
}
......
......@@ -74,18 +74,18 @@ public:
virtual TargetSampleType Predict(const InputSampleType & input) const;
/** Save the model to file */
virtual void Save(char * filename, const char * name=0);
virtual void Save(const std::string &filename, const std::string & name="");
/** Load the model from file */
virtual void Load(char * filename, const char * name=0);
virtual void Load(const std::string & filename, const std::string & name="");
/** Determine the file type. Returns true if this ImageIO can read the
* file specified. */
virtual bool CanReadFile(const char*);
virtual bool CanReadFile(const std::string &);
/** Determine the file type. Returns true if this ImageIO can write the
* file specified. */
virtual bool CanWriteFile(const char*);
virtual bool CanWriteFile(const std::string &);
//Setters/Getters to SVM model
// itkGetMacro(SVMType, int);
// itkSetMacro(SVMType, int);
......
......@@ -94,27 +94,27 @@ LibSVMMachineLearningModel<TInputValue,TOutputValue>
template <class TInputValue, class TOutputValue>
void
LibSVMMachineLearningModel<TInputValue,TOutputValue>
::Save(char * filename, const char * name)
::Save(const std::string & filename, const std::string & name)
{
m_SVMestimator->GetModel()->SaveModel(filename);
m_SVMestimator->GetModel()->SaveModel(filename.c_str());
}
template <class TInputValue, class TOutputValue>
void
LibSVMMachineLearningModel<TInputValue,TOutputValue>
::Load(char * filename, const char * name)
::Load(const std::string & filename, const std::string & name)
{
m_SVMestimator->GetModel()->LoadModel(filename);
m_SVMestimator->GetModel()->LoadModel(filename.c_str());
}
template <class TInputValue, class TOutputValue>
bool
LibSVMMachineLearningModel<TInputValue,TOutputValue>
::CanReadFile(const char * file)
::CanReadFile(const std::string & file)
{
//TODO: Rework.
std::ifstream ifs;
ifs.open(file);
ifs.open(file.c_str());
if(!ifs)
{
......@@ -139,7 +139,7 @@ LibSVMMachineLearningModel<TInputValue,TOutputValue>
template <class TInputValue, class TOutputValue>
bool
LibSVMMachineLearningModel<TInputValue,TOutputValue>
::CanWriteFile(const char * file)
::CanWriteFile(const std::string & file)
{
return false;
}
......
......@@ -57,18 +57,18 @@ public:
void PredictAll();
/** Save the model to file */
virtual void Save(char * filename, const char * name=0) = 0;
virtual void Save(const std::string & filename, const std::string & name="") = 0;
/** Load the model from file */
virtual void Load(char * filename, const char * name=0) = 0;
virtual void Load(const std::string & filename, const std::string & name="") = 0;
/** Determine the file type. Returns true if this ImageIO can read the
* file specified. */
virtual bool CanReadFile(const char*) = 0;
virtual bool CanReadFile(const std::string &) = 0;
/** Determine the file type. Returns true if this ImageIO can read the
* file specified. */
virtual bool CanWriteFile(const char*) = 0;
virtual bool CanWriteFile(const std::string &) = 0;
/** Input accessors */
itkSetObjectMacro(InputListSample,InputListSampleType);
......
......@@ -65,18 +65,18 @@ public:
virtual TargetSampleType Predict(const InputSampleType & input) const;
/** Save the model to file */
virtual void Save(char * filename, const char * name=0);
virtual void Save(const std::string & filename, const std::string & name="");
/** Load the model from file */
virtual void Load(char * filename, const char * name=0);
virtual void Load(const std::string & filename, const std::string & name="");
/** Determine the file type. Returns true if this ImageIO can read the
* file specified. */
virtual bool CanReadFile(const char*);
virtual bool CanReadFile(const std::string &);
/** Determine the file type. Returns true if this ImageIO can write the
* file specified. */
virtual bool CanWriteFile(const char*);
virtual bool CanWriteFile(const std::string &);
/* /\** Input accessors *\/ */
/* itkSetObjectMacro(InputListSample,InputListSampleType); */
......
......@@ -116,26 +116,26 @@ RandomForestsMachineLearningModel<TInputValue,TOutputValue>
template <class TInputValue, class TOutputValue>
void
RandomForestsMachineLearningModel<TInputValue,TOutputValue>
::Save(char * filename, const char * name)
::Save(const std::string & filename, const std::string & name)
{
m_RFModel->save(filename, name);
m_RFModel->save(filename.c_str(), name.c_str());
}
template <class TInputValue, class TOutputValue>
void
RandomForestsMachineLearningModel<TInputValue,TOutputValue>
::Load(char * filename, const char * name)
::Load(const std::string & filename, const std::string & name)
{
m_RFModel->load(filename, name);
m_RFModel->load(filename.c_str(), name.c_str());
}
template <class TInputValue, class TOutputValue>
bool
RandomForestsMachineLearningModel<TInputValue,TOutputValue>
::CanReadFile(const char * file)
::CanReadFile(const std::string & file)
{
std::ifstream ifs;
ifs.open(file);
ifs.open(file.c_str());
if(!ifs)
{
......@@ -163,7 +163,7 @@ RandomForestsMachineLearningModel<TInputValue,TOutputValue>
template <class TInputValue, class TOutputValue>
bool
RandomForestsMachineLearningModel<TInputValue,TOutputValue>
::CanWriteFile(const char * file)
::CanWriteFile(const std::string & file)
{
return false;
}
......
......@@ -63,18 +63,18 @@ public:
virtual TargetSampleType Predict(const InputSampleType & input) const;
/** Save the model to file */
virtual void Save(char * filename, const char * name=0);
virtual void Save(const std::string & filename, const std::string & name="");
/** Load the model from file */
virtual void Load(char * filename, const char * name=0);
virtual void Load(const std::string & filename, const std::string & name="");
/** Determine the file type. Returns true if this ImageIO can read the
* file specified. */
virtual bool CanReadFile(const char*);
virtual bool CanReadFile(const std::string &);
/** Determine the file type. Returns true if this ImageIO can write the
* file specified. */
virtual bool CanWriteFile(const char*);
virtual bool CanWriteFile(const std::string &);
//Setters/Getters to SVM model
itkGetMacro(SVMType, int);
......@@ -83,14 +83,39 @@ public:
itkGetMacro(KernelType, int);
itkSetMacro(KernelType, int);
//CV_TERMCRIT_ITER or CV_TERMCRIT_EPS
itkGetMacro(TermCriteriaType, int);
itkSetMacro(TermCriteriaType, int);
itkGetMacro(MaxIter, int);
itkSetMacro(MaxIter, int);
itkGetMacro(Epsilon, int);
itkSetMacro(Epsilon, int);
itkGetMacro(Epsilon, double);
itkSetMacro(Epsilon, double);
// for poly
itkGetMacro(Degree, double);
itkSetMacro(Degree, double);
// for poly/rbf/sigmoid
itkGetMacro(Gamma, double);
itkSetMacro(Gamma, double);
// for poly/sigmoid
itkGetMacro(Coef0, double);
itkSetMacro(Coef0, double);
// for CV_SVM_C_SVC, CV_SVM_EPS_SVR and CV_SVM_NU_SVR
itkGetMacro(C, double);
itkSetMacro(C, double);
// for CV_SVM_NU_SVC, CV_SVM_ONE_CLASS, and CV_SVM_NU_SVR
itkGetMacro(Nu, double);
itkSetMacro(Nu, double);
// for CV_SVM_EPS_SVR
itkGetMacro(P, double);
itkSetMacro(P, double);
protected:
/** Constructor */
......@@ -111,7 +136,13 @@ private:
int m_KernelType;
int m_TermCriteriaType;
int m_MaxIter;
int m_Epsilon;
double m_Epsilon;
double m_Degree;
double m_Gamma;
double m_Coef0;
double m_C;
double m_Nu;
double m_P;
};
} // end namespace otb
......
......@@ -27,14 +27,12 @@ namespace otb
template <class TInputValue, class TOutputValue>
SVMMachineLearningModel<TInputValue,TOutputValue>
::SVMMachineLearningModel()
::SVMMachineLearningModel() :
m_SVMType(CvSVM::C_SVC), m_KernelType(CvSVM::LINEAR), m_TermCriteriaType(CV_TERMCRIT_ITER),
m_MaxIter(100), m_Epsilon(1e-6), m_Degree(0), m_Gamma(1),
m_Coef0(0), m_C(1), m_Nu(0), m_P(0)
{
m_SVMModel = new CvSVM;
m_SVMType = CvSVM::C_SVC;
m_KernelType = CvSVM::LINEAR;
m_TermCriteriaType = CV_TERMCRIT_ITER;
m_MaxIter = 100;
m_Epsilon = 1e-6;
}
......@@ -59,10 +57,9 @@ SVMMachineLearningModel<TInputValue,TOutputValue>
otb::ListSampleToMat<TargetListSampleType>(this->GetTargetListSample(),labels);
// Set up SVM's parameters
CvSVMParams params;
params.svm_type = m_SVMType;
params.kernel_type = m_KernelType;
params.term_crit = cvTermCriteria(m_TermCriteriaType, m_MaxIter, m_Epsilon);
CvTermCriteria term_crit = cvTermCriteria(m_TermCriteriaType, m_MaxIter, m_Epsilon);
CvMat* class_weights;
CvSVMParams params( m_SVMType, m_KernelType, m_Degree, m_Gamma, m_Coef0, m_C, m_Nu, m_P, NULL , term_crit );
// Train the SVM
m_SVMModel->train(samples, labels, cv::Mat(), cv::Mat(), params);
......@@ -91,26 +88,32 @@ SVMMachineLearningModel<TInputValue,TOutputValue>
template <class TInputValue, class TOutputValue>
void
SVMMachineLearningModel<TInputValue,TOutputValue>
::Save(char * filename, const char * name)
::Save(const std::string & filename, const std::string & name)
{
m_SVMModel->save(filename, name);
if (name == "")
m_SVMModel->save(filename.c_str(), 0);
else
m_SVMModel->save(filename.c_str(), name.c_str());
}
template <class TInputValue, class TOutputValue>
void
SVMMachineLearningModel<TInputValue,TOutputValue>
::Load(char * filename, const char * name)
::Load(const std::string & filename, const std::string & name)
{
m_SVMModel->load(filename, name);
if (name == "")
m_SVMModel->load(filename.c_str(), 0);
else
m_SVMModel->load(filename.c_str(), name.c_str());
}
template <class TInputValue, class TOutputValue>
bool
SVMMachineLearningModel<TInputValue,TOutputValue>
::CanReadFile(const char * file)
::CanReadFile(const std::string & file)
{
std::ifstream ifs;
ifs.open(file);
ifs.open(file.c_str());
if(!ifs)
{
......@@ -137,7 +140,7 @@ SVMMachineLearningModel<TInputValue,TOutputValue>
template <class TInputValue, class TOutputValue>
bool
SVMMachineLearningModel<TInputValue,TOutputValue>
::CanWriteFile(const char * file)
::CanWriteFile(const std::string & file)
{
return false;
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment