Update to 2.0.0 tree from current Fremantle build
[opencv] / tests / ml / src / slmltests.cpp
diff --git a/tests/ml/src/slmltests.cpp b/tests/ml/src/slmltests.cpp
new file mode 100644 (file)
index 0000000..95bf80a
--- /dev/null
@@ -0,0 +1,131 @@
+/*M///////////////////////////////////////////////////////////////////////////////////////\r
+//\r
+//  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.\r
+//\r
+//  By downloading, copying, installing or using the software you agree to this license.\r
+//  If you do not agree to this license, do not download, install,\r
+//  copy or use the software.\r
+//\r
+//\r
+//                        Intel License Agreement\r
+//                For Open Source Computer Vision Library\r
+//\r
+// Copyright (C) 2000, Intel Corporation, all rights reserved.\r
+// Third party copyrights are property of their respective owners.\r
+//\r
+// Redistribution and use in source and binary forms, with or without modification,\r
+// are permitted provided that the following conditions are met:\r
+//\r
+//   * Redistribution's of source code must retain the above copyright notice,\r
+//     this list of conditions and the following disclaimer.\r
+//\r
+//   * Redistribution's in binary form must reproduce the above copyright notice,\r
+//     this list of conditions and the following disclaimer in the documentation\r
+//     and/or other materials provided with the distribution.\r
+//\r
+//   * The name of Intel Corporation may not be used to endorse or promote products\r
+//     derived from this software without specific prior written permission.\r
+//\r
+// This software is provided by the copyright holders and contributors "as is" and\r
+// any express or implied warranties, including, but not limited to, the implied\r
+// warranties of merchantability and fitness for a particular purpose are disclaimed.\r
+// In no event shall the Intel Corporation or contributors be liable for any direct,\r
+// indirect, incidental, special, exemplary, or consequential damages\r
+// (including, but not limited to, procurement of substitute goods or services;\r
+// loss of use, data, or profits; or business interruption) however caused\r
+// and on any theory of liability, whether in contract, strict liability,\r
+// or tort (including negligence or otherwise) arising in any way out of\r
+// the use of this software, even if advised of the possibility of such damage.\r
+//\r
+//M*/\r
+\r
+#include "mltest.h"\r
+#include <iostream>\r
+#include <fstream>\r
+\r
+CV_SLMLTest::CV_SLMLTest( const char* _modelName, const char* _testName ) :\r
+    CV_MLBaseTest( _modelName, _testName, "load-save" )\r
+{\r
+    validationFN = "slvalidation.xml";\r
+}\r
+\r
+int CV_SLMLTest::run_test_case( int testCaseIdx )\r
+{\r
+    int code = CvTS::OK;
+    code = prepare_test_case( testCaseIdx );\r
+\r
+    if( code == CvTS::OK )\r
+    {\r
+            data.mix_train_and_test_idx();\r
+            code = train( testCaseIdx );\r
+            if( code == CvTS::OK )\r
+            {\r
+                get_error( testCaseIdx, CV_TEST_ERROR, &test_resps1 );\r
+                save( tmpnam( fname1 ) );\r
+                load( fname1);\r
+                get_error( testCaseIdx, CV_TEST_ERROR, &test_resps2 );\r
+                save( tmpnam( fname2 ) );\r
+            }\r
+            else\r
+                ts->printf( CvTS::LOG, "model can not be trained" );\r
+    }\r
+    return code;\r
+}\r
+\r
+int CV_SLMLTest::validate_test_results( int testCaseIdx )\r
+{\r
+    int code = CvTS::OK;\r
+\r
+    // 1. compare files\r
+    ifstream f1( fname1 ), f2( fname2 );\r
+    string s1, s2;\r
+    int lineIdx = 0; \r
+    CV_Assert( f1.is_open() && f2.is_open() );\r
+    for( ; !f1.eof() && !f2.eof(); lineIdx++ )\r
+    {\r
+        getline( f1, s1 );\r
+        getline( f2, s2 );\r
+        if( s1.compare(s2) )\r
+        {\r
+            ts->printf( CvTS::LOG, "first and second saved files differ in %n-line; first %n line: %s; second %n-line: %s",
+               lineIdx, lineIdx, s1.c_str(), lineIdx, s2.c_str() );
+            code = CvTS::FAIL_INVALID_OUTPUT;\r
+        }\r
+    }\r
+    if( !f1.eof() || !f2.eof() )\r
+    {\r
+        ts->printf( CvTS::LOG, "in test case %d first and second saved files differ in %n-line; first %n line: %s; second %n-line: %s",
+            testCaseIdx, lineIdx, lineIdx, s1.c_str(), lineIdx, s2.c_str() );
+        code = CvTS::FAIL_INVALID_OUTPUT;\r
+    }\r
+    f1.close();\r
+    f2.close();\r
+    // delete temporary files\r
+    unlink( fname1 );\r
+    unlink( fname2 );\r
+\r
+    // 2. compare responses\r
+    CV_Assert( test_resps1.size() == test_resps2.size() );\r
+    vector<float>::const_iterator it1 = test_resps1.begin(), it2 = test_resps2.begin();\r
+    for( ; it1 != test_resps1.end(); it1++, it2++ )\r
+    {\r
+        if( fabs(*it1 - *it2) > FLT_EPSILON )\r
+        {\r
+            ts->printf( CvTS::LOG, "in test case %d responses predicted before saving and after loading is different", testCaseIdx );
+            code = CvTS::FAIL_INVALID_OUTPUT;\r
+        }\r
+    }\r
+    return code;\r
+}\r
+\r
+CV_SLMLTest lsmlnbayes( CV_NBAYES, "slnbayes" );
+//CV_SLMLTest lsmlknearest( CV_KNEAREST, "slknearest" ); // does not support save!
+CV_SLMLTest lsmlsvm( CV_SVM, "slsvm" );
+//CV_SLMLTest lsmlem( CV_EM, "slem" ); // does not support save!
+CV_SLMLTest lsmlann( CV_ANN, "slann" );
+CV_SLMLTest slmldtree( CV_DTREE, "sldtree" );
+CV_SLMLTest slmlboost( CV_BOOST, "slboost" );
+CV_SLMLTest slmlrtrees( CV_RTREES, "slrtrees" );
+CV_SLMLTest slmlertrees( CV_ERTREES, "slertrees" );
+
+/* End of file. */\r