几种人脸识别的loss和改进思路

1. 大致演进过程

softmax -> 基于欧式空间 -> 基于softmax变体 -> 基于角度空间 [1]

  • Softmax: (DeepID, DeepFace)
  • 欧式空间:对比损失、triplet loss、center loss 以及 triplet loss和softmax结合 (DeepID 2+, DeepID3, FaceNet, VggFace, TSE, TPE, Range loss)
  • softmax变体:特征归一化、权重归一化 (NormFace, L2 softmax, CoCo loss, vMF loss)
  • 角度空间:large margin (L softmax, A softmax(Sphereface) , Cos/Arc/Regular/Adaptive face, AMS loss, Adacos)

2. 关于角度

角度 θ \theta θ指的是 归一化后的输入x 和归一化之后的 类别权重w(类中心)之间的夹角
W*X = ||W|| ||X|| cos θ \theta θ
W(类中心)如何获得:训练好的网络的最后一层全连接层的每个结点的权重

3. 常见的人脸识别相关的softmax loss

3.0 原始softmax loss

单个样本的交叉熵:
L o s s i = − ∑ j = 1 K y j l o g ( p j ) Loss_i = -\sum_{j=1}^{K} y_j log(p_j) Lossi​=−j=1∑K​yj​log(pj​)
当类别 j j j 是当前样本GT类别时, y j y_j yj​ = 1,其余 y j y_j yj​ = 0。 所以可以简化为 L = − l o g ( p ) L=-log(p) L=−log(p)是关于p的单调递减函数。


一个 batch 的交叉熵:
L o s s = − 1 N ∑ i = 1 N l o g e s cos ⁡ ( θ w y , x ) e s c o s ( θ w y , x ) + ∑ k ≠ y K e s cos ⁡ ( θ w k , x ) Loss=-\frac{1}{N} \sum^N_{i=1}{log\frac{e^{s\cos(\theta_{w_{y},x})}} {e^{scos(\theta_{w_{y},x})}+\sum^{K}_{k\neq y}{e^{s\cos(\theta_{w_{k},x})}}}} Loss=−N1​i=1∑N​logescos(θwy​,x​)+∑k​=yK​escos(θwk​,x​)escos(θwy​,x​)​

总共N个样本,K个类别; w T x = c o s θ w^Tx=cos\theta wTx=cosθ,w是最后分类的全连接层权重,x是归一化后的输入全连接层的特征, c o s θ cos\theta cosθ是余弦相似度。s是缩放系数(如64),将归一化后的特征统一缩放到长度为s

3.1 基于挖掘的 [mining based]

思想:对分类概率 p 做文章,分类时强调难样本的重要性。什么是难样本:预测的所在正确类别的概率p较小的样本。

  • 原始softmax: L = − l o g ( p ) L = -log(p) L=−log(p),p是样本属于GT类别的概率,p增大,L减小。
3.1.1 Hard mining
  • L = − I ∗ l o g ( p ) L = -I * log(p) L=−I∗log(p),p较小的属于难样本, I I I为指示函数,难样本的 I = 1 I=1 I=1,容易样本 I = 0 I=0 I=0
3.1.2 Soft mining - focal loss

focal loss,将 I I I换成 ( 1 − p ) γ (1-p)^\gamma (1−p)γ, L = − ( 1 − p ) γ ∗ l o g ( p ) L=-(1-p)^\gamma*log(p) L=−(1−p)γ∗log(p), γ \gamma γ是调制系数


3.2 基于间隔的 [margin based]

思想:对 c o s θ cos\theta cosθ 做文章,增大不同类别特征之间的间隔,让分类变难(更严格)

