catching OpenCL double not supported exceptions
[profile/ivi/opencv.git] / modules / ocl / test / test_kmeans.cpp
1 /*M///////////////////////////////////////////////////////////////////////////////////////
2 //
3 //  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
4 //
5 //  By downloading, copying, installing or using the software you agree to this license.
6 //  If you do not agree to this license, do not download, install,
7 //  copy or use the software.
8 //
9 //
10 //                           License Agreement
11 //                For Open Source Computer Vision Library
12 //
13 // Copyright (C) 2010-2012, Multicoreware, Inc., all rights reserved.
14 // Copyright (C) 2010-2012, Advanced Micro Devices, Inc., all rights reserved.
15 // Third party copyrights are property of their respective owners.
16 //
17 // @Authors
18 //    Erping Pang,   pang_er_ping@163.com
19 //    Xiaopeng Fu,   fuxiaopeng2222@163.com
20 //
21 // Redistribution and use in source and binary forms, with or without modification,
22 // are permitted provided that the following conditions are met:
23 //
24 //   * Redistribution's of source code must retain the above copyright notice,
25 //     this list of conditions and the following disclaimer.
26 //
27 //   * Redistribution's in binary form must reproduce the above copyright notice,
28 //     this list of conditions and the following disclaimer in the documentation
29 //     and/or other oclMaterials provided with the distribution.
30 //
31 //   * The name of the copyright holders may not be used to endorse or promote products
32 //     derived from this software without specific prior written permission.
33 //
34 // This software is provided by the copyright holders and contributors "as is" and
35 // any express or implied warranties, including, but not limited to, the implied
36 // warranties of merchantability and fitness for a particular purpose are disclaimed.
37 // In no event shall the Intel Corporation or contributors be liable for any direct,
38 // indirect, incidental, special, exemplary, or consequential damages
39 // (including, but not limited to, procurement of substitute goods or services;
40 // loss of use, data, or profits; or business interruption) however caused
41 // and on any theory of liability, whether in contract, strict liability,
42 // or tort (including negligence or otherwise) arising in any way out of
43 // the use of this software, even if advised of the possibility of such damage.
44 //
45 //M*/
46
47 #include "test_precomp.hpp"
48
49 #ifdef HAVE_OPENCL
50
51 using namespace cvtest;
52 using namespace testing;
53 using namespace std;
54 using namespace cv;
55
56 #define OCL_KMEANS_USE_INITIAL_LABELS 1
57 #define OCL_KMEANS_PP_CENTERS         2
58
59 PARAM_TEST_CASE(Kmeans, int, int, int)
60 {
61     int type;
62     int K;
63     int flags;
64     cv::Mat src ;
65     ocl::oclMat d_src, d_dists;
66
67     Mat labels, centers;
68     ocl::oclMat d_labels, d_centers;
69     virtual void SetUp()
70     {
71         K = GET_PARAM(0);
72         type = GET_PARAM(1);
73         flags = GET_PARAM(2);
74
75         // MWIDTH=256, MHEIGHT=256. defined in utility.hpp
76         cv::Size size = cv::Size(MWIDTH, MHEIGHT);
77         src.create(size, type);
78         int row_idx = 0;
79         const int max_neighbour = MHEIGHT / K - 1;
80         CV_Assert(K <= MWIDTH);
81         for(int i = 0; i < K; i++ )
82         {
83             Mat center_row_header = src.row(row_idx);
84             center_row_header.setTo(0);
85             int nchannel = center_row_header.channels();
86             for(int j = 0; j < nchannel; j++)
87                 center_row_header.at<float>(0, i*nchannel+j) = 50000.0;
88
89             for(int j = 0; (j < max_neighbour) ||
90                            (i == K-1 && j < max_neighbour + MHEIGHT%K); j ++)
91             {
92                 Mat cur_row_header = src.row(row_idx + 1 + j);
93                 center_row_header.copyTo(cur_row_header);
94                 Mat tmpmat = randomMat(cur_row_header.size(), cur_row_header.type(), -200, 200, false);
95                 cur_row_header += tmpmat;
96             }
97             row_idx += 1 + max_neighbour;
98         }
99     }
100 };
101 OCL_TEST_P(Kmeans, Mat){
102
103     if(flags & KMEANS_USE_INITIAL_LABELS)
104     {
105         // inital a given labels
106         labels.create(src.rows, 1, CV_32S);
107         int *label = labels.ptr<int>();
108         for(int i = 0; i < src.rows; i++)
109             label[i] = rng.uniform(0, K);
110         d_labels.upload(labels);
111     }
112     d_src.upload(src);
113
114     for(int j = 0; j < LOOP_TIMES; j++)
115     {
116         kmeans(src, K, labels,
117             TermCriteria( CV_TERMCRIT_EPS+CV_TERMCRIT_ITER, 100, 0),
118             1, flags, centers);
119
120         ocl::kmeans(d_src, K, d_labels,
121             TermCriteria( CV_TERMCRIT_EPS+CV_TERMCRIT_ITER, 100, 0),
122             1, flags, d_centers);
123
124         Mat dd_labels(d_labels);
125         Mat dd_centers(d_centers);
126         if(flags & KMEANS_USE_INITIAL_LABELS)
127         {
128             EXPECT_MAT_NEAR(labels, dd_labels, 0);
129             EXPECT_MAT_NEAR(centers, dd_centers, 1e-3);
130         }
131         else
132         {
133             int row_idx = 0;
134             for(int i = 0; i < K; i++)
135             {
136                 // verify lables with ground truth resutls
137                 int label = labels.at<int>(row_idx);
138                 int header_label = dd_labels.at<int>(row_idx);
139                 for(int j = 0; (j < MHEIGHT/K)||(i == K-1 && j < MHEIGHT/K+MHEIGHT%K); j++)
140                 {
141                     ASSERT_NEAR(labels.at<int>(row_idx+j), label, 0);
142                     ASSERT_NEAR(dd_labels.at<int>(row_idx+j), header_label, 0);
143                 }
144
145                 // verify centers
146                 float *center = centers.ptr<float>(label);
147                 float *header_center = dd_centers.ptr<float>(header_label);
148                 for(int t = 0; t < centers.cols; t++)
149                     ASSERT_NEAR(center[t], header_center[t], 1e-3);
150
151                 row_idx += MHEIGHT/K;
152             }
153         }
154     }
155 }
156 INSTANTIATE_TEST_CASE_P(OCL_ML, Kmeans, Combine(
157     Values(3, 5, 8),
158     Values(CV_32FC1, CV_32FC2, CV_32FC4),
159     Values(OCL_KMEANS_USE_INITIAL_LABELS/*, OCL_KMEANS_PP_CENTERS*/)));
160
161 #endif