(inputs, depth_gt_ms, mask_ms, mode, **kwargs)
| 3 | import numpy as np |
| 4 | |
| 5 | def mvs_loss(inputs, depth_gt_ms, mask_ms, mode, **kwargs): |
| 6 | depth_loss_weights = kwargs.get("dlossw", [1.0 for k in inputs.keys() if "stage" in k]) |
| 7 | total_loss = torch.tensor(0.0, dtype=torch.float32, device=mask_ms["stage1"].device, requires_grad=False) |
| 8 | for (stage_inputs, stage_key) in [(inputs[k], k) for k in inputs.keys() if "stage" in k]: |
| 9 | prob_volume = stage_inputs["prob_volume"] if "global_volume" not in stage_inputs else stage_inputs["global_volume"]# (b, d, h, w) |
| 10 | depth_values = stage_inputs["depth_values"] if "depth_values_new" not in stage_inputs else stage_inputs["depth_values_new"]# (b, d, h, w) |
| 11 | interval = stage_inputs["interval"] # float |
| 12 | depth_gt = depth_gt_ms[stage_key] # (b, h, w) |
| 13 | mask = mask_ms[stage_key] |
| 14 | |
| 15 | mask = mask > 0.5 |
| 16 | |
| 17 | stage_idx = int(stage_key.replace("stage", "")) - 1 |
| 18 | stage_weight = depth_loss_weights[stage_idx] |
| 19 | |
| 20 | |
| 21 | if mode == "regression": |
| 22 | |
| 23 | depth_sub_plus=stage_inputs["depth_sub_plus"] |
| 24 | depth_sup_plus_small,depth_sup_plus_huge=depth_sub_plus.split([2,2],dim=1) |
| 25 | loss_depth=2*regression_loss(depth_sup_plus_small, depth_gt.unsqueeze(1).expand_as(depth_sup_plus_small), mask.unsqueeze(1).expand_as(depth_sup_plus_small),torch.ones_like(depth_sup_plus_small)*stage_weight)\ |
| 26 | +2*regression_loss(depth_sup_plus_huge, depth_gt.unsqueeze(1).expand_as(depth_sup_plus_huge), mask.unsqueeze(1).expand_as(depth_sup_plus_huge),torch.ones_like(depth_sup_plus_huge)*stage_weight) |
| 27 | |
| 28 | |
| 29 | var_gt=torch.where((depth_sub_plus[:,0]-depth_gt).abs()<(depth_sub_plus[:,1]-depth_gt).abs(),(depth_sub_plus[:,1]-depth_gt).abs(),(depth_sub_plus[:,0]-depth_gt).abs()) |
| 30 | loss_var_small=regression_loss((depth_sub_plus[:,0]-depth_sub_plus[:,1]).abs(), var_gt, mask,torch.ones_like(var_gt)*stage_weight) |
| 31 | |
| 32 | var_gt=torch.where((depth_sub_plus[:,2]-depth_gt).abs()<(depth_sub_plus[:,3]-depth_gt).abs(),(depth_sub_plus[:,3]-depth_gt).abs(),(depth_sub_plus[:,2]-depth_gt).abs()) |
| 33 | loss_var_huge=regression_loss((depth_sub_plus[:,2]-depth_sub_plus[:,3]).abs(), var_gt, mask,torch.ones_like(var_gt)*stage_weight) |
| 34 | |
| 35 | |
| 36 | coors=torch.stack( |
| 37 | [item.unsqueeze(0).expand_as(depth_sub_plus[:,0]) for item in torch.meshgrid(*[torch.arange(0, s) for s in depth_sub_plus[:,0].shape[-2:]])], |
| 38 | axis=-1).to(depth_sub_plus[:,0].device) |
| 39 | coor_mask=((coors[:,:,:,0]%2==0)&(coors[:,:,:,1]%2==0))|((coors[:,:,:,0]%2==1)&(coors[:,:,:,1]%2==1))# |
| 40 | |
| 41 | small_min,small_max=depth_sup_plus_small.min(1)[0],depth_sup_plus_small.max(1)[0] |
| 42 | huge_min,huge_max=depth_sup_plus_huge.min(1)[0],depth_sup_plus_huge.max(1)[0] |
| 43 | |
| 44 | loss_m=Monte_Carlo_sampling_loss(torch.where(coor_mask,small_min,small_max),depth_gt,mask,torch.ones_like(depth_gt)*stage_weight,mode="center",regress_fn=regression_loss)+\ |
| 45 | Monte_Carlo_sampling_loss(torch.where(~coor_mask,small_min,small_max),depth_gt,mask,torch.ones_like(depth_gt)*stage_weight,mode="center",regress_fn=regression_loss)+\ |
| 46 | Monte_Carlo_sampling_loss(torch.where(coor_mask,huge_min,huge_max),depth_gt,mask,torch.ones_like(depth_gt)*stage_weight,mode="center",regress_fn=regression_loss)+\ |
| 47 | Monte_Carlo_sampling_loss(torch.where(~coor_mask,huge_min,huge_max),depth_gt,mask,torch.ones_like(depth_gt)*stage_weight,mode="center",regress_fn=regression_loss) |
| 48 | |
| 49 | total_loss+=(loss_depth+loss_var_small+loss_var_huge+loss_m) |
| 50 | |
| 51 | |
| 52 | ###refine*********************** |
| 53 | |
| 54 | depth_sub_plus=stage_inputs["depth_sub_plus_refine"] |
| 55 | depth_sup_plus_small,depth_sup_plus_huge=depth_sub_plus.split([2,2],dim=1) |
| 56 | loss_depth=2*regression_loss(depth_sup_plus_small, depth_gt.unsqueeze(1).expand_as(depth_sup_plus_small), mask.unsqueeze(1).expand_as(depth_sup_plus_small),torch.ones_like(depth_sup_plus_small)*stage_weight)\ |
| 57 | +2*regression_loss(depth_sup_plus_huge, depth_gt.unsqueeze(1).expand_as(depth_sup_plus_huge), mask.unsqueeze(1).expand_as(depth_sup_plus_huge),torch.ones_like(depth_sup_plus_huge)*stage_weight) |
| 58 | |
| 59 | var_gt=torch.where((depth_sub_plus[:,0]-depth_gt).abs()<(depth_sub_plus[:,1]-depth_gt).abs(),(depth_sub_plus[:,1]-depth_gt).abs(),(depth_sub_plus[:,0]-depth_gt).abs()) |
| 60 | loss_var_small=regression_loss((depth_sub_plus[:,0]-depth_sub_plus[:,1]).abs(), var_gt, mask,torch.ones_like(var_gt)*stage_weight) |
| 61 | |
| 62 | var_gt=torch.where((depth_sub_plus[:,2]-depth_gt).abs()<(depth_sub_plus[:,3]-depth_gt).abs(),(depth_sub_plus[:,3]-depth_gt).abs(),(depth_sub_plus[:,2]-depth_gt).abs()) |
nothing calls this directly
no test coverage detected