Commit 735349bc authored by Christophe Palmann's avatar Christophe Palmann

ENH: fixed TrainImagesClassifier app comp. when libsvm and opencv modules are not installed

parent eab17f35
......@@ -22,6 +22,7 @@ namespace otb
{
namespace Wrapper
{
#ifdef OTB_USE_OPENCV
void TrainImagesClassifier::InitBoostParams()
{
AddChoice("classifier.boost", "Boost classifier");
......@@ -68,6 +69,7 @@ namespace Wrapper
boostClassifier->Train();
boostClassifier->Save(GetParameterString("io.out"));
}
#endif
} //end namespace wrapper
} //end namespace otb
......@@ -21,6 +21,7 @@ namespace otb
{
namespace Wrapper
{
#ifdef OTB_USE_OPENCV
void TrainImagesClassifier::InitDecisionTreeParams()
{
AddChoice("classifier.dt", "Decision Tree classifier");
......@@ -102,6 +103,6 @@ void TrainImagesClassifier::TrainDecisionTree(ListSampleType::Pointer trainingLi
classifier->Train();
classifier->Save(GetParameterString("io.out"));
}
#endif
} //end namespace wrapper
} //end namespace otb
......@@ -21,6 +21,7 @@ namespace otb
{
namespace Wrapper
{
#ifdef OTB_USE_OPENCV
void TrainImagesClassifier::InitGradientBoostedTreeParams()
{
AddChoice("classifier.gbt", "Gradient Boosted Tree classifier");
......@@ -80,6 +81,6 @@ void TrainImagesClassifier::TrainGradientBoostedTree(
classifier->Train();
classifier->Save(GetParameterString("io.out"));
}
#endif
} //end namespace wrapper
} //end namespace otb
......@@ -105,7 +105,9 @@ void TrainImagesClassifier::DoInit()
SetParameterDescription("classifier", "Choice of the classifier to use for the training.");
//Group LibSVM
#ifdef OTB_USE_LIBSVM
InitLibSVMParams();
#endif
#ifdef OTB_USE_OPENCV
InitSVMParams();
......@@ -395,44 +397,80 @@ void TrainImagesClassifier::DoExecute()
//--------------------------
LabelListSampleType::Pointer predictedList = LabelListSampleType::New();
const std::string classifierType = GetParameterString("classifier");
if (classifierType == "libsvm")
{
#ifdef OTB_USE_LIBSVM
TrainLibSVM(trainingListSample, trainingLabeledListSample);
#elseif
otbAppLogFATAL("Module LIBSVM is not installed. You should consider turning OTB_USE_LIBSVM on during cmake configuration.");
#endif
}
#ifdef OTB_USE_OPENCV
else if (classifierType == "svm")
{
#ifdef OTB_USE_OPENCV
TrainSVM(trainingListSample, trainingLabeledListSample);
#elseif
otbAppLogFATAL("Module OPENCV is not installed. You should consider turning OTB_USE_OPENCV on during cmake configuration.");
#endif
}
else if (classifierType == "boost")
{
#ifdef OTB_USE_OPENCV
TrainBoost(trainingListSample, trainingLabeledListSample);
#elseif
otbAppLogFATAL("Module OPENCV is not installed. You should consider turning OTB_USE_OPENCV on during cmake configuration.");
#endif
}
else if (classifierType == "dt")
{
#ifdef OTB_USE_OPENCV
TrainDecisionTree(trainingListSample, trainingLabeledListSample);
#elseif
otbAppLogFATAL("Module OPENCV is not installed. You should consider turning OTB_USE_OPENCV on during cmake configuration.");
#endif
}
else if (classifierType == "gbt")
{
#ifdef OTB_USE_OPENCV
TrainGradientBoostedTree(trainingListSample, trainingLabeledListSample);
#elseif
otbAppLogFATAL("Module OPENCV is not installed. You should consider turning OTB_USE_OPENCV on during cmake configuration.");
#endif
}
else if (classifierType == "ann")
{
#ifdef OTB_USE_OPENCV
TrainNeuralNetwork(trainingListSample, trainingLabeledListSample);
#elseif
otbAppLogFATAL("Module OPENCV is not installed. You should consider turning OTB_USE_OPENCV on during cmake configuration.");
#endif
}
else if (classifierType == "bayes")
{
#ifdef OTB_USE_OPENCV
TrainNormalBayes(trainingListSample, trainingLabeledListSample);
#elseif
otbAppLogFATAL("Module OPENCV is not installed. You should consider turning OTB_USE_OPENCV on during cmake configuration.");
#endif
}
else if (classifierType == "rf")
{
#ifdef OTB_USE_OPENCV
TrainRandomForests(trainingListSample, trainingLabeledListSample);
#elseif
otbAppLogFATAL("Module OPENCV is not installed. You should consider turning OTB_USE_OPENCV on during cmake configuration.");
#endif
}
else if (classifierType == "knn")
{
#ifdef OTB_USE_OPENCV
TrainKNN(trainingListSample, trainingLabeledListSample);
#elseif
otbAppLogFATAL("Module OPENCV is not installed. You should consider turning OTB_USE_OPENCV on during cmake configuration.");
#endif
}
#endif
//--------------------------
// Performances estimation
......
......@@ -42,7 +42,9 @@
# include "otbNeuralNetworkMachineLearningModel.h"
#endif
#ifdef OTB_USE_LIBSVM
#include "otbLibSVMMachineLearningModel.h"
#endif
// Statistic XML Reader
#include "otbStatisticsXMLFileReader.h"
......@@ -124,8 +126,11 @@ public:
typedef otb::NeuralNetworkMachineLearningModel<InternalPixelType, ListSampleGeneratorType::ClassLabelType> NeuralNetworkType;
typedef otb::NormalBayesMachineLearningModel<InternalPixelType, ListSampleGeneratorType::ClassLabelType> NormalBayesType;
#endif
#ifdef OTB_USE_LIBSVM
typedef otb::LibSVMMachineLearningModel<InternalPixelType, ListSampleGeneratorType::ClassLabelType> LibSVMType;
#endif
// Estimate performance on validation sample
typedef otb::ConfusionMatrixCalculator<LabelListSampleType, LabelListSampleType> ConfusionMatrixCalculatorType;
typedef ConfusionMatrixCalculatorType::ConfusionMatrixType ConfusionMatrixType;
......@@ -150,7 +155,10 @@ private:
void LogConfusionMatrix(ConfusionMatrixCalculatorType* confMatCalc);
#ifdef OTB_USE_LIBSVM
void InitLibSVMParams();
#endif
#ifdef OTB_USE_OPENCV
void InitBoostParams();
void InitSVMParams();
......@@ -161,8 +169,11 @@ private:
void InitRandomForestsParams();
void InitKNNParams();
#endif
#ifdef OTB_USE_LIBSVM
void TrainLibSVM(ListSampleType::Pointer trainingListSample, LabelListSampleType::Pointer trainingLabeledListSample);
#endif
#ifdef OTB_USE_OPENCV
void TrainBoost(ListSampleType::Pointer trainingListSample, LabelListSampleType::Pointer trainingLabeledListSample);
void TrainSVM(ListSampleType::Pointer trainingListSample, LabelListSampleType::Pointer trainingLabeledListSample);
......
......@@ -22,6 +22,7 @@ namespace otb
{
namespace Wrapper
{
#ifdef OTB_USE_OPENCV
void TrainImagesClassifier::InitKNNParams()
{
AddChoice("classifier.knn", "KNN classifier");
......@@ -46,6 +47,6 @@ namespace Wrapper
knnClassifier->Train();
knnClassifier->Save(GetParameterString("io.out"));
}
#endif
} //end namespace wrapper
} //end namespace otb
......@@ -22,6 +22,7 @@ namespace otb
{
namespace Wrapper
{
#ifdef OTB_USE_LIBSVM
void TrainImagesClassifier::InitLibSVMParams()
{
AddChoice("classifier.libsvm", "LibSVM classifier");
......@@ -78,6 +79,6 @@ namespace Wrapper
libSVMClassifier->Train();
libSVMClassifier->Save(GetParameterString("io.out"));
}
#endif
} //end namespace wrapper
} //end namespace otb
......@@ -22,6 +22,7 @@ namespace otb
{
namespace Wrapper
{
#ifdef OTB_USE_OPENCV
void TrainImagesClassifier::InitNeuralNetworkParams()
{
AddChoice("classifier.ann", "Artificial Neural Network classifier");
......@@ -209,6 +210,6 @@ void TrainImagesClassifier::TrainNeuralNetwork(ListSampleType::Pointer trainingL
classifier->Train();
classifier->Save(GetParameterString("io.out"));
}
#endif
} //end namespace wrapper
} //end namespace otb
......@@ -22,6 +22,7 @@ namespace otb
{
namespace Wrapper
{
#ifdef OTB_USE_OPENCV
void TrainImagesClassifier::InitNormalBayesParams()
{
AddChoice("classifier.bayes", "Normal Bayes classifier");
......@@ -39,6 +40,6 @@ namespace Wrapper
classifier->Train();
classifier->Save(GetParameterString("io.out"));
}
#endif
} //end namespace wrapper
} //end namespace otb
......@@ -21,6 +21,7 @@ namespace otb
{
namespace Wrapper
{
#ifdef OTB_USE_OPENCV
void TrainImagesClassifier::InitRandomForestsParams()
{
AddChoice("classifier.rf", "Random forests classifier");
......@@ -111,6 +112,6 @@ void TrainImagesClassifier::TrainRandomForests(ListSampleType::Pointer trainingL
classifier->Train();
classifier->Save(GetParameterString("io.out"));
}
#endif
} //end namespace wrapper
} //end namespace otb
......@@ -22,6 +22,7 @@ namespace otb
{
namespace Wrapper
{
#ifdef OTB_USE_OPENCV
void TrainImagesClassifier::InitSVMParams()
{
AddChoice("classifier.svm", "SVM classifier (OpenCV)");
......@@ -75,7 +76,6 @@ namespace Wrapper
"because the samples are not identically processed within OpenCV.");
}
void TrainImagesClassifier::TrainSVM(ListSampleType::Pointer trainingListSample, LabelListSampleType::Pointer trainingLabeledListSample)
{
SVMType::Pointer SVMClassifier = SVMType::New();
......@@ -152,6 +152,7 @@ namespace Wrapper
SetParameterFloat("classifier.svm.gamma", static_cast<float> (SVMClassifier->GetOutputGamma()));
SetParameterFloat("classifier.svm.degree", static_cast<float> (SVMClassifier->GetOutputDegree()));
}
#endif
} //end namespace wrapper
} //end namespace otb
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