Skip to content

Commit

Permalink
perf #70: introducing changes proposed by Lucashsmello
Browse files Browse the repository at this point in the history
- See this [PR](adambielski/siamese-triplet#54)
  where the original author of the PR proposes a faster implementation
  for the triplet selector
  • Loading branch information
SergioQuijanoRey committed Apr 21, 2024
1 parent 21b29b8 commit c8a7320
Showing 1 changed file with 8 additions and 9 deletions.
17 changes: 8 additions & 9 deletions src/adambielski_lib/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,19 +162,18 @@ def get_triplets(self, embeddings, labels):
anchor_positives = list(combinations(label_indices, 2)) # All anchor-positive pairs
anchor_positives = np.array(anchor_positives)

ap_distances = distance_matrix[anchor_positives[:, 0], anchor_positives[:, 1]]
for anchor_positive, ap_distance in zip(anchor_positives, ap_distances):
loss_values = ap_distance - distance_matrix[torch.LongTensor(np.array([anchor_positive[0]])), torch.LongTensor(negative_indices)] + self.margin
loss_values = loss_values.data.cpu().numpy()
hard_negative = self.negative_selection_fn(loss_values)
ap_distances = distance_matrix[anchor_positives[:, 0], anchor_positives[:, 1]] + self.margin
idxs = np.ix_(anchor_positives[:, 0], negative_indices)
loss_values = ap_distances.unsqueeze(dim=1) - distance_matrix[idxs]
loss_values = loss_values.data.cpu().numpy()
for i, loss_val in enumerate(loss_values):
hard_negative = self.negative_selection_fn(loss_val)
if hard_negative is not None:
hard_negative = negative_indices[hard_negative]
triplets.append([anchor_positive[0], anchor_positive[1], hard_negative])
triplets.append([anchor_positives[i][0], anchor_positives[i][1], hard_negative])

if len(triplets) == 0:
triplets.append([anchor_positive[0], anchor_positive[1], negative_indices[0]])

triplets = np.array(triplets)
triplets.append([anchor_positives[-1][0], anchor_positives[-1][1], negative_indices[0]])

return torch.LongTensor(triplets)

Expand Down

0 comments on commit c8a7320

Please sign in to comment.