text
stringlengths
1
93.6k
@staticmethod
def compute_loss_masks(reprojection_loss, identity_reprojection_loss,
depth_hint_reprojection_loss):
""" Compute loss masks for each of standard reprojection and depth hint
reprojection.
identity_reprojections_loss and/or depth_hint_reprojection_loss can be None"""
if identity_reprojection_loss is None:
# we are not using automasking - standard reprojection loss applied to all pixels
reprojection_loss_mask = torch.ones_like(reprojection_loss)
if depth_hint_reprojection_loss:
all_losses = torch.cat([reprojection_loss, depth_hint_reprojection_loss], dim=1)
idxs = torch.argmin(all_losses, dim=1, keepdim=True)
depth_hint_loss_mask = (idxs == 1).float()
else:
# we are using automasking
if depth_hint_reprojection_loss is not None:
all_losses = torch.cat([reprojection_loss, identity_reprojection_loss,
depth_hint_reprojection_loss], dim=1)
else:
all_losses = torch.cat([reprojection_loss, identity_reprojection_loss], dim=1)
idxs = torch.argmin(all_losses, dim=1, keepdim=True)
reprojection_loss_mask = (idxs != 1).float() # automask has index '1'
depth_hint_loss_mask = (idxs == 2).float() # will be zeros if not using depth hints
# just set depth hint mask to None if not using depth hints
depth_hint_loss_mask = \
None if depth_hint_reprojection_loss is None else depth_hint_loss_mask
return reprojection_loss_mask, depth_hint_loss_mask
def compute_losses(self, inputs, outputs):
"""Compute the reprojection, smoothness and proxy supervised losses for a minibatch
"""
losses = {}
total_loss = 0
# compute depth hint reprojection loss
if self.opt.use_depth_hints:
pred = outputs[("color_depth_hint", 's', 0)]
depth_hint_reproj_loss = self.compute_reprojection_loss(pred, inputs[("color", 0, 0)])
# set loss for missing pixels to be high so they are never chosen as minimum
depth_hint_reproj_loss += 1000 * (1 - inputs['depth_hint_mask'])
else:
depth_hint_reproj_loss = None
for scale in self.opt.scales:
loss = 0
reprojection_losses = []
if self.opt.v1_multiscale:
source_scale = scale
else:
source_scale = 0
disp = outputs[("disp", scale)]
color = inputs[("color", 0, scale)]
target = inputs[("color", 0, source_scale)]
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_losses = torch.cat(reprojection_losses, 1)
if not self.opt.disable_automasking:
identity_reprojection_losses = []
for frame_id in self.opt.frame_ids[1:]:
pred = inputs[("color", frame_id, source_scale)]
identity_reprojection_losses.append(self.compute_reprojection_loss(pred, target))
identity_reprojection_losses = torch.cat(identity_reprojection_losses, 1)
if self.opt.avg_reprojection:
identity_reprojection_loss = identity_reprojection_losses.mean(1, keepdim=True)
else:
# differently to Monodepth2, compute mins as we go
identity_reprojection_loss, _ = torch.min(identity_reprojection_losses, dim=1,
keepdim=True)
else:
identity_reprojection_loss = None
if self.opt.predictive_mask:
# use the predicted mask
mask = outputs["predictive_mask"]["disp", scale]
if not self.opt.v1_multiscale:
mask = F.interpolate(
mask, [self.opt.height, self.opt.width],
mode="bilinear", align_corners=False)
reprojection_losses *= mask
# add a loss pushing mask to 1 (using nn.BCELoss for stability)
weighting_loss = 0.2 * nn.BCELoss()(mask, torch.ones(mask.shape).cuda())
loss += weighting_loss.mean()