From 315c054379610c044cdc6b67d62935f678994de4 Mon Sep 17 00:00:00 2001 From: peng xiao Date: Tue, 17 Sep 2013 08:50:13 +0800 Subject: [PATCH] Fix new blank line at EOF. --- modules/ocl/test/test_ml.cpp | 178 ++++++++++++++++++++++++++++++++++- 1 file changed, 177 insertions(+), 1 deletion(-) diff --git a/modules/ocl/test/test_ml.cpp b/modules/ocl/test/test_ml.cpp index 834fc4e37..af86d35a6 100644 --- a/modules/ocl/test/test_ml.cpp +++ b/modules/ocl/test/test_ml.cpp @@ -121,4 +121,180 @@ TEST_P(KNN, Accuracy) } INSTANTIATE_TEST_CASE_P(OCL_ML, KNN, Combine(Values(6, 5), Values(Size(200, 400), Size(300, 600)), Values(4, 3), Values(false, true))); -#endif // HAVE_OPENCL \ No newline at end of file + +////////////////////////////////SVM///////////////////////////////////////////////// +PARAM_TEST_CASE(SVM_OCL, int, int, int) +{ + cv::Size size; + int kernel_type; + int svm_type; + Mat src, labels, samples, labels_predict; + int K; + cv::RNG rng ; + + virtual void SetUp() + { + + kernel_type = GET_PARAM(0); + svm_type = GET_PARAM(1); + K = GET_PARAM(2); + rng = TS::ptr()->get_rng(); + cv::Size size = cv::Size(MWIDTH, MHEIGHT); + src.create(size, CV_32FC1); + labels.create(1, size.height, CV_32SC1); + int row_idx = 0; + const int max_number = size.height / K - 1; + CV_Assert(K <= size.height); + for(int i = 0; i < K; i++ ) + { + Mat center_row_header = src.row(row_idx); + center_row_header.setTo(0); + int nchannel = center_row_header.channels(); + for(int j = 0; j < nchannel; j++) + { + center_row_header.at(0, i * nchannel + j) = 500.0; + } + labels.at(0, row_idx) = i; + for(int j = 0; (j < max_number) || + (i == K - 1 && j < max_number + size.height % K); j ++) + { + Mat cur_row_header = src.row(row_idx + 1 + j); + center_row_header.copyTo(cur_row_header); + Mat tmpmat = randomMat(rng, cur_row_header.size(), cur_row_header.type(), 1, 100, false); + cur_row_header += tmpmat; + labels.at(0, row_idx + 1 + j) = i; + } + row_idx += 1 + max_number; + } + labels.convertTo(labels, CV_32FC1); + cv::Size test_size = cv::Size(MWIDTH, 100); + samples.create(test_size, CV_32FC1); + labels_predict.create(1, test_size.height, CV_32SC1); + const int max_number_test = test_size.height / K - 1; + row_idx = 0; + for(int i = 0; i < K; i++ ) + { + Mat center_row_header = samples.row(row_idx); + center_row_header.setTo(0); + int nchannel = center_row_header.channels(); + for(int j = 0; j < nchannel; j++) + { + center_row_header.at(0, i * nchannel + j) = 500.0; + } + labels_predict.at(0, row_idx) = i; + for(int j = 0; (j < max_number_test) || + (i == K - 1 && j < max_number_test + test_size.height % K); j ++) + { + Mat cur_row_header = samples.row(row_idx + 1 + j); + center_row_header.copyTo(cur_row_header); + Mat tmpmat = randomMat(rng, cur_row_header.size(), cur_row_header.type(), 1, 100, false); + cur_row_header += tmpmat; + labels_predict.at(0, row_idx + 1 + j) = i; + } + row_idx += 1 + max_number_test; + } + labels_predict.convertTo(labels_predict, CV_32FC1); + } +}; +TEST_P(SVM_OCL, Accuracy) +{ + CvSVMParams params; + params.degree = 0.4; + params.gamma = 1; + params.coef0 = 1; + params.C = 1; + params.nu = 0.5; + params.p = 1; + params.svm_type = svm_type; + params.kernel_type = kernel_type; + + params.term_crit = cvTermCriteria(CV_TERMCRIT_ITER, 1000, 0.001); + + CvSVM SVM; + SVM.train(src, labels, Mat(), Mat(), params); + + cv::ocl::CvSVM_OCL SVM_OCL; + SVM_OCL.train(src, labels, Mat(), Mat(), params); + + int c = SVM.get_support_vector_count(); + int c1 = SVM_OCL.get_support_vector_count(); + + Mat sv(c, MHEIGHT, CV_32FC1); + Mat sv_ocl(c1, MHEIGHT, CV_32FC1); + for(int i = 0; i < c; i++) + { + const float* v = SVM.get_support_vector(i); + + for(int j = 0; j < MHEIGHT; j++) + { + sv.at(i, j) = v[j]; + } + } + for(int i = 0; i < c1; i++) + { + const float* v_ocl = SVM_OCL.get_support_vector(i); + + for(int j = 0; j < MHEIGHT; j++) + { + sv_ocl.at(i, j) = v_ocl[j]; + } + } + cv::BFMatcher matcher(cv::NORM_L2); + std::vector matches; + matcher.match(sv, sv_ocl, matches); + int count = 0; + + for(std::vector::iterator itr = matches.begin(); itr != matches.end(); itr++) + { + if((*itr).distance < 0.1) + { + count ++; + } + } + if(c != 0) + { + float matchedRatio = (float)count / c; + EXPECT_GT(matchedRatio, 0.95); + } + if(c != 0) + { + CvMat *result = cvCreateMat(1, samples.rows, CV_32FC1); + CvMat test_samples = samples; + + CvMat *result_ocl = cvCreateMat(1, samples.rows, CV_32FC1); + + SVM.predict(&test_samples, result); + + SVM_OCL.predict(&test_samples, result_ocl); + + int true_resp = 0, true_resp_ocl = 0; + for (int i = 0; i < samples.rows; i++) + { + if (result->data.fl[i] == labels_predict.at(0, i)) + { + true_resp++; + } + } + float matchedRatio = (float)true_resp / samples.rows; + + for (int i = 0; i < samples.rows; i++) + { + if (result_ocl->data.fl[i] == labels_predict.at(0, i)) + { + true_resp_ocl++; + } + } + float matchedRatio_ocl = (float)true_resp_ocl / samples.rows; + + if(matchedRatio != 0 && true_resp_ocl < true_resp) + { + EXPECT_NEAR(matchedRatio_ocl, matchedRatio, 0.03); + } + } +} +INSTANTIATE_TEST_CASE_P(OCL_ML, SVM_OCL, testing::Combine( + Values(CvSVM::LINEAR, CvSVM::POLY, CvSVM::RBF, CvSVM::SIGMOID), + Values(CvSVM::C_SVC, CvSVM::NU_SVC, CvSVM::ONE_CLASS, CvSVM::EPS_SVR, CvSVM::NU_SVR), + Values(2, 3, 4) + )); +#endif // HAVE_OPENCL