Work at SourceForge, help us to make it a better place! We have an immediate need for a Support Technician in our San Francisco or Denver office.

Close

[r36929]: trunk / contrib / brl / bseg / boxm2 / ocl / cl / statistics_library_functions.cl Maximize Restore History

Download this file

statistics_library_functions.cl    588 lines (549 with data), 18.4 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
#pragma OPENCL EXTENSION cl_khr_byte_addressable_store : enable
// "dot" doesnt seem to be defined for float16
float __OVERLOADABLE__dot(float16 x, float16 y)
{
return x.s0*y.s0 + x.s1*y.s1 + x.s2*y.s2 + x.s3*y.s3 + x.s4*y.s4 + x.s5*y.s5 + x.s6*y.s6 + x.s7*y.s7 + x.s8*y.s8 + x.s9*y.s9 + x.sa*y.sa + x.sb*y.sb + x.sc*y.sc + x.sd*y.sd + x.se*y.se + x.sf*y.sf;
}
float gauss_prob_density(float x, float mu, float sigma)
{
if (sigma <= 0.0f)
return 1.0f;
float ratio=(x - mu)/sigma;
return 0.398942280f*exp(-0.5f*ratio*ratio)/sigma;
}
#if 0
float16 gauss_prob_density_f16(float16 *x, float mu, float sigma)
{
if (sigma <= 0.0f)
return 1.0f;
float16 ratio=(*x - mu)/sigma;
return 0.398942280f*exp(-0.5f*ratio*ratio)/sigma;
}
#endif
float16 gauss_prob_density_f16(float16 *x, float mu, float16 *sigma)
{
//if (sigma <= 0.0f)
// return 1.0f;
float16 ratio=(*x - mu)/ *sigma;
return 0.398942280f*exp(-0.5f*ratio*ratio)/ *sigma;
}
//3D gaussian with assumed 3 independent dimensions
//Gaussian parameters stored are Mus and Sigma (not sigma^2)
inline float gauss_prob_density_rgb(float4 x, float4 mu, float4 sigma)
{
if ( any(sigma <= 0.0f) )
return 1.0f;
float4 pwr = (x-mu)*(x-mu) / (sigma*sigma);
return 0.0634936359f*(1.0f/(sigma.x*sigma.y*sigma.z))*exp(-0.5f*(pwr.x+pwr.y+pwr.z));
//return 0.398942280f*exp(-0.5f*(x.x - mu.x)*(x.x - mu.x)/(sigma.x*sigma.x))/sigma.x;
}
// This mixture distribution function uses the value of the weights to
// determine the number of components. A weight of 0 indicates the
// component is not used
float gauss_3_mixture_prob_density(float x,
float mu0, float sigma0, float w0,
float mu1, float sigma1, float w1,
float mu2, float sigma2, float w2)
{
float sum = 0.0f;
float sum_weights=0.0f;
if (w0>0.0f && sigma0 >0.0f)
{
sum += w0*gauss_prob_density(x, mu0, sigma0);
sum_weights+=w0;
if (w1>0.0f && sigma1 >0.0f)
{
sum += w1*gauss_prob_density(x, mu1, sigma1);
sum_weights+=w1;
if (w2>0.0f && sigma2 >0.0f)
{
sum += w2*gauss_prob_density(x, mu2, sigma2);
sum_weights+=w2;
}
}
if (sum_weights>0.0f)
sum/=sum_weights;
}
else
sum=1.0f;
return sum;
}
//Calculates the value of a mixture of 2, 3-d gaussians with independent
//dimensions. A weight of 0 indicates that the component is not used...
float gauss_2_mixture_rgb_prob_density(float4 x,
float4 mu0, float4 sigma0, float w0,
float4 mu1, float4 sigma1, float w1)
{
float sum = 0.0f;
float sum_weights=0.0f;
if (w0>0.0f && all(sigma0>0.0f))
{
sum += w0*gauss_prob_density_rgb(x, mu0, sigma0);
sum_weights+=w0;
if (w1>0.0f && all(sigma1>0.0f))
{
sum += w1*gauss_prob_density_rgb(x, mu1, sigma1);
sum_weights+=w1;
}
if (sum_weights>0.0f)
sum/=sum_weights;
}
else
sum=.5f;
return sum;
}
//
// update a single 1d gaussian with a sample. The gaussian parameters are
// mutated in place.
//
void update_gauss(float x, float rho, float* mu, float* sigma,
float min_sigma)
{
float var = (*sigma)*(*sigma);
float diff = x-(*mu);
//var = (1.0f-rho)*(var +rho*diff*diff);
*mu = *mu + rho*diff;
*sigma = sqrt(var);
*sigma = (*sigma < min_sigma)? min_sigma: *sigma;
}
//
// update a singel 3d (independent) gaussian with a sample. params
// are mutated in place
//
void update_gauss_3d(float4 x, float rho, float4* mu, float4* sigma, float min_sigma)
{
float4 var = (*sigma)*(*sigma);
float4 diff = x-(*mu);
var = (1.0f-rho)*(var+rho*diff*diff);
*mu = *mu + rho*diff;
*sigma = max(sqrt(var), min_sigma);
}
void sort_mix_3(float* mu0, float* sigma0, float* w0, short* Nobs0,
float* mu1, float* sigma1, float* w1, short* Nobs1,
float* mu2, float* sigma2, float* w2, short* Nobs2)
{
if ((*w1)>0.0f && (*sigma1)>0.0f )
{ // no need to sort
float fa = (*w0)/(*sigma0), fb = (*w1)/(*sigma1);
if ((*w2)==0.0f || (*sigma2)==0.0f) {
if (fa<fb) {//only need to swap a and b
float tmu0 = *mu0, tsig0 = *sigma0, tw0 = *w0;
short n0 = *Nobs0;
*mu0 = *mu1; *sigma0 = *sigma1; *w0 = *w1; *Nobs0 = *Nobs1;
*mu1 = tmu0; *sigma1 = tsig0; *w1 = tw0; *Nobs1 = n0;
return ;
}
else return;
}
float fc = (*w2)/(*sigma2);
if (fa>=fb&&fb>=fc)// [a b c ] - already sorted
return;
if (fa<fb&&fb<fc)// [c b a] - swap a and c
{
float tmu0 = *mu0, tsig0 = *sigma0, tw0 = *w0;
short n0 = *Nobs0;
*mu0 = *mu2; *sigma0 = *sigma2; *w0 = *w2; *Nobs0 = *Nobs2;
*mu2 = tmu0; *sigma2 = tsig0; *w2 = tw0; *Nobs2 = n0;
return ;
}
if (fa<fb&&fb>=fc) {
if (fa>=fc)// [b a c] - c stays where it is and a b swap
{
float tmu0 = *mu0, tsig0 = *sigma0, tw0 = *w0;
short n0 = *Nobs0;
*mu0 = *mu1; *sigma0 = *sigma1; *w0 = *w1; *Nobs0 = *Nobs1;
*mu1 = tmu0; *sigma1 = tsig0; *w1 = tw0; *Nobs1 = n0;
return;
}
else{
// [b c a] - two swaps
float tmu0 = *mu0, tsig0 = *sigma0, tw0 = *w0;
short n0 = *Nobs0;
*mu0 = *mu1; *sigma0 = *sigma1; *w0 = *w1; *Nobs0 = *Nobs1;
*mu1 = *mu2; *sigma1 = *sigma2; *w1 = *w2; *Nobs1 = *Nobs2;
*mu2 = tmu0; *sigma2 = tsig0; *w2 = tw0; *Nobs2 = n0;
return;
}
}
if (fa>=fb&&fb<fc) {
if (fa>=fc)// [a c b] - b and c swap
{
float tmu1 = *mu1, tsig1 = *sigma1, tw1 = *w1;
short n1 = *Nobs1;
*mu1 = *mu2; *sigma1 = *sigma2; *w1 = *w2; *Nobs1 = *Nobs2;
*mu2 = tmu1; *sigma2 = tsig1; *w2 = tw1; *Nobs2 = n1;
return;
}
else{
// [c a b] - two swaps
float tmu0 = *mu0, tsig0 = *sigma0, tw0 = *w0;
short n0 = *Nobs0;
*mu0 = *mu2; *sigma0 = *sigma2; *w0 = *w2; *Nobs0 = *Nobs2;
*mu2 = *mu1; *sigma2 = *sigma1; *w2 = *w1; *Nobs2 = *Nobs1;
*mu1 = tmu0; *sigma1 = tsig0; *w1 = tw0; *Nobs1 = n0;
return;
}
}
}
}
void sort_mix_2(float4* mu0, float4* sigma0, float* w0, short* Nobs0,
float4* mu1, float4* sigma1, float* w1, short* Nobs1)
{
// if second component exists, then sort
if ( (*w1)>0.0f && all((*sigma1)>0.0f))
{
float4 fa = (*w0)/(*sigma0), fb = (*w1)/(*sigma1);
float faSum = fa.x+fa.y+fa.z, fbSum = fb.x+fb.y+fb.z;
if (faSum < fbSum) { // if fa is less than fb swap em
float4 tmu0 = *mu0, tsigma0 = *sigma0;
float tw0 = *w0;
short n0 = *Nobs0;
*mu0 = *mu1; *sigma0 = *sigma1; *w0 = *w1; *Nobs0 = *Nobs1;
*mu1 = tmu0; *sigma1 = tsigma0; *w1 = tw0; *Nobs1 = n0;
}
}
}
void insert_gauss_3(float x, float init_weight, float init_sigma, int* match,
float* mu0, float* sigma0, float* w0, short* Nobs0,
float* mu1, float* sigma1, float* w1, short* Nobs1,
float* mu2, float* sigma2, float* w2, short* Nobs2)
{
if ((*w1)>0.0f && (*sigma1)>0.0f) // replace the third component
{
float adjust = *w0 + *w1;
adjust = (1.0f - init_weight)/adjust;
*w0 = (*w0)*adjust; *w1 = (*w1)*adjust;
*mu2 = x;
*sigma2 = init_sigma;
*w2 = init_weight;
*Nobs2 = 1;
*match = 2;
return;
}
else if ((*w0)>0.0f) {// replace the second component
*w0 = (1.0f-init_weight);
*mu1 = x;
*sigma1 = init_sigma;
*w1 = init_weight;
*Nobs1 = 1;
*match = 1;
return;
}
else { // replace the first component
// note that in C++ the weights don't sum to 1?
// see bsta_adaptive_updater.h - ::insert bug?
*w0 = 1.0f; //init_weight in C++
*mu0 = x;
*sigma0 = init_sigma;
*Nobs0 = 1;
*match = 0;
}
}
void insert_gauss_2(float4 x, float init_weight, float init_sigma, int* match,
float4* mu0, float4* sigma0, float* w0, short* Nobs0,
float4* mu1, float4* sigma1, float* w1, short* Nobs1)
{
if ((*w0)>0.0f && all((*sigma0)>0.0f)) {// replace the second component
*w0 = (1.0f-init_weight);
*mu1 = x;
*sigma1 = (float4) init_sigma;
*w1 = init_weight;
*Nobs1 = 1;
*match = 1;
return;
}
else{// replace the first component
// note that in C++ the weights don't sum to 1?
// see bsta_adaptive_updater.h - ::insert bug?
*w0 = 1.0f; //init_weight in C++
*mu0 = x;
*sigma0 = (float4) init_sigma;
*Nobs0 = 1;
*match = 0;
}
}
//
// update a 1d gaussian mixture with a sample. The mixture parameters are
// mutated in place. Note that the number of components can vary between
// 0 and 3 as the updating proceeds. Initially, the mixture contains no
// components. The number of observations for each component is Nobs(i).
//
// The number of observations for the whole mixture is Nobs_mix. When
//
// a new component is inserted in the mixture, it is given a mean of x
// and a sigma of min_sigma. The threshold t_match determines the maximum
// Mahalanobis distance that the sample can have in order to be considered
// to belong to a given component.
//
void update_gauss_3_mixture(float x, float w, float t_match,
float init_sigma, float min_sigma,
float* mu0, float* sigma0, float* w0, short* Nobs0,
float* mu1, float* sigma1, float* w1, short* Nobs1,
float* mu2, float* sigma2, float* w2, short* Nobs2,
float* Nobs_mix)
{
if (w>0.0f)
{
int match = -1;
(*Nobs_mix) += w;
float alpha = w/(*Nobs_mix), tsq=t_match*t_match;
float weight = 0.0f, rho = 0.0f;
// test for a match of component 0
if (*w0>0.0f && (*sigma0)>0.0f) {
weight = (1.0f-alpha)*(*w0);
if (match<0 &&
(x-*mu0)*(x-*mu0)/((*sigma0)*(*sigma0)) < tsq) {
weight += alpha;
(*Nobs0)++;
rho = (1.0f-alpha)/((float)*Nobs0) + alpha;
update_gauss(x, rho, mu0, sigma0, min_sigma);
match = 0;
}
*w0 = weight;
}
// test for a match of component 1
if (*w1>0.0f && (*sigma1)>0.0f) {
weight = (1.0f-alpha)*(*w1);
if (match<0 && ((x-*mu1)*(x-*mu1)/((*sigma1)*(*sigma1))) < tsq) {
weight += alpha;
(*Nobs1)++;
rho = (1.0f-alpha)/((float)*Nobs1) + alpha;
update_gauss(x, rho, mu1, sigma1, min_sigma);
match = 1;
}
*w1 = weight;
}
// test for a match of component 2
if (*w2>0.0f && (*sigma2)>0.0f) {
weight = (1.0f-alpha)*(*w2);
if (match<0 && ((x-*mu2)*(x-*mu2)/((*sigma2)*(*sigma2))) < tsq) {
weight += alpha;
(*Nobs2)++;
rho = (1.0f-alpha)/((float)*Nobs2) + alpha;
update_gauss(x, rho, mu2, sigma2, min_sigma);
match = 2;
}
*w2 = weight;
}
// If there were no matches then insert a new component
if (match<0)
insert_gauss_3(x, alpha, init_sigma, &match,
mu0, sigma0, w0, Nobs0,
mu1, sigma1, w1, Nobs1,
mu2, sigma2, w2, Nobs2);
// If there is more than one component, sort the components with
// respect to weight/sigma.
//
//if (match>0)
sort_mix_3(mu0, sigma0, w0, Nobs0,
mu1, sigma1, w1, Nobs1,
mu2, sigma2, w2, Nobs2);
}
}
#if 0
void update_gauss_3_mixture(float x, float w, float t_match,
float init_sigma, float min_sigma,
float* mu0, float* sigma0, float* w0, short* Nobs0,
float* mu1, float* sigma1, float* w1, short* Nobs1,
float* mu2, float* sigma2, float* w2, short* Nobs2,
float* Nobs_mix)
{
if (w>0.0f)
{
int match = -1;
(*Nobs0) ++;
float tsq=t_match*t_match;
float weight = 0.0f;
float rho = 1.0f/min((float)*Nobs0,20.0);
// test for a match of component 0
if (*w0>0.0f && (*sigma0)>0.0f) {
weight = (1.0f-rho)*(*w0);
weight += rho;
update_gauss(x, rho, mu0, sigma0, min_sigma);
match = 0;
*w0 = weight;
}
else
{
insert_gauss_3(x, rho, init_sigma, &match,
mu0, sigma0, w0, Nobs0,
mu1, sigma1, w1, Nobs1,
mu2, sigma2, w2, Nobs2);
}
}
}
#endif // 0
//
// update a 3d gaussian mixture with a sample. The mixture parameters are
// mutated in place. Note that the number of components can vary between
// 0 and 2 as the updating proceeds. Initially, the mixture contains no
// components. The number of observations for each component is Nobs(i).
// The number of observations for the whole mixture is Nobs_mix. When
// a new component is inserted in the mixture, it is given a mean of x
// and a sigma of min_sigma. The threshold t_match determines the maximum
// Mahalanobis distance that the sample can have in order to be considered
// to belong to a given component.
//
void update_gauss_2_mixture_rgb(float4 x, float w, float t_match,
float init_sigma, float min_sigma,
float4* mu0, float4* sigma0, float* w0, short* Nobs0,
float4* mu1, float4* sigma1, float* w1, short* Nobs1,
float* Nobs_mix)
{
if (w>0.0f)
{
int match = -1;
(*Nobs_mix) += w;
float alpha = w/(*Nobs_mix), tsq=t_match*t_match;
float weight = 0.0f, rho = 0.0f;
// test for a match of component 0
if (*w0>0.0f && all((*sigma0)>0.0f)) {
weight = (1.0f-alpha)*(*w0);
if (match<0 && all(((x-*mu0)*(x-*mu0)/((*sigma0)*(*sigma0))) < tsq)) {
weight += alpha;
(*Nobs0)++;
rho = (1.0f-alpha)/((float)*Nobs0) + alpha;
update_gauss_3d(x, rho, mu0, sigma0, min_sigma);
match = 0;
}
*w0 = weight;
}
// test for a match of component 1
if (*w1>0.0f && all((*sigma1)>0.0f)) {
weight = (1.0f-alpha)*(*w1);
if (match<0 && all(((x-*mu1)*(x-*mu1)/((*sigma1)*(*sigma1))) < tsq)) {
weight += alpha;
(*Nobs1)++;
rho = (1.0f-alpha)/((float)*Nobs1) + alpha;
update_gauss_3d(x, rho, mu1, sigma1, min_sigma);
match = 1;
}
*w1 = weight;
}
// If there were no matches then insert a new component
if (match<0)
insert_gauss_2(x, alpha, init_sigma, &match,
mu0, sigma0, w0, Nobs0,
mu1, sigma1, w1, Nobs1);
// If there is more than one component, sort the components with
// respect to weight/sigma.
//
sort_mix_2(mu0, sigma0, w0, Nobs0,
mu1, sigma1, w1, Nobs1);
}
}
//
// insert a new component in the mixture. The mixture is assumed
// to be sorted so that if w2>0 the third component is the one replaced.
//
void insert_gauss_32(float x, float init_weight, float init_sigma, int* match,
float* mu0, float* sigma0, float* w0, short* Nobs0,
float* mu1, float* sigma1, float* w1, short* Nobs1,
float* mu2, float* sigma2, float* w2, short* Nobs2)
{
if ((*w1)>0.0f && (*sigma1)>0.0f) // replace the third component
{
#if 0
float adjust = *w0 + *w1;
adjust = (1.0f - init_weight)/adjust;
*w0 = (*w0)*adjust; *w1 = (*w1)*adjust;
#endif
*mu2 = x;
*sigma2 = init_sigma;
*w2 = init_weight;
*Nobs2 = 1;
*match = 2;
return;
}
else if ((*w0)>0.0f) { // replace the second component
#if 0
*w0 = (1.0f-init_weight);
#endif
*mu1 = x;
*sigma1 = init_sigma;
*w1 = init_weight;
*Nobs1 = 1;
*match = 1;
return;
}
else { // replace the first component
// note that in C++ the weights don't sum to 1?
// see bsta_adaptive_updater.h - ::insert bug?
*w0 = init_weight; //init_weight in C++
*mu0 = x;
*sigma0 = init_sigma;
*Nobs0 = 1;
*match = 0;
}
}
void update_gauss_3_mixture2(float x, float w, float t_match,
float init_sigma, float min_sigma,
float* mu0, float* sigma0, float* w0, short* Nobs0,
float* mu1, float* sigma1, float* w1, short* Nobs1,
float* mu2, float* sigma2, float* w2, short* Nobs2,
float* Nobs_mix)
{
if (w>0.0f)
{
int match = -1;
//(*Nobs_mix) += w;
float alpha = w*0.3f, tsq=t_match*t_match;
float weight = 0.0f, rho = 0.0f;
// test for a match of component 0
if (*w0>0.0f && (*sigma0)>0.0f) {
weight = /*(1.0f-alpha)*/(*w0);
if (match<0 &&
(x-*mu0)*(x-*mu0)/((*sigma0)*(*sigma0)) < tsq) {
weight += alpha;
(*Nobs0)++;
rho = alpha;
update_gauss(x, rho, mu0, sigma0, min_sigma);
match = 0;
}
*w0 = weight;
}
// test for a match of component 1
if (*w1>0.0f && (*sigma1)>0.0f) {
weight = /*(1.0f-alpha)*/(*w1);
if (match<0 && ((x-*mu1)*(x-*mu1)/((*sigma1)*(*sigma1))) < tsq) {
weight += alpha;
(*Nobs1)++;
rho = alpha;
update_gauss(x, rho, mu1, sigma1, min_sigma);
match = 1;
}
*w1 = weight;
}
// test for a match of component 2
if (*w2>0.0f && (*sigma2)>0.0f) {
weight = /*(1.0f-alpha)*/(*w2);
if (match<0 && ((x-*mu2)*(x-*mu2)/((*sigma2)*(*sigma2))) < tsq) {
weight += alpha;
(*Nobs2)++;
rho = alpha;
update_gauss(x, rho, mu2, sigma2, min_sigma);
match = 2;
}
*w2 = weight;
}
// If there were no matches then insert a new component
if (match<0)
insert_gauss_32(x, w*0.1f, init_sigma, &match,
mu0, sigma0, w0, Nobs0,
mu1, sigma1, w1, Nobs1,
mu2, sigma2, w2, Nobs2);
// If there is more than one component, sort the components with
// respect to weight/sigma.
//if (match>0)
sort_mix_3(mu0, sigma0, w0, Nobs0,
mu1, sigma1, w1, Nobs1,
mu2, sigma2, w2, Nobs2);
}
float sum_weights=(*w0)+(*w1)+(*w2);
if (sum_weights>1.0f)
{
*w0 /= sum_weights;
*w1 /= sum_weights;
*w2 /= sum_weights;
}
}