0. softmax
σ
i
(
z
)
=
e
z
i
∑
j
=
1
m
e
z
j
\sigma_i(z)=\frac{e^{z_i}}{\sum_{j=1}^{m}e^{z_j}}
σi(z)=∑j=1mezjezi
其中, e x e^x ex的作用:
-
对 x x x取exp变为非负数,避免正负值抵消
-
更容易达到终极目标one-hot形式,或者说,softmax降低了训练难度,使得多分类问题更容易收敛。 Softmax鼓励真实目标类别输出比其他类别要大,但并不要求大很多。 (参考链接: https://zhuanlan.zhihu.com/p/34404607 )
1. softmax loss
L
1
=
−
1
m
∑
i
=
1
m
l
o
g
e
W
y
i
T
x
i
+
b
y
i
∑
j
=
1
n
e
W
j
T
x
i
+
b
j
L_1 = - \frac{1}{m}\sum_{i=1}^{m}log\frac{e^{W^T_{y_i}x_i+b_{y_i}}}{\sum_{j=1}^{n} e^{W_j^Tx_i + b_j}}
L1=−m1i=1∑mlog∑j=1neWjTxi+bjeWyiTxi+byi
其中,特征的维度 d d d为512, m m m是batch size; n n n是 class number; x i ∈ R d x_i\in \mathbb{R}^d xi∈Rd表示第 i i i个输入样本,属于 y i y_i yi类;
W j ∈ R d W_j\in \mathbb{R}^{d} Wj∈Rd表示最后一层全连接层的权重 W ∈ R d × n W\in \mathbb{R}^{d\times n} W∈Rd×n的第 j j j列, b ∈ R n b\in \mathbb{R}^n b∈Rn是偏置项。
缺点: softmax loss 没有显式66的优化features,使其对正配对的相似度评分更高,负配对的相似度评分更低,从而导致性能不高。
2.权重归一化 A-Softmax loss
权重归一化做了两件事情(1)
b
j
=
0
b_j=0
bj=0 (2)
W
j
T
x
i
=
∣
∣
W
j
∣
∣
∣
∣
x
i
∣
∣
c
o
s
θ
j
W_j^Tx_i=||W_j||\ ||x_i||\ cos\theta_j
WjTxi=∣∣Wj∣∣ ∣∣xi∣∣ cosθj,并应用L2 normalization使得
∣
∣
W
j
∣
∣
=
1
||W_j||=1
∣∣Wj∣∣=1,这样做的好处是前向过程不再关注权重,仅仅关注特征向量和权重的夹角和特征向量两个因素。
L
2
=
−
1
m
∑
i
=
1
m
l
o
g
e
∣
∣
x
i
∣
∣
c
o
s
θ
y
i
e
∣
∣
x
i
∣
∣
c
o
s
θ
y
i
+
∑
j
=
1
,
j
≠
y
i
n
e
∣
∣
x
j
∣
∣
c
o
s
θ
j
L_2=-\frac{1}{m}\sum_{i=1}^{m}log\frac{e^{||x_i||cos\theta_{y_i}}}{e^{||x_i||cos\theta_{y_i}}+\sum_{j=1,j\ne y_i}^{n}e^{||x_j||cos\theta_j}}
L2=−m1i=1∑mloge∣∣xi∣∣cosθyi+∑j=1,j=yine∣∣xj∣∣cosθje∣∣xi∣∣cosθyi
在SphereFace的论文中,权重的L2normalization仅仅有了一点点提升。
3.角度乘数margin
在 SphereFace 中,角度的margin放在了角度的乘数上
L
3
=
−
1
m
∑
i
−
1
m
l
o
g
e
∣
∣
x
i
∣
∣
c
o
s
(
m
θ
y
i
)
e
∣
∣
x
i
∣
∣
c
o
s
(
m
θ
y
i
)
+
∑
j
=
1
,
j
≠
y
i
n
∣
∣
x
j
∣
∣
c
o
s
θ
j
L_3=-\frac{1}{m}\sum_{i-1}^{m}log\frac{e^{||x_i||cos(m\theta_{y_i})}}{{e^{||x_i||cos(m\theta_{y_i})}}+\sum_{j=1,j\ne y_i}^{n}||x_j||cos\theta_j}
L3=−m1i−1∑mloge∣∣xi∣∣cos(mθyi)+∑j=1,j=yin∣∣xj∣∣cosθje∣∣xi∣∣cos(mθyi)
其中,
θ
y
i
∈
[
0
,
π
/
m
]
\theta_{y_i}\in[0,\pi/m]
θyi∈[0,π/m],为了去除取值范围的限制,论文提出了使用分段单调函数替代
c
o
s
(
m
θ
y
i
)
cos(m\theta_{y_i})
cos(mθyi),该函数
ψ
(
θ
y
i
)
\psi(\theta_{y_i})
ψ(θyi)公式为:
L
4
=
−
1
m
∑
i
=
1
m
l
o
g
e
∣
∣
x
i
∣
∣
ψ
(
θ
y
i
)
e
∣
∣
x
i
∣
∣
ψ
(
θ
y
i
)
+
∑
j
=
1
,
j
≠
y
i
n
e
∣
∣
x
j
∣
∣
c
o
s
θ
j
L_4=-\frac{1}{m}\sum_{i=1}^{m}log\frac{e^{||x_i||\psi(\theta_{y_i})}}{e^{||x_i||\psi(\theta_{y_i})}+\sum_{j=1,j\ne y_i}^{n}e^{||x_j||cos\theta_j}}
L4=−m1i=1∑mloge∣∣xi∣∣ψ(θyi)+∑j=1,j=yine∣∣xj∣∣cosθje∣∣xi∣∣ψ(θyi)
其中,
ψ
(
θ
y
i
)
=
(
−
1
)
k
c
o
s
(
m
θ
y
i
)
−
2
k
,
θ
y
i
∈
[
k
π
m
,
k
π
+
1
m
]
,
k
∈
[
0
,
m
−
1
]
,
m
≥
1
\psi(\theta_{y_i})=(-1)^kcos(m\theta_{y_i})-2k, \ \theta_{y_i}\in[\frac{k\pi}{m}, \frac{k\pi+1}{m}], k\in[0,m-1], m\ge1
ψ(θyi)=(−1)kcos(mθyi)−2k, θyi∈[mkπ,mkπ+1],k∈[0,m−1],m≥1;m是个整数并且表示了角度margin。然而,在SphereFace实现的过程中,加入了softmax的监督,并引入了超参数
λ
\lambda
λ来动态的控制权重。
ψ
(
θ
y
i
)
=
(
−
1
)
k
c
o
s
(
m
θ
y
i
)
−
2
k
+
λ
c
o
s
(
θ
y
i
)
1
+
λ
\psi(\theta_{y_i})=\frac{(-1)^kcos(m\theta_{y_i}) - 2k + \lambda cos(\theta_{y_i})}{1+\lambda}
ψ(θyi)=1+λ(−1)kcos(mθyi)−2k+λcos(θyi)
训练初始的时候
λ
\lambda
λ设置为1000,最后减小到5来保证角空间的相对收缩。引入了超参数增加了训练的复杂性。
4. 特征归一化 AM-Softmax loss
- 特征归一化的操作在人脸比对验证过程中被广泛使用。
- L2 normalization是超球面度量学习的重要步骤
应用L2 normalization 使 得 ∣ ∣ x i ∣ ∣ = 1 使得||x_i||=1 使得∣∣xi∣∣=1,然后乘以一个缩放系数 s s s,这里的 s s s就是超球面的半径。在论文中人脸识别模型训练过程中 s = 64 s=64 s=64,基于权重归一化和特征归一化我们可以得到 W j T = c o s ( θ j ) W_{j}^{T}=cos(\theta_j) WjT=cos(θj).
如果特征归一化应用到SphereFace上,则可以得到对应的形式SPhereFace-FNorm:
L
5
=
−
1
m
∑
i
=
1
m
l
o
g
e
s
ψ
(
θ
y
i
)
e
s
ψ
(
θ
y
i
)
+
∑
j
=
1
,
j
≠
y
i
n
e
s
c
o
s
θ
j
L_5=-\frac{1}{m}\sum_{i=1}^{m}log\frac{e^{s\psi(\theta_{y_i})}}{e^{s\psi(\theta_{y_i})}+\sum_{j=1,j\ne y_i}^{n}e^{scos\theta_j}}
L5=−m1i=1∑mlogesψ(θyi)+∑j=1,j=yinescosθjesψ(θyi)
5. 添加cosine margin Arcface loss
在《Additive margin softmax for face verification》一文中,m被移到了
c
o
s
θ
cos\theta
cosθ的外面,因此提出了cosine margin loss function:
L
6
=
−
1
m
∑
i
=
1
m
l
o
g
e
s
(
c
o
s
θ
y
i
−
m
)
e
s
(
c
o
s
θ
y
i
−
m
)
+
∑
j
=
1
,
j
≠
y
i
e
c
o
s
θ
j
L_6=-\frac{1}{m}\sum_{i=1}^{m}log\frac{e^{s(cos\theta_{y_i}-m)}}{e^{s(cos\theta_{y_i}-m)}+\sum_{j=1,j\ne y_i}e^{cos\theta_j}}
L6=−m1i=1∑mloges(cosθyi−m)+∑j=1,j=yiecosθjes(cosθyi−m)
在paper中,m=0.35,这样修改有3个优点:
- 非常容易实现,不需要复杂的超参数
- 结构更清晰,不需要Softmax监督即可收敛
- 性能明显提高
6. 添加角度margin
虽然,cosine margin从余弦空间到角空间是一对一的映射,但这两个边界之间仍然存在差异。与余弦边距相比,角边距有更清晰的几何解释,角空间中的边距对应于超球面流形上的弧距。
具体做法是,增加角度margin在cos函数里面,因为
c
o
s
(
θ
+
m
)
cos(\theta+m)
cos(θ+m)小于
c
o
s
(
θ
)
cos(\theta)
cos(θ)(
θ
∈
[
0
,
π
−
m
]
\theta\in [0,\pi-m]
θ∈[0,π−m]),所以分类的约束效果会更加强烈。ArcFace loss公式:
L
6
=
−
1
m
∑
i
=
1
m
l
o
g
e
s
(
c
o
s
(
θ
y
i
+
m
)
)
e
s
(
c
o
s
(
θ
y
i
+
m
)
)
+
∑
j
=
1
,
j
≠
y
i
e
c
o
s
θ
j
L_6=-\frac{1}{m}\sum_{i=1}^{m}log\frac{e^{s(cos(\theta_{y_i}+m))}}{e^{s(cos(\theta_{y_i}+m))}+\sum_{j=1,j\ne y_i}e^{cos\theta_j}}
L6=−m1i=1∑mloges(cos(θyi+m))+∑j=1,j=yiecosθjes(cos(θyi+m))
如果我们分解
c
o
s
(
θ
+
m
)
cos(\theta+m)
cos(θ+m),可以得到
c
o
s
(
θ
+
m
)
=
c
o
s
(
θ
)
c
o
s
(
m
)
−
s
i
n
(
θ
)
s
i
n
(
m
)
cos(\theta+m)=cos(\theta) cos(m)-sin(\theta) sin(m)
cos(θ+m)=cos(θ)cos(m)−sin(θ)sin(m),这其实与
c
o
s
θ
−
m
cos\theta-m
cosθ−m类似,但是不同的是
s
i
n
(
θ
)
sin(\theta )
sin(θ)是动态的。
参考链接:
https://www.groundai.com/project/arcface-additive-angular-margin-loss-for-deep-face-recognition/1