CV之FR之ME/LF:人脸识别中常用的模型评估指标/损失函数(Triplet Loss、Center Loss)简介、使用方法之详细攻略
目录
人脸识别中常用的模型评估指标/损失函数(Triplet Loss、Center Loss)简介
T1、Triplet Loss
(1)、英文原文解释
(2)、代码实现
T2、Center loss
(1)、英文原文解释
(2)、代码实现
人脸识别中常用的模型评估指标/损失函数(Triplet Loss、Center Loss)简介 T1、Triplet Loss《FaceNet: A Unified Embedding for Face Recognition and Clustering》https://arxiv.org/pdf/1503.03832.pdfhttp://www.goodtimesweb.org/surveillance/2015/1503.03832v1.pdf
三联体损耗嵌入由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)中的约束条件)。这些三胞胎将不会有助于训练,并导致较慢的收敛,因为他们仍然会通过网络。关键是要选择硬三胞胎,这是积极的,因此可以有助于改善模型。下面的部分将讨论我们在三重选择中使用的不同方法。
triplet_loss
(anchor, positive, negative, alpha): #(随机选取的人脸样本的特征,anchor的正、负样本的特征)
#它们的形状都是(batch_size,feature_size),feature_size是网络学习的人脸特征的维数
"""Calculate the triplet loss according to the FaceNet paper
with 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
center_loss
features, 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)) #计算loss
return loss, centers #返回loss和更新后的中心