-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmodule.py
504 lines (399 loc) · 20.8 KB
/
module.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
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
import torch
import torch.nn as nn
import torch.nn.functional as F
def init_bn(module):
if module.weight is not None:
nn.init.ones_(module.weight)
if module.bias is not None:
nn.init.zeros_(module.bias)
return
def init_uniform(module, init_method):
if module.weight is not None:
if init_method == "kaiming":
nn.init.kaiming_uniform_(module.weight)
elif init_method == "xavier":
nn.init.xavier_uniform_(module.weight)
return
class Conv2d(nn.Module):
"""Applies a 2D convolution (optionally with batch normalization and relu activation)
over an input signal composed of several input planes.
Attributes:
conv (nn.Module): convolution module
bn (nn.Module): batch normalization module
relu (bool): whether to activate by relu
Notes:
Default momentum for batch normalization is set to be 0.01,
"""
def __init__(self, in_channels, out_channels, kernel_size, stride=1,
relu=True, bn=True, bn_momentum=0.1, init_method="xavier", **kwargs):
super(Conv2d, self).__init__()
self.conv = nn.Conv2d(in_channels, out_channels, kernel_size, stride=stride,
bias=(not bn), **kwargs)
self.kernel_size = kernel_size
self.stride = stride
self.bn = nn.BatchNorm2d(out_channels, momentum=bn_momentum) if bn else None
self.relu = relu
def forward(self, x):
x = self.conv(x)
if self.bn is not None:
x = self.bn(x)
if self.relu:
x = F.relu(x, inplace=True)
return x
def init_weights(self, init_method):
"""default initialization"""
init_uniform(self.conv, init_method)
if self.bn is not None:
init_bn(self.bn)
class Deconv2d(nn.Module):
"""Applies a 2D deconvolution (optionally with batch normalization and relu activation)
over an input signal composed of several input planes.
Attributes:
conv (nn.Module): convolution module
bn (nn.Module): batch normalization module
relu (bool): whether to activate by relu
Notes:
Default momentum for batch normalization is set to be 0.01,
"""
def __init__(self, in_channels, out_channels, kernel_size, stride=1,
relu=True, bn=True, bn_momentum=0.1, init_method="xavier", **kwargs):
super(Deconv2d, self).__init__()
self.out_channels = out_channels
assert stride in [1, 2]
self.stride = stride
self.conv = nn.ConvTranspose2d(in_channels, out_channels, kernel_size, stride=stride,
bias=(not bn), **kwargs)
self.bn = nn.BatchNorm2d(out_channels, momentum=bn_momentum) if bn else None
self.relu = relu
def forward(self, x):
y = self.conv(x)
if self.stride == 2:
h, w = list(x.size())[2:]
y = y[:, :, :2 * h, :2 * w].contiguous()
if self.bn is not None:
x = self.bn(y)
if self.relu:
x = F.relu(x, inplace=True)
return x
def init_weights(self, init_method):
"""default initialization"""
init_uniform(self.conv, init_method)
if self.bn is not None:
init_bn(self.bn)
class Conv3d(nn.Module):
"""Applies a 3D convolution (optionally with batch normalization and relu activation)
over an input signal composed of several input planes.
Attributes:
conv (nn.Module): convolution module
bn (nn.Module): batch normalization module
relu (bool): whether to activate by relu
Notes:
Default momentum for batch normalization is set to be 0.01,
"""
def __init__(self, in_channels, out_channels, kernel_size=3, stride=1,
relu=True, bn=True, bn_momentum=0.1, init_method="xavier", **kwargs):
super(Conv3d, self).__init__()
self.out_channels = out_channels
self.kernel_size = kernel_size
assert stride in [1, 2]
self.stride = stride
self.conv = nn.Conv3d(in_channels, out_channels, kernel_size, stride=stride,
bias=(not bn), **kwargs)
self.bn = nn.BatchNorm3d(out_channels, momentum=bn_momentum) if bn else None
self.relu = relu
def forward(self, x):
x = self.conv(x)
if self.bn is not None:
x = self.bn(x)
if self.relu:
x = F.relu(x, inplace=True)
return x
def init_weights(self, init_method):
"""default initialization"""
init_uniform(self.conv, init_method)
if self.bn is not None:
init_bn(self.bn)
class Deconv3d(nn.Module):
"""Applies a 3D deconvolution (optionally with batch normalization and relu activation)
over an input signal composed of several input planes.
Attributes:
conv (nn.Module): convolution module
bn (nn.Module): batch normalization module
relu (bool): whether to activate by relu
Notes:
Default momentum for batch normalization is set to be 0.01,
"""
def __init__(self, in_channels, out_channels, kernel_size=3, stride=1,
relu=True, bn=True, bn_momentum=0.1, init_method="xavier", **kwargs):
super(Deconv3d, self).__init__()
self.out_channels = out_channels
#assert stride in [1, 2]
self.stride = stride
self.conv = nn.ConvTranspose3d(in_channels, out_channels, kernel_size, stride=stride,
bias=(not bn), **kwargs)
self.bn = nn.BatchNorm3d(out_channels, momentum=bn_momentum) if bn else None
self.relu = relu
# assert init_method in ["kaiming", "xavier"]
# self.init_weights(init_method)
def forward(self, x):
y = self.conv(x)
if self.bn is not None:
x = self.bn(y)
if self.relu:
x = F.relu(x, inplace=True)
return x
def init_weights(self, init_method):
"""default initialization"""
init_uniform(self.conv, init_method)
if self.bn is not None:
init_bn(self.bn)
class ConvBnReLU(nn.Module):
def __init__(self, in_channels, out_channels, kernel_size=3, stride=1, pad=1):
super(ConvBnReLU, self).__init__()
self.conv = nn.Conv2d(in_channels, out_channels, kernel_size, stride=stride, padding=pad, bias=False)
self.bn = nn.BatchNorm2d(out_channels)
def forward(self, x):
return F.relu(self.bn(self.conv(x)), inplace=True)
class ConvBn(nn.Module):
def __init__(self, in_channels, out_channels, kernel_size=3, stride=1, pad=1):
super(ConvBn, self).__init__()
self.conv = nn.Conv2d(in_channels, out_channels, kernel_size, stride=stride, padding=pad, bias=False)
self.bn = nn.BatchNorm2d(out_channels)
def forward(self, x):
return self.bn(self.conv(x))
class ConvBnReLU3D(nn.Module):
def __init__(self, in_channels, out_channels, kernel_size=3, stride=1, pad=1):
super(ConvBnReLU3D, self).__init__()
self.conv = nn.Conv3d(in_channels, out_channels, kernel_size, stride=stride, padding=pad, bias=False)
self.bn = nn.BatchNorm3d(out_channels)
def forward(self, x):
return F.relu(self.bn(self.conv(x)), inplace=True)
class ConvBn3D(nn.Module):
def __init__(self, in_channels, out_channels, kernel_size=3, stride=1, pad=1):
super(ConvBn3D, self).__init__()
self.conv = nn.Conv3d(in_channels, out_channels, kernel_size, stride=stride, padding=pad, bias=False)
self.bn = nn.BatchNorm3d(out_channels)
def forward(self, x):
return self.bn(self.conv(x))
def homo_warping(src_fea, src_proj, ref_proj, depth_values):
# src_fea: [B, C, H, W]
# src_proj: [B, 4, 4]
# ref_proj: [B, 4, 4]
# depth_values: [B, Ndepth] or [B, Ndepth, H, W]
# out: [B, C, Ndepth, H, W]
batch, channels = src_fea.shape[0], src_fea.shape[1]
num_depth = depth_values.shape[1]
height, width = src_fea.shape[2], src_fea.shape[3]
with torch.no_grad():
proj = torch.matmul(src_proj, torch.inverse(ref_proj))
rot = proj[:, :3, :3] # [B,3,3]
trans = proj[:, :3, 3:4] # [B,3,1]
y, x = torch.meshgrid([torch.arange(0, height, dtype=torch.float32, device=src_fea.device),
torch.arange(0, width, dtype=torch.float32, device=src_fea.device)])
y, x = y.contiguous(), x.contiguous()
y, x = y.view(height * width), x.view(height * width)
xyz = torch.stack((x, y, torch.ones_like(x))) # [3, H*W]
xyz = torch.unsqueeze(xyz, 0).repeat(batch, 1, 1) # [B, 3, H*W]
rot_xyz = torch.matmul(rot, xyz) # [B, 3, H*W]
rot_depth_xyz = rot_xyz.unsqueeze(2).repeat(1, 1, num_depth, 1) * depth_values.view(batch, 1, num_depth,
-1) # [B, 3, Ndepth, H*W]
proj_xyz = rot_depth_xyz + trans.view(batch, 3, 1, 1) # [B, 3, Ndepth, H*W]
invalid = (proj_xyz[:, 2:3, :, :]<1e-6).squeeze(1) # [B, Ndepth, H*W]
proj_xy = proj_xyz[:, :2, :, :] / (proj_xyz[:, 2:3, :, :]) # [B, 2, Ndepth, H*W]
proj_x_normalized = proj_xy[:, 0, :, :] / ((width - 1) / 2) - 1
proj_x_normalized[invalid] = -99.
proj_y_normalized = proj_xy[:, 1, :, :] / ((height - 1) / 2) - 1
proj_y_normalized[invalid] = -99.
proj_xy = torch.stack((proj_x_normalized, proj_y_normalized), dim=3) # [B, Ndepth, H*W, 2]
grid = proj_xy
warped_src_fea = F.grid_sample(src_fea, grid.view(batch, num_depth * height, width, 2), mode='bilinear',
padding_mode='zeros', align_corners=True)
warped_src_fea = warped_src_fea.view(batch, channels, num_depth, height, width)
return warped_src_fea
class FeatureNet(nn.Module):
def __init__(self, base_channels):
super(FeatureNet, self).__init__()
self.base_channels = base_channels
self.conv0 = nn.Sequential(
Conv2d(3, base_channels, 3, 1, padding=1),
Conv2d(base_channels, base_channels, 3, 1, padding=1))
self.conv1 = nn.Sequential(
Conv2d(base_channels, base_channels * 2, 5, stride=2, padding=2),
Conv2d(base_channels * 2, base_channels * 2, 3, 1, padding=1),
Conv2d(base_channels * 2, base_channels * 2, 3, 1, padding=1))
self.conv2 = nn.Sequential(
Conv2d(base_channels * 2, base_channels * 4, 5, stride=2, padding=2),
Conv2d(base_channels * 4, base_channels * 4, 3, 1, padding=1),
Conv2d(base_channels * 4, base_channels * 4, 3, 1, padding=1))
final_chs = base_channels * 4
self.inner1 = nn.Conv2d(base_channels * 2, final_chs, 1, bias=True)
self.inner2 = nn.Conv2d(base_channels * 1, final_chs, 1, bias=True)
'''
self.out1 = nn.Sequential(
Conv2d(base_channels * 4, base_channels * 4, 1),
DCN(in_channels=base_channels * 4, out_channels=base_channels * 4, kernel_size=3, stride=1, padding=1),
nn.BatchNorm2d(base_channels * 4),
nn.ReLU(inplace=True),
DCN(in_channels=base_channels * 4, out_channels=base_channels * 4, kernel_size=3,stride=1, padding=1),
nn.BatchNorm2d(base_channels * 4),
nn.ReLU(inplace=True),
DCN(in_channels=base_channels * 4, out_channels=base_channels * 4, kernel_size=3,stride=1, padding=1))
self.out2 = nn.Sequential(
Conv2d(final_chs, final_chs, 3,1,padding=1),
DCN(in_channels=final_chs, out_channels=final_chs,kernel_size=3, stride=1, padding=1),
nn.BatchNorm2d(final_chs),
nn.ReLU(inplace=True),
DCN(in_channels=final_chs, out_channels=final_chs,kernel_size=3, stride=1, padding=1),
nn.BatchNorm2d(final_chs),
nn.ReLU(inplace=True),
DCN(in_channels=final_chs, out_channels=base_channels * 2,kernel_size=3, stride=1, padding=1),
)
self.out3 = nn.Sequential(
Conv2d(final_chs, final_chs, 3, 1, padding=1),
DCN(in_channels=final_chs, out_channels=final_chs, kernel_size=3, stride=1,padding=1),
nn.BatchNorm2d(final_chs),
nn.ReLU(inplace=True),
DCN(in_channels=final_chs, out_channels=final_chs, kernel_size=3, stride=1,padding=1),
nn.BatchNorm2d(final_chs),
nn.ReLU(inplace=True),
DCN(in_channels=final_chs, out_channels=base_channels, kernel_size=3,stride=1, padding=1))
'''
self.out1 = nn.Conv2d(final_chs, base_channels * 4, 1, bias=False)
self.out2 = nn.Conv2d(final_chs, base_channels * 2, 3, padding=1, bias=False)
self.out3 = nn.Conv2d(final_chs, base_channels, 3, padding=1, bias=False)
self.out_channels = [4 * base_channels, base_channels * 2, base_channels]
def forward(self, x):
"""forward.
:param x: [B, C, H, W]
:return outputs: stage1 [B, 32, 128, 160], stage2 [B, 16, 256, 320], stage3 [B, 8, 512, 640]
"""
conv0 = self.conv0(x)
conv1 = self.conv1(conv0)
conv2 = self.conv2(conv1)
intra_feat = conv2
outputs = {}
out = self.out1(intra_feat)
outputs["stage1"] = out
intra_feat = F.interpolate(intra_feat, scale_factor=2, mode="nearest") + self.inner1(conv1)
out = self.out2(intra_feat)
outputs["stage2"] = out
intra_feat = F.interpolate(intra_feat, scale_factor=2, mode="nearest") + self.inner2(conv0)
out = self.out3(intra_feat)
outputs["stage3"] = out
return outputs
def depth_wta(p, depth_values):
'''Winner take all.'''
wta_index_map = torch.argmax(p, dim=1, keepdim=True).type(torch.long)
wta_depth_map = torch.gather(depth_values, 1, wta_index_map).squeeze(1)
return wta_depth_map
''' loss function same as TransMVSNet'''
def info_entropy_loss(prob_volume, prob_volume_pre, mask):
# prob_colume should be processed after SoftMax
B,D,H,W = prob_volume.shape
LSM = nn.LogSoftmax(dim=1)
valid_points = torch.sum(mask, dim=[1,2])+1e-6
entropy = -1*(torch.sum(torch.mul(prob_volume, LSM(prob_volume_pre)), dim=1)).squeeze(1)
entropy_masked = torch.sum(torch.mul(mask, entropy), dim=[1,2])
return torch.mean(entropy_masked / valid_points)
def entropy_loss(prob_volume, depth_gt, mask, depth_value, return_prob_map=False):
# from AA
mask_true = mask
valid_pixel_num = torch.sum(mask_true, dim=[1,2]) + 1e-6
shape = depth_gt.shape # B,H,W
depth_num = depth_value.shape[1]
if len(depth_value.shape) < 3:
depth_value_mat = depth_value.repeat(shape[1], shape[2], 1, 1).permute(2,3,0,1) # B,N,H,W
else:
depth_value_mat = depth_value
gt_index_image = torch.argmin(torch.abs(depth_value_mat-depth_gt.unsqueeze(1)), dim=1)
gt_index_image = torch.mul(mask_true, gt_index_image.type(torch.float))
gt_index_image = torch.round(gt_index_image).type(torch.long).unsqueeze(1) # B, 1, H, W
# gt index map -> gt one hot volume (B x 1 x H x W )
gt_index_volume = torch.zeros(shape[0], depth_num, shape[1], shape[2]).type(mask_true.type()).scatter_(1, gt_index_image, 1)
# cross entropy image (B x D X H x W)
cross_entropy_image = -torch.sum(gt_index_volume * torch.log(prob_volume + 1e-6), dim=1).squeeze(1) # B, 1, H, W
# masked cross entropy loss
masked_cross_entropy_image = torch.mul(mask_true, cross_entropy_image) # valid pixel
masked_cross_entropy = torch.sum(masked_cross_entropy_image, dim=[1, 2])
masked_cross_entropy = torch.mean(masked_cross_entropy / valid_pixel_num) # Origin use sum : aggregate with batch
# winner-take-all depth map
wta_index_map = torch.argmax(prob_volume, dim=1, keepdim=True).type(torch.long)
wta_depth_map = torch.gather(depth_value_mat, 1, wta_index_map).squeeze(1)
if return_prob_map:
photometric_confidence = torch.max(prob_volume, dim=1)[0] # output shape dimension B * H * W
return masked_cross_entropy, wta_depth_map, photometric_confidence
return masked_cross_entropy, wta_depth_map
def trans_mvsnet_loss(inputs, depth_gt_ms, mask_ms, **kwargs):
depth_loss_weights = kwargs.get("dlossw", None)
total_loss = torch.tensor(0.0, dtype=torch.float32, device=mask_ms["stage1"].device, requires_grad=False)
total_entropy = torch.tensor(0.0, dtype=torch.float32, device=mask_ms["stage1"].device, requires_grad=False)
for (stage_inputs, stage_key) in [(inputs[k], k) for k in inputs.keys() if "stage" in k]:
prob_volume = stage_inputs["prob_volume"]
depth_values = stage_inputs["depth_values"]
depth_gt = depth_gt_ms[stage_key]
mask = mask_ms[stage_key]
mask = mask > 0.5
entropy_weight = 2.0
entro_loss, depth_entropy = entropy_loss(prob_volume, depth_gt, mask, depth_values)
entro_loss = entro_loss * entropy_weight
depth_loss = F.smooth_l1_loss(depth_entropy[mask], depth_gt[mask], reduction='mean')
total_entropy += entro_loss
if depth_loss_weights is not None:
stage_idx = int(stage_key.replace("stage", "")) - 1
total_loss += depth_loss_weights[stage_idx] * entro_loss
else:
total_loss += entro_loss
return total_loss, depth_loss, total_entropy, depth_entropy
def focal_loss_bld(inputs, depth_gt_ms, mask_ms, depth_interval, **kwargs):
depth_loss_weights = kwargs.get("dlossw", None)
total_loss = torch.tensor(0.0, dtype=torch.float32, device=mask_ms["stage1"].device, requires_grad=False)
total_entropy = torch.tensor(0.0, dtype=torch.float32, device=mask_ms["stage1"].device, requires_grad=False)
for (stage_inputs, stage_key) in [(inputs[k], k) for k in inputs.keys() if "stage" in k]:
prob_volume = stage_inputs["prob_volume"]
depth_values = stage_inputs["depth_values"]
depth_gt = depth_gt_ms[stage_key]
mask = mask_ms[stage_key]
mask = mask > 0.5
entropy_weight = 2.0
entro_loss, depth_entropy = entropy_loss(prob_volume, depth_gt, mask, depth_values)
entro_loss = entro_loss * entropy_weight
depth_loss = F.smooth_l1_loss(depth_entropy[mask], depth_gt[mask], reduction='mean')
total_entropy += entro_loss
if depth_loss_weights is not None:
stage_idx = int(stage_key.replace("stage", "")) - 1
total_loss += depth_loss_weights[stage_idx] * entro_loss
else:
total_loss += entro_loss
abs_err = (depth_gt_ms['stage3'] - inputs["stage3"]["depth"]).abs()
abs_err_scaled = abs_err /(depth_interval *192./128.)
mask = mask_ms["stage3"]
mask = mask > 0.5
epe = abs_err_scaled[mask].mean()
less1 = (abs_err_scaled[mask] < 1.).to(depth_gt_ms['stage3'].dtype).mean()
less3 = (abs_err_scaled[mask] < 3.).to(depth_gt_ms['stage3'].dtype).mean()
return total_loss, depth_loss, epe, less1, less3
def get_cur_depth_range_samples(cur_depth, ndepth, depth_inteval_pixel, shape, max_depth=192.0, min_depth=0.0):
cur_depth_min = (cur_depth - ndepth / 2 * depth_inteval_pixel) # (B, H, W)
cur_depth_max = (cur_depth + ndepth / 2 * depth_inteval_pixel)
assert cur_depth.shape == torch.Size(shape), "cur_depth:{}, input shape:{}".format(cur_depth.shape, shape)
new_interval = (cur_depth_max - cur_depth_min) / (ndepth - 1) # (B, H, W)
depth_range_samples = cur_depth_min.unsqueeze(1) + (torch.arange(0, ndepth, device=cur_depth.device,
dtype=cur_depth.dtype,
requires_grad=False).reshape(1, -1, 1, 1) * new_interval.unsqueeze(1))
return depth_range_samples
def get_depth_range_samples(cur_depth, ndepth, depth_inteval_pixel, device, dtype, shape,
max_depth=192.0, min_depth=0.0, use_inverse_depth=False):
if cur_depth.dim() == 2:
cur_depth_min = cur_depth[:, 0] # (B,)
cur_depth_max = cur_depth[:, -1]
if use_inverse_depth is False:
new_interval = (cur_depth_max - cur_depth_min) / (ndepth - 1) # (B, ) Shouldn't cal this if we use inverse depth
depth_range_samples = cur_depth_min.unsqueeze(1) + (torch.arange(0, ndepth, device=device, dtype=dtype,
requires_grad=False).reshape(1, -1) * new_interval.unsqueeze(1)) #(B, D)
depth_range_samples = depth_range_samples.unsqueeze(-1).unsqueeze(-1).repeat(1, 1, shape[1], shape[2]) #(B, D, H, W)
else:
# When use inverse_depth for T&T
depth_range_samples = cur_depth.repeat(1, 1, shape[1], shape[2]) #(B, D, H, W)
else:
depth_range_samples = get_cur_depth_range_samples(cur_depth, ndepth, depth_inteval_pixel, shape, max_depth, min_depth)
return depth_range_samples