Siamese Networks with Triplet or Constrastive loss

Hi there,

Has anyone been able to successfully implement Siamese networks using fastai for images using triplet or contrastive loss. I have been working on this for a while now with little success.

Thank you in advance.

1 Like

I have been looking at this implementation right now.

1 Like

Thank you for the response, the linked implementation is for BCE loss, which works different from triplet or contrastive loss.

Correct me if I am wrong, but from what I understand, by definition it wouldn’t be a siamese network. Siamese network takes in two images, while a triplet network using a triplet loss takes in three.

You could easily extend the above linked network to take in three images and replace the loss function with a triplet loss function.

I was intending to use triplet loss, by having two pairs of images, one pair consisting of an anchor image and another image in the same classes, the other pair consisting of an anchor image and an image of a different class. Then optimizing the distance between the distances between the pairs.

Similar to this implementation:

Furthermore, when attempting to replace the loss function, methods like get_pred() seems to not work correctly.

This is one way to do it, but I don’t see what’s wrong with using a triplet network approach:
Use the same anchor image, and have the positive and negative images and use the triplet loss.

It will be much easier to implement a triplet network compared to trying to hack a siamese network to do it.

1 Like

It looks like the new pipeline class should be able to do this but I dont know about the details. So excited to learn more!

Hi. I am playing around with the Siamese Network and take advantage of this topic to ask a question for people with more experience.

How do you chose the margin for contrastive loss ? I found it very difficult to train my Siamese network. After 1, 2 epochs, the loss stop reducing. I am trying to finetune the back bone network (resnet34) and get very good accuracy (1% error rate) then load it to use the Siamese network. However, it seems that the problems is still there.

If I choose the margin small (like 1.), the dissimilar pair get small distance also.


I think I know my problem :smiley: . The code is based on this notebook . However, he label 1 with sample from the same class and 0 for others which is I think different from others sources that I read (0 for same class and 1 for other). I will continue investigate on Siamese network.

I am still curious about the margin and appreciate if someone can shed me a light on it. In addition, how can we choose a threshold for predicting similar or dissimilar. I know it should be from 0 to margin, but what is a clever way to choose it

1 Like

Did you ever get any progress made on this? I opened a thread on a similar topic (“Modding Siamese example for metric learning?”) because i didn’t see any examples that worked with fastaiv2 (e.g. that humpback whale example seems to be fastaiv1).

We do have a tutorial for Siamese for the new fastai (or fastaiv2) right ?

Yes but it doesn’t use any contrastive or triplet loss.

Ah ok. I made it works before with fastaiv1. Haven’t tried it yet for v2 but I’m reading the course now. Maybe I will try this later

1 Like