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

Function Monte_Carlo_sampling_loss

loss.py:106–155  ·  view source on GitHub ↗
(depth_est, depth_gt, mask, weight,mode="center",reflect=False,regress_fn=None)

Source from the content-addressed store, hash-verified

104 return total_loss
105
106def Monte_Carlo_sampling_loss(depth_est, depth_gt, mask, weight,mode="center",reflect=False,regress_fn=None):
107
108 batch,height, width= depth_gt.shape
109
110 if mode=="center":
111 x_offset,y_offset=0.5*torch.ones((batch,height-1, width-1)),0.5*torch.ones((batch,height-1, width-1))
112 else:
113 x_offset,y_offset=torch.rand(batch,height-1, width-1),torch.rand((batch,height-1, width-1))
114
115 x_offset,y_offset=x_offset.to(depth_gt.device),y_offset.to(depth_gt.device)
116
117 y, x = torch.meshgrid([torch.arange(0, height-1, dtype=torch.float32, device=depth_gt.device),
118 torch.arange(0, width-1, dtype=torch.float32, device=depth_gt.device)])
119 y, x = y.contiguous().unsqueeze(0).repeat(batch,1,1)+y_offset, x.contiguous().unsqueeze(0).repeat(batch,1,1)+x_offset
120 x=x/((width - 1) / 2) - 1
121 y=y/((height - 1) / 2) - 1
122
123 grid=torch.stack((x, y), dim=3)
124
125 sampled_gt=F.grid_sample(depth_gt.unsqueeze(1), grid, mode='bilinear',padding_mode='zeros',align_corners=True).type(torch.float32)
126 sampled_est=F.grid_sample(depth_est.unsqueeze(1), grid, mode='bilinear',padding_mode='zeros',align_corners=True).type(torch.float32)
127 sampled_weight=F.grid_sample(weight.unsqueeze(1), grid, mode='bilinear',padding_mode='zeros',align_corners=True).type(torch.float32)
128 sampled_mask=F.grid_sample(mask.float().unsqueeze(1), grid, mode='bilinear',padding_mode='zeros',align_corners=True).type(torch.float32)
129 #mask!=1 mean there is zero depth\
130 sampled_mask=sampled_mask>=1.
131
132
133 if reflect== False:
134 # loss = F.smooth_l1_loss(sampled_est[sampled_mask], sampled_gt[sampled_mask], reduction='mean')
135 loss =regress_fn(sampled_est, sampled_gt, sampled_mask,sampled_weight)
136
137 else:
138 with torch.no_grad():
139 err=depth_est-depth_gt
140 kernel = torch.ones((2,2)).unsqueeze(0).unsqueeze(0).to(depth_gt.device)
141 kernel_weight = torch.nn.Parameter(data=kernel, requires_grad=False)
142
143 up_sum=F.conv2d((err.unsqueeze(1)>0).float(),kernel_weight)
144 dn_sum=F.conv2d((err.unsqueeze(1)<0).float(),kernel_weight)
145
146 reflect_weight=torch.where((up_sum==4.)|(dn_sum==4.),2*torch.ones_like(sampled_gt),torch.ones_like(sampled_gt))
147 # reflect_weight=reflect_weight[sampled_mask]
148
149 loss = F.smooth_l1_loss((reflect_weight*sampled_est)[sampled_mask], (reflect_weight*sampled_gt)[sampled_mask], reduction='mean')
150
151 # loss = loss* weight
152
153
154
155 return loss
156def regression_loss(depth_est, depth_gt, mask, weight):
157 loss = F.smooth_l1_loss(depth_est[mask], depth_gt[mask], reduction='none')
158 loss = (loss* weight[mask]).mean()

Callers 1

mvs_lossFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected