-
Notifications
You must be signed in to change notification settings - Fork 17
/
net.py
272 lines (234 loc) · 11 KB
/
net.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
from __future__ import absolute_import, division, print_function
import torch
import torch.nn.functional as F
import torch.nn as nn
from .layers import SSIM, Backproject, Project
from .depth_encoder import DepthEncoder
from .depth_decoder import DepthDecoder
from .pose_encoder import PoseEncoder
from .pose_decoder import PoseDecoder
from ..mono_autoencoder.encoder import Encoder
from ..registry import MONO
def build_extractor(num_layers, pretrained_path):
extractor = Encoder(num_layers, None)
if pretrained_path is not None:
checkpoint = torch.load(pretrained_path, map_location='cpu')
for name, param in extractor.state_dict().items():
extractor.state_dict()[name].copy_(checkpoint['state_dict']['Encoder.' + name])
for param in extractor.parameters():
param.requires_grad = False
return extractor
@MONO.register_module
class mono_fm(nn.Module):
def __init__(self, options):
super(mono_fm, self).__init__()
self.opt = options
self.DepthEncoder = DepthEncoder(self.opt.depth_num_layers,
self.opt.depth_pretrained_path)
self.DepthDecoder = DepthDecoder(self.DepthEncoder.num_ch_enc)
self.PoseEncoder = PoseEncoder(self.opt.pose_num_layers,
self.opt.pose_pretrained_path)
self.PoseDecoder = PoseDecoder(self.PoseEncoder.num_ch_enc)
self.extractor = build_extractor(self.opt.depth_num_layers,
self.opt.extractor_pretrained_path)
self.ssim = SSIM()
self.backproject = Backproject(self.opt.imgs_per_gpu, self.opt.height, self.opt.width)
self.project= Project(self.opt.imgs_per_gpu, self.opt.height, self.opt.width)
def forward(self, inputs):
outputs = self.DepthDecoder(self.DepthEncoder(inputs["color_aug", 0, 0]))
if self.training:
outputs.update(self.predict_poses(inputs))
loss_dict = self.compute_losses(inputs, outputs)
return outputs, loss_dict
return outputs
def robust_l1(self, pred, target):
eps = 1e-3
return torch.sqrt(torch.pow(target - pred, 2) + eps ** 2)
def compute_perceptional_loss(self, tgt_f, src_f):
loss = self.robust_l1(tgt_f, src_f).mean(1, True)
return loss
def compute_reprojection_loss(self, pred, target):
photometric_loss = self.robust_l1(pred, target).mean(1, True)
ssim_loss = self.ssim(pred, target).mean(1, True)
reprojection_loss = (0.85 * ssim_loss + 0.15 * photometric_loss)
return reprojection_loss
def compute_losses(self, inputs, outputs):
loss_dict = {}
for scale in self.opt.scales:
"""
initialization
"""
disp = outputs[("disp", 0, scale)]
target = inputs[("color", 0, 0)]
reprojection_losses = []
perceptional_losses = []
"""
reconstruction
"""
#print(outputs)
outputs = self.generate_images_pred(inputs, outputs, scale)
outputs = self.generate_features_pred(inputs, outputs)
"""
automask
"""
if self.opt.automask:
for frame_id in self.opt.frame_ids[1:]:
pred = inputs[("color", frame_id, 0)]
identity_reprojection_loss = self.compute_reprojection_loss(pred, target)
identity_reprojection_loss += torch.randn(identity_reprojection_loss.shape).cuda() * 1e-5
reprojection_losses.append(identity_reprojection_loss)
"""
minimum reconstruction loss
"""
for frame_id in self.opt.frame_ids[1:]:
pred = outputs[("color", frame_id, scale)]
reprojection_losses.append(self.compute_reprojection_loss(pred, target))
reprojection_loss = torch.cat(reprojection_losses, 1)
min_reconstruct_loss, outputs[("min_index", scale)] = torch.min(reprojection_loss, dim=1)
loss_dict[('min_reconstruct_loss', scale)] = min_reconstruct_loss.mean()/len(self.opt.scales)
"""
minimum perceptional loss
"""
for frame_id in self.opt.frame_ids[1:]:
src_f = outputs[("feature", frame_id, 0)]
tgt_f = self.extractor(inputs[("color", 0, 0)])[0]
perceptional_losses.append(self.compute_perceptional_loss(tgt_f, src_f))
perceptional_loss = torch.cat(perceptional_losses, 1)
min_perceptional_loss, outputs[("min_index", scale)] = torch.min(perceptional_loss, dim=1)
loss_dict[('min_perceptional_loss', scale)] = self.opt.perception_weight * min_perceptional_loss.mean() / len(self.opt.scales)
"""
disp mean normalization
"""
if self.opt.disp_norm:
mean_disp = disp.mean(2, True).mean(3, True)
disp = disp / (mean_disp + 1e-7)
"""
smooth loss
"""
smooth_loss = self.get_smooth_loss(disp, target)
loss_dict[('smooth_loss', scale)] = self.opt.smoothness_weight * smooth_loss / (2 ** scale)/len(self.opt.scales)
return loss_dict
def disp_to_depth(self, disp, min_depth, max_depth):
min_disp = 1 / max_depth # 0.01
max_disp = 1 / min_depth # 10
scaled_disp = min_disp + (max_disp - min_disp) * disp # (10-0.01)*disp+0.01
depth = 1 / scaled_disp
return scaled_disp, depth
def predict_poses(self, inputs):
outputs = {}
#[192,640] for kitti
pose_feats = {f_i: F.interpolate(inputs["color_aug", f_i, 0], [192, 640], mode="bilinear", align_corners=False) for f_i in self.opt.frame_ids}
for f_i in self.opt.frame_ids[1:]:
if not f_i == "s":
if f_i < 0:
pose_inputs = [pose_feats[f_i], pose_feats[0]]
else:
pose_inputs = [pose_feats[0], pose_feats[f_i]]
pose_inputs = self.PoseEncoder(torch.cat(pose_inputs, 1))
axisangle, translation = self.PoseDecoder(pose_inputs)
outputs[("cam_T_cam", 0, f_i)] = self.transformation_from_parameters(axisangle[:, 0], translation[:, 0], invert=(f_i < 0))
return outputs
def generate_images_pred(self, inputs, outputs, scale):
disp = outputs[("disp", 0, scale)]
disp = F.interpolate(disp, [self.opt.height, self.opt.width], mode="bilinear", align_corners=False)
_, depth = self.disp_to_depth(disp, self.opt.min_depth, self.opt.max_depth)
for i, frame_id in enumerate(self.opt.frame_ids[1:]):
if frame_id == "s":
T = inputs["stereo_T"]
else:
T = outputs[("cam_T_cam", 0, frame_id)]
cam_points = self.backproject(depth, inputs[("inv_K")])
pix_coords = self.project(cam_points, inputs[("K")], T)#[b,h,w,2]
img = inputs[("color", frame_id, 0)]
outputs[("color", frame_id, scale)] = F.grid_sample(img, pix_coords, padding_mode="border")
return outputs
def generate_features_pred(self, inputs, outputs):
disp = outputs[("disp", 0, 0)]
disp = F.interpolate(disp, [int(self.opt.height/2), int(self.opt.width/2)], mode="bilinear", align_corners=False)
_, depth = self.disp_to_depth(disp, self.opt.min_depth, self.opt.max_depth)
for i, frame_id in enumerate(self.opt.frame_ids[1:]):
if frame_id == "s":
T = inputs["stereo_T"]
else:
T = outputs[("cam_T_cam", 0, frame_id)]
backproject = Backproject(self.opt.imgs_per_gpu, int(self.opt.height/2), int(self.opt.width/2))
project = Project(self.opt.imgs_per_gpu, int(self.opt.height/2), int(self.opt.width/2))
cam_points = backproject(depth, inputs[("inv_K")])
pix_coords = project(cam_points, inputs[("K")], T)#[b,h,w,2]
img = inputs[("color", frame_id, 0)]
src_f = self.extractor(img)[0]
outputs[("feature", frame_id, 0)] = F.grid_sample(src_f, pix_coords, padding_mode="border")
return outputs
def transformation_from_parameters(self, axisangle, translation, invert=False):
R = self.rot_from_axisangle(axisangle)
t = translation.clone()
if invert:
R = R.transpose(1, 2)
t *= -1
T = self.get_translation_matrix(t)
if invert:
M = torch.matmul(R, T)
else:
M = torch.matmul(T, R)
return M
def get_translation_matrix(self, translation_vector):
T = torch.zeros(translation_vector.shape[0], 4, 4).cuda()
t = translation_vector.contiguous().view(-1, 3, 1)
T[:, 0, 0] = 1
T[:, 1, 1] = 1
T[:, 2, 2] = 1
T[:, 3, 3] = 1
T[:, :3, 3, None] = t
return T
def rot_from_axisangle(self, vec):
angle = torch.norm(vec, 2, 2, True)
axis = vec / (angle + 1e-7)
ca = torch.cos(angle)
sa = torch.sin(angle)
C = 1 - ca
x = axis[..., 0].unsqueeze(1)
y = axis[..., 1].unsqueeze(1)
z = axis[..., 2].unsqueeze(1)
xs = x * sa
ys = y * sa
zs = z * sa
xC = x * C
yC = y * C
zC = z * C
xyC = x * yC
yzC = y * zC
zxC = z * xC
rot = torch.zeros((vec.shape[0], 4, 4)).cuda()
rot[:, 0, 0] = torch.squeeze(x * xC + ca)
rot[:, 0, 1] = torch.squeeze(xyC - zs)
rot[:, 0, 2] = torch.squeeze(zxC + ys)
rot[:, 1, 0] = torch.squeeze(xyC + zs)
rot[:, 1, 1] = torch.squeeze(y * yC + ca)
rot[:, 1, 2] = torch.squeeze(yzC - xs)
rot[:, 2, 0] = torch.squeeze(zxC - ys)
rot[:, 2, 1] = torch.squeeze(yzC + xs)
rot[:, 2, 2] = torch.squeeze(z * zC + ca)
rot[:, 3, 3] = 1
return rot
def get_smooth_loss(self, disp, img):
b, _, h, w = disp.size()
a1 = 0.5
a2 = 0.5
img = F.interpolate(img, (h, w), mode='area')
disp_dx, disp_dy = self.gradient(disp)
img_dx, img_dy = self.gradient(img)
disp_dxx, disp_dxy = self.gradient(disp_dx)
disp_dyx, disp_dyy = self.gradient(disp_dy)
img_dxx, img_dxy = self.gradient(img_dx)
img_dyx, img_dyy = self.gradient(img_dy)
smooth1 = torch.mean(disp_dx.abs() * torch.exp(-a1 * img_dx.abs().mean(1, True))) + \
torch.mean(disp_dy.abs() * torch.exp(-a1 * img_dy.abs().mean(1, True)))
smooth2 = torch.mean(disp_dxx.abs() * torch.exp(-a2 * img_dxx.abs().mean(1, True))) + \
torch.mean(disp_dxy.abs() * torch.exp(-a2 * img_dxy.abs().mean(1, True))) + \
torch.mean(disp_dyx.abs() * torch.exp(-a2 * img_dyx.abs().mean(1, True))) + \
torch.mean(disp_dyy.abs() * torch.exp(-a2 * img_dyy.abs().mean(1, True)))
return smooth1+smooth2
def gradient(self, D):
D_dy = D[:, :, 1:] - D[:, :, :-1]
D_dx = D[:, :, :, 1:] - D[:, :, :, :-1]
return D_dx, D_dy