Use logits to sample points :param coords: coords of points, FloatTensor[B, 3, N] :param logits: binary classification logits, FloatTensor[B, 2, N] :param num_points_per_object: M, #points per object after masking, int :return: selected_coords: FloatTensor[B, 3, M]
(coords, logits, num_points_per_object)
| 49 | |
| 50 | |
| 51 | def logits_mask(coords, logits, num_points_per_object): |
| 52 | """ |
| 53 | Use logits to sample points |
| 54 | :param coords: coords of points, FloatTensor[B, 3, N] |
| 55 | :param logits: binary classification logits, FloatTensor[B, 2, N] |
| 56 | :param num_points_per_object: M, #points per object after masking, int |
| 57 | :return: |
| 58 | selected_coords: FloatTensor[B, 3, M] |
| 59 | masked_coords_mean: mean coords of selected points, FloatTensor[B, 3] |
| 60 | mask: mask to select points, BoolTensor[B, N] |
| 61 | """ |
| 62 | batch_size, _, num_points = coords.shape |
| 63 | mask = torch.lt(logits[:, 0, :], logits[:, 1, :]) # [B, N] |
| 64 | num_candidates = torch.sum(mask, dim=-1, keepdim=True) # [B, 1] |
| 65 | masked_coords = coords * mask.view(batch_size, 1, num_points) # [B, C, N] |
| 66 | masked_coords_mean = torch.sum(masked_coords, dim=-1) / torch.max(num_candidates, |
| 67 | torch.ones_like(num_candidates)).float() # [B, C] |
| 68 | selected_indices = torch.zeros((batch_size, num_points_per_object), device=coords.device, dtype=torch.int32) |
| 69 | for i in range(batch_size): |
| 70 | current_mask = mask[i] # [N] |
| 71 | current_candidates = current_mask.nonzero().view(-1) |
| 72 | current_num_candidates = current_candidates.numel() |
| 73 | if current_num_candidates >= num_points_per_object: |
| 74 | choices = np.random.choice(current_num_candidates, num_points_per_object, replace=False) |
| 75 | selected_indices[i] = current_candidates[choices] |
| 76 | elif current_num_candidates > 0: |
| 77 | choices = np.concatenate([ |
| 78 | np.arange(current_num_candidates).repeat(num_points_per_object // current_num_candidates), |
| 79 | np.random.choice(current_num_candidates, num_points_per_object % current_num_candidates, replace=False) |
| 80 | ]) |
| 81 | np.random.shuffle(choices) |
| 82 | selected_indices[i] = current_candidates[choices] |
| 83 | selected_coords = gather(masked_coords - masked_coords_mean.view(batch_size, -1, 1), selected_indices) |
| 84 | return selected_coords, masked_coords_mean, mask |
nothing calls this directly
no outgoing calls
no test coverage detected