-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStereoNet.py
393 lines (303 loc) · 13.5 KB
/
StereoNet.py
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
"""
Classes and functions to instantiate, and train, a StereoNet model (https://arxiv.org/abs/1807.08865).
StereoNet model is decomposed into a feature extractor, cost volume creation, and a cascade of refiner networks.
Loss function is the Robust Loss function (https://arxiv.org/abs/1701.03077)
"""
from typing import Dict, Any
from collections import OrderedDict
import torch
from torch import nn
import torch.nn.functional as F
import pytorch_lightning as pl
class StereoNet(pl.LightningModule):
def __init__(self, k_downsampling_layers = 3,
k_refinement_layers = 3,
candidate_disparities = 256,
feature_extractor_filters = 32,
cost_volumizer_filters = 32,
mask: bool = True):
super().__init__()
self.save_hyperparameters()
self.k_downsampling_layers = k_downsampling_layers
self.k_refinement_layers = k_refinement_layers
self.candidate_disparities = candidate_disparities
self.mask = mask
self.feature_extractor_filters = feature_extractor_filters
self.cost_volumizer_filters = cost_volumizer_filters
self._max_downsampled_disps = \
(candidate_disparities+1) // (2**k_downsampling_layers)
self.feature_extractor = FeatureExtractor(
in_channels=3, out_channels=self.feature_extractor_filters,
k_downsampling_layers=self.k_downsampling_layers
)
self.cost_volumizer = CostVolume(
in_channels=self.feature_extractor_filters,
out_channels=self.cost_volumizer_filters,
max_downsampled_disps=self._max_downsampled_disps
)
self.refiners = nn.ModuleList()
for _ in range(self.k_refinement_layers):
self.refiners.append(Refinement())
def forward_pyramid(self, sample, side = 'left'):
if side == 'left':
reference = sample['left']
shifting = sample['right']
elif side == 'right':
reference = sample['right']
shifting = sample['left']
reference_embedding = self.feature_extractor(reference)
shifting_embedding = self.feature_extractor(shifting)
cost = self.cost_volumizer(
(reference_embedding, shifting_embedding),
side=side
)
disparity_pyramid = [soft_argmin(cost, self.candidate_disparities)]
for idx, refiner in enumerate(self.refiners, start=1):
scale = (2**self.k_refinement_layers) / (2**idx)
new_h = int(reference.size()[2]//scale)
new_w = int(reference.size()[3]//scale)
reference_rescaled = F.interpolate(
reference, [new_h, new_w],
mode='bilinear', align_corners=True
)
disparity_low_rescaled = F.interpolate(
disparity_pyramid[-1], [new_h, new_w],
mode='bilinear', align_corners=True
)
refined_disparity = F.relu(
refiner(
torch.cat((reference_rescaled, disparity_low_rescaled),
dim=1)) + disparity_low_rescaled
)
disparity_pyramid.append(refined_disparity)
return disparity_pyramid
def forward(self, sample):
disparities = self.forward_pyramid(sample, side='left')
return disparities[-1]
def training_step(self, batch, _):
left = batch['left']
right = batch['right']
disp_gt_left = batch['disp_left']
disp_gt_right = batch['disp_right']
sample = {'left': left, 'right': right}
disp_pred_left_nonuniform = self.forward_pyramid(sample, side='left')
disp_pred_right_nonuniform = self.forward_pyramid(sample, side='right')
for idx, (disparity_left, disparity_right) in \
enumerate(zip(
disp_pred_left_nonuniform,
disp_pred_right_nonuniform
)):
disp_pred_left_nonuniform[idx] = F.interpolate(
disparity_left, [left.size()[2], left.size()[3]],
mode='bilinear', align_corners=True
)
disp_pred_right_nonuniform[idx] = F.interpolate(
disparity_right, [left.size()[2], left.size()[3]],
mode='bilinear', align_corners=True
)
disp_pred_left = torch.stack(disp_pred_left_nonuniform, dim=0)
disp_pred_right = torch.stack(disp_pred_right_nonuniform, dim=0)
def _tiler(tensor, matching_size = None):
if matching_size is None:
matching_size = [disp_pred_left.size()[0], 1, 1, 1, 1]
return tensor.tile(matching_size)
disp_gt_left = _tiler(disp_gt_left)
disp_gt_right = _tiler(disp_gt_right)
if self.mask:
left_mask = (disp_gt_left < self.candidate_disparities).detach()
right_mask = (disp_gt_right < self.candidate_disparities).detach()
loss_left = torch.mean(robust_loss(
disp_gt_left[left_mask] - disp_pred_left[left_mask],
alpha=1, c=2
))
loss_right = torch.mean(robust_loss(
disp_gt_right[right_mask] - disp_pred_right[right_mask],
alpha=1, c=2
))
else:
loss_left = torch.mean(robust_loss(
disp_gt_left - disp_pred_left,
alpha=1, c=2
))
loss_right = torch.mean(robust_loss(
disp_gt_right - disp_pred_right,
alpha=1, c=2
))
loss = (loss_left + loss_right) / 2
self.log("train_loss_step", loss, on_step=True,
on_epoch=False, prog_bar=True, logger=True
)
self.log("train_loss_epoch",
F.l1_loss(disp_pred_left[-1], disp_gt_left[-1]),
on_step=False, on_epoch=True,
prog_bar=False, logger=True
)
return loss
def validation_step(self, batch, batch_idx):
left = batch['left']
right = batch['right']
disp_gt = batch['disp_left']
sample = {'left': left, 'right': right}
disp_pred = self(sample)
loss = F.l1_loss(disp_pred, disp_gt)
self.log("val_loss_epoch", loss, on_epoch=True, logger=True)
def configure_optimizers(self) -> Dict[str, Any]:
optimizer = torch.optim.RMSprop(
self.parameters(), lr=1e-3, weight_decay=0.0001
)
lr_dict = {
"scheduler": torch.optim.lr_scheduler.ExponentialLR(
optimizer, gamma=0.9, last_epoch=-1
),
"interval": "epoch",
"frequency": 1,
"name": "ExponentialDecayLR"
}
config = {"optimizer": optimizer, "lr_scheduler": lr_dict}
return config
class FeatureExtractor(torch.nn.Module):
def __init__(self, in_channels, out_channels, k_downsampling_layers):
super().__init__()
self.k = k_downsampling_layers
net = OrderedDict()
for block_idx in range(self.k):
net[f'segment_0_conv_{block_idx}'] = nn.Conv2d(
in_channels=in_channels, out_channels=out_channels,
kernel_size=5, stride=2, padding=2
)
in_channels = out_channels
for block_idx in range(6):
net[f'segment_1_res_{block_idx}'] = ResBlock(
in_channels=out_channels,
out_channels=out_channels,
kernel_size=3, padding=1
)
net['segment_2_conv_0'] = nn.Conv2d(
in_channels=32, out_channels=32,
kernel_size=3, padding=1
)
self.net = nn.Sequential(net)
def forward(self, x: torch.Tensor) -> torch.Tensor:
x = self.net(x)
return x
class CostVolume(torch.nn.Module):
def __init__(self, in_channels: int, out_channels: int, max_downsampled_disps: int):
super().__init__()
self.in_channels = in_channels
self.out_channels = out_channels
self._max_downsampled_disps = max_downsampled_disps
net: OrderedDict[str, nn.Module] = OrderedDict()
for block_idx in range(4):
net[f'segment_0_conv_{block_idx}'] = nn.Conv3d(
in_channels=in_channels, out_channels=out_channels,
kernel_size=3, padding=1
)
net[f'segment_0_bn_{block_idx}'] = nn.BatchNorm3d(
num_features=out_channels
)
net[f'segment_0_act_{block_idx}'] = nn.LeakyReLU(
negative_slope=0.2
)
in_channels = out_channels
net['segment_1_conv_0'] = nn.Conv3d(
in_channels=out_channels, out_channels=1,
kernel_size=3, padding=1
)
self.net = nn.Sequential(net)
def forward(self, x, side = 'left'):
reference_embedding, target_embedding = x
cost = compute_volume(
reference_embedding, target_embedding,
max_downsampled_disps=self._max_downsampled_disps, side=side
)
cost = self.net(cost)
cost = torch.squeeze(cost, dim=1)
return cost
def compute_volume(reference_embedding: torch.Tensor, target_embedding: torch.Tensor, max_downsampled_disps: int, side: str = 'left') -> torch.Tensor:
"""
Refer to the doc string in CostVolume.forward.
Refer to https://github.com/meteorshowers/X-StereoLab/blob/9ae8c1413307e7df91b14a7f31e8a95f9e5754f9/disparity/models/stereonet_disp.py
This difference based cost volume is also reflected in an implementation of the popular DispNetCorr:
Line 81 https://github.com/wyf2017/DSMnet/blob/b61652dfb3ee84b996f0ad4055eaf527dc6b965f/models/util_conv.py
"""
batch, channel, height, width = reference_embedding.size()
cost = torch.Tensor(batch, channel, max_downsampled_disps, height, width).zero_()
cost = cost.type_as(reference_embedding) # PyTorch Lightning handles the devices
cost[:, :, 0, :, :] = reference_embedding - target_embedding
for idx in range(1, max_downsampled_disps):
if side == 'left':
cost[:, :, idx, :, idx:] = reference_embedding[:, :, :, idx:] - target_embedding[:, :, :, :-idx]
if side == 'right':
cost[:, :, idx, :, :-idx] = reference_embedding[:, :, :, :-idx] - target_embedding[:, :, :, idx:]
cost = cost.contiguous()
return cost
class Refinement(torch.nn.Module):
def __init__(self) -> None:
super().__init__()
dilations = [1, 2, 4, 8, 1, 1]
net: OrderedDict[str, nn.Module] = OrderedDict()
net['segment_0_conv_0'] = nn.Conv2d(
in_channels=4, out_channels=32,
kernel_size=3, padding=1
)
for block_idx, dilation in enumerate(dilations):
net[f'segment_1_res_{block_idx}'] = ResBlock(
in_channels=32, out_channels=32,
kernel_size=3, padding=dilation, dilation=dilation
)
net['segment_2_conv_0'] = nn.Conv2d(
in_channels=32, out_channels=1,
kernel_size=3, padding=1
)
self.net = nn.Sequential(net)
def forward(self, x: torch.Tensor) -> torch.Tensor:
x = self.net(x)
return x
class ResBlock(torch.nn.Module):
def __init__(self,
in_channels: int,
out_channels: int,
kernel_size: int,
stride: int = 1,
padding: int = 0,
dilation: int = 1):
super().__init__()
self.conv_1 = nn.Conv2d(
in_channels=in_channels, out_channels=out_channels,
kernel_size=kernel_size, stride=stride,
padding=padding, dilation=dilation, bias=False
)
self.batch_norm_1 = nn.BatchNorm2d(num_features=out_channels)
self.activation_1 = nn.LeakyReLU(negative_slope=0.2)
self.conv_2 = nn.Conv2d(
in_channels=out_channels, out_channels=out_channels,
kernel_size=kernel_size, stride=stride,
padding=padding, dilation=dilation, bias=False
)
self.batch_norm_2 = nn.BatchNorm2d(num_features=out_channels)
self.activation_2 = nn.LeakyReLU(negative_slope=0.2)
def forward(self, x: torch.Tensor) -> torch.Tensor:
res = self.conv_1(x)
res = self.batch_norm_1(res)
res = self.activation_1(res)
res = self.conv_2(res)
res = self.batch_norm_2(res)
out = res + x
out = self.activation_2(out)
return out
def soft_argmin(cost: torch.Tensor, max_downsampled_disps: int) -> torch.Tensor:
"""
Soft argmin function described in the original paper. The disparity grid creates the first 'd' value in equation 2 while
cost is the C_i(d) term. The exp/sum(exp) == softmax function.
"""
disparity_softmax = F.softmax(-cost, dim=1)
disparity_grid = torch.linspace(0, max_downsampled_disps, disparity_softmax.size(1)).reshape(1, -1, 1, 1)
disparity_grid = disparity_grid.type_as(disparity_softmax)
disp = torch.sum(disparity_softmax * disparity_grid, dim=1, keepdim=True)
return disp
def robust_loss(x: torch.Tensor, alpha, c) -> torch.Tensor: # pylint: disable=invalid-name
"""
A General and Adaptive Robust Loss Function (https://arxiv.org/abs/1701.03077)
"""
f: torch.Tensor = (abs(alpha - 2) / alpha) * (torch.pow(torch.pow(x / c, 2)/abs(alpha - 2) + 1, alpha/2) - 1) # pylint: disable=invalid-name
return f