Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Memory optimized dists_add_symmetric #18

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 6 additions & 8 deletions cosypose/lib3d/distances.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,12 @@ def dists_add(TXO_pred, TXO_gt, points):
dists = TXO_gt_points - TXO_pred_points
return dists


def dists_add_symmetric(TXO_pred, TXO_gt, points):
TXO_pred_points = transform_pts(TXO_pred, points)
TXO_gt_points = transform_pts(TXO_gt, points)
dists = TXO_gt_points.unsqueeze(1) - TXO_pred_points.unsqueeze(2)
dists_norm_squared = (dists ** 2).sum(dim=-1)
assign = dists_norm_squared.argmin(dim=1)
ids_row = torch.arange(dists.shape[0]).unsqueeze(1).repeat(1, dists.shape[1])
ids_col = torch.arange(dists.shape[1]).unsqueeze(0).repeat(dists.shape[0], 1)
dists = dists[ids_row, assign, ids_col]
return dists
distances = torch.cdist(TXO_gt_points, TXO_pred_points,
p=2, compute_mode='donot_use_mm_for_euclid_dist')
Copy link
Author

@KushnirDmytro KushnirDmytro Apr 11, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

compute_mode='donot_use_mm_for_euclid_dist' is the important parameter value.
Checked on the debug data instance: for the default mode, it gives ~3% of points got a different closest point id.
Yet the difference is about 1e-4--1e-6, but the performance benefit is rather small.

closest_points_idx = torch.argmin(distances, dim=2).squeeze()
TXO_pred_closest_to_gt = torch.index_select(TXO_pred_points, 1, closest_points_idx)
min_translations = TXO_gt_points - TXO_pred_closest_to_gt
return min_translations