| 210 | |
| 211 | |
| 212 | def gfocal_loss(prob_volume, depth_values, interval, depth_gt, mask, weight, gamma, alpha): |
| 213 | depth_gt_volume = depth_gt.unsqueeze(1).expand_as(depth_values) # (b, d, h, w) |
| 214 | |
| 215 | gt_index_volume = ((depth_values <= depth_gt_volume) * ((depth_values + interval) > depth_gt_volume)) #gt 在哪一个value里面而已 |
| 216 | gt_index_volume=gt_index_volume.float() |
| 217 | |
| 218 | pos_weight = (gt_index_volume - prob_volume).abs() |
| 219 | neg_weight = prob_volume |
| 220 | focal_weight = (pos_weight.pow(gamma)) * (gt_index_volume > 0.0).float()\ |
| 221 | + alpha*(neg_weight.pow(gamma)) * (gt_index_volume <= 0.0).float() |
| 222 | |
| 223 | NEAR_0 = 1e-4 # Prevent overflow |
| 224 | prob_volume = torch.where(prob_volume <= 0.0, torch.zeros_like(prob_volume) + NEAR_0, prob_volume) |
| 225 | |
| 226 | mask = mask.unsqueeze(1).expand_as(depth_values).float() # b d h w |
| 227 | loss = (F.binary_cross_entropy(prob_volume, gt_index_volume, reduction="none") * focal_weight * mask).sum() / mask.sum() # all |
| 228 | loss = loss * weight |
| 229 | return loss |
| 230 | |
| 231 | def unified_step_focal_loss(prob_volume, depth_values, interval, depth_gt, mask, weight, gamma, alpha): |
| 232 | depth_gt_volume = depth_gt.unsqueeze(1).expand_as(depth_values) # (b, d, h, w) |