in c3dm/tools/eval_functions.py [0:0]
def eval_depth_pcl( pred, gt, K=None, masks=None,
gt_projection_type='perspective',
pred_projection_type='orthographic',
debug=False,
lap_thr=0.3,
):
ba = gt.shape[0]
if masks is not None:
# mult gt by mask
gt = gt * (masks > 0.).float()
gt = depth_flat_filter(gt, size=5, thr=lap_thr)
dmask = (gt > 0.).float()
dmask_mass = torch.clamp(dmask.sum((1,2,3)), 1e-4)
# convert to point clouds
pcl_pred = depth2pcl(pred, K, projection_type=pred_projection_type)
pcl_gt = depth2pcl(gt, K, projection_type=gt_projection_type)
if gt_projection_type==pred_projection_type and \
gt_projection_type=='perspective' and False:
# estimate the best scale
xy = pred * gt ; xx = pred * pred
xy *= dmask ; xx *= dmask
scale_best = xy.mean((1,2,3)) / torch.clamp(xx.mean((1,2,3)), 1e-12)
pred = pred * scale_best[:, None, None, None]
# convert to point clouds
c_pred = depth2pcl(pred, K, projection_type=pred_projection_type)
c_gt = depth2pcl(gt, K, projection_type=gt_projection_type)
# if debug:
# import pdb; pdb.set_trace()
# c_pred = c_pred * 3
else:
# debug visualisations
# pcl_pred = pcl_pred * masks
# from tools.vis_utils import get_visdom_connection, visdom_plot_pointclouds
# pcl_show = pcl_pred[0].view(3,-1)[:,masks[0].view(-1)>0.]
# viz = get_visdom_connection()
# visdom_plot_pointclouds(viz, \
# {'pcl_pred': pcl_show.cpu().detach().numpy()},
# 'pcl_debug',
# 'pcl_debug',
# win='pcl_debug',
# )
# import pdb; pdb.set_trace()
# mask the point clouds
pcl_pred, pcl_gt = [p * dmask for p in (pcl_pred, pcl_gt)]
# compute the pcl centers
pred_cnt, gt_cnt = [ \
p.sum((2,3), keepdim=True) / dmask_mass[:,None,None,None] \
for p in (pcl_pred, pcl_gt) ]
# center
c_pred, c_gt = [ \
p - c for p, c in zip((pcl_pred, pcl_gt), (pred_cnt, gt_cnt)) ]
# mask the centered point clouds
c_pred, c_gt = [p * dmask for p in (c_pred, c_gt)]
# estimate the best scale
xy = c_pred * c_gt ; xx = c_pred * c_pred
xy *= dmask ; xx *= dmask
scale_best = xy.mean((1,2,3)) / torch.clamp(xx.mean((1,2,3)), 1e-4)
# apply the best scale
c_pred = c_pred * scale_best[:, None, None, None]
# translate the point clouds back to original meanxy
# xy_mask = torch.FloatTensor([1.,1.,0.])[None,:,None,None].type_as(c_pred)
# d_c_pred, d_c_gt = [ \
# p.clone() + c * xy_mask \
# for p, c in zip((c_pred, c_gt), (pred_cnt, gt_cnt)) ]
# compute the per-vertex distance
df = c_gt - c_pred
dist = torch.clamp(df**2, 0.).sum(1,keepdim=True).sqrt()
dist = (dmask * dist).sum((1,2,3)) / dmask_mass
# if float(dist) <= 1e-3:
# import pdb; pdb.set_trace()
res = collections.OrderedDict( [
('dist_pcl', dist),
('scale_best', scale_best),
('pred', c_pred),
('pred_orig', pcl_pred),
('gt', c_gt),
('dmask', dmask),
] )
return res