Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I implement the Triplet Loss in Keras?

Tags:

python

keras

I'm trying to implement Google's Facenet paper:

enter image description here

First of all, is it possible to implement this paper using the Sequential API of Keras or should I go for the Graph API?

In either case, could you please tell me how do I pass the custom loss function tripletLoss to the model compile and how do I receive the anchor embedding, positive embedding and the negative embedding as parameters to calculate the loss?

Also, what should be the second parameter Y in model.fit(), I do not have any in this case...

like image 259
Prajwal K R Avatar asked Oct 11 '16 06:10

Prajwal K R


People also ask

How do you prepare data for triplet loss?

Training Data Preparation: For Triplet Loss, the objective is to build triplets <anchor, positive, negative> consisting of an anchor image, a positive image (which is similar to the anchor image), and a negative image (which is dissimilar to the anchor image).

How does Python implement triplet loss?

Triplet Loss can be implemented directly as a loss function in the compile method, or it can be implemented as a merge mode with the anchor, positive and negative embeddings of three individual images as the three branches of the merge function.


1 Answers

This issue explains how to create a custom objective (loss) in Keras:

def dummy_objective(y_true, y_pred):
    return 0.5  # your implem of tripletLoss here

model.compile(loss=dummy_objective, optimizer='adadelta')

Regarding the y parameter of .fit(), since you are the one handling it in the end (the y_true parameter of the objective function is taken from it), I would say you can pass whatever you need that can fit through Keras plumbing. And maybe a dummy vector to pass dimension checks if your really don't need any supervision.

Eventually, as to how to implement this particular paper, looking for triplet or facenet in Keras doc didn't return anything. So you'll probably have to either implement it yourself or find someone who has.

like image 165
Arnaud P Avatar answered Sep 21 '22 19:09

Arnaud P