CV之FRec之ME/LF:人脸识别中常用的模型评估指标/损失函数(Triplet Loss、Center Loss)简介、使用方法之详细攻略
CV之FRec之ME/LF:人臉識別中常用的模型評估指標/損失函數(Triplet Loss、Center Loss)簡介、使用方法之詳細攻略
目錄
T1、Triplet Loss
1、英文原文解釋
2、代碼實現
T2、Center loss
1、英文原文解釋
2、代碼實現
T1、Triplet Loss
《FaceNet: A Unified Embedding for Face Recognition and Clustering》
https://arxiv.org/pdf/1503.03832.pdf
http://www.goodtimesweb.org/surveillance/2015/1503.03832v1.pdf
1、英文原文解釋
| ? ? ? ?Triplet Loss The embedding is represented by f(x) ∈ R d . It embeds an image x into a d-dimensional Euclidean space. Additionally, we constrain this embedding to live on the d-dimensional hypersphere, i.e. kf(x)k2 = 1. This loss is motivated in [19] in the context of nearest-neighbor classifi- cation. Here we want to ensure that an image x a i (anchor) of a specific person is closer to all other images x p i (positive) of the same person than it is to any image x n i (negative) of any other person. This is visualized in Figure 3. Thus we want, kx a i ? x p i k 2 2 + α < kx a i ? x n i k 2 2 , ? (x a i , x p i , xn i ) ∈ T , (1) | 三聯體損耗嵌入由f(x)∈R d表示。它將圖像x嵌入到d維歐幾里得空間中。另外,我們將這個嵌入限制在d維超球面上,即kf(x)k2 = 1。這種損失是在[19]中最近鄰分類的背景下產生的。在這里,我們要確保一個特定的人的圖像x a i(錨點)是更接近所有其他圖像x p i(積極的)是同一個人比它是任何圖像x n i(消極的)任何其他的人。如圖3所示。因此我們希望,kx 2我?x p k 2 +α< kx 2?x n我k 2,?(x, x p i, xn i)∈T (1) |
| ? ? ? ?where α is a margin that is enforced between positive and negative pairs. T is the set of all possible triplets in the training set and has cardinality N. The loss that is being minimized is then L = X N i h kf(x a i ) ? f(x p i )k 2 2 ? kf(x a i ) ? f(x n i )k 2 2 + α i + . (2) Generating all possible triplets would result in many triplets that are easily satisfied (i.e. fulfill the constraint in Eq. (1)). These triplets would not contribute to the training and result in slower convergence, as they would still be passed through the network. It is crucial to select hard triplets, that are active and can therefore contribute to improving the model. The following section talks about the different approaches we use for the triplet selection. | 其中α是一個利潤率之間執行積極的和消極的對。T是在訓練集的集合所有可能的三胞胎,基數N的損失最小化是我L = X N h kf (X我)?f (X p i) k 2 2?kf (X我)?f (X N i) k 2 + 2 +α。(2)生成所有可能的三胞胎會產生許多容易滿足的三胞胎(即滿足式(1)中的約束條件)。這些三胞胎將不會有助于訓練,并導致較慢的收斂,因為他們仍然會通過網絡。關鍵是要選擇硬三胞胎,這是積極的,因此可以有助于改善模型。下面的部分將討論我們在三重選擇中使用的不同方法。 |
2、代碼實現
triplet_loss (anchor, positive, negative, alpha): #(隨機選取的人臉樣本的特征,anchor的正、負樣本的特征)#它們的形狀都是(batch_size,feature_size),feature_size是網絡學習的人臉特征的維數"""Calculate the triplet loss according to the FaceNet paperwith tf.variable_scope('triplet_loss'):pos_dist = tf.reduce_sum(tf.square(tf.subtract(anchor, positive)), 1)#pos_dist就是anchor到各自正樣本之間的距離neg_dist = tf.reduce_sum(tf.square(tf.subtract(anchor, negative)), 1)#neg_dist是anchor到負樣本的距離basic_loss = tf.add(tf.subtract(pos_dist,neg_dist), alpha)#用pos_dist減去neg_dist再加上一個alpha,最終損失只計算大于0的部分loss = tf.reduce_mean(tf.maximum(basic_loss, 0.0), 0)T2、Center loss
《A Discriminative Feature Learning Approach for Deep Face Recognition》
http://ydwen.github.io/papers/WenECCV16.pdf
1、英文原文解釋
| ? ? ? The Center Loss So, how to develop an effective loss function to improve the discriminative power of the deeply learned features? Intuitively, minimizing the intra-class variations while keeping the features of different classes separable is the key. To this end, we propose the center loss function, as formulated in Eq. 2. LC = 1 2 m i=1 xi ? cyi 2 2 (2) | 那么,如何建立一個有效的損失函數來提高學習特征的辨別力呢?直觀地說,在保持不同類的特性可分離的同時最小化類內的變化是關鍵。為此,我們提出了如式2所示的中心損失函數。LC =1 2 m i=1 xi?cyi 2 2 (2) ? |
| ?? ??The cyi ∈ Rd denotes the yith class center of deep features. The formulation effectively characterizes the intra-class variations. Ideally, the cyi should be updated as the deep features changed. In other words, we need to take the entire training set into account and average the features of every class in each iteration, which is inefficient even impractical. Therefore, the center loss can not be used directly. This is possibly the reason that such a center loss has never been used in CNNs until now. ? ? ? To address this problem, we make two necessary modifications. First, instead of updating the centers with respect to the entire training set, we perform the update based on mini-batch. In each iteration, the centers are computed by averaging the features of the corresponding classes (In this case, some of the centers may not update). Second, to avoid large perturbations caused by few mislabelled samples, we use a scalar α to control the learning rate of the centers. | cyi∈Rd表示深度特征的yith類中心。這個公式有效地描述了階級內部的變化。理想情況下,cyi應該隨著深度特性的變化而更新。換句話說,我們需要考慮整個訓練集,并在每次迭代中平均每個類的特性,這是低效甚至不切實際的。因此,中心損失不能直接使用。這可能就是為什么在CNNs中從未使用過這種中心丟失的原因。為了解決這個問題,我們做了兩個必要的修改。首先,我們不是根據整個訓練集更新中心,而是基于mini-batch執行更新。在每次迭代中,通過平均對應類的特性來計算中心(在這種情況下,一些中心可能不會更新)。第二,避免大擾動引起的幾貼樣品,我們用一個標量α控制中心的學習速率。 |
2、代碼實現
center_lossfeatures, label, alfa, nrof_classes#features是樣本的特征,形狀為(batch size,feature size) nrof_features = features.get_shape()[1] #nrof_features就是feature_size ,即神經網絡計算人臉的維數#centers為變量,它是各個類別對應的類別中心centers = tf.get_variable('centers', [nrof_classes, nrof_features], dtype=tf.float32,initializer=tf.constant_initializer(0), trainable=False)label = tf.reshape(label, [-1])centers_batch = tf.gather(centers, label) #根據label,取出features中每一個樣本對應的類別中心#centers_batch應該和features的形狀一致,為(batch size,feature size)diff = (1 - alfa) * (centers_batch - features) #計算類別中心和各個樣本特征的差距diff,diff用來更新各個類別中心的位置,計算diff時用到的alfa是一個超參數,它可以控制中心位置的更新幅度centers = tf.scatter_sub(centers, label, diff) #diff來重新中心loss = tf.reduce_mean(tf.square(features - centers_batch)) #計算lossreturn loss, centers #返回loss和更新后的中心總結
以上是生活随笔為你收集整理的CV之FRec之ME/LF:人脸识别中常用的模型评估指标/损失函数(Triplet Loss、Center Loss)简介、使用方法之详细攻略的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Dataset:数据集集合(CV方向数据
- 下一篇: 成功解决Scrapy框架的问题Modul