From 7d2472718404bdf4e3ca79069363cb7e6813b6de Mon Sep 17 00:00:00 2001 From: Alexander Alekhin Date: Fri, 1 Mar 2019 13:47:45 +0300 Subject: [PATCH] ml: handle sigmoid NaN result (should be Inf) - added more debug checks --- modules/ml/src/svm.cpp | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/modules/ml/src/svm.cpp b/modules/ml/src/svm.cpp index 4d987eb678..4a1ffd0575 100644 --- a/modules/ml/src/svm.cpp +++ b/modules/ml/src/svm.cpp @@ -205,11 +205,14 @@ public: for( j = 0; j < vcount; j++ ) { Qfloat t = results[j]; - Qfloat e = std::exp(std::abs(t)); - if( t > 0 ) - results[j] = (Qfloat)((e - 1.)/(e + 1.)); - else - results[j] = (Qfloat)((1. - e)/(1. + e)); + Qfloat e = std::exp(std::abs(t)); // Inf value is possible here + Qfloat r = (Qfloat)((e - 1.) / (e + 1.)); // NaN value is possible here (Inf/Inf or similar) + if (cvIsNaN(r)) + r = std::numeric_limits::infinity(); + if (t < 0) + r = -r; + CV_DbgAssert(!cvIsNaN(r)); + results[j] = r; } } @@ -327,7 +330,7 @@ public: const Qfloat max_val = (Qfloat)(FLT_MAX*1e-3); for( int j = 0; j < vcount; j++ ) { - if( results[j] > max_val ) + if (!(results[j] <= max_val)) // handle NaNs too results[j] = max_val; } } @@ -1949,6 +1952,7 @@ public: const DecisionFunc& df = svm->decision_func[dfi]; sum = -df.rho; int sv_count = svm->getSVCount(dfi); + CV_DbgAssert(sv_count > 0); const double* alpha = &svm->df_alpha[df.ofs]; const int* sv_index = &svm->df_index[df.ofs]; for( k = 0; k < sv_count; k++ )