From 898734b9b06d96ad70bde12847459846a281b88f Mon Sep 17 00:00:00 2001
From: Guillaume Pasero <guillaume.pasero@c-s.fr>
Date: Thu, 20 Apr 2017 10:41:03 +0200
Subject: [PATCH] BUG: svm_predict_probabilities() and svm_predict() don't give
 the same labels

---
 .../include/otbLibSVMMachineLearningModel.txx      | 14 +++++++++++++-
 1 file changed, 13 insertions(+), 1 deletion(-)

diff --git a/Modules/Learning/Supervised/include/otbLibSVMMachineLearningModel.txx b/Modules/Learning/Supervised/include/otbLibSVMMachineLearningModel.txx
index 10a8938706..2b14f6dca9 100644
--- a/Modules/Learning/Supervised/include/otbLibSVMMachineLearningModel.txx
+++ b/Modules/Learning/Supervised/include/otbLibSVMMachineLearningModel.txx
@@ -183,7 +183,19 @@ LibSVMMachineLearningModel<TInputValue,TOutputValue>
     }
   else
     {
-    target[0] = static_cast<TargetValueType>(svm_predict(m_Model, x));
+    // default case : if the model has probabilities, we call svm_predict_probabilities()
+    // which gives different results than svm_predict()
+    if (svm_check_probability_model(m_Model))
+      {
+      unsigned int nr_class = svm_get_nr_class(m_Model);
+      double *prob_estimates = new double[nr_class];
+      target[0] = static_cast<TargetValueType>(svm_predict_probability(m_Model, x, prob_estimates));
+      delete[] prob_estimates;
+      }
+    else
+      {
+      target[0] = static_cast<TargetValueType>(svm_predict(m_Model, x));
+      }
     }
 
   // Free allocated memory
-- 
GitLab