An error occurred while loading the file. Please try again.
-
Manuel Grizonnet authored
OTB followed since the beginning the ITK convention and use .txx extension for all template classes. Nevertheless, some development tools do not recognize .txx file extension. Other tool like GitHub can't do in-browser syntax highlighting for txx files I think. The root problem is the use of the txx which should be changed to hxx (or hpp). In 2011, after an in-depth discussion near April 20, 2011 on the Insight-Developers mailing list, ITK rename all txx files to hxx (and event prevent the push of .txx files with a pre-commit hook). It happens is major release v4. You can find some arguments in the discussion about the change and also in other projects related to ITK which applied the same modification, see for instance VXL: https://github.com/vxl/vxl/issues/209 This commit apply now the same modification for OTB. I understand that it will change some habit for developers and don't bring new features but I think that in general it is better to stay align with ITK guidelines. In my opinion, it always facilitate the use of OTB and ITK together if we share when we can the same code architecture, directory organization, naming conventions...
3a1fd1fc
/*
* Copyright (C) 2005-2017 Centre National d'Etudes Spatiales (CNES)
*
* This file is part of Orfeo Toolbox
*
* https://www.orfeo-toolbox.org/
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef otbLearningApplicationBase_txx
#define otbLearningApplicationBase_txx
#include "otbLearningApplicationBase.h"
// only need this filter as a dummy process object
#include "otbRGBAPixelConverter.h"
namespace otb
{
namespace Wrapper
{
template <class TInputValue, class TOutputValue>
LearningApplicationBase<TInputValue,TOutputValue>
::LearningApplicationBase() : m_RegressionFlag(false)
{
}
template <class TInputValue, class TOutputValue>
LearningApplicationBase<TInputValue,TOutputValue>
::~LearningApplicationBase()
{
ModelFactoryType::CleanFactories();
}
template <class TInputValue, class TOutputValue>
void
LearningApplicationBase<TInputValue,TOutputValue>
::DoInit()
{
AddDocTag(Tags::Learning);
// main choice parameter that will contain all machine learning options
AddParameter(ParameterType_Choice, "classifier", "Classifier to use for the training");
SetParameterDescription("classifier", "Choice of the classifier to use for the training.");
InitSupervisedClassifierParams();
m_SupervisedClassifier = GetChoiceKeys("classifier");
InitUnsupervisedClassifierParams();
std::vector<std::string> allClassifier = GetChoiceKeys("classifier");
// Check for empty unsupervised classifier
if( allClassifier.size() > m_UnsupervisedClassifier.size() )
m_UnsupervisedClassifier.assign( allClassifier.begin() + m_SupervisedClassifier.size(), allClassifier.end() );
}
template <class TInputValue, class TOutputValue>
typename LearningApplicationBase<TInputValue,TOutputValue>::ClassifierCategory
LearningApplicationBase<TInputValue,TOutputValue>
7172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
::GetClassifierCategory()
{
if( m_UnsupervisedClassifier.empty() )
{
return Supervised;
}
else
{
bool foundUnsupervised = std::find( m_UnsupervisedClassifier.begin(), m_UnsupervisedClassifier.end(),
GetParameterString( "classifier" ) ) != m_UnsupervisedClassifier.end();
return foundUnsupervised ? Unsupervised : Supervised;
}
}
template <class TInputValue, class TOutputValue>
void
LearningApplicationBase<TInputValue,TOutputValue>
::InitSupervisedClassifierParams()
{
//Group LibSVM
#ifdef OTB_USE_LIBSVM
InitLibSVMParams();
#endif
#ifdef OTB_USE_OPENCV
// OpenCV SVM implementation is buggy with linear kernel
// Users should use the libSVM implementation instead.
// InitSVMParams();
if (!m_RegressionFlag)
{
InitBoostParams(); // Regression not supported
}
InitDecisionTreeParams();
InitGradientBoostedTreeParams();
InitNeuralNetworkParams();
if (!m_RegressionFlag)
{
InitNormalBayesParams(); // Regression not supported
}
InitRandomForestsParams();
InitKNNParams();
#endif
#ifdef OTB_USE_SHARK
InitSharkRandomForestsParams();
#endif
}
template <class TInputValue, class TOutputValue>
void
LearningApplicationBase<TInputValue,TOutputValue>
::InitUnsupervisedClassifierParams()
{
#ifdef OTB_USE_SHARK
InitSharkKMeansParams();
#endif
}
template <class TInputValue, class TOutputValue>
typename LearningApplicationBase<TInputValue,TOutputValue>
::TargetListSampleType::Pointer
LearningApplicationBase<TInputValue,TOutputValue>
::Classify(typename ListSampleType::Pointer validationListSample,
std::string modelPath)
{
// Setup fake reporter
RGBAPixelConverter<int,int>::Pointer dummyFilter =
RGBAPixelConverter<int,int>::New();
dummyFilter->SetProgress(0.0f);
141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
this->AddProcess(dummyFilter,"Classify...");
dummyFilter->InvokeEvent(itk::StartEvent());
// load a machine learning model from file and predict the input sample list
ModelPointerType model = ModelFactoryType::CreateMachineLearningModel(modelPath,
ModelFactoryType::ReadMode);
if (model.IsNull())
{
otbAppLogFATAL(<< "Error when loading model " << modelPath);
}
model->Load(modelPath);
model->SetRegressionMode(this->m_RegressionFlag);
typename TargetListSampleType::Pointer predictedList = model->PredictBatch(validationListSample, NULL);
// update reporter
dummyFilter->UpdateProgress(1.0f);
dummyFilter->InvokeEvent(itk::EndEvent());
return predictedList;
}
template <class TInputValue, class TOutputValue>
void
LearningApplicationBase<TInputValue,TOutputValue>
::Train(typename ListSampleType::Pointer trainingListSample,
typename TargetListSampleType::Pointer trainingLabeledListSample,
std::string modelPath)
{
// Setup fake reporter
RGBAPixelConverter<int,int>::Pointer dummyFilter =
RGBAPixelConverter<int,int>::New();
dummyFilter->SetProgress(0.0f);
this->AddProcess(dummyFilter,"Training model...");
dummyFilter->InvokeEvent(itk::StartEvent());
// get the name of the chosen machine learning model
const std::string modelName = GetParameterString("classifier");
// call specific train function
if (modelName == "libsvm")
{
#ifdef OTB_USE_LIBSVM
TrainLibSVM(trainingListSample, trainingLabeledListSample, modelPath);
#else
otbAppLogFATAL("Module LIBSVM is not installed. You should consider turning OTB_USE_LIBSVM on during cmake configuration.");
#endif
}
if(modelName == "sharkrf")
{
#ifdef OTB_USE_SHARK
TrainSharkRandomForests(trainingListSample,trainingLabeledListSample,modelPath);
#else
otbAppLogFATAL("Module SharkLearning is not installed. You should consider turning OTB_USE_SHARK on during cmake configuration.");
#endif
}
else if(modelName == "sharkkm")
{
#ifdef OTB_USE_SHARK
TrainSharkKMeans( trainingListSample, trainingLabeledListSample, modelPath );
#else
otbAppLogFATAL("Module SharkLearning is not installed. You should consider turning OTB_USE_SHARK on during cmake configuration.");
#endif
}
else if (modelName == "svm")
{
#ifdef OTB_USE_OPENCV
TrainSVM(trainingListSample, trainingLabeledListSample, modelPath);
#else
211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
otbAppLogFATAL("Module OPENCV is not installed. You should consider turning OTB_USE_OPENCV on during cmake configuration.");
#endif
}
else if (modelName == "boost")
{
#ifdef OTB_USE_OPENCV
TrainBoost(trainingListSample, trainingLabeledListSample, modelPath);
#else
otbAppLogFATAL("Module OPENCV is not installed. You should consider turning OTB_USE_OPENCV on during cmake configuration.");
#endif
}
else if (modelName == "dt")
{
#ifdef OTB_USE_OPENCV
TrainDecisionTree(trainingListSample, trainingLabeledListSample, modelPath);
#else
otbAppLogFATAL("Module OPENCV is not installed. You should consider turning OTB_USE_OPENCV on during cmake configuration.");
#endif
}
else if (modelName == "gbt")
{
#ifdef OTB_USE_OPENCV
TrainGradientBoostedTree(trainingListSample, trainingLabeledListSample, modelPath);
#else
otbAppLogFATAL("Module OPENCV is not installed. You should consider turning OTB_USE_OPENCV on during cmake configuration.");
#endif
}
else if (modelName == "ann")
{
#ifdef OTB_USE_OPENCV
TrainNeuralNetwork(trainingListSample, trainingLabeledListSample, modelPath);
#else
otbAppLogFATAL("Module OPENCV is not installed. You should consider turning OTB_USE_OPENCV on during cmake configuration.");
#endif
}
else if (modelName == "bayes")
{
#ifdef OTB_USE_OPENCV
TrainNormalBayes(trainingListSample, trainingLabeledListSample, modelPath);
#else
otbAppLogFATAL("Module OPENCV is not installed. You should consider turning OTB_USE_OPENCV on during cmake configuration.");
#endif
}
else if (modelName == "rf")
{
#ifdef OTB_USE_OPENCV
TrainRandomForests(trainingListSample, trainingLabeledListSample, modelPath);
#else
otbAppLogFATAL("Module OPENCV is not installed. You should consider turning OTB_USE_OPENCV on during cmake configuration.");
#endif
}
else if (modelName == "knn")
{
#ifdef OTB_USE_OPENCV
TrainKNN(trainingListSample, trainingLabeledListSample, modelPath);
#else
otbAppLogFATAL("Module OPENCV is not installed. You should consider turning OTB_USE_OPENCV on during cmake configuration.");
#endif
}
// update reporter
dummyFilter->UpdateProgress(1.0f);
dummyFilter->InvokeEvent(itk::EndEvent());
}
}
}
#endif