Paper-info
- title : SphereFace: Deep Hypersphere Embedding for Face Recognition[CVPR-2017]
- author : Weiyang Liu, Yandong Wen, Zhiding Yu, Ming Li, Bhiksha Raj, Le Song.
- insight : angular softmax (A-Softmax) loss.
- github : metricface-pytorch
Motivation
- prior : 人脸位于流行结构上.
- 基于角度的softmax损失函数本质是:在超球面上加入了区分约束,这与先验知识非常匹配!
Details
- 归一化W,同时让bias置零
- 优化内积 --> 优化角度
- decision boundary更为简洁明了,人脸分布于空间超球面上!
- cos(mθ1)>cos(θ2)
1. 为了保证cos(m*theta)单调递减,需要对theta的取值范围加以约束,为了消除这种约束,采用如下的方式来替代cos:
,
# 根据m*theta的取值范围,找到对应的k,使得cos(m*theta)在对应的区间单调
theta = cos_theta.data.acos()
k = (self.m * theta / 3.14159265).floor() # k.shape == theta.shape
Sphereface-pytorch
class Sphereface(nn.Module):
def __init__(self, in_features, out_features, m = 4, use_gpu = False):
super(Sphereface, self).__init__()
self.in_features = in_features
self.out_features = out_features
self.m = m
self.base = 1000.0
self.gamma = 0.12
self.power = 1
self.LambdaMin = 5.0
self.iter = 0
self.weight = torch.FloatTensor(out_features, in_features)
if use_gpu:
self.weight = self.weight.cuda()
self.weight = Parameter(self.weight)
nn.init.xavier_uniform_(self.weight)
self.mlambda = [
lambda x: x ** 0,
lambda x: x ** 1,
lambda x: 2 * x ** 2 - 1,
lambda x: 4 * x ** 3 - 3 * x,
lambda x: 8 * x ** 4 - 8 * x ** 2 + 1,
lambda x: 16 * x ** 5 - 20 * x ** 3 + 5 * x
]
def forward(self, input, target):
self.iter += 1
self.lamb = max(self.LambdaMin, self.base * (1 + self.gamma * self.iter) ** (-1 * self.power))
cos_theta = F.linear(F.normalize(input), F.normalize(self.weight))
cos_theta = cos_theta.clamp(-1, 1)
cos_m_theta = self.mlambda[self.m](cos_theta)
theta = cos_theta.data.acos()
k = (self.m * theta / 3.14159265).floor()
phi_theta = ((-1.0) ** k) * cos_m_theta - 2 * k
NormOfFeature = torch.norm(input, 2, 1)
one_hot = torch.zeros(cos_theta.size(), device=input.device)
one_hot.scatter_(1, target.view(-1, 1), 1)
output = (one_hot * (phi_theta - cos_theta) / (1 + self.lamb)) + cos_theta
output *= NormOfFeature.view(-1, 1)
return output
Experiments
- Sphere-3D-CASIA_WebFace
- LFW/YTF