-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhbp_isp.cu
executable file
·1383 lines (1240 loc) · 43.9 KB
/
hbp_isp.cu
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
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Hierarchical Belief Propagation on Image Segmentation Pyramid.
* Author: Tingman Yan ([email protected])
*/
#include "hbp_isp.hpp"
#include <cfloat>
// compute the KL divergence for all boundary connection
// sum over all possiable labels
__global__ void
compute_KL_k(const float *const im_cost_d, const int2 *const bd_d, float *const KL_d, const int num_clus, const int m_labels, const int num_bd)
{
int id = blockIdx.x * blockDim.x + threadIdx.x;
if (id >= num_bd)
return;
int s = bd_d[id].x;
int t = bd_d[id].y;
float kl = 0;
for (int d = 0; d < m_labels; ++d)
{
float p_s = im_cost_d[s + d * num_clus];
float p_t = im_cost_d[t + d * num_clus];
kl += (p_t - p_s) * (expf(-p_s) - expf(-p_t));
// this is the same as first transform to exp(-cost(d)),
// then compute KL divergence
}
KL_d[id] = kl;
}
// for a vector v, set v[i] = i
__global__ void
set_to_identity(int *, const int);
// get the image-wise cluster (superpixel) label
__global__ void
update_image_label(const int *const clus_d, const int *const im_clus_d,
int *const im_clus_h_d, const int im_size)
{
int id = blockIdx.x * blockDim.x + threadIdx.x;
if (id >= im_size)
return;
int cc = im_clus_d[id];
im_clus_h_d[id] = clus_d[cc];
}
// for a vertor v, set v[i] = T
template <typename T>
__global__ void
set_to_zero(T *const, const int, const T);
// copy the best plane label in the previous level to the prior plane in
// the current level
__global__ void
copy_prior_plane(const int *const clus_d, const float4 *const plane_h_d,
float4 *const plane_d, const int num_clus,
const int num_clus_h, const int P)
{
int id = blockDim.x * blockIdx.x + threadIdx.x;
if (id >= num_clus)
return;
int cc = clus_d[id];
for (int p = 0; p < P; ++p)
{
float4 prior_best_plane = plane_h_d[cc + p * num_clus_h];
plane_d[id + p * num_clus] = prior_best_plane;
plane_d[id + (p + P) * num_clus] = prior_best_plane;
plane_d[id + (p + P * 2) * num_clus] = prior_best_plane;
}
}
__global__ void
sum_to_mean(float *, const int, const int);
__global__ void
compute_dist_color_pos_k(const float *const mean_d, const int2 *const bd_d,
float *const dc_d, const int num_bd,
const int num_clus, const int channels)
{
int x = blockDim.x * blockIdx.x + threadIdx.x;
int y = blockDim.y * blockIdx.y + threadIdx.y;
if (x >= num_bd || y >= channels)
return;
int2 bd = bd_d[x];
int s = bd.x, t = bd.y;
int index_s = y * num_clus + s, index_t = y * num_clus + t;
float mean_s = mean_d[index_s], mean_t = mean_d[index_t];
if (y < 3)
{ // for BGR
float dist = abs(mean_s - mean_t);
atomicAdd(&dc_d[x], dist);
}
else
{ // for pos
float dist = (mean_s - mean_t) * (mean_s - mean_t);
atomicAdd(&dc_d[num_bd + x], dist);
}
}
__global__ void
dist_trans(float *const dc_d, const float gamma, const int num_bd,
const int channels)
{
int x = blockDim.x * blockIdx.x + threadIdx.x;
int y = blockDim.y * blockIdx.y + threadIdx.y;
if (x >= num_bd || y >= channels)
return;
int id = y * num_bd + x;
float dist = dc_d[id];
if (y == 0)
{
dist = exp(-dist / gamma);
}
else
{
dist = sqrtf(dist);
}
dc_d[id] = dist;
}
__global__ void
sum_reduce_num_clus(const int *const clus_sort_d, int *const clus_num_pos_d,
const int num_clus)
{
int id = blockDim.x * blockIdx.x + threadIdx.x;
if (id >= num_clus)
return;
int c = clus_sort_d[id];
atomicAdd(&clus_num_pos_d[c], 1);
}
__global__ void
sum_reduce_num_nbs(const int2 *const bd_d, int *const nb_num_pos_d,
const int num_bd)
{
int id = blockDim.x * blockIdx.x + threadIdx.x;
if (id >= num_bd)
return;
int s = bd_d[id].x;
atomicAdd(&nb_num_pos_d[s], 1);
}
// for a bd[id] = s -> t, find the corresponding index reverse_id_d[id],
// such that bd[reverse_id_d[id]] = t -> s
__global__ void
reverse_bd_id_k(const int2 *const bd_d, const int *const pos_scan_d,
int *const reverse_id_d, const int num_bd)
{
int id = blockDim.x * blockIdx.x + threadIdx.x;
if (id >= num_bd)
return;
int2 bd = bd_d[id];
int s = bd.x, t = bd.y; // s -> t
// find the id of the corresponding t -> s
// - since the neighbors of a cluster is small, the speed is fast
for (int r_id = pos_scan_d[t]; r_id < pos_scan_d[t + 1]; ++r_id)
{
if (bd_d[r_id].y == s)
{
reverse_id_d[id] = r_id;
break;
}
}
}
__global__ void
read_plane_cost(const float4 *const plane_d, const float4 *const im_D4_d,
const float *const im_cost_d, float *const im_plane_cost_d,
const bool use_cost_vol, const float tau, const int im_size,
const int width, const int m_labels, const float inlier_thrsh,
const float lambda, const int num_clus, const int s_c,
const int P)
{
int id = blockIdx.x * blockDim.x + threadIdx.x;
if (id >= im_size)
return;
if (!use_cost_vol)
{
int cc = im_D4_d[id].x;
float D = im_D4_d[id].y;
int im_id = im_D4_d[id].z;
int x = im_id % width;
int y = im_id / width;
for (int p = 0; p < P; ++p)
{
float4 plane = plane_d[cc + (p + s_c) * num_clus];
float D_f = plane.x * x + plane.y * y + plane.z;
float cost = abs(D - D_f) <= inlier_thrsh ? 0 : 1;
im_plane_cost_d[id + p * im_size] = lambda * cost;
}
}
else
{
int cc = im_D4_d[id].x;
int im_id = im_D4_d[id].z;
int x = im_id % width;
int y = im_id / width;
for (int p = 0; p < P; ++p)
{
float cost = 0;
float4 plane = plane_d[cc + (p + s_c) * num_clus];
float d_f = plane.x * x + plane.y * y + plane.z;
int d = __float2int_rd(d_f);
int d_ = d + 1;
if (d < 0 || d_ >= m_labels)
cost = tau;
else
{
cost = (d_ - d_f) * im_cost_d[im_id + d * im_size] + (d_f - d) * im_cost_d[im_id + d_ * im_size];
}
im_plane_cost_d[id + p * im_size] = lambda * cost;
}
}
}
__global__ void
plane_inlier_ratio(const float4 *const plane_d, const float4 *const im_D4_d,
float *const im_plane_cost_d, const int im_size,
const int width, const float inlier_thrsh,
const int num_clus, const int K)
{
int id = blockIdx.x * blockDim.x + threadIdx.x;
if (id >= im_size)
return;
int cc = im_D4_d[id].x;
float D = im_D4_d[id].y;
int im_id = im_D4_d[id].z;
int x = im_id % width;
int y = im_id / width;
for (int k = 0; k < K; ++k)
{
float4 plane = plane_d[cc + k * num_clus];
float D_f = plane.x * x + plane.y * y + plane.z;
float is_inlier = abs(D - D_f) <= inlier_thrsh ? 1 : 0;
im_plane_cost_d[id + k * im_size] = is_inlier;
}
}
__global__ void
set_occlusion_cost_k(const float *const inlier_ratio_d,
const float *const mean_d, float *const cost_d,
const float inlier_ratio, const float tau,
const int num_clus, const int mean_channels, const int K)
{
int id = blockDim.x * blockIdx.x + threadIdx.x;
if (id >= num_clus)
return;
float w = mean_d[id + (mean_channels - 1) * num_clus];
for (int k = 8; k < K; ++k)
{
if (inlier_ratio_d[id + k * num_clus] / w < inlier_ratio)
cost_d[id + k * num_clus] = tau * w;
}
}
__global__ void
normalize_msg_k(float *const msg_d, const int size, const int K)
{
int id = blockDim.x * blockIdx.x + threadIdx.x;
if (id >= size)
return;
float mean = 0;
for (int k = 0; k < K; ++k)
{
mean += msg_d[id + k * size];
}
mean /= K;
for (int k = 0; k < K; ++k)
{
msg_d[id + k * size] -= mean;
}
}
__global__ void
passing_msg_k(const int2 *const bd_d, const float *const belief_d,
float *const msg_d, const int num_bd, const int num_clus,
const int P)
{
int x = blockDim.x * blockIdx.x + threadIdx.x;
int y = blockDim.y * blockIdx.y + threadIdx.y;
if (x >= num_bd || y >= P * 2)
return;
int t = bd_d[x].y;
msg_d[x + y * num_bd] = belief_d[t + y * num_clus] - msg_d[x + y * num_bd];
}
__global__ void
smooth_msg_k(const int2 *const bd_d, const float *const dc_d,
const int *const bdl_d, const int4 *const point_id_d,
const float4 *const plane_d, const float *const msg_d,
float *const msg_t_d, const float m_tau_smooth, const int num_bd, const int num_clus,
const int width, const int P)
{
int x = blockDim.x * blockIdx.x + threadIdx.x;
int y = blockDim.y * blockIdx.y + threadIdx.y;
if (x >= num_bd || y >= P * 3)
return;
int2 bd = bd_d[x];
int s = bd.x, t = bd.y;
float4 plane_s = plane_d[s + y * num_clus];
int4 p_s = point_id_d[s];
int4 p_t = point_id_d[t];
float dist_color = dc_d[x];
float dist_pos = dc_d[x + num_bd];
int bdl = bdl_d[x];
dist_pos = max(dist_pos, 1.0f);
dist_color = max(dist_color, 0.01f);
float c0 = dist_color * bdl;
float minimum = FLT_MAX;
msg_t_d[x + y * num_bd] = 0;
for (int p = 0; p < P * 2; ++p)
{
if (p == P)
{
msg_t_d[x + y * num_bd] += minimum;
minimum = FLT_MAX;
}
float4 plane_t = plane_d[t + p * num_clus];
float delta_px = plane_s.x - plane_t.x;
float delta_py = plane_s.y - plane_t.y;
float delta_pz = plane_s.z - plane_t.z;
float phi = abs(delta_px * (p_s.x % width) + delta_py * (p_s.x / width) + delta_pz) +
abs(delta_px * (p_s.y % width) + delta_py * (p_s.y / width) + delta_pz) +
abs(delta_px * (p_s.z % width) + delta_py * (p_s.z / width) + delta_pz) +
abs(delta_px * (p_t.x % width) + delta_py * (p_t.x / width) + delta_pz) +
abs(delta_px * (p_t.y % width) + delta_py * (p_t.y / width) + delta_pz) +
abs(delta_px * (p_t.z % width) + delta_py * (p_t.z / width) + delta_pz);
phi = min(phi / 3 / dist_pos, m_tau_smooth);
float msg = msg_d[x + p * num_bd] + c0 * phi;
if (msg < minimum)
minimum = msg;
}
msg_t_d[x + y * num_bd] += minimum;
}
__global__ void
smooth_msg(const int2 *const bd_d, const float *const dc_d,
const int *const bdl_d, const float *const mean_d,
const float4 *const plane_d, const float *const msg_d,
float *const msg_t_d, const float m_tau_smooth, const int num_bd, const int num_clus,
const int P)
{
int x = blockDim.x * blockIdx.x + threadIdx.x;
int y = blockDim.y * blockIdx.y + threadIdx.y;
if (x >= num_bd || y >= P * 3)
return;
int2 bd = bd_d[x];
int s = bd.x, t = bd.y;
float4 plane_s = plane_d[s + y * num_clus];
float x0 = mean_d[s + 3 * num_clus];
float y0 = mean_d[s + 4 * num_clus];
float x1 = mean_d[t + 3 * num_clus];
float y1 = mean_d[t + 4 * num_clus];
float dist_color = dc_d[x];
float dist_pos = dc_d[x + num_bd];
int bdl = bdl_d[x];
dist_pos = max(dist_pos, 1.0f);
dist_color = dist_color > 0.01f ? dist_color : 0.01f;
float c0 = dist_color * bdl;
float minimum = FLT_MAX;
msg_t_d[x + y * num_bd] = 0;
for (int p = 0; p < P * 2; ++p)
{
if (p == P)
{
msg_t_d[x + y * num_bd] += minimum;
minimum = FLT_MAX;
}
float4 plane_t = plane_d[t + p * num_clus];
float delta_px = plane_s.x - plane_t.x;
float delta_py = plane_s.y - plane_t.y;
float delta_pz = plane_s.z - plane_t.z;
float phi = abs(delta_px * x0 + delta_py * y0 + delta_pz) +
abs(delta_px * x1 + delta_py * y1 + delta_pz);
phi = min(phi / dist_pos, m_tau_smooth);
float msg = msg_d[x + p * num_bd] + c0 * phi;
if (msg < minimum)
minimum = msg;
}
msg_t_d[x + y * num_bd] += minimum;
}
__global__ void
aggregate_belief_k(const int2 *const bd_d, const float *const msg_t_d,
float *const belief_d, const int num_bd, const int num_clus,
const int K)
{
int x = blockDim.x * blockIdx.x + threadIdx.x;
int y = blockDim.y * blockIdx.y + threadIdx.y;
if (x >= num_bd || y >= K)
return;
int s = bd_d[x].x;
atomicAdd(&belief_d[s + y * num_clus], msg_t_d[x + y * num_bd]);
}
__global__ void
ping_pang_msg_k(float *const msg_d, const float *const msg_t_d,
const int *const reverse_id_d, const int num_bd, const int K)
{
int x = blockDim.x * blockIdx.x + threadIdx.x;
int y = blockDim.y * blockIdx.y + threadIdx.y;
if (x >= num_bd || y >= K)
return;
msg_d[reverse_id_d[x] + y * num_bd] = msg_t_d[x + y * num_bd];
}
// TODO: this is time consuming
__global__ void
get_belief_order(const float *const belief_d, int *const order_d,
const int num_clus, const int K)
{
int id = blockDim.x * blockIdx.x + threadIdx.x;
if (id >= num_clus)
return;
for (int i = 0; i < K - 1; ++i)
{
for (int j = i + 1; j < K; ++j)
{
if (belief_d[id + j * num_clus] < belief_d[id + i * num_clus])
order_d[id + i * num_clus]++;
else
order_d[id + j * num_clus]++;
}
}
}
__global__ void
select_belief(const int *const order_d, float4 *const plane_d,
float *const cost_d, float *const belief_d, const int num_clus,
const int P)
{
int id = blockDim.x * blockIdx.x + threadIdx.x;
if (id >= num_clus)
return;
int num_best = 0;
for (int k = 0; k < P * 3; ++k)
{
if (order_d[id + k * num_clus] < P)
{
plane_d[id + num_best * num_clus] = plane_d[id + k * num_clus];
cost_d[id + num_best * num_clus] = cost_d[id + k * num_clus];
belief_d[id + num_best * num_clus] = belief_d[id + k * num_clus];
num_best++;
}
}
}
__global__ void
select_msg(const int2 *const bd_d, const int *const order_d,
float *const msg_d, const int num_bd, const int num_clus,
const int P)
{
int id = blockDim.x * blockIdx.x + threadIdx.x;
if (id >= num_bd)
return;
int t = bd_d[id].y;
int num_best = 0;
for (int k = 0; k < P * 3; ++k)
{
if (order_d[t + k * num_clus] < P)
{
msg_d[id + num_best * num_bd] = msg_d[id + k * num_bd];
num_best++;
}
}
}
__forceinline__ __device__ void
normalize_plane(float4 &plane)
{
float norm = sqrtf(
plane.x * plane.x + plane.y * plane.y + plane.z * plane.z);
plane.x /= norm;
plane.y /= norm;
plane.z /= norm;
}
__forceinline__ __device__ void
make_three_diff_id(int &p0, int &p1, int &p2, const int &tn)
{
if (tn <= 3)
{
p1 = (p0 + 1) % tn;
p2 = (p1 + 1) % tn;
return;
}
if (p1 == p0)
p1 = tn - 1;
if (p2 == p0)
{
if (p1 == tn - 2)
p2 = tn - 1;
else
p2 = tn - 2;
}
else if (p2 == p1)
{
if (p0 == tn - 2)
p2 = tn - 1;
else
p2 = tn - 2;
}
}
// A, B, C are in x, y ,z, w format
__forceinline__ __device__ float4
fit_plane_from_points(const float4 &A, const float4 &B, const float4 &C)
{
float4 a = B - A;
float4 b = C - A;
float nx = a.y * b.z - a.z * b.y;
float ny = a.z * b.x - a.x * b.z;
float nz = a.x * b.y - a.y * b.x;
nx /= nz;
ny /= nz;
nz /= nz;
if (abs(nz - 1.f) > FLT_EPSILON)
{
return make_float4(0.f, 0.f, (A.z + B.z + C.z) / 3.f, 0.f);
}
else
return make_float4(-nx, -ny, nx * A.x + ny * A.y + A.z, 0.f);
}
// linear congruential generator
__device__ int
rand_lcg(unsigned int s)
{
// BSD_RND
int a = 1103515245;
int c = 12345;
unsigned int m = 2147483648;
return (a * s + c) % m;
}
__device__ float
rand_lcg_uniform(unsigned int &s, const float &f)
{
// BSD_RND
int a = 1103515245;
int c = 12345;
unsigned int m = 2147483648;
s = (a * s + c) % m;
return s / (float)m * f;
}
__global__ void
init_rand_seed_k(int *const s_child_d, int *const s_neighbor_d,
int *const s_ransac_d, const int *const clus_num_pos_d,
const int *const nb_num_pos_d,
const int *const im_clus_num_pos_d, const int num_clus,
const int num_clus_h)
{
int id = blockDim.x * blockIdx.x + threadIdx.x;
if (id >= num_clus)
return;
// s_neighbor = nb_pos
s_neighbor_d[id] = nb_num_pos_d[id + num_clus + 1];
// s_ransac = clus_pos
s_ransac_d[id] = im_clus_num_pos_d[id + num_clus];
if (id >= num_clus_h)
return;
// s_child = clus_pos
s_child_d[id] = clus_num_pos_d[id + num_clus_h];
}
__global__ void
ransac_candidate_plane(int *const s_d, const float4 *const im_D4_d,
const int *const im_clus_num_pos_d,
float4 *const plane_d, int4 *const point_id_d,
const int num_clus, const int width, const int P)
{
int id = blockDim.x * blockIdx.x + threadIdx.x;
if (id >= num_clus)
return;
int clus_num = im_clus_num_pos_d[id];
int clus_pos = im_clus_num_pos_d[id + num_clus];
unsigned int s = s_d[id];
for (int p = P * 2; p < P * 3; ++p)
{
s = rand_lcg(s);
int p0 = s % clus_num;
s = rand_lcg(s);
int p1 = s % (clus_num - 1);
s = rand_lcg(s);
int p2 = s % (clus_num - 2);
make_three_diff_id(p0, p1, p2, clus_num);
p0 += clus_pos;
p1 += clus_pos;
p2 += clus_pos;
float4 P0 = im_D4_d[p0];
float4 P1 = im_D4_d[p1];
float4 P2 = im_D4_d[p2];
int p0_id = P0.z;
int p1_id = P1.z;
int p2_id = P2.z;
float4 A = make_float4(p0_id % width, p0_id / width, P0.y, 0.f);
float4 B = make_float4(p1_id % width, p1_id / width, P1.y, 0.f);
float4 C = make_float4(p2_id % width, p2_id / width, P2.y, 0.f);
plane_d[id + p * num_clus] = fit_plane_from_points(A, B, C);
if (p == P * 3 - 1)
{
point_id_d[id].x = p0_id;
point_id_d[id].y = p1_id;
point_id_d[id].z = p2_id;
}
}
s_d[id] = s;
}
__global__ void
random_candidate_plane(int *const s_d, const float *const mean_d,
float4 *const plane_d, const float max_dz,
const float max_dn, const int num_clus, const int P)
{
int id = blockDim.x * blockIdx.x + threadIdx.x;
if (id >= num_clus)
return;
unsigned int s = s_d[id];
float x = mean_d[id + 3 * num_clus];
float y = mean_d[id + 4 * num_clus];
for (int p = P * 2; p < P * 3; ++p)
{
float4 plane = plane_d[id + p * num_clus];
float z = plane.x * x + plane.y * y + plane.z;
// TODO: try normal distribution
float delta_z = rand_lcg_uniform(s, max_dz);
z += delta_z;
float delta_nx = rand_lcg_uniform(s, max_dn);
float delta_ny = rand_lcg_uniform(s, max_dn);
float delta_nz = rand_lcg_uniform(s, max_dn);
normalize_plane(plane);
plane.x += delta_nx;
plane.y += delta_ny;
plane.z += delta_nz;
// normalize_plane(plane);
float a = -plane.x / plane.z;
float b = -plane.y / plane.z;
float c = z - a * x - b * y;
plane_d[id + p * num_clus] = make_float4(a, b, c, 0.f);
}
s_d[id] = s;
}
__global__ void
neighbor_candidate_plane(int *const s_d, const int2 *const bd_d,
const int *const nb_num_pos_d,
const float4 *const plane_d,
float4 *const plane_buf_d, const int num_clus,
const int P)
{
int id = blockDim.x * blockIdx.x + threadIdx.x;
if (id >= num_clus)
return;
int nb_num = nb_num_pos_d[id];
int nb_pos = nb_num_pos_d[id + num_clus + 1];
unsigned int s = s_d[id];
for (int p = 0; p < P; ++p)
{
/*
// This is more time-consuming
float r_pos = (1 - curand_uniform (&state)) * nb_num;
int t_id = bd_d[__float2int_rd (r_pos) + nb_pos].y;
*/
s = rand_lcg(s);
int r_pos = s % nb_num;
int t_id = bd_d[r_pos + nb_pos].y;
plane_buf_d[id + p * num_clus] = plane_d[t_id + (p + P * 2) * num_clus];
}
s_d[id] = s;
}
__global__ void
select_child_plane(int *const s_d, const int *const clus_sort_d,
const int *const clus_num_pos_d,
const float4 *const plane_d, float4 *const plane_h_d,
const int num_clus, const int num_clus_h, const int P)
{
int id = blockDim.x * blockIdx.x + threadIdx.x;
if (id >= num_clus_h)
return;
int clus_num = clus_num_pos_d[id];
int clus_pos = clus_num_pos_d[id + num_clus_h];
unsigned int s = s_d[id];
for (int p = 0; p < P; ++p)
{
s = rand_lcg(s);
int r_pos = s % clus_num;
int c = clus_sort_d[r_pos + clus_pos];
plane_h_d[id + (p + P * 2) * num_clus_h] = plane_d[c];
}
s_d[id] = s;
}
__global__ void
set_candidate_plane_k(const int *const clus_d, const float4 *const plane_h_d,
float4 *const plane_d, const int num_clus,
const int num_clus_h, const int P)
{
int id = blockDim.x * blockIdx.x + threadIdx.x;
if (id >= num_clus)
return;
int c = clus_d[id];
for (int p = P * 2; p < P * 3; ++p)
{
plane_d[id + p * num_clus] = plane_h_d[c + p * num_clus_h];
}
}
__global__ void
compute_plane_confidence_k(float *const plane_cfd_d, const float *const mean_d, const float *const cost_d, const int num_clus, const int mean_channels, const int P)
{
int id = blockDim.x * blockIdx.x + threadIdx.x;
if (id >= num_clus)
return;
float w = mean_d[id + (mean_channels - 1) * num_clus];
for (int p = 0; p < P * 3; ++p)
{
float avg_cost = cost_d[id + p * num_clus] / w;
plane_cfd_d[id + p * num_clus] = 1 - avg_cost;
}
}
__global__ void
reset_candidate_plane_k(float4 *const plane_d, const float *const plane_cfd_d, const int num_clus, const int P)
{
int id = blockDim.x * blockIdx.x + threadIdx.x;
if (id >= num_clus)
return;
float4 sum_plane = make_float4(0, 0, 0, 0);
float sum_plane_cfd = 0;
for (int p = 0; p < P * 3; ++p)
{
float cfd = plane_cfd_d[id + p * num_clus];
if (cfd > 0.6)
{
sum_plane += plane_d[id + p * num_clus] * cfd;
sum_plane_cfd += cfd;
}
}
for (int p = P * 2; p < P * 3; ++p)
{
if (plane_cfd_d[id + p * num_clus] < 0.4 && sum_plane_cfd > 0.6)
plane_d[id + p * num_clus] = sum_plane / sum_plane_cfd;
}
}
__global__ void
map_msg_k(const float *const belief_d, float *const min_belief_d, const int N,
const int C)
{
int x = blockDim.x * blockIdx.x + threadIdx.x;
int y = blockDim.y * blockIdx.y + threadIdx.y;
if (x >= N || y >= C)
return;
int id = y * N + x;
atomicMinFloat(&min_belief_d[x], belief_d[id]);
}
__global__ void
min_belief_to_plane(const float *const belief_d,
const float *const min_belief_d,
const float4 *const plane_d, float4 *const min_plane_d,
const int num_clus, const int P)
{
int id = blockIdx.x * blockDim.x + threadIdx.x;
if (id >= num_clus)
return;
int min_p = 0;
for (int p = 0; p < P; ++p)
{
if (belief_d[id + p * num_clus] == min_belief_d[id])
{
min_p = p;
break;
}
}
min_plane_d[id] = plane_d[id + min_p * num_clus];
}
__global__ void
plane_to_label(const float4 *const im_D4_d,
const float4 *const min_plane_d, float *const disp_img_d,
float *const label_img_d, const int im_size, const int width,
const int m_labels)
{
int id = blockIdx.x * blockDim.x + threadIdx.x;
if (id >= im_size)
return;
int c = im_D4_d[id].x;
int im_id = im_D4_d[id].z;
int x = im_id % width;
int y = im_id / width;
float4 plane = min_plane_d[c];
float d = plane.x * x + plane.y * y + plane.z;
d = fminf(fmaxf(d, 0), m_labels - 1);
disp_img_d[im_id] = d;
// label
label_img_d[im_id * 3] = plane.x;
label_img_d[im_id * 3 + 1] = plane.y;
label_img_d[im_id * 3 + 2] = plane.z;
}
__global__ void
set_D4_k(const int *const im_clus_d, const float *const im_D_d,
float4 *const im_D4_d, const int im_size)
{
int id = blockIdx.x * blockDim.x + threadIdx.x;
if (id >= im_size)
return;
im_D4_d[id].x = im_clus_d[id];
im_D4_d[id].y = im_D_d[id];
im_D4_d[id].z = id;
// im_D4_d[id].w is the depth cluster id.
}
//-------------------------------------------------------
float HBP_ISP::set_lambda_by_KL_divergence(const float *const im_cost_d, float *const KL_d)
{
switch_level(0); // pixel level
assert(num_clus = im_size);
cudaMemset(KL_d, 0, sizeof(float) * num_bd);
int grid = (num_bd + m_block - 1) / m_block;
compute_KL_k<<<grid, m_block>>>(im_cost_d, bd_d, KL_d, num_clus, m_labels, num_bd);
float lambda = m_alpha / (thrust::reduce(thrust::device, KL_d, KL_d + num_bd) / num_bd);
std::cout << "lambda: " << lambda << std::endl;
return lambda;
}
void HBP_ISP::setup_D4(int **isp_im_clus_d, float4 **isp_im_D4_d)
{
int grid = (im_size + m_block - 1) / m_block;
// init im_clus
set_to_identity<<<grid, m_block>>>(isp_im_clus_d[0], im_size);
for (int i = 0; i < isp_levels - 1; ++i)
{
update_image_label<<<grid, m_block>>>(
isp_clus_d[i],
isp_im_clus_d[i], isp_im_clus_d[i + 1], im_size);
}
for (int i = 0; i < isp_levels; ++i)
{
set_D4_k<<<grid, m_block>>>(isp_im_clus_d[i], im_D_d, isp_im_D4_d[i],
im_size);
// sort_by_key
thrust::sort_by_key(thrust::device, isp_im_clus_d[i],
isp_im_clus_d[i] + im_size, isp_im_D4_d[i]);
}
}
void HBP_ISP::switch_level(const int level)
{
// set all pointers to current level, so that we can reference them easily
clus_d = isp_clus_d[level];
im_clus_d = isp_im_clus_d[level];
mean_d = isp_mean_d[level];
cost_d = isp_cost_d[level];
belief_d = isp_belief_d[level];
plane_d = isp_plane_d[level];
bd_d = isp_bd_d[level];
dc_d = isp_dc_d[level];
bdl_d = isp_bdl_d[level];
clus_sort_d = isp_clus_sort_d[level];
clus_num_pos_d = isp_clus_num_pos_d[level];
im_clus_num_pos_d = isp_im_clus_num_pos_d[level];
nb_num_pos_d = isp_nb_num_pos_d[level];
reverse_id_d = isp_reverse_id_d[level];
im_D4_d = isp_im_D4_d[level];
point_id_d = isp_point_id_d[level];
num_clus = num_clus_isp[level];
num_bd = num_bd_isp[level];
if (level < isp_levels - 1)
{
im_clus_h_d = isp_im_clus_d[level + 1];
mean_h_d = isp_mean_d[level + 1];
cost_h_d = isp_cost_d[level + 1];
belief_h_d = isp_belief_d[level + 1];
plane_h_d = isp_plane_d[level + 1];
num_clus_h = num_clus_isp[level + 1];
}
else
{
num_clus_h = 1;
}
m_level = level;
}
void HBP_ISP::init_bp(float4 *const plane_d, float *const cost_d,
float *const belief_d)
{
cudaMemset(msg_d, 0, sizeof(float) * num_bd * K);
int grid = (num_clus + m_block - 1) / m_block;
copy_prior_plane<<<grid, m_block>>>(clus_d, plane_h_d, plane_d, num_clus,
num_clus_h, P);
compute_plane_cost(0);
compute_plane_cost(P);
// belief_d = cost_d, since msg_d = zero
cudaMemcpy(belief_d, cost_d, sizeof(float) * num_clus * K,
cudaMemcpyDeviceToDevice);
init_rand_seed_k<<<grid, m_block>>>(s_child_d, s_neighbor_d, s_ransac_d,
clus_num_pos_d, nb_num_pos_d, im_clus_num_pos_d, num_clus,
num_clus_h);
}
void HBP_ISP::compute_dist_color_pos(float *const mean_d, float *const dc_d)
{
// set to zero
cudaMemset(dc_d, 0, sizeof(float) * num_bd * dc_channels);
// normalize
dim3 grids((num_clus + m_blocks.x - 1) / m_blocks.x,
(mean_channels + m_blocks.y - 1) / m_blocks.y);
sum_to_mean<<<grids, m_blocks>>>(mean_d, num_clus, mean_channels);
grids = dim3((num_bd + m_blocks.x - 1) / m_blocks.x,
(mean_channels - 1 + m_blocks.y - 1) / m_blocks.y);
compute_dist_color_pos_k<<<grids, m_blocks>>>(
mean_d,
bd_d, dc_d, num_bd, num_clus, mean_channels - 1);
grids.y = (dc_channels + m_blocks.y - 1) / m_blocks.y;
dist_trans<<<grids, m_blocks>>>(dc_d, m_gamma, num_bd, dc_channels);
}
void HBP_ISP::reduce_num_pos(const int *const key_d, int *num_pos_d,
const int src_size, const int dst_size)
{
int grid = (src_size + m_block - 1) / m_block;
cudaMemset(num_pos_d, 0, sizeof(int) * dst_size);
// since we use atomicAdd, clus_sort_d and clus_d both works
sum_reduce_num_clus<<<grid, m_block>>>(
key_d,
num_pos_d, src_size);
thrust::exclusive_scan(thrust::device, num_pos_d, num_pos_d + dst_size,
num_pos_d + dst_size);
}
void HBP_ISP::sort_clus(int *const clus_sort_d, int *const clus_num_pos_d,
int *const im_clus_num_pos_d)
{
// clus_sort_d shall be key + clus form
int grid = (num_clus + m_block - 1) / m_block;
set_to_identity<<<grid, m_block>>>(clus_sort_d, num_clus);
cudaMemcpy(clus_sort_d + num_clus, clus_d, sizeof(int) * num_clus,
cudaMemcpyDeviceToDevice);
thrust::sort_by_key(thrust::device, clus_sort_d + num_clus,
clus_sort_d + num_clus * 2, clus_sort_d);
// since we use atomicAdd, clus_sort_d and clus_d both works
reduce_num_pos(clus_sort_d + num_clus, clus_num_pos_d, num_clus,
num_clus_h);
reduce_num_pos(im_clus_d, im_clus_num_pos_d, im_size, num_clus);