如何理解更严格:考虑简单的二分类,类中心分别为 w 1 w_{1} w1​和 w 2 w_{2} w2​。假设有样本 x 1 x_{1} x1​属于类别 w 1 w_{1} w1​,正常情况下把样本分对,只需要 w 1 T x 1 > w 2 T x 1 w_{1}^Tx_{1}>w_{2}^Tx_{1} w1T​x1​>w2T​x1​, c o s θ 1 > c o s θ 2 cos\theta_{1}>cos\theta_{2} cosθ1​>cosθ2​,但是基于margin的softmax loss不是直接使用 c o s θ 1 cos\theta_{1} cosθ1​,而是使用 c o s ( m 1 θ 1 + m 2 ) − m 3 cos(m_{1}\theta_{1}+m_{2})-m_{3} cos(m1​θ1​+m2​)−m3​的形式, 其中 m 1 ≥ 1 , m 2 > 0 , m 3 > 0 m_{1}\geq1, m_{2}>0, m_{3}>0 m1​≥1,m2​>0,m3​>0。因为 c o s θ 1 ≥ c o s ( m 1 θ 1 + m 2 ) − m 3 cos\theta_{1}\geq cos(m_{1}\theta_{1}+m_{2})-m_{3} cosθ1​≥cos(m1​θ1​+m2​)−m3​,它们要求 c o s ( m 1 θ 1 + m 2 ) − m 3 ≥ c o s θ 2 cos(m_{1}\theta_{1}+m_{2})-m_{3}\geq cos\theta_{2} cos(m1​θ1​+m2​)−m3​≥cosθ2​,把左侧的值进一步降低,最终实现的效果是: c o s θ 1 ≥ c o s ( m 1 θ 1 + m 2 ) − m 3 ≥ c o s θ 2 cos\theta_{1}\geq cos(m_{1}\theta_{1}+m_{2})-m_{3}\geq cos\theta_{2} cosθ1​≥cos(m1​θ1​+m2​)−m3​≥cosθ2​。

3.2.1 Angular margin (A-softmax)
  • 原理: c o s ( m θ ) cos(m\theta) cos(mθ) , m ≥ 1 m\geq1 m≥1 增大输入特征和当前GT类特征之间的夹角带来的影响(要求在分对的情况下,输入特征和类中心具有更小的夹角,相当于减小夹角,减小类内差异)
  • 此时正确分类需要满足: c o s θ 1 ≥ c o s ( m θ 1 ) ≥ c o s θ 2 cos\theta1 \geq cos(m\theta1) \geq cos\theta2 cosθ1≥cos(mθ1)≥cosθ2,相当于分对的条件变难了。
3.2.2 Additive margin (AM-softmax)
  • 原理: c o s θ − m cos\theta-m cosθ−m ,直接减小余弦相似度
3.2.3 Additive angular marign (Arc-softmax)
  • 原理 : c o s ( θ + m ) cos(\theta+m) cos(θ+m),也是相当于减小夹角,减小类内差异。m比如取0.5
  • 公式:
    L o s s = − 1 N ∑ N l o g e s cos ⁡ ( θ + m ) e s c o s ( θ + m ) + ∑ K e s cos ⁡ θ Loss=-\frac{1}{N} \sum^N{log\frac{e^{s\cos(\theta+m)}} {e^{scos(\theta+m)}+\sum^K{e^{s\cos\theta}}}} Loss=−N1​∑N​logescos(θ+m)+∑Kescosθescos(θ+m)​
  • 为什么使用 c o s ( θ + m ) cos(\theta+m) cos(θ+m),不使用 c o s ( m θ ) cos(m\theta) cos(mθ) :
  1. 输入范围发生变化, c o s ( m θ ) cos(m\theta) cos(mθ) 使输入区间变小,导致很难学, c o s ( θ + m ) cos(\theta+m) cos(θ+m)主要是使输入范围发生平移,没有改变区间大小
  2. 如果一个分对的样本、一个分错的样本,两个样本与类中心的夹角都比较小(离分界面都很近),乘性margin可能处理完还是很小(还是很近),而加性margin能明显使角度变大(更远离分界面),比如 0.001 + 2 比 0.001 x 2更有区分度。

3.3 同时结合挖掘和间隔的 [mining and margin based]

