adambielski / siamese-triplet

Siamese and triplet networks with online pair/triplet mining in PyTorch
BSD 3-Clause "New" or "Revised" License
3.1k stars 633 forks source link

Faster triplet selector #54

Open Lucashsmello opened 4 years ago

Lucashsmello commented 4 years ago

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 commented 4 years ago

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 commented 6 months ago

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