| 250 | loss = loss * weight |
| 251 | return loss |
| 252 | def unified_focal_loss(prob_volume, depth_values, interval, depth_gt, mask, weight, gamma, alpha): |
| 253 | depth_gt_volume = depth_gt.unsqueeze(1).expand_as(depth_values) # (b, d, h, w) |
| 254 | |
| 255 | gt_index_volume = ((depth_values <= depth_gt_volume) * ((depth_values + interval) > depth_gt_volume)) |
| 256 | |
| 257 | gt_unity_index_volume = torch.zeros_like(prob_volume, requires_grad=False) |
| 258 | gt_unity_index_volume[gt_index_volume] = 1.0 - (depth_gt_volume[gt_index_volume] - depth_values[gt_index_volume]) / interval |
| 259 | |
| 260 | gt_unity, _ = torch.max(gt_unity_index_volume, dim=1, keepdim=True) |
| 261 | gt_unity = torch.where(gt_unity > 0.0, gt_unity, torch.ones_like(gt_unity)) # (b, 1, h, w) |
| 262 | pos_weight = (sigmoid((gt_unity - prob_volume).abs() / gt_unity, base=5) - 0.5) * 4 + 1 # [1, 3] |
| 263 | neg_weight = (sigmoid(prob_volume / gt_unity, base=5) - 0.5) * 2 # [0, 1] |
| 264 | focal_weight = pos_weight.pow(gamma) * (gt_unity_index_volume > 0.0).float() + alpha * neg_weight.pow(gamma) * ( |
| 265 | gt_unity_index_volume <= 0.0).float() |
| 266 | |
| 267 | mask = mask.unsqueeze(1).expand_as(depth_values).float() |
| 268 | loss = (F.binary_cross_entropy(prob_volume, gt_unity_index_volume, reduction="none") * focal_weight * mask).sum() / mask.sum() |
| 269 | loss = loss * weight |
| 270 | return loss |
| 271 | def sigmoid(x, base=2.71828): |
| 272 | return 1 / (1 + torch.pow(base, -x)) |
| 273 | def entropy_loss(prob_volume, depth_gt, mask, depth_value, return_prob_map=False): |