MCPcopy Create free account
hub / github.com/DIVE128/DMVSNet / entropy_loss_expand

Function entropy_loss_expand

loss.py:308–346  ·  view source on GitHub ↗
(prob_volume, depth_gt, mask, depth_value, return_prob_map=False)

Source from the content-addressed store, hash-verified

306 return masked_cross_entropy
307
308def entropy_loss_expand(prob_volume, depth_gt, mask, depth_value, return_prob_map=False):
309 # from AA
310
311
312 shape = depth_gt.shape # B,H,W
313 depth_gt=depth_gt.unsqueeze(1).repeat(1,3,1,1).view(-1,shape[-2],shape[-1])
314 mask=mask.unsqueeze(1).repeat(1,3,1,1).view(-1,shape[-2],shape[-1])
315 shape = depth_gt.shape
316
317 mask_true = mask
318 valid_pixel_num = torch.sum(mask_true, dim=[1,2]) + 1e-6
319
320 depth_num = depth_value.shape[1]
321 if len(depth_value.shape) < 3:
322 depth_value_mat = depth_value.repeat(shape[1], shape[2], 1, 1).permute(2,3,0,1) # B,N,H,W
323 else:
324 depth_value_mat = depth_value
325
326 gt_index_image = torch.argmin(torch.abs(depth_value_mat-depth_gt.unsqueeze(1)), dim=1)
327 temp=gt_index_image
328
329 gt_index_image = torch.mul(mask_true, gt_index_image.type(torch.float))
330 gt_index_image = torch.round(gt_index_image).type(torch.long).unsqueeze(1) # B, 1, H, W
331
332 # gt index map -> gt one hot volume (B x 1 x H x W )
333 gt_index_volume = torch.zeros(shape[0], depth_num, shape[1], shape[2]).type(mask_true.type()).scatter_(1, gt_index_image, 1)
334
335 # cross entropy image (B x D X H x W)
336 cross_entropy_image = -torch.sum(gt_index_volume * torch.log(prob_volume + 1e-6), dim=1).squeeze(1) # B, 1, H, W
337
338 # masked cross entropy loss
339 masked_cross_entropy_image = torch.mul(mask_true, cross_entropy_image) # valid pixel
340 masked_cross_entropy = torch.sum(masked_cross_entropy_image, dim=[1, 2])
341
342 masked_cross_entropy = torch.mean(masked_cross_entropy / valid_pixel_num) # Origin use sum : aggregate with batch
343 # winner-take-all depth map
344 wta_index_map = torch.argmax(prob_volume, dim=1, keepdim=True).type(torch.long).squeeze(1)
345
346 return masked_cross_entropy

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected