forked from jzbontar/mc-cnn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
adcensus.cu
2105 lines (1879 loc) · 59 KB
/
adcensus.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
extern "C" {
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
}
#include "luaT.h"
#include "THC.h"
#include <stdio.h>
#include <assert.h>
#include <math_constants.h>
#include <stdint.h>
#include <unistd.h>
#include <png++/image.hpp>
#define TB 128
#define DISP_MAX 256
THCState* getCutorchState(lua_State* L)
{
lua_getglobal(L, "cutorch");
lua_getfield(L, -1, "getState");
lua_call(L, 0, 1);
THCState *state = (THCState*) lua_touserdata(L, -1);
lua_pop(L, 2);
return state;
}
void checkCudaError(lua_State *L) {
cudaError_t status = cudaPeekAtLastError();
if (status != cudaSuccess) {
luaL_error(L, cudaGetErrorString(status));
}
}
#define COLOR_DIFF(x, i, j) (abs(x[i] - x[j]))
THCudaTensor *new_tensor_like(THCState *state, THCudaTensor *x)
{
THCudaTensor *y = THCudaTensor_new(state);
THCudaTensor_resizeAs(state, y, x);
return y;
}
__device__ void sort(float *x, int n)
{
for (int i = 0; i < n - 1; i++) {
int min = i;
for (int j = i + 1; j < n; j++) {
if (x[j] < x[min]) {
min = j;
}
}
float tmp = x[min];
x[min] = x[i];
x[i] = tmp;
}
}
__global__ void ad(float *x0, float *x1, float *output, int size, int size2, int size3, int direction)
{
int id = blockIdx.x * blockDim.x + threadIdx.x;
if (id < size) {
int d = id;
int x = d % size3;
d /= size3;
int y = d % size2;
d /= size2;
d *= direction;
float dist;
if (0 <= x + d && x + d < size3) {
int cnt = 0;
dist = 0;
for (int yy = y - 4; yy <= y + 4; yy++) {
for (int xx = x - 4; xx <= x + 4; xx++) {
if (0 <= xx && xx < size3 && 0 <= xx + d && xx + d < size3 && 0 <= yy && yy < size2) {
int ind = yy * size3 + xx;
dist += abs(x0[ind] - x1[ind + d]);
cnt++;
}
}
}
dist /= cnt;
} else {
dist = CUDART_NAN;
}
output[id] = dist;
}
}
int ad(lua_State *L)
{
THCState *state = getCutorchState(L);
THCudaTensor *x0 = (THCudaTensor*)luaT_checkudata(L, 1, "torch.CudaTensor");
THCudaTensor *x1 = (THCudaTensor*)luaT_checkudata(L, 2, "torch.CudaTensor");
THCudaTensor *out = (THCudaTensor*)luaT_checkudata(L, 3, "torch.CudaTensor");
int direction = luaL_checkinteger(L, 4);
assert(direction == -1 || direction == 1);
ad<<<(THCudaTensor_nElement(state, out) - 1) / TB + 1, TB>>>(
THCudaTensor_data(state, x0),
THCudaTensor_data(state, x1),
THCudaTensor_data(state, out),
THCudaTensor_nElement(state, out),
THCudaTensor_size(state, out, 2),
THCudaTensor_size(state, out, 3),
direction);
checkCudaError(L);
return 0;
}
__global__ void census(float *x0, float *x1, float *output, int size, int num_channels, int size2, int size3, int direction)
{
int id = blockIdx.x * blockDim.x + threadIdx.x;
if (id < size) {
int d = id;
int x = d % size3;
d /= size3;
int y = d % size2;
d /= size2;
d *= direction;
float dist;
if (0 <= x + d && x + d < size3) {
dist = 0;
for (int i = 0; i < num_channels; i++) {
int ind_p = (i * size2 + y) * size3 + x;
for (int yy = y - 4; yy <= y + 4; yy++) {
for (int xx = x - 4; xx <= x + 4; xx++) {
if (0 <= xx && xx < size3 && 0 <= xx + d && xx + d < size3 && 0 <= yy && yy < size2) {
int ind_q = (i * size2 + yy) * size3 + xx;
if ((x0[ind_q] < x0[ind_p]) != (x1[ind_q + d] < x1[ind_p + d])) {
dist++;
}
} else {
dist++;
}
}
}
}
dist /= num_channels;
} else {
dist = CUDART_NAN;
}
output[id] = dist;
}
}
int census(lua_State *L)
{
THCState *state = getCutorchState(L);
THCudaTensor *x0 = (THCudaTensor*)luaT_checkudata(L, 1, "torch.CudaTensor");
THCudaTensor *x1 = (THCudaTensor*)luaT_checkudata(L, 2, "torch.CudaTensor");
THCudaTensor *out = (THCudaTensor*)luaT_checkudata(L, 3, "torch.CudaTensor");
int direction = luaL_checkinteger(L, 4);
assert(direction == -1 || direction == 1);
census<<<(THCudaTensor_nElement(state, out) - 1) / TB + 1, TB>>>(
THCudaTensor_data(state, x0),
THCudaTensor_data(state, x1),
THCudaTensor_data(state, out),
THCudaTensor_nElement(state, out),
THCudaTensor_size(state, x0, 1),
THCudaTensor_size(state, out, 2),
THCudaTensor_size(state, out, 3),
direction);
checkCudaError(L);
return 0;
}
#if 0
__global__ void add_vol(float *vol, float *cnt, float *out, int size, int size1, int size2, int size3, float ratio)
{
int id = blockIdx.x * blockDim.x + threadIdx.x;
if (id < size) {
int d = id;
int x = d % size3;
d /= size3;
int y = d % size2;
d /= size2;
int lo = floor(d * ratio);
int hi = lo + 1;
float alpha = (d * ratio) - lo;
assert(0 <= lo && hi < size1);
float val = vol[(lo * size2 + y) * size3 + x] * (1 - alpha) + vol[(hi * size2 + y) * size3 + x] * alpha;
if (!isnan(val) && cnt[id] > 0) {
out[id] += val;
cnt[id] += 1;
}
}
}
int add_vol(lua_State *L)
{
THCudaTensor *vol = (THCudaTensor*)luaT_checkudata(L, 1, "torch.CudaTensor");
THCudaTensor *cnt = (THCudaTensor*)luaT_checkudata(L, 2, "torch.CudaTensor");
THCudaTensor *out = (THCudaTensor*)luaT_checkudata(L, 3, "torch.CudaTensor");
float ratio = luaL_checknumber(L, 4);
add_vol<<<(THCudaTensor_nElement(out) - 1) / TB + 1, TB>>>(
THCudaTensor_data(vol),
THCudaTensor_data(cnt),
THCudaTensor_data(out),
THCudaTensor_nElement(out),
THCudaTensor_size(vol, 1),
THCudaTensor_size(out, 2),
THCudaTensor_size(out, 3),
ratio);
checkCudaError(L);
return 0;
}
__global__ void rho(float *x, int size, float lambda)
{
int id = blockIdx.x * blockDim.x + threadIdx.x;
if (id < size) {
x[id] = 1 - exp(-x[id] / lambda);
}
}
int rho(lua_State *L)
{
THCudaTensor *x = (THCudaTensor*)luaT_checkudata(L, 1, "torch.CudaTensor");
float lambda = luaL_checknumber(L, 2);
rho<<<(THCudaTensor_nElement(x) - 1) / TB + 1, TB>>>(
THCudaTensor_data(x),
THCudaTensor_nElement(x),
lambda);
checkCudaError(L);
return 0;
}
#endif
__global__ void spatial_argmin(float *input, float *output, int size, int size1, int size23)
{
int id = blockIdx.x * blockDim.x + threadIdx.x;
if (id < size) {
int dim23 = id % size23;
int dim0 = id / size23;
int argmin = 0;
float min = CUDART_INF;
for (int i = 0; i < size1; i++) {
float val = input[(dim0 * size1 + i) * size23 + dim23];
if (val < min) {
min = val;
argmin = i;
}
}
output[id] = argmin + 1;
}
}
int spatial_argmin(lua_State *L)
{
THCState *state = getCutorchState(L);
THCudaTensor *input = (THCudaTensor*)luaT_checkudata(L, 1, "torch.CudaTensor");
THCudaTensor *output = (THCudaTensor*)luaT_checkudata(L, 2, "torch.CudaTensor");
spatial_argmin<<<(THCudaTensor_nElement(state, output) - 1) / TB + 1, TB>>>(
THCudaTensor_data(state, input),
THCudaTensor_data(state, output),
THCudaTensor_nElement(state, output),
THCudaTensor_size(state, input, 1),
THCudaTensor_size(state, input, 2) * THCudaTensor_size(state, output, 3));
checkCudaError(L);
return 0;
}
__global__ void cross(float *x0, float *out, int size, int dim2, int dim3, int L1, float tau1)
{
int id = blockIdx.x * blockDim.x + threadIdx.x;
if (id < size) {
int dir = id;
int x = dir % dim3;
dir /= dim3;
int y = dir % dim2;
dir /= dim2;
int dx = 0;
int dy = 0;
if (dir == 0) {
dx = -1;
} else if (dir == 1) {
dx = 1;
} else if (dir == 2) {
dy = -1;
} else if (dir == 3) {
dy = 1;
} else {
assert(0);
}
int xx, yy, ind1, ind2, dist;
ind1 = y * dim3 + x;
for (xx = x + dx, yy = y + dy;;xx += dx, yy += dy) {
if (xx < 0 || xx >= dim3 || yy < 0 || yy >= dim2) break;
dist = max(abs(xx - x), abs(yy - y));
if (dist == 1) continue;
ind2 = yy * dim3 + xx;
/* rule 1 */
if (COLOR_DIFF(x0, ind1, ind2) >= tau1) break;
/* rule 2 */
if (dist >= L1) break;
}
out[id] = dir <= 1 ? xx : yy;
}
}
int cross(lua_State *L)
{
THCState *state = getCutorchState(L);
THCudaTensor *x0 = (THCudaTensor*)luaT_checkudata(L, 1, "torch.CudaTensor");
THCudaTensor *out = (THCudaTensor*)luaT_checkudata(L, 2, "torch.CudaTensor");
int L1 = luaL_checkinteger(L, 3);
float tau1 = luaL_checknumber(L, 4);
cross<<<(THCudaTensor_nElement(state, out) - 1) / TB + 1, TB>>>(
THCudaTensor_data(state, x0),
THCudaTensor_data(state, out),
THCudaTensor_nElement(state, out),
THCudaTensor_size(state, out, 2),
THCudaTensor_size(state, out, 3),
L1, tau1);
checkCudaError(L);
return 0;
}
__global__ void cbca(float *x0c, float *x1c, float *vol, float *out, int size, int dim2, int dim3, int direction)
{
int id = blockIdx.x * blockDim.x + threadIdx.x;
if (id < size) {
int d = id;
int x = d % dim3;
d /= dim3;
int y = d % dim2;
d /= dim2;
if (x + d * direction < 0 || x + d * direction >= dim3) {
out[id] = vol[id];
} else {
float sum = 0;
int cnt = 0;
int yy_s = max(x0c[(2 * dim2 + y) * dim3 + x], x1c[(2 * dim2 + y) * dim3 + x + d * direction]);
int yy_t = min(x0c[(3 * dim2 + y) * dim3 + x], x1c[(3 * dim2 + y) * dim3 + x + d * direction]);
for (int yy = yy_s + 1; yy < yy_t; yy++) {
int xx_s = max(x0c[(0 * dim2 + yy) * dim3 + x], x1c[(0 * dim2 + yy) * dim3 + x + d * direction] - d * direction);
int xx_t = min(x0c[(1 * dim2 + yy) * dim3 + x], x1c[(1 * dim2 + yy) * dim3 + x + d * direction] - d * direction);
for (int xx = xx_s + 1; xx < xx_t; xx++) {
float val = vol[(d * dim2 + yy) * dim3 + xx];
assert(!isnan(val));
sum += val;
cnt++;
}
}
assert(cnt > 0);
out[id] = sum / cnt;
assert(!isnan(out[id]));
}
}
}
int cbca(lua_State *L)
{
THCState *state = getCutorchState(L);
THCudaTensor *x0c = (THCudaTensor*)luaT_checkudata(L, 1, "torch.CudaTensor");
THCudaTensor *x1c = (THCudaTensor*)luaT_checkudata(L, 2, "torch.CudaTensor");
THCudaTensor *vol_in = (THCudaTensor*)luaT_checkudata(L, 3, "torch.CudaTensor");
THCudaTensor *vol_out = (THCudaTensor*)luaT_checkudata(L, 4, "torch.CudaTensor");
int direction = luaL_checkinteger(L, 5);
assert(direction == -1 or direction == 1);
cbca<<<(THCudaTensor_nElement(state, vol_out) - 1) / TB + 1, TB>>>(
THCudaTensor_data(state, x0c),
THCudaTensor_data(state, x1c),
THCudaTensor_data(state, vol_in),
THCudaTensor_data(state, vol_out),
THCudaTensor_nElement(state, vol_out),
THCudaTensor_size(state, vol_out, 2),
THCudaTensor_size(state, vol_out, 3),
direction);
checkCudaError(L);
return 0;
}
__global__ void sgm(float *x0, float *x1, float *vol, float *tmp, float *out, int dim1, int dim2, int dim3, float pi1, float pi2, float tau_so, float alpha1, float sgm_q1, float sgm_q2, int sgm_direction, int direction)
{
int x, y, dx, dy;
dx = dy = 0;
if (sgm_direction <= 1) {
y = blockIdx.x * blockDim.x + threadIdx.x;
if (y >= dim2) {
return;
}
if (sgm_direction == 0) {
x = 0;
dx = 1;
} else if (sgm_direction == 1) {
x = dim3 - 1;
dx = -1;
}
} else if (sgm_direction <= 3) {
x = blockIdx.x * blockDim.x + threadIdx.x;
if (x >= dim3) {
return;
}
if (sgm_direction == 2) {
y = 0;
dy = 1;
} else if (sgm_direction == 3) {
y = dim2 - 1;
dy = -1;
}
}
assert(dim1 <= 400);
float tmp_curr_[400];
float tmp_prev_[400];
float *tmp_curr = tmp_curr_;
float *tmp_prev = tmp_prev_;
float min_prev = CUDART_INF;
for (; 0 <= y && y < dim2 && 0 <= x && x < dim3; x += dx, y += dy) {
float min_curr = CUDART_INF;
for (int d = 0; d < dim1; d++) {
int ind = (d * dim2 + y) * dim3 + x;
if (x + d * direction < 0 ||
x + d * direction >= dim3 ||
y - dy < 0 ||
y - dy >= dim2 ||
x + d * direction - dx < 0 ||
x + d * direction - dx >= dim3 ||
x - dx < 0 ||
x - dx >= dim3) {
out[ind] += vol[ind];
tmp_curr[d] = vol[ind];
} else {
int ind2 = y * dim3 + x;
float D1 = COLOR_DIFF(x0, ind2, ind2 - dy * dim3 - dx);
float D2 = COLOR_DIFF(x1, ind2 + d * direction, ind2 + d * direction - dy * dim3 - dx);
float P1, P2;
if (D1 < tau_so && D2 < tau_so) {
P1 = pi1;
P2 = (pi1 * pi2);
} else if (D1 > tau_so && D2 > tau_so) {
P1 = pi1 / (sgm_q1 * sgm_q2);
P2 = (pi1 * pi2) / (sgm_q1 * sgm_q2);
} else {
P1 = pi1 / sgm_q1;
P2 = (pi1 * pi2) / sgm_q1;
}
assert(min_prev != CUDART_INF);
float cost = min(tmp_prev[d], min_prev + P2);
if (d > 0) {
cost = min(cost, tmp_prev[d - 1] + (sgm_direction == 2 ? P1 / alpha1 : P1));
}
if (d < dim1 - 1) {
cost = min(cost, tmp_prev[d + 1] + (sgm_direction == 3 ? P1 / alpha1 : P1));
}
float val = vol[ind] + cost - min_prev;
out[ind] += val;
tmp_curr[d] = val;
}
if (tmp_curr[d] < min_curr) {
min_curr = tmp_curr[d];
}
}
min_prev = min_curr;
float *swap = tmp_curr;
tmp_curr = tmp_prev;
tmp_prev = swap;
}
}
int sgm(lua_State *L)
{
THCState *state = getCutorchState(L);
THCudaTensor *x0 = (THCudaTensor*)luaT_checkudata(L, 1, "torch.CudaTensor");
THCudaTensor *x1 = (THCudaTensor*)luaT_checkudata(L, 2, "torch.CudaTensor");
THCudaTensor *vol = (THCudaTensor*)luaT_checkudata(L, 3, "torch.CudaTensor");
THCudaTensor *tmp = (THCudaTensor*)luaT_checkudata(L, 4, "torch.CudaTensor");
THCudaTensor *out = (THCudaTensor*)luaT_checkudata(L, 5, "torch.CudaTensor");
float pi1 = luaL_checknumber(L, 6);
float pi2 = luaL_checknumber(L, 7);
float tau_so = luaL_checknumber(L, 8);
float alpha1 = luaL_checknumber(L, 9);
float sgm_q1 = luaL_checknumber(L, 10);
float sgm_q2 = luaL_checknumber(L, 11);
int direction = luaL_checknumber(L, 12);
int dim1 = THCudaTensor_size(state, out, 1);
int dim2 = THCudaTensor_size(state, out, 2);
int dim3 = THCudaTensor_size(state, out, 3);
for (int sgm_direction = 0; sgm_direction < 4; sgm_direction++) {
int size = sgm_direction <= 1 ? dim2 : dim3;
sgm<<<(size - 1) / TB + 1, TB>>>(
THCudaTensor_data(state, x0),
THCudaTensor_data(state, x1),
THCudaTensor_data(state, vol),
THCudaTensor_data(state, tmp),
THCudaTensor_data(state, out),
dim1, dim2, dim3, pi1, pi2, tau_so, alpha1, sgm_q1, sgm_q2, sgm_direction, direction);
}
checkCudaError(L);
return 0;
}
#define INDEX(dim0, dim1, dim2, dim3) \
assert((dim1) >= 0 && (dim1) < size1 && (dim2) >= 0 && (dim2) < size2 && (dim3) >= 0 && (dim3) < size3), \
((((dim0) * size1 + (dim1)) * size2 + (dim2)) * size3 + dim3)
template <int sgm_direction>
__global__ void sgm2(float *x0, float *x1, float *input, float *output, float *tmp, float pi1, float pi2, float tau_so, float alpha1, float sgm_q1, float sgm_q2, int direction, int size1, int size2, int size3, int step)
{
int x, y, dx, dy;
int d = threadIdx.x;
if (sgm_direction == 0) {
/* right */
x = step;
y = blockIdx.x;
dx = 1;
dy = 0;
} else if (sgm_direction == 1) {
/* left */
x = size2 - 1 - step;
y = blockIdx.x;
dx = -1;
dy = 0;
} else if (sgm_direction == 2) {
/* down */
x = blockIdx.x;
y = step;
dx = 0;
dy = 1;
} else if (sgm_direction == 3) {
/* up */
x = blockIdx.x;
y = size1 - 1 - step;
dx = 0;
dy = -1;
}
if (y - dy < 0 || y - dy >= size1 || x - dx < 0 || x - dx >= size2) {
float val = input[INDEX(0, y, x, d)];
output[INDEX(0, y, x, d)] += val;
tmp[d * size2 + blockIdx.x] = val;
return;
}
__shared__ float output_s[400], output_min[400];
output_s[d] = output_min[d] = tmp[d * size2 + blockIdx.x];
__syncthreads();
for (int i = 256; i > 0; i /= 2) {
if (d < i && d + i < size3 && output_min[d + i] < output_min[d]) {
output_min[d] = output_min[d + i];
}
__syncthreads();
}
int ind2 = y * size2 + x;
float D1 = COLOR_DIFF(x0, ind2, ind2 - dy * size2 - dx);
float D2;
int xx = x + d * direction;
if (xx < 0 || xx >= size2 || xx - dx < 0 || xx - dx >= size2) {
D2 = 10;
} else {
D2 = COLOR_DIFF(x1, ind2 + d * direction, ind2 + d * direction - dy * size2 - dx);
}
float P1, P2;
if (D1 < tau_so && D2 < tau_so) {
P1 = pi1;
P2 = pi2;
} else if (D1 > tau_so && D2 > tau_so) {
P1 = pi1 / (sgm_q1 * sgm_q2);
P2 = pi2 / (sgm_q1 * sgm_q2);
} else {
P1 = pi1 / sgm_q1;
P2 = pi2 / sgm_q1;
}
float cost = min(output_s[d], output_min[0] + P2);
if (d - 1 >= 0) {
cost = min(cost, output_s[d - 1] + (sgm_direction == 2 ? P1 / alpha1 : P1));
}
if (d + 1 < size3) {
cost = min(cost, output_s[d + 1] + (sgm_direction == 3 ? P1 / alpha1 : P1));
}
float val = input[INDEX(0, y, x, d)] + cost - output_min[0];
output[INDEX(0, y, x, d)] += val;
tmp[d * size2 + blockIdx.x] = val;
}
int sgm2(lua_State *L)
{
THCState *state = getCutorchState(L);
THCudaTensor *x0 = (THCudaTensor*)luaT_checkudata(L, 1, "torch.CudaTensor");
THCudaTensor *x1 = (THCudaTensor*)luaT_checkudata(L, 2, "torch.CudaTensor");
THCudaTensor *input = (THCudaTensor*)luaT_checkudata(L, 3, "torch.CudaTensor");
THCudaTensor *output = (THCudaTensor*)luaT_checkudata(L, 4, "torch.CudaTensor");
THCudaTensor *tmp = (THCudaTensor*)luaT_checkudata(L, 5, "torch.CudaTensor");
float pi1 = luaL_checknumber(L, 6);
float pi2 = luaL_checknumber(L, 7);
float tau_so = luaL_checknumber(L, 8);
float alpha1 = luaL_checknumber(L, 9);
float sgm_q1 = luaL_checknumber(L, 10);
float sgm_q2 = luaL_checknumber(L, 11);
int direction = luaL_checknumber(L, 12);
int size1 = THCudaTensor_size(state, output, 1) * THCudaTensor_size(state, output, 3);
int size2 = THCudaTensor_size(state, output, 2) * THCudaTensor_size(state, output, 3);
int disp_max = THCudaTensor_size(state, output, 3);
for (int step = 0; step < THCudaTensor_size(state, input, 2); step++) {
sgm2<0><<<(size1 - 1) / disp_max + 1, disp_max>>>(
THCudaTensor_data(state, x0),
THCudaTensor_data(state, x1),
THCudaTensor_data(state, input),
THCudaTensor_data(state, output),
THCudaTensor_data(state, tmp),
pi1, pi2, tau_so, alpha1, sgm_q1, sgm_q2, direction,
THCudaTensor_size(state, input, 1),
THCudaTensor_size(state, input, 2),
THCudaTensor_size(state, input, 3),
step);
}
for (int step = 0; step < THCudaTensor_size(state, input, 2); step++) {
sgm2<1><<<(size1 - 1) / disp_max + 1, disp_max>>>(
THCudaTensor_data(state, x0),
THCudaTensor_data(state, x1),
THCudaTensor_data(state, input),
THCudaTensor_data(state, output),
THCudaTensor_data(state, tmp),
pi1, pi2, tau_so, alpha1, sgm_q1, sgm_q2, direction,
THCudaTensor_size(state, input, 1),
THCudaTensor_size(state, input, 2),
THCudaTensor_size(state, input, 3),
step);
}
for (int step = 0; step < THCudaTensor_size(state, input, 1); step++) {
sgm2<2><<<(size2 - 1) / disp_max + 1, disp_max>>>(
THCudaTensor_data(state, x0),
THCudaTensor_data(state, x1),
THCudaTensor_data(state, input),
THCudaTensor_data(state, output),
THCudaTensor_data(state, tmp),
pi1, pi2, tau_so, alpha1, sgm_q1, sgm_q2, direction,
THCudaTensor_size(state, input, 1),
THCudaTensor_size(state, input, 2),
THCudaTensor_size(state, input, 3),
step);
}
for (int step = 0; step < THCudaTensor_size(state, input, 1); step++) {
sgm2<3><<<(size2 - 1) / disp_max + 1, disp_max>>>(
THCudaTensor_data(state, x0),
THCudaTensor_data(state, x1),
THCudaTensor_data(state, input),
THCudaTensor_data(state, output),
THCudaTensor_data(state, tmp),
pi1, pi2, tau_so, alpha1, sgm_q1, sgm_q2, direction,
THCudaTensor_size(state, input, 1),
THCudaTensor_size(state, input, 2),
THCudaTensor_size(state, input, 3),
step);
}
checkCudaError(L);
return 0;
}
template <int sgm_direction>
__global__ void sgm3(float *x0, float *x1, float *input, float *output, float pi1, float pi2, float tau_so, float alpha1, float sgm_q1, float sgm_q2, int direction, int size1, int size2, int size3, int step)
{
int x, y, dx, dy;
int d = threadIdx.x;
if (sgm_direction == 0) {
/* right */
x = step;
y = blockIdx.x;
dx = 1;
dy = 0;
} else if (sgm_direction == 1) {
/* left */
x = size2 - 1 - step;
y = blockIdx.x;
dx = -1;
dy = 0;
} else if (sgm_direction == 2) {
/* down */
x = blockIdx.x;
y = step;
dx = 0;
dy = 1;
} else if (sgm_direction == 3) {
/* up */
x = blockIdx.x;
y = size1 - 1 - step;
dx = 0;
dy = -1;
}
if (y - dy < 0 || y - dy >= size1 || x - dx < 0 || x - dx >= size2) {
output[INDEX(sgm_direction, y, x, d)] = input[INDEX(0, y, x, d)];
return;
}
__shared__ float output_s[400], output_min[400];
output_s[d] = output_min[d] = output[INDEX(sgm_direction, y - dy, x - dx, d)];
__syncthreads();
for (int i = 256; i > 0; i /= 2) {
if (d < i && d + i < size3 && output_min[d + i] < output_min[d]) {
output_min[d] = output_min[d + i];
}
__syncthreads();
}
int ind2 = y * size2 + x;
float D1 = COLOR_DIFF(x0, ind2, ind2 - dy * size2 - dx);
float D2;
int xx = x + d * direction;
if (xx < 0 || xx >= size2 || xx - dx < 0 || xx - dx >= size2) {
D2 = 10;
} else {
D2 = COLOR_DIFF(x1, ind2 + d * direction, ind2 + d * direction - dy * size2 - dx);
}
float P1, P2;
if (D1 < tau_so && D2 < tau_so) {
P1 = pi1;
P2 = pi2;
} else if (D1 > tau_so && D2 > tau_so) {
P1 = pi1 / (sgm_q1 * sgm_q2);
P2 = pi2 / (sgm_q1 * sgm_q2);
} else {
P1 = pi1 / sgm_q1;
P2 = pi2 / sgm_q1;
}
float cost = min(output_s[d], output_min[0] + P2);
if (d - 1 >= 0) {
cost = min(cost, output_s[d - 1] + (sgm_direction == 2 ? P1 / alpha1 : P1));
}
if (d + 1 < size3) {
cost = min(cost, output_s[d + 1] + (sgm_direction == 3 ? P1 / alpha1 : P1));
}
output[INDEX(sgm_direction, y, x, d)] = input[INDEX(0, y, x, d)] + cost - output_min[0];
}
int sgm3(lua_State *L)
{
THCState *state = getCutorchState(L);
THCudaTensor *x0 = (THCudaTensor*)luaT_checkudata(L, 1, "torch.CudaTensor");
THCudaTensor *x1 = (THCudaTensor*)luaT_checkudata(L, 2, "torch.CudaTensor");
THCudaTensor *input = (THCudaTensor*)luaT_checkudata(L, 3, "torch.CudaTensor");
THCudaTensor *output = (THCudaTensor*)luaT_checkudata(L, 4, "torch.CudaTensor");
float pi1 = luaL_checknumber(L, 5);
float pi2 = luaL_checknumber(L, 6);
float tau_so = luaL_checknumber(L, 7);
float alpha1 = luaL_checknumber(L, 8);
float sgm_q1 = luaL_checknumber(L, 9);
float sgm_q2 = luaL_checknumber(L, 10);
int direction = luaL_checknumber(L, 11);
int size1 = THCudaTensor_size(state, output, 1) * THCudaTensor_size(state, output, 3);
int size2 = THCudaTensor_size(state, output, 2) * THCudaTensor_size(state, output, 3);
int disp_max = THCudaTensor_size(state, output, 3);
for (int step = 0; step < THCudaTensor_size(state, input, 2); step++) {
sgm3<0><<<(size1 - 1) / disp_max + 1, disp_max>>>(
THCudaTensor_data(state, x0),
THCudaTensor_data(state, x1),
THCudaTensor_data(state, input),
THCudaTensor_data(state, output),
pi1, pi2, tau_so, alpha1, sgm_q1, sgm_q2, direction,
THCudaTensor_size(state, input, 1),
THCudaTensor_size(state, input, 2),
THCudaTensor_size(state, input, 3),
step);
}
for (int step = 0; step < THCudaTensor_size(state, input, 2); step++) {
sgm3<1><<<(size1 - 1) / disp_max + 1, disp_max>>>(
THCudaTensor_data(state, x0),
THCudaTensor_data(state, x1),
THCudaTensor_data(state, input),
THCudaTensor_data(state, output),
pi1, pi2, tau_so, alpha1, sgm_q1, sgm_q2, direction,
THCudaTensor_size(state, input, 1),
THCudaTensor_size(state, input, 2),
THCudaTensor_size(state, input, 3),
step);
}
for (int step = 0; step < THCudaTensor_size(state, input, 1); step++) {
sgm3<2><<<(size2 - 1) / disp_max + 1, disp_max>>>(
THCudaTensor_data(state, x0),
THCudaTensor_data(state, x1),
THCudaTensor_data(state, input),
THCudaTensor_data(state, output),
pi1, pi2, tau_so, alpha1, sgm_q1, sgm_q2, direction,
THCudaTensor_size(state, input, 1),
THCudaTensor_size(state, input, 2),
THCudaTensor_size(state, input, 3),
step);
}
for (int step = 0; step < THCudaTensor_size(state, input, 1); step++) {
sgm3<3><<<(size2 - 1) / disp_max + 1, disp_max>>>(
THCudaTensor_data(state, x0),
THCudaTensor_data(state, x1),
THCudaTensor_data(state, input),
THCudaTensor_data(state, output),
pi1, pi2, tau_so, alpha1, sgm_q1, sgm_q2, direction,
THCudaTensor_size(state, input, 1),
THCudaTensor_size(state, input, 2),
THCudaTensor_size(state, input, 3),
step);
}
checkCudaError(L);
return 0;
}
__global__ void fliplr(float *in, float *out, int size, int dim3)
{
int id = blockIdx.x * blockDim.x + threadIdx.x;
if (id < size) {
int x = id % dim3;
out[id + dim3 - 2 * x - 1] = in[id];
}
}
int fliplr(lua_State *L)
{
THCState *state = getCutorchState(L);
THCudaTensor *in = (THCudaTensor*)luaT_checkudata(L, 1, "torch.CudaTensor");
THCudaTensor *out = (THCudaTensor*)luaT_checkudata(L, 2, "torch.CudaTensor");
fliplr<<<(THCudaTensor_nElement(state, out) - 1) / TB + 1, TB>>>(
THCudaTensor_data(state, in),
THCudaTensor_data(state, out),
THCudaTensor_nElement(state, out),
THCudaTensor_size(state, out, 3));
checkCudaError(L);
return 0;
}
__global__ void outlier_detection(float *d0, float *d1, float *outlier, int size, int dim3, int disp_max)
{
int id = blockIdx.x * blockDim.x + threadIdx.x;
if (id < size) {
int x = id % dim3;
int d0i = d0[id];
if (x - d0i < 0) {
//assert(0);
outlier[id] = 1;
} else if (abs(d0[id] - d1[id - d0i]) < 1.1) {
outlier[id] = 0; /* match */
} else {
outlier[id] = 1; /* occlusion */
for (int d = 0; d < disp_max; d++) {
if (x - d >= 0 && abs(d - d1[id - d]) < 1.1) {
outlier[id] = 2; /* mismatch */
break;
}
}
}
}
}
int outlier_detection(lua_State *L)
{
THCState *state = getCutorchState(L);
THCudaTensor *d0 = (THCudaTensor*)luaT_checkudata(L, 1, "torch.CudaTensor");
THCudaTensor *d1 = (THCudaTensor*)luaT_checkudata(L, 2, "torch.CudaTensor");
THCudaTensor *outlier = (THCudaTensor*)luaT_checkudata(L, 3, "torch.CudaTensor");
int disp_max = luaL_checkinteger(L, 4);
outlier_detection<<<(THCudaTensor_nElement(state, d0) - 1) / TB + 1, TB>>>(
THCudaTensor_data(state, d0),
THCudaTensor_data(state, d1),
THCudaTensor_data(state, outlier),
THCudaTensor_nElement(state, d0),
THCudaTensor_size(state, d0, 3),
disp_max);
checkCudaError(L);
return 0;
}
#if 0
__global__ void iterative_region_voting(float *d0, float *x0c, float *x1c, float *outlier, float *d0_out, float *outlier_out, int size, int dim2, int dim3, float tau_s, float tau_h, int disp_max)
{
int id = blockIdx.x * blockDim.x + threadIdx.x;
if (id < size) {
int x = id % dim3;
int y = id / dim3;
d0_out[id] = d0[id];
outlier_out[id] = outlier[id];
if (outlier[id] == 0) return;
assert(disp_max < DISP_MAX);
int hist[DISP_MAX];
for (int i = 0; i < disp_max; i++) {
hist[i] = 0;
}
int yy_s = x0c[(2 * dim2 + y) * dim3 + x];
int yy_t = x0c[(3 * dim2 + y) * dim3 + x];
for (int yy = yy_s + 1; yy < yy_t; yy++) {
int xx_s = x0c[(0 * dim2 + yy) * dim3 + x];
int xx_t = x0c[(1 * dim2 + yy) * dim3 + x];
for (int xx = xx_s + 1; xx < xx_t; xx++) {
if (outlier[yy * dim3 + xx] == 0) {
hist[(int)d0[yy * dim3 + xx]]++;
}
}
}
int cnt = 0;
int max_i = 0;
for (int i = 0; i < disp_max; i++) {
cnt += hist[i];
if (hist[i] > hist[max_i]) {
max_i = i;
}
}
if (cnt > tau_s && (float)hist[max_i] / cnt > tau_h) {
outlier_out[id] = 0;
d0_out[id] = max_i;
}
}
}
int iterative_region_voting(lua_State *L)
{
THCudaTensor *d0 = (THCudaTensor*)luaT_checkudata(L, 1, "torch.CudaTensor");
THCudaTensor *x0c = (THCudaTensor*)luaT_checkudata(L, 2, "torch.CudaTensor");
THCudaTensor *x1c = (THCudaTensor*)luaT_checkudata(L, 3, "torch.CudaTensor");
THCudaTensor *outlier = (THCudaTensor*)luaT_checkudata(L, 4, "torch.CudaTensor");
float tau_s = luaL_checknumber(L, 5);
float tau_h = luaL_checknumber(L, 6);
int disp_max = luaL_checkinteger(L, 7);
int iterations = luaL_checkinteger(L, 8);
THCudaTensor *d0_tmp = new_tensor_like(state, d0);
THCudaTensor *outlier_tmp = new_tensor_like(state, outlier);
assert(iterations % 2 == 0);
for (int i = 0; i < iterations; i++) {
iterative_region_voting<<<(THCudaTensor_nElement(d0) - 1) / TB + 1, TB>>>(
THCudaTensor_data(i % 2 == 0 ? d0 : d0_tmp),
THCudaTensor_data(x0c),
THCudaTensor_data(x1c),
THCudaTensor_data(i % 2 == 0 ? outlier : outlier_tmp),
THCudaTensor_data(i % 2 == 0 ? d0_tmp : d0),
THCudaTensor_data(i % 2 == 0 ? outlier_tmp : outlier),
THCudaTensor_nElement(d0),
THCudaTensor_size(d0, 2),
THCudaTensor_size(d0, 3),
tau_s, tau_h, disp_max);
}
checkCudaError(L);
return 0;
}
#endif