Skip to content

Train with known triplets #695

Answered by KevinMusgrave
swarmt asked this question in Q&A
Apr 30, 2024 · 1 comments · 7 replies
Discussion options

You must be logged in to vote

Pairs or triplets are formed based on labels. So when you load a batch of image pairs, give each image in the pair the same label.

For example, let's say you have a batch of 4 images, where images 0 and 1 are a pair and images 2 and 3 are a pair:

from pytorch_metric_learning.losses import ContrastiveLoss

loss_fn = ContrastiveLoss()

embeddings = model(images)
labels = torch.tensor([0,0,1,1])
loss = loss_fn(embeddings, labels)

If each image in each batch has exactly 1 positive, then you can accomplish the same thing with SelfSupervisedLoss without creating labels:

from pytorch_metric_learning.losses import ContrastiveLoss, SelfSupervisedLoss

loss_fn = SelfSupervisedLoss(ContrastiveLoss())

Replies: 1 comment 7 replies

Comment options

You must be logged in to vote
7 replies
@KevinMusgrave
Comment options

@swarmt
Comment options

@swarmt
Comment options

@swarmt
Comment options

@KevinMusgrave
Comment options

Answer selected by swarmt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants