diff --git a/Testing/Code/Learning/CMakeLists.txt b/Testing/Code/Learning/CMakeLists.txt index c74bf3ad5fcdcd7897923685d6d1cbe02c45c55e..476656e84322be1a118c8e0c0fca465cd0c0c989 100644 --- a/Testing/Code/Learning/CMakeLists.txt +++ b/Testing/Code/Learning/CMakeLists.txt @@ -684,14 +684,10 @@ IF(OTB_USE_OPENCV) ADD_TEST(leTuRandomForestsMachineLearningModelNew ${LEARNING_TESTS6} otbRandomForestsMachineLearningModelNew) - ADD_TEST(leTvRandomForestsMachineLearningModel ${LEARNING_TESTS6} - #--compare-ascii ${NOTOL} - #${BASELINE_FILES}/RandomForestsMachineLearningModel.txt - #${TEMP}/RandomForestsMachineLearningModel.txt otbRandomForestsMachineLearningModel ${INPUTDATA}/letter.scale - ${TEMP}/RandomForestsMachineLearningModel.txt + ${TEMP}/rf_model.txt ) ADD_TEST(leTuKNearestNeighborsMachineLearningModelNew ${LEARNING_TESTS6} @@ -699,15 +695,14 @@ IF(OTB_USE_OPENCV) ADD_TEST(leTvKNearestNeighborsMachineLearningModel ${LEARNING_TESTS6} otbKNearestNeighborsMachineLearningModel - ${INPUTDATA}/letter.scale) + ${INPUTDATA}/letter.scale + ${TEMP}/knn_model.txt + ) ADD_TEST(leTuSVMMachineLearningModelNew ${LEARNING_TESTS6} otbSVMMachineLearningModelNew) ADD_TEST(leTvSVMMachineLearningModel ${LEARNING_TESTS6} - --compare-ascii ${NOTOL} - ${BASELINE_FILES}/svm_model.txt - ${TEMP}/svm_model.txt otbSVMMachineLearningModel ${INPUTDATA}/letter.scale ${TEMP}/svm_model.txt @@ -717,73 +712,54 @@ IF(OTB_USE_OPENCV) otbLibSVMMachineLearningModelNew) ADD_TEST(leTvLibSVMMachineLearningModel ${LEARNING_TESTS6} - --compare-ascii ${NOTOL} - ${BASELINE_FILES}/libsvm_model.txt - ${TEMP}/libsvm_model.txt otbLibSVMMachineLearningModel ${INPUTDATA}/letter.scale ${TEMP}/libsvm_model.txt - ) + ) - ADD_TEST(leTuBoostMachineLearningModelNew ${LEARNING_TESTS6} + ADD_TEST(leTuBoostMachineLearningModelNew ${LEARNING_TESTS6} otbBoostMachineLearningModelNew) ADD_TEST(leTvBoostMachineLearningModel ${LEARNING_TESTS6} - #--compare-ascii ${NOTOL} - #${BASELINE_FILES}/BoostLearningModel.txt - #${TEMP}/BoostMachineLearningModel.txt otbBoostMachineLearningModel ${INPUTDATA}/letter.scale - ${TEMP}/BoostMachineLearningModel.txt + ${TEMP}/boost_model.txt ) ADD_TEST(leTuANNMachineLearningModelNew ${LEARNING_TESTS6} otbANNMachineLearningModelNew) - ADD_TEST(leTvANNMachineLearningModel ${LEARNING_TESTS6} - #--compare-ascii ${NOTOL} - #${BASELINE_FILES}/ANNMachineLearningModel.txt - #${TEMP}/ANNMachineLearningModel.txt otbANNMachineLearningModel ${INPUTDATA}/letter.scale - ${TEMP}/ANNMachineLearningModel.txt + ${TEMP}/ann_model.txt ) ADD_TEST(leTuNormalBayesMachineLearningModelNew ${LEARNING_TESTS6} otbNormalBayesMachineLearningModelNew) ADD_TEST(leTvNormalBayesMachineLearningModel ${LEARNING_TESTS6} - --compare-ascii ${NOTOL} - ${BASELINE_FILES}/NormalBayesMachineLearningModel.txt - ${TEMP}/NormalBayesMachineLearningModel.txt otbNormalBayesMachineLearningModel ${INPUTDATA}/letter.scale - ${TEMP}/NormalBayesMachineLearningModel.txt + ${TEMP}/normalbayes_model.txt ) ADD_TEST(leTuDecisionTreeMachineLearningModelNew ${LEARNING_TESTS6} otbDecisionTreeMachineLearningModelNew) ADD_TEST(leTvDecisionTreeMachineLearningModel ${LEARNING_TESTS6} - --compare-ascii ${NOTOL} - ${BASELINE_FILES}/DecisionTreeMachineLearningModel.txt - ${TEMP}/DecisionTreeMachineLearningModel.txt otbDecisionTreeMachineLearningModel ${INPUTDATA}/letter.scale - ${TEMP}/DecisionTreeMachineLearningModel.txt + ${TEMP}/decisiontree_model.txt ) ADD_TEST(leTuGradientBoostedTreeMachineLearningModelNew ${LEARNING_TESTS6} otbGradientBoostedTreeMachineLearningModelNew) ADD_TEST(leTvGradientBoostedTreeMachineLearningModel ${LEARNING_TESTS6} - #--compare-ascii ${NOTOL} - #${BASELINE_FILES}/GradientBoostedTreeMachineLearningModel.txt - #${TEMP}/GradientBoostedTreeMachineLearningModel.txt otbGradientBoostedTreeMachineLearningModel ${INPUTDATA}/letter.scale - ${TEMP}/GradientBoostedTreeMachineLearningModel.txt + ${TEMP}/gbt_model.txt ) ADD_TEST(leTuImageClassificationFilterNew ${LEARNING_TESTS6} @@ -811,48 +787,67 @@ IF(OTB_USE_OPENCV) ADD_TEST(leTuLibSVMMachineLearningModelCanRead ${LEARNING_TESTS6} otbLibSVMMachineLearningModelCanRead - ${INPUTDATA}/svm_model_image + ${TEMP}/libsvm_model.txt ) + SET_TESTS_PROPERTIES(leTuLibSVMMachineLearningModelCanRead + PROPERTIES DEPENDS leTvLibSVMMachineLearningModel) ADD_TEST(leTuSVMMachineLearningModelCanRead ${LEARNING_TESTS6} otbSVMMachineLearningModelCanRead - ${INPUTDATA}/opencv_svm_model.txt + ${TEMP}/svm_model.txt ) - + SET_TESTS_PROPERTIES(leTuSVMMachineLearningModelCanRead + PROPERTIES DEPENDS leTvSVMMachineLearningModel) + ADD_TEST(leTuRandomForestsMachineLearningModelCanRead ${LEARNING_TESTS6} otbRandomForestsMachineLearningModelCanRead - ${TEMP}/RandomForestsMachineLearningModel.txt + ${TEMP}/rf_model.txt ) SET_TESTS_PROPERTIES(leTuRandomForestsMachineLearningModelCanRead PROPERTIES DEPENDS leTvRandomForestsMachineLearningModel) ADD_TEST(leTuBoostMachineLearningModelCanRead ${LEARNING_TESTS6} otbBoostMachineLearningModelCanRead - ${INPUTDATA}/boost_model.txt + ${TEMP}/boost_model.txt ) - + SET_TESTS_PROPERTIES(leTuBoostMachineLearningModelCanRead + PROPERTIES DEPENDS leTvBoostMachineLearningModel) + ADD_TEST(leTuANNMachineLearningModelCanRead ${LEARNING_TESTS6} otbNeuralNetworkMachineLearningModelCanRead - ${INPUTDATA}/NeuralNetworkMachineLearningModel.txt + ${TEMP}/ann_model.txt ) - + SET_TESTS_PROPERTIES(leTuANNMachineLearningModelCanRead + PROPERTIES DEPENDS leTvANNMachineLearningModel) + ADD_TEST(leTuNormalBayesMachineLearningModelCanRead ${LEARNING_TESTS6} otbNormalBayesMachineLearningModelCanRead - ${INPUTDATA}/NormalBayesMachineLearningModel.txt + ${TEMP}/normalbayes_model.txt ) - + SET_TESTS_PROPERTIES(leTuNormalBayesMachineLearningModelCanRead + PROPERTIES DEPENDS leTvNormalBayesMachineLearningModel) + ADD_TEST(leTuDecisionTreeMachineLearningModelCanRead ${LEARNING_TESTS6} otbDecisionTreeMachineLearningModelCanRead - ${INPUTDATA}/DecisionTreeMachineLearningModel.txt + ${TEMP}/decisiontree_model.txt ) - + SET_TESTS_PROPERTIES(leTuDecisionTreeMachineLearningModelCanRead + PROPERTIES DEPENDS leTvDecisionTreeMachineLearningModel) + ADD_TEST(leTuGradientBoostedTreeMachineLearningModelCanRead ${LEARNING_TESTS6} otbGradientBoostedTreeMachineLearningModelCanRead - ${TEMP}/GradientBoostedTreeMachineLearningModel.txt + ${TEMP}/gbt_model.txt ) SET_TESTS_PROPERTIES(leTuGradientBoostedTreeMachineLearningModelCanRead PROPERTIES DEPENDS leTvGradientBoostedTreeMachineLearningModel) - + + ADD_TEST(leTuKNNMachineLearningModelCanRead ${LEARNING_TESTS6} + otbKNNMachineLearningModelCanRead + ${TEMP}/knn_model.txt + ) + SET_TESTS_PROPERTIES(leTuKNNMachineLearningModelCanRead + PROPERTIES DEPENDS leTvKNearestNeighborsMachineLearningModel) + ENDIF(OTB_USE_OPENCV) # Testing srcs diff --git a/Testing/Code/Learning/otbLearningTests6.cxx b/Testing/Code/Learning/otbLearningTests6.cxx index b6d1b5dd728bb66dac9311a1db3b7775f90a6828..7ee3864cab940c6cc5df5a82fa67f8cb2eb7c2cd 100644 --- a/Testing/Code/Learning/otbLearningTests6.cxx +++ b/Testing/Code/Learning/otbLearningTests6.cxx @@ -52,4 +52,5 @@ void RegisterTests() REGISTER_TEST(otbNormalBayesMachineLearningModelCanRead); REGISTER_TEST(otbDecisionTreeMachineLearningModelCanRead); REGISTER_TEST(otbGradientBoostedTreeMachineLearningModelCanRead); + REGISTER_TEST(otbKNNMachineLearningModelCanRead); } diff --git a/Testing/Code/Learning/otbMachineLearningModelCanRead.cxx b/Testing/Code/Learning/otbMachineLearningModelCanRead.cxx index 7ceee95da846d08af80171d55cd4bebd369b0bed..051d31ae385ce346af403712f5e1ef116be971c0 100644 --- a/Testing/Code/Learning/otbMachineLearningModelCanRead.cxx +++ b/Testing/Code/Learning/otbMachineLearningModelCanRead.cxx @@ -26,6 +26,7 @@ #include "otbNormalBayesMachineLearningModel.h" #include "otbDecisionTreeMachineLearningModel.h" #include "otbGradientBoostedTreeMachineLearningModel.h" +#include "otbKNearestNeighborsMachineLearningModel.h" typedef otb::MachineLearningModel<float,short> MachineLearningModelType; typedef MachineLearningModelType::InputValueType InputValueType; @@ -243,4 +244,29 @@ int otbGradientBoostedTreeMachineLearningModelCanRead(int argc, char* argv[]) return EXIT_SUCCESS; } +int otbKNNMachineLearningModelCanRead(int argc, char* argv[]) +{ + if (argc != 2) + { + std::cerr << "Usage: " << argv[0] + << "<model>" << std::endl; + std::cerr << "Called here with " << argc << " arguments\n"; + for (int i = 1; i < argc; ++i) + { + std::cerr << " - " << argv[i] << "\n"; + } + return EXIT_FAILURE; + } + std::string filename(argv[1]); + typedef otb::KNearestNeighborsMachineLearningModel<InputValueType, TargetValueType> KNNType; + KNNType::Pointer classifier = KNNType::New(); + bool lCanRead = classifier->CanReadFile(filename); + if (lCanRead == false) + { + std::cerr << "Erreur otb::KNearestNeighborsMachineLearningModel : impossible to open the file " << filename << "." << std::endl; + return EXIT_FAILURE; + } + + return EXIT_SUCCESS; +} diff --git a/Testing/Code/Learning/otbTrainMachineLearningModel.cxx b/Testing/Code/Learning/otbTrainMachineLearningModel.cxx index 69f38622f48e1c2a8f417107a8f8b4901bb653b0..be5420c279b4e6096164238afd3e2a86ab01f368 100644 --- a/Testing/Code/Learning/otbTrainMachineLearningModel.cxx +++ b/Testing/Code/Learning/otbTrainMachineLearningModel.cxx @@ -30,6 +30,7 @@ #include "otbNormalBayesMachineLearningModel.h" #include "otbDecisionTreeMachineLearningModel.h" #include "otbGradientBoostedTreeMachineLearningModel.h" +#include "otbKNearestNeighborsMachineLearningModel.h" #include "otbConfusionMatrixCalculator.h" @@ -77,7 +78,6 @@ bool ReadDataFile(const std::string & infname, InputListSampleType * samples, Ta // Parse label TargetSampleType label; label[0] = atoi(line.substr(0, pos).c_str()); - //std::cout << "label = " << label[0] << std::endl; bool endOfLine = false; unsigned int id = 0; @@ -96,11 +96,10 @@ bool ReadDataFile(const std::string & infname, InputListSampleType * samples, Ta std::string feature = line.substr(pos,nextpos-pos); std::string::size_type semicolonpos = feature.find_first_of(":"); id = atoi(feature.substr(0,semicolonpos).c_str()); - //std::cout << "id = " << id << std::endl; sample[id - 1] = atof(feature.substr(semicolonpos+1,feature.size()-semicolonpos).c_str()); - //std::cout << "sample[" << id << "] = " << sample[id] << std::endl; pos = nextpos; } + } samples->PushBack(sample); labels->PushBack(label); @@ -140,13 +139,13 @@ int otbLibSVMMachineLearningModel(int argc, char * argv[]) return EXIT_FAILURE; } - SVMType::Pointer svmclassifier = SVMType::New(); - svmclassifier->SetInputListSample(samples); - svmclassifier->SetTargetListSample(labels); - svmclassifier->Train(); + SVMType::Pointer classifier = SVMType::New(); + classifier->SetInputListSample(samples); + classifier->SetTargetListSample(labels); + classifier->Train(); - svmclassifier->SetTargetListSample(predicted); - svmclassifier->PredictAll(); + classifier->SetTargetListSample(predicted); + classifier->PredictAll(); ConfusionMatrixCalculatorType::Pointer cmCalculator = ConfusionMatrixCalculatorType::New(); @@ -156,12 +155,42 @@ int otbLibSVMMachineLearningModel(int argc, char * argv[]) std::cout << "Confusion matrix: " << std::endl; std::cout << cmCalculator->GetConfusionMatrix() << std::endl; - std::cout << "Kappa: " << cmCalculator->GetKappaIndex() << std::endl; + const float kappaIdx = cmCalculator->GetKappaIndex(); + std::cout<<"Kappa: "<<kappaIdx<<std::endl; std::cout << "Overall Accuracy: " << cmCalculator->GetOverallAccuracy() << std::endl; - svmclassifier->Save(argv[2]); + classifier->Save(argv[2]); - return EXIT_SUCCESS; + //Load Model to new LibSVM + TargetListSampleType::Pointer predictedLoad = TargetListSampleType::New(); + SVMType::Pointer classifierLoad = SVMType::New(); + + classifierLoad->Load(argv[2]); + classifierLoad->SetInputListSample(samples); + classifierLoad->SetTargetListSample(predictedLoad); + classifierLoad->PredictAll(); + + ConfusionMatrixCalculatorType::Pointer cmCalculatorLoad = ConfusionMatrixCalculatorType::New(); + + cmCalculatorLoad->SetProducedLabels(predictedLoad); + cmCalculatorLoad->SetReferenceLabels(labels); + cmCalculatorLoad->Compute(); + + std::cout<<"Confusion matrix: "<<std::endl; + std::cout<<cmCalculatorLoad->GetConfusionMatrix()<<std::endl; + const float kappaIdxLoad = cmCalculatorLoad->GetKappaIndex(); + std::cout<<"Kappa: "<<kappaIdxLoad<<std::endl; + std::cout<<"Overall Accuracy: "<<cmCalculatorLoad->GetOverallAccuracy()<<std::endl; + + + if ( vcl_abs(kappaIdxLoad - kappaIdx) < 0.00000001) + { + return EXIT_SUCCESS; + } + else + { + return EXIT_FAILURE; + } } int otbSVMMachineLearningModelNew(int argc, char * argv[]) @@ -192,15 +221,15 @@ int otbSVMMachineLearningModel(int argc, char * argv[]) return EXIT_FAILURE; } - SVMType::Pointer svmclassifier = SVMType::New(); - svmclassifier->SetInputListSample(samples); - svmclassifier->SetTargetListSample(labels); - svmclassifier->Train(); + SVMType::Pointer classifier = SVMType::New(); + classifier->SetInputListSample(samples); + classifier->SetTargetListSample(labels); + classifier->Train(); - svmclassifier->SetTargetListSample(predicted); - svmclassifier->PredictAll(); + classifier->SetTargetListSample(predicted); + classifier->PredictAll(); - svmclassifier->Save(argv[2]); + classifier->Save(argv[2]); ConfusionMatrixCalculatorType::Pointer cmCalculator = ConfusionMatrixCalculatorType::New(); @@ -210,10 +239,40 @@ int otbSVMMachineLearningModel(int argc, char * argv[]) std::cout<<"Confusion matrix: "<<std::endl; std::cout<<cmCalculator->GetConfusionMatrix()<<std::endl; - std::cout<<"Kappa: "<<cmCalculator->GetKappaIndex()<<std::endl; + const float kappaIdx = cmCalculator->GetKappaIndex(); + std::cout<<"Kappa: "<<kappaIdx<<std::endl; std::cout<<"Overall Accuracy: "<<cmCalculator->GetOverallAccuracy()<<std::endl; - return EXIT_SUCCESS; + //Load Model to new SVM + TargetListSampleType::Pointer predictedLoad = TargetListSampleType::New(); + SVMType::Pointer classifierLoad = SVMType::New(); + + classifierLoad->Load(argv[2]); + classifierLoad->SetInputListSample(samples); + classifierLoad->SetTargetListSample(predictedLoad); + classifierLoad->PredictAll(); + + ConfusionMatrixCalculatorType::Pointer cmCalculatorLoad = ConfusionMatrixCalculatorType::New(); + + cmCalculatorLoad->SetProducedLabels(predictedLoad); + cmCalculatorLoad->SetReferenceLabels(labels); + cmCalculatorLoad->Compute(); + + std::cout<<"Confusion matrix: "<<std::endl; + std::cout<<cmCalculatorLoad->GetConfusionMatrix()<<std::endl; + const float kappaIdxLoad = cmCalculatorLoad->GetKappaIndex(); + std::cout<<"Kappa: "<<kappaIdxLoad<<std::endl; + std::cout<<"Overall Accuracy: "<<cmCalculatorLoad->GetOverallAccuracy()<<std::endl; + + + if ( vcl_abs(kappaIdxLoad - kappaIdx) < 0.00000001) + { + return EXIT_SUCCESS; + } + else + { + return EXIT_FAILURE; + } } int otbKNearestNeighborsMachineLearningModelNew(int argc, char * argv[]) @@ -225,10 +284,10 @@ int otbKNearestNeighborsMachineLearningModelNew(int argc, char * argv[]) int otbKNearestNeighborsMachineLearningModel(int argc, char * argv[]) { - if (argc != 2 ) + if (argc != 3 ) { std::cout<<"Wrong number of arguments "<<std::endl; - std::cout<<"Usage : sample file"<<std::endl; + std::cout<<"Usage : sample file, output file"<<std::endl; return EXIT_FAILURE; } @@ -243,13 +302,15 @@ int otbKNearestNeighborsMachineLearningModel(int argc, char * argv[]) return EXIT_FAILURE; } - KNearestNeighborsType::Pointer knnclassifier = KNearestNeighborsType::New(); - knnclassifier->SetInputListSample(samples); - knnclassifier->SetTargetListSample(labels); - knnclassifier->Train(); + KNearestNeighborsType::Pointer classifier = KNearestNeighborsType::New(); + classifier->SetInputListSample(samples); + classifier->SetTargetListSample(labels); + classifier->Train(); + //write the model + classifier->Save(argv[2]); - knnclassifier->SetTargetListSample(predicted); - knnclassifier->Superclass::PredictAll(); + classifier->SetTargetListSample(predicted); + classifier->PredictAll(); ConfusionMatrixCalculatorType::Pointer cmCalculator = ConfusionMatrixCalculatorType::New(); @@ -259,13 +320,41 @@ int otbKNearestNeighborsMachineLearningModel(int argc, char * argv[]) std::cout<<"Confusion matrix: "<<std::endl; std::cout<<cmCalculator->GetConfusionMatrix()<<std::endl; - std::cout<<"Kappa: "<<cmCalculator->GetKappaIndex()<<std::endl; + const float kappaIdx = cmCalculator->GetKappaIndex(); + std::cout<<"Kappa: "<<kappaIdx<<std::endl; std::cout<<"Overall Accuracy: "<<cmCalculator->GetOverallAccuracy()<<std::endl; - //write the model - //knnclassifier->Save(argv[2]); - return EXIT_SUCCESS; + //Load Model to new KNN + TargetListSampleType::Pointer predictedLoad = TargetListSampleType::New(); + KNearestNeighborsType::Pointer classifierLoad = KNearestNeighborsType::New(); + + classifierLoad->Load(argv[2]); + classifierLoad->SetInputListSample(samples); + classifierLoad->SetTargetListSample(predictedLoad); + classifierLoad->PredictAll(); + + ConfusionMatrixCalculatorType::Pointer cmCalculatorLoad = ConfusionMatrixCalculatorType::New(); + + cmCalculatorLoad->SetProducedLabels(predictedLoad); + cmCalculatorLoad->SetReferenceLabels(labels); + cmCalculatorLoad->Compute(); + + std::cout<<"Confusion matrix: "<<std::endl; + std::cout<<cmCalculatorLoad->GetConfusionMatrix()<<std::endl; + const float kappaIdxLoad = cmCalculatorLoad->GetKappaIndex(); + std::cout<<"Kappa: "<<kappaIdxLoad<<std::endl; + std::cout<<"Overall Accuracy: "<<cmCalculatorLoad->GetOverallAccuracy()<<std::endl; + + + if ( vcl_abs(kappaIdxLoad - kappaIdx) < 0.00000001) + { + return EXIT_SUCCESS; + } + else + { + return EXIT_FAILURE; + } } int otbRandomForestsMachineLearningModelNew(int argc, char * argv[]) @@ -298,26 +387,25 @@ int otbRandomForestsMachineLearningModel(int argc, char * argv[]) std::vector<float> priors(26,1.); - RandomForestType::Pointer rfclassifier = RandomForestType::New(); - rfclassifier->SetInputListSample(samples); - rfclassifier->SetTargetListSample(labels); + RandomForestType::Pointer classifier = RandomForestType::New(); + classifier->SetInputListSample(samples); + classifier->SetTargetListSample(labels); //set parameters - rfclassifier->SetPriors(priors); - // rfclassifier->SetMaxNumberOfTrees(30); - // rfclassifier->SetMaxDepth(30); - // rfclassifier->SetMaxNumberOfCategories(30); - // rfclassifier->SetMaxNumberOfVariables(4); + classifier->SetPriors(priors); + // classifier->SetMaxNumberOfTrees(30); + // classifier->SetMaxDepth(30); + // classifier->SetMaxNumberOfCategories(30); + // classifier->SetMaxNumberOfVariables(4); - rfclassifier->Train(); + classifier->Train(); + classifier->Save(argv[2]); - rfclassifier->SetTargetListSample(predicted); - rfclassifier->PredictAll(); + classifier->SetTargetListSample(predicted); + classifier->PredictAll(); ConfusionMatrixCalculatorType::Pointer cmCalculator = ConfusionMatrixCalculatorType::New(); - std::cout<<rfclassifier->GetTargetListSample()->Size()<<std::endl; - cmCalculator->SetProducedLabels(predicted); cmCalculator->SetReferenceLabels(labels); cmCalculator->Compute(); @@ -328,18 +416,14 @@ int otbRandomForestsMachineLearningModel(int argc, char * argv[]) std::cout<<"Kappa: "<<kappaIdx<<std::endl; std::cout<<"Overall Accuracy: "<<cmCalculator->GetOverallAccuracy()<<std::endl; - rfclassifier->Save(argv[2]); - //std::cout<<"GetTrainError: "<<rfclassifier->GetTrainError() << std::endl; - - //Load Model to new RF TargetListSampleType::Pointer predictedLoad = TargetListSampleType::New(); - RandomForestType::Pointer rfclassifierLoad = RandomForestType::New(); + RandomForestType::Pointer classifierLoad = RandomForestType::New(); - rfclassifierLoad->Load(argv[2]); - rfclassifierLoad->SetInputListSample(samples); - rfclassifierLoad->SetTargetListSample(predictedLoad); - rfclassifierLoad->PredictAll(); + classifierLoad->Load(argv[2]); + classifierLoad->SetInputListSample(samples); + classifierLoad->SetTargetListSample(predictedLoad); + classifierLoad->PredictAll(); ConfusionMatrixCalculatorType::Pointer cmCalculatorLoad = ConfusionMatrixCalculatorType::New(); @@ -410,10 +494,40 @@ int otbBoostMachineLearningModel(int argc, char * argv[]) std::cout<<"Confusion matrix: "<<std::endl; std::cout<<cmCalculator->GetConfusionMatrix()<<std::endl; - std::cout<<"Kappa: "<<cmCalculator->GetKappaIndex()<<std::endl; + const float kappaIdx = cmCalculator->GetKappaIndex(); + std::cout<<"Kappa: "<<kappaIdx<<std::endl; std::cout<<"Overall Accuracy: "<<cmCalculator->GetOverallAccuracy()<<std::endl; - return EXIT_SUCCESS; + //Load Model to new Boost model + TargetListSampleType::Pointer predictedLoad = TargetListSampleType::New(); + BoostType::Pointer classifierLoad = BoostType::New(); + + classifierLoad->Load(argv[2]); + classifierLoad->SetInputListSample(samples); + classifierLoad->SetTargetListSample(predictedLoad); + classifierLoad->PredictAll(); + + ConfusionMatrixCalculatorType::Pointer cmCalculatorLoad = ConfusionMatrixCalculatorType::New(); + + cmCalculatorLoad->SetProducedLabels(predictedLoad); + cmCalculatorLoad->SetReferenceLabels(labels); + cmCalculatorLoad->Compute(); + + std::cout<<"Confusion matrix: "<<std::endl; + std::cout<<cmCalculatorLoad->GetConfusionMatrix()<<std::endl; + const float kappaIdxLoad = cmCalculatorLoad->GetKappaIndex(); + std::cout<<"Kappa: "<<kappaIdxLoad<<std::endl; + std::cout<<"Overall Accuracy: "<<cmCalculatorLoad->GetOverallAccuracy()<<std::endl; + + + if ( vcl_abs(kappaIdxLoad - kappaIdx) < 0.00000001) + { + return EXIT_SUCCESS; + } + else + { + return EXIT_FAILURE; + } } int otbANNMachineLearningModelNew(int argc, char * argv[]) @@ -473,12 +587,42 @@ int otbANNMachineLearningModel(int argc, char * argv[]) std::cout << "Confusion matrix: " << std::endl; std::cout << cmCalculator->GetConfusionMatrix() << std::endl; - std::cout << "Kappa: " << cmCalculator->GetKappaIndex() << std::endl; + const float kappaIdx = cmCalculator->GetKappaIndex(); + std::cout<<"Kappa: "<<kappaIdx<<std::endl; std::cout << "Overall Accuracy: " << cmCalculator->GetOverallAccuracy() << std::endl; classifier->Save(argv[2]); - return EXIT_SUCCESS; + //Load Model to new ANN + TargetListSampleType::Pointer predictedLoad = TargetListSampleType::New(); + ANNType::Pointer classifierLoad = ANNType::New(); + + classifierLoad->Load(argv[2]); + classifierLoad->SetInputListSample(samples); + classifierLoad->SetTargetListSample(predictedLoad); + classifierLoad->PredictAll(); + + ConfusionMatrixCalculatorType::Pointer cmCalculatorLoad = ConfusionMatrixCalculatorType::New(); + + cmCalculatorLoad->SetProducedLabels(predictedLoad); + cmCalculatorLoad->SetReferenceLabels(labels); + cmCalculatorLoad->Compute(); + + std::cout<<"Confusion matrix: "<<std::endl; + std::cout<<cmCalculatorLoad->GetConfusionMatrix()<<std::endl; + const float kappaIdxLoad = cmCalculatorLoad->GetKappaIndex(); + std::cout<<"Kappa: "<<kappaIdxLoad<<std::endl; + std::cout<<"Overall Accuracy: "<<cmCalculatorLoad->GetOverallAccuracy()<<std::endl; + + + if ( vcl_abs(kappaIdxLoad - kappaIdx) < 0.00000001) + { + return EXIT_SUCCESS; + } + else + { + return EXIT_FAILURE; + } } int otbNormalBayesMachineLearningModelNew(int argc, char * argv[]) @@ -527,10 +671,40 @@ int otbNormalBayesMachineLearningModel(int argc, char * argv[]) std::cout<<"Confusion matrix: "<<std::endl; std::cout<<cmCalculator->GetConfusionMatrix()<<std::endl; - std::cout<<"Kappa: "<<cmCalculator->GetKappaIndex()<<std::endl; + const float kappaIdx = cmCalculator->GetKappaIndex(); + std::cout<<"Kappa: "<<kappaIdx<<std::endl; std::cout<<"Overall Accuracy: "<<cmCalculator->GetOverallAccuracy()<<std::endl; - return EXIT_SUCCESS; + //Load Model to new Normal Bayes + TargetListSampleType::Pointer predictedLoad = TargetListSampleType::New(); + NormalBayesType::Pointer classifierLoad = NormalBayesType::New(); + + classifierLoad->Load(argv[2]); + classifierLoad->SetInputListSample(samples); + classifierLoad->SetTargetListSample(predictedLoad); + classifierLoad->PredictAll(); + + ConfusionMatrixCalculatorType::Pointer cmCalculatorLoad = ConfusionMatrixCalculatorType::New(); + + cmCalculatorLoad->SetProducedLabels(predictedLoad); + cmCalculatorLoad->SetReferenceLabels(labels); + cmCalculatorLoad->Compute(); + + std::cout<<"Confusion matrix: "<<std::endl; + std::cout<<cmCalculatorLoad->GetConfusionMatrix()<<std::endl; + const float kappaIdxLoad = cmCalculatorLoad->GetKappaIndex(); + std::cout<<"Kappa: "<<kappaIdxLoad<<std::endl; + std::cout<<"Overall Accuracy: "<<cmCalculatorLoad->GetOverallAccuracy()<<std::endl; + + + if ( vcl_abs(kappaIdxLoad - kappaIdx) < 0.00000001) + { + return EXIT_SUCCESS; + } + else + { + return EXIT_FAILURE; + } } int otbDecisionTreeMachineLearningModelNew(int argc, char * argv[]) @@ -579,10 +753,40 @@ int otbDecisionTreeMachineLearningModel(int argc, char * argv[]) std::cout<<"Confusion matrix: "<<std::endl; std::cout<<cmCalculator->GetConfusionMatrix()<<std::endl; - std::cout<<"Kappa: "<<cmCalculator->GetKappaIndex()<<std::endl; + const float kappaIdx = cmCalculator->GetKappaIndex(); + std::cout<<"Kappa: "<<kappaIdx<<std::endl; std::cout<<"Overall Accuracy: "<<cmCalculator->GetOverallAccuracy()<<std::endl; - return EXIT_SUCCESS; + //Load Model to new Decision Tree + TargetListSampleType::Pointer predictedLoad = TargetListSampleType::New(); + DecisionTreeType::Pointer classifierLoad = DecisionTreeType::New(); + + classifierLoad->Load(argv[2]); + classifierLoad->SetInputListSample(samples); + classifierLoad->SetTargetListSample(predictedLoad); + classifierLoad->PredictAll(); + + ConfusionMatrixCalculatorType::Pointer cmCalculatorLoad = ConfusionMatrixCalculatorType::New(); + + cmCalculatorLoad->SetProducedLabels(predictedLoad); + cmCalculatorLoad->SetReferenceLabels(labels); + cmCalculatorLoad->Compute(); + + std::cout<<"Confusion matrix: "<<std::endl; + std::cout<<cmCalculatorLoad->GetConfusionMatrix()<<std::endl; + const float kappaIdxLoad = cmCalculatorLoad->GetKappaIndex(); + std::cout<<"Kappa: "<<kappaIdxLoad<<std::endl; + std::cout<<"Overall Accuracy: "<<cmCalculatorLoad->GetOverallAccuracy()<<std::endl; + + + if ( vcl_abs(kappaIdxLoad - kappaIdx) < 0.00000001) + { + return EXIT_SUCCESS; + } + else + { + return EXIT_FAILURE; + } } @@ -632,12 +836,40 @@ int otbGradientBoostedTreeMachineLearningModel(int argc, char * argv[]) std::cout<<"Confusion matrix: "<<std::endl; std::cout<<cmCalculator->GetConfusionMatrix()<<std::endl; - std::cout<<"Kappa: "<<cmCalculator->GetKappaIndex()<<std::endl; + const float kappaIdx = cmCalculator->GetKappaIndex(); + std::cout<<"Kappa: "<<kappaIdx<<std::endl; std::cout<<"Overall Accuracy: "<<cmCalculator->GetOverallAccuracy()<<std::endl; - return EXIT_SUCCESS; -} + //Load Model to new GBT + TargetListSampleType::Pointer predictedLoad = TargetListSampleType::New(); + GBTreeType::Pointer classifierLoad = GBTreeType::New(); + + classifierLoad->Load(argv[2]); + classifierLoad->SetInputListSample(samples); + classifierLoad->SetTargetListSample(predictedLoad); + classifierLoad->PredictAll(); + + ConfusionMatrixCalculatorType::Pointer cmCalculatorLoad = ConfusionMatrixCalculatorType::New(); + cmCalculatorLoad->SetProducedLabels(predictedLoad); + cmCalculatorLoad->SetReferenceLabels(labels); + cmCalculatorLoad->Compute(); + std::cout<<"Confusion matrix: "<<std::endl; + std::cout<<cmCalculatorLoad->GetConfusionMatrix()<<std::endl; + const float kappaIdxLoad = cmCalculatorLoad->GetKappaIndex(); + std::cout<<"Kappa: "<<kappaIdxLoad<<std::endl; + std::cout<<"Overall Accuracy: "<<cmCalculatorLoad->GetOverallAccuracy()<<std::endl; + + + if ( vcl_abs(kappaIdxLoad - kappaIdx) < 0.00000001) + { + return EXIT_SUCCESS; + } + else + { + return EXIT_FAILURE; + } +}