I am trying to implement facenet in Keras with Thensorflow backend and I have some problem with the triplet loss.
I call the fit function with 3*n number of images and then I define my custom loss function as follows:
def triplet_loss(self, y_true, y_pred):
embeddings = K.reshape(y_pred, (-1, 3, output_dim))
positive_distance = K.mean(K.square(embeddings[:,0] - embeddings[:,1]),axis=-1)
negative_distance = K.mean(K.square(embeddings[:,0] - embeddings[:,2]),axis=-1)
return K.mean(K.maximum(0.0, positive_distance - negative_distance + _alpha))
self._model.compile(loss=triplet_loss, optimizer="sgd")
self._model.fit(x=x,y=y,nb_epoch=1, batch_size=len(x))
Run Code Online (Sandbox Code Playgroud)
where y is just a dummy array filled with …