FaceNet Triplet loss メモ (途中)
サンプルの組みごとにlossを計算する。
GoogleのFaceNetをベースにした
GitHub - davidsandberg/facenet: Face recognition using Tensorflow
で書かれているTriplet lossを確認してみた。
def triplet_loss(anchor, positive, negative, alpha): """Calculate the triplet loss according to the FaceNet paper Args: anchor: the embeddings for the anchor images. positive: the embeddings for the positive images. negative: the embeddings for the negative images. Returns: the triplet loss according to the FaceNet paper as a float tensor. """ with tf.variable_scope('triplet_loss'): pos_dist = tf.reduce_sum(tf.square(tf.subtract(anchor, positive)), 1) neg_dist = tf.reduce_sum(tf.square(tf.subtract(anchor, negative)), 1) basic_loss = tf.add(tf.subtract(pos_dist,neg_dist), alpha) loss = tf.reduce_mean(tf.maximum(basic_loss, 0.0), 0) return loss
https://github.com/davidsandberg/facenet/blob/master/src/facenet.py#L44を参照
論文ではlossの計算でbasic_lossの総和を取っていたがここでは平均になっている。しかし、sumでもmeanでも結果は変わらないと思う。
最後のtf.maximumではbasic_lossか0を取っていた。basic_lossが下がりすぎると過学習するので、0を設定していると思う。
これにより、他のサンプルの組のbasic_lossを下げようとするため汎用的なmodelが作成できるはず(予想)。
参考
https://arxiv.org/pdf/1503.03832.pdf FaceNet 論文
future-architect.github.io
github.com