3.3.1 Mis-classified vector guided softmax loss (MV-softmax)
  • 原理:将3.2基于间隔的方法对 c o s θ w y , x cos\theta_{w_{y},x} cosθwy​,x​ 的改造定义为 f ( θ w y , x , m ) f(\theta_{w_{y},x},m) f(θwy​,x​,m),比如 f ( θ w y , x , m ) = c o s ( m 1 θ w y , x + m 2 ) − m 3 f(\theta_{w_{y},x},m)=cos(m_{1}\theta_{w_{y},x}+m_{2})-m_{3} f(θwy​,x​,m)=cos(m1​θwy​,x​+m2​)−m3​的形式。统一了基于间隔的方法公式。另外,与基于margin的方法不同,MV-softmax:
  1. 难样本定义为错分的样本
  2. 因为softmax loss的大体形式为 − l o g ( p ) -log(p) −log(p)是关于p的单调递减函数,MV-softmax在分母上强调难样本的贡献(乘以系数 h ≥ 1 h\geq 1 h≥1),增大分母,减小p的值,从而增大难样本的loss。
  3. 不是固定地使用某个margin值,而是不同样本取不同margin。也就是后面的 h = e s t ( c o s θ w k , x + 1 ) I k h = e^{st(cos\theta_{w_{k},x}+1)I_{k}} h=est(cosθwk​,x​+1)Ik​
  • 公式:MV-softmax将基于margin的softmax loss改造为:
    L o s s = − 1 N ∑ N l o g e s f ( θ w y , x , m ) e s f ( θ w y , x , m ) + ∑ k ≠ y K h ∗ e s cos ⁡ θ w k , x Loss=-\frac{1}{N} \sum^N{log\frac{e^{sf(\theta_{w_{y},x},m)}} {e^{sf(\theta_{w_{y},x},m)}+\sum^K_{k\neq y}h*{e^{s\cos\theta_{w_{k},x}}}}} Loss=−N1​∑N​logesf(θwy​,x​,m)+∑k​=yK​h∗escosθwk​,x​esf(θwy​,x​,m)​

