-
Notifications
You must be signed in to change notification settings - Fork 232
/
preprocess_kitti.lua
executable file
·145 lines (123 loc) · 4.37 KB
/
preprocess_kitti.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
#!/usr/bin/env luajit
require 'image'
require 'nn'
require 'cutorch'
require 'libadcensus'
for _, dataset in ipairs({2012, 2015}) do
print(('dataset %d'):format(dataset))
torch.manualSeed(42)
if dataset == 2012 then
n_tr = 194
n_te = 195
path = 'data.kitti'
image_0 = 'image_0'
image_1 = 'image_1'
disp_noc = 'disp_noc'
nchannel = 1
elseif dataset == 2015 then
n_tr = 200
n_te = 200
path = 'data.kitti2015'
image_0 = 'image_2'
image_1 = 'image_3'
nchannel = 3
disp_noc = 'disp_noc_0'
end
height = 350
width = 1242
x0 = torch.FloatTensor(n_tr + n_te, 1, height, width):zero()
x1 = torch.FloatTensor(n_tr + n_te, 1, height, width):zero()
dispnoc = torch.FloatTensor(n_tr, 1, height, width):zero()
metadata = torch.IntTensor(n_tr + n_te, 3):zero()
examples = {}
for i = 1,n_tr do
examples[#examples + 1] = {dir='training', cnt=i}
end
for i = 1,n_te do
examples[#examples + 1] = {dir='testing', cnt=i}
end
for i, arg in ipairs(examples) do
img_path = '%s/unzip/%s/%s/%06d_10.png'
img_0 = image.loadPNG(img_path:format(path, arg['dir'], image_0, arg['cnt'] - 1), nchannel, 'byte'):float()
img_1 = image.loadPNG(img_path:format(path, arg['dir'], image_1, arg['cnt'] - 1), nchannel, 'byte'):float()
if dataset == 2015 then
img_0 = image.rgb2y(img_0)
img_1 = image.rgb2y(img_1)
end
-- crop
img_height = img_0:size(2)
img_width = img_0:size(3)
img_0 = img_0:narrow(2, img_height - height + 1, height)
img_1 = img_1:narrow(2, img_height - height + 1, height)
-- preprocess
print(i)
img_0:add(-img_0:mean()):div(img_0:std())
img_1:add(-img_1:mean()):div(img_1:std())
x0[{i,{},{},{1,img_width}}]:copy(img_0)
x1[{i,{},{},{1,img_width}}]:copy(img_1)
if arg['dir'] == 'training' then
img_disp = torch.FloatTensor(1, img_height, img_width)
adcensus.readPNG16(img_disp, ('%s/unzip/training/%s/%06d_10.png'):format(path, disp_noc, arg['cnt'] - 1))
dispnoc[{i, 1}]:narrow(2, 1, img_width):copy(img_disp:narrow(2, img_height - height + 1, height))
end
metadata[{i, 1}] = img_height
metadata[{i, 2}] = img_width
metadata[{i, 3}] = arg['cnt'] - 1
collectgarbage()
end
-- split train and test
perm = torch.randperm(n_tr):long()
te = perm[{{1,40}}]:clone()
tr = perm[{{41,n_tr}}]:clone()
-- prepare tr dataset
nnz_tr = torch.FloatTensor(23e6, 4)
nnz_te = torch.FloatTensor(23e6, 4)
nnz_tr_t = 0
nnz_te_t = 0
for i = 1,n_tr do
local disp = dispnoc[{{i}}]:cuda()
adcensus.remove_nonvisible(disp)
adcensus.remove_occluded(disp)
adcensus.remove_white(x0[{{i}}]:cuda(), disp)
disp = disp:float()
is_te = false
for j = 1,te:nElement() do
if i == te[j] then
is_te = true
end
end
if is_te then
nnz_te_t = adcensus.make_dataset2(disp, nnz_te, i, nnz_te_t)
else
nnz_tr_t = adcensus.make_dataset2(disp, nnz_tr, i, nnz_tr_t)
end
end
nnz_tr = torch.FloatTensor(nnz_tr_t, 4):copy(nnz_tr[{{1,nnz_tr_t}}])
nnz_te = torch.FloatTensor(nnz_te_t, 4):copy(nnz_te[{{1,nnz_te_t}}])
function tofile(fname, x)
tfile = torch.DiskFile(fname .. '.type', 'w')
if x:type() == 'torch.FloatTensor' then
tfile:writeString('float32')
torch.DiskFile(fname, 'w'):binary():writeFloat(x:storage())
elseif x:type() == 'torch.LongTensor' then
tfile:writeString('int64')
torch.DiskFile(fname, 'w'):binary():writeLong(x:storage())
elseif x:type() == 'torch.IntTensor' then
tfile:writeString('int32')
torch.DiskFile(fname, 'w'):binary():writeInt(x:storage())
end
dimfile = torch.DiskFile(fname .. '.dim', 'w')
for i = 1,x:dim() do
dimfile:writeString(('%d\n'):format(x:size(i)))
end
end
os.execute(('rm -f %s/*.{bin,dim,type}'):format(path))
tofile(('%s/x0.bin'):format(path), x0)
tofile(('%s/x1.bin'):format(path), x1)
tofile(('%s/dispnoc.bin'):format(path), dispnoc)
tofile(('%s/metadata.bin'):format(path), metadata)
tofile(('%s/tr.bin'):format(path), tr)
tofile(('%s/te.bin'):format(path), te)
tofile(('%s/nnz_tr.bin'):format(path), nnz_tr)
tofile(('%s/nnz_te.bin'):format(path), nnz_te)
end