-
Notifications
You must be signed in to change notification settings - Fork 232
/
main.lua
executable file
·1293 lines (1174 loc) · 42.2 KB
/
main.lua
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
#! /usr/bin/env luajit
require 'torch'
io.stdout:setvbuf('no')
for i = 1,#arg do
io.write(arg[i] .. ' ')
end
io.write('\n')
dataset = table.remove(arg, 1)
arch = table.remove(arg, 1)
assert(dataset == 'kitti' or dataset == 'kitti2015' or dataset == 'mb')
assert(arch == 'fast' or arch == 'slow' or arch == 'ad' or arch == 'census')
cmd = torch.CmdLine()
cmd:option('-gpu', 1, 'gpu id')
cmd:option('-seed', 42, 'random seed')
cmd:option('-debug', false)
cmd:option('-d', 'kitti | mb')
cmd:option('-a', 'train_tr | train_all | test_te | test_all | submit | time | predict', 'train')
cmd:option('-net_fname', '')
cmd:option('-make_cache', false)
cmd:option('-use_cache', false)
cmd:option('-print_args', false)
cmd:option('-sm_terminate', '', 'terminate the stereo method after this step')
cmd:option('-sm_skip', '', 'which part of the stereo method to skip')
cmd:option('-tiny', false)
cmd:option('-subset', 1)
cmd:option('-left', '')
cmd:option('-right', '')
cmd:option('-disp_max', '')
if dataset == 'kitti' or dataset == 'kitti2015' then
cmd:option('-hflip', 0)
cmd:option('-vflip', 0)
cmd:option('-rotate', 7)
cmd:option('-hscale', 0.9)
cmd:option('-scale', 1)
cmd:option('-trans', 0)
cmd:option('-hshear', 0.1)
cmd:option('-brightness', 0.7)
cmd:option('-contrast', 1.3)
cmd:option('-d_vtrans', 0)
cmd:option('-d_rotate', 0)
cmd:option('-d_hscale', 1)
cmd:option('-d_hshear', 0)
cmd:option('-d_brightness', 0.3)
cmd:option('-d_contrast', 1)
elseif dataset == 'mb' then
cmd:option('-hflip', 0)
cmd:option('-vflip', 0)
cmd:option('-rotate', 28)
cmd:option('-hscale', 0.8)
cmd:option('-scale', 0.8)
cmd:option('-trans', 0)
cmd:option('-hshear', 0.1)
cmd:option('-brightness', 1.3)
cmd:option('-contrast', 1.1)
cmd:option('-d_vtrans', 1)
cmd:option('-d_rotate', 3)
cmd:option('-d_hscale', 0.9)
cmd:option('-d_hshear', 0.3)
cmd:option('-d_brightness', 0.7)
cmd:option('-d_contrast', 1.1)
end
cmd:option('-rect', 'imperfect')
cmd:option('-color', 'gray')
if arch == 'slow' then
if dataset == 'kitti' or dataset == 'kitti2015' then
cmd:option('-at', 0)
cmd:option('-l1', 4)
cmd:option('-fm', 112)
cmd:option('-ks', 3)
cmd:option('-l2', 4)
cmd:option('-nh2', 384)
cmd:option('-lr', 0.003)
cmd:option('-bs', 128)
cmd:option('-mom', 0.9)
cmd:option('-true1', 1)
cmd:option('-false1', 4)
cmd:option('-false2', 10)
if dataset == 'kitti' then
cmd:option('-L1', 5)
cmd:option('-cbca_i1', 2)
cmd:option('-cbca_i2', 0)
cmd:option('-tau1', 0.13)
cmd:option('-pi1', 1.32)
cmd:option('-pi2', 24.25)
cmd:option('-sgm_i', 1)
cmd:option('-sgm_q1', 3)
cmd:option('-sgm_q2', 2)
cmd:option('-alpha1', 2)
cmd:option('-tau_so', 0.08)
cmd:option('-blur_sigma', 5.99)
cmd:option('-blur_t', 6)
elseif dataset == 'kitti2015' then
cmd:option('-L1', 5)
cmd:option('-cbca_i1', 2)
cmd:option('-cbca_i2', 4)
cmd:option('-tau1', 0.03)
cmd:option('-pi1', 2.3)
cmd:option('-pi2', 24.25)
cmd:option('-sgm_i', 1)
cmd:option('-sgm_q1', 3)
cmd:option('-sgm_q2', 2)
cmd:option('-alpha1', 1.75)
cmd:option('-tau_so', 0.08)
cmd:option('-blur_sigma', 5.99)
cmd:option('-blur_t', 5)
end
elseif dataset == 'mb' then
cmd:option('-ds', 2001)
cmd:option('-d_exp', 0.2)
cmd:option('-d_light', 0.2)
cmd:option('-l1', 5)
cmd:option('-fm', 112)
cmd:option('-ks', 3)
cmd:option('-l2', 3)
cmd:option('-nh2', 384)
cmd:option('-lr', 0.003)
cmd:option('-bs', 128)
cmd:option('-mom', 0.9)
cmd:option('-true1', 0.5)
cmd:option('-false1', 1.5)
cmd:option('-false2', 18)
cmd:option('-L1', 14)
cmd:option('-tau1', 0.02)
cmd:option('-cbca_i1', 2)
cmd:option('-cbca_i2', 16)
cmd:option('-pi1', 1.3)
cmd:option('-pi2', 13.9)
cmd:option('-sgm_i', 1)
cmd:option('-sgm_q1', 4.5)
cmd:option('-sgm_q2', 2)
cmd:option('-alpha1', 2.75)
cmd:option('-tau_so', 0.13)
cmd:option('-blur_sigma', 1.67)
cmd:option('-blur_t', 2)
end
elseif arch == 'census' then
if dataset == 'kitti' or dataset == 'kitti2015' then
cmd:option('-L1', 0)
cmd:option('-cbca_i1', 4)
cmd:option('-cbca_i2', 8)
cmd:option('-tau1', 0.01)
cmd:option('-pi1', 4)
cmd:option('-pi2', 128.00)
cmd:option('-sgm_i', 1)
cmd:option('-sgm_q1', 3)
cmd:option('-sgm_q2', 3.5)
cmd:option('-alpha1', 1.25)
cmd:option('-tau_so', 1.0)
cmd:option('-blur_sigma', 7.74)
cmd:option('-blur_t', 6)
elseif dataset == 'mb' then
cmd:option('-L1', 5)
cmd:option('-cbca_i1', 8)
cmd:option('-cbca_i2', 8)
cmd:option('-tau1', 0.22)
cmd:option('-pi1', 4.0)
cmd:option('-pi2', 32.0)
cmd:option('-sgm_i', 1)
cmd:option('-sgm_q1', 4)
cmd:option('-sgm_q2', 3)
cmd:option('-alpha1', 1.5)
cmd:option('-tau_so', 1.00)
cmd:option('-blur_sigma', 2.78)
cmd:option('-blur_t', 3)
end
elseif arch == 'ad' then
if dataset == 'kitti' or dataset == 'kitti2015' then
cmd:option('-L1', 3)
cmd:option('-cbca_i1', 0)
cmd:option('-cbca_i2', 4)
cmd:option('-tau1', 0.03)
cmd:option('-pi1', 0.76)
cmd:option('-pi2', 13.93)
cmd:option('-sgm_i', 1)
cmd:option('-sgm_q1', 3.5)
cmd:option('-sgm_q2', 2)
cmd:option('-alpha1', 2.5)
cmd:option('-tau_so', 0.01)
cmd:option('-blur_sigma', 7.74)
cmd:option('-blur_t', 6)
elseif dataset == 'mb' then
cmd:option('-L1', 5)
cmd:option('-cbca_i1', 0)
cmd:option('-cbca_i2', 4)
cmd:option('-tau1', 0.36)
cmd:option('-pi1', 0.4)
cmd:option('-pi2', 8.0)
cmd:option('-sgm_i', 1)
cmd:option('-sgm_q1', 3)
cmd:option('-sgm_q2', 4)
cmd:option('-alpha1', 2.5)
cmd:option('-tau_so', 0.08)
cmd:option('-blur_sigma', 7.74)
cmd:option('-blur_t', 1)
end
elseif arch == 'fast' then
if dataset == 'kitti' then
cmd:option('-at', 0)
cmd:option('-m', 0.2, 'margin')
cmd:option('-pow', 1)
cmd:option('-l1', 4)
cmd:option('-fm', 64)
cmd:option('-ks', 3)
cmd:option('-lr', 0.002)
cmd:option('-bs', 128)
cmd:option('-mom', 0.9)
cmd:option('-true1', 1)
cmd:option('-false1', 4)
cmd:option('-false2', 10)
cmd:option('-L1', 0)
cmd:option('-cbca_i1', 0)
cmd:option('-cbca_i2', 0)
cmd:option('-tau1', 0)
cmd:option('-pi1', 4)
cmd:option('-pi2', 55.72)
cmd:option('-sgm_i', 1)
cmd:option('-sgm_q1', 3)
cmd:option('-sgm_q2', 2.5)
cmd:option('-alpha1', 1.5)
cmd:option('-tau_so', 0.02)
cmd:option('-blur_sigma', 7.74)
cmd:option('-blur_t', 5)
elseif dataset == 'kitti2015' then
cmd:option('-at', 0)
cmd:option('-m', 0.2, 'margin')
cmd:option('-pow', 1)
cmd:option('-l1', 4)
cmd:option('-fm', 64)
cmd:option('-ks', 3)
cmd:option('-lr', 0.002)
cmd:option('-bs', 128)
cmd:option('-mom', 0.9)
cmd:option('-true1', 1)
cmd:option('-false1', 4)
cmd:option('-false2', 10)
cmd:option('-L1', 0)
cmd:option('-cbca_i1', 0)
cmd:option('-cbca_i2', 0)
cmd:option('-tau1', 0)
cmd:option('-pi1', 2.3)
cmd:option('-pi2', 18.38)
cmd:option('-sgm_i', 1)
cmd:option('-sgm_q1', 3)
cmd:option('-sgm_q2', 2)
cmd:option('-alpha1', 1.25)
cmd:option('-tau_so', 0.08)
cmd:option('-blur_sigma', 4.64)
cmd:option('-blur_t', 5)
elseif dataset == 'mb' then
cmd:option('-m', 0.2, 'margin')
cmd:option('-pow', 1)
cmd:option('-ds', 2001)
cmd:option('-d_exp', 0.2)
cmd:option('-d_light', 0.2)
cmd:option('-l1', 5)
cmd:option('-fm', 64)
cmd:option('-ks', 3)
cmd:option('-lr', 0.002)
cmd:option('-bs', 128)
cmd:option('-mom', 0.9)
cmd:option('-true1', 0.5)
cmd:option('-false1', 1.5)
cmd:option('-false2', 6)
cmd:option('-L1', 0)
cmd:option('-tau1', 0.0)
cmd:option('-cbca_i1', 0)
cmd:option('-cbca_i2', 0)
cmd:option('-pi1', 2.3)
cmd:option('-pi2', 24.3)
cmd:option('-sgm_i', 1)
cmd:option('-sgm_q1', 4)
cmd:option('-sgm_q2', 2)
cmd:option('-alpha1', 1.5)
cmd:option('-tau_so', 0.08)
cmd:option('-blur_sigma', 6)
cmd:option('-blur_t', 2)
end
end
opt = cmd:parse(arg)
if opt.print_args then
print((opt.ks - 1) * opt.l1 + 1, 'arch_patch_size')
print(opt.l1, 'arch1_num_layers')
print(opt.fm, 'arch1_num_feature_maps')
print(opt.ks, 'arch1_kernel_size')
print(opt.l2, 'arch2_num_layers')
print(opt.nh2, 'arch2_num_units_2')
print(opt.false1, 'dataset_neg_low')
print(opt.false2, 'dataset_neg_high')
print(opt.true1, 'dataset_pos_low')
print(opt.tau1, 'cbca_intensity')
print(opt.L1, 'cbca_distance')
print(opt.cbca_i1, 'cbca_num_iterations_1')
print(opt.cbca_i2, 'cbca_num_iterations_2')
print(opt.pi1, 'sgm_P1')
print(opt.pi1 * opt.pi2, 'sgm_P2')
print(opt.sgm_q1, 'sgm_Q1')
print(opt.sgm_q1 * opt.sgm_q2, 'sgm_Q2')
print(opt.alpha1, 'sgm_V')
print(opt.tau_so, 'sgm_intensity')
print(opt.blur_sigma, 'blur_sigma')
print(opt.blur_t, 'blur_threshold')
os.exit()
end
require 'cunn'
require 'cutorch'
require 'image'
require 'libadcensus'
require 'libcv'
require 'cudnn'
cudnn.benchmark = true
include('Margin2.lua')
include('Normalize2.lua')
include('BCECriterion2.lua')
include('StereoJoin.lua')
include('StereoJoin1.lua')
include('SpatialConvolution1_fw.lua')
-- include('SpatialLogSoftMax.lua')
torch.manualSeed(opt.seed)
cutorch.manualSeed(opt.seed)
cutorch.setDevice(tonumber(opt.gpu))
cmd_str = dataset .. '_' .. arch
for i = 1,#arg do
cmd_str = cmd_str .. '_' .. arg[i]
end
function isnan(n)
return tostring(n) == tostring(0/0)
end
function fromfile(fname)
local file = io.open(fname .. '.dim')
local dim = {}
for line in file:lines() do
table.insert(dim, tonumber(line))
end
if #dim == 1 and dim[1] == 0 then
return torch.Tensor()
end
local file = io.open(fname .. '.type')
local type = file:read('*all')
local x
if type == 'float32' then
x = torch.FloatTensor(torch.FloatStorage(fname))
elseif type == 'int32' then
x = torch.IntTensor(torch.IntStorage(fname))
elseif type == 'int64' then
x = torch.LongTensor(torch.LongStorage(fname))
else
print(fname, type)
assert(false)
end
x = x:reshape(torch.LongStorage(dim))
return x
end
function get_window_size(net)
ws = 1
for i = 1,#net.modules do
local module = net:get(i)
if torch.typename(module) == 'cudnn.SpatialConvolution' then
ws = ws + module.kW - 1
end
end
return ws
end
-- load training data
if dataset == 'kitti' or dataset == 'kitti2015' then
height = 350
width = 1242
disp_max = 228
n_te = dataset == 'kitti' and 195 or 200
n_input_plane = 1
err_at = 3
if opt.a == 'train_tr' or opt.a == 'train_all' or opt.a == 'test_te' or opt.a == 'test_all' or opt.a == 'submit' then
if opt.at == 1 then
function load(fname)
local X_12 = fromfile('data.kitti/' .. fname)
local X_15 = fromfile('data.kitti2015/' .. fname)
local X = torch.cat(X_12[{{1,194}}], X_15[{{1,200}}], 1)
X = torch.cat(X, dataset == 'kitti' and X_12[{{195,389}}] or X_15[{{200,400}}], 1)
return X
end
X0 = load('x0.bin')
X1 = load('x1.bin')
metadata = load('metadata.bin')
dispnoc = torch.cat(fromfile('data.kitti/dispnoc.bin'), fromfile('data.kitti2015/dispnoc.bin'), 1)
tr = torch.cat(fromfile('data.kitti/tr.bin'), fromfile('data.kitti2015/tr.bin'):add(194))
te = dataset == 'kitti' and fromfile('data.kitti/te.bin') or fromfile('data.kitti2015/te.bin'):add(194)
function load_nnz(fname)
local X_12 = fromfile('data.kitti/' .. fname)
local X_15 = fromfile('data.kitti2015/' .. fname)
X_15[{{},1}]:add(194)
return torch.cat(X_12, X_15, 1)
end
nnz_tr = load_nnz('nnz_tr.bin')
nnz_te = load_nnz('nnz_te.bin')
elseif dataset == 'kitti' then
X0 = fromfile('data.kitti/x0.bin')
X1 = fromfile('data.kitti/x1.bin')
dispnoc = fromfile('data.kitti/dispnoc.bin')
metadata = fromfile('data.kitti/metadata.bin')
tr = fromfile('data.kitti/tr.bin')
te = fromfile('data.kitti/te.bin')
nnz_tr = fromfile('data.kitti/nnz_tr.bin')
nnz_te = fromfile('data.kitti/nnz_te.bin')
elseif dataset == 'kitti2015' then
X0 = fromfile('data.kitti2015/x0.bin')
X1 = fromfile('data.kitti2015/x1.bin')
dispnoc = fromfile('data.kitti2015/dispnoc.bin')
metadata = fromfile('data.kitti2015/metadata.bin')
tr = fromfile('data.kitti2015/tr.bin')
te = fromfile('data.kitti2015/te.bin')
nnz_tr = fromfile('data.kitti2015/nnz_tr.bin')
nnz_te = fromfile('data.kitti2015/nnz_te.bin')
end
end
elseif dataset == 'mb' then
if opt.color == 'rgb' then
n_input_plane = 3
else
n_input_plane = 1
end
err_at = 1
if opt.a == 'train_tr' or opt.a == 'train_all' or opt.a == 'test_te' or opt.a == 'test_all' or opt.a == 'submit' then
data_dir = ('data.mb.%s_%s'):format(opt.rect, opt.color)
te = fromfile(('%s/te.bin'):format(data_dir))
metadata = fromfile(('%s/meta.bin'):format(data_dir))
nnz_tr = fromfile(('%s/nnz_tr.bin'):format(data_dir))
nnz_te = fromfile(('%s/nnz_te.bin'):format(data_dir))
fname_submit = {}
for line in io.open(('%s/fname_submit.txt'):format(data_dir), 'r'):lines() do
table.insert(fname_submit, line)
end
X = {}
dispnoc = {}
height = 1500
width = 1000
for n = 1,metadata:size(1) do
local XX = {}
light = 1
while true do
fname = ('%s/x_%d_%d.bin'):format(data_dir, n, light)
if not paths.filep(fname) then
break
end
table.insert(XX, fromfile(fname))
light = light + 1
if opt.a == 'test_te' or opt.a == 'submit' then
break -- we don't need to load training data
end
end
table.insert(X, XX)
fname = ('%s/dispnoc%d.bin'):format(data_dir, n)
if paths.filep(fname) then
table.insert(dispnoc, fromfile(fname))
end
end
end
end
function savePNG(fname, x, isvol)
local pred
local pred_jet = torch.Tensor(1, 3, x:size(3), x:size(4))
if isvol == true then
pred = torch.CudaTensor(1, 1, x:size(3), x:size(4))
adcensus.spatial_argmin(x, pred)
else
pred = x:double():add(1)
end
adcensus.grey2jet(pred[{1,1}]:div(disp_max):double(), pred_jet)
image.savePNG(fname, pred_jet[1])
end
function saveOutlier(fname, x0, outlier)
local img = torch.Tensor(1,3,height,img_width)
img[{1,1}]:copy(x0)
img[{1,2}]:copy(x0)
img[{1,3}]:copy(x0)
for i=1,height do
for j=1,img_width do
if outlier[{1,1,i,j}] == 1 then
img[{1,1,i,j}] = 0
img[{1,2,i,j}] = 1
img[{1,3,i,j}] = 0
elseif outlier[{1,1,i,j}] == 2 then
img[{1,1,i,j}] = 1
img[{1,2,i,j}] = 0
img[{1,3,i,j}] = 0
end
end
end
image.savePNG(fname, img[1])
end
function gaussian(sigma)
local kr = math.ceil(sigma * 3)
local ks = kr * 2 + 1
local k = torch.Tensor(ks, ks)
for i = 1, ks do
for j = 1, ks do
local y = (i - 1) - kr
local x = (j - 1) - kr
k[{i,j}] = math.exp(-(x * x + y * y) / (2 * sigma * sigma))
end
end
return k
end
function print_net(net)
local s
local t = torch.typename(net)
if t == 'cudnn.SpatialConvolution' then
print(('conv(in=%d, out=%d, k=%d)'):format(net.nInputPlane, net.nOutputPlane, net.kW))
elseif t == 'nn.SpatialConvolutionMM_dsparse' then
print(('conv_dsparse(in=%d, out=%d, k=%d, s=%d)'):format(net.nInputPlane, net.nOutputPlane, net.kW, net.sW))
elseif t == 'cudnn.SpatialMaxPooling' then
print(('max_pool(k=%d, d=%d)'):format(net.kW, net.dW))
elseif t == 'nn.StereoJoin' then
print(('StereoJoin(%d)'):format(net.disp_max))
elseif t == 'nn.Margin2' then
print(('Margin2(margin=%f, pow=%d)'):format(opt.m, opt.pow))
elseif t == 'nn.GHCriterion' then
print(('GHCriterion(m_pos=%f, m_neg=%f, pow=%d)'):format(opt.m_pos, opt.m_neg, opt.pow))
elseif t == 'nn.Sequential' then
for i = 1,#net.modules do
print_net(net.modules[i])
end
else
print(net)
end
end
function clean_net(net)
net.output = torch.CudaTensor()
net.gradInput = nil
net.weight_v = nil
net.bias_v = nil
net.gradWeight = nil
net.gradBias = nil
net.iDesc = nil
net.oDesc = nil
net.finput = torch.CudaTensor()
net.fgradInput = torch.CudaTensor()
net.tmp_in = torch.CudaTensor()
net.tmp_out = torch.CudaTensor()
if net.modules then
for _, module in ipairs(net.modules) do
clean_net(module)
end
end
return net
end
function save_net(epoch)
if arch == 'slow' then
obj = {clean_net(net_te), clean_net(net_te2), opt}
elseif arch == 'fast' then
obj = {clean_net(net_te), opt}
end
if epoch == 0 then
fname = ('net/net_%s.t7'):format(cmd_str)
else
fname = ('net/net_%s_%d.t7'):format(cmd_str, epoch)
end
torch.save(fname, obj, 'ascii')
return fname
end
if opt.a == 'train_tr' or opt.a == 'train_all' or opt.a == 'time' then
function mul32(a,b)
return {a[1]*b[1]+a[2]*b[4], a[1]*b[2]+a[2]*b[5], a[1]*b[3]+a[2]*b[6]+a[3], a[4]*b[1]+a[5]*b[4], a[4]*b[2]+a[5]*b[5], a[4]*b[3]+a[5]*b[6]+a[6]}
end
function make_patch(src, dst, dim3, dim4, scale, phi, trans, hshear, brightness, contrast)
local m = {1, 0, -dim4, 0, 1, -dim3}
m = mul32({1, 0, trans[1], 0, 1, trans[2]}, m) -- translate
m = mul32({scale[1], 0, 0, 0, scale[2], 0}, m) -- scale
local c = math.cos(phi)
local s = math.sin(phi)
m = mul32({c, s, 0, -s, c, 0}, m) -- rotate
m = mul32({1, hshear, 0, 0, 1, 0}, m) -- shear
m = mul32({1, 0, (ws - 1) / 2, 0, 1, (ws - 1) / 2}, m)
m = torch.FloatTensor(m)
cv.warp_affine(src, dst, m)
dst:mul(contrast):add(brightness)
end
-- subset training dataset
if opt.subset < 1 then
function sample(xs, p)
local perm = torch.randperm(xs:nElement()):long()
return xs:index(1, perm[{{1, xs:size(1) * p}}])
end
local tr_subset
if dataset == 'kitti' or dataset == 'kitti2015' then
tr_subset = sample(tr, opt.subset)
elseif dataset == 'mb' then
tr_2014 = sample(torch.range(11, 23):long(), opt.subset)
tr_2006 = sample(torch.range(24, 44):long(), opt.subset)
tr_2005 = sample(torch.range(45, 50):long(), opt.subset)
tr_2003 = sample(torch.range(51, 52):long(), opt.subset)
tr_2001 = sample(torch.range(53, 60):long(), opt.subset)
tr_subset = torch.cat(tr_2014, tr_2006)
tr_subset = torch.cat(tr_subset, tr_2005)
tr_subset = torch.cat(tr_subset, tr_2003)
tr_subset = torch.cat(tr_subset, tr_2001)
end
local nnz_tr_output = torch.FloatTensor(nnz_tr:size()):zero()
local t = adcensus.subset_dataset(tr_subset, nnz_tr, nnz_tr_output);
nnz_tr = nnz_tr_output[{{1,t}}]
end
collectgarbage()
if opt.a == 'train_all' then
nnz = torch.cat(nnz_tr, nnz_te, 1)
elseif opt.a == 'train_tr' or opt.a == 'time' then
nnz = nnz_tr
end
if opt.a ~= 'time' then
perm = torch.randperm(nnz:size(1))
end
local fm = torch.totable(torch.linspace(opt.fm, opt.fm, opt.l1):int())
-- network for training
if arch == 'slow' then
net_tr = nn.Sequential()
for i = 1,#fm do
net_tr:add(cudnn.SpatialConvolution(i == 1 and n_input_plane or fm[i - 1], fm[i], opt.ks, opt.ks))
net_tr:add(cudnn.ReLU(true))
end
net_tr:add(nn.Reshape(opt.bs, 2 * fm[#fm]))
for i = 1,opt.l2 do
net_tr:add(nn.Linear(i == 1 and 2 * fm[#fm] or opt.nh2, opt.nh2))
net_tr:add(cudnn.ReLU(true))
end
net_tr:add(nn.Linear(opt.nh2, 1))
net_tr:add(cudnn.Sigmoid(false))
net_tr:cuda()
criterion = nn.BCECriterion2():cuda()
-- network for testing (make sure it's synched with net_tr)
local pad = (opt.ks - 1) / 2
net_te = nn.Sequential()
for i = 1,#fm do
net_te:add(cudnn.SpatialConvolution(i == 1 and n_input_plane or fm[i - 1], fm[i], opt.ks, opt.ks, 1, 1, pad, pad))
net_te:add(cudnn.ReLU(true))
end
net_te:cuda()
net_te2 = nn.Sequential()
for i = 1,opt.l2 do
net_te2:add(nn.SpatialConvolution1_fw(i == 1 and 2 * fm[#fm] or opt.nh2, opt.nh2))
net_te2:add(cudnn.ReLU(true))
end
net_te2:add(nn.SpatialConvolution1_fw(opt.nh2, 1))
net_te2:add(cudnn.Sigmoid(true))
net_te2:cuda()
-- tie weights
net_te_all = {}
for i, v in ipairs(net_te.modules) do table.insert(net_te_all, v) end
for i, v in ipairs(net_te2.modules) do table.insert(net_te_all, v) end
local finput = torch.CudaTensor()
local i_tr = 1
local i_te = 1
while i_tr <= net_tr:size() do
local module_tr = net_tr:get(i_tr)
local module_te = net_te_all[i_te]
local skip = {['nn.Reshape']=1, ['nn.Dropout']=1}
while skip[torch.typename(module_tr)] do
i_tr = i_tr + 1
module_tr = net_tr:get(i_tr)
end
if module_tr.weight then
-- print(('tie weights of %s and %s'):format(torch.typename(module_te), torch.typename(module_tr)))
assert(module_te.weight:nElement() == module_tr.weight:nElement())
assert(module_te.bias:nElement() == module_tr.bias:nElement())
module_te.weight = torch.CudaTensor(module_tr.weight:storage(), 1, module_te.weight:size())
module_te.bias = torch.CudaTensor(module_tr.bias:storage(), 1, module_te.bias:size())
end
i_tr = i_tr + 1
i_te = i_te + 1
end
elseif arch == 'fast' then
net_tr = nn.Sequential()
for i = 1,#fm do
net_tr:add(cudnn.SpatialConvolution(i == 1 and n_input_plane or fm[i - 1], fm[i], opt.ks, opt.ks))
if i < #fm then
net_tr:add(cudnn.ReLU(true))
end
end
net_tr:add(nn.Normalize2())
net_tr:add(nn.StereoJoin1())
net_tr:cuda()
net_te = net_tr:clone('weight', 'bias')
net_te.modules[#net_te.modules] = nn.StereoJoin(1):cuda()
for i = 1,#net_te.modules do
local m = net_te:get(i)
if torch.typename(m) == 'cudnn.SpatialConvolution' then
m.padW = 1
m.padH = 1
end
end
criterion = nn.Margin2(opt.m, opt.pow):cuda()
end
print_net(net_tr)
params = {}
grads = {}
momentums = {}
for i = 1,net_tr:size() do
local m = net_tr:get(i)
if m.weight then
m.weight_v = torch.CudaTensor(m.weight:size()):zero()
table.insert(params, m.weight)
table.insert(grads, m.gradWeight)
table.insert(momentums, m.weight_v)
end
if m.bias then
m.bias_v = torch.CudaTensor(m.bias:size()):zero()
table.insert(params, m.bias)
table.insert(grads, m.gradBias)
table.insert(momentums, m.bias_v)
end
end
ws = get_window_size(net_tr)
x_batch_tr = torch.CudaTensor(opt.bs * 2, n_input_plane, ws, ws)
y_batch_tr = torch.CudaTensor(opt.bs)
x_batch_tr_ = torch.FloatTensor(x_batch_tr:size())
y_batch_tr_ = torch.FloatTensor(y_batch_tr:size())
time = sys.clock()
for epoch = 1,14 do
if opt.a == 'time' then
break
end
if epoch == 12 then
opt.lr = opt.lr / 10
end
local err_tr = 0
local err_tr_cnt = 0
for t = 1,nnz:size(1) - opt.bs/2,opt.bs/2 do
for i = 1,opt.bs/2 do
d_pos = torch.uniform(-opt.true1, opt.true1)
d_neg = torch.uniform(opt.false1, opt.false2)
if torch.uniform() < 0.5 then
d_neg = -d_neg
end
assert(opt.hscale <= 1 and opt.scale <= 1)
local s = torch.uniform(opt.scale, 1)
local scale = {s * torch.uniform(opt.hscale, 1), s}
if opt.hflip == 1 and torch.uniform() < 0.5 then
scale[1] = -scale[1]
end
if opt.vflip == 1 and torch.uniform() < 0.5 then
scale[2] = -scale[2]
end
local hshear = torch.uniform(-opt.hshear, opt.hshear)
local trans = {torch.uniform(-opt.trans, opt.trans), torch.uniform(-opt.trans, opt.trans)}
local phi = torch.uniform(-opt.rotate * math.pi / 180, opt.rotate * math.pi / 180)
local brightness = torch.uniform(-opt.brightness, opt.brightness)
assert(opt.contrast >= 1 and opt.d_contrast >= 1)
local contrast = torch.uniform(1 / opt.contrast, opt.contrast)
local scale_ = {scale[1] * torch.uniform(opt.d_hscale, 1), scale[2]}
local hshear_ = hshear + torch.uniform(-opt.d_hshear, opt.d_hshear)
local trans_ = {trans[1], trans[2] + torch.uniform(-opt.d_vtrans, opt.d_vtrans)}
local phi_ = phi + torch.uniform(-opt.d_rotate * math.pi / 180, opt.d_rotate * math.pi / 180)
local brightness_ = brightness + torch.uniform(-opt.d_brightness, opt.d_brightness)
local contrast_ = contrast * torch.uniform(1 / opt.d_contrast, opt.d_contrast)
local ind = perm[t + i - 1]
img = nnz[{ind, 1}]
dim3 = nnz[{ind, 2}]
dim4 = nnz[{ind, 3}]
d = nnz[{ind, 4}]
if dataset == 'kitti' or dataset == 'kitti2015' then
x0 = X0[img]
x1 = X1[img]
elseif dataset == 'mb' then
light = (torch.random() % (#X[img] - 1)) + 2
exp = (torch.random() % X[img][light]:size(1)) + 1
light_ = light
exp_ = exp
if torch.uniform() < opt.d_exp then
exp_ = (torch.random() % X[img][light]:size(1)) + 1
end
if torch.uniform() < opt.d_light then
light_ = math.max(2, light - 1)
end
x0 = X[img][light][{exp,1}]
x1 = X[img][light_][{exp_,2}]
end
make_patch(x0, x_batch_tr_[i * 4 - 3], dim3, dim4, scale, phi, trans, hshear, brightness, contrast)
make_patch(x1, x_batch_tr_[i * 4 - 2], dim3, dim4 - d + d_pos, scale_, phi_, trans_, hshear_, brightness_, contrast_)
make_patch(x0, x_batch_tr_[i * 4 - 1], dim3, dim4, scale, phi, trans, hshear, brightness, contrast)
make_patch(x1, x_batch_tr_[i * 4 - 0], dim3, dim4 - d + d_neg, scale_, phi_, trans_, hshear_, brightness_, contrast_)
y_batch_tr_[i * 2 - 1] = 0
y_batch_tr_[i * 2] = 1
end
x_batch_tr:copy(x_batch_tr_)
y_batch_tr:copy(y_batch_tr_)
for i = 1,#params do
grads[i]:zero()
end
net_tr:forward(x_batch_tr)
local err = criterion:forward(net_tr.output, y_batch_tr)
if err >= 0 and err < 100 then
err_tr = err_tr + err
err_tr_cnt = err_tr_cnt + 1
else
print(('WARNING! err=%f'):format(err))
end
criterion:backward(net_tr.output, y_batch_tr)
net_tr:backward(x_batch_tr, criterion.gradInput)
for i = 1,#params do
momentums[i]:mul(opt.mom):add(-opt.lr, grads[i])
params[i]:add(momentums[i])
end
end
if opt.debug then
save_net(epoch)
end
print(epoch, err_tr / err_tr_cnt, opt.lr, sys.clock() - time)
collectgarbage()
end
opt.net_fname = save_net(0)
if opt.a == 'train_tr' then
opt.a = 'test_te'
elseif opt.a == 'train_all' then
opt.a = 'submit'
end
collectgarbage()
end
if not opt.use_cache then
if arch == 'slow' then
net = torch.load(opt.net_fname, 'ascii')
net_te = net[1]
net_te2 = net[2]
elseif arch == 'fast' then
net_te = torch.load(opt.net_fname, 'ascii')[1]
net_te.modules[#net_te.modules] = nil
net_te2 = nn.StereoJoin(1):cuda()
end
end
x_batch_te1 = torch.CudaTensor()
x_batch_te2 = torch.CudaTensor()
function forward_free(net, input)
local currentOutput = input
for i=1,#net.modules do
net.modules[i].oDesc = nil
local nextOutput = net.modules[i]:updateOutput(currentOutput)
if currentOutput:storage() ~= nextOutput:storage() then
currentOutput:storage():resize(1)
currentOutput:resize(0)
end
currentOutput = nextOutput
end
net.output = currentOutput
return currentOutput
end
function fix_border(net, vol, direction)
local n = (get_window_size(net) - 1) / 2
for i=1,n do
vol[{{},{},{},direction * i}]:copy(vol[{{},{},{},direction * (n + 1)}])
end
end
function stereo_predict(x_batch, id)
local vols, vol
if arch == 'ad' then
vols = torch.CudaTensor(2, disp_max, x_batch:size(3), x_batch:size(4)):fill(0 / 0)
adcensus.ad(x_batch[{{1}}], x_batch[{{2}}], vols[{{1}}], -1)
adcensus.ad(x_batch[{{2}}], x_batch[{{1}}], vols[{{2}}], 1)
end
if arch == 'census' then
vols = torch.CudaTensor(2, disp_max, x_batch:size(3), x_batch:size(4)):fill(0 / 0)
adcensus.census(x_batch[{{1}}], x_batch[{{2}}], vols[{{1}}], -1)
adcensus.census(x_batch[{{2}}], x_batch[{{1}}], vols[{{2}}], 1)
end
if arch == 'fast' then
forward_free(net_te, x_batch:clone())
vols = torch.CudaTensor(2, disp_max, x_batch:size(3), x_batch:size(4)):fill(0 / 0)
adcensus.StereoJoin(net_te.output[{{1}}], net_te.output[{{2}}], vols[{{1}}], vols[{{2}}])
fix_border(net_te, vols[{{1}}], -1)
fix_border(net_te, vols[{{2}}], 1)
clean_net(net_te)
end
disp = {}
local mb_directions = opt.a == 'predict' and {1, -1} or {-1}
for _, direction in ipairs(dataset == 'mb' and mb_directions or {1, -1}) do
sm_active = true
if arch == 'slow' then
if opt.use_cache then
vol = torch.load(('cache/%s_%d.t7'):format(id, direction))
else
local output = forward_free(net_te, x_batch:clone())
clean_net(net_te)
collectgarbage()
vol = torch.CudaTensor(1, disp_max, output:size(3), output:size(4)):fill(0 / 0)
collectgarbage()
for d = 1,disp_max do
local l = output[{{1},{},{},{d,-1}}]
local r = output[{{2},{},{},{1,-d}}]
x_batch_te2:resize(2, r:size(2), r:size(3), r:size(4))
x_batch_te2[1]:copy(l)
x_batch_te2[2]:copy(r)
x_batch_te2:resize(1, 2 * r:size(2), r:size(3), r:size(4))
forward_free(net_te2, x_batch_te2)
vol[{1,d,{},direction == -1 and {d,-1} or {1,-d}}]:copy(net_te2.output[{1,1}])
end
clean_net(net_te2)
fix_border(net_te, vol, direction)
if opt.make_cache then
torch.save(('cache/%s_%d.t7'):format(id, direction), vol)
end
end
collectgarbage()
elseif arch == 'fast' or arch == 'ad' or arch == 'census' then
vol = vols[{{direction == -1 and 1 or 2}}]
end
sm_active = sm_active and (opt.sm_terminate ~= 'cnn')
-- cross computation
local x0c, x1c
if sm_active and opt.sm_skip ~= 'cbca' then
x0c = torch.CudaTensor(1, 4, vol:size(3), vol:size(4))
x1c = torch.CudaTensor(1, 4, vol:size(3), vol:size(4))
adcensus.cross(x_batch[1], x0c, opt.L1, opt.tau1)
adcensus.cross(x_batch[2], x1c, opt.L1, opt.tau1)
local tmp_cbca = torch.CudaTensor(1, disp_max, vol:size(3), vol:size(4))
for i = 1,opt.cbca_i1 do
adcensus.cbca(x0c, x1c, vol, tmp_cbca, direction)
vol:copy(tmp_cbca)