mirror of
https://github.com/opencv/opencv.git
synced 2026-09-15 07:29:07 -05:00
ts: refactor OpenCV tests
- removed tr1 usage (dropped in C++17) - moved includes of vector/map/iostream/limits into ts.hpp - require opencv_test + anonymous namespace (added compile check) - fixed norm() usage (must be from cvtest::norm for checks) and other conflict functions - added missing license headers
This commit is contained in:
@@ -41,13 +41,12 @@
|
||||
|
||||
#include "test_precomp.hpp"
|
||||
|
||||
using namespace std;
|
||||
using namespace cv;
|
||||
namespace opencv_test { namespace {
|
||||
|
||||
using cv::ml::TrainData;
|
||||
using cv::ml::EM;
|
||||
using cv::ml::KNearest;
|
||||
|
||||
static
|
||||
void defaultDistribs( Mat& means, vector<Mat>& covs, int type=CV_32FC1 )
|
||||
{
|
||||
CV_TRACE_FUNCTION();
|
||||
@@ -74,7 +73,6 @@ void defaultDistribs( Mat& means, vector<Mat>& covs, int type=CV_32FC1 )
|
||||
}
|
||||
|
||||
// generate points sets by normal distributions
|
||||
static
|
||||
void generateData( Mat& data, Mat& labels, const vector<int>& sizes, const Mat& _means, const vector<Mat>& covs, int dataType, int labelType )
|
||||
{
|
||||
CV_TRACE_FUNCTION();
|
||||
@@ -117,7 +115,6 @@ void generateData( Mat& data, Mat& labels, const vector<int>& sizes, const Mat&
|
||||
}
|
||||
}
|
||||
|
||||
static
|
||||
int maxIdx( const vector<int>& count )
|
||||
{
|
||||
int idx = -1;
|
||||
@@ -135,7 +132,6 @@ int maxIdx( const vector<int>& count )
|
||||
return idx;
|
||||
}
|
||||
|
||||
static
|
||||
bool getLabelsMap( const Mat& labels, const vector<int>& sizes, vector<int>& labelsMap, bool checkClusterUniq=true )
|
||||
{
|
||||
size_t total = 0, nclusters = sizes.size();
|
||||
@@ -182,7 +178,6 @@ bool getLabelsMap( const Mat& labels, const vector<int>& sizes, vector<int>& lab
|
||||
return true;
|
||||
}
|
||||
|
||||
static
|
||||
bool calcErr( const Mat& labels, const Mat& origLabels, const vector<int>& sizes, float& err, bool labelsEquivalent = true, bool checkClusterUniq=true )
|
||||
{
|
||||
err = 0;
|
||||
@@ -706,3 +701,5 @@ TEST(ML_KNearest, accuracy) { CV_KNearestTest test; test.safe_run(); }
|
||||
TEST(ML_EM, accuracy) { CV_EMTest test; test.safe_run(); }
|
||||
TEST(ML_EM, save_load) { CV_EMTest_SaveLoad test; test.safe_run(); }
|
||||
TEST(ML_EM, classification) { CV_EMTest_Classification test; test.safe_run(); }
|
||||
|
||||
}} // namespace
|
||||
|
||||
@@ -3,10 +3,6 @@
|
||||
|
||||
#if 0
|
||||
|
||||
#include <string>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
|
||||
using namespace std;
|
||||
|
||||
|
||||
|
||||
@@ -58,11 +58,9 @@
|
||||
|
||||
#include "test_precomp.hpp"
|
||||
|
||||
using namespace std;
|
||||
using namespace cv;
|
||||
using namespace cv::ml;
|
||||
namespace opencv_test { namespace {
|
||||
|
||||
static bool calculateError( const Mat& _p_labels, const Mat& _o_labels, float& error)
|
||||
bool calculateError( const Mat& _p_labels, const Mat& _o_labels, float& error)
|
||||
{
|
||||
CV_TRACE_FUNCTION();
|
||||
error = 0.0f;
|
||||
@@ -226,3 +224,5 @@ void CV_LRTest_SaveLoad::run( int /*start_from*/ )
|
||||
|
||||
TEST(ML_LR, accuracy) { CV_LRTest test; test.safe_run(); }
|
||||
TEST(ML_LR, save_load) { CV_LRTest_SaveLoad test; test.safe_run(); }
|
||||
|
||||
}} // namespace
|
||||
|
||||
@@ -1,3 +1,6 @@
|
||||
// This file is part of OpenCV project.
|
||||
// It is subject to the license terms in the LICENSE file found in the top-level directory
|
||||
// of this distribution and at http://opencv.org/license.html.
|
||||
#include "test_precomp.hpp"
|
||||
|
||||
CV_TEST_MAIN("ml")
|
||||
|
||||
@@ -41,8 +41,7 @@
|
||||
|
||||
#include "test_precomp.hpp"
|
||||
|
||||
using namespace cv;
|
||||
using namespace std;
|
||||
namespace opencv_test {
|
||||
|
||||
CV_AMLTest::CV_AMLTest( const char* _modelName ) : CV_MLBaseTest( _modelName )
|
||||
{
|
||||
@@ -125,6 +124,8 @@ int CV_AMLTest::validate_test_results( int testCaseIdx )
|
||||
return cvtest::TS::OK;
|
||||
}
|
||||
|
||||
namespace {
|
||||
|
||||
TEST(ML_DTree, regression) { CV_AMLTest test( CV_DTREE ); test.safe_run(); }
|
||||
TEST(ML_Boost, regression) { CV_AMLTest test( CV_BOOST ); test.safe_run(); }
|
||||
TEST(ML_RTrees, regression) { CV_AMLTest test( CV_RTREES ); test.safe_run(); }
|
||||
@@ -219,4 +220,5 @@ TEST(ML_RTrees, getVotes)
|
||||
EXPECT_EQ(result.at<float>(0, predicted_class), rt->predict(test));
|
||||
}
|
||||
|
||||
}} // namespace
|
||||
/* End of file. */
|
||||
|
||||
@@ -43,8 +43,7 @@
|
||||
|
||||
//#define GENERATE_TESTDATA
|
||||
|
||||
using namespace cv;
|
||||
using namespace std;
|
||||
namespace opencv_test { namespace {
|
||||
|
||||
int str_to_svm_type(String& str)
|
||||
{
|
||||
@@ -89,6 +88,7 @@ int str_to_ann_train_method( String& str )
|
||||
return -1;
|
||||
}
|
||||
|
||||
#if 0
|
||||
int str_to_ann_activation_function(String& str)
|
||||
{
|
||||
if (!str.compare("IDENTITY"))
|
||||
@@ -104,6 +104,7 @@ int str_to_ann_activation_function(String& str)
|
||||
CV_Error(CV_StsBadArg, "incorrect ann activation function string");
|
||||
return -1;
|
||||
}
|
||||
#endif
|
||||
|
||||
void ann_check_data( Ptr<TrainData> _data )
|
||||
{
|
||||
@@ -373,6 +374,8 @@ int str_to_margin_type( String& str )
|
||||
CV_Error( CV_StsBadArg, "incorrect svmsgd margin type string" );
|
||||
return -1;
|
||||
}
|
||||
|
||||
}
|
||||
// ---------------------------------- MLBaseTest ---------------------------------------------------
|
||||
|
||||
CV_MLBaseTest::CV_MLBaseTest(const char* _modelName)
|
||||
@@ -700,4 +703,5 @@ void CV_MLBaseTest::load( const char* filename )
|
||||
CV_Error( CV_StsNotImplemented, "invalid stat model name");
|
||||
}
|
||||
|
||||
} // namespace
|
||||
/* End of file. */
|
||||
|
||||
@@ -1,20 +1,13 @@
|
||||
#ifdef __GNUC__
|
||||
# pragma GCC diagnostic ignored "-Wmissing-declarations"
|
||||
# if defined __clang__ || defined __APPLE__
|
||||
# pragma GCC diagnostic ignored "-Wmissing-prototypes"
|
||||
# pragma GCC diagnostic ignored "-Wextra"
|
||||
# endif
|
||||
#endif
|
||||
|
||||
#ifndef __OPENCV_TEST_PRECOMP_HPP__
|
||||
#define __OPENCV_TEST_PRECOMP_HPP__
|
||||
|
||||
#include <iostream>
|
||||
#include <map>
|
||||
#include "opencv2/ts.hpp"
|
||||
#include "opencv2/ml.hpp"
|
||||
#include "opencv2/core/core_c.h"
|
||||
|
||||
namespace opencv_test {
|
||||
using namespace cv::ml;
|
||||
|
||||
#define CV_NBAYES "nbayes"
|
||||
#define CV_KNEAREST "knearest"
|
||||
#define CV_SVM "svm"
|
||||
@@ -94,4 +87,6 @@ protected:
|
||||
std::string fname1, fname2;
|
||||
};
|
||||
|
||||
} // namespace
|
||||
|
||||
#endif
|
||||
|
||||
@@ -41,11 +41,7 @@
|
||||
|
||||
#include "test_precomp.hpp"
|
||||
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
|
||||
using namespace cv;
|
||||
using namespace std;
|
||||
namespace opencv_test {
|
||||
|
||||
CV_SLMLTest::CV_SLMLTest( const char* _modelName ) : CV_MLBaseTest( _modelName )
|
||||
{
|
||||
@@ -148,6 +144,8 @@ int CV_SLMLTest::validate_test_results( int testCaseIdx )
|
||||
return code;
|
||||
}
|
||||
|
||||
namespace {
|
||||
|
||||
TEST(ML_NaiveBayes, save_load) { CV_SLMLTest test( CV_NBAYES ); test.safe_run(); }
|
||||
TEST(ML_KNearest, save_load) { CV_SLMLTest test( CV_KNEAREST ); test.safe_run(); }
|
||||
TEST(ML_SVM, save_load) { CV_SLMLTest test( CV_SVM ); test.safe_run(); }
|
||||
@@ -295,10 +293,11 @@ TEST(DISABLED_ML_SVM, linear_save_load)
|
||||
svm3->predict(samples, r3);
|
||||
|
||||
double eps = 1e-4;
|
||||
EXPECT_LE(norm(r1, r2, NORM_INF), eps);
|
||||
EXPECT_LE(norm(r1, r3, NORM_INF), eps);
|
||||
EXPECT_LE(cvtest::norm(r1, r2, NORM_INF), eps);
|
||||
EXPECT_LE(cvtest::norm(r1, r3, NORM_INF), eps);
|
||||
|
||||
remove(tname.c_str());
|
||||
}
|
||||
|
||||
}} // namespace
|
||||
/* End of file. */
|
||||
|
||||
@@ -40,15 +40,12 @@
|
||||
//M*/
|
||||
|
||||
#include "test_precomp.hpp"
|
||||
#include "opencv2/highgui.hpp"
|
||||
|
||||
using namespace cv;
|
||||
using namespace cv::ml;
|
||||
namespace opencv_test { namespace {
|
||||
|
||||
using cv::ml::SVMSGD;
|
||||
using cv::ml::TrainData;
|
||||
|
||||
|
||||
|
||||
class CV_SVMSGDTrainTest : public cvtest::BaseTest
|
||||
{
|
||||
public:
|
||||
@@ -300,7 +297,7 @@ TEST(ML_SVMSGD, twoPoints)
|
||||
|
||||
float realShift = -500000.5;
|
||||
|
||||
float normRealWeights = static_cast<float>(norm(realWeights));
|
||||
float normRealWeights = static_cast<float>(cv::norm(realWeights)); // TODO cvtest
|
||||
realWeights /= normRealWeights;
|
||||
realShift /= normRealWeights;
|
||||
|
||||
@@ -311,8 +308,11 @@ TEST(ML_SVMSGD, twoPoints)
|
||||
Mat foundWeights = svmsgd->getWeights();
|
||||
float foundShift = svmsgd->getShift();
|
||||
|
||||
float normFoundWeights = static_cast<float>(norm(foundWeights));
|
||||
float normFoundWeights = static_cast<float>(cv::norm(foundWeights)); // TODO cvtest
|
||||
foundWeights /= normFoundWeights;
|
||||
foundShift /= normFoundWeights;
|
||||
CV_Assert((norm(foundWeights - realWeights) < 0.001) && (abs((foundShift - realShift) / realShift) < 0.05));
|
||||
EXPECT_LE(cv::norm(Mat(foundWeights - realWeights)), 0.001); // TODO cvtest
|
||||
EXPECT_LE(std::abs((foundShift - realShift) / realShift), 0.05);
|
||||
}
|
||||
|
||||
}} // namespace
|
||||
|
||||
@@ -41,8 +41,8 @@
|
||||
|
||||
#include "test_precomp.hpp"
|
||||
|
||||
using namespace cv;
|
||||
using namespace std;
|
||||
namespace opencv_test { namespace {
|
||||
|
||||
using cv::ml::SVM;
|
||||
using cv::ml::TrainData;
|
||||
|
||||
@@ -166,3 +166,5 @@ void CV_SVMGetSupportVectorsTest::run(int /*startFrom*/ )
|
||||
|
||||
|
||||
TEST(ML_SVM, getSupportVectors) { CV_SVMGetSupportVectorsTest test; test.safe_run(); }
|
||||
|
||||
}} // namespace
|
||||
|
||||
Reference in New Issue
Block a user