File: | ccv_scd.c |
Warning: | line 1607, column 24 Array access (via field 'sx') results in a null pointer dereference |
Press '?' to see keyboard shortcuts
Keyboard shortcuts:
1 | #include "ccv.h" | |||
2 | #include "ccv_internal.h" | |||
3 | #if defined(HAVE_SSE21) | |||
4 | #include <xmmintrin.h> | |||
5 | #elif defined(HAVE_NEON) | |||
6 | #include <arm_neon.h> | |||
7 | #endif | |||
8 | #ifdef HAVE_GSL1 | |||
9 | #include <gsl/gsl_rng.h> | |||
10 | #include <gsl/gsl_randist.h> | |||
11 | #endif | |||
12 | #ifdef USE_OPENMP | |||
13 | #include <omp.h> | |||
14 | #endif | |||
15 | #ifdef USE_DISPATCH | |||
16 | #include <dispatch/dispatch.h> | |||
17 | #endif | |||
18 | #include "3rdparty/sqlite3/sqlite3.h" | |||
19 | ||||
20 | const ccv_scd_param_t ccv_scd_default_params = { | |||
21 | .interval = 5, | |||
22 | .min_neighbors = 1, | |||
23 | .step_through = 4, | |||
24 | .size = { | |||
25 | .width = 48, | |||
26 | .height = 48, | |||
27 | }, | |||
28 | }; | |||
29 | ||||
30 | #define CCV_SCD_CHANNEL(11) (11) | |||
31 | ||||
32 | // this uses a look up table for cubic root computation because rgb to luv only requires data within range of 0~1 | |||
33 | static inline float fast_cube_root(const float d) | |||
34 | { | |||
35 | static const float cube_root[2048] = { | |||
36 | 0.000000e+00, 7.875788e-02, 9.922871e-02, 1.135885e-01, 1.250203e-01, 1.346741e-01, 1.431126e-01, 1.506584e-01, | |||
37 | 1.575158e-01, 1.638230e-01, 1.696787e-01, 1.751560e-01, 1.803105e-01, 1.851861e-01, 1.898177e-01, 1.942336e-01, | |||
38 | 1.984574e-01, 2.025087e-01, 2.064040e-01, 2.101577e-01, 2.137818e-01, 2.172870e-01, 2.206827e-01, 2.239769e-01, | |||
39 | 2.271770e-01, 2.302894e-01, 2.333199e-01, 2.362736e-01, 2.391553e-01, 2.419692e-01, 2.447191e-01, 2.474085e-01, | |||
40 | 2.500407e-01, 2.526186e-01, 2.551450e-01, 2.576222e-01, 2.600528e-01, 2.624387e-01, 2.647821e-01, 2.670846e-01, | |||
41 | 2.693482e-01, 2.715743e-01, 2.737645e-01, 2.759202e-01, 2.780428e-01, 2.801334e-01, 2.821933e-01, 2.842235e-01, | |||
42 | 2.862251e-01, 2.881992e-01, 2.901465e-01, 2.920681e-01, 2.939647e-01, 2.958371e-01, 2.976862e-01, 2.995125e-01, | |||
43 | 3.013168e-01, 3.030998e-01, 3.048621e-01, 3.066041e-01, 3.083267e-01, 3.100302e-01, 3.117152e-01, 3.133821e-01, | |||
44 | 3.150315e-01, 3.166639e-01, 3.182795e-01, 3.198789e-01, 3.214625e-01, 3.230307e-01, 3.245837e-01, 3.261220e-01, | |||
45 | 3.276460e-01, 3.291559e-01, 3.306521e-01, 3.321348e-01, 3.336045e-01, 3.350613e-01, 3.365056e-01, 3.379375e-01, | |||
46 | 3.393574e-01, 3.407656e-01, 3.421622e-01, 3.435475e-01, 3.449216e-01, 3.462850e-01, 3.476377e-01, 3.489799e-01, | |||
47 | 3.503119e-01, 3.516339e-01, 3.529460e-01, 3.542483e-01, 3.555412e-01, 3.568248e-01, 3.580992e-01, 3.593646e-01, | |||
48 | 3.606211e-01, 3.618689e-01, 3.631082e-01, 3.643391e-01, 3.655617e-01, 3.667762e-01, 3.679827e-01, 3.691814e-01, | |||
49 | 3.703723e-01, 3.715556e-01, 3.727314e-01, 3.738999e-01, 3.750610e-01, 3.762151e-01, 3.773621e-01, 3.785022e-01, | |||
50 | 3.796354e-01, 3.807619e-01, 3.818818e-01, 3.829952e-01, 3.841021e-01, 3.852027e-01, 3.862970e-01, 3.873852e-01, | |||
51 | 3.884673e-01, 3.895434e-01, 3.906136e-01, 3.916779e-01, 3.927365e-01, 3.937894e-01, 3.948367e-01, 3.958785e-01, | |||
52 | 3.969149e-01, 3.979458e-01, 3.989714e-01, 3.999918e-01, 4.010071e-01, 4.020171e-01, 4.030222e-01, 4.040223e-01, | |||
53 | 4.050174e-01, 4.060076e-01, 4.069931e-01, 4.079738e-01, 4.089499e-01, 4.099212e-01, 4.108880e-01, 4.118503e-01, | |||
54 | 4.128081e-01, 4.137615e-01, 4.147105e-01, 4.156551e-01, 4.165955e-01, 4.175317e-01, 4.184637e-01, 4.193916e-01, | |||
55 | 4.203153e-01, 4.212351e-01, 4.221508e-01, 4.230626e-01, 4.239704e-01, 4.248744e-01, 4.257746e-01, 4.266710e-01, | |||
56 | 4.275636e-01, 4.284525e-01, 4.293377e-01, 4.302193e-01, 4.310973e-01, 4.319718e-01, 4.328427e-01, 4.337101e-01, | |||
57 | 4.345741e-01, 4.354346e-01, 4.362918e-01, 4.371456e-01, 4.379961e-01, 4.388433e-01, 4.396872e-01, 4.405279e-01, | |||
58 | 4.413654e-01, 4.421997e-01, 4.430309e-01, 4.438590e-01, 4.446840e-01, 4.455060e-01, 4.463249e-01, 4.471409e-01, | |||
59 | 4.479539e-01, 4.487639e-01, 4.495711e-01, 4.503753e-01, 4.511767e-01, 4.519752e-01, 4.527710e-01, 4.535639e-01, | |||
60 | 4.543541e-01, 4.551415e-01, 4.559263e-01, 4.567083e-01, 4.574877e-01, 4.582644e-01, 4.590385e-01, 4.598100e-01, | |||
61 | 4.605789e-01, 4.613453e-01, 4.621091e-01, 4.628704e-01, 4.636292e-01, 4.643855e-01, 4.651394e-01, 4.658908e-01, | |||
62 | 4.666398e-01, 4.673865e-01, 4.681307e-01, 4.688726e-01, 4.696122e-01, 4.703494e-01, 4.710843e-01, 4.718169e-01, | |||
63 | 4.725473e-01, 4.732754e-01, 4.740013e-01, 4.747250e-01, 4.754464e-01, 4.761657e-01, 4.768828e-01, 4.775978e-01, | |||
64 | 4.783106e-01, 4.790214e-01, 4.797300e-01, 4.804365e-01, 4.811410e-01, 4.818434e-01, 4.825437e-01, 4.832420e-01, | |||
65 | 4.839384e-01, 4.846327e-01, 4.853250e-01, 4.860154e-01, 4.867038e-01, 4.873902e-01, 4.880748e-01, 4.887574e-01, | |||
66 | 4.894381e-01, 4.901170e-01, 4.907939e-01, 4.914690e-01, 4.921423e-01, 4.928137e-01, 4.934832e-01, 4.941510e-01, | |||
67 | 4.948170e-01, 4.954812e-01, 4.961436e-01, 4.968042e-01, 4.974631e-01, 4.981203e-01, 4.987757e-01, 4.994294e-01, | |||
68 | 5.000814e-01, 5.007317e-01, 5.013803e-01, 5.020273e-01, 5.026726e-01, 5.033162e-01, 5.039582e-01, 5.045985e-01, | |||
69 | 5.052372e-01, 5.058743e-01, 5.065099e-01, 5.071438e-01, 5.077761e-01, 5.084069e-01, 5.090362e-01, 5.096638e-01, | |||
70 | 5.102900e-01, 5.109145e-01, 5.115376e-01, 5.121592e-01, 5.127792e-01, 5.133978e-01, 5.140148e-01, 5.146304e-01, | |||
71 | 5.152445e-01, 5.158572e-01, 5.164684e-01, 5.170782e-01, 5.176865e-01, 5.182934e-01, 5.188988e-01, 5.195029e-01, | |||
72 | 5.201056e-01, 5.207069e-01, 5.213068e-01, 5.219053e-01, 5.225024e-01, 5.230982e-01, 5.236927e-01, 5.242857e-01, | |||
73 | 5.248775e-01, 5.254679e-01, 5.260570e-01, 5.266448e-01, 5.272312e-01, 5.278164e-01, 5.284002e-01, 5.289828e-01, | |||
74 | 5.295641e-01, 5.301442e-01, 5.307229e-01, 5.313004e-01, 5.318767e-01, 5.324517e-01, 5.330254e-01, 5.335979e-01, | |||
75 | 5.341693e-01, 5.347394e-01, 5.353082e-01, 5.358759e-01, 5.364423e-01, 5.370076e-01, 5.375717e-01, 5.381346e-01, | |||
76 | 5.386963e-01, 5.392569e-01, 5.398163e-01, 5.403746e-01, 5.409316e-01, 5.414876e-01, 5.420424e-01, 5.425960e-01, | |||
77 | 5.431486e-01, 5.437000e-01, 5.442503e-01, 5.447995e-01, 5.453476e-01, 5.458946e-01, 5.464405e-01, 5.469853e-01, | |||
78 | 5.475290e-01, 5.480717e-01, 5.486133e-01, 5.491537e-01, 5.496932e-01, 5.502316e-01, 5.507689e-01, 5.513052e-01, | |||
79 | 5.518404e-01, 5.523747e-01, 5.529078e-01, 5.534400e-01, 5.539711e-01, 5.545012e-01, 5.550303e-01, 5.555584e-01, | |||
80 | 5.560855e-01, 5.566117e-01, 5.571368e-01, 5.576609e-01, 5.581840e-01, 5.587062e-01, 5.592273e-01, 5.597475e-01, | |||
81 | 5.602668e-01, 5.607851e-01, 5.613024e-01, 5.618188e-01, 5.623342e-01, 5.628487e-01, 5.633622e-01, 5.638748e-01, | |||
82 | 5.643865e-01, 5.648973e-01, 5.654072e-01, 5.659161e-01, 5.664241e-01, 5.669311e-01, 5.674374e-01, 5.679426e-01, | |||
83 | 5.684470e-01, 5.689505e-01, 5.694531e-01, 5.699549e-01, 5.704557e-01, 5.709556e-01, 5.714548e-01, 5.719529e-01, | |||
84 | 5.724503e-01, 5.729468e-01, 5.734424e-01, 5.739372e-01, 5.744311e-01, 5.749242e-01, 5.754164e-01, 5.759078e-01, | |||
85 | 5.763984e-01, 5.768881e-01, 5.773770e-01, 5.778650e-01, 5.783523e-01, 5.788387e-01, 5.793243e-01, 5.798091e-01, | |||
86 | 5.802931e-01, 5.807762e-01, 5.812586e-01, 5.817402e-01, 5.822210e-01, 5.827010e-01, 5.831801e-01, 5.836585e-01, | |||
87 | 5.841362e-01, 5.846130e-01, 5.850891e-01, 5.855644e-01, 5.860389e-01, 5.865127e-01, 5.869856e-01, 5.874579e-01, | |||
88 | 5.879294e-01, 5.884001e-01, 5.888700e-01, 5.893393e-01, 5.898077e-01, 5.902755e-01, 5.907425e-01, 5.912087e-01, | |||
89 | 5.916742e-01, 5.921390e-01, 5.926031e-01, 5.930664e-01, 5.935290e-01, 5.939909e-01, 5.944521e-01, 5.949125e-01, | |||
90 | 5.953723e-01, 5.958313e-01, 5.962896e-01, 5.967473e-01, 5.972042e-01, 5.976604e-01, 5.981160e-01, 5.985708e-01, | |||
91 | 5.990250e-01, 5.994784e-01, 5.999312e-01, 6.003833e-01, 6.008347e-01, 6.012855e-01, 6.017355e-01, 6.021850e-01, | |||
92 | 6.026337e-01, 6.030817e-01, 6.035291e-01, 6.039758e-01, 6.044219e-01, 6.048673e-01, 6.053120e-01, 6.057562e-01, | |||
93 | 6.061996e-01, 6.066424e-01, 6.070846e-01, 6.075261e-01, 6.079670e-01, 6.084072e-01, 6.088468e-01, 6.092858e-01, | |||
94 | 6.097241e-01, 6.101618e-01, 6.105989e-01, 6.110353e-01, 6.114712e-01, 6.119064e-01, 6.123410e-01, 6.127750e-01, | |||
95 | 6.132084e-01, 6.136411e-01, 6.140732e-01, 6.145048e-01, 6.149357e-01, 6.153660e-01, 6.157957e-01, 6.162249e-01, | |||
96 | 6.166534e-01, 6.170813e-01, 6.175086e-01, 6.179354e-01, 6.183616e-01, 6.187872e-01, 6.192122e-01, 6.196365e-01, | |||
97 | 6.200604e-01, 6.204836e-01, 6.209063e-01, 6.213284e-01, 6.217499e-01, 6.221709e-01, 6.225913e-01, 6.230111e-01, | |||
98 | 6.234304e-01, 6.238490e-01, 6.242672e-01, 6.246848e-01, 6.251017e-01, 6.255182e-01, 6.259341e-01, 6.263494e-01, | |||
99 | 6.267643e-01, 6.271785e-01, 6.275922e-01, 6.280054e-01, 6.284180e-01, 6.288301e-01, 6.292416e-01, 6.296526e-01, | |||
100 | 6.300631e-01, 6.304730e-01, 6.308824e-01, 6.312913e-01, 6.316996e-01, 6.321074e-01, 6.325147e-01, 6.329215e-01, | |||
101 | 6.333277e-01, 6.337335e-01, 6.341386e-01, 6.345433e-01, 6.349475e-01, 6.353511e-01, 6.357543e-01, 6.361569e-01, | |||
102 | 6.365590e-01, 6.369606e-01, 6.373618e-01, 6.377624e-01, 6.381625e-01, 6.385621e-01, 6.389612e-01, 6.393598e-01, | |||
103 | 6.397579e-01, 6.401555e-01, 6.405526e-01, 6.409492e-01, 6.413454e-01, 6.417410e-01, 6.421362e-01, 6.425309e-01, | |||
104 | 6.429250e-01, 6.433188e-01, 6.437120e-01, 6.441047e-01, 6.444970e-01, 6.448888e-01, 6.452801e-01, 6.456710e-01, | |||
105 | 6.460613e-01, 6.464512e-01, 6.468406e-01, 6.472296e-01, 6.476181e-01, 6.480061e-01, 6.483937e-01, 6.487808e-01, | |||
106 | 6.491674e-01, 6.495536e-01, 6.499393e-01, 6.503246e-01, 6.507094e-01, 6.510937e-01, 6.514776e-01, 6.518611e-01, | |||
107 | 6.522441e-01, 6.526266e-01, 6.530087e-01, 6.533904e-01, 6.537716e-01, 6.541524e-01, 6.545327e-01, 6.549126e-01, | |||
108 | 6.552920e-01, 6.556710e-01, 6.560495e-01, 6.564277e-01, 6.568054e-01, 6.571826e-01, 6.575595e-01, 6.579359e-01, | |||
109 | 6.583118e-01, 6.586874e-01, 6.590625e-01, 6.594372e-01, 6.598114e-01, 6.601852e-01, 6.605586e-01, 6.609316e-01, | |||
110 | 6.613042e-01, 6.616763e-01, 6.620481e-01, 6.624194e-01, 6.627903e-01, 6.631607e-01, 6.635308e-01, 6.639005e-01, | |||
111 | 6.642697e-01, 6.646385e-01, 6.650070e-01, 6.653750e-01, 6.657426e-01, 6.661098e-01, 6.664766e-01, 6.668430e-01, | |||
112 | 6.672090e-01, 6.675746e-01, 6.679398e-01, 6.683046e-01, 6.686690e-01, 6.690330e-01, 6.693966e-01, 6.697598e-01, | |||
113 | 6.701226e-01, 6.704850e-01, 6.708471e-01, 6.712087e-01, 6.715700e-01, 6.719308e-01, 6.722913e-01, 6.726514e-01, | |||
114 | 6.730111e-01, 6.733705e-01, 6.737294e-01, 6.740879e-01, 6.744461e-01, 6.748039e-01, 6.751614e-01, 6.755184e-01, | |||
115 | 6.758750e-01, 6.762313e-01, 6.765872e-01, 6.769428e-01, 6.772979e-01, 6.776527e-01, 6.780071e-01, 6.783612e-01, | |||
116 | 6.787149e-01, 6.790682e-01, 6.794212e-01, 6.797737e-01, 6.801260e-01, 6.804778e-01, 6.808293e-01, 6.811804e-01, | |||
117 | 6.815312e-01, 6.818815e-01, 6.822316e-01, 6.825813e-01, 6.829306e-01, 6.832796e-01, 6.836282e-01, 6.839765e-01, | |||
118 | 6.843244e-01, 6.846719e-01, 6.850191e-01, 6.853660e-01, 6.857125e-01, 6.860586e-01, 6.864043e-01, 6.867498e-01, | |||
119 | 6.870949e-01, 6.874397e-01, 6.877841e-01, 6.881282e-01, 6.884719e-01, 6.888152e-01, 6.891583e-01, 6.895010e-01, | |||
120 | 6.898433e-01, 6.901854e-01, 6.905270e-01, 6.908684e-01, 6.912094e-01, 6.915500e-01, 6.918904e-01, 6.922303e-01, | |||
121 | 6.925700e-01, 6.929094e-01, 6.932484e-01, 6.935870e-01, 6.939254e-01, 6.942633e-01, 6.946011e-01, 6.949384e-01, | |||
122 | 6.952754e-01, 6.956121e-01, 6.959485e-01, 6.962845e-01, 6.966202e-01, 6.969556e-01, 6.972907e-01, 6.976255e-01, | |||
123 | 6.979599e-01, 6.982940e-01, 6.986278e-01, 6.989613e-01, 6.992944e-01, 6.996273e-01, 6.999598e-01, 7.002920e-01, | |||
124 | 7.006239e-01, 7.009555e-01, 7.012867e-01, 7.016177e-01, 7.019483e-01, 7.022786e-01, 7.026086e-01, 7.029384e-01, | |||
125 | 7.032678e-01, 7.035969e-01, 7.039256e-01, 7.042542e-01, 7.045823e-01, 7.049102e-01, 7.052377e-01, 7.055650e-01, | |||
126 | 7.058919e-01, 7.062186e-01, 7.065449e-01, 7.068710e-01, 7.071967e-01, 7.075222e-01, 7.078474e-01, 7.081722e-01, | |||
127 | 7.084967e-01, 7.088210e-01, 7.091449e-01, 7.094686e-01, 7.097920e-01, 7.101150e-01, 7.104378e-01, 7.107603e-01, | |||
128 | 7.110825e-01, 7.114044e-01, 7.117260e-01, 7.120473e-01, 7.123684e-01, 7.126891e-01, 7.130095e-01, 7.133297e-01, | |||
129 | 7.136496e-01, 7.139692e-01, 7.142885e-01, 7.146075e-01, 7.149262e-01, 7.152447e-01, 7.155629e-01, 7.158808e-01, | |||
130 | 7.161984e-01, 7.165157e-01, 7.168328e-01, 7.171495e-01, 7.174660e-01, 7.177821e-01, 7.180981e-01, 7.184138e-01, | |||
131 | 7.187291e-01, 7.190442e-01, 7.193590e-01, 7.196736e-01, 7.199879e-01, 7.203019e-01, 7.206156e-01, 7.209290e-01, | |||
132 | 7.212422e-01, 7.215551e-01, 7.218677e-01, 7.221801e-01, 7.224922e-01, 7.228040e-01, 7.231156e-01, 7.234268e-01, | |||
133 | 7.237378e-01, 7.240486e-01, 7.243591e-01, 7.246693e-01, 7.249793e-01, 7.252890e-01, 7.255983e-01, 7.259076e-01, | |||
134 | 7.262164e-01, 7.265251e-01, 7.268335e-01, 7.271415e-01, 7.274494e-01, 7.277570e-01, 7.280643e-01, 7.283714e-01, | |||
135 | 7.286782e-01, 7.289847e-01, 7.292911e-01, 7.295971e-01, 7.299029e-01, 7.302084e-01, 7.305137e-01, 7.308187e-01, | |||
136 | 7.311234e-01, 7.314279e-01, 7.317322e-01, 7.320362e-01, 7.323400e-01, 7.326434e-01, 7.329467e-01, 7.332497e-01, | |||
137 | 7.335525e-01, 7.338549e-01, 7.341572e-01, 7.344592e-01, 7.347609e-01, 7.350624e-01, 7.353637e-01, 7.356647e-01, | |||
138 | 7.359655e-01, 7.362660e-01, 7.365662e-01, 7.368662e-01, 7.371660e-01, 7.374656e-01, 7.377649e-01, 7.380639e-01, | |||
139 | 7.383628e-01, 7.386613e-01, 7.389597e-01, 7.392578e-01, 7.395556e-01, 7.398532e-01, 7.401506e-01, 7.404477e-01, | |||
140 | 7.407446e-01, 7.410412e-01, 7.413377e-01, 7.416338e-01, 7.419298e-01, 7.422255e-01, 7.425209e-01, 7.428162e-01, | |||
141 | 7.431112e-01, 7.434059e-01, 7.437005e-01, 7.439948e-01, 7.442889e-01, 7.445827e-01, 7.448763e-01, 7.451697e-01, | |||
142 | 7.454628e-01, 7.457558e-01, 7.460485e-01, 7.463409e-01, 7.466331e-01, 7.469251e-01, 7.472169e-01, 7.475084e-01, | |||
143 | 7.477998e-01, 7.480908e-01, 7.483817e-01, 7.486723e-01, 7.489627e-01, 7.492529e-01, 7.495428e-01, 7.498326e-01, | |||
144 | 7.501221e-01, 7.504114e-01, 7.507005e-01, 7.509893e-01, 7.512779e-01, 7.515663e-01, 7.518545e-01, 7.521424e-01, | |||
145 | 7.524302e-01, 7.527177e-01, 7.530050e-01, 7.532921e-01, 7.535789e-01, 7.538656e-01, 7.541520e-01, 7.544382e-01, | |||
146 | 7.547241e-01, 7.550099e-01, 7.552955e-01, 7.555808e-01, 7.558660e-01, 7.561509e-01, 7.564356e-01, 7.567201e-01, | |||
147 | 7.570043e-01, 7.572884e-01, 7.575722e-01, 7.578558e-01, 7.581393e-01, 7.584225e-01, 7.587055e-01, 7.589883e-01, | |||
148 | 7.592708e-01, 7.595532e-01, 7.598354e-01, 7.601173e-01, 7.603990e-01, 7.606806e-01, 7.609619e-01, 7.612430e-01, | |||
149 | 7.615239e-01, 7.618046e-01, 7.620851e-01, 7.623653e-01, 7.626454e-01, 7.629253e-01, 7.632049e-01, 7.634844e-01, | |||
150 | 7.637637e-01, 7.640427e-01, 7.643216e-01, 7.646002e-01, 7.648786e-01, 7.651569e-01, 7.654349e-01, 7.657127e-01, | |||
151 | 7.659904e-01, 7.662678e-01, 7.665451e-01, 7.668221e-01, 7.670989e-01, 7.673756e-01, 7.676520e-01, 7.679282e-01, | |||
152 | 7.682042e-01, 7.684801e-01, 7.687557e-01, 7.690312e-01, 7.693064e-01, 7.695814e-01, 7.698563e-01, 7.701310e-01, | |||
153 | 7.704054e-01, 7.706797e-01, 7.709538e-01, 7.712276e-01, 7.715013e-01, 7.717748e-01, 7.720481e-01, 7.723212e-01, | |||
154 | 7.725941e-01, 7.728668e-01, 7.731394e-01, 7.734116e-01, 7.736838e-01, 7.739558e-01, 7.742275e-01, 7.744991e-01, | |||
155 | 7.747704e-01, 7.750416e-01, 7.753126e-01, 7.755834e-01, 7.758540e-01, 7.761245e-01, 7.763947e-01, 7.766647e-01, | |||
156 | 7.769346e-01, 7.772043e-01, 7.774737e-01, 7.777431e-01, 7.780122e-01, 7.782811e-01, 7.785498e-01, 7.788184e-01, | |||
157 | 7.790868e-01, 7.793550e-01, 7.796230e-01, 7.798908e-01, 7.801584e-01, 7.804259e-01, 7.806932e-01, 7.809603e-01, | |||
158 | 7.812271e-01, 7.814939e-01, 7.817604e-01, 7.820268e-01, 7.822930e-01, 7.825589e-01, 7.828248e-01, 7.830904e-01, | |||
159 | 7.833558e-01, 7.836211e-01, 7.838862e-01, 7.841511e-01, 7.844158e-01, 7.846804e-01, 7.849448e-01, 7.852090e-01, | |||
160 | 7.854730e-01, 7.857369e-01, 7.860005e-01, 7.862641e-01, 7.865273e-01, 7.867905e-01, 7.870535e-01, 7.873163e-01, | |||
161 | 7.875788e-01, 7.878413e-01, 7.881036e-01, 7.883657e-01, 7.886276e-01, 7.888893e-01, 7.891509e-01, 7.894123e-01, | |||
162 | 7.896735e-01, 7.899345e-01, 7.901954e-01, 7.904561e-01, 7.907166e-01, 7.909770e-01, 7.912372e-01, 7.914972e-01, | |||
163 | 7.917571e-01, 7.920167e-01, 7.922763e-01, 7.925356e-01, 7.927948e-01, 7.930537e-01, 7.933126e-01, 7.935712e-01, | |||
164 | 7.938297e-01, 7.940881e-01, 7.943462e-01, 7.946042e-01, 7.948620e-01, 7.951197e-01, 7.953772e-01, 7.956345e-01, | |||
165 | 7.958916e-01, 7.961487e-01, 7.964054e-01, 7.966621e-01, 7.969186e-01, 7.971749e-01, 7.974311e-01, 7.976871e-01, | |||
166 | 7.979429e-01, 7.981986e-01, 7.984541e-01, 7.987095e-01, 7.989646e-01, 7.992196e-01, 7.994745e-01, 7.997292e-01, | |||
167 | 7.999837e-01, 8.002381e-01, 8.004923e-01, 8.007463e-01, 8.010002e-01, 8.012539e-01, 8.015075e-01, 8.017609e-01, | |||
168 | 8.020141e-01, 8.022672e-01, 8.025202e-01, 8.027729e-01, 8.030255e-01, 8.032780e-01, 8.035302e-01, 8.037823e-01, | |||
169 | 8.040344e-01, 8.042861e-01, 8.045378e-01, 8.047893e-01, 8.050406e-01, 8.052918e-01, 8.055428e-01, 8.057937e-01, | |||
170 | 8.060444e-01, 8.062950e-01, 8.065454e-01, 8.067956e-01, 8.070457e-01, 8.072957e-01, 8.075454e-01, 8.077950e-01, | |||
171 | 8.080446e-01, 8.082938e-01, 8.085430e-01, 8.087921e-01, 8.090409e-01, 8.092896e-01, 8.095381e-01, 8.097866e-01, | |||
172 | 8.100348e-01, 8.102829e-01, 8.105308e-01, 8.107786e-01, 8.110263e-01, 8.112738e-01, 8.115211e-01, 8.117683e-01, | |||
173 | 8.120154e-01, 8.122622e-01, 8.125089e-01, 8.127556e-01, 8.130020e-01, 8.132483e-01, 8.134944e-01, 8.137404e-01, | |||
174 | 8.139862e-01, 8.142319e-01, 8.144775e-01, 8.147229e-01, 8.149682e-01, 8.152133e-01, 8.154582e-01, 8.157030e-01, | |||
175 | 8.159477e-01, 8.161922e-01, 8.164365e-01, 8.166808e-01, 8.169249e-01, 8.171688e-01, 8.174126e-01, 8.176562e-01, | |||
176 | 8.178997e-01, 8.181431e-01, 8.183863e-01, 8.186293e-01, 8.188722e-01, 8.191150e-01, 8.193576e-01, 8.196001e-01, | |||
177 | 8.198425e-01, 8.200847e-01, 8.203267e-01, 8.205686e-01, 8.208104e-01, 8.210521e-01, 8.212935e-01, 8.215349e-01, | |||
178 | 8.217760e-01, 8.220171e-01, 8.222581e-01, 8.224988e-01, 8.227395e-01, 8.229799e-01, 8.232203e-01, 8.234605e-01, | |||
179 | 8.237006e-01, 8.239405e-01, 8.241804e-01, 8.244200e-01, 8.246595e-01, 8.248989e-01, 8.251381e-01, 8.253772e-01, | |||
180 | 8.256162e-01, 8.258550e-01, 8.260937e-01, 8.263323e-01, 8.265706e-01, 8.268089e-01, 8.270471e-01, 8.272851e-01, | |||
181 | 8.275229e-01, 8.277607e-01, 8.279983e-01, 8.282357e-01, 8.284730e-01, 8.287102e-01, 8.289472e-01, 8.291842e-01, | |||
182 | 8.294209e-01, 8.296576e-01, 8.298941e-01, 8.301305e-01, 8.303667e-01, 8.306028e-01, 8.308387e-01, 8.310746e-01, | |||
183 | 8.313103e-01, 8.315458e-01, 8.317813e-01, 8.320166e-01, 8.322517e-01, 8.324867e-01, 8.327217e-01, 8.329564e-01, | |||
184 | 8.331911e-01, 8.334256e-01, 8.336599e-01, 8.338942e-01, 8.341283e-01, 8.343623e-01, 8.345962e-01, 8.348299e-01, | |||
185 | 8.350635e-01, 8.352969e-01, 8.355302e-01, 8.357634e-01, 8.359964e-01, 8.362294e-01, 8.364622e-01, 8.366948e-01, | |||
186 | 8.369274e-01, 8.371598e-01, 8.373921e-01, 8.376243e-01, 8.378563e-01, 8.380882e-01, 8.383200e-01, 8.385516e-01, | |||
187 | 8.387831e-01, 8.390145e-01, 8.392458e-01, 8.394769e-01, 8.397079e-01, 8.399388e-01, 8.401695e-01, 8.404002e-01, | |||
188 | 8.406307e-01, 8.408611e-01, 8.410913e-01, 8.413214e-01, 8.415514e-01, 8.417813e-01, 8.420110e-01, 8.422406e-01, | |||
189 | 8.424702e-01, 8.426995e-01, 8.429288e-01, 8.431579e-01, 8.433869e-01, 8.436158e-01, 8.438445e-01, 8.440731e-01, | |||
190 | 8.443016e-01, 8.445300e-01, 8.447582e-01, 8.449863e-01, 8.452144e-01, 8.454422e-01, 8.456700e-01, 8.458977e-01, | |||
191 | 8.461251e-01, 8.463526e-01, 8.465798e-01, 8.468069e-01, 8.470340e-01, 8.472609e-01, 8.474877e-01, 8.477143e-01, | |||
192 | 8.479409e-01, 8.481673e-01, 8.483936e-01, 8.486198e-01, 8.488458e-01, 8.490717e-01, 8.492976e-01, 8.495233e-01, | |||
193 | 8.497488e-01, 8.499743e-01, 8.501996e-01, 8.504249e-01, 8.506500e-01, 8.508750e-01, 8.510998e-01, 8.513246e-01, | |||
194 | 8.515491e-01, 8.517737e-01, 8.519981e-01, 8.522223e-01, 8.524465e-01, 8.526706e-01, 8.528944e-01, 8.531182e-01, | |||
195 | 8.533419e-01, 8.535655e-01, 8.537889e-01, 8.540123e-01, 8.542355e-01, 8.544586e-01, 8.546816e-01, 8.549044e-01, | |||
196 | 8.551272e-01, 8.553498e-01, 8.555723e-01, 8.557947e-01, 8.560170e-01, 8.562392e-01, 8.564612e-01, 8.566832e-01, | |||
197 | 8.569050e-01, 8.571267e-01, 8.573483e-01, 8.575698e-01, 8.577912e-01, 8.580124e-01, 8.582336e-01, 8.584546e-01, | |||
198 | 8.586755e-01, 8.588963e-01, 8.591169e-01, 8.593375e-01, 8.595580e-01, 8.597783e-01, 8.599985e-01, 8.602186e-01, | |||
199 | 8.604387e-01, 8.606585e-01, 8.608783e-01, 8.610980e-01, 8.613176e-01, 8.615370e-01, 8.617563e-01, 8.619756e-01, | |||
200 | 8.621947e-01, 8.624136e-01, 8.626326e-01, 8.628513e-01, 8.630700e-01, 8.632885e-01, 8.635070e-01, 8.637253e-01, | |||
201 | 8.639436e-01, 8.641617e-01, 8.643796e-01, 8.645976e-01, 8.648154e-01, 8.650330e-01, 8.652506e-01, 8.654680e-01, | |||
202 | 8.656853e-01, 8.659026e-01, 8.661197e-01, 8.663368e-01, 8.665537e-01, 8.667705e-01, 8.669872e-01, 8.672037e-01, | |||
203 | 8.674202e-01, 8.676366e-01, 8.678529e-01, 8.680690e-01, 8.682851e-01, 8.685010e-01, 8.687168e-01, 8.689325e-01, | |||
204 | 8.691481e-01, 8.693637e-01, 8.695791e-01, 8.697944e-01, 8.700095e-01, 8.702246e-01, 8.704396e-01, 8.706545e-01, | |||
205 | 8.708693e-01, 8.710839e-01, 8.712984e-01, 8.715129e-01, 8.717272e-01, 8.719414e-01, 8.721556e-01, 8.723696e-01, | |||
206 | 8.725836e-01, 8.727974e-01, 8.730111e-01, 8.732247e-01, 8.734382e-01, 8.736516e-01, 8.738649e-01, 8.740780e-01, | |||
207 | 8.742912e-01, 8.745041e-01, 8.747170e-01, 8.749298e-01, 8.751425e-01, 8.753550e-01, 8.755675e-01, 8.757799e-01, | |||
208 | 8.759921e-01, 8.762043e-01, 8.764163e-01, 8.766283e-01, 8.768401e-01, 8.770519e-01, 8.772635e-01, 8.774751e-01, | |||
209 | 8.776865e-01, 8.778979e-01, 8.781091e-01, 8.783202e-01, 8.785312e-01, 8.787422e-01, 8.789530e-01, 8.791637e-01, | |||
210 | 8.793744e-01, 8.795849e-01, 8.797953e-01, 8.800057e-01, 8.802159e-01, 8.804260e-01, 8.806360e-01, 8.808460e-01, | |||
211 | 8.810558e-01, 8.812655e-01, 8.814751e-01, 8.816847e-01, 8.818941e-01, 8.821034e-01, 8.823127e-01, 8.825217e-01, | |||
212 | 8.827308e-01, 8.829397e-01, 8.831486e-01, 8.833573e-01, 8.835659e-01, 8.837745e-01, 8.839829e-01, 8.841912e-01, | |||
213 | 8.843995e-01, 8.846076e-01, 8.848156e-01, 8.850236e-01, 8.852314e-01, 8.854392e-01, 8.856469e-01, 8.858544e-01, | |||
214 | 8.860618e-01, 8.862692e-01, 8.864765e-01, 8.866837e-01, 8.868908e-01, 8.870977e-01, 8.873046e-01, 8.875114e-01, | |||
215 | 8.877181e-01, 8.879247e-01, 8.881311e-01, 8.883376e-01, 8.885438e-01, 8.887501e-01, 8.889562e-01, 8.891622e-01, | |||
216 | 8.893681e-01, 8.895739e-01, 8.897797e-01, 8.899853e-01, 8.901908e-01, 8.903963e-01, 8.906016e-01, 8.908069e-01, | |||
217 | 8.910121e-01, 8.912171e-01, 8.914221e-01, 8.916270e-01, 8.918318e-01, 8.920364e-01, 8.922410e-01, 8.924455e-01, | |||
218 | 8.926499e-01, 8.928543e-01, 8.930585e-01, 8.932626e-01, 8.934667e-01, 8.936706e-01, 8.938744e-01, 8.940782e-01, | |||
219 | 8.942819e-01, 8.944854e-01, 8.946889e-01, 8.948923e-01, 8.950956e-01, 8.952988e-01, 8.955019e-01, 8.957049e-01, | |||
220 | 8.959078e-01, 8.961107e-01, 8.963134e-01, 8.965160e-01, 8.967186e-01, 8.969210e-01, 8.971235e-01, 8.973257e-01, | |||
221 | 8.975279e-01, 8.977300e-01, 8.979320e-01, 8.981339e-01, 8.983358e-01, 8.985375e-01, 8.987392e-01, 8.989407e-01, | |||
222 | 8.991421e-01, 8.993436e-01, 8.995448e-01, 8.997460e-01, 8.999471e-01, 9.001482e-01, 9.003491e-01, 9.005499e-01, | |||
223 | 9.007506e-01, 9.009513e-01, 9.011519e-01, 9.013523e-01, 9.015527e-01, 9.017531e-01, 9.019532e-01, 9.021534e-01, | |||
224 | 9.023534e-01, 9.025534e-01, 9.027532e-01, 9.029530e-01, 9.031526e-01, 9.033523e-01, 9.035518e-01, 9.037512e-01, | |||
225 | 9.039505e-01, 9.041498e-01, 9.043489e-01, 9.045479e-01, 9.047469e-01, 9.049459e-01, 9.051446e-01, 9.053434e-01, | |||
226 | 9.055420e-01, 9.057405e-01, 9.059390e-01, 9.061373e-01, 9.063356e-01, 9.065338e-01, 9.067319e-01, 9.069299e-01, | |||
227 | 9.071279e-01, 9.073257e-01, 9.075235e-01, 9.077212e-01, 9.079187e-01, 9.081162e-01, 9.083136e-01, 9.085110e-01, | |||
228 | 9.087082e-01, 9.089054e-01, 9.091024e-01, 9.092994e-01, 9.094964e-01, 9.096932e-01, 9.098899e-01, 9.100866e-01, | |||
229 | 9.102831e-01, 9.104796e-01, 9.106760e-01, 9.108723e-01, 9.110685e-01, 9.112647e-01, 9.114607e-01, 9.116567e-01, | |||
230 | 9.118526e-01, 9.120483e-01, 9.122441e-01, 9.124397e-01, 9.126353e-01, 9.128307e-01, 9.130261e-01, 9.132214e-01, | |||
231 | 9.134166e-01, 9.136118e-01, 9.138068e-01, 9.140018e-01, 9.141967e-01, 9.143915e-01, 9.145862e-01, 9.147808e-01, | |||
232 | 9.149753e-01, 9.151698e-01, 9.153642e-01, 9.155585e-01, 9.157528e-01, 9.159469e-01, 9.161409e-01, 9.163349e-01, | |||
233 | 9.165288e-01, 9.167226e-01, 9.169164e-01, 9.171100e-01, 9.173036e-01, 9.174970e-01, 9.176905e-01, 9.178838e-01, | |||
234 | 9.180770e-01, 9.182702e-01, 9.184632e-01, 9.186562e-01, 9.188492e-01, 9.190420e-01, 9.192348e-01, 9.194274e-01, | |||
235 | 9.196200e-01, 9.198125e-01, 9.200049e-01, 9.201973e-01, 9.203895e-01, 9.205818e-01, 9.207739e-01, 9.209659e-01, | |||
236 | 9.211578e-01, 9.213497e-01, 9.215415e-01, 9.217332e-01, 9.219248e-01, 9.221163e-01, 9.223078e-01, 9.224992e-01, | |||
237 | 9.226905e-01, 9.228818e-01, 9.230729e-01, 9.232640e-01, 9.234550e-01, 9.236459e-01, 9.238367e-01, 9.240275e-01, | |||
238 | 9.242182e-01, 9.244088e-01, 9.245993e-01, 9.247897e-01, 9.249801e-01, 9.251704e-01, 9.253606e-01, 9.255507e-01, | |||
239 | 9.257408e-01, 9.259307e-01, 9.261206e-01, 9.263105e-01, 9.265002e-01, 9.266899e-01, 9.268795e-01, 9.270689e-01, | |||
240 | 9.272584e-01, 9.274477e-01, 9.276370e-01, 9.278262e-01, 9.280154e-01, 9.282044e-01, 9.283934e-01, 9.285822e-01, | |||
241 | 9.287710e-01, 9.289598e-01, 9.291484e-01, 9.293370e-01, 9.295255e-01, 9.297140e-01, 9.299023e-01, 9.300906e-01, | |||
242 | 9.302788e-01, 9.304669e-01, 9.306549e-01, 9.308429e-01, 9.310308e-01, 9.312186e-01, 9.314064e-01, 9.315941e-01, | |||
243 | 9.317816e-01, 9.319692e-01, 9.321566e-01, 9.323440e-01, 9.325313e-01, 9.327185e-01, 9.329057e-01, 9.330927e-01, | |||
244 | 9.332797e-01, 9.334666e-01, 9.336535e-01, 9.338402e-01, 9.340270e-01, 9.342135e-01, 9.344001e-01, 9.345866e-01, | |||
245 | 9.347730e-01, 9.349593e-01, 9.351455e-01, 9.353317e-01, 9.355178e-01, 9.357038e-01, 9.358898e-01, 9.360756e-01, | |||
246 | 9.362615e-01, 9.364472e-01, 9.366328e-01, 9.368184e-01, 9.370039e-01, 9.371893e-01, 9.373747e-01, 9.375600e-01, | |||
247 | 9.377452e-01, 9.379303e-01, 9.381154e-01, 9.383004e-01, 9.384854e-01, 9.386702e-01, 9.388550e-01, 9.390397e-01, | |||
248 | 9.392243e-01, 9.394089e-01, 9.395934e-01, 9.397778e-01, 9.399621e-01, 9.401464e-01, 9.403306e-01, 9.405147e-01, | |||
249 | 9.406988e-01, 9.408827e-01, 9.410667e-01, 9.412505e-01, 9.414343e-01, 9.416180e-01, 9.418016e-01, 9.419851e-01, | |||
250 | 9.421686e-01, 9.423520e-01, 9.425353e-01, 9.427186e-01, 9.429018e-01, 9.430850e-01, 9.432680e-01, 9.434510e-01, | |||
251 | 9.436339e-01, 9.438167e-01, 9.439995e-01, 9.441822e-01, 9.443648e-01, 9.445474e-01, 9.447299e-01, 9.449123e-01, | |||
252 | 9.450946e-01, 9.452769e-01, 9.454591e-01, 9.456412e-01, 9.458233e-01, 9.460053e-01, 9.461872e-01, 9.463691e-01, | |||
253 | 9.465508e-01, 9.467326e-01, 9.469142e-01, 9.470958e-01, 9.472773e-01, 9.474587e-01, 9.476401e-01, 9.478214e-01, | |||
254 | 9.480026e-01, 9.481838e-01, 9.483649e-01, 9.485459e-01, 9.487268e-01, 9.489077e-01, 9.490886e-01, 9.492693e-01, | |||
255 | 9.494500e-01, 9.496306e-01, 9.498111e-01, 9.499916e-01, 9.501719e-01, 9.503523e-01, 9.505326e-01, 9.507128e-01, | |||
256 | 9.508929e-01, 9.510729e-01, 9.512529e-01, 9.514329e-01, 9.516127e-01, 9.517925e-01, 9.519722e-01, 9.521519e-01, | |||
257 | 9.523315e-01, 9.525110e-01, 9.526904e-01, 9.528698e-01, 9.530491e-01, 9.532284e-01, 9.534075e-01, 9.535866e-01, | |||
258 | 9.537657e-01, 9.539447e-01, 9.541236e-01, 9.543024e-01, 9.544812e-01, 9.546599e-01, 9.548386e-01, 9.550171e-01, | |||
259 | 9.551957e-01, 9.553741e-01, 9.555525e-01, 9.557307e-01, 9.559090e-01, 9.560872e-01, 9.562653e-01, 9.564433e-01, | |||
260 | 9.566213e-01, 9.567992e-01, 9.569771e-01, 9.571549e-01, 9.573326e-01, 9.575102e-01, 9.576878e-01, 9.578653e-01, | |||
261 | 9.580427e-01, 9.582201e-01, 9.583974e-01, 9.585747e-01, 9.587519e-01, 9.589290e-01, 9.591061e-01, 9.592831e-01, | |||
262 | 9.594600e-01, 9.596368e-01, 9.598137e-01, 9.599904e-01, 9.601671e-01, 9.603436e-01, 9.605201e-01, 9.606966e-01, | |||
263 | 9.608730e-01, 9.610494e-01, 9.612256e-01, 9.614019e-01, 9.615780e-01, 9.617541e-01, 9.619301e-01, 9.621060e-01, | |||
264 | 9.622819e-01, 9.624578e-01, 9.626336e-01, 9.628092e-01, 9.629849e-01, 9.631604e-01, 9.633359e-01, 9.635113e-01, | |||
265 | 9.636867e-01, 9.638621e-01, 9.640373e-01, 9.642125e-01, 9.643876e-01, 9.645627e-01, 9.647377e-01, 9.649126e-01, | |||
266 | 9.650874e-01, 9.652622e-01, 9.654370e-01, 9.656116e-01, 9.657863e-01, 9.659608e-01, 9.661353e-01, 9.663097e-01, | |||
267 | 9.664841e-01, 9.666584e-01, 9.668326e-01, 9.670068e-01, 9.671809e-01, 9.673550e-01, 9.675289e-01, 9.677029e-01, | |||
268 | 9.678767e-01, 9.680505e-01, 9.682242e-01, 9.683979e-01, 9.685715e-01, 9.687451e-01, 9.689186e-01, 9.690920e-01, | |||
269 | 9.692653e-01, 9.694387e-01, 9.696119e-01, 9.697851e-01, 9.699582e-01, 9.701312e-01, 9.703043e-01, 9.704772e-01, | |||
270 | 9.706500e-01, 9.708228e-01, 9.709955e-01, 9.711683e-01, 9.713409e-01, 9.715135e-01, 9.716859e-01, 9.718584e-01, | |||
271 | 9.720308e-01, 9.722031e-01, 9.723753e-01, 9.725475e-01, 9.727197e-01, 9.728917e-01, 9.730637e-01, 9.732357e-01, | |||
272 | 9.734076e-01, 9.735794e-01, 9.737512e-01, 9.739228e-01, 9.740945e-01, 9.742661e-01, 9.744377e-01, 9.746091e-01, | |||
273 | 9.747805e-01, 9.749519e-01, 9.751231e-01, 9.752944e-01, 9.754655e-01, 9.756366e-01, 9.758077e-01, 9.759787e-01, | |||
274 | 9.761496e-01, 9.763204e-01, 9.764913e-01, 9.766620e-01, 9.768327e-01, 9.770033e-01, 9.771739e-01, 9.773444e-01, | |||
275 | 9.775148e-01, 9.776852e-01, 9.778556e-01, 9.780258e-01, 9.781960e-01, 9.783661e-01, 9.785362e-01, 9.787063e-01, | |||
276 | 9.788762e-01, 9.790462e-01, 9.792160e-01, 9.793859e-01, 9.795555e-01, 9.797252e-01, 9.798949e-01, 9.800645e-01, | |||
277 | 9.802339e-01, 9.804034e-01, 9.805728e-01, 9.807421e-01, 9.809114e-01, 9.810806e-01, 9.812497e-01, 9.814188e-01, | |||
278 | 9.815878e-01, 9.817568e-01, 9.819257e-01, 9.820946e-01, 9.822634e-01, 9.824321e-01, 9.826008e-01, 9.827695e-01, | |||
279 | 9.829381e-01, 9.831066e-01, 9.832750e-01, 9.834434e-01, 9.836118e-01, 9.837800e-01, 9.839482e-01, 9.841164e-01, | |||
280 | 9.842845e-01, 9.844526e-01, 9.846206e-01, 9.847885e-01, 9.849564e-01, 9.851242e-01, 9.852920e-01, 9.854597e-01, | |||
281 | 9.856274e-01, 9.857950e-01, 9.859625e-01, 9.861299e-01, 9.862974e-01, 9.864647e-01, 9.866320e-01, 9.867993e-01, | |||
282 | 9.869665e-01, 9.871337e-01, 9.873008e-01, 9.874678e-01, 9.876347e-01, 9.878017e-01, 9.879685e-01, 9.881353e-01, | |||
283 | 9.883021e-01, 9.884688e-01, 9.886354e-01, 9.888020e-01, 9.889685e-01, 9.891350e-01, 9.893014e-01, 9.894677e-01, | |||
284 | 9.896340e-01, 9.898003e-01, 9.899665e-01, 9.901326e-01, 9.902986e-01, 9.904646e-01, 9.906306e-01, 9.907965e-01, | |||
285 | 9.909624e-01, 9.911281e-01, 9.912939e-01, 9.914596e-01, 9.916252e-01, 9.917908e-01, 9.919563e-01, 9.921218e-01, | |||
286 | 9.922872e-01, 9.924526e-01, 9.926178e-01, 9.927831e-01, 9.929483e-01, 9.931134e-01, 9.932785e-01, 9.934435e-01, | |||
287 | 9.936085e-01, 9.937734e-01, 9.939383e-01, 9.941031e-01, 9.942678e-01, 9.944325e-01, 9.945971e-01, 9.947617e-01, | |||
288 | 9.949263e-01, 9.950907e-01, 9.952552e-01, 9.954196e-01, 9.955838e-01, 9.957481e-01, 9.959123e-01, 9.960765e-01, | |||
289 | 9.962406e-01, 9.964046e-01, 9.965686e-01, 9.967325e-01, 9.968964e-01, 9.970602e-01, 9.972240e-01, 9.973878e-01, | |||
290 | 9.975514e-01, 9.977150e-01, 9.978786e-01, 9.980421e-01, 9.982055e-01, 9.983689e-01, 9.985323e-01, 9.986956e-01, | |||
291 | 9.988588e-01, 9.990220e-01, 9.991851e-01, 9.993482e-01, 9.995112e-01, 9.996742e-01, 9.998372e-01, 1.000000e+00, | |||
292 | }; | |||
293 | int i = (int)(d * 2047); | |||
294 | assert(i >= 0 && i < 2048)((void) sizeof ((i >= 0 && i < 2048) ? 1 : 0), __extension__ ({ if (i >= 0 && i < 2048) ; else __assert_fail ("i >= 0 && i < 2048", "ccv_scd.c", 294, __extension__ __PRETTY_FUNCTION__); })); | |||
295 | return cube_root[i]; | |||
296 | } | |||
297 | ||||
298 | static inline void _ccv_rgb_to_luv(const float r, const float g, const float b, float* pl, float* pu, float* pv) | |||
299 | { | |||
300 | const float x = 0.412453f * r + 0.35758f * g + 0.180423f * b; | |||
301 | const float y = 0.212671f * r + 0.71516f * g + 0.072169f * b; | |||
302 | const float z = 0.019334f * r + 0.119193f * g + 0.950227f * b; | |||
303 | ||||
304 | const float x_n = 0.312713f, y_n = 0.329016f; | |||
305 | const float uv_n_divisor = -2.f * x_n + 12.f * y_n + 3.f; | |||
306 | const float u_n = 4.f * x_n / uv_n_divisor; | |||
307 | const float v_n = 9.f * y_n / uv_n_divisor; | |||
308 | ||||
309 | const float uv_divisor = ccv_max((x + 15.f * y + 3.f * z), FLT_EPSILON)({ typeof ((x + 15.f * y + 3.f * z)) _a = ((x + 15.f * y + 3.f * z)); typeof (1.19209290e-7F) _b = (1.19209290e-7F); (_a > _b) ? _a : _b; }); | |||
310 | const float u = 4.f * x / uv_divisor; | |||
311 | const float v = 9.f * y / uv_divisor; | |||
312 | ||||
313 | const float y_cube_root = fast_cube_root(y); | |||
314 | ||||
315 | const float l_value = ccv_max(0.f, ((116.f * y_cube_root) - 16.f))({ typeof (0.f) _a = (0.f); typeof (((116.f * y_cube_root) - 16.f )) _b = (((116.f * y_cube_root) - 16.f)); (_a > _b) ? _a : _b; }); | |||
316 | const float u_value = 13.f * l_value * (u - u_n); | |||
317 | const float v_value = 13.f * l_value * (v - v_n); | |||
318 | ||||
319 | // L in [0, 100], U in [-134, 220], V in [-140, 122] | |||
320 | *pl = l_value * (255.f / 100.f); | |||
321 | *pu = (u_value + 134.f) * (255.f / (220.f + 134.f)); | |||
322 | *pv = (v_value + 140.f) * (255.f / (122.f + 140.f)); | |||
323 | } | |||
324 | ||||
325 | void ccv_scd(ccv_dense_matrix_t* a, ccv_dense_matrix_t** b, int type) | |||
326 | { | |||
327 | int ch = CCV_GET_CHANNEL(a->type)((a->type) & 0xFFF); | |||
328 | assert(ch == 1 || ch == 3)((void) sizeof ((ch == 1 || ch == 3) ? 1 : 0), __extension__ ( { if (ch == 1 || ch == 3) ; else __assert_fail ("ch == 1 || ch == 3" , "ccv_scd.c", 328, __extension__ __PRETTY_FUNCTION__); })); | |||
329 | ccv_declare_derived_signature(sig, a->sig != 0, ccv_sign_with_literal("ccv_scd"), a->sig, CCV_EOF_SIGN)char _ccv_identifier_329[] = ("ccv_scd"); size_t _ccv_string_size_329 = sizeof(_ccv_identifier_329);; uint64_t sig = (a->sig != 0) ? ccv_cache_generate_signature(_ccv_identifier_329, _ccv_string_size_329 , a->sig, ((uint64_t)0)) : 0;; | |||
330 | // diagonal u, v, x, y and LUV color, therefore 11 channels | |||
331 | ccv_dense_matrix_t* db = *b = ccv_dense_matrix_renew(*b, a->rows, a->cols, CCV_32F | 11, CCV_32F | 11, sig); | |||
332 | ccv_object_return_if_cached(, db){ if ((!(db) || (((int*)(db))[0] & CCV_GARBAGE)) && (!(0) || (((int*)(0))[0] & CCV_GARBAGE)) && (!(0 ) || (((int*)(0))[0] & CCV_GARBAGE)) && (!(0) || ( ((int*)(0))[0] & CCV_GARBAGE)) && (!(0) || (((int *)(0))[0] & CCV_GARBAGE)) && (!(0) || (((int*)(0) )[0] & CCV_GARBAGE)) && (!(0) || (((int*)(0))[0] & CCV_GARBAGE)) && (!(0) || (((int*)(0))[0] & CCV_GARBAGE )) && (!(0) || (((int*)(0))[0] & CCV_GARBAGE)) && (!(0) || (((int*)(0))[0] & CCV_GARBAGE)) && (!(0 ) || (((int*)(0))[0] & CCV_GARBAGE)) && (!(0) || ( ((int*)(0))[0] & CCV_GARBAGE)) && (!(0) || (((int *)(0))[0] & CCV_GARBAGE)) && (!(0) || (((int*)(0) )[0] & CCV_GARBAGE)) && (!(0) || (((int*)(0))[0] & CCV_GARBAGE)) && (!(0) || (((int*)(0))[0] & CCV_GARBAGE )) && (!(0) || (((int*)(0))[0] & CCV_GARBAGE)) && (!(0) || (((int*)(0))[0] & CCV_GARBAGE)) && (!(0 ) || (((int*)(0))[0] & CCV_GARBAGE)) && (!(0) || ( ((int*)(0))[0] & CCV_GARBAGE)) && (!(0) || (((int *)(0))[0] & CCV_GARBAGE)) && (!(0) || (((int*)(0) )[0] & CCV_GARBAGE)) && (!(0) || (((int*)(0))[0] & CCV_GARBAGE)) && (!(0) || (((int*)(0))[0] & CCV_GARBAGE )) && (!(0) || (((int*)(0))[0] & CCV_GARBAGE)) && (!(0) || (((int*)(0))[0] & CCV_GARBAGE)) && (!(0 ) || (((int*)(0))[0] & CCV_GARBAGE)) && (!(0) || ( ((int*)(0))[0] & CCV_GARBAGE)) && (!(0) || (((int *)(0))[0] & CCV_GARBAGE)) && (!(0) || (((int*)(0) )[0] & CCV_GARBAGE)) && (!(0) || (((int*)(0))[0] & CCV_GARBAGE)) && (!(0) || (((int*)(0))[0] & CCV_GARBAGE )) && (!(0) || (((int*)(0))[0] & CCV_GARBAGE)) && (!(0) || (((int*)(0))[0] & CCV_GARBAGE)) && (!(0 ) || (((int*)(0))[0] & CCV_GARBAGE)) && (!(0) || ( ((int*)(0))[0] & CCV_GARBAGE)) && (!(0) || (((int *)(0))[0] & CCV_GARBAGE)) && (!(0) || (((int*)(0) )[0] & CCV_GARBAGE)) && (!(0) || (((int*)(0))[0] & CCV_GARBAGE)) && (!(0) || (((int*)(0))[0] & CCV_GARBAGE )) && (!(0) || (((int*)(0))[0] & CCV_GARBAGE)) && (!(0) || (((int*)(0))[0] & CCV_GARBAGE)) && (!(0 ) || (((int*)(0))[0] & CCV_GARBAGE)) && (!(0) || ( ((int*)(0))[0] & CCV_GARBAGE)) && (!(0) || (((int *)(0))[0] & CCV_GARBAGE)) && (!(0) || (((int*)(0) )[0] & CCV_GARBAGE)) && (!(0) || (((int*)(0))[0] & CCV_GARBAGE)) && (!(0) || (((int*)(0))[0] & CCV_GARBAGE )) && (!(0) || (((int*)(0))[0] & CCV_GARBAGE)) && (!(0) || (((int*)(0))[0] & CCV_GARBAGE)) && (!(0 ) || (((int*)(0))[0] & CCV_GARBAGE)) && (!(0) || ( ((int*)(0))[0] & CCV_GARBAGE)) && (!(0) || (((int *)(0))[0] & CCV_GARBAGE)) && (!(0) || (((int*)(0) )[0] & CCV_GARBAGE)) && (!(0) || (((int*)(0))[0] & CCV_GARBAGE)) && (!(0) || (((int*)(0))[0] & CCV_GARBAGE )) && (!(0) || (((int*)(0))[0] & CCV_GARBAGE)) && (!(0) || (((int*)(0))[0] & CCV_GARBAGE)) && (!(0 ) || (((int*)(0))[0] & CCV_GARBAGE)) && (!(0) || ( ((int*)(0))[0] & CCV_GARBAGE)) && (!(0) || (((int *)(0))[0] & CCV_GARBAGE)) && (!(0) || (((int*)(0) )[0] & CCV_GARBAGE)) && (!(0) || (((int*)(0))[0] & CCV_GARBAGE))) { (void)((db) && (((int*)(db))[0] &= ~CCV_GARBAGE));(void)((0) && (((int*)(0))[0] &= ~ CCV_GARBAGE));(void)((0) && (((int*)(0))[0] &= ~CCV_GARBAGE ));(void)((0) && (((int*)(0))[0] &= ~CCV_GARBAGE) );(void)((0) && (((int*)(0))[0] &= ~CCV_GARBAGE)) ;(void)((0) && (((int*)(0))[0] &= ~CCV_GARBAGE)); (void)((0) && (((int*)(0))[0] &= ~CCV_GARBAGE));( void)((0) && (((int*)(0))[0] &= ~CCV_GARBAGE));(void )((0) && (((int*)(0))[0] &= ~CCV_GARBAGE));(void) ((0) && (((int*)(0))[0] &= ~CCV_GARBAGE));(void)( (0) && (((int*)(0))[0] &= ~CCV_GARBAGE));(void)(( 0) && (((int*)(0))[0] &= ~CCV_GARBAGE));(void)((0 ) && (((int*)(0))[0] &= ~CCV_GARBAGE));(void)((0) && (((int*)(0))[0] &= ~CCV_GARBAGE));(void)((0) && (((int*)(0))[0] &= ~CCV_GARBAGE));(void)((0) && ( ((int*)(0))[0] &= ~CCV_GARBAGE));(void)((0) && (( (int*)(0))[0] &= ~CCV_GARBAGE));(void)((0) && ((( int*)(0))[0] &= ~CCV_GARBAGE));(void)((0) && (((int *)(0))[0] &= ~CCV_GARBAGE));(void)((0) && (((int* )(0))[0] &= ~CCV_GARBAGE));(void)((0) && (((int*) (0))[0] &= ~CCV_GARBAGE));(void)((0) && (((int*)( 0))[0] &= ~CCV_GARBAGE));(void)((0) && (((int*)(0 ))[0] &= ~CCV_GARBAGE));(void)((0) && (((int*)(0) )[0] &= ~CCV_GARBAGE));(void)((0) && (((int*)(0)) [0] &= ~CCV_GARBAGE));(void)((0) && (((int*)(0))[ 0] &= ~CCV_GARBAGE));(void)((0) && (((int*)(0))[0 ] &= ~CCV_GARBAGE));(void)((0) && (((int*)(0))[0] &= ~CCV_GARBAGE));(void)((0) && (((int*)(0))[0] &= ~CCV_GARBAGE));(void)((0) && (((int*)(0))[0] &= ~ CCV_GARBAGE));(void)((0) && (((int*)(0))[0] &= ~CCV_GARBAGE ));(void)((0) && (((int*)(0))[0] &= ~CCV_GARBAGE) );(void)((0) && (((int*)(0))[0] &= ~CCV_GARBAGE)) ;(void)((0) && (((int*)(0))[0] &= ~CCV_GARBAGE)); (void)((0) && (((int*)(0))[0] &= ~CCV_GARBAGE));( void)((0) && (((int*)(0))[0] &= ~CCV_GARBAGE));(void )((0) && (((int*)(0))[0] &= ~CCV_GARBAGE));(void) ((0) && (((int*)(0))[0] &= ~CCV_GARBAGE));(void)( (0) && (((int*)(0))[0] &= ~CCV_GARBAGE));(void)(( 0) && (((int*)(0))[0] &= ~CCV_GARBAGE));(void)((0 ) && (((int*)(0))[0] &= ~CCV_GARBAGE));(void)((0) && (((int*)(0))[0] &= ~CCV_GARBAGE));(void)((0) && (((int*)(0))[0] &= ~CCV_GARBAGE));(void)((0) && ( ((int*)(0))[0] &= ~CCV_GARBAGE));(void)((0) && (( (int*)(0))[0] &= ~CCV_GARBAGE));(void)((0) && ((( int*)(0))[0] &= ~CCV_GARBAGE));(void)((0) && (((int *)(0))[0] &= ~CCV_GARBAGE));(void)((0) && (((int* )(0))[0] &= ~CCV_GARBAGE));(void)((0) && (((int*) (0))[0] &= ~CCV_GARBAGE));(void)((0) && (((int*)( 0))[0] &= ~CCV_GARBAGE));(void)((0) && (((int*)(0 ))[0] &= ~CCV_GARBAGE));(void)((0) && (((int*)(0) )[0] &= ~CCV_GARBAGE));(void)((0) && (((int*)(0)) [0] &= ~CCV_GARBAGE));(void)((0) && (((int*)(0))[ 0] &= ~CCV_GARBAGE));(void)((0) && (((int*)(0))[0 ] &= ~CCV_GARBAGE));(void)((0) && (((int*)(0))[0] &= ~CCV_GARBAGE));(void)((0) && (((int*)(0))[0] &= ~CCV_GARBAGE));(void)((0) && (((int*)(0))[0] &= ~ CCV_GARBAGE));(void)((0) && (((int*)(0))[0] &= ~CCV_GARBAGE ));(void)((0) && (((int*)(0))[0] &= ~CCV_GARBAGE) );(void)((0) && (((int*)(0))[0] &= ~CCV_GARBAGE)) ;(void)((0) && (((int*)(0))[0] &= ~CCV_GARBAGE)); (void)((0) && (((int*)(0))[0] &= ~CCV_GARBAGE));; ; return ; } }; | |||
333 | ccv_dense_matrix_t* blur = 0; | |||
334 | ccv_blur(a, &blur, 0, 0.5); // do a modest blur, which suppresses noise | |||
335 | ccv_dense_matrix_t* dx = 0; | |||
336 | ccv_sobel(blur, &dx, 0, 1, 0); | |||
337 | ccv_dense_matrix_t* dy = 0; | |||
338 | ccv_sobel(blur, &dy, 0, 0, 1); | |||
339 | ccv_dense_matrix_t* du = 0; | |||
340 | ccv_sobel(blur, &du, 0, 1, 1); | |||
341 | ccv_dense_matrix_t* dv = 0; | |||
342 | ccv_sobel(blur, &dv, 0, -1, 1); | |||
343 | ccv_matrix_free(blur); | |||
344 | assert(CCV_GET_DATA_TYPE(dx->type) == CCV_GET_DATA_TYPE(dy->type))((void) sizeof ((((dx->type) & 0xFF000) == ((dy->type ) & 0xFF000)) ? 1 : 0), __extension__ ({ if (((dx->type ) & 0xFF000) == ((dy->type) & 0xFF000)) ; else __assert_fail ("CCV_GET_DATA_TYPE(dx->type) == CCV_GET_DATA_TYPE(dy->type)" , "ccv_scd.c", 344, __extension__ __PRETTY_FUNCTION__); })); | |||
345 | assert(CCV_GET_DATA_TYPE(dy->type) == CCV_GET_DATA_TYPE(du->type))((void) sizeof ((((dy->type) & 0xFF000) == ((du->type ) & 0xFF000)) ? 1 : 0), __extension__ ({ if (((dy->type ) & 0xFF000) == ((du->type) & 0xFF000)) ; else __assert_fail ("CCV_GET_DATA_TYPE(dy->type) == CCV_GET_DATA_TYPE(du->type)" , "ccv_scd.c", 345, __extension__ __PRETTY_FUNCTION__); })); | |||
346 | assert(CCV_GET_DATA_TYPE(du->type) == CCV_GET_DATA_TYPE(dv->type))((void) sizeof ((((du->type) & 0xFF000) == ((dv->type ) & 0xFF000)) ? 1 : 0), __extension__ ({ if (((du->type ) & 0xFF000) == ((dv->type) & 0xFF000)) ; else __assert_fail ("CCV_GET_DATA_TYPE(du->type) == CCV_GET_DATA_TYPE(dv->type)" , "ccv_scd.c", 346, __extension__ __PRETTY_FUNCTION__); })); | |||
347 | assert(CCV_GET_CHANNEL(dx->type) == CCV_GET_CHANNEL(dy->type))((void) sizeof ((((dx->type) & 0xFFF) == ((dy->type ) & 0xFFF)) ? 1 : 0), __extension__ ({ if (((dx->type) & 0xFFF) == ((dy->type) & 0xFFF)) ; else __assert_fail ("CCV_GET_CHANNEL(dx->type) == CCV_GET_CHANNEL(dy->type)" , "ccv_scd.c", 347, __extension__ __PRETTY_FUNCTION__); })); | |||
348 | assert(CCV_GET_CHANNEL(dy->type) == CCV_GET_CHANNEL(du->type))((void) sizeof ((((dy->type) & 0xFFF) == ((du->type ) & 0xFFF)) ? 1 : 0), __extension__ ({ if (((dy->type) & 0xFFF) == ((du->type) & 0xFFF)) ; else __assert_fail ("CCV_GET_CHANNEL(dy->type) == CCV_GET_CHANNEL(du->type)" , "ccv_scd.c", 348, __extension__ __PRETTY_FUNCTION__); })); | |||
349 | assert(CCV_GET_CHANNEL(du->type) == CCV_GET_CHANNEL(dv->type))((void) sizeof ((((du->type) & 0xFFF) == ((dv->type ) & 0xFFF)) ? 1 : 0), __extension__ ({ if (((du->type) & 0xFFF) == ((dv->type) & 0xFFF)) ; else __assert_fail ("CCV_GET_CHANNEL(du->type) == CCV_GET_CHANNEL(dv->type)" , "ccv_scd.c", 349, __extension__ __PRETTY_FUNCTION__); })); | |||
350 | // this is a naive unoptimized implementation yet | |||
351 | int i, j, k; | |||
352 | unsigned char* a_ptr = a->data.u8; | |||
353 | unsigned char* dx_ptr = dx->data.u8; | |||
354 | unsigned char* dy_ptr = dy->data.u8; | |||
355 | unsigned char* du_ptr = du->data.u8; | |||
356 | unsigned char* dv_ptr = dv->data.u8; | |||
357 | float* dbp = db->data.f32; | |||
358 | if (ch == 1) | |||
359 | { | |||
360 | #define for_block(_for_get_d, _for_get_a) \ | |||
361 | for (i = 0; i < a->rows; i++) \ | |||
362 | { \ | |||
363 | for (j = 0; j < a->cols; j++) \ | |||
364 | { \ | |||
365 | float fdx = _for_get_d(dx_ptr, j), fdy = _for_get_d(dy_ptr, j); \ | |||
366 | float fdu = _for_get_d(du_ptr, j), fdv = _for_get_d(dv_ptr, j); \ | |||
367 | float adx = fabsf(fdx), ady = fabsf(fdy); \ | |||
368 | float adu = fabsf(fdu), adv = fabsf(fdv); \ | |||
369 | dbp[0] = fdx, dbp[1] = fdy; \ | |||
370 | dbp[2] = fdu, dbp[3] = fdv; \ | |||
371 | dbp[4] = adx, dbp[5] = ady; \ | |||
372 | dbp[6] = adu, dbp[7] = adv; \ | |||
373 | dbp[8] = _for_get_a(a_ptr, j) / 255.0; \ | |||
374 | dbp[9] = dbp[10] = 0; \ | |||
375 | dbp += 11; \ | |||
376 | } \ | |||
377 | a_ptr += a->step; \ | |||
378 | dx_ptr += dx->step; \ | |||
379 | dy_ptr += dy->step; \ | |||
380 | du_ptr += du->step; \ | |||
381 | dv_ptr += dv->step; \ | |||
382 | } | |||
383 | ccv_matrix_getter(dx->type, ccv_matrix_getter_a, a->type, for_block){ switch (((dx->type) & 0xFF000)) { case CCV_32S: { { switch (((a->type) & 0xFF000)) { case CCV_32S: { for_block(_ccv_get_32s_value , _ccv_get_32s_value); break; } case CCV_32F: { for_block(_ccv_get_32s_value , _ccv_get_32f_value); break; } case CCV_64S: { for_block(_ccv_get_32s_value , _ccv_get_64s_value); break; } case CCV_64F: { for_block(_ccv_get_32s_value , _ccv_get_64f_value); break; } default: { for_block(_ccv_get_32s_value , _ccv_get_8u_value); } } }; break; } case CCV_32F: { { switch (((a->type) & 0xFF000)) { case CCV_32S: { for_block(_ccv_get_32f_value , _ccv_get_32s_value); break; } case CCV_32F: { for_block(_ccv_get_32f_value , _ccv_get_32f_value); break; } case CCV_64S: { for_block(_ccv_get_32f_value , _ccv_get_64s_value); break; } case CCV_64F: { for_block(_ccv_get_32f_value , _ccv_get_64f_value); break; } default: { for_block(_ccv_get_32f_value , _ccv_get_8u_value); } } }; break; } case CCV_64S: { { switch (((a->type) & 0xFF000)) { case CCV_32S: { for_block(_ccv_get_64s_value , _ccv_get_32s_value); break; } case CCV_32F: { for_block(_ccv_get_64s_value , _ccv_get_32f_value); break; } case CCV_64S: { for_block(_ccv_get_64s_value , _ccv_get_64s_value); break; } case CCV_64F: { for_block(_ccv_get_64s_value , _ccv_get_64f_value); break; } default: { for_block(_ccv_get_64s_value , _ccv_get_8u_value); } } }; break; } case CCV_64F: { { switch (((a->type) & 0xFF000)) { case CCV_32S: { for_block(_ccv_get_64f_value , _ccv_get_32s_value); break; } case CCV_32F: { for_block(_ccv_get_64f_value , _ccv_get_32f_value); break; } case CCV_64S: { for_block(_ccv_get_64f_value , _ccv_get_64s_value); break; } case CCV_64F: { for_block(_ccv_get_64f_value , _ccv_get_64f_value); break; } default: { for_block(_ccv_get_64f_value , _ccv_get_8u_value); } } }; break; } default: { { switch ((( a->type) & 0xFF000)) { case CCV_32S: { for_block(_ccv_get_8u_value , _ccv_get_32s_value); break; } case CCV_32F: { for_block(_ccv_get_8u_value , _ccv_get_32f_value); break; } case CCV_64S: { for_block(_ccv_get_8u_value , _ccv_get_64s_value); break; } case CCV_64F: { for_block(_ccv_get_8u_value , _ccv_get_64f_value); break; } default: { for_block(_ccv_get_8u_value , _ccv_get_8u_value); } } }; } } }; | |||
384 | #undef for_block | |||
385 | } else { | |||
386 | #define for_block(_for_get_d, _for_get_a) \ | |||
387 | for (i = 0; i < a->rows; i++) \ | |||
388 | { \ | |||
389 | for (j = 0; j < a->cols; j++) \ | |||
390 | { \ | |||
391 | float fdx = _for_get_d(dx_ptr, j * ch), fdy = _for_get_d(dy_ptr, j * ch); \ | |||
392 | float fdu = _for_get_d(du_ptr, j * ch), fdv = _for_get_d(dv_ptr, j * ch); \ | |||
393 | float adx = fabsf(fdx), ady = fabsf(fdy); \ | |||
394 | float adu = fabsf(fdu), adv = fabsf(fdv); \ | |||
395 | /* select the strongest ones from all the channels */ \ | |||
396 | for (k = 1; k < ch; k++) \ | |||
397 | { \ | |||
398 | if (fabsf((float)_for_get_d(dx_ptr, j * ch + k)) > adx) \ | |||
399 | { \ | |||
400 | fdx = _for_get_d(dx_ptr, j * ch + k); \ | |||
401 | adx = fabsf(fdx); \ | |||
402 | } \ | |||
403 | if (fabsf((float)_for_get_d(dy_ptr, j * ch + k)) > ady) \ | |||
404 | { \ | |||
405 | fdy = _for_get_d(dy_ptr, j * ch + k); \ | |||
406 | ady = fabsf(fdy); \ | |||
407 | } \ | |||
408 | if (fabsf((float)_for_get_d(du_ptr, j * ch + k)) > adu) \ | |||
409 | { \ | |||
410 | fdu = _for_get_d(du_ptr, j * ch + k); \ | |||
411 | adu = fabsf(fdu); \ | |||
412 | } \ | |||
413 | if (fabsf((float)_for_get_d(dv_ptr, j * ch + k)) > adv) \ | |||
414 | { \ | |||
415 | fdv = _for_get_d(dv_ptr, j * ch + k); \ | |||
416 | adv = fabsf(fdv); \ | |||
417 | } \ | |||
418 | } \ | |||
419 | dbp[0] = fdx, dbp[1] = fdy; \ | |||
420 | dbp[2] = fdu, dbp[3] = fdv; \ | |||
421 | dbp[4] = adx, dbp[5] = ady; \ | |||
422 | dbp[6] = adu, dbp[7] = adv; \ | |||
423 | _ccv_rgb_to_luv(_for_get_a(a_ptr, j * ch) / 255.0, \ | |||
424 | _for_get_a(a_ptr, j * ch + 1) / 255.0, \ | |||
425 | _for_get_a(a_ptr, j * ch + 2) / 255.0, \ | |||
426 | dbp + 8, dbp + 9, dbp + 10); \ | |||
427 | dbp += 11; \ | |||
428 | } \ | |||
429 | a_ptr += a->step; \ | |||
430 | dx_ptr += dx->step; \ | |||
431 | dy_ptr += dy->step; \ | |||
432 | du_ptr += du->step; \ | |||
433 | dv_ptr += dv->step; \ | |||
434 | } | |||
435 | ccv_matrix_getter(dx->type, ccv_matrix_getter_a, a->type, for_block){ switch (((dx->type) & 0xFF000)) { case CCV_32S: { { switch (((a->type) & 0xFF000)) { case CCV_32S: { for_block(_ccv_get_32s_value , _ccv_get_32s_value); break; } case CCV_32F: { for_block(_ccv_get_32s_value , _ccv_get_32f_value); break; } case CCV_64S: { for_block(_ccv_get_32s_value , _ccv_get_64s_value); break; } case CCV_64F: { for_block(_ccv_get_32s_value , _ccv_get_64f_value); break; } default: { for_block(_ccv_get_32s_value , _ccv_get_8u_value); } } }; break; } case CCV_32F: { { switch (((a->type) & 0xFF000)) { case CCV_32S: { for_block(_ccv_get_32f_value , _ccv_get_32s_value); break; } case CCV_32F: { for_block(_ccv_get_32f_value , _ccv_get_32f_value); break; } case CCV_64S: { for_block(_ccv_get_32f_value , _ccv_get_64s_value); break; } case CCV_64F: { for_block(_ccv_get_32f_value , _ccv_get_64f_value); break; } default: { for_block(_ccv_get_32f_value , _ccv_get_8u_value); } } }; break; } case CCV_64S: { { switch (((a->type) & 0xFF000)) { case CCV_32S: { for_block(_ccv_get_64s_value , _ccv_get_32s_value); break; } case CCV_32F: { for_block(_ccv_get_64s_value , _ccv_get_32f_value); break; } case CCV_64S: { for_block(_ccv_get_64s_value , _ccv_get_64s_value); break; } case CCV_64F: { for_block(_ccv_get_64s_value , _ccv_get_64f_value); break; } default: { for_block(_ccv_get_64s_value , _ccv_get_8u_value); } } }; break; } case CCV_64F: { { switch (((a->type) & 0xFF000)) { case CCV_32S: { for_block(_ccv_get_64f_value , _ccv_get_32s_value); break; } case CCV_32F: { for_block(_ccv_get_64f_value , _ccv_get_32f_value); break; } case CCV_64S: { for_block(_ccv_get_64f_value , _ccv_get_64s_value); break; } case CCV_64F: { for_block(_ccv_get_64f_value , _ccv_get_64f_value); break; } default: { for_block(_ccv_get_64f_value , _ccv_get_8u_value); } } }; break; } default: { { switch ((( a->type) & 0xFF000)) { case CCV_32S: { for_block(_ccv_get_8u_value , _ccv_get_32s_value); break; } case CCV_32F: { for_block(_ccv_get_8u_value , _ccv_get_32f_value); break; } case CCV_64S: { for_block(_ccv_get_8u_value , _ccv_get_64s_value); break; } case CCV_64F: { for_block(_ccv_get_8u_value , _ccv_get_64f_value); break; } default: { for_block(_ccv_get_8u_value , _ccv_get_8u_value); } } }; } } }; | |||
436 | #undef for_block | |||
437 | } | |||
438 | ccv_matrix_free(dx); | |||
439 | ccv_matrix_free(dy); | |||
440 | ccv_matrix_free(du); | |||
441 | ccv_matrix_free(dv); | |||
442 | } | |||
443 | ||||
444 | #if defined(HAVE_SSE21) | |||
445 | static inline void _ccv_scd_run_feature_at_sse2(float* at, int cols, ccv_scd_stump_feature_t* feature, __m128 surf[8]) | |||
446 | { | |||
447 | int i; | |||
448 | // extract feature | |||
449 | for (i = 0; i < 4; i++) | |||
450 | { | |||
451 | __m128 d0 = _mm_loadu_ps(at + (cols * feature->sy[i] + feature->sx[i]) * CCV_SCD_CHANNEL(11)); | |||
452 | __m128 d1 = _mm_loadu_ps(at + 4 + (cols * feature->sy[i] + feature->sx[i]) * CCV_SCD_CHANNEL(11)); | |||
453 | __m128 du0 = _mm_loadu_ps(at + (cols * feature->dy[i] + feature->sx[i]) * CCV_SCD_CHANNEL(11)); | |||
454 | __m128 du1 = _mm_loadu_ps(at + 4 + (cols * feature->dy[i] + feature->sx[i]) * CCV_SCD_CHANNEL(11)); | |||
455 | __m128 dv0 = _mm_loadu_ps(at + (cols * feature->sy[i] + feature->dx[i]) * CCV_SCD_CHANNEL(11)); | |||
456 | __m128 dv1 = _mm_loadu_ps(at + 4 + (cols * feature->sy[i] + feature->dx[i]) * CCV_SCD_CHANNEL(11)); | |||
457 | __m128 duv0 = _mm_loadu_ps(at + (cols * feature->dy[i] + feature->dx[i]) * CCV_SCD_CHANNEL(11)); | |||
458 | __m128 duv1 = _mm_loadu_ps(at + 4 + (cols * feature->dy[i] + feature->dx[i]) * CCV_SCD_CHANNEL(11)); | |||
459 | surf[i * 2] = _mm_sub_ps(_mm_add_ps(duv0, d0), _mm_add_ps(du0, dv0)); | |||
460 | surf[i * 2 + 1] = _mm_sub_ps(_mm_add_ps(duv1, d1), _mm_add_ps(du1, dv1)); | |||
461 | } | |||
462 | // L2Hys normalization | |||
463 | __m128 v0 = _mm_add_ps(_mm_mul_ps(surf[0], surf[0]), _mm_mul_ps(surf[1], surf[1])); | |||
464 | __m128 v1 = _mm_add_ps(_mm_mul_ps(surf[2], surf[2]), _mm_mul_ps(surf[3], surf[3])); | |||
465 | __m128 v2 = _mm_add_ps(_mm_mul_ps(surf[4], surf[4]), _mm_mul_ps(surf[5], surf[5])); | |||
466 | __m128 v3 = _mm_add_ps(_mm_mul_ps(surf[6], surf[6]), _mm_mul_ps(surf[7], surf[7])); | |||
467 | v0 = _mm_add_ps(v0, v1); | |||
468 | v2 = _mm_add_ps(v2, v3); | |||
469 | union { | |||
470 | float f[4]; | |||
471 | __m128 p; | |||
472 | } vx; | |||
473 | vx.p = _mm_add_ps(v0, v2); | |||
474 | v0 = _mm_set1_ps(1.0 / (sqrtf(vx.f[0] + vx.f[1] + vx.f[2] + vx.f[3]) + 1e-6)); | |||
475 | static float thlf = -2.0 / 5.65685424949; // -sqrtf(32) | |||
476 | static float thuf = 2.0 / 5.65685424949; // sqrtf(32) | |||
477 | const __m128 thl = _mm_set1_ps(thlf); | |||
478 | const __m128 thu = _mm_set1_ps(thuf); | |||
479 | for (i = 0; i < 8; i++) | |||
480 | { | |||
481 | surf[i] = _mm_mul_ps(surf[i], v0); | |||
482 | surf[i] = _mm_min_ps(surf[i], thu); | |||
483 | surf[i] = _mm_max_ps(surf[i], thl); | |||
484 | } | |||
485 | __m128 u0 = _mm_add_ps(_mm_mul_ps(surf[0], surf[0]), _mm_mul_ps(surf[1], surf[1])); | |||
486 | __m128 u1 = _mm_add_ps(_mm_mul_ps(surf[2], surf[2]), _mm_mul_ps(surf[3], surf[3])); | |||
487 | __m128 u2 = _mm_add_ps(_mm_mul_ps(surf[4], surf[4]), _mm_mul_ps(surf[5], surf[5])); | |||
488 | __m128 u3 = _mm_add_ps(_mm_mul_ps(surf[6], surf[6]), _mm_mul_ps(surf[7], surf[7])); | |||
489 | u0 = _mm_add_ps(u0, u1); | |||
490 | u2 = _mm_add_ps(u2, u3); | |||
491 | union { | |||
492 | float f[4]; | |||
493 | __m128 p; | |||
494 | } ux; | |||
495 | ux.p = _mm_add_ps(u0, u2); | |||
496 | u0 = _mm_set1_ps(1.0 / (sqrtf(ux.f[0] + ux.f[1] + ux.f[2] + ux.f[3]) + 1e-6)); | |||
497 | for (i = 0; i < 8; i++) | |||
498 | surf[i] = _mm_mul_ps(surf[i], u0); | |||
499 | } | |||
500 | #else | |||
501 | static inline void _ccv_scd_run_feature_at(float* at, int cols, ccv_scd_stump_feature_t* feature, float surf[32]) | |||
502 | { | |||
503 | int i, j; | |||
504 | // extract feature | |||
505 | for (i = 0; i < 4; i++) | |||
506 | { | |||
507 | float* d = at + (cols * feature->sy[i] + feature->sx[i]) * CCV_SCD_CHANNEL(11); | |||
508 | float* du = at + (cols * feature->dy[i] + feature->sx[i]) * CCV_SCD_CHANNEL(11); | |||
509 | float* dv = at + (cols * feature->sy[i] + feature->dx[i]) * CCV_SCD_CHANNEL(11); | |||
510 | float* duv = at + (cols * feature->dy[i] + feature->dx[i]) * CCV_SCD_CHANNEL(11); | |||
511 | for (j = 0; j < 8; j++) | |||
512 | surf[i * 8 + j] = duv[j] - du[j] + d[j] - dv[j]; | |||
513 | } | |||
514 | // L2Hys normalization | |||
515 | float v = 0; | |||
516 | for (i = 0; i < 32; i++) | |||
517 | v += surf[i] * surf[i]; | |||
518 | v = 1.0 / (sqrtf(v) + 1e-6); | |||
519 | static float theta = 2.0 / 5.65685424949; // sqrtf(32) | |||
520 | float u = 0; | |||
521 | for (i = 0; i < 32; i++) | |||
522 | { | |||
523 | surf[i] = surf[i] * v; | |||
524 | surf[i] = ccv_clamp(surf[i], -theta, theta)({ typeof (-theta) _a = (-theta); typeof (theta) _b = (theta) ; typeof (surf[i]) _x = (surf[i]); (_x < _a) ? _a : ((_x > _b) ? _b : _x); }); | |||
525 | u += surf[i] * surf[i]; | |||
526 | } | |||
527 | u = 1.0 / (sqrtf(u) + 1e-6); | |||
528 | for (i = 0; i < 32; i++) | |||
529 | surf[i] = surf[i] * u; | |||
530 | } | |||
531 | #endif | |||
532 | ||||
533 | #ifdef HAVE_GSL1 | |||
534 | static ccv_array_t* _ccv_scd_collect_negatives(gsl_rng* rng, ccv_size_t size, ccv_array_t* hard_mine, int total, int grayscale) | |||
535 | { | |||
536 | ccv_array_t* negatives = ccv_array_new(ccv_compute_dense_matrix_size(size.height, size.width, CCV_8U | (grayscale ? CCV_C1 : CCV_C3))(((sizeof(ccv_dense_matrix_t) + 63) & -64) + (((size.width ) * _ccv_get_data_type_size[((CCV_8U | (grayscale ? CCV_C1 : CCV_C3 )) & 0xFF000) >> 12] * ((CCV_8U | (grayscale ? CCV_C1 : CCV_C3)) & 0xFFF) + 3) & -4) * (size.height)), total, 0); | |||
537 | int i, j, k; | |||
538 | for (i = 0; i < total;) | |||
539 | { | |||
540 | FLUSH(CCV_CLI_INFO, " - collect negatives %d%% (%d / %d)", (i + 1) * 100 / total, i + 1, total)do { if ((CCV_CLI_INFO & ccv_cli_get_output_levels())) { for (_CCV_PRINT_LOOP = 0; _CCV_PRINT_LOOP < _CCV_PRINT_COUNT; _CCV_PRINT_LOOP++) printf("\b"); for (_CCV_PRINT_LOOP = 0; _CCV_PRINT_LOOP < _CCV_PRINT_COUNT; _CCV_PRINT_LOOP++) printf(" "); for ( _CCV_PRINT_LOOP = 0; _CCV_PRINT_LOOP < _CCV_PRINT_COUNT; _CCV_PRINT_LOOP ++) printf("\b"); _CCV_PRINT_COUNT = printf(" - collect negatives %d%% (%d / %d)" , (i + 1) * 100 / total, i + 1, total); fflush(stdout); } } while (0); | |||
541 | double ratio = (double)(total - i) / hard_mine->rnum; | |||
542 | for (j = 0; j < hard_mine->rnum && i < total; j++) | |||
543 | { | |||
544 | ccv_file_info_t* file_info = (ccv_file_info_t*)ccv_array_get(hard_mine, j)((void*)(((char*)((hard_mine)->data)) + (size_t)(hard_mine )->rsize * (size_t)(j))); | |||
545 | ccv_dense_matrix_t* image = 0; | |||
546 | ccv_read(file_info->filename, &image, CCV_IO_ANY_FILE | (grayscale ? CCV_IO_GRAY : CCV_IO_RGB_COLOR))ccv_read_impl(file_info->filename, &image, CCV_IO_ANY_FILE | (grayscale ? CCV_IO_GRAY : CCV_IO_RGB_COLOR), 0, 0, 0); | |||
547 | if (image == 0) | |||
548 | { | |||
549 | PRINT(CCV_CLI_ERROR, "\n - %s: cannot be open, possibly corrupted\n", file_info->filename)do { if ((CCV_CLI_ERROR & ccv_cli_get_output_levels())) { printf("\n - %s: cannot be open, possibly corrupted\n", file_info ->filename); fflush(stdout); } } while (0); | |||
550 | continue; | |||
551 | } | |||
552 | double max_scale_ratio = ccv_min((double)image->rows / size.height, (double)image->cols / size.width)({ typeof ((double)image->rows / size.height) _a = ((double )image->rows / size.height); typeof ((double)image->cols / size.width) _b = ((double)image->cols / size.width); (_a < _b) ? _a : _b; }); | |||
553 | if (max_scale_ratio <= 0.5) // too small to be interesting | |||
554 | continue; | |||
555 | for (k = 0; k < ratio; k++) | |||
556 | if (k < (int)ratio || gsl_rng_uniform(rng) <= ccv_max(0.1, ratio - (int)ratio)({ typeof (0.1) _a = (0.1); typeof (ratio - (int)ratio) _b = ( ratio - (int)ratio); (_a > _b) ? _a : _b; })) | |||
557 | { | |||
558 | FLUSH(CCV_CLI_INFO, " - collect negatives %d%% (%d / %d)", (i + 1) * 100 / total, i + 1, total)do { if ((CCV_CLI_INFO & ccv_cli_get_output_levels())) { for (_CCV_PRINT_LOOP = 0; _CCV_PRINT_LOOP < _CCV_PRINT_COUNT; _CCV_PRINT_LOOP++) printf("\b"); for (_CCV_PRINT_LOOP = 0; _CCV_PRINT_LOOP < _CCV_PRINT_COUNT; _CCV_PRINT_LOOP++) printf(" "); for ( _CCV_PRINT_LOOP = 0; _CCV_PRINT_LOOP < _CCV_PRINT_COUNT; _CCV_PRINT_LOOP ++) printf("\b"); _CCV_PRINT_COUNT = printf(" - collect negatives %d%% (%d / %d)" , (i + 1) * 100 / total, i + 1, total); fflush(stdout); } } while (0); | |||
559 | ccv_rect_t rect; | |||
560 | double scale_ratio = gsl_rng_uniform(rng) * (max_scale_ratio - 0.5) + 0.5; | |||
561 | rect.width = ccv_min(image->cols, (int)(size.width * scale_ratio + 0.5))({ typeof (image->cols) _a = (image->cols); typeof ((int )(size.width * scale_ratio + 0.5)) _b = ((int)(size.width * scale_ratio + 0.5)); (_a < _b) ? _a : _b; }); | |||
562 | rect.height = ccv_min(image->rows, (int)(size.height * scale_ratio + 0.5))({ typeof (image->rows) _a = (image->rows); typeof ((int )(size.height * scale_ratio + 0.5)) _b = ((int)(size.height * scale_ratio + 0.5)); (_a < _b) ? _a : _b; }); | |||
563 | rect.x = gsl_rng_uniform_int(rng, ccv_max(image->cols - rect.width + 1, 1)({ typeof (image->cols - rect.width + 1) _a = (image->cols - rect.width + 1); typeof (1) _b = (1); (_a > _b) ? _a : _b ; })); | |||
564 | rect.y = gsl_rng_uniform_int(rng, ccv_max(image->rows - rect.height + 1, 1)({ typeof (image->rows - rect.height + 1) _a = (image-> rows - rect.height + 1); typeof (1) _b = (1); (_a > _b) ? _a : _b; })); | |||
565 | ccv_dense_matrix_t* sliced = 0; | |||
566 | ccv_slice(image, (ccv_matrix_t**)&sliced, 0, rect.y, rect.x, rect.height, rect.width); | |||
567 | ccv_dense_matrix_t* b = 0; | |||
568 | if (size.width > rect.width) | |||
569 | ccv_resample(sliced, &b, 0, (double)size.height / (double)sliced->rows, (double)size.width / (double)sliced->cols, CCV_INTER_CUBIC); | |||
570 | else | |||
571 | ccv_resample(sliced, &b, 0, (double)size.height / (double)sliced->rows, (double)size.width / (double)sliced->cols, CCV_INTER_AREA); | |||
572 | ccv_matrix_free(sliced); | |||
573 | b->sig = 0; | |||
574 | // this leveraged the fact that because I know the ccv_dense_matrix_t is continuous in memory | |||
575 | ccv_array_push(negatives, b); | |||
576 | ccv_matrix_free(b); | |||
577 | ++i; | |||
578 | if (i >= total) | |||
579 | break; | |||
580 | } | |||
581 | ccv_matrix_free(image); | |||
582 | } | |||
583 | } | |||
584 | PRINT(CCV_CLI_INFO, "\n")do { if ((CCV_CLI_INFO & ccv_cli_get_output_levels())) { printf ("\n"); fflush(stdout); } } while (0); | |||
585 | ccv_make_array_immutable(negatives); | |||
586 | return negatives; | |||
587 | } | |||
588 | ||||
589 | static ccv_array_t*_ccv_scd_collect_positives(ccv_size_t size, ccv_array_t* posfiles, int grayscale) | |||
590 | { | |||
591 | ccv_array_t* positives = ccv_array_new(ccv_compute_dense_matrix_size(size.height, size.width, CCV_8U | (grayscale ? CCV_C1 : CCV_C3))(((sizeof(ccv_dense_matrix_t) + 63) & -64) + (((size.width ) * _ccv_get_data_type_size[((CCV_8U | (grayscale ? CCV_C1 : CCV_C3 )) & 0xFF000) >> 12] * ((CCV_8U | (grayscale ? CCV_C1 : CCV_C3)) & 0xFFF) + 3) & -4) * (size.height)), posfiles->rnum, 0); | |||
592 | int i; | |||
593 | for (i = 0; i < posfiles->rnum; i++) | |||
594 | { | |||
595 | FLUSH(CCV_CLI_INFO, " - collect positives %d%% (%d / %d)", (i + 1) * 100 / posfiles->rnum, i + 1, posfiles->rnum)do { if ((CCV_CLI_INFO & ccv_cli_get_output_levels())) { for (_CCV_PRINT_LOOP = 0; _CCV_PRINT_LOOP < _CCV_PRINT_COUNT; _CCV_PRINT_LOOP++) printf("\b"); for (_CCV_PRINT_LOOP = 0; _CCV_PRINT_LOOP < _CCV_PRINT_COUNT; _CCV_PRINT_LOOP++) printf(" "); for ( _CCV_PRINT_LOOP = 0; _CCV_PRINT_LOOP < _CCV_PRINT_COUNT; _CCV_PRINT_LOOP ++) printf("\b"); _CCV_PRINT_COUNT = printf(" - collect positives %d%% (%d / %d)" , (i + 1) * 100 / posfiles->rnum, i + 1, posfiles->rnum ); fflush(stdout); } } while (0); | |||
596 | ccv_file_info_t* file_info = (ccv_file_info_t*)ccv_array_get(posfiles, i)((void*)(((char*)((posfiles)->data)) + (size_t)(posfiles)-> rsize * (size_t)(i))); | |||
597 | ccv_dense_matrix_t* a = 0; | |||
598 | ccv_read(file_info->filename, &a, CCV_IO_ANY_FILE | (grayscale ? CCV_IO_GRAY : CCV_IO_RGB_COLOR))ccv_read_impl(file_info->filename, &a, CCV_IO_ANY_FILE | (grayscale ? CCV_IO_GRAY : CCV_IO_RGB_COLOR), 0, 0, 0); | |||
599 | a->sig = 0; | |||
600 | ccv_array_push(positives, a); | |||
601 | ccv_matrix_free(a); | |||
602 | } | |||
603 | PRINT(CCV_CLI_INFO, "\n")do { if ((CCV_CLI_INFO & ccv_cli_get_output_levels())) { printf ("\n"); fflush(stdout); } } while (0); | |||
604 | ccv_make_array_immutable(positives); | |||
605 | return positives; | |||
606 | } | |||
607 | ||||
608 | static ccv_array_t* _ccv_scd_stump_features(ccv_size_t base, int range_through, int step_through, ccv_size_t size) | |||
609 | { | |||
610 | ccv_array_t* features = ccv_array_new(sizeof(ccv_scd_stump_feature_t), 64, 0); | |||
611 | int x, y, w, h; | |||
612 | for (w = base.width; w <= size.width; w += range_through) | |||
613 | if (w % 4 == 0) // only allow 4:1 | |||
614 | { | |||
615 | h = w / 4; | |||
616 | for (x = 0; x <= size.width - w; x += step_through) | |||
617 | for (y = 0; y <= size.height - h; y += step_through) | |||
618 | { | |||
619 | // 4x1 feature | |||
620 | ccv_scd_stump_feature_t feature; | |||
621 | feature.sx[0] = x; | |||
622 | feature.dx[0] = x + (w / 4); | |||
623 | feature.sx[1] = x + (w / 4); | |||
624 | feature.dx[1] = x + 2 * (w / 4); | |||
625 | feature.sx[2] = x + 2 * (w / 4); | |||
626 | feature.dx[2] = x + 3 * (w / 4); | |||
627 | feature.sx[3] = x + 3 * (w / 4); | |||
628 | feature.dx[3] = x + w; | |||
629 | feature.sy[0] = feature.sy[1] = feature.sy[2] = feature.sy[3] = y; | |||
630 | feature.dy[0] = feature.dy[1] = feature.dy[2] = feature.dy[3] = y + h; | |||
631 | ccv_array_push(features, &feature); | |||
632 | } | |||
633 | } | |||
634 | for (h = base.height; h <= size.height; h += range_through) | |||
635 | if (h % 4 == 0) // only allow 1:4 | |||
636 | { | |||
637 | w = h / 4; | |||
638 | for (x = 0; x <= size.width - w; x += step_through) | |||
639 | for (y = 0; y <= size.height - h; y += step_through) | |||
640 | { | |||
641 | // 1x4 feature | |||
642 | ccv_scd_stump_feature_t feature; | |||
643 | feature.sx[0] = feature.sx[1] = feature.sx[2] = feature.sx[3] = x; | |||
644 | feature.dx[0] = feature.dx[1] = feature.dx[2] = feature.dx[3] = x + w; | |||
645 | feature.sy[0] = y; | |||
646 | feature.dy[0] = y + (h / 4); | |||
647 | feature.sy[1] = y + (h / 4); | |||
648 | feature.dy[1] = y + 2 * (h / 4); | |||
649 | feature.sy[2] = y + 2 * (h / 4); | |||
650 | feature.dy[2] = y + 3 * (h / 4); | |||
651 | feature.sy[3] = y + 3 * (h / 4); | |||
652 | feature.dy[3] = y + h; | |||
653 | ccv_array_push(features, &feature); | |||
654 | } | |||
655 | } | |||
656 | for (w = base.width; w <= size.width; w += range_through) | |||
657 | for (h = base.height; h <= size.height; h += range_through) | |||
658 | for (x = 0; x <= size.width - w; x += step_through) | |||
659 | for (y = 0; y <= size.height - h; y += step_through) | |||
660 | if (w % 2 == 0 && h % 2 == 0 && | |||
661 | (w == h || w == h * 2 || w * 2 == h || w * 2 == h * 3 || w * 3 == h * 2)) // allow 1:1, 1:2, 2:1, 2:3, 3:2 | |||
662 | { | |||
663 | // 2x2 feature | |||
664 | ccv_scd_stump_feature_t feature; | |||
665 | feature.sx[0] = feature.sx[1] = x; | |||
666 | feature.dx[0] = feature.dx[1] = x + (w / 2); | |||
667 | feature.sy[0] = feature.sy[2] = y; | |||
668 | feature.dy[0] = feature.dy[2] = y + (h / 2); | |||
669 | feature.sx[2] = feature.sx[3] = x + (w / 2); | |||
670 | feature.dx[2] = feature.dx[3] = x + w; | |||
671 | feature.sy[1] = feature.sy[3] = y + (h / 2); | |||
672 | feature.dy[1] = feature.dy[3] = y + h; | |||
673 | ccv_array_push(features, &feature); | |||
674 | } | |||
675 | return features; | |||
676 | } | |||
677 | ||||
678 | typedef struct { | |||
679 | double value; | |||
680 | int index; | |||
681 | } ccv_scd_value_index_t; | |||
682 | ||||
683 | #define more_than(s1, s2, aux) ((s1).value >= (s2).value) | |||
684 | static CCV_IMPLEMENT_QSORT(_ccv_scd_value_index_sortby_value, ccv_scd_value_index_t, more_than)void _ccv_scd_value_index_sortby_value(ccv_scd_value_index_t * array, size_t total, int aux) { int isort_thresh = 7; ccv_scd_value_index_t t; int sp = 0; struct { ccv_scd_value_index_t *lb; ccv_scd_value_index_t *ub; } stack[48]; if( total <= 1 ) return; stack[0].lb = array ; stack[0].ub = array + (total - 1); while( sp >= 0 ) { ccv_scd_value_index_t * left = stack[sp].lb; ccv_scd_value_index_t* right = stack[sp --].ub; for(;;) { int i, n = (int)(right - left) + 1, m; ccv_scd_value_index_t * ptr; ccv_scd_value_index_t* ptr2; if( n <= isort_thresh ) { insert_sort: for( ptr = left + 1; ptr <= right; ptr++ ) { for( ptr2 = ptr; ptr2 > left && more_than(ptr2[ 0],ptr2[-1], aux); ptr2--) (((t)) = ((ptr2[0])), ((ptr2[0])) = ((ptr2[-1])), ((ptr2[-1])) = ((t))); } break; } else { ccv_scd_value_index_t * left0; ccv_scd_value_index_t* left1; ccv_scd_value_index_t* right0; ccv_scd_value_index_t* right1; ccv_scd_value_index_t * pivot; ccv_scd_value_index_t* a; ccv_scd_value_index_t* b; ccv_scd_value_index_t * c; int swap_cnt = 0; left0 = left; right0 = right; pivot = left + (n/2); if( n > 40 ) { int d = n / 8; a = left, b = left + d, c = left + 2*d; left = more_than(*a, *b, aux) ? (more_than (*b, *c, aux) ? b : (more_than(*a, *c, aux) ? c : a)) : (more_than (*c, *b, aux) ? b : (more_than(*a, *c, aux) ? a : c)); a = pivot - d, b = pivot, c = pivot + d; pivot = more_than(*a, *b, aux ) ? (more_than(*b, *c, aux) ? b : (more_than(*a, *c, aux) ? c : a)) : (more_than(*c, *b, aux) ? b : (more_than(*a, *c, aux ) ? a : c)); a = right - 2*d, b = right - d, c = right; right = more_than(*a, *b, aux) ? (more_than(*b, *c, aux) ? b : (more_than (*a, *c, aux) ? c : a)) : (more_than(*c, *b, aux) ? b : (more_than (*a, *c, aux) ? a : c)); } a = left, b = pivot, c = right; pivot = more_than(*a, *b, aux) ? (more_than(*b, *c, aux) ? b : (more_than (*a, *c, aux) ? c : a)) : (more_than(*c, *b, aux) ? b : (more_than (*a, *c, aux) ? a : c)); if( pivot != left0 ) { (((t)) = ((*pivot )), ((*pivot)) = ((*left0)), ((*left0)) = ((t))); pivot = left0 ; } left = left1 = left0 + 1; right = right1 = right0; for(;; ) { while( left <= right && !more_than(*pivot, *left , aux) ) { if( !more_than(*left, *pivot, aux) ) { if( left > left1 ) (((t)) = ((*left1)), ((*left1)) = ((*left)), ((*left )) = ((t))); swap_cnt = 1; left1++; } left++; } while( left <= right && !more_than(*right, *pivot, aux) ) { if( !more_than (*pivot, *right, aux) ) { if( right < right1 ) (((t)) = (( *right1)), ((*right1)) = ((*right)), ((*right)) = ((t))); swap_cnt = 1; right1--; } right--; } if( left > right ) break; ((( t)) = ((*left)), ((*left)) = ((*right)), ((*right)) = ((t))); swap_cnt = 1; left++; right--; } if( swap_cnt == 0 ) { left = left0, right = right0; goto insert_sort; } n = ({ typeof ((int )(left1 - left0)) _a = ((int)(left1 - left0)); typeof ((int)( left - left1)) _b = ((int)(left - left1)); (_a < _b) ? _a : _b; }); for( i = 0; i < n; i++ ) (((t)) = ((left0[i])), ( (left0[i])) = ((left[i-n])), ((left[i-n])) = ((t))); n = ({ typeof ((int)(right0 - right1)) _a = ((int)(right0 - right1)); typeof ((int)(right1 - right)) _b = ((int)(right1 - right)); (_a < _b) ? _a : _b; }); for( i = 0; i < n; i++ ) (((t)) = ((left [i])), ((left[i])) = ((right0[i-n+1])), ((right0[i-n+1])) = ( (t))); n = (int)(left - left1); m = (int)(right1 - right); if ( n > 1 ) { if( m > 1 ) { if( n > m ) { stack[++sp]. lb = left0; stack[sp].ub = left0 + n - 1; left = right0 - m + 1, right = right0; } else { stack[++sp].lb = right0 - m + 1; stack[sp].ub = right0; left = left0, right = left0 + n - 1; } } else left = left0, right = left0 + n - 1; } else if( m > 1 ) left = right0 - m + 1, right = right0; else break; } } } } | |||
685 | #undef more_than | |||
686 | #define less_than(s1, s2, aux) ((s1).index < (s2).index) | |||
687 | static CCV_IMPLEMENT_QSORT(_ccv_scd_value_index_sortby_index, ccv_scd_value_index_t, less_than)void _ccv_scd_value_index_sortby_index(ccv_scd_value_index_t * array, size_t total, int aux) { int isort_thresh = 7; ccv_scd_value_index_t t; int sp = 0; struct { ccv_scd_value_index_t *lb; ccv_scd_value_index_t *ub; } stack[48]; if( total <= 1 ) return; stack[0].lb = array ; stack[0].ub = array + (total - 1); while( sp >= 0 ) { ccv_scd_value_index_t * left = stack[sp].lb; ccv_scd_value_index_t* right = stack[sp --].ub; for(;;) { int i, n = (int)(right - left) + 1, m; ccv_scd_value_index_t * ptr; ccv_scd_value_index_t* ptr2; if( n <= isort_thresh ) { insert_sort: for( ptr = left + 1; ptr <= right; ptr++ ) { for( ptr2 = ptr; ptr2 > left && less_than(ptr2[ 0],ptr2[-1], aux); ptr2--) (((t)) = ((ptr2[0])), ((ptr2[0])) = ((ptr2[-1])), ((ptr2[-1])) = ((t))); } break; } else { ccv_scd_value_index_t * left0; ccv_scd_value_index_t* left1; ccv_scd_value_index_t* right0; ccv_scd_value_index_t* right1; ccv_scd_value_index_t * pivot; ccv_scd_value_index_t* a; ccv_scd_value_index_t* b; ccv_scd_value_index_t * c; int swap_cnt = 0; left0 = left; right0 = right; pivot = left + (n/2); if( n > 40 ) { int d = n / 8; a = left, b = left + d, c = left + 2*d; left = less_than(*a, *b, aux) ? (less_than (*b, *c, aux) ? b : (less_than(*a, *c, aux) ? c : a)) : (less_than (*c, *b, aux) ? b : (less_than(*a, *c, aux) ? a : c)); a = pivot - d, b = pivot, c = pivot + d; pivot = less_than(*a, *b, aux ) ? (less_than(*b, *c, aux) ? b : (less_than(*a, *c, aux) ? c : a)) : (less_than(*c, *b, aux) ? b : (less_than(*a, *c, aux ) ? a : c)); a = right - 2*d, b = right - d, c = right; right = less_than(*a, *b, aux) ? (less_than(*b, *c, aux) ? b : (less_than (*a, *c, aux) ? c : a)) : (less_than(*c, *b, aux) ? b : (less_than (*a, *c, aux) ? a : c)); } a = left, b = pivot, c = right; pivot = less_than(*a, *b, aux) ? (less_than(*b, *c, aux) ? b : (less_than (*a, *c, aux) ? c : a)) : (less_than(*c, *b, aux) ? b : (less_than (*a, *c, aux) ? a : c)); if( pivot != left0 ) { (((t)) = ((*pivot )), ((*pivot)) = ((*left0)), ((*left0)) = ((t))); pivot = left0 ; } left = left1 = left0 + 1; right = right1 = right0; for(;; ) { while( left <= right && !less_than(*pivot, *left , aux) ) { if( !less_than(*left, *pivot, aux) ) { if( left > left1 ) (((t)) = ((*left1)), ((*left1)) = ((*left)), ((*left )) = ((t))); swap_cnt = 1; left1++; } left++; } while( left <= right && !less_than(*right, *pivot, aux) ) { if( !less_than (*pivot, *right, aux) ) { if( right < right1 ) (((t)) = (( *right1)), ((*right1)) = ((*right)), ((*right)) = ((t))); swap_cnt = 1; right1--; } right--; } if( left > right ) break; ((( t)) = ((*left)), ((*left)) = ((*right)), ((*right)) = ((t))); swap_cnt = 1; left++; right--; } if( swap_cnt == 0 ) { left = left0, right = right0; goto insert_sort; } n = ({ typeof ((int )(left1 - left0)) _a = ((int)(left1 - left0)); typeof ((int)( left - left1)) _b = ((int)(left - left1)); (_a < _b) ? _a : _b; }); for( i = 0; i < n; i++ ) (((t)) = ((left0[i])), ( (left0[i])) = ((left[i-n])), ((left[i-n])) = ((t))); n = ({ typeof ((int)(right0 - right1)) _a = ((int)(right0 - right1)); typeof ((int)(right1 - right)) _b = ((int)(right1 - right)); (_a < _b) ? _a : _b; }); for( i = 0; i < n; i++ ) (((t)) = ((left [i])), ((left[i])) = ((right0[i-n+1])), ((right0[i-n+1])) = ( (t))); n = (int)(left - left1); m = (int)(right1 - right); if ( n > 1 ) { if( m > 1 ) { if( n > m ) { stack[++sp]. lb = left0; stack[sp].ub = left0 + n - 1; left = right0 - m + 1, right = right0; } else { stack[++sp].lb = right0 - m + 1; stack[sp].ub = right0; left = left0, right = left0 + n - 1; } } else left = left0, right = left0 + n - 1; } else if( m > 1 ) left = right0 - m + 1, right = right0; else break; } } } } | |||
688 | #undef less_than | |||
689 | ||||
690 | static float* _ccv_scd_get_surf_at(float* fv, int feature_no, int example_no, int positive_count, int negative_count) | |||
691 | { | |||
692 | return fv + ((off_t)example_no + feature_no * (positive_count + negative_count)) * 32; | |||
693 | } | |||
694 | ||||
695 | static void _ccv_scd_precompute_feature_vectors(const ccv_array_t* features, const ccv_array_t* positives, const ccv_array_t* negatives, float* fv) | |||
696 | { | |||
697 | parallel_for(i, positives->rnum){ int i; for ((i) = 0; (i) < (positives->rnum); (i)++) { { | |||
698 | int j; | |||
699 | if ((i + 1) % 4031 == 1) | |||
700 | FLUSH(CCV_CLI_INFO, " - precompute feature vectors of example %d / %d over %d features", (int)(i + 1), positives->rnum + negatives->rnum, features->rnum)do { if ((CCV_CLI_INFO & ccv_cli_get_output_levels())) { for (_CCV_PRINT_LOOP = 0; _CCV_PRINT_LOOP < _CCV_PRINT_COUNT; _CCV_PRINT_LOOP++) printf("\b"); for (_CCV_PRINT_LOOP = 0; _CCV_PRINT_LOOP < _CCV_PRINT_COUNT; _CCV_PRINT_LOOP++) printf(" "); for ( _CCV_PRINT_LOOP = 0; _CCV_PRINT_LOOP < _CCV_PRINT_COUNT; _CCV_PRINT_LOOP ++) printf("\b"); _CCV_PRINT_COUNT = printf(" - precompute feature vectors of example %d / %d over %d features" , (int)(i + 1), positives->rnum + negatives->rnum, features ->rnum); fflush(stdout); } } while (0); | |||
701 | ccv_dense_matrix_t* a = (ccv_dense_matrix_t*)ccv_array_get(positives, i)((void*)(((char*)((positives)->data)) + (size_t)(positives )->rsize * (size_t)(i))); | |||
702 | a->data.u8 = (unsigned char*)(a + 1); | |||
703 | ccv_dense_matrix_t* b = 0; | |||
704 | ccv_scd(a, &b, 0); | |||
705 | ccv_dense_matrix_t* sat = 0; | |||
706 | ccv_sat(b, &sat, 0, CCV_PADDING_ZERO); | |||
707 | ccv_matrix_free(b); | |||
708 | for (j = 0; j < features->rnum; j++) | |||
709 | { | |||
710 | ccv_scd_stump_feature_t* feature = (ccv_scd_stump_feature_t*)ccv_array_get(features, j)((void*)(((char*)((features)->data)) + (size_t)(features)-> rsize * (size_t)(j))); | |||
711 | // save to fv | |||
712 | #if defined(HAVE_SSE21) | |||
713 | _ccv_scd_run_feature_at_sse2(sat->data.f32, sat->cols, feature, (__m128*)_ccv_scd_get_surf_at(fv, j, i, positives->rnum, negatives->rnum)); | |||
714 | #else | |||
715 | _ccv_scd_run_feature_at(sat->data.f32, sat->cols, feature, _ccv_scd_get_surf_at(fv, j, i, positives->rnum, negatives->rnum)); | |||
716 | #endif | |||
717 | } | |||
718 | ccv_matrix_free(sat); | |||
719 | } parallel_endfor} } | |||
720 | parallel_for(i, negatives->rnum){ int i; for ((i) = 0; (i) < (negatives->rnum); (i)++) { { | |||
721 | int j; | |||
722 | if ((i + 1) % 731 == 1 || (i + 1) == negatives->rnum) | |||
723 | FLUSH(CCV_CLI_INFO, " - precompute feature vectors of example %d / %d over %d features", (int)(i + positives->rnum + 1), positives->rnum + negatives->rnum, features->rnum)do { if ((CCV_CLI_INFO & ccv_cli_get_output_levels())) { for (_CCV_PRINT_LOOP = 0; _CCV_PRINT_LOOP < _CCV_PRINT_COUNT; _CCV_PRINT_LOOP++) printf("\b"); for (_CCV_PRINT_LOOP = 0; _CCV_PRINT_LOOP < _CCV_PRINT_COUNT; _CCV_PRINT_LOOP++) printf(" "); for ( _CCV_PRINT_LOOP = 0; _CCV_PRINT_LOOP < _CCV_PRINT_COUNT; _CCV_PRINT_LOOP ++) printf("\b"); _CCV_PRINT_COUNT = printf(" - precompute feature vectors of example %d / %d over %d features" , (int)(i + positives->rnum + 1), positives->rnum + negatives ->rnum, features->rnum); fflush(stdout); } } while (0); | |||
724 | ccv_dense_matrix_t* a = (ccv_dense_matrix_t*)ccv_array_get(negatives, i)((void*)(((char*)((negatives)->data)) + (size_t)(negatives )->rsize * (size_t)(i))); | |||
725 | a->data.u8 = (unsigned char*)(a + 1); | |||
726 | ccv_dense_matrix_t* b = 0; | |||
727 | ccv_scd(a, &b, 0); | |||
728 | ccv_dense_matrix_t* sat = 0; | |||
729 | ccv_sat(b, &sat, 0, CCV_PADDING_ZERO); | |||
730 | ccv_matrix_free(b); | |||
731 | for (j = 0; j < features->rnum; j++) | |||
732 | { | |||
733 | ccv_scd_stump_feature_t* feature = (ccv_scd_stump_feature_t*)ccv_array_get(features, j)((void*)(((char*)((features)->data)) + (size_t)(features)-> rsize * (size_t)(j))); | |||
734 | // save to fv | |||
735 | #if defined(HAVE_SSE21) | |||
736 | _ccv_scd_run_feature_at_sse2(sat->data.f32, sat->cols, feature, (__m128*)_ccv_scd_get_surf_at(fv, j, i + positives->rnum, positives->rnum, negatives->rnum)); | |||
737 | #else | |||
738 | _ccv_scd_run_feature_at(sat->data.f32, sat->cols, feature, _ccv_scd_get_surf_at(fv, j, i + positives->rnum, positives->rnum, negatives->rnum)); | |||
739 | #endif | |||
740 | } | |||
741 | ccv_matrix_free(sat); | |||
742 | } parallel_endfor} } | |||
743 | } | |||
744 | ||||
745 | typedef struct { | |||
746 | int feature_no; | |||
747 | double C; | |||
748 | ccv_scd_value_index_t* pwidx; | |||
749 | ccv_scd_value_index_t* nwidx; | |||
750 | int positive_count; | |||
751 | int negative_count; | |||
752 | int active_positive_count; | |||
753 | int active_negative_count; | |||
754 | float* fv; | |||
755 | } ccv_loss_minimize_context_t; | |||
756 | ||||
757 | static int _ccv_scd_stump_feature_gentle_adaboost_loss(const ccv_dense_matrix_t* x, double* f, ccv_dense_matrix_t* df, void* data) | |||
758 | { | |||
759 | ccv_loss_minimize_context_t* context = (ccv_loss_minimize_context_t*)data; | |||
760 | int i, j; | |||
761 | float loss = 0; | |||
762 | float* d = df->data.f32; | |||
763 | for (i = 0; i < 32; i++) | |||
764 | { | |||
765 | loss += context->C * fabs(x->data.f32[i]); | |||
766 | d[i] = x->data.f32[i] > 0 ? context->C : -context->C; | |||
767 | } | |||
768 | d[32] = 0; | |||
769 | float* surf = _ccv_scd_get_surf_at(context->fv, context->feature_no, 0, context->positive_count, context->negative_count); | |||
770 | for (i = 0; i < context->active_positive_count; i++) | |||
771 | { | |||
772 | float* cur_surf = surf + (off_t)(context->pwidx[i].index) * 32; | |||
773 | float v = x->data.f32[32]; | |||
774 | for (j = 0; j < 32; j++) | |||
775 | v += cur_surf[j] * x->data.f32[j]; | |||
776 | v = expf(v); | |||
777 | float tanh = (v - 1) / (v + 1); | |||
778 | loss += context->pwidx[i].value * (1.0 - tanh) * (1.0 - tanh); | |||
779 | float dv = -8.0 * context->pwidx[i].value * v / ((1.0 + v) * (1.0 + v) * (1.0 + v)); | |||
780 | for (j = 0; j < 32; j++) | |||
781 | d[j] += dv * cur_surf[j]; | |||
782 | d[32] += dv; | |||
783 | } | |||
784 | for (i = 0; i < context->active_negative_count; i++) | |||
785 | { | |||
786 | float* cur_surf = surf + (off_t)(context->nwidx[i].index + context->positive_count) * 32; | |||
787 | float v = x->data.f32[32]; | |||
788 | for (j = 0; j < 32; j++) | |||
789 | v += cur_surf[j] * x->data.f32[j]; | |||
790 | v = expf(v); | |||
791 | float tanh = (v - 1) / (v + 1); | |||
792 | loss += context->nwidx[i].value * (-1.0 - tanh) * (-1.0 - tanh); | |||
793 | float dv = 8.0 * context->nwidx[i].value * v * v / ((1.0 + v) * (1.0 + v) * (1.0 + v)); | |||
794 | for (j = 0; j < 32; j++) | |||
795 | d[j] += dv * cur_surf[j]; | |||
796 | d[32] += dv; | |||
797 | } | |||
798 | f[0] = loss; | |||
799 | return 0; | |||
800 | } | |||
801 | ||||
802 | static int _ccv_scd_weight_trimming(ccv_scd_value_index_t* idx, int count, double weight_trimming) | |||
803 | { | |||
804 | int active_count = count; | |||
805 | int i; | |||
806 | double w = 0; | |||
807 | for (i = 0; i < count; i++) | |||
808 | { | |||
809 | w += idx[i].value; | |||
810 | if (w >= weight_trimming) | |||
811 | { | |||
812 | active_count = i + 1; | |||
813 | break; | |||
814 | } | |||
815 | } | |||
816 | assert(active_count > 0)((void) sizeof ((active_count > 0) ? 1 : 0), __extension__ ({ if (active_count > 0) ; else __assert_fail ("active_count > 0" , "ccv_scd.c", 816, __extension__ __PRETTY_FUNCTION__); })); | |||
817 | for (i = active_count; i < count; i++) | |||
818 | if (idx[i - 1].value == idx[i].value) // for exactly the same weight, we skip | |||
819 | active_count = i + 1; | |||
820 | else | |||
821 | break; | |||
822 | return active_count; | |||
823 | } | |||
824 | ||||
825 | static void _ccv_scd_stump_feature_supervised_train(gsl_rng* rng, ccv_array_t* features, int positive_count, int negative_count, double* pw, double* nw, float* fv, double C, double weight_trimming) | |||
826 | { | |||
827 | int i; | |||
828 | ccv_scd_value_index_t* pwidx = (ccv_scd_value_index_t*)ccmallocmalloc(sizeof(ccv_scd_value_index_t) * positive_count); | |||
829 | ccv_scd_value_index_t* nwidx = (ccv_scd_value_index_t*)ccmallocmalloc(sizeof(ccv_scd_value_index_t) * negative_count); | |||
830 | for (i = 0; i < positive_count; i++) | |||
831 | pwidx[i].value = pw[i], pwidx[i].index = i; | |||
832 | for (i = 0; i < negative_count; i++) | |||
833 | nwidx[i].value = nw[i], nwidx[i].index = i; | |||
834 | _ccv_scd_value_index_sortby_value(pwidx, positive_count, 0); | |||
835 | _ccv_scd_value_index_sortby_value(nwidx, negative_count, 0); | |||
836 | int active_positive_count = _ccv_scd_weight_trimming(pwidx, positive_count, weight_trimming * 0.5); // the sum of positive weights is 0.5 | |||
837 | int active_negative_count = _ccv_scd_weight_trimming(nwidx, negative_count, weight_trimming * 0.5); // the sum of negative weights is 0.5 | |||
838 | _ccv_scd_value_index_sortby_index(pwidx, active_positive_count, 0); | |||
839 | _ccv_scd_value_index_sortby_index(nwidx, active_negative_count, 0); | |||
840 | parallel_for(i, features->rnum){ int i; for ((i) = 0; (i) < (features->rnum); (i)++) { { | |||
841 | if ((i + 1) % 31 == 1 || (i + 1) == features->rnum) | |||
842 | FLUSH(CCV_CLI_INFO, " - supervised train feature %d / %d with logistic regression, active set {%d, %d}", (int)(i + 1), features->rnum, active_positive_count, active_negative_count)do { if ((CCV_CLI_INFO & ccv_cli_get_output_levels())) { for (_CCV_PRINT_LOOP = 0; _CCV_PRINT_LOOP < _CCV_PRINT_COUNT; _CCV_PRINT_LOOP++) printf("\b"); for (_CCV_PRINT_LOOP = 0; _CCV_PRINT_LOOP < _CCV_PRINT_COUNT; _CCV_PRINT_LOOP++) printf(" "); for ( _CCV_PRINT_LOOP = 0; _CCV_PRINT_LOOP < _CCV_PRINT_COUNT; _CCV_PRINT_LOOP ++) printf("\b"); _CCV_PRINT_COUNT = printf(" - supervised train feature %d / %d with logistic regression, active set {%d, %d}" , (int)(i + 1), features->rnum, active_positive_count, active_negative_count ); fflush(stdout); } } while (0); | |||
843 | ccv_scd_stump_feature_t* feature = (ccv_scd_stump_feature_t*)ccv_array_get(features, i)((void*)(((char*)((features)->data)) + (size_t)(features)-> rsize * (size_t)(i))); | |||
844 | ccv_loss_minimize_context_t context = { | |||
845 | .feature_no = i, | |||
846 | .C = C, | |||
847 | .positive_count = positive_count, | |||
848 | .negative_count = negative_count, | |||
849 | .active_positive_count = active_positive_count, | |||
850 | .active_negative_count = active_negative_count, | |||
851 | .pwidx = pwidx, | |||
852 | .nwidx = nwidx, | |||
853 | .fv = fv, | |||
854 | }; | |||
855 | ccv_dense_matrix_t* x = ccv_dense_matrix_new(1, 33, CCV_32F | CCV_C1, 0, 0); | |||
856 | int j; | |||
857 | for (j = 0; j < 33; j++) | |||
858 | x->data.f32[j] = gsl_rng_uniform_pos(rng) * 2 - 1.0; | |||
859 | ccv_minimize(x, 10, 1.0, _ccv_scd_stump_feature_gentle_adaboost_loss, ccv_minimize_default_params, &context); | |||
860 | for (j = 0; j < 32; j++) | |||
861 | feature->w[j] = x->data.f32[j]; | |||
862 | feature->bias = x->data.f32[32]; | |||
863 | ccv_matrix_free(x); | |||
864 | } parallel_endfor} } | |||
865 | ccfreefree(pwidx); | |||
866 | ccfreefree(nwidx); | |||
867 | } | |||
868 | ||||
869 | static double _ccv_scd_auc(double* s, int posnum, int negnum) | |||
870 | { | |||
871 | ccv_scd_value_index_t* sidx = (ccv_scd_value_index_t*)ccmallocmalloc(sizeof(ccv_scd_value_index_t) * (posnum + negnum)); | |||
872 | int i; | |||
873 | for (i = 0; i < posnum + negnum; i++) | |||
874 | sidx[i].value = s[i], sidx[i].index = i; | |||
875 | _ccv_scd_value_index_sortby_value(sidx, posnum + negnum, 0); | |||
876 | int fp = 0, tp = 0, fp_prev = 0, tp_prev = 0; | |||
877 | double a = 0; | |||
878 | double f_prev = -DBL_MAX1.7976931348623157e+308; | |||
879 | for (i = 0; i < posnum + negnum; i++) | |||
880 | { | |||
881 | if (sidx[i].value != f_prev) | |||
882 | { | |||
883 | a += (double)(fp - fp_prev) * (tp + tp_prev) * 0.5; | |||
884 | f_prev = sidx[i].value; | |||
885 | fp_prev = fp; | |||
886 | tp_prev = tp; | |||
887 | } | |||
888 | if (sidx[i].index < posnum) | |||
889 | ++tp; | |||
890 | else | |||
891 | ++fp; | |||
892 | } | |||
893 | ccfreefree(sidx); | |||
894 | a += (double)(negnum - fp_prev) * (posnum + tp_prev) * 0.5; | |||
895 | return a / ((double)posnum * negnum); | |||
896 | } | |||
897 | ||||
898 | static int _ccv_scd_find_match_feature(ccv_scd_stump_feature_t* value, ccv_array_t* features) | |||
899 | { | |||
900 | int i; | |||
901 | for (i = 0; i < features->rnum; i++) | |||
902 | { | |||
903 | ccv_scd_stump_feature_t* feature = (ccv_scd_stump_feature_t*)ccv_array_get(features, i)((void*)(((char*)((features)->data)) + (size_t)(features)-> rsize * (size_t)(i))); | |||
904 | if (feature->sx[0] == value->sx[0] && feature->sy[0] == value->sy[0] && | |||
905 | feature->dx[0] == value->dx[0] && feature->dy[0] == value->dy[0] && | |||
906 | feature->sx[1] == value->sx[1] && feature->sy[1] == value->sy[1] && | |||
907 | feature->dx[1] == value->dx[1] && feature->dy[1] == value->dy[1] && | |||
908 | feature->sx[2] == value->sx[2] && feature->sy[2] == value->sy[2] && | |||
909 | feature->dx[2] == value->dx[2] && feature->dy[2] == value->dy[2] && | |||
910 | feature->sx[3] == value->sx[3] && feature->sy[3] == value->sy[3] && | |||
911 | feature->dx[3] == value->dx[3] && feature->dy[3] == value->dy[3]) | |||
912 | return i; | |||
913 | } | |||
914 | return -1; | |||
915 | } | |||
916 | ||||
917 | static int _ccv_scd_best_feature_gentle_adaboost(double* s, ccv_array_t* features, double* pw, double* nw, int positive_count, int negative_count, float* fv) | |||
918 | { | |||
919 | int i; | |||
920 | double* error_rate = (double*)cccalloccalloc(features->rnum, sizeof(double)); | |||
921 | assert(positive_count + negative_count > 0)((void) sizeof ((positive_count + negative_count > 0) ? 1 : 0), __extension__ ({ if (positive_count + negative_count > 0) ; else __assert_fail ("positive_count + negative_count > 0" , "ccv_scd.c", 921, __extension__ __PRETTY_FUNCTION__); })); | |||
922 | parallel_for(i, features->rnum){ int i; for ((i) = 0; (i) < (features->rnum); (i)++) { { | |||
923 | int j, k; | |||
924 | if ((i + 1) % 331 == 1 || (i + 1) == features->rnum) | |||
925 | FLUSH(CCV_CLI_INFO, " - go through %d / %d (%.1f%%) for adaboost", (int)(i + 1), features->rnum, (float)(i + 1) * 100 / features->rnum)do { if ((CCV_CLI_INFO & ccv_cli_get_output_levels())) { for (_CCV_PRINT_LOOP = 0; _CCV_PRINT_LOOP < _CCV_PRINT_COUNT; _CCV_PRINT_LOOP++) printf("\b"); for (_CCV_PRINT_LOOP = 0; _CCV_PRINT_LOOP < _CCV_PRINT_COUNT; _CCV_PRINT_LOOP++) printf(" "); for ( _CCV_PRINT_LOOP = 0; _CCV_PRINT_LOOP < _CCV_PRINT_COUNT; _CCV_PRINT_LOOP ++) printf("\b"); _CCV_PRINT_COUNT = printf(" - go through %d / %d (%.1f%%) for adaboost" , (int)(i + 1), features->rnum, (float)(i + 1) * 100 / features ->rnum); fflush(stdout); } } while (0); | |||
926 | ccv_scd_stump_feature_t* feature = (ccv_scd_stump_feature_t*)ccv_array_get(features, i)((void*)(((char*)((features)->data)) + (size_t)(features)-> rsize * (size_t)(i))); | |||
927 | for (j = 0; j < positive_count; j++) | |||
928 | { | |||
929 | float* surf = _ccv_scd_get_surf_at(fv, i, j, positive_count, negative_count); | |||
930 | float v = feature->bias; | |||
931 | for (k = 0; k < 32; k++) | |||
932 | v += surf[k] * feature->w[k]; | |||
933 | v = expf(v); | |||
934 | v = (v - 1) / (v + 1); // probability | |||
935 | error_rate[i] += pw[j] * (1 - v) * (1 - v); | |||
936 | } | |||
937 | for (j = 0; j < negative_count; j++) | |||
938 | { | |||
939 | float* surf = _ccv_scd_get_surf_at(fv, i, j + positive_count, positive_count, negative_count); | |||
940 | float v = feature->bias; | |||
941 | for (k = 0; k < 32; k++) | |||
942 | v += surf[k] * feature->w[k]; | |||
943 | v = expf(v); | |||
944 | v = (v - 1) / (v + 1); // probability | |||
945 | error_rate[i] += nw[j] * (-1 - v) * (-1 - v); | |||
946 | } | |||
947 | } parallel_endfor} } | |||
948 | double min_error_rate = error_rate[0]; | |||
949 | int j = 0; | |||
950 | for (i = 1; i < features->rnum; i++) | |||
951 | if (error_rate[i] < min_error_rate) | |||
952 | { | |||
953 | min_error_rate = error_rate[i]; | |||
954 | j = i; | |||
955 | } | |||
956 | ccfreefree(error_rate); | |||
957 | return j; | |||
958 | } | |||
959 | ||||
960 | static float _ccv_scd_threshold_at_hit_rate(double* s, int posnum, int negnum, float hit_rate, float* tp_out, float* fp_out) | |||
961 | { | |||
962 | ccv_scd_value_index_t* psidx = (ccv_scd_value_index_t*)ccmallocmalloc(sizeof(ccv_scd_value_index_t) * posnum); | |||
963 | int i; | |||
964 | for (i = 0; i < posnum; i++) | |||
965 | psidx[i].value = s[i], psidx[i].index = i; | |||
966 | _ccv_scd_value_index_sortby_value(psidx, posnum, 0); | |||
967 | float threshold = psidx[(int)((posnum - 0.5) * hit_rate - 0.5)].value - 1e-6; | |||
968 | ccfreefree(psidx); | |||
969 | int tp = 0; | |||
970 | for (i = 0; i < posnum; i++) | |||
971 | if (s[i] > threshold) | |||
972 | ++tp; | |||
973 | int fp = 0; | |||
974 | for (i = 0; i < negnum; i++) | |||
975 | if (s[i + posnum] > threshold) | |||
976 | ++fp; | |||
977 | if (tp_out) | |||
978 | *tp_out = (float)tp / posnum; | |||
979 | if (fp_out) | |||
980 | *fp_out = (float)fp / negnum; | |||
981 | return threshold; | |||
982 | } | |||
983 | ||||
984 | static int _ccv_scd_classifier_cascade_pass(ccv_scd_classifier_cascade_t* cascade, ccv_dense_matrix_t* a) | |||
985 | { | |||
986 | #if defined(HAVE_SSE21) | |||
987 | __m128 surf[8]; | |||
988 | #else | |||
989 | float surf[32]; | |||
990 | #endif | |||
991 | ccv_dense_matrix_t* b = 0; | |||
992 | ccv_scd(a, &b, 0); | |||
993 | ccv_dense_matrix_t* sat = 0; | |||
994 | ccv_sat(b, &sat, 0, CCV_PADDING_ZERO); | |||
995 | ccv_matrix_free(b); | |||
996 | int pass = 1; | |||
997 | int i, j; | |||
998 | for (i = 0; i < cascade->count; i++) | |||
999 | { | |||
1000 | ccv_scd_stump_classifier_t* classifier = cascade->classifiers + i; | |||
1001 | float v = 0; | |||
1002 | for (j = 0; j < classifier->count; j++) | |||
1003 | { | |||
1004 | ccv_scd_stump_feature_t* feature = classifier->features + j; | |||
1005 | #if defined(HAVE_SSE21) | |||
1006 | _ccv_scd_run_feature_at_sse2(sat->data.f32, sat->cols, feature, surf); | |||
1007 | __m128 u0 = _mm_add_ps(_mm_mul_ps(surf[0], _mm_loadu_ps(feature->w)), _mm_mul_ps(surf[1], _mm_loadu_ps(feature->w + 4))); | |||
1008 | __m128 u1 = _mm_add_ps(_mm_mul_ps(surf[2], _mm_loadu_ps(feature->w + 8)), _mm_mul_ps(surf[3], _mm_loadu_ps(feature->w + 12))); | |||
1009 | __m128 u2 = _mm_add_ps(_mm_mul_ps(surf[4], _mm_loadu_ps(feature->w + 16)), _mm_mul_ps(surf[5], _mm_loadu_ps(feature->w + 20))); | |||
1010 | __m128 u3 = _mm_add_ps(_mm_mul_ps(surf[6], _mm_loadu_ps(feature->w + 24)), _mm_mul_ps(surf[7], _mm_loadu_ps(feature->w + 28))); | |||
1011 | u0 = _mm_add_ps(u0, u1); | |||
1012 | u2 = _mm_add_ps(u2, u3); | |||
1013 | union { | |||
1014 | float f[4]; | |||
1015 | __m128 p; | |||
1016 | } ux; | |||
1017 | ux.p = _mm_add_ps(u0, u2); | |||
1018 | float u = expf(feature->bias + ux.f[0] + ux.f[1] + ux.f[2] + ux.f[3]); | |||
1019 | #else | |||
1020 | _ccv_scd_run_feature_at(sat->data.f32, sat->cols, feature, surf); | |||
1021 | float u = feature->bias; | |||
1022 | int k; | |||
1023 | for (k = 0; k < 32; k++) | |||
1024 | u += surf[k] * feature->w[k]; | |||
1025 | u = expf(u); | |||
1026 | #endif | |||
1027 | v += (u - 1) / (u + 1); | |||
1028 | } | |||
1029 | if (v <= classifier->threshold) | |||
1030 | { | |||
1031 | pass = 0; | |||
1032 | break; | |||
1033 | } | |||
1034 | } | |||
1035 | ccv_matrix_free(sat); | |||
1036 | return pass; | |||
1037 | } | |||
1038 | ||||
1039 | static ccv_array_t* _ccv_scd_hard_mining(gsl_rng* rng, ccv_scd_classifier_cascade_t* cascade, ccv_array_t* hard_mine, ccv_array_t* negatives, int negative_count, int grayscale, int even_dist) | |||
1040 | { | |||
1041 | ccv_array_t* hard_negatives = ccv_array_new(ccv_compute_dense_matrix_size(cascade->size.height, cascade->size.width, CCV_8U | (grayscale ? CCV_C1 : CCV_C3))(((sizeof(ccv_dense_matrix_t) + 63) & -64) + (((cascade-> size.width) * _ccv_get_data_type_size[((CCV_8U | (grayscale ? CCV_C1 : CCV_C3)) & 0xFF000) >> 12] * ((CCV_8U | ( grayscale ? CCV_C1 : CCV_C3)) & 0xFFF) + 3) & -4) * ( cascade->size.height)), negative_count, 0); | |||
1042 | int i, j, t; | |||
1043 | for (i = 0; i < negatives->rnum; i++) | |||
1044 | { | |||
1045 | ccv_dense_matrix_t* a = (ccv_dense_matrix_t*)ccv_array_get(negatives, i)((void*)(((char*)((negatives)->data)) + (size_t)(negatives )->rsize * (size_t)(i))); | |||
1046 | a->data.u8 = (unsigned char*)(a + 1); | |||
1047 | if (_ccv_scd_classifier_cascade_pass(cascade, a)) | |||
1048 | ccv_array_push(hard_negatives, a); | |||
1049 | } | |||
1050 | int n_per_mine = ccv_max((negative_count - hard_negatives->rnum) / hard_mine->rnum, 10)({ typeof ((negative_count - hard_negatives->rnum) / hard_mine ->rnum) _a = ((negative_count - hard_negatives->rnum) / hard_mine->rnum); typeof (10) _b = (10); (_a > _b) ? _a : _b; }); | |||
1051 | // the hard mining comes in following fashion: | |||
1052 | // 1). original, with n_per_mine set; | |||
1053 | // 2). horizontal flip, with n_per_mine set; | |||
1054 | // 3). vertical flip, with n_per_mine set; | |||
1055 | // 4). 180 rotation, with n_per_mine set; | |||
1056 | // 5~8). repeat above, but with no n_per_mine set; | |||
1057 | // after above, if we still cannot collect enough, so be it. | |||
1058 | for (t = (even_dist ? 0 : 4); t < 8 /* exhausted all variations */ && hard_negatives->rnum < negative_count; t++) | |||
1059 | { | |||
1060 | for (i = 0; i < hard_mine->rnum; i++) | |||
1061 | { | |||
1062 | FLUSH(CCV_CLI_INFO, " - hard mine negatives %d%% with %d-th permutation", 100 * hard_negatives->rnum / negative_count, t + 1)do { if ((CCV_CLI_INFO & ccv_cli_get_output_levels())) { for (_CCV_PRINT_LOOP = 0; _CCV_PRINT_LOOP < _CCV_PRINT_COUNT; _CCV_PRINT_LOOP++) printf("\b"); for (_CCV_PRINT_LOOP = 0; _CCV_PRINT_LOOP < _CCV_PRINT_COUNT; _CCV_PRINT_LOOP++) printf(" "); for ( _CCV_PRINT_LOOP = 0; _CCV_PRINT_LOOP < _CCV_PRINT_COUNT; _CCV_PRINT_LOOP ++) printf("\b"); _CCV_PRINT_COUNT = printf(" - hard mine negatives %d%% with %d-th permutation" , 100 * hard_negatives->rnum / negative_count, t + 1); fflush (stdout); } } while (0); | |||
1063 | ccv_file_info_t* file_info = (ccv_file_info_t*)ccv_array_get(hard_mine, i)((void*)(((char*)((hard_mine)->data)) + (size_t)(hard_mine )->rsize * (size_t)(i))); | |||
1064 | ccv_dense_matrix_t* image = 0; | |||
1065 | ccv_read(file_info->filename, &image, CCV_IO_ANY_FILE | (grayscale ? CCV_IO_GRAY : CCV_IO_RGB_COLOR))ccv_read_impl(file_info->filename, &image, CCV_IO_ANY_FILE | (grayscale ? CCV_IO_GRAY : CCV_IO_RGB_COLOR), 0, 0, 0); | |||
1066 | if (image == 0) | |||
1067 | { | |||
1068 | PRINT(CCV_CLI_ERROR, "\n - %s: cannot be open, possibly corrupted\n", file_info->filename)do { if ((CCV_CLI_ERROR & ccv_cli_get_output_levels())) { printf("\n - %s: cannot be open, possibly corrupted\n", file_info ->filename); fflush(stdout); } } while (0); | |||
1069 | continue; | |||
1070 | } | |||
1071 | if (t % 2 != 0) | |||
1072 | ccv_flip(image, 0, 0, CCV_FLIP_X); | |||
1073 | if (t % 4 >= 2) | |||
1074 | ccv_flip(image, 0, 0, CCV_FLIP_Y); | |||
1075 | if (t >= 4) | |||
1076 | n_per_mine = negative_count; // no hard limit on n_per_mine anymore for the last pass | |||
1077 | ccv_scd_param_t params = { | |||
1078 | .interval = 3, | |||
1079 | .min_neighbors = 0, | |||
1080 | .step_through = 4, | |||
1081 | .size = cascade->size, | |||
1082 | }; | |||
1083 | ccv_array_t* objects = ccv_scd_detect_objects(image, &cascade, 1, params); | |||
1084 | if (objects->rnum > 0) | |||
1085 | { | |||
1086 | gsl_ran_shuffle(rng, objects->data, objects->rnum, objects->rsize); | |||
1087 | for (j = 0; j < ccv_min(objects->rnum, n_per_mine)({ typeof (objects->rnum) _a = (objects->rnum); typeof ( n_per_mine) _b = (n_per_mine); (_a < _b) ? _a : _b; }); j++) | |||
1088 | { | |||
1089 | ccv_rect_t* rect = (ccv_rect_t*)ccv_array_get(objects, j)((void*)(((char*)((objects)->data)) + (size_t)(objects)-> rsize * (size_t)(j))); | |||
1090 | if (rect->x < 0 || rect->y < 0 || rect->x + rect->width > image->cols || rect->y + rect->height > image->rows) | |||
1091 | continue; | |||
1092 | ccv_dense_matrix_t* sliced = 0; | |||
1093 | ccv_slice(image, (ccv_matrix_t**)&sliced, 0, rect->y, rect->x, rect->height, rect->width); | |||
1094 | ccv_dense_matrix_t* resized = 0; | |||
1095 | assert(sliced->rows >= cascade->size.height && sliced->cols >= cascade->size.width)((void) sizeof ((sliced->rows >= cascade->size.height && sliced->cols >= cascade->size.width) ? 1 : 0), __extension__ ({ if (sliced->rows >= cascade-> size.height && sliced->cols >= cascade->size .width) ; else __assert_fail ("sliced->rows >= cascade->size.height && sliced->cols >= cascade->size.width" , "ccv_scd.c", 1095, __extension__ __PRETTY_FUNCTION__); })); | |||
1096 | if (sliced->rows > cascade->size.height || sliced->cols > cascade->size.width) | |||
1097 | { | |||
1098 | ccv_resample(sliced, &resized, 0, (double)cascade->size.height / (double)sliced->rows, (double)cascade->size.width / (double)sliced->cols, CCV_INTER_CUBIC); | |||
1099 | ccv_matrix_free(sliced); | |||
1100 | } else { | |||
1101 | resized = sliced; | |||
1102 | } | |||
1103 | if (_ccv_scd_classifier_cascade_pass(cascade, resized)) | |||
1104 | ccv_array_push(hard_negatives, resized); | |||
1105 | ccv_matrix_free(resized); | |||
1106 | if (hard_negatives->rnum >= negative_count) | |||
1107 | break; | |||
1108 | } | |||
1109 | } | |||
1110 | ccv_matrix_free(image); | |||
1111 | if (hard_negatives->rnum >= negative_count) | |||
1112 | break; | |||
1113 | } | |||
1114 | } | |||
1115 | FLUSH(CCV_CLI_INFO, " - hard mine negatives : %d\n", hard_negatives->rnum)do { if ((CCV_CLI_INFO & ccv_cli_get_output_levels())) { for (_CCV_PRINT_LOOP = 0; _CCV_PRINT_LOOP < _CCV_PRINT_COUNT; _CCV_PRINT_LOOP++) printf("\b"); for (_CCV_PRINT_LOOP = 0; _CCV_PRINT_LOOP < _CCV_PRINT_COUNT; _CCV_PRINT_LOOP++) printf(" "); for ( _CCV_PRINT_LOOP = 0; _CCV_PRINT_LOOP < _CCV_PRINT_COUNT; _CCV_PRINT_LOOP ++) printf("\b"); _CCV_PRINT_COUNT = printf(" - hard mine negatives : %d\n" , hard_negatives->rnum); fflush(stdout); } } while (0); | |||
1116 | ccv_make_array_immutable(hard_negatives); | |||
1117 | return hard_negatives; | |||
1118 | } | |||
1119 | ||||
1120 | typedef struct { | |||
1121 | ccv_function_state_reserve_fieldint line_no;; | |||
1122 | int t, k; | |||
1123 | uint64_t array_signature; | |||
1124 | ccv_array_t* features; | |||
1125 | ccv_array_t* positives; | |||
1126 | ccv_array_t* negatives; | |||
1127 | double* s; | |||
1128 | double* pw; | |||
1129 | double* nw; | |||
1130 | float* fv; // feature vector for examples * feature | |||
1131 | double auc_prev; | |||
1132 | double accu_true_positive_rate; | |||
1133 | double accu_false_positive_rate; | |||
1134 | ccv_scd_classifier_cascade_t* cascade; | |||
1135 | ccv_scd_train_param_t params; | |||
1136 | } ccv_scd_classifier_cascade_new_function_state_t; | |||
1137 | ||||
1138 | static void _ccv_scd_classifier_cascade_new_function_state_read(const char* filename, ccv_scd_classifier_cascade_new_function_state_t* z) | |||
1139 | { | |||
1140 | ccv_scd_classifier_cascade_t* cascade = ccv_scd_classifier_cascade_read(filename); | |||
1141 | if (!cascade) | |||
1142 | return; | |||
1143 | if (z->cascade) | |||
1144 | ccv_scd_classifier_cascade_free(z->cascade); | |||
1145 | z->cascade = cascade; | |||
1146 | assert(z->cascade->size.width == z->params.size.width)((void) sizeof ((z->cascade->size.width == z->params .size.width) ? 1 : 0), __extension__ ({ if (z->cascade-> size.width == z->params.size.width) ; else __assert_fail ( "z->cascade->size.width == z->params.size.width", "ccv_scd.c" , 1146, __extension__ __PRETTY_FUNCTION__); })); | |||
1147 | assert(z->cascade->size.height == z->params.size.height)((void) sizeof ((z->cascade->size.height == z->params .size.height) ? 1 : 0), __extension__ ({ if (z->cascade-> size.height == z->params.size.height) ; else __assert_fail ("z->cascade->size.height == z->params.size.height" , "ccv_scd.c", 1147, __extension__ __PRETTY_FUNCTION__); })); | |||
1148 | sqlite3* db = 0; | |||
1149 | if (SQLITE_OK0 == sqlite3_open(filename, &db)) | |||
1150 | { | |||
1151 | const char negative_data_qs[] = | |||
1152 | "SELECT data, rnum, rsize FROM negative_data WHERE id=0;"; | |||
1153 | sqlite3_stmt* negative_data_stmt = 0; | |||
1154 | if (SQLITE_OK0 == sqlite3_prepare_v2(db, negative_data_qs, sizeof(negative_data_qs), &negative_data_stmt, 0)) | |||
1155 | { | |||
1156 | if (sqlite3_step(negative_data_stmt) == SQLITE_ROW100) | |||
1157 | { | |||
1158 | int rsize = ccv_compute_dense_matrix_size(z->cascade->size.height, z->cascade->size.width, CCV_8U | (z->params.grayscale ? CCV_C1 : CCV_C3))(((sizeof(ccv_dense_matrix_t) + 63) & -64) + (((z->cascade ->size.width) * _ccv_get_data_type_size[((CCV_8U | (z-> params.grayscale ? CCV_C1 : CCV_C3)) & 0xFF000) >> 12 ] * ((CCV_8U | (z->params.grayscale ? CCV_C1 : CCV_C3)) & 0xFFF) + 3) & -4) * (z->cascade->size.height)); | |||
1159 | int rnum = sqlite3_column_int(negative_data_stmt, 1); | |||
1160 | assert(sqlite3_column_int(negative_data_stmt, 2) == rsize)((void) sizeof ((sqlite3_column_int(negative_data_stmt, 2) == rsize) ? 1 : 0), __extension__ ({ if (sqlite3_column_int(negative_data_stmt , 2) == rsize) ; else __assert_fail ("sqlite3_column_int(negative_data_stmt, 2) == rsize" , "ccv_scd.c", 1160, __extension__ __PRETTY_FUNCTION__); })); | |||
1161 | size_t size = sqlite3_column_bytes(negative_data_stmt, 0); | |||
1162 | assert(size == (size_t)rsize * rnum)((void) sizeof ((size == (size_t)rsize * rnum) ? 1 : 0), __extension__ ({ if (size == (size_t)rsize * rnum) ; else __assert_fail ("size == (size_t)rsize * rnum" , "ccv_scd.c", 1162, __extension__ __PRETTY_FUNCTION__); })); | |||
1163 | if (z->negatives) | |||
1164 | ccv_array_clear(z->negatives); | |||
1165 | else | |||
1166 | z->negatives = ccv_array_new(rsize, rnum, 0); | |||
1167 | int i; | |||
1168 | const uint8_t* data = (const uint8_t*)sqlite3_column_blob(negative_data_stmt, 0); | |||
1169 | for (i = 0; i < rnum; i++) | |||
1170 | ccv_array_push(z->negatives, data + (off_t)i * rsize); | |||
1171 | ccv_make_array_immutable(z->negatives); | |||
1172 | z->array_signature = z->negatives->sig; | |||
1173 | } | |||
1174 | sqlite3_finalize(negative_data_stmt); | |||
1175 | } | |||
1176 | const char function_state_qs[] = | |||
1177 | "SELECT t, k, positive_count, auc_prev, " // 4 | |||
1178 | "accu_true_positive_rate, accu_false_positive_rate, " // 6 | |||
1179 | "line_no, s, pw, nw FROM function_state WHERE fsid = 0;"; // 10 | |||
1180 | sqlite3_stmt* function_state_stmt = 0; | |||
1181 | if (SQLITE_OK0 == sqlite3_prepare_v2(db, function_state_qs, sizeof(function_state_qs), &function_state_stmt, 0)) | |||
1182 | { | |||
1183 | if (sqlite3_step(function_state_stmt) == SQLITE_ROW100) | |||
1184 | { | |||
1185 | z->t = sqlite3_column_int(function_state_stmt, 0); | |||
1186 | z->k = sqlite3_column_int(function_state_stmt, 1); | |||
1187 | int positive_count = sqlite3_column_int(function_state_stmt, 2); | |||
1188 | assert(positive_count == z->positives->rnum)((void) sizeof ((positive_count == z->positives->rnum) ? 1 : 0), __extension__ ({ if (positive_count == z->positives ->rnum) ; else __assert_fail ("positive_count == z->positives->rnum" , "ccv_scd.c", 1188, __extension__ __PRETTY_FUNCTION__); })); | |||
1189 | z->auc_prev = sqlite3_column_double(function_state_stmt, 3); | |||
1190 | z->accu_true_positive_rate = sqlite3_column_double(function_state_stmt, 4); | |||
1191 | z->accu_false_positive_rate = sqlite3_column_double(function_state_stmt, 5); | |||
1192 | z->line_no = sqlite3_column_int(function_state_stmt, 6); | |||
1193 | size_t size = sqlite3_column_bytes(function_state_stmt, 7); | |||
1194 | const void* s = sqlite3_column_blob(function_state_stmt, 7); | |||
1195 | memcpy(z->s, s, size); | |||
1196 | size = sqlite3_column_bytes(function_state_stmt, 8); | |||
1197 | const void* pw = sqlite3_column_blob(function_state_stmt, 8); | |||
1198 | memcpy(z->pw, pw, size); | |||
1199 | size = sqlite3_column_bytes(function_state_stmt, 9); | |||
1200 | const void* nw = sqlite3_column_blob(function_state_stmt, 9); | |||
1201 | memcpy(z->nw, nw, size); | |||
1202 | } | |||
1203 | sqlite3_finalize(function_state_stmt); | |||
1204 | } | |||
1205 | _ccv_scd_precompute_feature_vectors(z->features, z->positives, z->negatives, z->fv); | |||
1206 | sqlite3_close(db); | |||
1207 | } | |||
1208 | } | |||
1209 | ||||
1210 | static void _ccv_scd_classifier_cascade_new_function_state_write(ccv_scd_classifier_cascade_new_function_state_t* z, const char* filename) | |||
1211 | { | |||
1212 | ccv_scd_classifier_cascade_write(z->cascade, filename); | |||
1213 | sqlite3* db = 0; | |||
1214 | if (SQLITE_OK0 == sqlite3_open(filename, &db)) | |||
1215 | { | |||
1216 | const char function_state_create_table_qs[] = | |||
1217 | "CREATE TABLE IF NOT EXISTS function_state " | |||
1218 | "(fsid INTEGER PRIMARY KEY ASC, t INTEGER, k INTEGER, positive_count INTEGER, auc_prev DOUBLE, accu_true_positive_rate DOUBLE, accu_false_positive_rate DOUBLE, line_no INTEGER, s BLOB, pw BLOB, nw BLOB);" | |||
1219 | "CREATE TABLE IF NOT EXISTS negative_data " | |||
1220 | "(id INTEGER PRIMARY KEY ASC, data BLOB, rnum INTEGER, rsize INTEGER);"; | |||
1221 | assert(SQLITE_OK == sqlite3_exec(db, function_state_create_table_qs, 0, 0, 0))((void) sizeof ((0 == sqlite3_exec(db, function_state_create_table_qs , 0, 0, 0)) ? 1 : 0), __extension__ ({ if (0 == sqlite3_exec( db, function_state_create_table_qs, 0, 0, 0)) ; else __assert_fail ("SQLITE_OK == sqlite3_exec(db, function_state_create_table_qs, 0, 0, 0)" , "ccv_scd.c", 1221, __extension__ __PRETTY_FUNCTION__); })); | |||
1222 | const char function_state_insert_qs[] = | |||
1223 | "REPLACE INTO function_state " | |||
1224 | "(fsid, t, k, positive_count, auc_prev, accu_true_positive_rate, accu_false_positive_rate, line_no, s, pw, nw) VALUES " | |||
1225 | "(0, $t, $k, $positive_count, $auc_prev, $accu_true_positive_rate, $accu_false_positive_rate, $line_no, $s, $pw, $nw);"; | |||
1226 | sqlite3_stmt* function_state_insert_stmt = 0; | |||
1227 | assert(SQLITE_OK == sqlite3_prepare_v2(db, function_state_insert_qs, sizeof(function_state_insert_qs), &function_state_insert_stmt, 0))((void) sizeof ((0 == sqlite3_prepare_v2(db, function_state_insert_qs , sizeof(function_state_insert_qs), &function_state_insert_stmt , 0)) ? 1 : 0), __extension__ ({ if (0 == sqlite3_prepare_v2( db, function_state_insert_qs, sizeof(function_state_insert_qs ), &function_state_insert_stmt, 0)) ; else __assert_fail ( "SQLITE_OK == sqlite3_prepare_v2(db, function_state_insert_qs, sizeof(function_state_insert_qs), &function_state_insert_stmt, 0)" , "ccv_scd.c", 1227, __extension__ __PRETTY_FUNCTION__); })); | |||
1228 | sqlite3_bind_int(function_state_insert_stmt, 1, z->t); | |||
1229 | sqlite3_bind_int(function_state_insert_stmt, 2, z->k); | |||
1230 | sqlite3_bind_int(function_state_insert_stmt, 3, z->positives->rnum); | |||
1231 | sqlite3_bind_double(function_state_insert_stmt, 4, z->auc_prev); | |||
1232 | sqlite3_bind_double(function_state_insert_stmt, 5, z->accu_true_positive_rate); | |||
1233 | sqlite3_bind_double(function_state_insert_stmt, 6, z->accu_false_positive_rate); | |||
1234 | sqlite3_bind_int(function_state_insert_stmt, 7, z->line_no); | |||
1235 | sqlite3_bind_blob(function_state_insert_stmt, 8, z->s, sizeof(double) * (z->positives->rnum + z->negatives->rnum), SQLITE_STATIC((sqlite3_destructor_type)0)); | |||
1236 | sqlite3_bind_blob(function_state_insert_stmt, 9, z->pw, sizeof(double) * z->positives->rnum, SQLITE_STATIC((sqlite3_destructor_type)0)); | |||
1237 | sqlite3_bind_blob(function_state_insert_stmt, 10, z->nw, sizeof(double) * z->negatives->rnum, SQLITE_STATIC((sqlite3_destructor_type)0)); | |||
1238 | assert(SQLITE_DONE == sqlite3_step(function_state_insert_stmt))((void) sizeof ((101 == sqlite3_step(function_state_insert_stmt )) ? 1 : 0), __extension__ ({ if (101 == sqlite3_step(function_state_insert_stmt )) ; else __assert_fail ("SQLITE_DONE == sqlite3_step(function_state_insert_stmt)" , "ccv_scd.c", 1238, __extension__ __PRETTY_FUNCTION__); })); | |||
1239 | sqlite3_finalize(function_state_insert_stmt); | |||
1240 | if (z->array_signature != z->negatives->sig) | |||
1241 | { | |||
1242 | const char negative_data_insert_qs[] = | |||
1243 | "REPLACE INTO negative_data " | |||
1244 | "(id, data, rnum, rsize) VALUES (0, $data, $rnum, $rsize);"; | |||
1245 | sqlite3_stmt* negative_data_insert_stmt = 0; | |||
1246 | assert(SQLITE_OK == sqlite3_prepare_v2(db, negative_data_insert_qs, sizeof(negative_data_insert_qs), &negative_data_insert_stmt, 0))((void) sizeof ((0 == sqlite3_prepare_v2(db, negative_data_insert_qs , sizeof(negative_data_insert_qs), &negative_data_insert_stmt , 0)) ? 1 : 0), __extension__ ({ if (0 == sqlite3_prepare_v2( db, negative_data_insert_qs, sizeof(negative_data_insert_qs), &negative_data_insert_stmt, 0)) ; else __assert_fail ("SQLITE_OK == sqlite3_prepare_v2(db, negative_data_insert_qs, sizeof(negative_data_insert_qs), &negative_data_insert_stmt, 0)" , "ccv_scd.c", 1246, __extension__ __PRETTY_FUNCTION__); })); | |||
1247 | sqlite3_bind_blob(negative_data_insert_stmt, 1, z->negatives->data, z->negatives->rsize * z->negatives->rnum, SQLITE_STATIC((sqlite3_destructor_type)0)); | |||
1248 | sqlite3_bind_int(negative_data_insert_stmt, 2, z->negatives->rnum); | |||
1249 | sqlite3_bind_int(negative_data_insert_stmt, 3, z->negatives->rsize); | |||
1250 | assert(SQLITE_DONE == sqlite3_step(negative_data_insert_stmt))((void) sizeof ((101 == sqlite3_step(negative_data_insert_stmt )) ? 1 : 0), __extension__ ({ if (101 == sqlite3_step(negative_data_insert_stmt )) ; else __assert_fail ("SQLITE_DONE == sqlite3_step(negative_data_insert_stmt)" , "ccv_scd.c", 1250, __extension__ __PRETTY_FUNCTION__); })); | |||
1251 | sqlite3_finalize(negative_data_insert_stmt); | |||
1252 | z->array_signature = z->negatives->sig; | |||
1253 | } | |||
1254 | sqlite3_close(db); | |||
1255 | } | |||
1256 | } | |||
1257 | #endif | |||
1258 | ||||
1259 | ccv_scd_classifier_cascade_t* ccv_scd_classifier_cascade_new(ccv_array_t* posfiles, ccv_array_t* hard_mine, int negative_count, const char* filename, ccv_scd_train_param_t params) | |||
1260 | { | |||
1261 | #ifdef HAVE_GSL1 | |||
1262 | assert(posfiles->rnum > 0)((void) sizeof ((posfiles->rnum > 0) ? 1 : 0), __extension__ ({ if (posfiles->rnum > 0) ; else __assert_fail ("posfiles->rnum > 0" , "ccv_scd.c", 1262, __extension__ __PRETTY_FUNCTION__); })); | |||
| ||||
1263 | assert(hard_mine->rnum > 0)((void) sizeof ((hard_mine->rnum > 0) ? 1 : 0), __extension__ ({ if (hard_mine->rnum > 0) ; else __assert_fail ("hard_mine->rnum > 0" , "ccv_scd.c", 1263, __extension__ __PRETTY_FUNCTION__); })); | |||
1264 | gsl_rng_env_setup(); | |||
1265 | gsl_rng* rng = gsl_rng_alloc(gsl_rng_default); | |||
1266 | ccv_scd_classifier_cascade_new_function_state_t z = {0}; | |||
1267 | z.features = _ccv_scd_stump_features(params.feature.base, params.feature.range_through, params.feature.step_through, params.size); | |||
1268 | PRINT(CCV_CLI_INFO, " - using %d features\n", z.features->rnum)do { if ((CCV_CLI_INFO & ccv_cli_get_output_levels())) { printf (" - using %d features\n", z.features->rnum); fflush(stdout ); } } while (0); | |||
1269 | int i, j, p, q; | |||
1270 | z.positives = _ccv_scd_collect_positives(params.size, posfiles, params.grayscale); | |||
1271 | double* h = (double*)ccmallocmalloc(sizeof(double) * (z.positives->rnum + negative_count)); | |||
1272 | z.s = (double*)ccmallocmalloc(sizeof(double) * (z.positives->rnum + negative_count)); | |||
1273 | assert(z.s)((void) sizeof ((z.s) ? 1 : 0), __extension__ ({ if (z.s) ; else __assert_fail ("z.s", "ccv_scd.c", 1273, __extension__ __PRETTY_FUNCTION__ ); })); | |||
1274 | z.pw = (double*)ccmallocmalloc(sizeof(double) * z.positives->rnum); | |||
1275 | assert(z.pw)((void) sizeof ((z.pw) ? 1 : 0), __extension__ ({ if (z.pw) ; else __assert_fail ("z.pw", "ccv_scd.c", 1275, __extension__ __PRETTY_FUNCTION__); })); | |||
1276 | z.nw = (double*)ccmallocmalloc(sizeof(double) * negative_count); | |||
1277 | assert(z.nw)((void) sizeof ((z.nw) ? 1 : 0), __extension__ ({ if (z.nw) ; else __assert_fail ("z.nw", "ccv_scd.c", 1277, __extension__ __PRETTY_FUNCTION__); })); | |||
1278 | ccmemalignposix_memalign((void**)&z.fv, 16, sizeof(float) * (z.positives->rnum + negative_count) * z.features->rnum * 32); | |||
1279 | assert(z.fv)((void) sizeof ((z.fv) ? 1 : 0), __extension__ ({ if (z.fv) ; else __assert_fail ("z.fv", "ccv_scd.c", 1279, __extension__ __PRETTY_FUNCTION__); })); | |||
1280 | z.params = params; | |||
1281 | ccv_function_state_begin(_ccv_scd_classifier_cascade_new_function_state_read, z, filename)(_ccv_scd_classifier_cascade_new_function_state_read)((filename ), &(z)); switch ((z).line_no) { case 0:;; | |||
1282 | z.negatives = _ccv_scd_collect_negatives(rng, params.size, hard_mine, negative_count, params.grayscale); | |||
1283 | _ccv_scd_precompute_feature_vectors(z.features, z.positives, z.negatives, z.fv); | |||
1284 | z.cascade = (ccv_scd_classifier_cascade_t*)ccmallocmalloc(sizeof(ccv_scd_classifier_cascade_t)); | |||
1285 | z.cascade->margin = ccv_margin(0, 0, 0, 0); | |||
1286 | z.cascade->size = params.size; | |||
1287 | z.cascade->count = 0; | |||
1288 | z.cascade->classifiers = 0; | |||
1289 | z.accu_true_positive_rate = 1; | |||
1290 | z.accu_false_positive_rate = 1; | |||
1291 | ccv_function_state_resume(_ccv_scd_classifier_cascade_new_function_state_write, z, filename)do { (z).line_no = 1291; (_ccv_scd_classifier_cascade_new_function_state_write )(&(z), (filename)); case 1291:; } while (0); | |||
1292 | for (z.t = 0; z.t < params.boosting; z.t++) | |||
1293 | { | |||
1294 | for (i = 0; i < z.positives->rnum; i++) | |||
1295 | z.pw[i] = 0.5 / z.positives->rnum; | |||
1296 | for (i = 0; i < z.negatives->rnum; i++) | |||
1297 | z.nw[i] = 0.5 / z.negatives->rnum; | |||
1298 | memset(z.s, 0, sizeof(double) * (z.positives->rnum + z.negatives->rnum)); | |||
1299 | z.cascade->classifiers = (ccv_scd_stump_classifier_t*)ccreallocrealloc(z.cascade->classifiers, sizeof(ccv_scd_stump_classifier_t) * (z.t + 1)); | |||
1300 | z.cascade->count = z.t + 1; | |||
1301 | z.cascade->classifiers[z.t].threshold = 0; | |||
1302 | z.cascade->classifiers[z.t].features = 0; | |||
1303 | z.cascade->classifiers[z.t].count = 0; | |||
1304 | z.auc_prev = 0; | |||
1305 | assert(z.positives->rnum > 0 && z.negatives->rnum > 0)((void) sizeof ((z.positives->rnum > 0 && z.negatives ->rnum > 0) ? 1 : 0), __extension__ ({ if (z.positives-> rnum > 0 && z.negatives->rnum > 0) ; else __assert_fail ("z.positives->rnum > 0 && z.negatives->rnum > 0" , "ccv_scd.c", 1305, __extension__ __PRETTY_FUNCTION__); })); | |||
1306 | // for the first prune stages, we have more restrictive number of features (faster) | |||
1307 | for (z.k = 0; z.k < (z.t < params.stop_criteria.prune_stage ? params.stop_criteria.prune_feature : params.stop_criteria.maximum_feature); z.k++) | |||
1308 | { | |||
1309 | ccv_scd_stump_classifier_t* classifier = z.cascade->classifiers + z.t; | |||
1310 | classifier->features = (ccv_scd_stump_feature_t*)ccreallocrealloc(classifier->features, sizeof(ccv_scd_stump_feature_t) * (z.k + 1)); | |||
1311 | _ccv_scd_stump_feature_supervised_train(rng, z.features, z.positives->rnum, z.negatives->rnum, z.pw, z.nw, z.fv, params.C, params.weight_trimming); | |||
1312 | int best_feature_no = _ccv_scd_best_feature_gentle_adaboost(z.s, z.features, z.pw, z.nw, z.positives->rnum, z.negatives->rnum, z.fv); | |||
1313 | ccv_scd_stump_feature_t best_feature = *(ccv_scd_stump_feature_t*)ccv_array_get(z.features, best_feature_no)((void*)(((char*)((z.features)->data)) + (size_t)(z.features )->rsize * (size_t)(best_feature_no))); | |||
1314 | for (i = 0; i < z.positives->rnum + z.negatives->rnum; i++) | |||
1315 | { | |||
1316 | float* surf = _ccv_scd_get_surf_at(z.fv, best_feature_no, i, z.positives->rnum, z.negatives->rnum); | |||
1317 | float v = best_feature.bias; | |||
1318 | for (j = 0; j < 32; j++) | |||
1319 | v += best_feature.w[j] * surf[j]; | |||
1320 | v = expf(v); | |||
1321 | h[i] = (v - 1) / (v + 1); | |||
1322 | } | |||
1323 | // compute the total score so far | |||
1324 | for (i = 0; i < z.positives->rnum + z.negatives->rnum; i++) | |||
1325 | z.s[i] += h[i]; | |||
1326 | // compute AUC | |||
1327 | double auc = _ccv_scd_auc(z.s, z.positives->rnum, z.negatives->rnum); | |||
1328 | float true_positive_rate = 0; | |||
1329 | float false_positive_rate = 0; | |||
1330 | // compute true positive / false positive rate | |||
1331 | _ccv_scd_threshold_at_hit_rate(z.s, z.positives->rnum, z.negatives->rnum, params.stop_criteria.hit_rate, &true_positive_rate, &false_positive_rate); | |||
1332 | FLUSH(CCV_CLI_INFO, " - at %d-th iteration, auc: %lf, TP rate: %f, FP rate: %f\n", z.k + 1, auc, true_positive_rate, false_positive_rate)do { if ((CCV_CLI_INFO & ccv_cli_get_output_levels())) { for (_CCV_PRINT_LOOP = 0; _CCV_PRINT_LOOP < _CCV_PRINT_COUNT; _CCV_PRINT_LOOP++) printf("\b"); for (_CCV_PRINT_LOOP = 0; _CCV_PRINT_LOOP < _CCV_PRINT_COUNT; _CCV_PRINT_LOOP++) printf(" "); for ( _CCV_PRINT_LOOP = 0; _CCV_PRINT_LOOP < _CCV_PRINT_COUNT; _CCV_PRINT_LOOP ++) printf("\b"); _CCV_PRINT_COUNT = printf(" - at %d-th iteration, auc: %lf, TP rate: %f, FP rate: %f\n" , z.k + 1, auc, true_positive_rate, false_positive_rate); fflush (stdout); } } while (0); | |||
1333 | PRINT(CCV_CLI_INFO, " --- pick feature %s @ (%d, %d, %d, %d)\n", ((best_feature.dy[3] == best_feature.dy[0] ? "4x1" : (best_feature.dx[3] == best_feature.dx[0] ? "1x4" : "2x2"))), best_feature.sx[0], best_feature.sy[0], best_feature.dx[3], best_feature.dy[3])do { if ((CCV_CLI_INFO & ccv_cli_get_output_levels())) { printf (" --- pick feature %s @ (%d, %d, %d, %d)\n", ((best_feature. dy[3] == best_feature.dy[0] ? "4x1" : (best_feature.dx[3] == best_feature .dx[0] ? "1x4" : "2x2"))), best_feature.sx[0], best_feature.sy [0], best_feature.dx[3], best_feature.dy[3]); fflush(stdout); } } while (0); | |||
1334 | classifier->features[z.k] = best_feature; | |||
1335 | classifier->count = z.k + 1; | |||
1336 | double auc_prev = z.auc_prev; | |||
1337 | z.auc_prev = auc; | |||
1338 | // auc stop to improve, as well as the false positive rate goal reach, at that point, we stop | |||
1339 | if (auc - auc_prev < params.stop_criteria.auc_crit && false_positive_rate < params.stop_criteria.false_positive_rate) | |||
1340 | break; | |||
1341 | // re-weight, with Gentle AdaBoost | |||
1342 | for (i = 0; i < z.positives->rnum; i++) | |||
1343 | z.pw[i] *= exp(-h[i]); | |||
1344 | for (i = 0; i < z.negatives->rnum; i++) | |||
1345 | z.nw[i] *= exp(h[i + z.positives->rnum]); | |||
1346 | // re-normalize | |||
1347 | double w = 0; | |||
1348 | for (i = 0; i < z.positives->rnum; i++) | |||
1349 | w += z.pw[i]; | |||
1350 | w = 0.5 / w; | |||
1351 | for (i = 0; i < z.positives->rnum; i++) | |||
1352 | z.pw[i] *= w; | |||
1353 | w = 0; | |||
1354 | for (i = 0; i < z.negatives->rnum; i++) | |||
1355 | w += z.nw[i]; | |||
1356 | w = 0.5 / w; | |||
1357 | for (i = 0; i < z.negatives->rnum; i++) | |||
1358 | z.nw[i] *= w; | |||
1359 | ccv_function_state_resume(_ccv_scd_classifier_cascade_new_function_state_write, z, filename)do { (z).line_no = 1359; (_ccv_scd_classifier_cascade_new_function_state_write )(&(z), (filename)); case 1359:; } while (0); | |||
1360 | } | |||
1361 | // backtrack removal | |||
1362 | while (z.cascade->classifiers[z.t].count > 1) | |||
1363 | { | |||
1364 | double max_auc = 0; | |||
1365 | p = -1; | |||
1366 | for (i = 0; i < z.cascade->classifiers[z.t].count; i++) | |||
1367 | { | |||
1368 | ccv_scd_stump_feature_t* feature = z.cascade->classifiers[z.t].features + i; | |||
1369 | int k = _ccv_scd_find_match_feature(feature, z.features); | |||
1370 | assert(k >= 0)((void) sizeof ((k >= 0) ? 1 : 0), __extension__ ({ if (k >= 0) ; else __assert_fail ("k >= 0", "ccv_scd.c", 1370, __extension__ __PRETTY_FUNCTION__); })); | |||
1371 | for (j = 0; j < z.positives->rnum + z.negatives->rnum; j++) | |||
1372 | { | |||
1373 | float* surf = _ccv_scd_get_surf_at(z.fv, k, j, z.positives->rnum, z.negatives->rnum); | |||
1374 | float v = feature->bias; | |||
1375 | for (q = 0; q < 32; q++) | |||
1376 | v += feature->w[q]* surf[q]; | |||
1377 | v = expf(v); | |||
1378 | h[j] = z.s[j] - (v - 1) / (v + 1); | |||
1379 | } | |||
1380 | double auc = _ccv_scd_auc(h, z.positives->rnum, z.negatives->rnum); | |||
1381 | FLUSH(CCV_CLI_INFO, " - attempting without %d-th feature, auc: %lf", i + 1, auc)do { if ((CCV_CLI_INFO & ccv_cli_get_output_levels())) { for (_CCV_PRINT_LOOP = 0; _CCV_PRINT_LOOP < _CCV_PRINT_COUNT; _CCV_PRINT_LOOP++) printf("\b"); for (_CCV_PRINT_LOOP = 0; _CCV_PRINT_LOOP < _CCV_PRINT_COUNT; _CCV_PRINT_LOOP++) printf(" "); for ( _CCV_PRINT_LOOP = 0; _CCV_PRINT_LOOP < _CCV_PRINT_COUNT; _CCV_PRINT_LOOP ++) printf("\b"); _CCV_PRINT_COUNT = printf(" - attempting without %d-th feature, auc: %lf" , i + 1, auc); fflush(stdout); } } while (0); | |||
1382 | if (auc >= max_auc) | |||
1383 | max_auc = auc, p = i; | |||
1384 | } | |||
1385 | if (max_auc >= z.auc_prev) | |||
1386 | { | |||
1387 | FLUSH(CCV_CLI_INFO, " - remove %d-th feature with new auc %lf\n", p + 1, max_auc)do { if ((CCV_CLI_INFO & ccv_cli_get_output_levels())) { for (_CCV_PRINT_LOOP = 0; _CCV_PRINT_LOOP < _CCV_PRINT_COUNT; _CCV_PRINT_LOOP++) printf("\b"); for (_CCV_PRINT_LOOP = 0; _CCV_PRINT_LOOP < _CCV_PRINT_COUNT; _CCV_PRINT_LOOP++) printf(" "); for ( _CCV_PRINT_LOOP = 0; _CCV_PRINT_LOOP < _CCV_PRINT_COUNT; _CCV_PRINT_LOOP ++) printf("\b"); _CCV_PRINT_COUNT = printf(" - remove %d-th feature with new auc %lf\n" , p + 1, max_auc); fflush(stdout); } } while (0); | |||
1388 | ccv_scd_stump_feature_t* feature = z.cascade->classifiers[z.t].features + p; | |||
1389 | int k = _ccv_scd_find_match_feature(feature, z.features); | |||
1390 | assert(k >= 0)((void) sizeof ((k >= 0) ? 1 : 0), __extension__ ({ if (k >= 0) ; else __assert_fail ("k >= 0", "ccv_scd.c", 1390, __extension__ __PRETTY_FUNCTION__); })); | |||
1391 | for (j = 0; j < z.positives->rnum + z.negatives->rnum; j++) | |||
1392 | { | |||
1393 | float* surf = _ccv_scd_get_surf_at(z.fv, k, j, z.positives->rnum, z.negatives->rnum); | |||
1394 | float v = feature->bias; | |||
1395 | for (q = 0; q < 32; q++) | |||
1396 | v += feature->w[q] * surf[q]; | |||
1397 | v = expf(v); | |||
1398 | z.s[j] -= (v - 1) / (v + 1); | |||
1399 | } | |||
1400 | z.auc_prev = _ccv_scd_auc(z.s, z.positives->rnum, z.negatives->rnum); | |||
1401 | --z.cascade->classifiers[z.t].count; | |||
1402 | if (p < z.cascade->classifiers[z.t].count) | |||
1403 | memmove(z.cascade->classifiers[z.t].features + p + 1, z.cascade->classifiers[z.t].features + p, sizeof(ccv_scd_stump_feature_t) * (z.cascade->classifiers[z.t].count - p)); | |||
1404 | } else | |||
1405 | break; | |||
1406 | } | |||
1407 | float true_positive_rate = 0; | |||
1408 | float false_positive_rate = 0; | |||
1409 | z.cascade->classifiers[z.t].threshold = _ccv_scd_threshold_at_hit_rate(z.s, z.positives->rnum, z.negatives->rnum, params.stop_criteria.hit_rate, &true_positive_rate, &false_positive_rate); | |||
1410 | z.accu_true_positive_rate *= true_positive_rate; | |||
1411 | z.accu_false_positive_rate *= false_positive_rate; | |||
1412 | FLUSH(CCV_CLI_INFO, " - %d-th stage classifier TP rate : %f, FP rate : %f, ATP rate : %lf, AFP rate : %lg, at threshold : %f\n", z.t + 1, true_positive_rate, false_positive_rate, z.accu_true_positive_rate, z.accu_false_positive_rate, z.cascade->classifiers[z.t].threshold)do { if ((CCV_CLI_INFO & ccv_cli_get_output_levels())) { for (_CCV_PRINT_LOOP = 0; _CCV_PRINT_LOOP < _CCV_PRINT_COUNT; _CCV_PRINT_LOOP++) printf("\b"); for (_CCV_PRINT_LOOP = 0; _CCV_PRINT_LOOP < _CCV_PRINT_COUNT; _CCV_PRINT_LOOP++) printf(" "); for ( _CCV_PRINT_LOOP = 0; _CCV_PRINT_LOOP < _CCV_PRINT_COUNT; _CCV_PRINT_LOOP ++) printf("\b"); _CCV_PRINT_COUNT = printf(" - %d-th stage classifier TP rate : %f, FP rate : %f, ATP rate : %lf, AFP rate : %lg, at threshold : %f\n" , z.t + 1, true_positive_rate, false_positive_rate, z.accu_true_positive_rate , z.accu_false_positive_rate, z.cascade->classifiers[z.t]. threshold); fflush(stdout); } } while (0); | |||
1413 | if (z.accu_false_positive_rate < params.stop_criteria.accu_false_positive_rate) | |||
1414 | break; | |||
1415 | ccv_function_state_resume(_ccv_scd_classifier_cascade_new_function_state_write, z, filename)do { (z).line_no = 1415; (_ccv_scd_classifier_cascade_new_function_state_write )(&(z), (filename)); case 1415:; } while (0); | |||
1416 | if (z.t < params.boosting - 1) | |||
1417 | { | |||
1418 | int pass = 0; | |||
1419 | for (i = 0; i < z.positives->rnum; i++) | |||
1420 | { | |||
1421 | ccv_dense_matrix_t* a = (ccv_dense_matrix_t*)ccv_array_get(z.positives, i)((void*)(((char*)((z.positives)->data)) + (size_t)(z.positives )->rsize * (size_t)(i))); | |||
1422 | a->data.u8 = (unsigned char*)(a + 1); | |||
1423 | if (_ccv_scd_classifier_cascade_pass(z.cascade, a)) | |||
1424 | ++pass; | |||
1425 | } | |||
1426 | PRINT(CCV_CLI_INFO, " - %d-th stage classifier TP rate (with pass) : %f\n", z.t + 1, (float)pass / z.positives->rnum)do { if ((CCV_CLI_INFO & ccv_cli_get_output_levels())) { printf (" - %d-th stage classifier TP rate (with pass) : %f\n", z.t + 1, (float)pass / z.positives->rnum); fflush(stdout); } } while (0); | |||
1427 | ccv_array_t* hard_negatives = _ccv_scd_hard_mining(rng, z.cascade, hard_mine, z.negatives, negative_count, params.grayscale, z.t < params.stop_criteria.prune_stage /* try to balance even distribution among negatives when we are in prune stage */); | |||
1428 | ccv_array_free(z.negatives); | |||
1429 | z.negatives = hard_negatives; | |||
1430 | _ccv_scd_precompute_feature_vectors(z.features, z.positives, z.negatives, z.fv); | |||
1431 | } | |||
1432 | ccv_function_state_resume(_ccv_scd_classifier_cascade_new_function_state_write, z, filename)do { (z).line_no = 1432; (_ccv_scd_classifier_cascade_new_function_state_write )(&(z), (filename)); case 1432:; } while (0); | |||
1433 | } | |||
1434 | ccv_array_free(z.negatives); | |||
1435 | ccv_function_state_finish()}; | |||
1436 | ccv_array_free(z.features); | |||
1437 | ccv_array_free(z.positives); | |||
1438 | ccfreefree(h); | |||
1439 | ccfreefree(z.s); | |||
1440 | ccfreefree(z.pw); | |||
1441 | ccfreefree(z.nw); | |||
1442 | ccfreefree(z.fv); | |||
1443 | gsl_rng_free(rng); | |||
1444 | return z.cascade; | |||
1445 | #else | |||
1446 | assert(0 && "ccv_scd_classifier_cascade_new requires GSL library and support")((void) sizeof ((0 && "ccv_scd_classifier_cascade_new requires GSL library and support" ) ? 1 : 0), __extension__ ({ if (0 && "ccv_scd_classifier_cascade_new requires GSL library and support" ) ; else __assert_fail ("0 && \"ccv_scd_classifier_cascade_new requires GSL library and support\"" , "ccv_scd.c", 1446, __extension__ __PRETTY_FUNCTION__); })); | |||
1447 | return 0; | |||
1448 | #endif | |||
1449 | } | |||
1450 | ||||
1451 | void ccv_scd_classifier_cascade_write(ccv_scd_classifier_cascade_t* cascade, const char* filename) | |||
1452 | { | |||
1453 | sqlite3* db = 0; | |||
1454 | if (SQLITE_OK0 == sqlite3_open(filename, &db)) | |||
1455 | { | |||
1456 | const char create_table_qs[] = | |||
1457 | "CREATE TABLE IF NOT EXISTS cascade_params " | |||
1458 | "(id INTEGER PRIMARY KEY ASC, count INTEGER, " | |||
1459 | "margin_left INTEGER, margin_top INTEGER, margin_right INTEGER, margin_bottom INTEGER, " | |||
1460 | "size_width INTEGER, size_height INTEGER);" | |||
1461 | "CREATE TABLE IF NOT EXISTS classifier_params " | |||
1462 | "(classifier INTEGER PRIMARY KEY ASC, count INTEGER, threshold DOUBLE);" | |||
1463 | "CREATE TABLE IF NOT EXISTS feature_params " | |||
1464 | "(classifier INTEGER, id INTEGER, " | |||
1465 | "sx_0 INTEGER, sy_0 INTEGER, dx_0 INTEGER, dy_0 INTEGER, " | |||
1466 | "sx_1 INTEGER, sy_1 INTEGER, dx_1 INTEGER, dy_1 INTEGER, " | |||
1467 | "sx_2 INTEGER, sy_2 INTEGER, dx_2 INTEGER, dy_2 INTEGER, " | |||
1468 | "sx_3 INTEGER, sy_3 INTEGER, dx_3 INTEGER, dy_3 INTEGER, " | |||
1469 | "bias DOUBLE, w BLOB, UNIQUE (classifier, id));"; | |||
1470 | assert(SQLITE_OK == sqlite3_exec(db, create_table_qs, 0, 0, 0))((void) sizeof ((0 == sqlite3_exec(db, create_table_qs, 0, 0, 0)) ? 1 : 0), __extension__ ({ if (0 == sqlite3_exec(db, create_table_qs , 0, 0, 0)) ; else __assert_fail ("SQLITE_OK == sqlite3_exec(db, create_table_qs, 0, 0, 0)" , "ccv_scd.c", 1470, __extension__ __PRETTY_FUNCTION__); })); | |||
1471 | const char cascade_params_insert_qs[] = | |||
1472 | "REPLACE INTO cascade_params " | |||
1473 | "(id, count, " | |||
1474 | "margin_left, margin_top, margin_right, margin_bottom, " | |||
1475 | "size_width, size_height) VALUES " | |||
1476 | "(0, $count, " // 0 | |||
1477 | "$margin_left, $margin_top, $margin_bottom, $margin_right, " // 4 | |||
1478 | "$size_width, $size_height);"; // 6 | |||
1479 | sqlite3_stmt* cascade_params_insert_stmt = 0; | |||
1480 | assert(SQLITE_OK == sqlite3_prepare_v2(db, cascade_params_insert_qs, sizeof(cascade_params_insert_qs), &cascade_params_insert_stmt, 0))((void) sizeof ((0 == sqlite3_prepare_v2(db, cascade_params_insert_qs , sizeof(cascade_params_insert_qs), &cascade_params_insert_stmt , 0)) ? 1 : 0), __extension__ ({ if (0 == sqlite3_prepare_v2( db, cascade_params_insert_qs, sizeof(cascade_params_insert_qs ), &cascade_params_insert_stmt, 0)) ; else __assert_fail ( "SQLITE_OK == sqlite3_prepare_v2(db, cascade_params_insert_qs, sizeof(cascade_params_insert_qs), &cascade_params_insert_stmt, 0)" , "ccv_scd.c", 1480, __extension__ __PRETTY_FUNCTION__); })); | |||
1481 | sqlite3_bind_int(cascade_params_insert_stmt, 1, cascade->count); | |||
1482 | sqlite3_bind_int(cascade_params_insert_stmt, 2, cascade->margin.left); | |||
1483 | sqlite3_bind_int(cascade_params_insert_stmt, 3, cascade->margin.top); | |||
1484 | sqlite3_bind_int(cascade_params_insert_stmt, 4, cascade->margin.right); | |||
1485 | sqlite3_bind_int(cascade_params_insert_stmt, 5, cascade->margin.bottom); | |||
1486 | sqlite3_bind_int(cascade_params_insert_stmt, 6, cascade->size.width); | |||
1487 | sqlite3_bind_int(cascade_params_insert_stmt, 7, cascade->size.height); | |||
1488 | assert(SQLITE_DONE == sqlite3_step(cascade_params_insert_stmt))((void) sizeof ((101 == sqlite3_step(cascade_params_insert_stmt )) ? 1 : 0), __extension__ ({ if (101 == sqlite3_step(cascade_params_insert_stmt )) ; else __assert_fail ("SQLITE_DONE == sqlite3_step(cascade_params_insert_stmt)" , "ccv_scd.c", 1488, __extension__ __PRETTY_FUNCTION__); })); | |||
1489 | sqlite3_finalize(cascade_params_insert_stmt); | |||
1490 | const char classifier_params_insert_qs[] = | |||
1491 | "REPLACE INTO classifier_params " | |||
1492 | "(classifier, count, threshold) VALUES " | |||
1493 | "($classifier, $count, $threshold);"; | |||
1494 | sqlite3_stmt* classifier_params_insert_stmt = 0; | |||
1495 | assert(SQLITE_OK == sqlite3_prepare_v2(db, classifier_params_insert_qs, sizeof(classifier_params_insert_qs), &classifier_params_insert_stmt, 0))((void) sizeof ((0 == sqlite3_prepare_v2(db, classifier_params_insert_qs , sizeof(classifier_params_insert_qs), &classifier_params_insert_stmt , 0)) ? 1 : 0), __extension__ ({ if (0 == sqlite3_prepare_v2( db, classifier_params_insert_qs, sizeof(classifier_params_insert_qs ), &classifier_params_insert_stmt, 0)) ; else __assert_fail ("SQLITE_OK == sqlite3_prepare_v2(db, classifier_params_insert_qs, sizeof(classifier_params_insert_qs), &classifier_params_insert_stmt, 0)" , "ccv_scd.c", 1495, __extension__ __PRETTY_FUNCTION__); })); | |||
1496 | const char feature_params_insert_qs[] = | |||
1497 | "REPLACE INTO feature_params " | |||
1498 | "(classifier, id, " | |||
1499 | "sx_0, sy_0, dx_0, dy_0, " | |||
1500 | "sx_1, sy_1, dx_1, dy_1, " | |||
1501 | "sx_2, sy_2, dx_2, dy_2, " | |||
1502 | "sx_3, sy_3, dx_3, dy_3, " | |||
1503 | "bias, w) VALUES " | |||
1504 | "($classifier, $id, " // 1 | |||
1505 | "$sx_0, $sy_0, $dx_0, $dy_0, " // 5 | |||
1506 | "$sx_1, $sy_1, $dx_1, $dy_1, " // 9 | |||
1507 | "$sx_2, $sy_2, $dx_2, $dy_2, " // 13 | |||
1508 | "$sx_3, $sy_3, $dx_3, $dy_3, " // 17 | |||
1509 | "$bias, $w);"; // 19 | |||
1510 | sqlite3_stmt* feature_params_insert_stmt = 0; | |||
1511 | assert(SQLITE_OK == sqlite3_prepare_v2(db, feature_params_insert_qs, sizeof(feature_params_insert_qs), &feature_params_insert_stmt, 0))((void) sizeof ((0 == sqlite3_prepare_v2(db, feature_params_insert_qs , sizeof(feature_params_insert_qs), &feature_params_insert_stmt , 0)) ? 1 : 0), __extension__ ({ if (0 == sqlite3_prepare_v2( db, feature_params_insert_qs, sizeof(feature_params_insert_qs ), &feature_params_insert_stmt, 0)) ; else __assert_fail ( "SQLITE_OK == sqlite3_prepare_v2(db, feature_params_insert_qs, sizeof(feature_params_insert_qs), &feature_params_insert_stmt, 0)" , "ccv_scd.c", 1511, __extension__ __PRETTY_FUNCTION__); })); | |||
1512 | int i, j, k; | |||
1513 | for (i = 0; i < cascade->count; i++) | |||
1514 | { | |||
1515 | ccv_scd_stump_classifier_t* classifier = cascade->classifiers + i; | |||
1516 | sqlite3_bind_int(classifier_params_insert_stmt, 1, i); | |||
1517 | sqlite3_bind_int(classifier_params_insert_stmt, 2, classifier->count); | |||
1518 | sqlite3_bind_double(classifier_params_insert_stmt, 3, classifier->threshold); | |||
1519 | assert(SQLITE_DONE == sqlite3_step(classifier_params_insert_stmt))((void) sizeof ((101 == sqlite3_step(classifier_params_insert_stmt )) ? 1 : 0), __extension__ ({ if (101 == sqlite3_step(classifier_params_insert_stmt )) ; else __assert_fail ("SQLITE_DONE == sqlite3_step(classifier_params_insert_stmt)" , "ccv_scd.c", 1519, __extension__ __PRETTY_FUNCTION__); })); | |||
1520 | sqlite3_reset(classifier_params_insert_stmt); | |||
1521 | sqlite3_clear_bindings(classifier_params_insert_stmt); | |||
1522 | for (j = 0; j < classifier->count; j++) | |||
1523 | { | |||
1524 | ccv_scd_stump_feature_t* feature = classifier->features + j; | |||
1525 | sqlite3_bind_int(feature_params_insert_stmt, 1, i); | |||
1526 | sqlite3_bind_int(feature_params_insert_stmt, 2, j); | |||
1527 | for (k = 0; k < 4; k++) | |||
1528 | { | |||
1529 | sqlite3_bind_int(feature_params_insert_stmt, 3 + k * 4, feature->sx[k]); | |||
1530 | sqlite3_bind_int(feature_params_insert_stmt, 4 + k * 4, feature->sy[k]); | |||
1531 | sqlite3_bind_int(feature_params_insert_stmt, 5 + k * 4, feature->dx[k]); | |||
1532 | sqlite3_bind_int(feature_params_insert_stmt, 6 + k * 4, feature->dy[k]); | |||
1533 | } | |||
1534 | sqlite3_bind_double(feature_params_insert_stmt, 19, feature->bias); | |||
1535 | sqlite3_bind_blob(feature_params_insert_stmt, 20, feature->w, sizeof(float) * 32, SQLITE_STATIC((sqlite3_destructor_type)0)); | |||
1536 | assert(SQLITE_DONE == sqlite3_step(feature_params_insert_stmt))((void) sizeof ((101 == sqlite3_step(feature_params_insert_stmt )) ? 1 : 0), __extension__ ({ if (101 == sqlite3_step(feature_params_insert_stmt )) ; else __assert_fail ("SQLITE_DONE == sqlite3_step(feature_params_insert_stmt)" , "ccv_scd.c", 1536, __extension__ __PRETTY_FUNCTION__); })); | |||
1537 | sqlite3_reset(feature_params_insert_stmt); | |||
1538 | sqlite3_clear_bindings(feature_params_insert_stmt); | |||
1539 | } | |||
1540 | } | |||
1541 | sqlite3_finalize(classifier_params_insert_stmt); | |||
1542 | sqlite3_finalize(feature_params_insert_stmt); | |||
1543 | sqlite3_close(db); | |||
1544 | } | |||
1545 | } | |||
1546 | ||||
1547 | ccv_scd_classifier_cascade_t* ccv_scd_classifier_cascade_read(const char* filename) | |||
1548 | { | |||
1549 | int i; | |||
1550 | sqlite3* db = 0; | |||
1551 | ccv_scd_classifier_cascade_t* cascade = 0; | |||
1552 | if (SQLITE_OK0 == sqlite3_open(filename, &db)) | |||
1553 | { | |||
1554 | const char cascade_params_qs[] = | |||
1555 | "SELECT count, " // 1 | |||
1556 | "margin_left, margin_top, margin_right, margin_bottom, " // 5 | |||
1557 | "size_width, size_height FROM cascade_params WHERE id = 0;"; // 7 | |||
1558 | sqlite3_stmt* cascade_params_stmt = 0; | |||
1559 | if (SQLITE_OK0 == sqlite3_prepare_v2(db, cascade_params_qs, sizeof(cascade_params_qs), &cascade_params_stmt, 0)) | |||
1560 | { | |||
1561 | if (sqlite3_step(cascade_params_stmt) == SQLITE_ROW100) | |||
1562 | { | |||
1563 | cascade = (ccv_scd_classifier_cascade_t*)ccmallocmalloc(sizeof(ccv_scd_classifier_cascade_t)); | |||
1564 | cascade->count = sqlite3_column_int(cascade_params_stmt, 0); | |||
1565 | cascade->classifiers = (ccv_scd_stump_classifier_t*)cccalloccalloc(cascade->count, sizeof(ccv_scd_stump_classifier_t)); | |||
1566 | cascade->margin = ccv_margin(sqlite3_column_int(cascade_params_stmt, 1), sqlite3_column_int(cascade_params_stmt, 2), sqlite3_column_int(cascade_params_stmt, 3), sqlite3_column_int(cascade_params_stmt, 4)); | |||
1567 | cascade->size = ccv_size(sqlite3_column_int(cascade_params_stmt, 5), sqlite3_column_int(cascade_params_stmt, 6)); | |||
1568 | } | |||
1569 | sqlite3_finalize(cascade_params_stmt); | |||
1570 | } | |||
1571 | if (cascade
| |||
1572 | { | |||
1573 | const char classifier_params_qs[] = | |||
1574 | "SELECT classifier, count, threshold FROM classifier_params ORDER BY classifier ASC;"; | |||
1575 | sqlite3_stmt* classifier_params_stmt = 0; | |||
1576 | if (SQLITE_OK0 == sqlite3_prepare_v2(db, classifier_params_qs, sizeof(classifier_params_qs), &classifier_params_stmt, 0)) | |||
1577 | { | |||
1578 | while (sqlite3_step(classifier_params_stmt) == SQLITE_ROW100) | |||
1579 | if (sqlite3_column_int(classifier_params_stmt, 0) < cascade->count) | |||
1580 | { | |||
1581 | ccv_scd_stump_classifier_t* classifier = cascade->classifiers + sqlite3_column_int(classifier_params_stmt, 0); | |||
1582 | classifier->count = sqlite3_column_int(classifier_params_stmt, 1); | |||
1583 | classifier->features = (ccv_scd_stump_feature_t*)ccmallocmalloc(sizeof(ccv_scd_stump_feature_t) * classifier->count); | |||
1584 | classifier->threshold = (float)sqlite3_column_double(classifier_params_stmt, 2); | |||
1585 | } | |||
1586 | sqlite3_finalize(classifier_params_stmt); | |||
1587 | } | |||
1588 | const char feature_params_qs[] = | |||
1589 | "SELECT classifier, id, " | |||
1590 | "sx_0, sy_0, dx_0, dy_0, " | |||
1591 | "sx_1, sy_1, dx_1, dy_1, " | |||
1592 | "sx_2, sy_2, dx_2, dy_2, " | |||
1593 | "sx_3, sy_3, dx_3, dy_3, " | |||
1594 | "bias, w FROM feature_params ORDER BY classifier, id ASC;"; | |||
1595 | sqlite3_stmt* feature_params_stmt = 0; | |||
1596 | if (SQLITE_OK0 == sqlite3_prepare_v2(db, feature_params_qs, sizeof(feature_params_qs), &feature_params_stmt, 0)) | |||
1597 | { | |||
1598 | while (sqlite3_step(feature_params_stmt) == SQLITE_ROW100) | |||
1599 | if (sqlite3_column_int(feature_params_stmt, 0) < cascade->count) | |||
1600 | { | |||
1601 | ccv_scd_stump_classifier_t* classifier = cascade->classifiers + sqlite3_column_int(feature_params_stmt, 0); | |||
1602 | if (sqlite3_column_int(feature_params_stmt, 1) < classifier->count) | |||
1603 | { | |||
1604 | ccv_scd_stump_feature_t* feature = classifier->features + sqlite3_column_int(feature_params_stmt, 1); | |||
1605 | for (i = 0; i < 4; i++) | |||
1606 | { | |||
1607 | feature->sx[i] = sqlite3_column_int(feature_params_stmt, 2 + i * 4); | |||
| ||||
1608 | feature->sy[i] = sqlite3_column_int(feature_params_stmt, 3 + i * 4); | |||
1609 | feature->dx[i] = sqlite3_column_int(feature_params_stmt, 4 + i * 4); | |||
1610 | feature->dy[i] = sqlite3_column_int(feature_params_stmt, 5 + i * 4); | |||
1611 | } | |||
1612 | feature->bias = (float)sqlite3_column_double(feature_params_stmt, 18); | |||
1613 | int wnum = sqlite3_column_bytes(feature_params_stmt, 19); | |||
1614 | assert(wnum == 32 * sizeof(float))((void) sizeof ((wnum == 32 * sizeof(float)) ? 1 : 0), __extension__ ({ if (wnum == 32 * sizeof(float)) ; else __assert_fail ("wnum == 32 * sizeof(float)" , "ccv_scd.c", 1614, __extension__ __PRETTY_FUNCTION__); })); | |||
1615 | const void* w = sqlite3_column_blob(feature_params_stmt, 19); | |||
1616 | memcpy(feature->w, w, sizeof(float) * 32); | |||
1617 | } | |||
1618 | } | |||
1619 | sqlite3_finalize(feature_params_stmt); | |||
1620 | } | |||
1621 | } | |||
1622 | sqlite3_close(db); | |||
1623 | } | |||
1624 | return cascade; | |||
1625 | } | |||
1626 | ||||
1627 | void ccv_scd_classifier_cascade_free(ccv_scd_classifier_cascade_t* cascade) | |||
1628 | { | |||
1629 | int i; | |||
1630 | for (i = 0; i < cascade->count; i++) | |||
1631 | { | |||
1632 | ccv_scd_stump_classifier_t* classifier = cascade->classifiers + i; | |||
1633 | ccfreefree(classifier->features); | |||
1634 | } | |||
1635 | ccfreefree(cascade->classifiers); | |||
1636 | ccfreefree(cascade); | |||
1637 | } | |||
1638 | ||||
1639 | static int _ccv_is_equal_same_class(const void* _r1, const void* _r2, void* data) | |||
1640 | { | |||
1641 | const ccv_comp_t* r1 = (const ccv_comp_t*)_r1; | |||
1642 | const ccv_comp_t* r2 = (const ccv_comp_t*)_r2; | |||
1643 | ||||
1644 | if (r2->classification.id != r1->classification.id) | |||
1645 | return 0; | |||
1646 | ||||
1647 | int i = ccv_max(ccv_min(r2->rect.x + r2->rect.width, r1->rect.x + r1->rect.width) - ccv_max(r2->rect.x, r1->rect.x), 0)({ typeof (({ typeof (r2->rect.x + r2->rect.width) _a = (r2->rect.x + r2->rect.width); typeof (r1->rect.x + r1->rect.width) _b = (r1->rect.x + r1->rect.width); (_a < _b) ? _a : _b; }) - ({ typeof (r2->rect.x) _a = ( r2->rect.x); typeof (r1->rect.x) _b = (r1->rect.x); ( _a > _b) ? _a : _b; })) _a = (({ typeof (r2->rect.x + r2 ->rect.width) _a = (r2->rect.x + r2->rect.width); typeof (r1->rect.x + r1->rect.width) _b = (r1->rect.x + r1 ->rect.width); (_a < _b) ? _a : _b; }) - ({ typeof (r2-> rect.x) _a = (r2->rect.x); typeof (r1->rect.x) _b = (r1 ->rect.x); (_a > _b) ? _a : _b; })); typeof (0) _b = (0 ); (_a > _b) ? _a : _b; }) * ccv_max(ccv_min(r2->rect.y + r2->rect.height, r1->rect.y + r1->rect.height) - ccv_max(r2->rect.y, r1->rect.y), 0)({ typeof (({ typeof (r2->rect.y + r2->rect.height) _a = (r2->rect.y + r2->rect.height); typeof (r1->rect.y + r1->rect.height) _b = (r1->rect.y + r1->rect.height ); (_a < _b) ? _a : _b; }) - ({ typeof (r2->rect.y) _a = (r2->rect.y); typeof (r1->rect.y) _b = (r1->rect.y) ; (_a > _b) ? _a : _b; })) _a = (({ typeof (r2->rect.y + r2->rect.height) _a = (r2->rect.y + r2->rect.height ); typeof (r1->rect.y + r1->rect.height) _b = (r1->rect .y + r1->rect.height); (_a < _b) ? _a : _b; }) - ({ typeof (r2->rect.y) _a = (r2->rect.y); typeof (r1->rect.y) _b = (r1->rect.y); (_a > _b) ? _a : _b; })); typeof (0 ) _b = (0); (_a > _b) ? _a : _b; }); | |||
1648 | int m = ccv_min(r2->rect.width * r2->rect.height, r1->rect.width * r1->rect.height)({ typeof (r2->rect.width * r2->rect.height) _a = (r2-> rect.width * r2->rect.height); typeof (r1->rect.width * r1->rect.height) _b = (r1->rect.width * r1->rect.height ); (_a < _b) ? _a : _b; }); | |||
1649 | ||||
1650 | return i >= 0.3 * m; // IoM > 0.3 like HeadHunter does | |||
1651 | } | |||
1652 | ||||
1653 | ccv_array_t* ccv_scd_detect_objects(ccv_dense_matrix_t* a, ccv_scd_classifier_cascade_t** cascades, int count, ccv_scd_param_t params) | |||
1654 | { | |||
1655 | int i, j, k, x, y, p, q; | |||
1656 | int scale_upto = 1; | |||
1657 | float up_ratio = 1.0; | |||
1658 | for (i = 0; i < count; i++) | |||
1659 | up_ratio = ccv_max(up_ratio, ccv_max((float)cascades[i]->size.width / params.size.width, (float)cascades[i]->size.height / params.size.height))({ typeof (up_ratio) _a = (up_ratio); typeof (({ typeof ((float )cascades[i]->size.width / params.size.width) _a = ((float )cascades[i]->size.width / params.size.width); typeof ((float )cascades[i]->size.height / params.size.height) _b = ((float )cascades[i]->size.height / params.size.height); (_a > _b ) ? _a : _b; })) _b = (({ typeof ((float)cascades[i]->size .width / params.size.width) _a = ((float)cascades[i]->size .width / params.size.width); typeof ((float)cascades[i]->size .height / params.size.height) _b = ((float)cascades[i]->size .height / params.size.height); (_a > _b) ? _a : _b; })); ( _a > _b) ? _a : _b; }); | |||
1660 | if (up_ratio - 1.0 > 1e-4) | |||
1661 | { | |||
1662 | ccv_dense_matrix_t* resized = 0; | |||
1663 | ccv_resample(a, &resized, 0, up_ratio, up_ratio, CCV_INTER_CUBIC); | |||
1664 | a = resized; | |||
1665 | } | |||
1666 | for (i = 0; i < count; i++) | |||
1667 | scale_upto = ccv_max(scale_upto, (int)(log(ccv_min((double)a->rows / (cascades[i]->size.height - cascades[i]->margin.top - cascades[i]->margin.bottom), (double)a->cols / (cascades[i]->size.width - cascades[i]->margin.left - cascades[i]->margin.right))) / log(2.) - DBL_MIN) + 1)({ typeof (scale_upto) _a = (scale_upto); typeof ((int)(log(( { typeof ((double)a->rows / (cascades[i]->size.height - cascades[i]->margin.top - cascades[i]->margin.bottom)) _a = ((double)a->rows / (cascades[i]->size.height - cascades [i]->margin.top - cascades[i]->margin.bottom)); typeof ( (double)a->cols / (cascades[i]->size.width - cascades[i ]->margin.left - cascades[i]->margin.right)) _b = ((double )a->cols / (cascades[i]->size.width - cascades[i]->margin .left - cascades[i]->margin.right)); (_a < _b) ? _a : _b ; })) / log(2.) - 2.2250738585072014e-308) + 1) _b = ((int)(log (({ typeof ((double)a->rows / (cascades[i]->size.height - cascades[i]->margin.top - cascades[i]->margin.bottom )) _a = ((double)a->rows / (cascades[i]->size.height - cascades [i]->margin.top - cascades[i]->margin.bottom)); typeof ( (double)a->cols / (cascades[i]->size.width - cascades[i ]->margin.left - cascades[i]->margin.right)) _b = ((double )a->cols / (cascades[i]->size.width - cascades[i]->margin .left - cascades[i]->margin.right)); (_a < _b) ? _a : _b ; })) / log(2.) - 2.2250738585072014e-308) + 1); (_a > _b) ? _a : _b; }); | |||
1668 | ccv_dense_matrix_t** pyr = (ccv_dense_matrix_t**)alloca(sizeof(ccv_dense_matrix_t*) * scale_upto)__builtin_alloca (sizeof(ccv_dense_matrix_t*) * scale_upto); | |||
1669 | pyr[0] = a; | |||
1670 | for (i = 1; i < scale_upto; i++) | |||
1671 | { | |||
1672 | pyr[i] = 0; | |||
1673 | ccv_sample_down(pyr[i - 1], &pyr[i], 0, 0, 0); | |||
1674 | } | |||
1675 | #if defined(HAVE_SSE21) | |||
1676 | __m128 surf[8]; | |||
1677 | #else | |||
1678 | float surf[32]; | |||
1679 | #endif | |||
1680 | ccv_array_t** seq = (ccv_array_t**)alloca(sizeof(ccv_array_t*) * count)__builtin_alloca (sizeof(ccv_array_t*) * count); | |||
1681 | for (i = 0; i < count; i++) | |||
1682 | seq[i] = ccv_array_new(sizeof(ccv_comp_t), 64, 0); | |||
1683 | for (i = 0; i < scale_upto; i++) | |||
1684 | { | |||
1685 | // run it | |||
1686 | for (j = 0; j < count; j++) | |||
1687 | { | |||
1688 | double scale_ratio = pow(2., 1. / (params.interval + 1)); | |||
1689 | double scale = 1; | |||
1690 | ccv_scd_classifier_cascade_t* cascade = cascades[j]; | |||
1691 | for (k = 0; k <= params.interval; k++) | |||
1692 | { | |||
1693 | int rows = (int)(pyr[i]->rows / scale + 0.5); | |||
1694 | int cols = (int)(pyr[i]->cols / scale + 0.5); | |||
1695 | if (rows < cascade->size.height || cols < cascade->size.width) | |||
1696 | break; | |||
1697 | ccv_dense_matrix_t* image = k == 0 ? pyr[i] : 0; | |||
1698 | if (k > 0) | |||
1699 | ccv_resample(pyr[i], &image, 0, (double)rows / (double)pyr[i]->rows, (double)cols / (double)pyr[i]->cols, CCV_INTER_AREA); | |||
1700 | ccv_dense_matrix_t* scd = 0; | |||
1701 | if (cascade->margin.left == 0 && cascade->margin.top == 0 && cascade->margin.right == 0 && cascade->margin.bottom == 0) | |||
1702 | { | |||
1703 | ccv_scd(image, &scd, 0); | |||
1704 | if (k > 0) | |||
1705 | ccv_matrix_free(image); | |||
1706 | } else { | |||
1707 | ccv_dense_matrix_t* bordered = 0; | |||
1708 | ccv_border(image, (ccv_matrix_t**)&bordered, 0, cascade->margin); | |||
1709 | if (k > 0) | |||
1710 | ccv_matrix_free(image); | |||
1711 | ccv_scd(bordered, &scd, 0); | |||
1712 | ccv_matrix_free(bordered); | |||
1713 | } | |||
1714 | ccv_dense_matrix_t* sat = 0; | |||
1715 | ccv_sat(scd, &sat, 0, CCV_PADDING_ZERO); | |||
1716 | assert(CCV_GET_CHANNEL(sat->type) == CCV_SCD_CHANNEL)((void) sizeof ((((sat->type) & 0xFFF) == (11)) ? 1 : 0 ), __extension__ ({ if (((sat->type) & 0xFFF) == (11)) ; else __assert_fail ("CCV_GET_CHANNEL(sat->type) == CCV_SCD_CHANNEL" , "ccv_scd.c", 1716, __extension__ __PRETTY_FUNCTION__); })); | |||
1717 | ccv_matrix_free(scd); | |||
1718 | float* ptr = sat->data.f32; | |||
1719 | for (y = 0; y < rows; y += params.step_through) | |||
1720 | { | |||
1721 | if (y >= sat->rows - cascade->size.height - 1) | |||
1722 | break; | |||
1723 | for (x = 0; x < cols; x += params.step_through) | |||
1724 | { | |||
1725 | if (x >= sat->cols - cascade->size.width - 1) | |||
1726 | break; | |||
1727 | int pass = 1; | |||
1728 | float sum = 0; | |||
1729 | for (p = 0; p < cascade->count; p++) | |||
1730 | { | |||
1731 | ccv_scd_stump_classifier_t* classifier = cascade->classifiers + p; | |||
1732 | float v = 0; | |||
1733 | for (q = 0; q < classifier->count; q++) | |||
1734 | { | |||
1735 | ccv_scd_stump_feature_t* feature = classifier->features + q; | |||
1736 | #if defined(HAVE_SSE21) | |||
1737 | _ccv_scd_run_feature_at_sse2(ptr + x * CCV_SCD_CHANNEL(11), sat->cols, feature, surf); | |||
1738 | __m128 u0 = _mm_add_ps(_mm_mul_ps(surf[0], _mm_loadu_ps(feature->w)), _mm_mul_ps(surf[1], _mm_loadu_ps(feature->w + 4))); | |||
1739 | __m128 u1 = _mm_add_ps(_mm_mul_ps(surf[2], _mm_loadu_ps(feature->w + 8)), _mm_mul_ps(surf[3], _mm_loadu_ps(feature->w + 12))); | |||
1740 | __m128 u2 = _mm_add_ps(_mm_mul_ps(surf[4], _mm_loadu_ps(feature->w + 16)), _mm_mul_ps(surf[5], _mm_loadu_ps(feature->w + 20))); | |||
1741 | __m128 u3 = _mm_add_ps(_mm_mul_ps(surf[6], _mm_loadu_ps(feature->w + 24)), _mm_mul_ps(surf[7], _mm_loadu_ps(feature->w + 28))); | |||
1742 | u0 = _mm_add_ps(u0, u1); | |||
1743 | u2 = _mm_add_ps(u2, u3); | |||
1744 | union { | |||
1745 | float f[4]; | |||
1746 | __m128 p; | |||
1747 | } ux; | |||
1748 | ux.p = _mm_add_ps(u0, u2); | |||
1749 | float u = expf(feature->bias + ux.f[0] + ux.f[1] + ux.f[2] + ux.f[3]); | |||
1750 | #else | |||
1751 | _ccv_scd_run_feature_at(ptr + x * CCV_SCD_CHANNEL(11), sat->cols, feature, surf); | |||
1752 | float u = feature->bias; | |||
1753 | int r; | |||
1754 | for (r = 0; r < 32; r++) | |||
1755 | u += surf[r] * feature->w[r]; | |||
1756 | u = expf(u); | |||
1757 | #endif | |||
1758 | v += (u - 1) / (u + 1); | |||
1759 | } | |||
1760 | if (v <= classifier->threshold) | |||
1761 | { | |||
1762 | pass = 0; | |||
1763 | break; | |||
1764 | } | |||
1765 | sum = v / classifier->count; | |||
1766 | } | |||
1767 | if (pass) | |||
1768 | { | |||
1769 | ccv_comp_t comp; | |||
1770 | comp.rect = ccv_rect((int)((x + 0.5) * (scale / up_ratio) * (1 << i) - 0.5), | |||
1771 | (int)((y + 0.5) * (scale / up_ratio) * (1 << i) - 0.5), | |||
1772 | (cascade->size.width - cascade->margin.left - cascade->margin.right) * (scale / up_ratio) * (1 << i), | |||
1773 | (cascade->size.height - cascade->margin.top - cascade->margin.bottom) * (scale / up_ratio) * (1 << i)); | |||
1774 | comp.neighbors = 1; | |||
1775 | comp.classification.id = j + 1; | |||
1776 | comp.classification.confidence = sum + (cascade->count - 1); | |||
1777 | ccv_array_push(seq[j], &comp); | |||
1778 | } | |||
1779 | } | |||
1780 | ptr += sat->cols * CCV_SCD_CHANNEL(11) * params.step_through; | |||
1781 | } | |||
1782 | ccv_matrix_free(sat); | |||
1783 | scale *= scale_ratio; | |||
1784 | } | |||
1785 | } | |||
1786 | } | |||
1787 | ||||
1788 | for (i = 1; i < scale_upto; i++) | |||
1789 | ccv_matrix_free(pyr[i]); | |||
1790 | if (up_ratio - 1.0 > 1e-4) | |||
1791 | ccv_matrix_free(a); | |||
1792 | ||||
1793 | ccv_array_t* result_seq = ccv_array_new(sizeof(ccv_comp_t), 64, 0); | |||
1794 | for (k = 0; k < count; k++) | |||
1795 | { | |||
1796 | /* simple non-maximum suppression, we merge when intersected area / min area > 0.3 */ | |||
1797 | if(params.min_neighbors == 0) | |||
1798 | { | |||
1799 | for (i = 0; i < seq[k]->rnum; i++) | |||
1800 | { | |||
1801 | ccv_comp_t* comp = (ccv_comp_t*)ccv_array_get(seq[k], i)((void*)(((char*)((seq[k])->data)) + (size_t)(seq[k])-> rsize * (size_t)(i))); | |||
1802 | ccv_array_push(result_seq, comp); | |||
1803 | } | |||
1804 | } else { | |||
1805 | ccv_array_t* idx_seq = 0; | |||
1806 | // group retrieved rectangles in order to filter out noise | |||
1807 | int ncomp = ccv_array_group(seq[k], &idx_seq, _ccv_is_equal_same_class, 0); | |||
1808 | ccv_comp_t* comps = (ccv_comp_t*)cccalloccalloc(ncomp + 1, sizeof(ccv_comp_t)); | |||
1809 | ||||
1810 | // count number of neighbors | |||
1811 | for (i = 0; i < seq[k]->rnum; i++) | |||
1812 | { | |||
1813 | ccv_comp_t r1 = *(ccv_comp_t*)ccv_array_get(seq[k], i)((void*)(((char*)((seq[k])->data)) + (size_t)(seq[k])-> rsize * (size_t)(i))); | |||
1814 | int idx = *(int*)ccv_array_get(idx_seq, i)((void*)(((char*)((idx_seq)->data)) + (size_t)(idx_seq)-> rsize * (size_t)(i))); | |||
1815 | ||||
1816 | comps[idx].classification.id = r1.classification.id; | |||
1817 | if (r1.classification.confidence > comps[idx].classification.confidence || comps[idx].neighbors == 0) | |||
1818 | { | |||
1819 | comps[idx].rect = r1.rect; | |||
1820 | comps[idx].classification.confidence = r1.classification.confidence; | |||
1821 | } | |||
1822 | ||||
1823 | ++comps[idx].neighbors; | |||
1824 | } | |||
1825 | ||||
1826 | // push merged bounding box to result_seq | |||
1827 | for (i = 0; i < ncomp; i++) | |||
1828 | { | |||
1829 | int n = comps[i].neighbors; | |||
1830 | if (n >= params.min_neighbors) | |||
1831 | ccv_array_push(result_seq, comps + i); | |||
1832 | } | |||
1833 | ccv_array_free(idx_seq); | |||
1834 | ccfreefree(comps); | |||
1835 | } | |||
1836 | ccv_array_free(seq[k]); | |||
1837 | } | |||
1838 | ||||
1839 | return result_seq; | |||
1840 | } |