-
Notifications
You must be signed in to change notification settings - Fork 2
/
depth_estimation.lua
405 lines (368 loc) · 12.5 KB
/
depth_estimation.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
require 'torch'
require 'nnx'
require 'image'
require 'optim'
require 'load_data'
require 'groundtruth_discrete'
require 'groundtruth_continuous'
require 'sys'
op = xlua.OptionParser('%prog [options]')
--common
op:option{'-t', '--network-type', action='store', dest='network_type', default='mnist',
help='Network type: mnist | opticalflow'}
op:option{'-n', '--n-train-set', action='store', dest='n_train_set', default=2000,
help='Number of patches in the training set'}
op:option{'-m', '--n-test-set', action='store', dest='n_test_set', default=1000,
help='Number of patches in the test set'}
op:option{'-l', '--load-network', action='store', dest='network', default=nil,
help='Load pre-trained network'}
op:option{'-ni', '--num-input-images', action='store', dest='num_input_images',
default=10, help='Number of annotated images used'}
op:option{'-e', '--num-epochs', action='store', dest='nEpochs', default=10,
help='Number of epochs'}
op:option{'-nt', '--num-threads', action='store', dest='nThreads', default=2,
help='Number of threads used'}
op:option{'-o', '--output_mode', action='store', dest='output_model', default='model',
help='Name of the file to save the trained model'}
op:option{'-d', '--delta', action='store', dest='delta', default=10,
help='Delta between two consecutive frames'}
op:option{'-rd', '--root-directory', action='store', dest='root_directory',
default='./data', help='Root dataset directory'}
--discrete
op:option{'-i', '--input-image', action='store', dest='input_image', default=nil,
help='Run network on image. Must be the number of the image (no .jpg)'}
op:option{'-cd', '--cut-depth', action='store', dest='cut_depth', default=nil,
help='Specify cutDepth manually'}
op:option{'-nc', '--num-classes', action='store', dest='num_classes', default=2,
help='Number of depth classes'}
--continuous
op:option{'-c', '--continuous', action='store_true', dest='continuous', default=false,
help='Continuous output (experimental)'}
op:option{'-mc', '--motion-correction', action='store_true', dest='motion_correction', default=false,
help='Eliminate panning, tilting and rotation camera movements'}
opt=op:parse()
opt.nThreads = tonumber(opt.nThreads)
opt.n_train_set = tonumber(opt.n_train_set)
opt.n_test_set = tonumber(opt.n_test_set)
depthDiscretizer.nClasses = tonumber(opt.num_classes)
torch.manualSeed(1)
if opt.nThreads > 1 then
require 'openmp'
openmp.setDefaultNumThreads(opt.nThreads)
end
geometry = {}
geometry.wImg = 640
geometry.hImg = 360
geometry.wPatch = 32
geometry.hPatch = 32
geometry.nImgsPerSample = 2 --todo
geometry.maxw = 17
geometry.maxh = 17
if not opt.continuous then
classes = {}
for i = 1,depthDiscretizer.nClasses do
table.insert(classes, i)
end
end
if opt.network_type == 'opticalflow' then
classes = {}
for i = 1,geometry.maxw*geometry.maxh do
table.insert(classes, i)
end
end
if not opt.network then
input_dim = 2
if opt.continuous then
output_dim = 1
else
output_dim = #classes
end
model = nn.Sequential()
if opt.network_type == 'opticalflow' then
local hInput = geometry.hPatch
local wInput = geometry.wPatch
local kernelSize = 16
local nChannelsIn = 1
local nFeatures = 10
--model:add(nn.SpatialSubtractiveNormalization(input_dim, image.gaussian1D(15)))
model:add(nn.SplitTable(1))
local parallel = nn.ParallelTable()
local parallelElem1 = nn.Sequential()
local parallelElem2 = nn.Sequential()
local conv = nn.SpatialConvolution(nChannelsIn, nFeatures, kernelSize, kernelSize)
parallelElem1:add(nn.Reshape(nChannelsIn, hInput, wInput))
parallelElem1:add(nn.Narrow(2, math.ceil(geometry.maxh/2), kernelSize))
parallelElem1:add(nn.Narrow(3, math.ceil(geometry.maxw/2), kernelSize))
parallelElem1:add(conv)
parallelElem1:add(nn.Tanh())
parallelElem2:add(nn.Reshape(nChannelsIn, hInput, wInput))
parallelElem2:add(conv)
parallelElem2:add(nn.Tanh())
parallel:add(parallelElem1)
parallel:add(parallelElem2)
model:add(parallel)
model:add(nn.SpatialMatching(geometry.maxh, geometry.maxw, false))
--model:add(nn.Tanh())
model:add(nn.Reshape(hInput-kernelSize-geometry.maxh+2,
wInput-kernelSize-geometry.maxw+2,
geometry.maxw*geometry.maxh))
else
--model:add(nn.SpatialNormalization{nInputPlane=input_dim,
-- kernel=image.gaussian(15)})
model:add(nn.SpatialSubtractiveNormalization(input_dim, image.gaussian1D(15)))
model:add(nn.SpatialConvolution(input_dim, 50, 5, 5))
model:add(nn.Tanh())
model:add(nn.SpatialMaxPooling(2,2,2,2))
model:add(nn.SpatialSubtractiveNormalization(50, image.gaussian1D(15)))
model:add(nn.SpatialConvolutionMap(nn.tables.random(50, 128, 10), 5, 5))
model:add(nn.Tanh())
model:add(nn.SpatialMaxPooling(2, 2, 2, 2))
model:add(nn.SpatialConvolution(128, 200, 5, 5))
model:add(nn.Tanh())
local spatial = nn.SpatialClassifier()
spatial:add(nn.Linear(200,output_dim))
model:add(spatial)
end
else
model = torch.load(opt.network)
end
parameters, gradParameters = model:getParameters()
if opt.continuous then
if opt.network_type == 'opticalflow' then
criterion = nn.ClassNLLCriterion()
else
criterion = nn.MSECriterion()
end
else
criterion = nn.DistNLLCriterion()
criterion.targetIsProbability = true
end
if not opt.network then
loadData(opt.num_input_images, opt.delta, opt.root_directory)
if opt.continuous then
local data = preSortDataContinuous(geometry, raw_data, 26, 20, true);
if data == nil then
sys:exit(0)
end
if opt.network_type == 'opticalflow' then
trainData = generateContinuousDatasetOpticalFlow(geometry, data, opt.n_train_set);
testData = generateContinuousDatasetOpticalFlow(geometry, data, opt.n_test_set);
else
trainData = generateContinuousDataset(geometry, data, opt.n_train_set);
testData = generateContinuousDataset(geometry, data, opt.n_test_set);
end
else
--todo maxDepth depends on the dataset, therefore the classes depend too
preSortDataDiscrete(geometry.hPatch, geometry.wPatch, false, opt.motion_correction)
if opt.cut_depth then
cutDepth=opt.cut_depth
end
trainData = generateDataDiscrete(opt.n_train_set, geometry.hPatch, geometry.wPatch, true, true, opt.motion_correction)
testData = generateDataDiscrete(opt.n_test_set, geometry.hPatch, geometry.wPatch, false, true, opt.motion_correction)
end
else
maxDepth=model.maxDepth
cutDepth=model.cutDepth
end
if not opt.network then
for epoch = 1,opt.nEpochs do
print("Epoch " .. epoch)
xlua.progress(0, trainData:size())
if opt.continuous then
if opt.network_type == 'opticalflow' then
nGood = 0
nBad = 0
else
sumdist = torch.Tensor(output_dim):zero()
nsamples = 0
end
else
confusion = nn.ConfusionMatrix(classes)
end
for t = 1,trainData:size() do
modProgress(t, trainData:size(), 100);
local sample = trainData[t]
local input = sample[1]
local target
if opt.network_type == 'opticalflow' then
target = sample[2][2] * geometry.maxw + sample[2][1] + 1
else
target = sample[2]
end
local feval = function(x)
if x ~= parameters then
parameters:copy(x)
end
gradParameters:zero()
local output
if opt.network_type == 'opticalflow' then
output = model:forward(input)
output = output:squeeze()
output = torch.Tensor(output:size()):fill(1) - output:abs()
else
output = model:forward(input):select(3,1):select(2,1)
end
local err = criterion:forward(output, target)
local df_do = criterion:backward(output, target)
model:backward(input, df_do)
if opt.continuous then
if opt.network_type == 'opticalflow' then
_, ioutput = output:max(1)
ioutput = ioutput[1]
--print(output .. ' ' .. target)
if ioutput == target then
nGood = nGood + 1
else
nBad = nBad + 1
end
else
sumdist = sumdist + torch.abs(output - target)
nsamples = nsamples + 1
end
else
confusion:add(output, target)
end
return err, gradParameters
end
config = {learningRate = 1e-2,
weightDecay = 0,
momentum = 0,
learningRateDecay = 5e-7}
optim.sgd(feval, parameters, config)
end
if opt.continuous then
if opt.network_type == 'opticalflow' then
print('nGood = ' .. nGood .. ' nBad = ' .. nBad)
nGood = 0
nBad = 0
else
print('Mean error: ')
print(sumdist/nsamples)
end
sumdist = torch.Tensor(output_dim):zero()
nsamples = 0
else
print(confusion)
confusion = nn.ConfusionMatrix(classes)
end
for t = 1,testData:size() do
--modProgress(t, testData:size(), 100)
local sample = testData[t]
local input = sample[1]
--input[2] = input[1]:clone()
local target
if opt.network_type == 'opticalflow' then
target = sample[2][2] * geometry.maxw + sample[2][1] + 1
else
target = sample[2]
end
local output
if opt.network_type == 'opticalflow' then
output = model:forward(input):squeeze()
--output = torch.Tensor(output:size()):fill(1) - output:abs()
output = output:abs()
else
output = model:forward(input):select(3,1):select(2,1)
end
if opt.continuous then
if opt.network_type == 'opticalflow' then
_, ioutput = output:min(1)
ioutput = ioutput[1]
--[[
youtput = (ioutput-1)/geometry.maxw
xoutput = math.mod(ioutput-1, geometry.maxw)
if (sample[2][2]-1 <= youtput) and (youtput <= sample[2][2]+1) and
(sample[2][1]-1 <= xoutput) and (xoutput <= sample[2][1]+1) then
--]]
if ioutput == target then
nGood = nGood + 1
else
nBad = nBad + 1
end
else
sumdist = sumdist + torch.abs(output - target)
nsamples = nsamples + 1
end
else
confusion:add(output, target)
end
end
if opt.continuous then
if opt.network_type == 'opticalflow' then
print('nGood = ' .. nGood .. ' nBad = ' .. nBad)
else
print('Mean error:')
print(sumdist/nsamples)
end
else
print(confusion)
end
end
model.maxDepth = maxDepth
model.cutDepth = cutDepth
torch.save(opt.output_model, model)
end
if opt.input_image then --todo this is old code, not sure this works anymore
if opt.continuous then
print('input_image not implemented for continuous output')
sys.exit(0)
end
local directories = {}
local nDirs = 0
local findIn = 'find -L ' .. opt.root_directory .. ' -name images'
for i in io.popen(findIn):lines() do
nDirs = nDirs + 1
directories[nDirs] = string.gsub(i, "images", "")
end
print('Loading image: ' .. directories[1] .. 'images/' .. opt.input_image .. '.jpg')
local im = image.loadJPG(directories[1] .. 'images/' .. opt.input_image .. '.jpg')
local im2 = image.loadJPG(directories[1] .. string.format('images/%09d.jpg',
tonumber(opt.input_image)+opt.delta))
local h_im = im:size(2)
local w_im = im:size(3)
local h = 360
local w = 640
local input = torch.Tensor(2, h, w)
image.scale(image.rgb2y(im)[1], input[1], 'bilinear')
image.scale(image.rgb2y(im2)[1], input[2], 'bilinear')
local gt = torch.DiskFile(directories[1] .. 'depths/' .. opt.input_image .. '.mat')
local nPts = gt:readInt()
local inputdisplay = torch.Tensor(3, h, w)
for i = 1,3 do
inputdisplay[i]:copy(input[1])
end
for i = 1,nPts do
local yo = gt:readInt()
local xo = gt:readInt()
local y = math.floor(yo * h / h_im)+1
local x = math.floor(xo * w / w_im)+1
local depth = gt:readDouble()
inputdisplay[1][y][x] = 0
inputdisplay[2][y][x] = 0
inputdisplay[3][y][x] = 0
--print (depth .. " " .. x .. " " .. y .. " " .. xo .. " " .. yo .. " " .. i .. " " .. getClass(depth))
inputdisplay[1][y][x] = 1-math.min(depth/30., 1.)
inputdisplay[2][y][x] = math.min(depth/30., 1.)
if getClass(depth) == 1 then
inputdisplay[3][y][x] = 1
else
inputdisplay[3][y][x] = 0
end
end
image.display{image=inputdisplay}
local output = model:forward(input)
assert(output:size(1) == 2) --not implemented for nClasses > 2
local houtput = output:size(2)
local woutput = output:size(3)
todisplay = torch.Tensor(houtput, woutput)
for i = 1,houtput do
for j = 1,woutput do
if output[1][i][j] < output[2][i][j] then
todisplay[i][j] = 0
else
todisplay[i][j] = 1
end
end
end
image.display{image=todisplay}
end