设计的 h ≥ 1 h\geq 1 h≥1; h = e s t I k h = e^{stI_{k}} h=estIk​ 或 h = e s t ( c o s θ w k , x + 1 ) I k , k ≠ y h = e^{st(cos\theta_{w_{k},x}+1)I_{k}}, k\neq y h=est(cosθwk​,x​+1)Ik​,k​=y,实际使用的是后面这个可调节的表达式,分错的角度 θ w k , x \theta_{w_{k},x} θwk​,x​越大,loss越大,惩罚越大。其中 t 是超参数,可以取0.2; I k I_{k} Ik​为指示函数,
I k = { 0 , f ( θ w y , x , m ) ≥ cos ⁡ ( θ w k , x ) 分 对 的 情 况 下 1 , f ( θ w y , x , m ) < cos ⁡ ( θ w k , x ) 分 错 的 情 况 下 I_{k}=\left\{\begin{aligned} & 0, & f(\theta_{w_{y},x},m) \geq \cos (\theta_{w_{k},x}) 分对的情况下 \\ & 1, & f(\theta_{w_{y},x},m) < \cos (\theta_{w_{k},x}) 分错的情况下 \\ \end{aligned}\right. Ik​={​0,1,​f(θwy​,x​,m)≥cos(θwk​,x​)分对的情况下f(θwy​,x​,m)<cos(θwk​,x​)分错的情况下​

  • 实现:mv-arc-softmax: https://github.com/xiaoboCASIA/SV-X-Softmax/blob/master/fc_layers.py
3.3.2 Circle loss

原理:提出基于类内相似度和类间相似度的loss形式,号称统一了类别学习(proxy-based)和对比学习(pair-wise based, no proxy),在训练过程中,同时强调正样本和负样本。
公式:
设计的
L u n i = l o g [ 1 + ∑ i K ∑ j L e γ ( s n j − s p i + m ) ] L_{uni}=log[1+\sum^{K}_{i} \sum^{L}_{j} e^{\gamma (s^{j}_{n}-s^{i}_{p}+m)}] Luni​=log[1+i∑K​j∑L​eγ(snj​−spi​+m)]

s p s_{p} sp​和 s n s_{n} sn​分别代表类内相似度和类间相似度,类内相似度有K个,类间相似度有L个。
γ \gamma γ 是缩放因子, m m m是用于相似度分隔的margin

  • 退化成基于类别的loss (AM-softmax):
    N个类别的情况下,对每个样本,类内相似度就一个 w y T x / ( ∣ ∣ w ∣ ∣ ∗ ∣ ∣ x ∣ ∣ ) w^{T}_{y}x/(||w||*||x||) wyT​x/(∣∣w∣∣∗∣∣x∣∣),K=1;类间相似度有N-1个,L=N-1。详细推导步骤:

L u n i = l o g [ 1 + ∑ j N − 1 e γ ( s n j − s p + m ) ] = l o g [ e γ ( s p − m ) e γ ( s p − m ) + e γ ( s p − m ) ∗ ∑ j N − 1 e γ ( s n j − s p + m ) e γ ( s p − m ) ] = l o g [ e γ ( s p − m ) + ∑ j N − 1 e γ s n j e γ ( s p − m ) ] = − l o g e γ ( s p − m ) e γ ( s p − m ) + ∑ j N − 1 e γ s n j \begin{aligned} L_{uni} = & log[1+ \sum^{N-1}_{j} e^{\gamma (s^{j}_{n}-s_{p}+m)}] \\ = & log[\frac{e^{\gamma (s_{p}-m)}}{e^{\gamma (s_{p}-m)}} + \frac {e^{\gamma (s_{p}-m)} * \sum^{N-1}_{j} e^{\gamma (s^{j}_{n}-s_{p}+m)} }{e^{\gamma (s_{p}-m)}}] \\ = & log[\frac {e^{\gamma (s_{p}-m)} + \sum^{N-1}_{j} e^{\gamma s^{j}_{n}}}{e^{\gamma (s_{p}-m)}}] \\ = & -log \frac {e^{\gamma (s_{p}-m)}} {e^{\gamma (s_{p}-m)} + \sum^{N-1}_{j} e^{\gamma s^{j}_{n}}} \end{aligned} Luni​====​log[1+j∑N−1​eγ(snj​−sp​+m)]log[eγ(sp​−m)eγ(sp​−m)​+eγ(sp​−m)eγ(sp​−m)∗∑jN−1​eγ(snj​−sp​+m)​]log[eγ(sp​−m)eγ(sp​−m)+∑jN−1​eγsnj​​]−logeγ(sp​−m)+∑jN−1​eγsnj​eγ(sp​−m)​​

  • 退化成基于样本对的loss (triple loss):
    详细推导步骤不太清楚。
    L t r i p l e = lim ⁡ γ → + ∞ 1 γ L u n i = m a x [ s n j − s p i ] + L_{triple} = \lim_{\gamma \rightarrow + \infty} \frac {1}{\gamma} L_{uni}= max[s^j_{n} - s^i_{p}]_{+} Ltriple​=γ→+∞lim​γ1​Luni​=max[snj​−spi​]+​

  • 让类间相似度和类内相似度根据各自当前状态分别进行优化(根据自己的节奏),即分别乘以 α n \alpha_{n} αn​和 α p \alpha_{p} αp​,忽略 m m m, 最终使用的公式(简化形式)为:
    L c i r c l e = l o g [ 1 + ∑ i K ∑ j L e γ ( α n j s n j − α p i s p i ) ] L_{circle} = log[1+\sum^{K}_{i} \sum^{L}_{j} e^{\gamma (\alpha ^{j}_{n} s^{j}_{n} - \alpha ^{i}_{p} s^{i}_{p})}] Lcircle​=log[1+i∑K​j∑L​eγ(αnj​snj​−αpi​spi​)]
    其中, α p = 1 + m − s p \alpha_{p} = 1+m-s_{p} αp​=1+m−sp​, α n = s n + m \alpha_{n}=s_{n} + m αn​=sn​+m。根据经验只使用 α p \alpha_{p} αp​,m设置为0.1,同时结合MV-softmax使用(相当于 α n \alpha_{n} αn​对应的效果)。

详细推导过程见论文,也不太清楚。



参考资料:
use large pose dataset to improve acc on cox cam2
[1] Mei Wang & Weihong Deng, Deep Face Recognition: A Survey, Neurocomputing
https://zhuanlan.zhihu.com/p/76541084

http://www.cbsr.ia.ac.cn/users/xiangyuzhu/projects/3DDFA/main.htm

https://resources.wolframcloud.com/NeuralNetRepository/resources/2D-Face-Alignment-Net-Trained-on-300W-Large-Pose-Data

http://www.cbsr.ia.ac.cn/users/xiangyuzhu/projects/3DDFA/Database/300W-LP/main.htm

[300W-3D]: https://drive.google.com/file/d/0B7OEHD3T4eCkRFRPSXdFWEhRdlE/view?usp=sharing [300W-3D-Face]: https://drive.google.com/file/d/0B7OEHD3T4eCkZmgzUWZfd2FVVWs/view?usp=sharing [300W-LP]: https://drive.google.com/file/d/0B7OEHD3T4eCkVGs0TkhUWFN6N1k/view?usp=sharing