Facenet:A Unified Embedding for Face Recognition and Clustering

論文提出一種新的loss函數,名叫Triplet Loss。
圖片.png
假設有一張目標臉Anchor,我們的目的是要使得其與相同身份的另一張圖片Positive之間的距離要小於其與不同身份的一張臉Negtive的距離。如上圖所示,剛開始,Anchor和Positive之間的距離要大於Anchor和Negative的距離,經過學習之後使得Anchor和Positive之間的距離小於Anchor和Negative的距離。由此定義損失函數公式如下:
image.png
triplet loss 代碼[1]

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 

訓練方法

因此我們在訓練的時候需要3張圖片爲一組。
有一個問題就是如果我們隨機挑選圖片,會存在選擇的圖片組本身Anchor和Positive之間的距離d(A,P)就小於d(A,N)的情況,這樣的話模型就學不到任何東西,因此我們需要選擇難以訓練的圖片組,也就是d(A,P)>d(A,N),文中稱之爲hard positive/negative examplars, 即Triplet。
有兩種方法來獲取Triplet:
Online:在每一個batch中,先把圖片過一遍神經網絡算出特徵(infrense),然後根據算出來的特徵來構造Triplet,用構造出的Triplet來訓練網絡。
Offline:每n個step後在全部數據集上構造Triplet。
論文中使用的是Online方法。

訓練過程

500個epoch。每一個epoch有1000個batch。一個batch從數據集中隨機提取45個人,每個人40張圖片,共1800張圖片。從這1800張圖片中提取滿足條件的Triplet參與訓練。
Triplet 選擇方法:一個mini-batch中的同label的每一對圖片都構成一對Anchor-Positive;然後再在不同label中隨機選擇一個滿足d(A,N)-α < d(A,P) < d(A,N)的Negative。

疑問:

1。爲什麼hardest negative會導致collapsed model?

Reference:

[1] https://github.com/davidsandberg/facenet
[2] FaceNet—深度學習與人臉識別的二次結合

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章