File size: 7,570 Bytes
18dd6ad |
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 |
import torch
import torch.nn as nn
import numpy as np
import torch.nn.functional as F
# compute loss
class compute_loss(nn.Module):
def __init__(self, args):
"""args.loss_fn can be one of following:
- L1 - L1 loss (no uncertainty)
- L2 - L2 loss (no uncertainty)
- AL - Angular loss (no uncertainty)
- NLL_vMF - NLL of vonMF distribution
- NLL_ours - NLL of Angular vonMF distribution
- UG_NLL_vMF - NLL of vonMF distribution (+ pixel-wise MLP + uncertainty-guided sampling)
- UG_NLL_ours - NLL of Angular vonMF distribution (+ pixel-wise MLP + uncertainty-guided sampling)
"""
super(compute_loss, self).__init__()
self.loss_type = args.loss_fn
if self.loss_type in ['L1', 'L2', 'AL', 'NLL_vMF', 'NLL_ours']:
self.loss_fn = self.forward_R
elif self.loss_type in ['UG_NLL_vMF', 'UG_NLL_ours']:
self.loss_fn = self.forward_UG
else:
raise Exception('invalid loss type')
def forward(self, *args):
return self.loss_fn(*args)
def forward_R(self, norm_out, gt_norm, gt_norm_mask):
pred_norm, pred_kappa = norm_out[:, 0:3, :, :], norm_out[:, 3:, :, :]
if self.loss_type == 'L1':
l1 = torch.sum(torch.abs(gt_norm - pred_norm), dim=1, keepdim=True)
loss = torch.mean(l1[gt_norm_mask])
elif self.loss_type == 'L2':
l2 = torch.sum(torch.square(gt_norm - pred_norm), dim=1, keepdim=True)
loss = torch.mean(l2[gt_norm_mask])
elif self.loss_type == 'AL':
dot = torch.cosine_similarity(pred_norm, gt_norm, dim=1)
valid_mask = gt_norm_mask[:, 0, :, :].float() \
* (dot.detach() < 0.999).float() \
* (dot.detach() > -0.999).float()
valid_mask = valid_mask > 0.0
al = torch.acos(dot[valid_mask])
loss = torch.mean(al)
elif self.loss_type == 'NLL_vMF':
dot = torch.cosine_similarity(pred_norm, gt_norm, dim=1)
valid_mask = gt_norm_mask[:, 0, :, :].float() \
* (dot.detach() < 0.999).float() \
* (dot.detach() > -0.999).float()
valid_mask = valid_mask > 0.0
dot = dot[valid_mask]
kappa = pred_kappa[:, 0, :, :][valid_mask]
loss_pixelwise = - torch.log(kappa) \
- (kappa * (dot - 1)) \
+ torch.log(1 - torch.exp(- 2 * kappa))
loss = torch.mean(loss_pixelwise)
elif self.loss_type == 'NLL_ours':
dot = torch.cosine_similarity(pred_norm, gt_norm, dim=1)
valid_mask = gt_norm_mask[:, 0, :, :].float() \
* (dot.detach() < 0.999).float() \
* (dot.detach() > -0.999).float()
valid_mask = valid_mask > 0.0
dot = dot[valid_mask]
kappa = pred_kappa[:, 0, :, :][valid_mask]
loss_pixelwise = - torch.log(torch.square(kappa) + 1) \
+ kappa * torch.acos(dot) \
+ torch.log(1 + torch.exp(-kappa * np.pi))
loss = torch.mean(loss_pixelwise)
else:
raise Exception('invalid loss type')
return loss
def forward_UG(self, pred_list, coord_list, gt_norm, gt_norm_mask):
loss = 0.0
for (pred, coord) in zip(pred_list, coord_list):
if coord is None:
pred = F.interpolate(pred, size=[gt_norm.size(2), gt_norm.size(3)], mode='bilinear', align_corners=True)
pred_norm, pred_kappa = pred[:, 0:3, :, :], pred[:, 3:, :, :]
if self.loss_type == 'UG_NLL_vMF':
dot = torch.cosine_similarity(pred_norm, gt_norm, dim=1)
valid_mask = gt_norm_mask[:, 0, :, :].float() \
* (dot.detach() < 0.999).float() \
* (dot.detach() > -0.999).float()
valid_mask = valid_mask > 0.5
# mask
dot = dot[valid_mask]
kappa = pred_kappa[:, 0, :, :][valid_mask]
loss_pixelwise = - torch.log(kappa) \
- (kappa * (dot - 1)) \
+ torch.log(1 - torch.exp(- 2 * kappa))
loss = loss + torch.mean(loss_pixelwise)
elif self.loss_type == 'UG_NLL_ours':
dot = torch.cosine_similarity(pred_norm, gt_norm, dim=1)
valid_mask = gt_norm_mask[:, 0, :, :].float() \
* (dot.detach() < 0.999).float() \
* (dot.detach() > -0.999).float()
valid_mask = valid_mask > 0.5
dot = dot[valid_mask]
kappa = pred_kappa[:, 0, :, :][valid_mask]
loss_pixelwise = - torch.log(torch.square(kappa) + 1) \
+ kappa * torch.acos(dot) \
+ torch.log(1 + torch.exp(-kappa * np.pi))
loss = loss + torch.mean(loss_pixelwise)
else:
raise Exception
else:
# coord: B, 1, N, 2
# pred: B, 4, N
gt_norm_ = F.grid_sample(gt_norm, coord, mode='nearest', align_corners=True) # (B, 3, 1, N)
gt_norm_mask_ = F.grid_sample(gt_norm_mask.float(), coord, mode='nearest', align_corners=True) # (B, 1, 1, N)
gt_norm_ = gt_norm_[:, :, 0, :] # (B, 3, N)
gt_norm_mask_ = gt_norm_mask_[:, :, 0, :] > 0.5 # (B, 1, N)
pred_norm, pred_kappa = pred[:, 0:3, :], pred[:, 3:, :]
if self.loss_type == 'UG_NLL_vMF':
dot = torch.cosine_similarity(pred_norm, gt_norm_, dim=1) # (B, N)
valid_mask = gt_norm_mask_[:, 0, :].float() \
* (dot.detach() < 0.999).float() \
* (dot.detach() > -0.999).float()
valid_mask = valid_mask > 0.5
dot = dot[valid_mask]
kappa = pred_kappa[:, 0, :][valid_mask]
loss_pixelwise = - torch.log(kappa) \
- (kappa * (dot - 1)) \
+ torch.log(1 - torch.exp(- 2 * kappa))
loss = loss + torch.mean(loss_pixelwise)
elif self.loss_type == 'UG_NLL_ours':
dot = torch.cosine_similarity(pred_norm, gt_norm_, dim=1) # (B, N)
valid_mask = gt_norm_mask_[:, 0, :].float() \
* (dot.detach() < 0.999).float() \
* (dot.detach() > -0.999).float()
valid_mask = valid_mask > 0.5
dot = dot[valid_mask]
kappa = pred_kappa[:, 0, :][valid_mask]
loss_pixelwise = - torch.log(torch.square(kappa) + 1) \
+ kappa * torch.acos(dot) \
+ torch.log(1 + torch.exp(-kappa * np.pi))
loss = loss + torch.mean(loss_pixelwise)
else:
raise Exception
return loss
|