(prob_volume, depth_gt, mask, depth_value, return_prob_map=False)
| 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): |
| 274 | # from AA |
| 275 | mask_true = mask |
| 276 | valid_pixel_num = torch.sum(mask_true, dim=[1,2]) + 1e-6 |
| 277 | |
| 278 | shape = depth_gt.shape # B,H,W |
| 279 | |
| 280 | depth_num = depth_value.shape[1] |
| 281 | if len(depth_value.shape) < 3: |
| 282 | depth_value_mat = depth_value.repeat(shape[1], shape[2], 1, 1).permute(2,3,0,1) # B,N,H,W |
| 283 | else: |
| 284 | depth_value_mat = depth_value |
| 285 | |
| 286 | gt_index_image = torch.argmin(torch.abs(depth_value_mat-depth_gt.unsqueeze(1)), dim=1) |
| 287 | temp=gt_index_image |
| 288 | |
| 289 | gt_index_image = torch.mul(mask_true, gt_index_image.type(torch.float)) |
| 290 | gt_index_image = torch.round(gt_index_image).type(torch.long).unsqueeze(1) # B, 1, H, W |
| 291 | |
| 292 | # gt index map -> gt one hot volume (B x 1 x H x W ) |
| 293 | gt_index_volume = torch.zeros(shape[0], depth_num, shape[1], shape[2]).type(mask_true.type()).scatter_(1, gt_index_image, 1) |
| 294 | |
| 295 | # cross entropy image (B x D X H x W) |
| 296 | cross_entropy_image = -torch.sum(gt_index_volume * torch.log(prob_volume + 1e-6), dim=1).squeeze(1) # B, 1, H, W |
| 297 | |
| 298 | # masked cross entropy loss |
| 299 | masked_cross_entropy_image = torch.mul(mask_true, cross_entropy_image) # valid pixel |
| 300 | masked_cross_entropy = torch.sum(masked_cross_entropy_image, dim=[1, 2]) |
| 301 | |
| 302 | masked_cross_entropy = torch.mean(masked_cross_entropy / valid_pixel_num) # Origin use sum : aggregate with batch |
| 303 | # winner-take-all depth map |
| 304 | wta_index_map = torch.argmax(prob_volume, dim=1, keepdim=True).type(torch.long).squeeze(1) |
| 305 | |
| 306 | return masked_cross_entropy |
| 307 | |
| 308 | def entropy_loss_expand(prob_volume, depth_gt, mask, depth_value, return_prob_map=False): |
| 309 | # from AA |
nothing calls this directly
no outgoing calls
no test coverage detected