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

Faster triplet selector #54

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

Lucashsmello
Copy link

Hi,

it is possible to make the triplet selector run much faster both in cpu and cuda. It run 5x faster in my setup. It consists of just converting some operations made in a for loop to a single tensor operation.

Here is some code that a used to test and compare running time:

import torch
from utils import FunctionNegativeTripletSelector, random_hard_negative
from time import time
import numpy as np

torch.manual_seed(123)
n = 256
X = torch.rand((n, 8), dtype=torch.float32)
Y = torch.randint(0, 4, size=(n,))

np.random.seed(123)
selector = ORIGINALFunctionNegativeTripletSelector(1.0, random_hard_negative)
t = time()
result1 = selector.get_triplets(X, Y)
t = time()-t
print("%s %.3fsec" % (selector.__class__.__name__, t))

np.random.seed(123)
selector = NEWFunctionNegativeTripletSelector(1.0, random_hard_negative)
t = time()
result2 = selector.get_triplets(X, Y)
t = time()-t
print("%s %.3fsec" % (selector.__class__.__name__, t))

assert((result1 == result2).all())

@adambielski
Copy link
Owner

HI @Lucashsmello - thanks for the PR. Yes, unfortunately the triplet selection is not optimized, it's been on the TODO list for quite some time but I didn't have much time to work on this repo. I'll take a look at your PR and merge if there are no problems, I know that even more optimization can be done

@SergioQuijanoRey
Copy link

I've been using this changes for bigger datasets and helps a lot!

SergioQuijanoRey added a commit to SergioQuijanoRey/TFG that referenced this pull request Apr 21, 2024
- See this [PR](adambielski/siamese-triplet#54)
  where the original author of the PR proposes a faster implementation
  for the triplet selector
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants